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;
957 p1 = line.
p+x1*line.
d;
958 p2 = line.
p+x2*line.
d;
1004 template <
typename T>
1008 return (a % b).isZero();
1017 template <
typename T>
1031 template <
typename T>
1043 template <
typename T>
1055 template <
typename T>
1067 template <
typename T>
1079 template <
typename T>
1091 template <
typename T>
1103 template <
typename T>
1115 template <
typename T>
1127 template <
typename T>
1139 template <
typename T>
1151 template <
typename T>
1163 template <
typename T>
1175 template <
typename T>
1187 template <
typename T>
1199 template <
typename T>
1211 template <
typename T>
1223 template <
typename T>
1235 template <
typename T>
1247 template <
typename T>
1257 template <
typename T>
1259 (
const T& ax,
const T& ay,
const T& az,
1260 const T& bx,
const T& by,
const T& bz,
1261 const T& nx,
const T& ny,
const T& nz)
1264 T bl = (T) sqrt((
double)ax * ax + ay * ay + az * az);
1265 T el = (T) sqrt((
double)bx * bx + by * by + bz * bz);
1266 T bel = (T) (ax * bx + ay * by + az * bz);
1278 else if (bel < -1.0)
1283 T acosbel = (T) acos(bel);
1285 if (( nx * (az * by - ay * bz)
1286 + ny * (ax * bz - az * bx)
1287 + nz * (ay * bx - ax * by)) > 0)
1298 template <
typename T>
1321 template <
typename T>
1323 (
const T& ax,
const T& ay,
const T& az,
1324 const T& bx,
const T& by,
const T& bz,
1325 const T& cx,
const T& cy,
const T& cz,
1326 const T& dx,
const T& dy,
const T& dz)
1351 T ndax = aby * cbz - abz * cby;
1352 T nday = abz * cbx - abx * cbz;
1353 T ndaz = abx * cby - aby * cbx;
1356 T neax = cbz * cdy - cby * cdz;
1357 T neay = cbx * cdz - cbz * cdx;
1358 T neaz = cby * cdx - cbx * cdy;
1361 T bl = (T) sqrt((
double)ndax * ndax + nday * nday + ndaz * ndaz);
1362 T el = (T) sqrt((
double)neax * neax + neay * neay + neaz * neaz);
1363 T bel = (T) (ndax * neax + nday * neay + ndaz * neaz);
1375 else if (bel < -1.0)
1380 T acosbel = (T) acos(bel);
1382 if ((cbx * (ndaz * neay - nday * neaz)
1383 + cby * (ndax * neaz - ndaz * neax)
1384 + cbz * (nday * neax - ndax * neay))
1390 acosbel = (acosbel > 0.0)
1400 #endif // BALL_MATHS_ANALYTICALGEOMETRY_H