CHAI3D  3.0.0
CGeometry.h
Go to the documentation of this file.
1 //==============================================================================
2 /*
3  Software License Agreement (BSD License)
4  Copyright (c) 2003-2015, CHAI3D.
5  (www.chai3d.org)
6 
7  All rights reserved.
8 
9  Redistribution and use in source and binary forms, with or without
10  modification, are permitted provided that the following conditions
11  are met:
12 
13  * Redistributions of source code must retain the above copyright
14  notice, this list of conditions and the following disclaimer.
15 
16  * Redistributions in binary form must reproduce the above
17  copyright notice, this list of conditions and the following
18  disclaimer in the documentation and/or other materials provided
19  with the distribution.
20 
21  * Neither the name of CHAI3D nor the names of its contributors may
22  be used to endorse or promote products derived from this software
23  without specific prior written permission.
24 
25  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  POSSIBILITY OF SUCH DAMAGE.
37 
38  \author <http://www.chai3d.org>
39  \author Francois Conti
40  \version 3.0.0 $Rev: 1469 $
41 */
42 //==============================================================================
43 
44 //------------------------------------------------------------------------------
45 #ifndef CGeometryH
46 #define CGeometryH
47 //------------------------------------------------------------------------------
48 #include "math/CPolySolver.h"
49 //------------------------------------------------------------------------------
50 
51 //------------------------------------------------------------------------------
52 namespace chai3d {
53 //------------------------------------------------------------------------------
54 
55 //==============================================================================
63 //==============================================================================
64 
65 //------------------------------------------------------------------------------
69 //------------------------------------------------------------------------------
70 
72 
73 //==============================================================================
88 //==============================================================================
89 inline double cTriangleArea(const cVector3d& a_vertex0,
90  const cVector3d& a_vertex1,
91  const cVector3d& a_vertex2)
92 {
93  cVector3d u, v;
94  a_vertex1.subr(a_vertex0, u);
95  a_vertex2.subr(a_vertex0, v);
96  return (0.5 * (cCross(u,v).length()));
97 }
98 
99 
100 //==============================================================================
116 //==============================================================================
118  const cVector3d& a_planePoint,
119  const cVector3d& a_planeNormal)
120 {
121  cVector3d n = a_planeNormal;
122 
123  // compute a projection matrix
124  cMatrix3d projectionMatrix;
125  projectionMatrix.set(
126  (n(1) * n(1)) + (n(2) * n(2)), -(n(0) * n(1) ),-(n(0) * n(2)),
127  -(n(1) * n(0)), (n(0) * n(0)) + (n(2) * n(2) ),-(n(1) * n(2)),
128  -(n(2) * n(0)),-(n(2) * n(1)), (n(0) * n(0) ) + (n(1) * n(1))
129  );
130 
131  // project point on plane and return projected point.
132  cVector3d point;
133  a_point.subr(a_planePoint, point);
134  projectionMatrix.mul(point);
135  point.add(a_planePoint);
136 
137  // return result
138  return (point);
139 }
140 
141 
142 //==============================================================================
158 //==============================================================================
160  const cVector3d& a_planePoint0,
161  const cVector3d& a_planePoint1,
162  const cVector3d& a_planePoint2)
163 {
164  // create two vectors from the three input points lying in the projection plane.
165  cVector3d v01, v02;
166  a_planePoint1.subr(a_planePoint0, v01);
167  a_planePoint2.subr(a_planePoint0, v02);
168 
169  // compute the normal vector of the plane
170  cVector3d n;
171  v01.crossr(v02, n);
172  n.normalize();
173 
174  // return projected point
175  return (cProjectPointOnPlane(a_point,a_planePoint0,n));
176 }
177 
178 
179 //==============================================================================
209 //==============================================================================
210 inline void cProjectPointOnPlane(const cVector3d& a_point,
211  const cVector3d& a_planePoint0,
212  const cVector3d& a_planePoint1,
213  const cVector3d& a_planePoint2,
214  double& a_r01,
215  double& a_r02)
216 {
217  cVector3d v01 = cSub(a_planePoint1, a_planePoint0);
218  cVector3d v02 = cSub(a_planePoint2, a_planePoint0);
219  cVector3d point = cSub(a_point, a_planePoint0);
220 
221  // matrix
222  double m00 = v01(0) * v01(0) + v01(1) * v01(1) + v01(2) * v01(2);
223  double m01 = v01(0) * v02(0) + v01(1) * v02(1) + v01(2) * v02(2);
224  double m10 = m01;
225  double m11 = v02(0) * v02(0) + v02(1) * v02(1) + v02(2) * v02(2);
226  double det = m00 * m11 - m10 * m01;
227 
228  // vector
229  double vm0 = v01(0) * point(0) + v01(1) * point(1) + v01(2) * point(2);
230  double vm1 = v02(0) * point(0) + v02(1) * point(1) + v02(2) * point(2);
231 
232  // inverse
233  a_r01 = (1.0 / det) * ( m11 * vm0 - m01 * vm1);
234  a_r02 = (1.0 / det) * (-m10 * vm0 + m00 * vm1);
235 }
236 
237 
238 //==============================================================================
254 //==============================================================================
256  const cVector3d& a_pointOnLine,
257  const cVector3d& a_directionOfLine)
258 {
259  // temp variable
260  cVector3d point, result;
261 
262  // compute norm of line direction
263  double lengthDirSq = a_directionOfLine.lengthsq();
264 
265  if (lengthDirSq > 0.0)
266  {
267  a_point.subr(a_pointOnLine, point);
268 
269  a_directionOfLine.mulr( (point.dot(a_directionOfLine) / (lengthDirSq)),
270  result);
271 
272  result.add(a_pointOnLine);
273  return (result);
274  }
275  else
276  {
277  return (a_pointOnLine);
278  }
279 }
280 
281 
282 //==============================================================================
298 //==============================================================================
300  const cVector3d& a_segmentPointA,
301  const cVector3d& a_segmentPointB)
302 {
303  // if both points are equal
304  if (a_segmentPointA.equals(a_segmentPointB))
305  {
306  return (a_segmentPointA);
307  }
308 
309  // compute line
310  cVector3d segmentAB;
311  a_segmentPointB.subr(a_segmentPointA, segmentAB);
312 
313  // project tool onto segment
314  cVector3d projection = cProjectPointOnLine(a_point, a_segmentPointA, segmentAB);
315 
316  double distanceAB = segmentAB.lengthsq();
317  double distanceAP = cDistanceSq(projection, a_segmentPointA);
318  double distanceBP = cDistanceSq(projection, a_segmentPointB);
319 
320  if (distanceAP > distanceAB)
321  {
322  return(a_segmentPointB);
323  }
324  else
325  if (distanceBP > distanceAB)
326  {
327  return(a_segmentPointA);
328  }
329  else
330  {
331  return(projection);
332  }
333 }
334 
335 
336 //==============================================================================
356 //==============================================================================
358  const double& a_height,
359  const double& a_radius)
360 {
361  cVector3d center(0.0, 0.0, a_height);
362  cVector3d pointProj(a_point);
363 
364  pointProj(2) = a_height;
365 
366  cVector3d ray = cSub (pointProj, center);
367 
368  if (ray.length() < a_radius) return (pointProj);
369  else
370  {
371  ray.normalize();
372  return center + a_radius * ray;
373  }
374 }
375 
376 
377 //==============================================================================
392 //==============================================================================
393 inline cVector3d cProject(const cVector3d& a_vector0,
394  const cVector3d& a_vector1)
395 {
396  cVector3d result;
397 
398  // compute projection
399  double lengthSq = a_vector1.lengthsq();
400  a_vector1.mulr( (a_vector0.dot(a_vector1) / (lengthSq)), result);
401 
402  // return result
403  return (result);
404 }
405 
406 
407 //==============================================================================
423 //==============================================================================
424 inline double cDistanceToLine(const cVector3d& a_point,
425  const cVector3d& a_linePoint1,
426  const cVector3d& a_linePoint2)
427 {
428  cVector3d point = cProjectPointOnLine(a_point, a_linePoint1, cSub(a_linePoint2, a_linePoint1));
429  return (cDistance(a_point, point));
430 }
431 
432 
433 //==============================================================================
448 //==============================================================================
449 inline cVector3d cComputeSurfaceNormal(const cVector3d& a_surfacePoint0,
450  const cVector3d& a_surfacePoint1,
451  const cVector3d& a_surfacePoint2)
452 {
453  cVector3d v01, v02, result;
454 
455  // compute surface normal
456  a_surfacePoint1.subr(a_surfacePoint0, v01);
457  a_surfacePoint2.subr(a_surfacePoint0, v02);
458  v01.normalize();
459  v02.normalize();
460  v01.crossr(v02, result);
461  result.normalize();
462 
463  // return result
464  return (result);
465 }
466 
467 
468 //==============================================================================
486 //==============================================================================
487 inline bool cBoxContains(const cVector3d& a_point,
488  const cVector3d& a_boxMin,
489  const cVector3d& a_boxMax)
490 {
491  if ((a_point(0) >= a_boxMin(0) ) && (a_point(0) <= a_boxMax(0)) &&
492  (a_point(1) >= a_boxMin(1) ) && (a_point(1) <= a_boxMax(1)) &&
493  (a_point(2) >= a_boxMin(2) ) && (a_point(2) <= a_boxMax(2)))
494  {
495  return (true);
496  }
497  else
498  {
499  return (false);
500  }
501 }
502 
503 
504 //==============================================================================
533 //==============================================================================
534 inline int cIntersectionSegmentPlane(const cVector3d& a_segmentPointA,
535  const cVector3d& a_segmentPointB,
536  const cVector3d& a_planePos,
537  const cVector3d& a_planeNormal,
538  cVector3d& a_collisionPoint,
539  cVector3d& a_collisionNormal)
540 {
541  cVector3d d = a_segmentPointB - a_segmentPointA;
542  cVector3d p = a_segmentPointA;
543  double length = d.length();
544 
545  // sanity check
546  if (cZero(length))
547  {
548  return (0);
549  }
550 
551  // normalize d
552  d.mul(1.0 / length);
553 
554  // compute intersection between segment and disk plane
555  double c[2];
556  double s[1];
557 
558  c[0] = a_planeNormal(0)*p(0) - a_planeNormal(0)*a_planePos(0) +
559  a_planeNormal(1)*p(1) - a_planeNormal(1)*a_planePos(1) +
560  a_planeNormal(2)*p(2) - a_planeNormal(2)*a_planePos(2);
561  c[1] = a_planeNormal(0)*d(0) + a_planeNormal(1)*d(1) + a_planeNormal(2)*d(2);
562 
563  int num = cSolveLinear(c, s);
564 
565  if (num == 0)
566  {
567  return (0);
568  }
569  else
570  {
571  if ((s[0] >= 0.0) && (s[0] <= length))
572  {
573  a_collisionPoint = p + s[0] * d;
574  if (cDot(a_planeNormal, cSub(a_segmentPointA, a_collisionPoint)) >= 0.0)
575  {
576  a_collisionNormal = a_planeNormal;
577  }
578  else
579  {
580  a_collisionNormal =-a_planeNormal;
581  }
582  return (1);
583  }
584  else
585  {
586  return (0);
587  }
588  }
589 }
590 
591 
592 //==============================================================================
622 //==============================================================================
623 inline int cIntersectionSegmentDisk(const cVector3d& a_segmentPointA,
624  const cVector3d& a_segmentPointB,
625  const cVector3d& a_diskPos,
626  const cVector3d& a_diskNormal,
627  const double& a_diskRadius,
628  cVector3d& a_collisionPoint,
629  cVector3d& a_collisionNormal)
630 {
631  // compute intersection between segment and disk plane
632  if (cIntersectionSegmentPlane(a_segmentPointA,
633  a_segmentPointB,
634  a_diskPos,
635  a_diskNormal,
636  a_collisionPoint,
637  a_collisionNormal) == 0)
638  {
639  return (0);
640  }
641 
642  // check if intersection point within disk
643  if (cDistance(a_collisionPoint, a_diskPos) <= a_diskRadius)
644  {
645  return (1);
646  }
647  else
648  {
649  return (0);
650  }
651 }
652 
653 
654 //==============================================================================
690 //==============================================================================
691 inline int cIntersectionSegmentSphere(const cVector3d& a_segmentPointA,
692  const cVector3d& a_segmentPointB,
693  const cVector3d& a_spherePos,
694  const double& a_sphereRadius,
695  cVector3d& a_collisionPoint0,
696  cVector3d& a_collisionNormal0,
697  cVector3d& a_collisionPoint1,
698  cVector3d& a_collisionNormal1)
699 {
700  // temp variables
701  cVector3d AB, CA;
702  a_segmentPointB.subr(a_segmentPointA, AB);
703  a_segmentPointA.subr(a_spherePos, CA);
704  double radiusSq = a_sphereRadius * a_sphereRadius;
705 
706  double a = AB.lengthsq();
707  double b = 2.0 * cDot(AB, CA);
708  double c = CA.lengthsq() - radiusSq;
709 
710  // invalid segment
711  if (a == 0)
712  {
713  return (0);
714  }
715 
716  double d = b*b - 4*a*c;
717 
718  // segment ray is located outside of sphere
719  if (d < 0)
720  {
721  return (0);
722  }
723 
724  // segment ray intersects sphere
725  d = sqrt(d);
726  double e = 2.0 * a;
727 
728  // compute both solutions
729  double u0 = (-b + d) / e;
730  double u1 = (-b - d) / e;
731 
732  // check if the solutions are located along the segment AB
733  bool valid_u0 = cContains(u0, 0.0, 1.0);
734  bool valid_u1 = cContains(u1, 0.0, 1.0);
735 
736  // two intersection points are located along segment AB
737  if (valid_u0 && valid_u1)
738  {
739  if (u0 > u1) { cSwap(u0, u1); }
740 
741  // compute point 0
742  AB.mulr(u0, a_collisionPoint0);
743  a_collisionPoint0.add(a_segmentPointA);
744 
745  a_collisionPoint0.subr(a_spherePos, a_collisionNormal0);
746  a_collisionNormal0.normalize();
747 
748  // compute point 1
749  AB.mulr(u1, a_collisionPoint1);
750  a_collisionPoint1.add(a_segmentPointA);
751 
752  a_collisionPoint1.subr(a_spherePos, a_collisionNormal1);
753  a_collisionNormal1.normalize();
754 
755  return (2);
756  }
757 
758  // one intersection point is located along segment AB
759  else if (valid_u0)
760  {
761  // compute point 0
762  AB.mulr(u0, a_collisionPoint0);
763  a_collisionPoint0.add(a_segmentPointA);
764 
765  a_collisionPoint0.subr(a_spherePos, a_collisionNormal0);
766  a_collisionNormal0.normalize();
767 
768  // check dot product to see if the intial segment point is located
769  // inside the sphere.
770  double dotProduct = cDot(AB, a_collisionNormal0);
771  if (dotProduct < 0.0)
772  {
773  return (1);
774  }
775  else
776  {
777  return (0);
778  }
779  }
780 
781  // one intersection point is located along segment AB
782  else if (valid_u1)
783  {
784  // compute point 0
785  AB.mulr(u1, a_collisionPoint0);
786  a_collisionPoint0.add(a_segmentPointA);
787 
788  a_collisionPoint0.subr(a_spherePos, a_collisionNormal0);
789  a_collisionNormal0.normalize();
790 
791  // check dot product to see if the intial segment point is located
792  // inside the sphere.
793  double dotProduct = cDot(AB, a_collisionNormal0);
794  if (dotProduct < 0.0)
795  {
796  return (1);
797  }
798  else
799  {
800  return (0);
801  }
802  }
803 
804  // both points are located outside of the segment AB
805  else
806  {
807  return (0);
808  }
809 }
810 
811 
812 //==============================================================================
852 //==============================================================================
853 inline int cIntersectionSegmentToplessCylinder(const cVector3d& a_segmentPointA,
854  const cVector3d& a_segmentPointB,
855  const cVector3d& a_cylinderPointA,
856  const cVector3d& a_cylinderPointB,
857  const double& a_cylinderRadius,
858  cVector3d& a_collisionPoint0,
859  cVector3d& a_collisionNormal0,
860  double& a_collisionPointRelPosAB0,
861  cVector3d& a_collisionPoint1,
862  cVector3d& a_collisionNormal1,
863  double& a_collisionPointRelPosAB1)
864 {
865  // compute some initial values
866  cVector3d cylinderAB = a_cylinderPointB - a_cylinderPointA;
867  double lengthCylinderAB = cylinderAB.length();
868 
869  // sanity check
870  if (lengthCylinderAB == 0.0) { return (0); }
871 
872  // compute more initial values
873  cVector3d cylinderDir = cNormalize(cylinderAB);
874  cVector3d RC = a_segmentPointA - a_cylinderPointA;
875  cVector3d segmentAB = a_segmentPointB - a_segmentPointA;
876  cVector3d segmentDir = cNormalize(segmentAB);
877  cVector3d n = cCross(segmentDir, cylinderDir);
878 
879  // segment is parallel to cylinder
880  double length = n.length();
881  if (length == 0.0)
882  {
883  return (0);
884  }
885 
886  n.normalize();
887  double d = fabs (cDot (RC,n));
888 
889  if (d <= a_cylinderRadius)
890  {
891  cVector3d O = cCross(RC, cylinderDir);
892  double t = -cDot(O,n) / length;
893  O = cCross(n, cylinderDir);
894  O.normalize();
895  double s = fabs(sqrt(a_cylinderRadius*a_cylinderRadius - d*d) / cDot(segmentDir,O));
896  double u0 = t - s;
897  double u1 = t + s;
898 
899  // reorder solutions
900  if (u0 > u1) { cSwap (u0,u1); }
901 
902  bool valid_u0 = true;
903  bool valid_u1 = true;
904 
905  // check if solutions along segment
906  double lengthAB = segmentAB.length();
907  if (!cContains(u0, 0.0, lengthAB)) { valid_u0 = false; }
908  if (!cContains(u1, 0.0, lengthAB)) { valid_u1 = false; }
909 
910  // check if solutions lay along cylinder
911  cVector3d P0, P1;
912  cVector3d cylinderDirNeg = cNegate(cylinderDir);
913 
914  if (valid_u0)
915  {
916  P0 = a_segmentPointA + u0 * segmentDir;
917  cVector3d P0A = cSub(P0, a_cylinderPointA);
918  cVector3d P0B = cSub(P0, a_cylinderPointB);
919 
920  double cosAngleA = cCosAngle(cylinderDir, P0A);
921  double cosAngleB = cCosAngle(cylinderDirNeg, P0B);
922 
923  if ((cosAngleA <= 0.0) || (cosAngleB <= 0.0))
924  {
925  valid_u0 = false;
926  }
927  else
928  {
929  a_collisionPointRelPosAB0 = cosAngleA * (P0A.length() / lengthCylinderAB);
930  }
931  }
932 
933  if (valid_u1)
934  {
935  P1 = a_segmentPointA + u1 * segmentDir;
936  cVector3d P1A = cSub(P1, a_cylinderPointA);
937  cVector3d P1B = cSub(P1, a_cylinderPointB);
938 
939  double cosAngleA = cCosAngle(cylinderDir, P1A);
940  double cosAngleB = cCosAngle(cylinderDirNeg, P1B);
941 
942  if ((cosAngleA <= 0.0) || (cosAngleB <= 0.0))
943  {
944  valid_u1 = false;
945  }
946  else
947  {
948  a_collisionPointRelPosAB1 = cosAngleA * (P1A.length() / lengthCylinderAB);
949  }
950  }
951 
952  if (valid_u0 && valid_u1)
953  {
954  a_collisionPoint0 = P0;
955  a_collisionNormal0 = cNormalize(cCross(cylinderDir, cCross(cSub(P0, a_cylinderPointA), cylinderDir)));
956  a_collisionPoint1 = P1;
957  a_collisionNormal1 = cNormalize(cCross(cylinderDir, cCross(cSub(P1, a_cylinderPointA), cylinderDir)));
958  return (2);
959  }
960  else if (valid_u0)
961  {
962  a_collisionPoint0 = P0;
963  a_collisionNormal0 = cNormalize(cCross(cylinderDir, cCross(cSub(P0, a_cylinderPointA), cylinderDir)));
964 
965  // check dot product to see if the intial segment point is located inside the cylinder.
966  double dotProduct = cDot(segmentAB, a_collisionNormal0);
967  if (dotProduct < 0.0)
968  {
969  return (1);
970  }
971  else
972  {
973  return (0);
974  }
975  }
976  else if (valid_u1)
977  {
978  a_collisionPoint0 = P1;
979  a_collisionNormal0 = cNormalize(cCross(cylinderDir, cCross(cSub(P1, a_cylinderPointA), cylinderDir)));
980 
981  // check dot product to see if the intial segment point is located inside the cylinder.
982  double dotProduct = cDot(segmentAB, a_collisionNormal0);
983  if (dotProduct < 0.0)
984  {
985  return (1);
986  }
987  else
988  {
989  return (0);
990  }
991  }
992  }
993 
994  return (0);
995 }
996 
997 
998 //==============================================================================
1031 //==============================================================================
1032 inline int cIntersectionSegmentCylinder(const cVector3d& a_segmentPointA,
1033  const cVector3d& a_segmentPointB,
1034  const double& a_baseRadius,
1035  const double& a_topRadius,
1036  const double& a_height,
1037  cVector3d& a_collisionPoint,
1038  cVector3d& a_collisionNormal)
1039 {
1040  // initialization
1041  cVector3d p = a_segmentPointA;
1042  cVector3d d = a_segmentPointB - a_segmentPointA;
1043  double length = d.length();
1044 
1045  // sanity check
1046  if (cZero(length))
1047  {
1048  return (0);
1049  }
1050  d.mul(1.0 / length);
1051 
1052  // compute collision with cylinder side
1053  double A = a_baseRadius;
1054  double B = (a_topRadius - a_baseRadius) / a_height;
1055 
1056  double c[3];
1057  double s[2];
1058 
1059  c[0] =-2.0*A*B*p(2) - cSqr(B*p(2)) - cSqr(A) + cSqr(p(0)) + cSqr(p(1));
1060  c[1] = 2.0*p(0)*d(0) + 2.0*p(1)*d(1) - 2.0*A*B*d(2) - 2.0*cSqr(B)*p(2)*d(2);
1061  c[2] = cSqr(d(0)) + cSqr(d(1)) - cSqr(B*d(2));
1062 
1063  int numCol = cSolveQuadric(c, s);
1064 
1065  // compute collision with top cap
1066  cVector3d collisionPointTop, collisionNormalTop;
1067 
1068  int numColTop = cIntersectionSegmentDisk(a_segmentPointA,
1069  a_segmentPointB,
1070  cVector3d(0.0, 0.0, a_height),
1071  cVector3d(0.0, 0.0, 1.0),
1072  a_topRadius,
1073  collisionPointTop,
1074  collisionNormalTop);
1075 
1076  // compute collision with base cap
1077  cVector3d collisionPointBase, collisionNormalBase;
1078 
1079  int numColBase = cIntersectionSegmentDisk(a_segmentPointA,
1080  a_segmentPointB,
1081  cVector3d(0.0, 0.0, 0.0),
1082  cVector3d(0.0, 0.0,-1.0),
1083  a_baseRadius,
1084  collisionPointBase,
1085  collisionNormalBase);
1086 
1087  // evaluate solutions
1088  bool hit = false;
1089  double distance = C_LARGE;
1090 
1091  // check top cap
1092  if (numColTop > 0)
1093  {
1094  hit = true;
1095  distance = cDistance(a_segmentPointA, collisionPointTop);
1096  a_collisionPoint = collisionPointTop;
1097  a_collisionNormal = cVector3d(0.0, 0.0, 1.0);
1098  }
1099 
1100  // check base cap
1101  if (numColBase > 0)
1102  {
1103  hit = true;
1104  double dist = cDistance(a_segmentPointA, collisionPointBase);
1105  if (dist < distance)
1106  {
1107  a_collisionPoint = collisionPointBase;
1108  a_collisionNormal = cVector3d(0.0, 0.0,-1.0);
1109  distance = dist;
1110  }
1111  }
1112 
1113  // check cylinder solution 1
1114  if (numCol > 0)
1115  {
1116  if ((s[0] >= 0.0) && (s[0] <= length) && (s[0] < distance))
1117  {
1118  cVector3d collisionPoint = p + d * s[0];
1119  if ((collisionPoint(2) > 0.0) && (collisionPoint(2) < a_height))
1120  {
1121  cVector3d normal(collisionPoint(0), collisionPoint(1), 0.0);
1122  normal.normalize();
1123  normal(2) = (a_baseRadius - a_topRadius) / a_height;
1124  a_collisionNormal = normal;
1125  a_collisionPoint = collisionPoint;
1126  distance = s[0];
1127  hit = true;
1128  }
1129  }
1130  }
1131 
1132  // check cylinder solution 2
1133  if (numCol > 1)
1134  {
1135  if ((s[1] >= 0.0) && (s[1] <= length) && (s[1] < distance))
1136  {
1137  cVector3d collisionPoint = p + d * s[1];
1138  if ((collisionPoint(2) > 0.0) && (collisionPoint(2) < a_height))
1139  {
1140  cVector3d normal(collisionPoint(0), collisionPoint(1), 0.0);
1141  normal.normalize();
1142  normal(2) = (a_baseRadius - a_topRadius) / a_height;
1143  a_collisionNormal = normal;
1144  a_collisionPoint = collisionPoint;
1145  hit = true;
1146  }
1147  }
1148  }
1149 
1150  if (hit)
1151  {
1152  return (1);
1153  }
1154  else
1155  {
1156  return (0);
1157  }
1158 }
1159 
1160 
1161 //==============================================================================
1191 //==============================================================================
1192 inline int cIntersectionSegmentBox(const cVector3d& a_segmentPointA,
1193  const cVector3d& a_segmentPointB,
1194  const cVector3d& a_boxMin,
1195  const cVector3d& a_boxMax,
1196  cVector3d& a_collisionPoint,
1197  cVector3d& a_collisionNormal)
1198 {
1199  double tmin = 0.0;
1200  double tmax = C_LARGE;
1201 
1202  cVector3d d = a_segmentPointB - a_segmentPointA;
1203  double distance = d.length();
1204  if (distance < C_SMALL)
1205  {
1206  return(0);
1207  }
1208  d.normalize();
1209 
1210  // for all three slabs
1211  double normalDir = 1.0;
1212  int normalAxis = 0;
1213 
1214  for (int i=0; i<3; i++)
1215  {
1216  if (fabs(d(i)) < C_SMALL)
1217  {
1218  // ray is parallel to slab. No hit if origin not within slab
1219  if (a_segmentPointA(i) < a_boxMin(i) || a_segmentPointA(i) > a_boxMax(i))
1220  {
1221  return (0);
1222  }
1223  }
1224  else
1225  {
1226  // compute intersection t value of ray with near and far plane of slab
1227  double ood = 1.0 / d(i);
1228  double t1 = (a_boxMin(i) - a_segmentPointA(i)) * ood;
1229  double t2 = (a_boxMax(i) - a_segmentPointA(i)) * ood;
1230  int n = 1;
1231 
1232  // make t1 be intersection with near plane, t2 with far plane
1233  if (t1 > t2)
1234  {
1235  cSwap(t1, t2);
1236  n = -1;
1237  }
1238 
1239  // compute the intersection of slab intersection intervals
1240  if (t1 > tmin)
1241  {
1242  tmin = t1;
1243  normalAxis = i;
1244  normalDir = -n;
1245  }
1246 
1247  if (t2 < tmax)
1248  {
1249  tmax = t2;
1250  }
1251 
1252  // exit with no collision as soon as slab intersection becomes empty
1253  if (tmin > tmax)
1254  {
1255  return (0);
1256  }
1257  }
1258  }
1259 
1260  if (tmin > distance)
1261  {
1262  return (0);
1263  }
1264 
1265  // compute collision point and surface normal
1266  a_collisionPoint = a_segmentPointA + cMul(tmin, d);
1267  a_collisionNormal.zero();
1268  a_collisionNormal(normalAxis) = normalDir;
1269 
1270  return (1);
1271 }
1272 
1273 
1274 //==============================================================================
1304 //==============================================================================
1305 inline int cIntersectionSegmentTorus(const cVector3d& a_segmentPointA,
1306  const cVector3d& a_segmentPointB,
1307  const double& a_innerRadius,
1308  const double& a_outerRadius,
1309  cVector3d& a_collisionPoint,
1310  cVector3d& a_collisionNormal)
1311 {
1312  cVector3d collisionPoint, collisionNormal;
1313 
1314  // compute boundary box
1315  double width = a_outerRadius + a_innerRadius;
1316  cVector3d boxMin(-width,-width,-a_innerRadius);
1317  cVector3d boxMax( width, width, a_innerRadius);
1318 
1319  // check collision with boundary box
1320  if (cIntersectionSegmentBox(a_segmentPointA,
1321  a_segmentPointB,
1322  boxMin,
1323  boxMax,
1324  collisionPoint,
1325  collisionNormal) == 0)
1326  {
1327  return (0);
1328  }
1329 
1330  cVector3d p = a_segmentPointA;
1331  cVector3d d = cNormalize(a_segmentPointB - a_segmentPointA);
1332 
1333  double dd = cDot(d, d);
1334  double pd = cDot(p, d);
1335  double pp = cDot(p, p);
1336  double r2 = cSqr(a_innerRadius);
1337  double R2 = cSqr(a_outerRadius);
1338 
1339  double c[5];
1340  double s[4];
1341 
1342  c[4] = cSqr(dd);
1343  c[3] = 4.0 * dd * pd;
1344  c[2] = 4.0 * cSqr(pd) + 2.0 * dd * (pp - r2 - R2) + 4.0 * R2 * cSqr(d(2));
1345  c[1] = 4.0 * pd * (pp - r2 - R2) + 8.0 * R2 * p(2) * d(2);
1346  c[0] = cSqr(pp - r2 - R2) + 4.0 * R2 * cSqr(p(2)) - 4.0 * R2 * r2;
1347 
1348  int num = cSolveQuartic(c, s);
1349 
1350  if (num == 0)
1351  {
1352  // no solutions found
1353  return (0);
1354  }
1355 
1356  // search for nearest solution (sollision point)
1357  double sol = C_LARGE;
1358  for (int i=0; i<num; i++)
1359  {
1360  if (s[i] < sol)
1361  {
1362  sol = s[i];
1363  }
1364  }
1365 
1366  // compute point
1367  a_collisionPoint = p + sol * d;
1368 
1369  // compute surface normal
1370  cVector3d pt = cMul(a_outerRadius, cNormalize(cVector3d(a_collisionPoint(0), a_collisionPoint(1), 0.0)));
1371  a_collisionNormal = cNormalize(a_collisionPoint - pt);
1372 
1373  // return one collision point detected
1374  return (1);
1375 }
1376 
1377 
1378 //==============================================================================
1411 //==============================================================================
1412 inline bool cIntersectionSegmentTriangle(const cVector3d& a_segmentPointA,
1413  const cVector3d& a_segmentPointB,
1414  const cVector3d& a_triangleVertex0,
1415  const cVector3d& a_triangleVertex1,
1416  const cVector3d& a_triangleVertex2,
1417  const bool a_reportFrontSideCollision,
1418  const bool a_reportBackSideCollision,
1419  cVector3d& a_collisionPoint,
1420  cVector3d& a_collisionNormal,
1421  double& a_collisionPosVertex01,
1422  double& a_collisionPosVertex02
1423  )
1424 {
1425  // This value controls how close rays can be to parallel to the triangle
1426  // surface before we discard them
1427  const double C_INTERSECT_EPSILON = 10e-14f;
1428 
1429  // compute a ray and check its length
1430  cVector3d rayDir;
1431  a_segmentPointB.subr(a_segmentPointA, rayDir);
1432  double segmentLengthSquare = rayDir.lengthsq();
1433  if (segmentLengthSquare == 0.0) { return (false); }
1434 
1435  // Compute the triangle's normal
1436  cVector3d t_E0, t_E1, t_N;
1437  a_triangleVertex1.subr(a_triangleVertex0, t_E0);
1438  a_triangleVertex2.subr(a_triangleVertex0, t_E1);
1439  t_E0.crossr(t_E1, t_N);
1440 
1441  // If the ray is parallel to the triangle (perpendicular to the
1442  // normal), there's no collision
1443  double dotProduct = t_N.dot(rayDir);
1444  if (fabs(dotProduct)<10E-15f) return (false);
1445 
1446  if ((dotProduct < 0.0) && (a_reportFrontSideCollision == false)) return (false);
1447  if ((dotProduct > 0.0) && (a_reportBackSideCollision == false)) return (false);
1448 
1449  double t_T = cDot(t_N, cSub(a_triangleVertex0, a_segmentPointA)) / cDot(t_N, rayDir);
1450 
1451  if (t_T + C_INTERSECT_EPSILON < 0) return (false);
1452 
1453  cVector3d t_Q = cSub(cAdd(a_segmentPointA, cMul(t_T, rayDir)), a_triangleVertex0);
1454  double t_Q0 = cDot(t_E0,t_Q);
1455  double t_Q1 = cDot(t_E1,t_Q);
1456  double t_E00 = cDot(t_E0,t_E0);
1457  double t_E01 = cDot(t_E0,t_E1);
1458  double t_E11 = cDot(t_E1,t_E1);
1459  double t_D = (t_E00 * t_E11) - (t_E01 * t_E01);
1460 
1461  if ((t_D > -C_INTERSECT_EPSILON) && (t_D < C_INTERSECT_EPSILON)) return(false);
1462 
1463  double t_S0 = ((t_E11 * t_Q0) - (t_E01 * t_Q1)) / t_D;
1464  double t_S1 = ((t_E00 * t_Q1) - (t_E01 * t_Q0)) / t_D;
1465 
1466  // Collision has occurred. It is reported.
1467  if (
1468  (t_S0 >= 0.0 - C_INTERSECT_EPSILON) &&
1469  (t_S1 >= 0.0 - C_INTERSECT_EPSILON) &&
1470  ((t_S0 + t_S1) <= 1.0 + C_INTERSECT_EPSILON)
1471  )
1472  {
1473  cVector3d t_I = cAdd(a_triangleVertex0, cMul(t_S0,t_E0), cMul(t_S1, t_E1));
1474 
1475  // Square distance between ray origin and collision point.
1476  double distanceSquare = a_segmentPointA.distancesq(t_I);
1477 
1478  // check if collision occurred within segment. If yes, report collision
1479  if (distanceSquare <= segmentLengthSquare)
1480  {
1481  a_collisionPoint = cAdd(a_segmentPointA, cMul(t_T, rayDir));
1482  t_N.normalizer(a_collisionNormal);
1483  if (cCosAngle(a_collisionNormal, rayDir) > 0.0)
1484  {
1485  a_collisionNormal.negate();
1486  }
1487 
1488  a_collisionPosVertex01 = t_S0;
1489  a_collisionPosVertex02 = t_S1;
1490 
1491  return (true);
1492  }
1493  }
1494 
1495  // no collision occurred
1496  return (false);
1497 }
1498 
1500 
1501 //------------------------------------------------------------------------------
1502 } // namespace chai3d
1503 //------------------------------------------------------------------------------
1504 
1505 //------------------------------------------------------------------------------
1506 #endif // CGeometryH
1507 //------------------------------------------------------------------------------
1508 
1509 
This class implements a 3D vector.
Definition: CVector3d.h:85
cVector3d cProjectPointOnLine(const cVector3d &a_point, const cVector3d &a_pointOnLine, const cVector3d &a_directionOfLine)
This function computes the projection of a point onto a line.
Definition: CGeometry.h:255
const double C_SMALL
Small value near zero.
Definition: CConstants.h:103
cVector3d cProject(const cVector3d &a_vector0, const cVector3d &a_vector1)
This function computes the projection of a vector V0 onto a vector V1.
Definition: CGeometry.h:393
double cDistance(const cVector3d &a_point1, const cVector3d &a_point2)
This function computes the Euclidean distance between two points.
Definition: CMaths.h:949
void normalize()
This method normalizes this vector to length 1.
Definition: CVector3d.h:1048
void zero()
This method clears all vector components with zeros.
Definition: CVector3d.h:253
bool equals(const cVector3d &a_vector, const double a_epsilon=0.0) const
This method performs an equality test between two vectors.
Definition: CVector3d.h:1195
cVector3d cComputeSurfaceNormal(const cVector3d &a_surfacePoint0, const cVector3d &a_surfacePoint1, const cVector3d &a_surfacePoint2)
This function computes the surface normal of a plane.
Definition: CGeometry.h:449
int cIntersectionSegmentPlane(const cVector3d &a_segmentPointA, const cVector3d &a_segmentPointB, const cVector3d &a_planePos, const cVector3d &a_planeNormal, cVector3d &a_collisionPoint, cVector3d &a_collisionNormal)
This function computes the intersection between a segment and a plane.
Definition: CGeometry.h:534
double distancesq(const cVector3d &a_vector) const
This method computes the squared value distance between two points.
Definition: CVector3d.h:1166
int cSolveQuartic(double a_coefficient[5], double a_solution[4])
This function computes the solution of a quartic equation.
Definition: CPolySolver.h:272
void crossr(const cVector3d &a_vector, cVector3d &a_result) const
This method computes the cross product.
Definition: CVector3d.h:969
double cCosAngle(const cVector3d &a_vector1, const cVector3d &a_vector2)
This function computes the cosine of the angle between two vectors.
Definition: CMaths.h:1420
cVector3d cAdd(const cVector3d &a_vector1, const cVector3d &a_vector2)
This function computes the addition of two vectors.
Definition: CMaths.h:704
cVector3d cProjectPointOnSegment(const cVector3d &a_point, const cVector3d &a_segmentPointA, const cVector3d &a_segmentPointB)
This function computes the projection of a point onto a segment.
Definition: CGeometry.h:299
int cSolveQuadric(double a_coefficient[3], double a_solution[2])
This function computes the solution of a quadric equation.
Definition: CPolySolver.h:121
void normalizer(cVector3d &a_result) const
This method normalizes this vector to length 1.
Definition: CVector3d.h:1080
double length() const
This method computes the Euclidean norm of this vector.
Definition: CVector3d.h:1009
cVector3d cSub(const cVector3d &a_vector1, const cVector3d &a_vector2)
This function computes the subtraction of two vectors.
Definition: CMaths.h:765
double cDistanceToLine(const cVector3d &a_point, const cVector3d &a_linePoint1, const cVector3d &a_linePoint2)
This function computes the distance between a point and a line.
Definition: CGeometry.h:424
void mul(const cMatrix3d &a_matrix)
This function computes the multiplication of this matrix with another.
Definition: CMatrix3d.h:883
void mulr(const double &a_scalar, cVector3d &a_result) const
This method computes the multiplication of this vector with a scalar.
Definition: CVector3d.h:733
cVector3d cProjectPointOnDiskXY(const cVector3d &a_point, const double &a_height, const double &a_radius)
This function computes the projection of a point onto a disk.
Definition: CGeometry.h:357
double cTriangleArea(const cVector3d &a_vertex0, const cVector3d &a_vertex1, const cVector3d &a_vertex2)
This function computes the area of a triangle.
Definition: CGeometry.h:89
double cDistanceSq(const cVector3d &a_point1, const cVector3d &a_point2)
This function computes the squared distance between two points.
Definition: CMaths.h:971
bool cBoxContains(const cVector3d &a_point, const cVector3d &a_boxMin, const cVector3d &a_boxMax)
This function checks if a point is contained in a box.
Definition: CGeometry.h:487
int cIntersectionSegmentTorus(const cVector3d &a_segmentPointA, const cVector3d &a_segmentPointB, const double &a_innerRadius, const double &a_outerRadius, cVector3d &a_collisionPoint, cVector3d &a_collisionNormal)
This function computes the nearest intersection point between a segment and a torus.
Definition: CGeometry.h:1305
This class implements a 3D matrix.
Definition: CMatrix3d.h:97
void negate()
This method computes the negated vector.
Definition: CVector3d.h:894
int cSolveLinear(double a_coefficient[2], double a_solution[1])
This function computes the solution of a linear equation.
Definition: CPolySolver.h:91
cVector3d cMul(const double &a_value, const cVector3d &a_vector)
This function computes the multiplication of a vector by a scalar.
Definition: CMaths.h:820
double lengthsq() const
This method computes the squared value of the Euclidean norm of this vector.
Definition: CVector3d.h:1027
int cIntersectionSegmentToplessCylinder(const cVector3d &a_segmentPointA, const cVector3d &a_segmentPointB, const cVector3d &a_cylinderPointA, const cVector3d &a_cylinderPointB, const double &a_cylinderRadius, cVector3d &a_collisionPoint0, cVector3d &a_collisionNormal0, double &a_collisionPointRelPosAB0, cVector3d &a_collisionPoint1, cVector3d &a_collisionNormal1, double &a_collisionPointRelPosAB1)
This function computes the intersection between a segment and a topless cylinder. ...
Definition: CGeometry.h:853
double cDot(const cVector3d &a_vector1, const cVector3d &a_vector2)
This function computes the dot product between two vectors.
Definition: CMaths.h:905
void set(const double &a_value)
This method initializes all elements of this matrix with an input value.
Definition: CMatrix3d.h:345
Implements polynomial solvers.
void cSwap(T &a_value1, T &a_value2)
This function swaps two elements.
Definition: CMaths.h:309
cVector3d cProjectPointOnPlane(const cVector3d &a_point, const cVector3d &a_planePoint, const cVector3d &a_planeNormal)
This function computes the projection of a point onto a plane.
Definition: CGeometry.h:117
bool cIntersectionSegmentTriangle(const cVector3d &a_segmentPointA, const cVector3d &a_segmentPointB, const cVector3d &a_triangleVertex0, const cVector3d &a_triangleVertex1, const cVector3d &a_triangleVertex2, const bool a_reportFrontSideCollision, const bool a_reportBackSideCollision, cVector3d &a_collisionPoint, cVector3d &a_collisionNormal, double &a_collisionPosVertex01, double &a_collisionPosVertex02)
This function computes the intersection between a segment and a triangle.
Definition: CGeometry.h:1412
cVector3d cNormalize(const cVector3d &a_vector)
This function computes the normalized vector.
Definition: CMaths.h:926
double cSqr(const double &a_value)
This function computes the square of a scalar.
Definition: CMaths.h:452
bool cContains(const T &a_value, const V &a_low, const V &a_high)
This function checks whether a value is within a specified range.
Definition: CMaths.h:427
void subr(const cVector3d &a_vector, cVector3d &a_result) const
This method computes the subtraction of this vector with another.
Definition: CVector3d.h:616
int cIntersectionSegmentCylinder(const cVector3d &a_segmentPointA, const cVector3d &a_segmentPointB, const double &a_baseRadius, const double &a_topRadius, const double &a_height, cVector3d &a_collisionPoint, cVector3d &a_collisionNormal)
This function computes the nearest intersection point between a segment and a cylinder.
Definition: CGeometry.h:1032
const double C_LARGE
Biggest value for a double.
Definition: CConstants.h:109
cVector3d cNegate(const cVector3d &a_vector)
This function computes the negated vector.
Definition: CMaths.h:793
cVector3d cCross(const cVector3d &a_vector1, const cVector3d &a_vector2)
This function computes the cross product between two vectors.
Definition: CMaths.h:877
void mul(const double &a_scalar)
This method computes the multiplication of this vector with a scalar.
Definition: CVector3d.h:674
double dot(const cVector3d &a_vector) const
This method computes the dot product between this vector and another.
Definition: CVector3d.h:992
Definition: CAudioBuffer.cpp:56
int cIntersectionSegmentDisk(const cVector3d &a_segmentPointA, const cVector3d &a_segmentPointB, const cVector3d &a_diskPos, const cVector3d &a_diskNormal, const double &a_diskRadius, cVector3d &a_collisionPoint, cVector3d &a_collisionNormal)
This function computes the intersection between a segment and a disk.
Definition: CGeometry.h:623
int cIntersectionSegmentBox(const cVector3d &a_segmentPointA, const cVector3d &a_segmentPointB, const cVector3d &a_boxMin, const cVector3d &a_boxMax, cVector3d &a_collisionPoint, cVector3d &a_collisionNormal)
This function computes the nearest intersection point between a segment and a box.
Definition: CGeometry.h:1192
void add(const cVector3d &a_vector)
This method computes the addition of this vector with another.
Definition: CVector3d.h:446
bool cZero(const double &a_value)
This function checks if a value is equal to or almost near zero.
Definition: CMaths.h:153
int cIntersectionSegmentSphere(const cVector3d &a_segmentPointA, const cVector3d &a_segmentPointB, const cVector3d &a_spherePos, const double &a_sphereRadius, cVector3d &a_collisionPoint0, cVector3d &a_collisionNormal0, cVector3d &a_collisionPoint1, cVector3d &a_collisionNormal1)
This function computes the intersection between a segment and a sphere.
Definition: CGeometry.h:691

CHAI3D 3.0.0 documentation
Please address any questions to support@chai3d.org
(C) 2003-2015 - CHAI3D
All Rights Reserved.