5 #ifndef BALL_MATHS_ANALYTICALGEOMETRY_H
6 #define BALL_MATHS_ANALYTICALGEOMETRY_H
8 #ifndef BALL_COMMON_EXCEPTION_H
12 #ifndef BALL_MATHS_ANGLE_H
16 #ifndef BALL_MATHS_CIRCLE3_H
20 #ifndef BALL_MATHS_LINE3_H
24 #ifndef BALL_MATHS_PLANE3_H
28 #ifndef BALL_MATHS_SPHERE3_H
32 #ifndef BALL_MATHS_VECTOR3_H
36 #define BALL_MATRIX_CELL(m, dim, row, col) *((m) + (row) * (dim) + (col))
37 #define BALL_CELL(x, y) *((m) + (y) * (dim) + (x))
64 T* submatrix =
new T[dim1 * dim1];
68 for (
Index j = 0; j < dim1; ++j)
72 *(submatrix + j * dim1 +
k) = *(m + (j + 1) * dim + (
k < i ?
k :
k + 1));
75 determinant += *(m + i) * (i / 2.0 == i / 2 ? 1 : -1) *
getDeterminant_(submatrix, dim1);
117 template <
typename T>
131 template <
typename T>
135 return (m00 * m11 - m01 * m10);
141 template <
typename T>
157 template <
typename T>
159 getDeterminant3(
const T& m00,
const T& m01,
const T& m02,
const T& m10,
const T& m11,
const T& m12,
const T& m20,
const T& m21,
const T& m22)
161 return ( m00 * m11 * m22 + m01 * m12 * m20 + m02 * m10 * m21 - m02 * m11 * m20 - m00 * m12 * m21 - m01 * m10 * m22);
190 template <
typename T>
196 const Size col_dim = dim + 1;
197 T* matrix =
new T[dim * (dim + 1)];
199 T* target = (T*)matrix;
202 while (target <= end)
204 *target++ = *source++;
207 for (i = 0; i < (
Index)dim; ++i)
211 for (j = i + 1; j < (
Index)dim; ++j)
224 for (k = i; k < (
Index)dim + 1; ++
k)
239 for (j = dim; j >= i; --j)
244 for (j = i + 1; j < (
Index)dim; ++j)
248 for (k = dim; k>= i; --
k)
257 for (i = dim - 2; i >= 0; --i)
261 for (j = i + 1; j < (
Index)dim; ++j)
273 #undef BALL_MATRIX_CELL
283 template <
typename T>
285 bool SolveSystem2(
const T& a1,
const T& b1,
const T& c1,
const T& a2,
const T& b2,
const T& c2, T& x1, T& x2)
287 T quot = (a1 * b2 - a2 * b1);
294 x1 = (c1 * b2 - c2 * b1) / quot;
295 x2 = (a1 * c2 - a2 * c1) / quot;
309 template <
typename T>
321 T discriminant = b * b - 4 * a *
c;
328 T sqrt_discriminant = sqrt(discriminant);
331 x1 = x2 = -b / (2 * a);
337 x1 = (-b + sqrt_discriminant) / (2 * a);
338 x2 = (-b - sqrt_discriminant) / (2 * a);
349 template <
typename T>
365 template <
typename T>
375 ((s * a.
x + r * b.
x) / sum,
376 (s * a.
y + r * b.
y) / sum,
377 (s * a.
z + r * b.
z) / sum);
385 template <
typename T>
393 return sqrt(dx * dx + dy * dy + dz * dz);
402 template <
typename T>
406 if (line.
d.getLength() == (T)0)
410 return ((line.
d % (point - line.
p)).getLength() / line.
d.getLength());
419 template <
typename T>
432 template <
typename T>
435 T cross_product_length = (a.
d % b.
d).getLength();
439 if (a.
d.getLength() == (T)0)
443 return ((a.
d % (b.
p - a.
p)).getLength() / a.
d.getLength());
452 return (
Maths::abs(spat_product) / cross_product_length);
468 template <
typename T>
472 T length = plane.
n.getLength();
478 return (
Maths::abs(plane.
n * (point - plane.
p)) / length);
487 template <
typename T>
500 template <
typename T>
504 T length = plane.
n.getLength();
518 template <
typename T>
531 template <
typename T>
535 T length = a.
n.getLength();
549 template <
typename T>
568 template <
typename T>
572 T length_product = a.
d.getSquareLength() * b.
d.getSquareLength();
578 intersection_angle = acos(
Maths::abs(a.
d * b.
d) / sqrt(length_product));
588 template <
typename T>
600 intersection_angle = asin(
Maths::abs(plane.
n * vector) / sqrt(length_product));
611 template <
typename T>
615 return GetAngle(plane, vector, intersection_angle);
624 template <
typename T>
628 T length_product = plane.
n.getSquareLength() * line.
d.getSquareLength();
635 intersection_angle = asin(
Maths::abs(plane.
n * line.
d) / sqrt(length_product));
645 template <
typename T>
649 return GetAngle(plane, line, intersection_angle);
659 template <
typename T>
663 T length_product = a.
n.getSquareLength() * b.
n.getSquareLength();
670 intersection_angle = acos(
Maths::abs(a.
n * b.
n) / sqrt(length_product));
680 template <
typename T>
684 if ((
SolveSystem2(a.
d.x, -b.
d.x, b.
p.x - a.
p.x, a.
d.y, -b.
d.y, b.
p.y - a.
p.y, c1, c2) ==
true &&
Maths::isEqual(a.
p.z + a.
d.z * c1, b.
p.z + b.
d.z * c2)) || (
SolveSystem2(a.
d.x, -b.
d.x, b.
p.x - a.
p.x, a.
d.z, -b.
d.z, b.
p.z - a.
p.z, c1, c2) ==
true &&
Maths::isEqual(a.
p.y + a.
d.y * c1, b.
p.y + b.
d.y * c2)) || (
SolveSystem2(a.
d.y, -b.
d.y, b.
p.y - a.
p.y, a.
d.z, -b.
d.z, b.
p.z - a.
p.z, c1, c2) ==
true &&
Maths::isEqual(a.
p.x + a.
d.x * c1, b.
p.x + b.
d.x * c2)))
686 point.
set(a.
p.x + a.
d.x * c1, a.
p.y + a.
d.y * c1, a.
p.z + a.
d.z * c1);
699 template <
typename T>
703 T dot_product = plane.
n * line.
d;
708 intersection_point.
set(line.
p + (plane.
n * (plane.
p - line.
p)) * line.
d / dot_product);
718 template <
typename T>
731 template <
typename T>
734 T u = plane1.
p*plane1.
n;
735 T v = plane2.
p*plane2.
n;
736 T det = plane1.
n.x*plane2.
n.y-plane1.
n.y*plane2.
n.x;
739 det = plane1.
n.x*plane2.
n.z-plane1.
n.z*plane2.
n.x;
742 det = plane1.
n.y*plane2.
n.z-plane1.
n.z*plane2.
n.y;
749 T a = plane2.
n.z/det;
750 T b = -plane1.
n.z/det;
751 T
c = -plane2.
n.y/det;
752 T d = plane1.
n.y/det;
757 line.
d.y = a*plane1.
n.x+b*plane2.
n.x;
758 line.
d.z = c*plane1.
n.x+d*plane2.
n.x;
763 T a = plane2.
n.z/det;
764 T b = -plane1.
n.z/det;
765 T
c = -plane2.
n.x/det;
766 T d = plane1.
n.x/det;
770 line.
d.x = a*plane1.
n.y+b*plane2.
n.y;
772 line.
d.z = c*plane1.
n.y+d*plane2.
n.y;
777 T a = plane2.
n.y/det;
778 T b = -plane1.
n.y/det;
779 T
c = -plane2.
n.x/det;
780 T d = plane1.
n.x/det;
784 line.
d.x = a*plane1.
n.z+b*plane2.
n.z;
785 line.
d.y = c*plane1.
n.z+d*plane2.
n.z;
798 template <
typename T>
804 if (number_of_solutions == 0)
809 intersection_point1 = line.
p + x1 * line.
d;
810 intersection_point2 = line.
p + x2 * line.
d;
822 template <
typename T>
826 return GetIntersection(sphere, line, intersection_point1, intersection_point2);
835 template <
typename T>
850 intersection_circle.
set(sphere.
p + sphere.
radius * Vector3, plane.
n, 0);
854 intersection_circle.
set
855 (sphere.
p + distance * Vector3, plane.
n,
856 sqrt(sphere.
radius * sphere.
radius - distance * distance));
868 template <
typename T>
883 template <
typename T>
887 T square_dist = norm * norm;
892 T dist = sqrt(square_dist);
904 T u = radius1_square - radius2_square + square_dist;
905 T length = u / (2 * square_dist);
906 T square_radius = radius1_square - u * length / 2;
907 if (square_radius < 0)
912 intersection_circle.
p = a.
p + (norm * length);
913 intersection_circle.
radius = sqrt(square_radius);
914 intersection_circle.
n = norm / dist;
934 T p1_square_length = s1.
p*s1.
p;
935 T p2_square_length = s2.
p*s2.
p;
936 T p3_square_length = s3.
p*s3.
p;
937 T u = (r2_square-r1_square-p2_square_length+p1_square_length)/2;
938 T v = (r3_square-r1_square-p3_square_length+p1_square_length)/2;
958 p1 = line.
p+x1*line.
d;
959 p2 = line.
p+x2*line.
d;
1005 template <
typename T>
1009 return (a % b).isZero();
1018 template <
typename T>
1032 template <
typename T>
1044 template <
typename T>
1056 template <
typename T>
1068 template <
typename T>
1080 template <
typename T>
1092 template <
typename T>
1104 template <
typename T>
1116 template <
typename T>
1128 template <
typename T>
1140 template <
typename T>
1152 template <
typename T>
1164 template <
typename T>
1176 template <
typename T>
1188 template <
typename T>
1200 template <
typename T>
1212 template <
typename T>
1224 template <
typename T>
1236 template <
typename T>
1248 template <
typename T>
1258 template <
typename T>
1260 (
const T& ax,
const T& ay,
const T& az,
1261 const T& bx,
const T& by,
const T& bz,
1262 const T& nx,
const T& ny,
const T& nz)
1265 T bl = (T) sqrt((
double)ax * ax + ay * ay + az * az);
1266 T el = (T) sqrt((
double)bx * bx + by * by + bz * bz);
1267 T bel = (T) (ax * bx + ay * by + az * bz);
1279 else if (bel < -1.0)
1284 T acosbel = (T) acos(bel);
1286 if (( nx * (az * by - ay * bz)
1287 + ny * (ax * bz - az * bx)
1288 + nz * (ay * bx - ax * by)) > 0)
1299 template <
typename T>
1322 template <
typename T>
1324 (
const T& ax,
const T& ay,
const T& az,
1325 const T& bx,
const T& by,
const T& bz,
1326 const T& cx,
const T& cy,
const T& cz,
1327 const T& dx,
const T& dy,
const T& dz)
1352 T ndax = aby * cbz - abz * cby;
1353 T nday = abz * cbx - abx * cbz;
1354 T ndaz = abx * cby - aby * cbx;
1357 T neax = cbz * cdy - cby * cdz;
1358 T neay = cbx * cdz - cbz * cdx;
1359 T neaz = cby * cdx - cbx * cdy;
1362 T bl = (T) sqrt((
double)ndax * ndax + nday * nday + ndaz * ndaz);
1363 T el = (T) sqrt((
double)neax * neax + neay * neay + neaz * neaz);
1364 T bel = (T) (ndax * neax + nday * neay + ndaz * neaz);
1376 else if (bel < -1.0)
1381 T acosbel = (T) acos(bel);
1383 if ((cbx * (ndaz * neay - nday * neaz)
1384 + cby * (ndax * neaz - ndaz * neax)
1385 + cbz * (nday * neax - ndax * neay))
1391 acosbel = (acosbel > 0.0)
1401 #endif // BALL_MATHS_ANALYTICALGEOMETRY_H
bool GetIntersection(const TLine3< T > &a, const TLine3< T > &b, TVector3< T > &point)
BALL_INLINE bool GetAngle(const TVector3< T > &a, const TVector3< T > &b, TAngle< T > &intersection_angle)
T getDeterminant(const T *m, Size dim)
bool SolveSystem(const T *m, T *x, const Size dim)
BALL_INLINE T getDeterminant2(const T *m)
static T getTripleProduct(const TVector3< T > &a, const TVector3< T > &b, const TVector3< T > &c)
bool isEqual(const T1 &a, const T2 &b)
BALL_INLINE bool isCollinear(const TVector3< T > &a, const TVector3< T > &b)
TAngle< T > getTorsionAngle(const T &ax, const T &ay, const T &az, const T &bx, const T &by, const T &bz, const T &cx, const T &cy, const T &cz, const T &dx, const T &dy, const T &dz)
bool isNotZero(const T &t)
BALL_INLINE bool isOrthogonal(const TVector3< T > &a, const TVector3< T > &b)
#define BALL_MATRIX_CELL(m, dim, row, col)
bool isGreaterOrEqual(const T1 &a, const T2 &b)
BALL_INLINE bool isComplanar(const TVector3< T > &a, const TVector3< T > &b, const TVector3< T > &c)
BALL_EXTERN_VARIABLE const double c
BALL_INLINE T getDeterminant_(const T *m, Size dim)
T getSquareLength() const
TAngle< T > getOrientedAngle(const T &ax, const T &ay, const T &az, const T &bx, const T &by, const T &bz, const T &nx, const T &ny, const T &nz)
short SolveQuadraticEquation(const T &a, const T &b, const T &c, T &x1, T &x2)
BALL_EXTERN_VARIABLE const double k
TVector3< float > Vector3
bool isGreater(const T1 &a, const T2 &b)
bool isNotEqual(const T1 &a, const T2 &b)
BALL_INLINE bool SolveSystem2(const T &a1, const T &b1, const T &c1, const T &a2, const T &b2, const T &c2, T &x1, T &x2)
BALL_INLINE bool isParallel(const TLine3< T > &line, const TPlane3< T > &plane)
bool isLess(const T1 &a, const T2 &b)
TAngle< T > getAngle(const TVector3 &vector) const
BALL_EXTERN_VARIABLE const double PI
PI.
void set(const TCircle3 &circle)
BALL_INLINE TVector3< T > GetPartition(const TVector3< T > &a, const TVector3< T > &b)
BALL_INLINE bool isIntersecting(const TVector3< T > &point, const TLine3< T > &line)
BALL_INLINE T getDeterminant3(const T *m)
BALL_INLINE T GetDistance(const TVector3< T > &a, const TVector3< T > &b)