AGX Dynamics  2.35.0.1
cd_vector.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 /*
18 This source code has been taken and modified by Algoryx Simulation AB
19 from the source and under the license given below.
20 */
21 
23 
24 #ifndef CD_VECTOR_H
25 
26 #define CD_VECTOR_H
27 
84 // http://codesuppository.blogspot.com
85 //
86 // mailto: jratcliff@infiniplex.net
87 //
88 // http://www.amillionpixels.us
89 //
90 
91 
92 #ifdef _MSC_VER
93 # pragma warning( push )
94 # pragma warning(disable:4786)
95 #endif
96 
97 #include <cmath>
98 #include <math.h>
99 #include <float.h>
100 
101 inline float fast_sqrt( float f ) { return sqrtf(f); }
102 inline float faster_sqrt( float f ) { return sqrtf(f); }
103 
104 inline double fast_sqrt( double f ) { return sqrt(f); }
105 inline double faster_sqrt( double f ) { return sqrt(f); }
106 
107 namespace ConvexDecomposition
108 {
109 
110 
111  const double DEG_TO_RAD = ((2.0f * 3.14152654f) / 360.0f);
112  const double RAD_TO_DEG = (360.0f / (2.0f * 3.141592654f));
113 
114  template <class Type> class Vector3d
115  {
116  public:
117  Vector3d(void) {
118  x = 0.0;
119  y = 0.0;
120  z = 0.0;
121  } // null constructor
122 
123  Vector3d(const Vector3d &a) // constructor copies existing vector.
124  {
125  x = a.x;
126  y = a.y;
127  z = a.z;
128  }
129 
130  Vector3d(Type a,Type b,Type c) // construct with initial point.
131  {
132  x = a;
133  y = b;
134  z = c;
135  }
136 
137  Vector3d(const double *t)
138  {
139  x = t[0];
140  y = t[1];
141  z = t[2];
142  }
143 
144  Vector3d(const int *t)
145  {
146  x = t[0];
147  y = t[1];
148  z = t[2];
149  }
150 
151  bool operator==(const Vector3d<Type> &a) const
152  {
153  return( a.x == x && a.y == y && a.z == z );
154  }
155 
156  bool operator!=(const Vector3d<Type> &a) const
157  {
158  return( a.x != x || a.y != y || a.z != z );
159  }
160 
161  // Operators
162  Vector3d& operator = (const Vector3d& A) // ASSIGNMENT (=)
163  { x=A.x; y=A.y; z=A.z;
164  return(*this); }
165 
166  Vector3d operator + (const Vector3d& A) const // ADDITION (+)
167  { Vector3d Sum(x+A.x, y+A.y, z+A.z);
168  return(Sum); }
169 
170  Vector3d operator - (const Vector3d& A) const // SUBTRACTION (-)
171  { Vector3d Diff(x-A.x, y-A.y, z-A.z);
172  return(Diff); }
173 
174  Vector3d operator * (const double s) const // MULTIPLY BY SCALAR (*)
175  { Vector3d Scaled(x*s, y*s, z*s);
176  return(Scaled); }
177 
178 
179  Vector3d operator + (const double s) const // ADD CONSTANT TO ALL 3 COMPONENTS (*)
180  { Vector3d Scaled(x+s, y+s, z+s);
181  return(Scaled); }
182 
183 
184 
185 
186  Vector3d operator / (const double s) const // DIVIDE BY SCALAR (/)
187  {
188  double r = 1.0f / s;
189  Vector3d Scaled(x*r, y*r, z*r);
190  return(Scaled);
191  }
192 
193  void operator /= (Type A) // ACCUMULATED VECTOR ADDITION (/=)
194  { x/=A; y/=A; z/=A; }
195 
196  void operator += (const Vector3d A) // ACCUMULATED VECTOR ADDITION (+=)
197  { x+=A.x; y+=A.y; z+=A.z; }
198  void operator -= (const Vector3d A) // ACCUMULATED VECTOR SUBTRACTION (+=)
199  { x-=A.x; y-=A.y; z-=A.z; }
200  void operator *= (const double s) // ACCUMULATED SCALAR MULTIPLICATION (*=) (bpc 4/24/2000)
201  {x*=s; y*=s; z*=s;}
202 
203  void operator += (const double A) // ACCUMULATED VECTOR ADDITION (+=)
204  { x+=A; y+=A; z+=A; }
205 
206 
207  Vector3d operator - (void) const // NEGATION (-)
208  { Vector3d Negated(-x, -y, -z);
209  return(Negated); }
210 
211  Type operator [] (const int i) const // ALLOWS VECTOR ACCESS AS AN ARRAY.
212  { return( (i==0)?x:((i==1)?y:z) ); }
213  Type & operator [] (const int i)
214  { return( (i==0)?x:((i==1)?y:z) ); }
215  //
216 
217  // accessor methods.
218  Type GetX(void) const { return x; }
219  Type GetY(void) const { return y; }
220  Type GetZ(void) const { return z; }
221 
222  Type X(void) const { return x; }
223  Type Y(void) const { return y; }
224  Type Z(void) const { return z; }
225 
226  void SetX(Type t) { x = t; }
227  void SetY(Type t) { y = t; }
228  void SetZ(Type t) { z = t; }
229 
230  bool IsSame(const Vector3d<double> &v,double epsilon) const
231  {
232  double dx = fabsf( x - v.x );
233  if ( dx > epsilon ) return false;
234  double dy = fabsf( y - v.y );
235  if ( dy > epsilon ) return false;
236  double dz = fabsf( z - v.z );
237  if ( dz > epsilon ) return false;
238  return true;
239  }
240 
241 
242  double ComputeNormal(const Vector3d<double> &A,
243  const Vector3d<double> &B,
244  const Vector3d<double> &C)
245  {
246  double vx,vy,vz,wx,wy,wz,vw_x,vw_y,vw_z,mag;
247 
248  vx = (B.x - C.x);
249  vy = (B.y - C.y);
250  vz = (B.z - C.z);
251 
252  wx = (A.x - B.x);
253  wy = (A.y - B.y);
254  wz = (A.z - B.z);
255 
256  vw_x = vy * wz - vz * wy;
257  vw_y = vz * wx - vx * wz;
258  vw_z = vx * wy - vy * wx;
259 
260  mag = std::sqrt((vw_x * vw_x) + (vw_y * vw_y) + (vw_z * vw_z));
261 
262  if ( mag < 0.000001f )
263  {
264  mag = 0;
265  }
266  else
267  {
268  mag = 1.0f/mag;
269  }
270 
271  x = vw_x * mag;
272  y = vw_y * mag;
273  z = vw_z * mag;
274 
275  return mag;
276  }
277 
278 
279  void ScaleSumScale(double c0,double c1,const Vector3d<double> &pos)
280  {
281  x = (x*c0) + (pos.x*c1);
282  y = (y*c0) + (pos.y*c1);
283  z = (z*c0) + (pos.z*c1);
284  }
285 
286  void SwapYZ(void)
287  {
288  double t = y;
289  y = z;
290  z = t;
291  }
292 
293  void Get(Type *v) const
294  {
295  v[0] = x;
296  v[1] = y;
297  v[2] = z;
298  }
299 
300  void Set(const int *p)
301  {
302  x = (Type) p[0];
303  y = (Type) p[1];
304  z = (Type) p[2];
305  }
306 
307  void Set(const double *p)
308  {
309  x = (Type) p[0];
310  y = (Type) p[1];
311  z = (Type) p[2];
312  }
313 
314 
315  void Set(Type a,Type b,Type c)
316  {
317  x = a;
318  y = b;
319  z = c;
320  }
321 
322  void Zero(void)
323  {
324  x = y = z = 0;
325  }
326 
327  const Type* Ptr() const { return &x; }
328  Type* Ptr() { return &x; }
329 
330 
331  // return -(*this).
332  Vector3d negative(void) const
333  {
334  Vector3d result;
335  result.x = -x;
336  result.y = -y;
337  result.z = -z;
338  return result;
339  }
340 
341  Type Magnitude(void) const
342  {
343  return Type(sqrt(x * x + y * y + z * z));
344  }
345 
346  Type FastMagnitude(void) const
347  {
348  return Type(fast_sqrt(x * x + y * y + z * z));
349  }
350 
351  Type FasterMagnitude(void) const
352  {
353  return Type(faster_sqrt(x * x + y * y + z * z));
354  }
355 
356  void Lerp(const Vector3d<Type>& from,const Vector3d<Type>& to,double slerp)
357  {
358  x = ((to.x - from.x) * slerp) + from.x;
359  y = ((to.y - from.y) * slerp) + from.y;
360  z = ((to.z - from.z) * slerp) + from.z;
361  }
362 
363  // Highly specialized interpolate routine. Will compute the interpolated position
364  // shifted forward or backwards along the ray defined between (from) and (to).
365  // Reason for existence is so that when a bullet collides with a wall, for
366  // example, you can generate a graphic effect slightly *before* it hit the
367  // wall so that the effect doesn't sort into the wall itself.
368  void Interpolate(const Vector3d<double> &from,const Vector3d<double> &to,double offset)
369  {
370  x = to.x-from.x;
371  y = to.y-from.y;
372  z = to.z-from.z;
373  double d = std::sqrt( x*x + y*y + z*z );
374  double recip = 1.0f / d;
375  x*=recip;
376  y*=recip;
377  z*=recip; // normalize vector
378  d+=offset; // shift along ray
379  x = x*d + from.x;
380  y = y*d + from.y;
381  z = z*d + from.z;
382  }
383 
384  bool BinaryEqual(const Vector3d<double> &p) const
385  {
386  const int *source = (const int *) &x;
387  const int *dest = (const int *) &p.x;
388 
389  if ( source[0] == dest[0] &&
390  source[1] == dest[1] &&
391  source[2] == dest[2] ) return true;
392 
393  return false;
394  }
395 
396  bool BinaryEqual(const Vector3d<int> &p) const
397  {
398  if ( x == p.x && y == p.y && z == p.z ) return true;
399  return false;
400  }
401 
402 
404  void Reflection(const Vector3d<Type> &a,const Vector3d<Type> &b)// compute reflection vector.
405  {
406  Vector3d<double> c;
407  Vector3d<double> d;
408 
409  double dot = a.Dot(b) * 2.0f;
410 
411  c = b * dot;
412 
413  d = c - a;
414 
415  x = -d.x;
416  y = -d.y;
417  z = -d.z;
418  }
419 
420  void AngleAxis(Type angle,const Vector3d<Type>& axis)
421  {
422  x = axis.x*angle;
423  y = axis.y*angle;
424  z = axis.z*angle;
425  }
426 
427  Type Length(void) const // length of vector.
428  {
429  return Type(sqrt( x*x + y*y + z*z ));
430  }
431 
432 
433  double ComputePlane(const Vector3d<double> &A,
434  const Vector3d<double> &B,
435  const Vector3d<double> &C)
436  {
437  double vx,vy,vz,wx,wy,wz,vw_x,vw_y,vw_z,mag;
438 
439  vx = (B.x - C.x);
440  vy = (B.y - C.y);
441  vz = (B.z - C.z);
442 
443  wx = (A.x - B.x);
444  wy = (A.y - B.y);
445  wz = (A.z - B.z);
446 
447  vw_x = vy * wz - vz * wy;
448  vw_y = vz * wx - vx * wz;
449  vw_z = vx * wy - vy * wx;
450 
451  mag = sqrt((vw_x * vw_x) + (vw_y * vw_y) + (vw_z * vw_z));
452 
453  if ( mag < 0.000001f )
454  {
455  mag = 0;
456  }
457  else
458  {
459  mag = 1.0f/mag;
460  }
461 
462  x = vw_x * mag;
463  y = vw_y * mag;
464  z = vw_z * mag;
465 
466 
467  double D = 0.0f - ((x*A.x)+(y*A.y)+(z*A.z));
468 
469  return D;
470  }
471 
472 
473  Type FastLength(void) const // length of vector.
474  {
475  return Type(fast_sqrt( x*x + y*y + z*z ));
476  }
477 
478 
479  Type FasterLength(void) const // length of vector.
480  {
481  return Type(faster_sqrt( x*x + y*y + z*z ));
482  }
483 
484  Type Length2(void) const // squared distance, prior to square root.
485  {
486  Type l2 = x*x+y*y+z*z;
487  return l2;
488  }
489 
490  Type Distance(const Vector3d<Type> &a) const // distance between two points.
491  {
492  Vector3d<Type> d(a.x-x,a.y-y,a.z-z);
493  return d.Length();
494  }
495 
496  Type FastDistance(const Vector3d<Type> &a) const // distance between two points.
497  {
498  Vector3d<Type> d(a.x-x,a.y-y,a.z-z);
499  return d.FastLength();
500  }
501 
502  Type FasterDistance(const Vector3d<Type> &a) const // distance between two points.
503  {
504  Vector3d<Type> d(a.x-x,a.y-y,a.z-z);
505  return d.FasterLength();
506  }
507 
508 
509  Type DistanceXY(const Vector3d<Type> &a) const
510  {
511  double dx = a.x - x;
512  double dy = a.y - y;
513  double dist = dx*dx + dy*dy;
514  return dist;
515  }
516 
517  Type Distance2(const Vector3d<Type> &a) const // squared distance.
518  {
519  double dx = a.x - x;
520  double dy = a.y - y;
521  double dz = a.z - z;
522  return dx*dx + dy*dy + dz*dz;
523  }
524 
525  Type Partial(const Vector3d<Type> &p) const
526  {
527  return (x*p.y) - (p.x*y);
528  }
529 
530  Type Area(const Vector3d<Type> &p1,const Vector3d<Type> &p2) const
531  {
532  Type A = Partial(p1);
533  A+= p1.Partial(p2);
534  A+= p2.Partial(*this);
535  return A*0.5f;
536  }
537 
538  inline double Normalize(void) // normalize to a unit vector, returns distance.
539  {
540  double d = std::sqrt( static_cast< double >( x*x + y*y + z*z ) );
541  if ( d > 0 )
542  {
543  double r = 1.0f / d;
544  x *= r;
545  y *= r;
546  z *= r;
547  }
548  else
549  {
550  x = y = z = 1;
551  }
552  return d;
553  }
554 
555  inline double FastNormalize(void) // normalize to a unit vector, returns distance.
556  {
557  double d = fast_sqrt( static_cast< double >( x*x + y*y + z*z ) );
558  if ( d > 0 )
559  {
560  double r = 1.0f / d;
561  x *= r;
562  y *= r;
563  z *= r;
564  }
565  else
566  {
567  x = y = z = 1;
568  }
569  return d;
570  }
571 
572  inline double FasterNormalize(void) // normalize to a unit vector, returns distance.
573  {
574  double d = faster_sqrt( static_cast< double >( x*x + y*y + z*z ) );
575  if ( d > 0 )
576  {
577  double r = 1.0f / d;
578  x *= r;
579  y *= r;
580  z *= r;
581  }
582  else
583  {
584  x = y = z = 1;
585  }
586  return d;
587  }
588 
589 
590 
591 
592  Type Dot(const Vector3d<Type> &a) const // computes dot product.
593  {
594  return (x * a.x + y * a.y + z * a.z );
595  }
596 
597 
598  Vector3d<Type> Cross( const Vector3d<Type>& other ) const
599  {
600  Vector3d<Type> result( y*other.z - z*other.y, z*other.x - x*other.z, x*other.y - y*other.x );
601 
602  return result;
603  }
604 
605  void Cross(const Vector3d<Type> &a,const Vector3d<Type> &b) // cross two vectors result in this one.
606  {
607  x = a.y*b.z - a.z*b.y;
608  y = a.z*b.x - a.x*b.z;
609  z = a.x*b.y - a.y*b.x;
610  }
611 
612  /******************************************/
613  // Check if next edge (b to c) turns inward
614  //
615  // Edge from a to b is already in face
616  // Edge from b to c is being considered for addition to face
617  /******************************************/
618  bool Concave(const Vector3d<double>& a,const Vector3d<double>& b)
619  {
620  double vx,vy,vz,wx,wy,wz,vw_x,vw_y,vw_z,mag,nx,ny,nz,mag_a,mag_b;
621 
622  wx = b.x - a.x;
623  wy = b.y - a.y;
624  wz = b.z - a.z;
625 
626  mag_a = (double) std::sqrt((wx * wx) + (wy * wy) + (wz * wz));
627 
628  vx = x - b.x;
629  vy = y - b.y;
630  vz = z - b.z;
631 
632  mag_b = (double) std::sqrt((vx * vx) + (vy * vy) + (vz * vz));
633 
634  vw_x = (vy * wz) - (vz * wy);
635  vw_y = (vz * wx) - (vx * wz);
636  vw_z = (vx * wy) - (vy * wx);
637 
638  mag = (double) std::sqrt((vw_x * vw_x) + (vw_y * vw_y) + (vw_z * vw_z));
639 
640  // Check magnitude of cross product, which is a sine function
641  // i.e., mag (a x b) = mag (a) * mag (b) * sin (theta);
642  // If sin (theta) small, then angle between edges is very close to
643  // 180, which we may want to call a concavity. Setting the
644  // CONCAVITY_TOLERANCE value greater than about 0.01 MAY cause
645  // face consolidation to get stuck on particular face. Most meshes
646  // convert properly with a value of 0.0
647 
648  if (mag/(mag_a*mag_b) <= 0.0f ) return true;
649 
650  mag = 1.0f / mag;
651 
652  nx = vw_x * mag;
653  ny = vw_y * mag;
654  nz = vw_z * mag;
655 
656  // Dot product of tri normal with cross product result will
657  // yield positive number if edges are convex (+1.0 if two tris
658  // are coplanar), negative number if edges are concave (-1.0 if
659  // two tris are coplanar.)
660 
661  mag = ( x * nx) + ( y * ny) + ( z * nz);
662 
663  if (mag > 0.0f ) return false;
664 
665  return(true);
666  }
667 
668  bool PointTestXY(const Vector3d<double> &i,const Vector3d<double> &j) const
669  {
670  if (((( i.y <= y ) && ( y < j.y )) ||
671  (( j.y <= y ) && ( y < i.y ))) &&
672  ( x < (j.x - i.x) * (y - i.y) / (j.y - i.y) + i.x)) return true;
673  return false;
674  }
675 
676  // test to see if this point is inside the triangle specified by
677  // these three points on the X/Y plane.
678  bool PointInTriXY(const Vector3d<double> &p1,
679  const Vector3d<double> &p2,
680  const Vector3d<double> &p3) const
681  {
682  double ax = p3.x - p2.x;
683  double ay = p3.y - p2.y;
684  double bx = p1.x - p3.x;
685  double by = p1.y - p3.y;
686  double cx = p2.x - p1.x;
687  double cy = p2.y - p1.y;
688  double apx = x - p1.x;
689  double apy = y - p1.y;
690  double bpx = x - p2.x;
691  double bpy = y - p2.y;
692  double cpx = x - p3.x;
693  double cpy = y - p3.y;
694 
695  double aCROSSbp = ax*bpy - ay*bpx;
696  double cCROSSap = cx*apy - cy*apx;
697  double bCROSScp = bx*cpy - by*cpx;
698 
699  return ((aCROSSbp >= 0.0f) && (bCROSScp >= 0.0f) && (cCROSSap >= 0.0f));
700  }
701 
702  // test to see if this point is inside the triangle specified by
703  // these three points on the X/Y plane.
704  bool PointInTriYZ(const Vector3d<double> &p1,
705  const Vector3d<double> &p2,
706  const Vector3d<double> &p3) const
707  {
708  double ay = p3.y - p2.y;
709  double az = p3.z - p2.z;
710  double by = p1.y - p3.y;
711  double bz = p1.z - p3.z;
712  double cy = p2.y - p1.y;
713  double cz = p2.z - p1.z;
714  double apy = y - p1.y;
715  double apz = z - p1.z;
716  double bpy = y - p2.y;
717  double bpz = z - p2.z;
718  double cpy = y - p3.y;
719  double cpz = z - p3.z;
720 
721  double aCROSSbp = ay*bpz - az*bpy;
722  double cCROSSap = cy*apz - cz*apy;
723  double bCROSScp = by*cpz - bz*cpy;
724 
725  return ((aCROSSbp >= 0.0f) && (bCROSScp >= 0.0f) && (cCROSSap >= 0.0f));
726  }
727 
728 
729  // test to see if this point is inside the triangle specified by
730  // these three points on the X/Y plane.
731  bool PointInTriXZ(const Vector3d<double> &p1,
732  const Vector3d<double> &p2,
733  const Vector3d<double> &p3) const
734  {
735  double az = p3.z - p2.z;
736  double ax = p3.x - p2.x;
737  double bz = p1.z - p3.z;
738  double bx = p1.x - p3.x;
739  double cz = p2.z - p1.z;
740  double cx = p2.x - p1.x;
741  double apz = z - p1.z;
742  double apx = x - p1.x;
743  double bpz = z - p2.z;
744  double bpx = x - p2.x;
745  double cpz = z - p3.z;
746  double cpx = x - p3.x;
747 
748  double aCROSSbp = az*bpx - ax*bpz;
749  double cCROSSap = cz*apx - cx*apz;
750  double bCROSScp = bz*cpx - bx*cpz;
751 
752  return ((aCROSSbp >= 0.0f) && (bCROSScp >= 0.0f) && (cCROSSap >= 0.0f));
753  }
754 
755  // Given a point and a line (defined by two points), compute the closest point
756  // in the line. (The line is treated as infinitely long.)
757  void NearestPointInLine(const Vector3d<Type> &point,
758  const Vector3d<Type> &line0,
759  const Vector3d<Type> &line1)
760  {
761  Vector3d<Type> &nearestPoint = *this;
762  Vector3d<Type> lineDelta = line1 - line0;
763 
764  // Handle degenerate lines
765  if ( lineDelta == Vector3d<Type>(0, 0, 0) )
766  {
767  nearestPoint = line0;
768  }
769  else
770  {
771  double delta = (point-line0).Dot(lineDelta) / (lineDelta).Dot(lineDelta);
772  nearestPoint = line0 + delta*lineDelta;
773  }
774  }
775 
776  // Given a point and a line segment (defined by two points), compute the closest point
777  // in the line. Cap the point at the endpoints of the line segment.
778  void NearestPointInLineSegment(const Vector3d<Type> &point,
779  const Vector3d<Type> &line0,
780  const Vector3d<Type> &line1)
781  {
782  Vector3d<Type> &nearestPoint = *this;
783  Vector3d<Type> lineDelta = line1 - line0;
784 
785  // Handle degenerate lines
786  if ( lineDelta == Vector3d<double>(0, 0, 0) )
787  {
788  nearestPoint = line0;
789  }
790  else
791  {
792  double delta = (point-line0).Dot(lineDelta) / (lineDelta).Dot(lineDelta);
793 
794  // Clamp the point to conform to the segment's endpoints
795  if ( delta < 0 )
796  delta = 0;
797  else if ( delta > 1 )
798  delta = 1;
799 
800  nearestPoint = line0 + delta*lineDelta;
801  }
802  }
803 
804  // Given a point and a plane (defined by three points), compute the closest point
805  // in the plane. (The plane is unbounded.)
806  void NearestPointInPlane(const Vector3d<Type> &point,
807  const Vector3d<Type> &triangle0,
808  const Vector3d<Type> &triangle1,
809  const Vector3d<Type> &triangle2)
810  {
811  Vector3d<Type> &nearestPoint = *this;
812  Vector3d<Type> lineDelta0 = triangle1 - triangle0;
813  Vector3d<Type> lineDelta1 = triangle2 - triangle0;
814  Vector3d<Type> pointDelta = point - triangle0;
815  Vector3d<Type> normal;
816 
817  // Get the normal of the polygon (doesn't have to be a unit vector)
818  normal.Cross(lineDelta0, lineDelta1);
819 
820  double delta = normal.Dot(pointDelta) / normal.Dot(normal);
821  nearestPoint = point - delta*normal;
822  }
823 
824  // Given a point and a plane (defined by a coplanar point and a normal), compute the closest point
825  // in the plane. (The plane is unbounded.)
826  void NearestPointInPlane(const Vector3d<Type> &point,
827  const Vector3d<Type> &planePoint,
828  const Vector3d<Type> &planeNormal)
829  {
830  Vector3d<Type> &nearestPoint = *this;
831  Vector3d<Type> pointDelta = point - planePoint;
832 
833  double delta = planeNormal.Dot(pointDelta) / planeNormal.Dot(planeNormal);
834  nearestPoint = point - delta*planeNormal;
835  }
836 
837  // Given a point and a triangle (defined by three points), compute the closest point
838  // in the triangle. Clamp the point so it's confined to the area of the triangle.
839  void NearestPointInTriangle(const Vector3d<Type> &point,
840  const Vector3d<Type> &triangle0,
841  const Vector3d<Type> &triangle1,
842  const Vector3d<Type> &triangle2)
843  {
844  static const Vector3d<Type> zeroVector(0, 0, 0);
845 
846  Vector3d<Type> &nearestPoint = *this;
847 
848  Vector3d<Type> lineDelta0 = triangle1 - triangle0;
849  Vector3d<Type> lineDelta1 = triangle2 - triangle0;
850 
851  // Handle degenerate triangles
852  if ( (lineDelta0 == zeroVector) || (lineDelta1 == zeroVector) )
853  {
854  nearestPoint.NearestPointInLineSegment(point, triangle1, triangle2);
855  }
856  else if ( lineDelta0 == lineDelta1 )
857  {
858  nearestPoint.NearestPointInLineSegment(point, triangle0, triangle1);
859  }
860 
861  else
862  {
863  static Vector3d<Type> axis[3];
864  axis[0].NearestPointInLine(triangle0, triangle1, triangle2);
865  axis[1].NearestPointInLine(triangle1, triangle0, triangle2);
866  axis[2].NearestPointInLine(triangle2, triangle0, triangle1);
867 
868  Type axisDot[3];
869  axisDot[0] = (triangle0-axis[0]).Dot(point-axis[0]);
870  axisDot[1] = (triangle1-axis[1]).Dot(point-axis[1]);
871  axisDot[2] = (triangle2-axis[2]).Dot(point-axis[2]);
872 
873  bool bForce = true;
874  Type bestMagnitude2 = 0;
875  Type closeMagnitude2;
876  Vector3d<double> closePoint;
877 
878  if ( axisDot[0] < 0 )
879  {
880  closePoint.NearestPointInLineSegment(point, triangle1, triangle2);
881  closeMagnitude2 = point.Distance2(closePoint);
882  if ( bForce || (bestMagnitude2 > closeMagnitude2) )
883  {
884  bForce = false;
885  bestMagnitude2 = closeMagnitude2;
886  nearestPoint = closePoint;
887  }
888  }
889  if ( axisDot[1] < 0 )
890  {
891  closePoint.NearestPointInLineSegment(point, triangle0, triangle2);
892  closeMagnitude2 = point.Distance2(closePoint);
893  if ( bForce || (bestMagnitude2 > closeMagnitude2) )
894  {
895  bForce = false;
896  bestMagnitude2 = closeMagnitude2;
897  nearestPoint = closePoint;
898  }
899  }
900  if ( axisDot[2] < 0 )
901  {
902  closePoint.NearestPointInLineSegment(point, triangle0, triangle1);
903  closeMagnitude2 = point.Distance2(closePoint);
904  if ( bForce || (bestMagnitude2 > closeMagnitude2) )
905  {
906  bForce = false;
907  bestMagnitude2 = closeMagnitude2;
908  nearestPoint = closePoint;
909  }
910  }
911 
912  // If bForce is true at this point, it means the nearest point lies
913  // inside the triangle; use the nearest-point-on-a-plane equation
914  if ( bForce )
915  {
916  Vector3d<Type> normal;
917 
918  // Get the normal of the polygon (doesn't have to be a unit vector)
919  normal.Cross(lineDelta0, lineDelta1);
920 
921  Vector3d<double> pointDelta = point - triangle0;
922  double delta = normal.Dot(pointDelta) / normal.Dot(normal);
923 
924  nearestPoint = point - delta*normal;
925  }
926  }
927  }
928 
929 
930  //private:
931 
932  Type x;
933  Type y;
934  Type z;
935  };
936 
937 
938  template <class Type> class Vector2d
939  {
940  public:
941  Vector2d(void) { } // null constructor, does not initialize point.
942 
943  Vector2d(const Vector2d &a) // constructor copies existing vector.
944  {
945  x = a.x;
946  y = a.y;
947  }
948 
949  Vector2d(const double *t)
950  {
951  x = t[0];
952  y = t[1];
953  }
954 
955 
956  Vector2d(Type a,Type b) // construct with initial point.
957  {
958  x = a;
959  y = b;
960  }
961 
962  const Type* Ptr() const { return &x; }
963  Type* Ptr() { return &x; }
964 
965  Vector2d & operator+=(const Vector2d &a) // += operator.
966  {
967  x+=a.x;
968  y+=a.y;
969  return *this;
970  }
971 
972  Vector2d & operator-=(const Vector2d &a)
973  {
974  x-=a.x;
975  y-=a.y;
976  return *this;
977  }
978 
979  Vector2d & operator*=(const Vector2d &a)
980  {
981  x*=a.x;
982  y*=a.y;
983  return *this;
984  }
985 
986  Vector2d & operator/=(const Vector2d &a)
987  {
988  x/=a.x;
989  y/=a.y;
990  return *this;
991  }
992 
993  bool operator==(const Vector2d<Type> &a) const
994  {
995  if ( a.x == x && a.y == y ) return true;
996  return false;
997  }
998 
999  bool operator!=(const Vector2d &a) const
1000  {
1001  if ( a.x != x || a.y != y ) return true;
1002  return false;
1003  }
1004 
1005  Vector2d operator+(Vector2d a) const
1006  {
1007  a.x+=x;
1008  a.y+=y;
1009  return a;
1010  }
1011 
1012  Vector2d operator-(Vector2d a) const
1013  {
1014  a.x = x-a.x;
1015  a.y = y-a.y;
1016  return a;
1017  }
1018 
1019  Vector2d operator - (void) const
1020  {
1021  return negative();
1022  }
1023 
1024  Vector2d operator*(Vector2d a) const
1025  {
1026  a.x*=x;
1027  a.y*=y;
1028  return a;
1029  }
1030 
1031  Vector2d operator*(Type c) const
1032  {
1033  Vector2d<Type> a;
1034 
1035  a.x = x * c;
1036  a.y = y * c;
1037 
1038  return a;
1039  }
1040 
1041  Vector2d operator/(Vector2d a) const
1042  {
1043  a.x = x/a.x;
1044  a.y = y/a.y;
1045  return a;
1046  }
1047 
1048 
1049  Type Dot(const Vector2d<Type> &a) const // computes dot product.
1050  {
1051  return (x * a.x + y * a.y );
1052  }
1053 
1054  Type GetX(void) const { return x; }
1055  Type GetY(void) const { return y; }
1056 
1057  void SetX(Type t) { x = t; }
1058  void SetY(Type t) { y = t; }
1059 
1060  void Set(Type a,Type b)
1061  {
1062  x = a;
1063  y = b;
1064  }
1065 
1066  void Zero(void)
1067  {
1068  x = y = 0;
1069  }
1070 
1071  Vector2d negative(void) const
1072  {
1073  Vector2d result;
1074  result.x = -x;
1075  result.y = -y;
1076  return result;
1077  }
1078 
1079  Type magnitude(void) const
1080  {
1081  return (Type) sqrtf(x * x + y * y );
1082  }
1083 
1084  Type fastmagnitude(void) const
1085  {
1086  return (Type) fast_sqrt(x * x + y * y );
1087  }
1088 
1089  Type fastermagnitude(void) const
1090  {
1091  return (Type) faster_sqrt( x * x + y * y );
1092  }
1093 
1094  void Reflection(Vector2d &a,Vector2d &b); // compute reflection vector.
1095 
1096  Type Length(void) const // length of vector.
1097  {
1098  return Type(sqrtf( x*x + y*y ));
1099  }
1100 
1101  Type FastLength(void) const // length of vector.
1102  {
1103  return Type(fast_sqrt( x*x + y*y ));
1104  }
1105 
1106  Type FasterLength(void) const // length of vector.
1107  {
1108  return Type(faster_sqrt( x*x + y*y ));
1109  }
1110 
1111  Type Length2(void) // squared distance, prior to square root.
1112  {
1113  return x*x+y*y;
1114  }
1115 
1116  Type Distance(const Vector2d &a) const // distance between two points.
1117  {
1118  Type dx = a.x - x;
1119  Type dy = a.y - y;
1120  Type d = dx*dx+dy*dy;
1121  return sqrtf(d);
1122  }
1123 
1124  Type FastDistance(const Vector2d &a) const // distance between two points.
1125  {
1126  Type dx = a.x - x;
1127  Type dy = a.y - y;
1128  Type d = dx*dx+dy*dy;
1129  return fast_sqrt(d);
1130  }
1131 
1132  Type FasterDistance(const Vector2d &a) const // distance between two points.
1133  {
1134  Type dx = a.x - x;
1135  Type dy = a.y - y;
1136  Type d = dx*dx+dy*dy;
1137  return faster_sqrt(d);
1138  }
1139 
1140  Type Distance2(Vector2d &a) // squared distance.
1141  {
1142  Type dx = a.x - x;
1143  Type dy = a.y - y;
1144  return dx*dx + dy *dy;
1145  }
1146 
1147  void Lerp(const Vector2d<Type>& from,const Vector2d<Type>& to,double slerp)
1148  {
1149  x = ((to.x - from.x)*slerp) + from.x;
1150  y = ((to.y - from.y)*slerp) + from.y;
1151  }
1152 
1153 
1154  void Cross(const Vector2d<Type> &a,const Vector2d<Type> &b) // cross two vectors result in this one.
1155  {
1156  x = a.y*b.x - a.x*b.y;
1157  y = 0; // Setting to 0, what would it mean actually to have a cross product in 2d?
1158  }
1159 
1160  Type Normalize(void) // normalize to a unit vector, returns distance.
1161  {
1162  Type l = Length();
1163  if ( l != 0 )
1164  {
1165  l = Type( 1 ) / l;
1166  x*=l;
1167  y*=l;
1168  }
1169  else
1170  {
1171  x = y = 0;
1172  }
1173  return l;
1174  }
1175 
1176  Type FastNormalize(void) // normalize to a unit vector, returns distance.
1177  {
1178  Type l = FastLength();
1179  if ( l != 0 )
1180  {
1181  l = Type( 1 ) / l;
1182  x*=l;
1183  y*=l;
1184  }
1185  else
1186  {
1187  x = y = 0;
1188  }
1189  return l;
1190  }
1191 
1192  Type FasterNormalize(void) // normalize to a unit vector, returns distance.
1193  {
1194  Type l = FasterLength();
1195  if ( l != 0 )
1196  {
1197  l = Type( 1 ) / l;
1198  x*=l;
1199  y*=l;
1200  }
1201  else
1202  {
1203  x = y = 0;
1204  }
1205  return l;
1206  }
1207 
1208 
1209  Type x;
1210  Type y;
1211  };
1212 
1213  class Line
1214  {
1215  public:
1216  Line(const Vector3d<double> &from,const Vector3d<double> &to)
1217  {
1218  mP1 = from;
1219  mP2 = to;
1220  }
1221  // JWR Test for the intersection of two lines.
1222 
1223  bool Intersect(const Line& src,Vector3d<double> &sect);
1224  private:
1225  Vector3d<double> mP1;
1226  Vector3d<double> mP2;
1227 
1228  };
1229 
1230 
1231  template <class Type> Vector3d<Type> operator * (Type s, const Vector3d<Type> &v )
1232  { Vector3d <Type> Scaled(v.x*s, v.y*s, v.z*s);
1233  return(Scaled); }
1234 
1235  template <class Type> Vector2d<Type> operator * (Type s, const Vector2d<Type> &v )
1236  { Vector2d <Type> Scaled(v.x*s, v.y*s);
1237  return(Scaled); }
1238 
1239 }
1240 
1241 #ifdef _MSC_VER
1242 # pragma warning( pop )
1243 #endif
1244 #endif
AGXCABLE_EXPORT SegmentDamage & operator+=(SegmentDamage &lhs, const SegmentDamage &rhs)
const Real DEG_TO_RAD
Definition: Math.h:69
ItT operator-(const ItT &it, const agx::ItOp &inc)
Definition: List.h:101
AgXString< T > operator+(const Indentation &indentation, const AgXString< T > &str)
Definition: Indentation.h:66
const Real RAD_TO_DEG
Definition: Math.h:70
bool operator!=(T val, InvalidIndexStruct)
Definition: agx.h:196
bool operator==(T val, InvalidIndexStruct)
Definition: agx.h:190
Vec3T< T > operator*(const Vec3T< T > &v, const AffineMatrix4x4T< T > &m)
LineT< Vec3 > Line
Definition: agx/Line.h:39