94 a_vertex1.
subr(a_vertex0, u);
95 a_vertex2.
subr(a_vertex0, v);
96 return (0.5 * (
cCross(u,v).length()));
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))
133 a_point.
subr(a_planePoint, point);
134 projectionMatrix.
mul(point);
135 point.
add(a_planePoint);
166 a_planePoint1.
subr(a_planePoint0, v01);
167 a_planePoint2.
subr(a_planePoint0, v02);
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);
225 double m11 = v02(0) * v02(0) + v02(1) * v02(1) + v02(2) * v02(2);
226 double det = m00 * m11 - m10 * m01;
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);
233 a_r01 = (1.0 / det) * ( m11 * vm0 - m01 * vm1);
234 a_r02 = (1.0 / det) * (-m10 * vm0 + m00 * vm1);
263 double lengthDirSq = a_directionOfLine.
lengthsq();
265 if (lengthDirSq > 0.0)
267 a_point.
subr(a_pointOnLine, point);
269 a_directionOfLine.
mulr( (point.
dot(a_directionOfLine) / (lengthDirSq)),
272 result.
add(a_pointOnLine);
277 return (a_pointOnLine);
304 if (a_segmentPointA.
equals(a_segmentPointB))
306 return (a_segmentPointA);
311 a_segmentPointB.
subr(a_segmentPointA, segmentAB);
316 double distanceAB = segmentAB.
lengthsq();
317 double distanceAP =
cDistanceSq(projection, a_segmentPointA);
318 double distanceBP =
cDistanceSq(projection, a_segmentPointB);
320 if (distanceAP > distanceAB)
322 return(a_segmentPointB);
325 if (distanceBP > distanceAB)
327 return(a_segmentPointA);
358 const double& a_height,
359 const double& a_radius)
364 pointProj(2) = a_height;
368 if (ray.
length() < a_radius)
return (pointProj);
372 return center + a_radius * ray;
399 double lengthSq = a_vector1.
lengthsq();
400 a_vector1.
mulr( (a_vector0.
dot(a_vector1) / (lengthSq)), result);
456 a_surfacePoint1.
subr(a_surfacePoint0, v01);
457 a_surfacePoint2.
subr(a_surfacePoint0, v02);
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)))
541 cVector3d d = a_segmentPointB - a_segmentPointA;
543 double length = d.
length();
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);
571 if ((s[0] >= 0.0) && (s[0] <= length))
573 a_collisionPoint = p + s[0] * d;
574 if (
cDot(a_planeNormal,
cSub(a_segmentPointA, a_collisionPoint)) >= 0.0)
576 a_collisionNormal = a_planeNormal;
580 a_collisionNormal =-a_planeNormal;
627 const double& a_diskRadius,
637 a_collisionNormal) == 0)
643 if (
cDistance(a_collisionPoint, a_diskPos) <= a_diskRadius)
694 const double& a_sphereRadius,
702 a_segmentPointB.
subr(a_segmentPointA, AB);
703 a_segmentPointA.
subr(a_spherePos, CA);
704 double radiusSq = a_sphereRadius * a_sphereRadius;
707 double b = 2.0 *
cDot(AB, CA);
708 double c = CA.
lengthsq() - radiusSq;
716 double d = b*b - 4*a*c;
729 double u0 = (-b + d) / e;
730 double u1 = (-b - d) / e;
737 if (valid_u0 && valid_u1)
739 if (u0 > u1) {
cSwap(u0, u1); }
742 AB.
mulr(u0, a_collisionPoint0);
743 a_collisionPoint0.
add(a_segmentPointA);
745 a_collisionPoint0.
subr(a_spherePos, a_collisionNormal0);
749 AB.
mulr(u1, a_collisionPoint1);
750 a_collisionPoint1.
add(a_segmentPointA);
752 a_collisionPoint1.
subr(a_spherePos, a_collisionNormal1);
762 AB.
mulr(u0, a_collisionPoint0);
763 a_collisionPoint0.
add(a_segmentPointA);
765 a_collisionPoint0.
subr(a_spherePos, a_collisionNormal0);
770 double dotProduct =
cDot(AB, a_collisionNormal0);
771 if (dotProduct < 0.0)
785 AB.
mulr(u1, a_collisionPoint0);
786 a_collisionPoint0.
add(a_segmentPointA);
788 a_collisionPoint0.
subr(a_spherePos, a_collisionNormal0);
793 double dotProduct =
cDot(AB, a_collisionNormal0);
794 if (dotProduct < 0.0)
857 const double& a_cylinderRadius,
860 double& a_collisionPointRelPosAB0,
863 double& a_collisionPointRelPosAB1)
866 cVector3d cylinderAB = a_cylinderPointB - a_cylinderPointA;
867 double lengthCylinderAB = cylinderAB.
length();
870 if (lengthCylinderAB == 0.0) {
return (0); }
874 cVector3d RC = a_segmentPointA - a_cylinderPointA;
875 cVector3d segmentAB = a_segmentPointB - a_segmentPointA;
880 double length = n.
length();
887 double d = fabs (
cDot (RC,n));
889 if (d <= a_cylinderRadius)
892 double t = -
cDot(O,n) / length;
893 O =
cCross(n, cylinderDir);
895 double s = fabs(sqrt(a_cylinderRadius*a_cylinderRadius - d*d) /
cDot(segmentDir,O));
900 if (u0 > u1) {
cSwap (u0,u1); }
902 bool valid_u0 =
true;
903 bool valid_u1 =
true;
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; }
916 P0 = a_segmentPointA + u0 * segmentDir;
920 double cosAngleA =
cCosAngle(cylinderDir, P0A);
921 double cosAngleB =
cCosAngle(cylinderDirNeg, P0B);
923 if ((cosAngleA <= 0.0) || (cosAngleB <= 0.0))
929 a_collisionPointRelPosAB0 = cosAngleA * (P0A.
length() / lengthCylinderAB);
935 P1 = a_segmentPointA + u1 * segmentDir;
939 double cosAngleA =
cCosAngle(cylinderDir, P1A);
940 double cosAngleB =
cCosAngle(cylinderDirNeg, P1B);
942 if ((cosAngleA <= 0.0) || (cosAngleB <= 0.0))
948 a_collisionPointRelPosAB1 = cosAngleA * (P1A.
length() / lengthCylinderAB);
952 if (valid_u0 && valid_u1)
954 a_collisionPoint0 = P0;
956 a_collisionPoint1 = P1;
962 a_collisionPoint0 = P0;
966 double dotProduct =
cDot(segmentAB, a_collisionNormal0);
967 if (dotProduct < 0.0)
978 a_collisionPoint0 = P1;
982 double dotProduct =
cDot(segmentAB, a_collisionNormal0);
983 if (dotProduct < 0.0)
1034 const double& a_baseRadius,
1035 const double& a_topRadius,
1036 const double& a_height,
1042 cVector3d d = a_segmentPointB - a_segmentPointA;
1043 double length = d.
length();
1050 d.
mul(1.0 / length);
1053 double A = a_baseRadius;
1054 double B = (a_topRadius - a_baseRadius) / a_height;
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);
1066 cVector3d collisionPointTop, collisionNormalTop;
1074 collisionNormalTop);
1077 cVector3d collisionPointBase, collisionNormalBase;
1085 collisionNormalBase);
1095 distance =
cDistance(a_segmentPointA, collisionPointTop);
1096 a_collisionPoint = collisionPointTop;
1097 a_collisionNormal =
cVector3d(0.0, 0.0, 1.0);
1104 double dist =
cDistance(a_segmentPointA, collisionPointBase);
1105 if (dist < distance)
1107 a_collisionPoint = collisionPointBase;
1108 a_collisionNormal =
cVector3d(0.0, 0.0,-1.0);
1116 if ((s[0] >= 0.0) && (s[0] <= length) && (s[0] < distance))
1118 cVector3d collisionPoint = p + d * s[0];
1119 if ((collisionPoint(2) > 0.0) && (collisionPoint(2) < a_height))
1121 cVector3d normal(collisionPoint(0), collisionPoint(1), 0.0);
1123 normal(2) = (a_baseRadius - a_topRadius) / a_height;
1124 a_collisionNormal = normal;
1125 a_collisionPoint = collisionPoint;
1135 if ((s[1] >= 0.0) && (s[1] <= length) && (s[1] < distance))
1137 cVector3d collisionPoint = p + d * s[1];
1138 if ((collisionPoint(2) > 0.0) && (collisionPoint(2) < a_height))
1140 cVector3d normal(collisionPoint(0), collisionPoint(1), 0.0);
1142 normal(2) = (a_baseRadius - a_topRadius) / a_height;
1143 a_collisionNormal = normal;
1144 a_collisionPoint = collisionPoint;
1202 cVector3d d = a_segmentPointB - a_segmentPointA;
1203 double distance = d.
length();
1211 double normalDir = 1.0;
1214 for (
int i=0; i<3; i++)
1219 if (a_segmentPointA(i) < a_boxMin(i) || a_segmentPointA(i) > a_boxMax(i))
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;
1260 if (tmin > distance)
1266 a_collisionPoint = a_segmentPointA +
cMul(tmin, d);
1267 a_collisionNormal.
zero();
1268 a_collisionNormal(normalAxis) = normalDir;
1307 const double& a_innerRadius,
1308 const double& a_outerRadius,
1312 cVector3d collisionPoint, collisionNormal;
1315 double width = a_outerRadius + a_innerRadius;
1316 cVector3d boxMin(-width,-width,-a_innerRadius);
1317 cVector3d boxMax( width, width, a_innerRadius);
1325 collisionNormal) == 0)
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);
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;
1358 for (
int i=0; i<num; i++)
1367 a_collisionPoint = p + sol * d;
1371 a_collisionNormal =
cNormalize(a_collisionPoint - pt);
1417 const bool a_reportFrontSideCollision,
1418 const bool a_reportBackSideCollision,
1421 double& a_collisionPosVertex01,
1422 double& a_collisionPosVertex02
1427 const double C_INTERSECT_EPSILON = 10e-14f;
1431 a_segmentPointB.
subr(a_segmentPointA, rayDir);
1432 double segmentLengthSquare = rayDir.
lengthsq();
1433 if (segmentLengthSquare == 0.0) {
return (
false); }
1437 a_triangleVertex1.
subr(a_triangleVertex0, t_E0);
1438 a_triangleVertex2.
subr(a_triangleVertex0, t_E1);
1443 double dotProduct = t_N.
dot(rayDir);
1444 if (fabs(dotProduct)<10E-15f)
return (
false);
1446 if ((dotProduct < 0.0) && (a_reportFrontSideCollision ==
false))
return (
false);
1447 if ((dotProduct > 0.0) && (a_reportBackSideCollision ==
false))
return (
false);
1449 double t_T =
cDot(t_N,
cSub(a_triangleVertex0, a_segmentPointA)) /
cDot(t_N, rayDir);
1451 if (t_T + C_INTERSECT_EPSILON < 0)
return (
false);
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);
1461 if ((t_D > -C_INTERSECT_EPSILON) && (t_D < C_INTERSECT_EPSILON))
return(
false);
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;
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)
1476 double distanceSquare = a_segmentPointA.
distancesq(t_I);
1479 if (distanceSquare <= segmentLengthSquare)
1481 a_collisionPoint =
cAdd(a_segmentPointA,
cMul(t_T, rayDir));
1483 if (
cCosAngle(a_collisionNormal, rayDir) > 0.0)
1485 a_collisionNormal.
negate();
1488 a_collisionPosVertex01 = t_S0;
1489 a_collisionPosVertex02 = t_S1;
1506 #endif // CGeometryH
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