OpendTect-6_4  6.4
trigonometry.h
Go to the documentation of this file.
1 #ifndef trigonometry_h
2 #define trigonometry_h
3 
4 /*+
5 ________________________________________________________________________
6 
7  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
8  Author: Kristofer Tingdahl
9  Date: 23-11-2002
10  RCS: $Id$
11 ________________________________________________________________________
12 
13 
14 -*/
15 
16 #include "algomod.h"
17 #include "coord.h"
18 #include "typeset.h"
19 #include <math.h>
20 class Plane3;
21 
22 
27 inline void interpolateOnTriangle2D( const Coord pt,
28  const Coord a, const Coord b, const Coord c,
29  double& weight_a, double& weight_b, double& weight_c )
30 {
31  const Coord3 ba = Coord3(b-a,0.);
32  const Coord3 ca = Coord3(c-a,0.);
33  const double triarea = ba.cross( ca ).abs()*0.5;
34 
35  const Coord3 pa = Coord3(pt-a,0.);
36  const Coord3 pb = Coord3(pt-b,0.);
37  const Coord3 pc = Coord3(pt-c,0.);
38 
39  if ( mIsZero(triarea,1e-8) )
40  {
41  const double distpa = pa.abs();
42  if ( mIsZero(distpa,1e-8) )
43  {
44  weight_a = 1.; weight_b = 0.; weight_c = 0.;
45  return;
46  }
47 
48  const double distpb = pb.abs();
49  if ( mIsZero(distpb,1e-8) )
50  {
51  weight_a = 0.; weight_b = 1.; weight_c = 0.;
52  return;
53  }
54 
55  const double distpc = pc.abs();
56  if ( mIsZero(distpc,1e-8) )
57  {
58  weight_a = 0.; weight_b = 0.; weight_c = 1.;
59  return;
60  }
61 
62  const double totalinversedist = 1./distpa + 1./distpb + 1./distpc;
63  weight_a = 1./(distpa*totalinversedist);
64  weight_b = 1./(distpb*totalinversedist);
65  weight_c = 1./(distpc*totalinversedist);
66  }
67  else
68  {
69  const double triareapab = pa.cross( pb ).abs()*0.5;
70  const double triareapac = pa.cross( pc ).abs()*0.5;
71  const double triareapcb = pc.cross( pb ).abs()*0.5;
72 
73  weight_a = triareapcb / triarea;
74  weight_b = triareapac / triarea;
75  weight_c = triareapab / triarea;
76  }
77 }
78 
79 
85 /*Calculate a 3x3 matrix's determinent given by v[0]-v[8] with 9 elements. */
86 inline double determinent33( const double* v )
87 {
88  return v[0]*(v[4]*v[8]-v[5]*v[7])+v[1]*(v[5]*v[6]-v[3]*v[8])+
89  v[2]*(v[3]*v[7]-v[4]*v[6]);
90 }
91 
92 
93 /*Calculate a 4x4 matrix's determinent given by rows r0, r1, r2, r3 with the
94  last column 1, 1, 1, 1. */
95 inline double determinent44( const Coord3& r0, const Coord3& r1,
96  const Coord3& r2, const Coord3& r3 )
97 {
98  const double d0[9] = { r1.y, r1.z, 1, r2.y, r2.z, 1, r3.y, r3.z, 1 };
99  const double d1[9] = { r1.x, r1.z, 1, r2.x, r2.z, 1, r3.x, r3.z, 1 };
100  const double d2[9] = { r1.x, r1.y, 1, r2.x, r2.y, 1, r3.x, r3.y, 1 };
101  const double d3[9] = { r1.x, r1.y, r1.z, r2.x, r2.y, r2.z, r3.x, r3.y,r3.z};
102  return r0.x*determinent33( d0 )-r0.y*determinent33( d1 )+
103  r0.z*determinent33( d2 )-determinent33( d3 );
104 }
105 
107 inline double determinent44( const double* r0, const double* r1,
108  const double* r2, const double* r3 )
109 {
110  const double d0[9] = { r1[1], r1[2], r1[3], r2[1], r2[2], r2[3],
111  r3[1], r3[2], r3[3] };
112  const double d1[9] = { r1[0], r1[2], r1[3], r2[0], r2[2], r2[3],
113  r3[0], r3[2], r3[3] };
114  const double d2[9] = { r1[0], r1[1], r1[3], r2[0], r2[1], r2[3],
115  r3[0], r3[1], r3[3] };
116  const double d3[9] = { r1[0], r1[1], r1[2], r2[0], r2[1], r2[2],
117  r3[0], r3[1], r3[2] };
118  return r0[0]*determinent33( d0 )-r0[1]*determinent33( d1 )+
119  r0[2]*determinent33( d2 )-r0[3]*determinent33( d3 );
120 }
121 
123 inline bool isInsideCircle( const Coord& pt,
124  const Coord& p1, const Coord& p2, const Coord& p3 )
125 {
126  Coord center;
127  const Coord d12 = p1-p2;
128  const Coord d13 = p1-p3;
129  const Coord d = p1-pt;
130  const double deter=d13.x*d12.y-d13.y*d12.x;
131  const double a12 = ( p1.dot(p1)-p2.dot(p2) )/2;
132  const double a13 = ( p1.dot(p1)-p3.dot(p3) )/2;
133  center.x = (a13*d12.y-a12*d13.y)/deter;
134  center.y = (d13.x*a12-d12.x*a13)/deter;
135  return pt.sqAbs()-p1.sqAbs()+2*center.dot(d)<0;
136 }
137 
138 
140 inline bool isInsideCircumSphere( const Coord3& p, const Coord3& a,
141  const Coord3& b, const Coord3& c, const Coord3& d )
142 {
143  const Coord3 ab = a-b;
144  const Coord3 ac = a-c;
145  const Coord3 ad = a-d;
146  const double t[9] = { ab.x, ab.y, ab.z, ac.x, ac.y, ac.z, ad.x, ad.y, ad.z};
147  const double deter = determinent33( t );
148 
149  const double sqra = a.x*a.x+a.y*a.y+a.z*a.z;
150  const double d0 = (sqra-(b.x*b.x+b.y*b.y+b.z*b.z))/2;
151  const double d1 = (sqra-(c.x*c.x+c.y*c.y+c.z*c.z))/2;
152  const double d2 = (sqra-(d.x*d.x+d.y*d.y+d.z*d.z))/2;
153  const double t0[9] = { d0, ab.y, ab.z, d1, ac.y, ac.z, d2, ad.y, ad.z };
154  const double t1[9] = { ab.x, d0, ab.z, ac.x, d1, ac.z, ad.x, d2, ad.z };
155  const double t2[9] = { ab.x, ab.y, d0, ac.x, ac.y, d1, ad.x, ad.y, d2 };
156  const double centerx = determinent33(t0)/deter;
157  const double centery = determinent33(t1)/deter;
158  const double centerz = determinent33(t2)/deter;
159 
160  return (p.x*p.x+p.y*p.y+p.z*p.z-sqra+
161  2*(centerx*(a.x-p.x)+centery*(a.y-p.y)+centerz*(a.z-p.z)))<0;
162 }
163 
164 
166 inline bool sameSide2D( const Coord& p1, const Coord& p2,
167  const Coord& a, const Coord& b, double epsilon )
168 {
169  double xdiff = b.x-a.x;
170  double ydiff = b.y-a.y;
171  return ((p1.x-a.x)*ydiff-(p1.y-a.y)*xdiff)*
172  ((p2.x-a.x)*ydiff-(p2.y-a.y)*xdiff)>=-epsilon;
173 }
174 
175 
177 inline bool sameSide3D( const Coord3& p1, const Coord3& p2,
178  const Coord3& a, const Coord3& b, double epsilon )
179 {
180  const Coord3 cpp1 = (b-a).cross(p1-a);
181  const Coord3 cpp2 = (b-a).cross(p2-a);
182  return cpp1.dot(cpp2)>=-epsilon;
183 }
184 
185 
187 inline bool pointInTriangle2D( const Coord& p, const Coord& a, const Coord& b,
188  const Coord& c, double epsilon )
189 {
190  if ( (p.x>a.x && p.x>b.x && p.x>c.x) || (p.x<a.x && p.x<b.x && p.x<c.x) ||
191  (p.y>a.y && p.y>b.y && p.y>c.y) || (p.y<a.y && p.y<b.y && p.y<c.y) )
192  return false;
193 
194  return sameSide2D(p,a,b,c,epsilon) && sameSide2D(p,b,a,c,epsilon) &&
195  sameSide2D(p,c,a,b,epsilon);
196 }
197 
198 
200 inline bool pointInTriangle3D( const Coord3& p, const Coord3& a,
201  const Coord3& b, const Coord3& c, double epsilon,
202  bool useangularmethod )
203 {
204  if ( !useangularmethod )
205  {
206  return sameSide3D(p,a,b,c,epsilon) && sameSide3D(p,b,a,c,epsilon) &&
207  sameSide3D(p,c,a,b,epsilon);
208  }
209 
210  Coord3 ap = a - p;
211  ap = ap.normalize();
212  Coord3 bp = b - p;
213  bp = bp.normalize();
214  Coord3 cp = c - p;
215  cp = cp.normalize();
216 
217  const double d1 = ap.dot( bp );
218  const double d2 = bp.dot( cp );
219  const double d3 = cp.dot( ap );
220  const double angle = Math::ACos(d1) + Math::ACos(d2) + Math::ACos(d3);
221 
222  return mIsEqual(angle,M_2PI,epsilon);
223 }
224 
225 
227 inline bool pointInTriangle3D( const Coord3& p, const Coord3& a,
228  const Coord3& b, const Coord3& c, double epsilon )
229 { return pointInTriangle3D( p, a, b, c, epsilon, false ); }
230 
231 
233 inline bool pointOnEdge2D( const Coord& p, const Coord& a, const Coord& b,
234  double epsilon )
235 {
236  const Coord pa = p-a;
237  const Coord ba = b-a;
238  const double t = pa.dot(ba)/ba.sqAbs();
239  if ( t<0 || t>1 )
240  return false;
241 
242  const Coord intersectpt = a+Coord(t*ba.x, t*ba.y);
243  const Coord pq = p-intersectpt;
244  return pq.sqAbs()<epsilon*epsilon;
245 }
246 
247 
248 inline bool pointOnEdge3D( const Coord3& p, const Coord3& a, const Coord3& b,
249  double epsilon )
250 {
251  const Coord3 pa = p-a;
252  const Coord3 ba = b-a;
253  const double t = pa.dot(ba)/ba.sqAbs();
254  if ( t<0 || t>1 )
255  return false;
256 
257  const Coord3 pq = pa-t*ba;
258  return pq.sqAbs()<epsilon*epsilon;
259 }
260 
261 
265 inline bool pointInPolygon( const Coord3& pt, const TypeSet<Coord3>& plgknots,
266  double epsilon )
267 {
268  const int nrvertices = plgknots.size();
269  if ( nrvertices==2 )
270  {
271  const double newepsilon = plgknots[0].distTo(plgknots[1])*0.001;
272  return pointOnEdge3D( pt, plgknots[0], plgknots[1], newepsilon );
273  }
274  else if ( nrvertices==3 )
275  return pointInTriangle3D( pt, plgknots[0], plgknots[1], plgknots[2],
276  epsilon );
277  else
278  {
279  Coord3 p1, p2;
280  double anglesum = 0, cosangle;
281  for ( int idx=0; idx<nrvertices; idx++ )
282  {
283  p1 = plgknots[idx] - pt;
284  p2 = plgknots[(idx+1)%nrvertices] - pt;
285 
286  const double d1 = p1.abs();
287  const double d2 = p2.abs();
288  if ( d1*d2 <= epsilon*epsilon || d1 <= epsilon || d2 <= epsilon )
289  return true;
290  else
291  cosangle = p1.dot(p2) / (d1*d2);
292 
293  anglesum += acos( cosangle );
294  }
295 
296  return mIsEqual( anglesum, 6.2831853071795862, 1e-4 );
297  }
298 }
299 
300 
301 typedef Coord3 Vector3;
302 
308 TypeSet<Vector3>* makeSphereVectorSet( double dangle );
309 
316 Coord3 estimateAverageVector( const TypeSet<Coord3>&, bool normalize,
317  bool checkforundefs );
318 
319 
329 {
330 public:
331  Quaternion(double s,double x,double y,double z);
332  Quaternion(const Vector3& axis,float angle);
333 
334  void setRotation(const Vector3& axis,float angle);
335  void getRotation(Vector3& axis,float& angle) const;
337  Coord3 rotate(const Coord3&) const;
338 
339  Quaternion operator+(const Quaternion&) const;
340  Quaternion& operator+=(const Quaternion&);
341  Quaternion operator-(const Quaternion&) const;
342  Quaternion& operator-=(const Quaternion&);
343  Quaternion operator*(const Quaternion&) const;
344  Quaternion& operator*=(const Quaternion&);
345 
346  Quaternion inverse() const;
347 
348  double s_;
350 };
351 
352 
363 {
364 public:
365  ParamLine2(double slope=0,double intcpt=0);
366  ParamLine2(const Coord&,double slope);
367  ParamLine2(const Coord& p0,const Coord& p1);
370  bool operator==(const ParamLine2&) const;
371 
372  Coord direction(bool normalized=true) const;
373 
374  Coord getPoint(double t) const;
375 
376  double closestPoint(const Coord& point) const;
382  double distanceToPoint(const Coord&) const;
383  double sqDistanceToPoint(const Coord&) const;
384 
385  double x0_;
386  double y0_;
387  double alpha_;
388  double beta_;
389 };
390 
391 
398 {
399 public:
400  Line2(double slope=0,double intcpt=0);
401  Line2(const Coord&,double slope);
402  Line2(const Coord&,const Coord&);
403 
404  bool operator==(const Line2&) const;
405 
406  Coord direction() const;
408  Coord closestPoint(const Coord& point) const;
412  Coord intersection(const Line2&,bool checkinlimit=true) const;
413 
414  double distanceTo(const Line2&) const;
416  bool getParallelLine(Line2& line,double dist) const;
418  bool getPerpendicularLine(Line2& line,const Coord& pt) const;
420  bool isOnLine(const Coord& pt) const;
421 
422  double slope_;
423  double yintcpt_;
424 
425  bool isvertical_;
426  double xintcpt_;
430 };
431 
432 
442 {
443 public:
444  Line3();
445  Line3( double x0, double y0, double z0,
446  double alpha, double beta, double gamma );
447  Line3( const Coord3&, const Vector3& );
448 
449  Vector3 direction( bool normalize = true ) const
450  {
451  const Vector3 res( alpha_, beta_, gamma_ );
452  return normalize ? res.normalize() : res;
453  }
454 
455  Coord3 getPoint(double t) const;
456  bool intersectWith( const Plane3&, double& t ) const;
461  double distanceToPoint( const Coord3& point ) const;
462  double sqDistanceToPoint( const Coord3& point ) const;
463  double closestPoint( const Coord3& point ) const;
466  void closestPoint( const Line3& line, double& t_this,
467  double& t_line ) const;
471  double x0_;
472  double y0_;
473  double z0_;
474  double alpha_;
475  double beta_;
476  double gamma_;
477 };
478 
479 
485 {
486 public:
487  Plane3();
488  Plane3(double, double, double, double);
489  Plane3( const Coord3& vectors, const Coord3&,
490  bool twovectors );
494  Plane3( const Coord3&, const Coord3&, const Coord3& );
495  Plane3( const TypeSet<Coord3>& );
496 
497  void set( const Coord3& vector, const Coord3&,
498  bool twovectors );
502  void set( const Coord3&, const Coord3&, const Coord3& );
503  float set( const TypeSet<Coord3>& );
509  bool operator==(const Plane3&) const;
510  bool operator!=(const Plane3&) const;
511 
512  Coord3 normal() const { return Coord3( A_, B_, C_ ); }
513 
514  double distanceToPoint( const Coord3&,
515  bool wichside=false ) const;
520  bool intersectWith( const Line3&, Coord3& ) const;
523  bool intersectWith( const Plane3&, Line3& ) const;
526  Coord3 getProjection(const Coord3& pos);
527  bool onSameSide(const Coord3& p1,const Coord3& p2);
531  double A_;
532  double B_;
533  double C_;
534  double D_;
535 };
536 
537 
544 {
545 public:
546  Plane3CoordSystem(const Coord3& normal,
547  const Coord3& origin,
548  const Coord3& pt10);
553  virtual ~Plane3CoordSystem() {}
554  bool isOK() const;
558  const Plane3& plane() const { return plane_; }
559 
560  Coord transform(const Coord3&,bool project) const;
565  Coord3 transform(const Coord&) const;
566 
567 protected:
568 
569  const Plane3 plane_;
573  bool isok_;
574 };
575 
576 
583 {
584 public:
585  Sphere(float r=0,float t=0,float p=0)
586  : radius(r),theta(t),phi(p) {}
587 
588  Sphere(const Coord3& crd)
589  : radius((float) crd.x),theta((float) crd.y)
590  , phi((float) crd.z) {}
591  inline bool operator ==(const Sphere&) const;
592  inline bool operator !=( const Sphere& oth ) const
593  { return !(oth == *this); }
594 
595  float radius;
596  float theta;
597  float phi;
598 
599  static const Sphere& nullSphere();
600  bool isNull() const;
601 
602 };
603 
604 
605 mGlobal(Algo) Sphere cartesian2Spherical(const Coord3&,bool math);
608 mGlobal(Algo) Coord3 spherical2Cartesian(const Sphere&,bool math);
612 inline bool Sphere::operator ==( const Sphere& s ) const
613 {
614  const float dr = radius-s.radius;
615  const float dt = theta-s.theta;
616  const float dp = phi-s.phi;
617  return mIsZero(dr,1e-8) && mIsZero(dt,1e-8) && mIsZero(dp,1e-8);
618 }
619 
620 
621 #endif
Quaternion is an extension to complex numbers.
Definition: trigonometry.h:328
#define mExpClass(module)
Definition: commondefs.h:160
double alpha_
Definition: trigonometry.h:387
Coord start_
Definition: trigonometry.h:428
Coord3 getProjection(const Coord3 &pos)
double alpha_
Definition: trigonometry.h:474
Vector3 direction(bool normalize=true) const
Definition: trigonometry.h:449
#define M_2PI
Definition: commondefs.h:70
int operator-(const DateInfo &di1, const DateInfo &di2)
Definition: dateinfo.h:129
double determinent33(const double *v)
Here are some commonly used functions to judge the position relation between point and line...
Definition: trigonometry.h:86
Coord3 vec01_
Definition: trigonometry.h:572
float radius
Definition: trigonometry.h:595
double y0_
Definition: trigonometry.h:472
bool pointInPolygon(const Coord3 &pt, const TypeSet< Coord3 > &plgknots, double epsilon)
Definition: trigonometry.h:265
Coord3 Vector3
Definition: trigonometry.h:301
bool operator==(const ArrayNDInfo &a1, const ArrayNDInfo &a2)
Definition: arrayndinfo.h:53
DistType dot(const Coord3 &) const
Definition: coord.h:249
float ACos(float)
double yintcpt_
Definition: trigonometry.h:423
#define mGlobal(module)
Definition: commondefs.h:163
#define mIsZero(x, eps)
Definition: commondefs.h:53
double gamma_
Definition: trigonometry.h:476
bool isInsideCircumSphere(const Coord3 &p, const Coord3 &a, const Coord3 &b, const Coord3 &c, const Coord3 &d)
Definition: trigonometry.h:140
const Plane3 & plane() const
Definition: trigonometry.h:558
DistType sqAbs() const
double y0_
Definition: trigonometry.h:386
bool sameSide2D(const Coord &p1, const Coord &p2, const Coord &a, const Coord &b, double epsilon)
Definition: trigonometry.h:166
bool intersectWith(const Line3 &, Coord3 &) const
Sphere(const Coord3 &crd)
Definition: trigonometry.h:588
A cartesian coordinate in 2D space.
Definition: coord.h:25
A Plane3 is a plane in space, with the equation: Ax + By + Cz + D = 0.
Definition: trigonometry.h:484
A Line3 is a line in space, with the following equations:
Definition: trigonometry.h:441
Defines a 2D coordinate system on a 3D plane and transforms between the 3D space and the coordinate s...
Definition: trigonometry.h:543
Coord stop_
Definition: trigonometry.h:429
#define mIsEqual(x, y, eps)
Definition: commondefs.h:54
double determinent44(const Coord3 &r0, const Coord3 &r1, const Coord3 &r2, const Coord3 &r3)
Definition: trigonometry.h:95
double B_
Definition: trigonometry.h:532
T sqAbs() const
Definition: geometry.h:369
TypeSet< Vector3 > * makeSphereVectorSet(double dangle)
Divides a sphere in a number of vectors, divided by approximately dangle from each other...
const Plane3 plane_
Definition: trigonometry.h:569
Coord3 cross(const Coord3 &) const
Definition: coord.h:253
bool pointInTriangle3D(const Coord3 &p, const Coord3 &a, const Coord3 &b, const Coord3 &c, double epsilon, bool useangularmethod)
Definition: trigonometry.h:200
bool onSameSide(const Coord3 &p1, const Coord3 &p2)
double D_
Definition: trigonometry.h:534
Coord3 estimateAverageVector(const TypeSet< Coord3 > &, bool normalize, bool checkforundefs)
Computes an average of a number of vectors using:
double A_
Definition: trigonometry.h:531
bool pointOnEdge2D(const Coord &p, const Coord &a, const Coord &b, double epsilon)
Definition: trigonometry.h:233
double beta_
Definition: trigonometry.h:475
Coord3 normalize() const
Definition: coord.h:257
double C_
Definition: trigonometry.h:533
const Coord3 origin_
Definition: trigonometry.h:570
float phi
Definition: trigonometry.h:597
T y
Definition: geometry.h:67
double z0_
Definition: trigonometry.h:473
DistType abs() const
void interpolateOnTriangle2D(const Coord pt, const Coord a, const Coord b, const Coord c, double &weight_a, double &weight_b, double &weight_c)
Given a point pt in a triangle ABC, we calculate the interpolation weights for each vertex...
Definition: trigonometry.h:27
double x0_
Definition: trigonometry.h:471
double x0_
Definition: trigonometry.h:385
bool operator!=(const ArrayNDInfo &a1, const ArrayNDInfo &a2)
Definition: arrayndinfo.h:62
Sphere(float r=0, float t=0, float p=0)
Definition: trigonometry.h:585
float theta
Definition: trigonometry.h:596
A cartesian coordinate in 3D space.
Definition: coord.h:72
double xintcpt_
Definition: trigonometry.h:426
OrdType z
Definition: coord.h:125
Coord3 operator*(double f, const Coord3 &b)
Definition: coord.h:130
size_type size() const
Definition: typeset.h:254
bool isvertical_
Definition: trigonometry.h:425
bool pointInTriangle2D(const Coord &p, const Coord &a, const Coord &b, const Coord &c, double epsilon)
Definition: trigonometry.h:187
bool sameSide3D(const Coord3 &p1, const Coord3 &p2, const Coord3 &a, const Coord3 &b, double epsilon)
Definition: trigonometry.h:177
T x
Definition: geometry.h:66
double s_
Definition: trigonometry.h:348
double slope_
Definition: trigonometry.h:422
A ParamLine2 is a line in space, with the following equations:
Definition: trigonometry.h:362
bool isInsideCircle(const Coord &pt, const Coord &p1, const Coord &p2, const Coord &p3)
Definition: trigonometry.h:123
Vector3 vec_
Definition: trigonometry.h:349
Represents a point in spherical coordinates. The angle phi lies in the horizontal plane...
Definition: trigonometry.h:582
Coord3 vec10_
Definition: trigonometry.h:571
bool pointOnEdge3D(const Coord3 &p, const Coord3 &a, const Coord3 &b, double epsilon)
Definition: trigonometry.h:248
double beta_
Definition: trigonometry.h:388
TrcKeyZSampling::Dir direction(TrcKeyZSampling::Dir slctype, int dimnr)
Definition: trckeyzsampling.h:139
bool isok_
Definition: trigonometry.h:573
Coord3 normal() const
Definition: trigonometry.h:512
Sphere cartesian2Spherical(const Coord3 &, bool math)
virtual ~Plane3CoordSystem()
Definition: trigonometry.h:553
double distanceToPoint(const Coord3 &, bool wichside=false) const
Coord3 spherical2Cartesian(const Sphere &, bool math)
OrdType dot(const Coord &) const
A Line2 is a line on XY-plane, and it is defined in slope-intercept form y = slope*x + y-intercept; f...
Definition: trigonometry.h:397

Generated at for the OpendTect seismic interpretation project. Copyright (C): dGB Beheer B. V. 2019