33 #ifndef __H__UGMATH__MATH_UTIL_IMPL__
34 #define __H__UGMATH__MATH_UTIL_IMPL__
47 template <
class TNumber>
51 return deg *
PI / 180.;
56 template <
class TNumber>
60 return rad * 180. /
PI;
65 template <
class TNumber>
67 urand(TNumber lowerBound, TNumber upperBound)
73 return lowerBound + (TNumber)((upperBound - lowerBound) * ((double)t / (
double)RAND_MAX));
78 template <
class TNumber>
80 clip(TNumber val, TNumber lowerBound, TNumber upperBound)
84 else if(val < lowerBound)
90 template <
class TNumber>
91 inline TNumber
sq(TNumber val)
97 template <
class vector_t>
101 for(
size_t i = 0; i < centerOut.size(); ++i)
105 for(
size_t i = 0; i < numPoints; ++i)
106 VecAdd(centerOut, centerOut, pointSet[i]);
112 template <
class vector_t>
122 template <
class vector_t>
124 const vector_t& v0,
const vector_t& v1)
149 template <
class vector_t>
159 template <
class vector_t>
161 const vector_t& from,
const vector_t& dir)
175 VecAdd(vOut, from, tmpDir);
185 template <
class vector_t>
187 const vector_t& from,
const vector_t& to)
195 template <
class vector_t>
204 template <
class vector_t>
206 const vector_t& v1,
const vector_t& v2)
223 template <
class vector_t>
233 template <
class vector_t>
236 const vector_t& from,
const vector_t& dir)
242 template <
class vector_t>
244 const vector_t& p,
const vector_t& v1,
const vector_t& v2,
245 const vector_t& v3,
const vector_t& n)
263 bc1Out = t; bc2Out = 0;
271 bc1Out = 0; bc2Out = tmpT;
281 bc1Out = 1. - t; bc2Out = t;
295 bc1Out = 0; bc2Out = 0;
299 bc1Out = 1; bc2Out = 0;
306 bc1Out = 0; bc2Out = 0;
310 bc1Out = 0; bc2Out = 1;
316 bc1Out = 1; bc2Out = 0;
320 bc1Out = 0; bc2Out = 1;
331 template <
class vector_t>
341 template <
class vector_t>
343 const vector_t& p,
const vector_t& n)
359 template <
class vector_t>
361 const vector_t& rayFrom,
const vector_t& rayDir,
362 const vector_t& p,
const vector_t& n)
372 tOut =
VecDot(v, n) / denom;
382 template <
class vector_t>
384 const vector_t &p0,
const vector_t &dir0,
385 const vector_t &p1,
const vector_t &dir1)
397 m[0][0] = dir0[0]; m[0][1] = -1 * dir1[0];
398 m[1][0] = dir0[1]; m[1][1] = -1 * dir1[1];
404 if(det == 0.0)
return false;
427 template <
class vector_t>
429 const vector_t &p0,
const vector_t &p1,
430 const vector_t &vFrom,
const vector_t &vDir,
436 if(bcOut >= -sml && bcOut <= 1.+sml)
442 template <
class vector_t>
444 const vector_t &from0,
const vector_t &to0,
445 const vector_t &from1,
const vector_t &to1,
452 if((t0Out >= -threshold) && (t0Out <= (1. + threshold))
453 && (t1Out >= -threshold) && (t1Out <= (1. + threshold)))
461 template <
class vector_t>
463 const vector_t& from1,
const vector_t& dir1,
464 const vector_t& from2,
const vector_t& dir2)
478 number tmp = l11 * l22 - l12 * l12;
479 if(fabs(tmp) <
SMALL)
482 t2Out = (l11*rb - l12*ra) / tmp;
483 t1Out = (ra - l12*t2Out) / l11;
487 template <
class vector_t>
489 const vector_t& a1,
const vector_t& a2,
490 const vector_t& b1,
const vector_t& b2)
497 if((t1Out >= 0) && (t1Out <= 1.) && (t2Out >= 0) && (t2Out <= 1.))
505 template <
class vector_t>
507 const vector_t &p0,
const vector_t &p1,
const vector_t &p2,
508 const vector_t &vFrom,
const vector_t &vDir,
542 m[0][0] = p1.x() - p0.x(); m[0][1] = p2.x() - p0.x(); m[0][2] = -vDir.x(); b[0] = vFrom.x() - p0.x();
543 m[1][0] = p1.y() - p0.y(); m[1][1] = p2.y() - p0.y(); m[1][2] = -vDir.y(); b[1] = vFrom.y() - p0.y();
544 m[2][0] = p1.z() - p0.z(); m[2][1] = p2.z() - p0.z(); m[2][2] = -vDir.z(); b[2] = vFrom.z() - p0.z();
552 number dBestEntry = fabs(m[0][0]);
555 if(fac > dBestEntry){
560 if(fabs(m[2][0]) > dBestEntry)
565 for(i2 = 0; i2 < 3; ++i2){
568 fac = -m[i2][0]/m[i1][0];
569 for(j = 0; j < 3; ++j)
570 m[i2][j] = m[i2][j] + fac*m[i1][j];
571 b[i2] = b[i2] + fac * b[i1];
578 if(fabs(m[i2][1]) < fabs(m[i3][1])){
584 if((m[i2][1]!=0) && (m[i3][1]!=0)){
585 fac = -m[i3][1]/m[i2][1];
586 for(j = 1; j < 3; ++j)
587 m[i3][j] = m[i3][j] + fac*m[i2][j];
588 b[i3] = b[i3] + fac * b[i2];
593 tOut = b[i3] / m[i3][2];
598 b[i2] -= tOut*m[i2][2];
600 bc2Out = b[i2] / m[i2][1];
605 b[i1] -= (tOut*m[i1][2] + bc2Out*m[i1][1]);
607 bc1Out = b[i1] / m[i1][0];
611 if((bc1Out >=-small ) && (bc2Out >= -small ) && ((bc1Out + bc2Out) <= 1.+small))
613 vOut.x() = vFrom.x() + tOut*vDir.x();
614 vOut.y() = vFrom.y() + tOut*vDir.y();
615 vOut.z() = vFrom.z() + tOut*vDir.z();
622 template <
class vector_t>
inline
624 const vector_t &p1,
const vector_t &p2,
625 const vector_t &vFrom,
const vector_t &vDir)
753 template <
class vector_t>
755 const vector_t& boxMin,
const vector_t& boxMax,
758 number tMin = 0, tMax = 0;
760 bool bMinMaxSet =
false;
762 for(
size_t i = 0; i < vector_t::Size; ++i){
763 if(fabs(rayDir[i]) >
SMALL)
766 t1 = (boxMin[i] - rayFrom[i]) / rayDir[i];
767 t2 = (boxMax[i] - rayFrom[i]) / rayDir[i];
772 if((t1 <= tMax) && (t2 >= tMin))
774 tMin = std::max(t1, tMin);
775 tMax = std::min(t2, tMax);
787 else if((rayFrom[i] < boxMin[i]) || (rayFrom[i] > boxMax[i]))
796 std::swap(tMin, tMax);
825 template <
class vector_t>
827 const vector_t& boxMin,
const vector_t& boxMax)
835 return ((tmin * tmax < 0) || (tmin >= 0 && tmin <= 1.));
842 template <
class vector_t>
844 const vector_t& v,
const vector_t& dir,
845 const vector_t& center,
number radius)
852 if(h > radius -
SMALL){
864 number a = sqrt(radius * radius - h * h);
872 template <
class vector_t>
874 const vector_t& v1,
const vector_t& v2,
875 const vector_t& center,
number radius)
883 case 1:
if((s1Out >= 0) && (s1Out <= 1))
887 if((s1Out < 0) || (s1Out > 1))
889 if((s2Out < 0) || (s2Out > 1))
900 template <
class vector_t>
902 const vector_t& box2Min,
const vector_t& box2Max)
904 for(
size_t i = 0; i < box1Min.size(); ++i){
905 if(box1Min[i] > box2Max[i] || box1Max[i] < box2Min[i])
913 template <
class vector_t>
923 template <
class vector_t>
925 const vector_t& p3,
const vector_t& p4)
931 template <
class vector_t>
939 template <
class vector_t>
957 template <
class vector_t>
959 const vector_t& boxMax)
961 for(
size_t i = 0; i < v.size(); ++i){
962 if(v[i] < boxMin[i] || v[i] > boxMax[i])
970 template <
class vector_t>
972 const vector_t& v1,
const vector_t& v2)
986 locSmall = locSmall * locSmall *
SMALL;
990 edgeNorm.x() = e.y();
991 edgeNorm.y() = -e.x();
994 if(
VecDot(tv1, edgeNorm) *
VecDot(tv2, edgeNorm) < -locSmall)
998 edgeNorm.x() = e.y();
999 edgeNorm.y() = -e.x();
1002 if(
VecDot(tv1, edgeNorm) *
VecDot(tv2, edgeNorm) < -locSmall)
1006 edgeNorm.x() = e.y();
1007 edgeNorm.y() = -e.x();
1010 if(
VecDot(tv1, edgeNorm) *
VecDot(tv2, edgeNorm) < -locSmall)
1023 UG_THROW(
"Not implemented for 3D!");
1030 template <
class vector_t>
1032 const vector_t& v1,
const vector_t& v2)
1039 template <
class vector_t>
1041 const vector_t& v1,
const vector_t& v2,
1056 locSmall = locSmall * locSmall *
SMALL;
1059 edgeNorm.x() = e.y();
1060 edgeNorm.y() = -e.x();
1063 if(
VecDot(tv1, edgeNorm) *
VecDot(tv2, edgeNorm) < -locSmall)
1067 edgeNorm.x() = e.y();
1068 edgeNorm.y() = -e.x();
1071 if(
VecDot(tv1, edgeNorm) *
VecDot(tv2, edgeNorm) < -locSmall)
1075 edgeNorm.x() = e.y();
1076 edgeNorm.y() = -e.x();
1079 if(
VecDot(tv1, edgeNorm) *
VecDot(tv2, edgeNorm) < -locSmall)
1083 edgeNorm.x() = e.y();
1084 edgeNorm.y() = -e.x();
1087 if(
VecDot(tv1, edgeNorm) *
VecDot(tv2, edgeNorm) < -locSmall)
1102 UG_THROW(
"Not implemented for 3D!");
1109 template <
class vector_t>
1111 const vector_t& v2,
const vector_t& v3)
1125 locSmall = locSmall * locSmall * locSmall *
SMALL;
1132 if((
VecDot(v3, n) - pn) * (
VecDot(v, n) - pn) < -locSmall)
1140 if((
VecDot(v2, n) - pn) * (
VecDot(v, n) - pn) < -locSmall)
1148 if((
VecDot(v0, n) - pn) * (
VecDot(v, n) - pn) < -locSmall)
1156 if((
VecDot(v1, n) - pn) * (
VecDot(v, n) - pn) < -locSmall)
1165 template <
class vector_t>
1167 const vector_t& n,
const vector_t& r0)
A class for fixed size, dense matrices.
Definition: math_matrix.h:52
a mathematical Vector with N entries.
Definition: math_vector.h:97
MathMatrix< N, N, T >::value_type Determinant(const MathMatrix< N, N, T > &m)
Determinant of a matrix.
Definition: math_matrix_functions_common_impl.hpp:382
MathMatrix< N, M, T >::value_type Inverse(MathMatrix< N, M, T > &mOut, const MathMatrix< M, N, T > &m)
Inverse of a matrix.
Definition: math_matrix_functions_common_impl.hpp:560
#define UG_THROW(msg)
Definition: error.h:57
double number
Definition: types.h:124
void MatVecMult(vector_t_out &vOut, const matrix_t &m, const vector_t_in &v)
Matrix - Vector Multiplication.
Definition: math_matrix_vector_functions_common_impl.hpp:49
number TriangleArea(const vector_t &p1, const vector_t &p2, const vector_t &p3)
calculates the are of the triangle defined by p1, p2 and p3
Definition: math_util_impl.hpp:914
number ProjectPointToLine(vector_t &vOut, const vector_t &v, const vector_t &from, const vector_t &to)
finds the projection of v onto the line defined by from and to
Definition: math_util_impl.hpp:186
bool LineLineIntersection2d(vector_t &vOut, number &t0Out, number &t1Out, const vector_t &from0, const vector_t &to0, const vector_t &from1, const vector_t &to1, const number threshold=0)
calculates the intersection of a two lines in 2d
Definition: math_util_impl.hpp:443
bool PointIsInsideQuadrilateral(const vector_t &v, const vector_t &v0, const vector_t &v1, const vector_t &v2, const vector_t &v3)
Returns true if the point lies inside or on the boundary of a quadrilateral.
Definition: math_util_impl.hpp:1040
bool LineLineProjection(number &t1Out, number &t2Out, const vector_t &a1, const vector_t &a2, const vector_t &b1, const vector_t &b2)
calculates the closest point between the rays through the given lines.
Definition: math_util_impl.hpp:488
number QuadrilateralArea(const vector_t &p1, const vector_t &p2, const vector_t &p3, const vector_t &p4)
calculates the are of the triangle defined by p1, p2 and p3
Definition: math_util_impl.hpp:924
vector_t TriangleBarycenter(const vector_t &p1, const vector_t &p2, const vector_t &p3)
Calculates the barycenter of a triangle (1/3) * (p1+p2+p3)
Definition: math_util_impl.hpp:114
bool RayPlaneIntersection(vector_t &vOut, number &tOut, const vector_t &rayFrom, const vector_t &rayDir, const vector_t &p, const vector_t &n)
calculates the intersection of the ray rayFrom+t*rayDir and the plane (x-p)*n=0.
Definition: math_util_impl.hpp:360
bool RayBoxIntersection(const vector_t &rayFrom, const vector_t &rayDir, const vector_t &boxMin, const vector_t &boxMax, number *tNearOut=NULL, number *tFarOut=NULL)
checks if a ray is intersecting a box
Definition: math_util_impl.hpp:754
bool RayRayIntersection2d(vector_t &vOut, number &t0Out, number &t1Out, const vector_t &p0, const vector_t &dir0, const vector_t &p1, const vector_t &dir1)
calculates the intersection of two Rays in 2d
Definition: math_util_impl.hpp:383
number DistancePointToRay(const vector_t &v, const vector_t &from, const vector_t &dir)
calculates the distance of a point to a ray
Definition: math_util_impl.hpp:225
TNumber urand(TNumber lowerBound, TNumber upperBound)
uniform distributed random numbers in [lowerBound, upperBound[. Use srand to set a seed.
Definition: math_util_impl.hpp:67
number DistancePointToLine(const vector_t &v, const vector_t &v1, const vector_t &v2)
calculates the distance of a point to a line segment
Definition: math_util_impl.hpp:197
number DropAPerpendicular(vector_t &vOut, const vector_t &v, const vector_t &v0, const vector_t &v1)
finds the projection of v onto the line defined by v0 and v1
Definition: math_util_impl.hpp:123
bool PointIsInsideTriangle(const vector_t &v, const vector_t &v0, const vector_t &v1, const vector_t &v2)
Returns true if the point lies inside or on the boundary of a triangle.
Definition: math_util_impl.hpp:971
vector_t PointOnRay(const vector_t &from, const vector_t &dir, number s)
returns the point described by a relative ray coordinate
Definition: math_util_impl.hpp:150
TNumber sq(TNumber val)
returns the square of a value (val*val)
Definition: math_util_impl.hpp:91
number GeometricApproximationDegree(vector_t &n1, vector_t &n2, vector_t &n3, vector_t &tn)
the returned degree lies between 0 and 1. The closer to 1 the better.
Definition: math_util_impl.hpp:932
bool BoxBoundProbe(const vector_t &v, const vector_t &boxMin, const vector_t &boxMax)
Returns true if the point lies inside or on the boundary of the box.
Definition: math_util_impl.hpp:958
bool RayLineIntersection2d(vector_t &vOut, number &bcOut, number &tOut, const vector_t &p0, const vector_t &p1, const vector_t &vFrom, const vector_t &vDir, number sml=0)
calculates the intersection of a ray with a Line in 2d
Definition: math_util_impl.hpp:428
void CalculateCenter(vector_t ¢erOut, const vector_t *pointSet, size_t numPoints)
calculates the center of a point-set
Definition: math_util_impl.hpp:98
int RaySphereIntersection(number &s1Out, number &s2Out, const vector_t &v, const vector_t &dir, const vector_t ¢er, number radius)
checks whether the line segment (v1, v2) intersect the given sphere.
Definition: math_util_impl.hpp:843
bool RayTriangleIntersection(vector_t &vOut, number &bc1Out, number &bc2Out, number &tOut, const vector_t &p0, const vector_t &p1, const vector_t &p2, const vector_t &vFrom, const vector_t &vDir, const number small=SMALL)
calculates the intersection of a ray with a triangle
Definition: math_util_impl.hpp:506
TNumber clip(TNumber val, TNumber lowerBound, TNumber upperBound)
clips a number to the given interval [lowerBound, upperBound].
Definition: math_util_impl.hpp:80
bool LineBoxIntersection(const vector_t &v1, const vector_t &v2, const vector_t &boxMin, const vector_t &boxMax)
checks whether the given line-segment (v1, v2) intersect the given box.
Definition: math_util_impl.hpp:826
number TriangleQuality_Area(const vector_t &p1, const vector_t &p2, const vector_t &p3)
returns a value between 0 and 1. The higher the better.
Definition: math_util_impl.hpp:940
void ProjectPointToPlane(vector_t &vOut, const vector_t &v, const vector_t &p, const vector_t &n)
projects v onto the plane defined by the point p and the planes normal n.
Definition: math_util_impl.hpp:342
bool PointIsInsideTriangle_HighAcc(const vector_t &v, const vector_t &v0, const vector_t &v1, const vector_t &v2)
Returns true if the point lies inside or on a side of the given triangle.
Definition: math_util_impl.hpp:1031
TNumber deg_to_rad(TNumber deg)
Definition: math_util_impl.hpp:49
bool PointIsInsideTetrahedron(const vector_t &v, const vector_t &v0, const vector_t &v1, const vector_t &v2, const vector_t &v3)
Returns true if the point lies inside or on the boundary of a tetrahedron.
Definition: math_util_impl.hpp:1110
number DistancePointToPlane(const vector_t &v, const vector_t &p, const vector_t &n)
Calculates the distance between the specified point and the plane.
Definition: math_util_impl.hpp:332
void ReflectVectorAtPlane(vector_t &vReflectedOut, const vector_t &v, const vector_t &n, const vector_t &r0)
reflects a vector at a plane
Definition: math_util_impl.hpp:1166
bool BoxBoxIntersection(const vector_t &box1Min, const vector_t &box1Max, const vector_t &box2Min, const vector_t &box2Max)
checks whether two boxes intersect.
Definition: math_util_impl.hpp:901
int LineSphereIntersection(number &s1Out, number &s2Out, const vector_t &v1, const vector_t &v2, const vector_t ¢er, number radius)
Definition: math_util_impl.hpp:873
bool RayRayProjection(number &t1Out, number &t2Out, const vector_t &from1, const vector_t &dir1, const vector_t &from2, const vector_t &dir2)
Calculates the parameter values at wich two rays are closest.
Definition: math_util_impl.hpp:462
TNumber rad_to_deg(TNumber rad)
Definition: math_util_impl.hpp:58
number ProjectPointToRay(vector_t &vOut, const vector_t &v, const vector_t &from, const vector_t &dir)
finds the projection of v onto the ray defined by from and dir
Definition: math_util_impl.hpp:160
number DistancePointToTriangle(vector_t &vOut, number &bc1Out, number &bc2Out, const vector_t &p, const vector_t &v1, const vector_t &v2, const vector_t &v3, const vector_t &n)
calculates the minimal distance of a point to a triangle.
Definition: math_util_impl.hpp:243
vector_t::value_type VecLength(const vector_t &v)
returns the length of v. Slower than VecLengthSq.
Definition: math_vector_functions_common_impl.hpp:341
void VecScaleAppend(vector_t &vOut, typename vector_t::value_type s1, const vector_t &v1)
Scales a Vector and adds it to a second vector.
Definition: math_vector_functions_common_impl.hpp:126
void GenVecCross(MathVector< dim > &result, const MathVector< dim > &v_1, const MathVector< dim > &v_2)
calculates the usual cross product in 3d, and the (det, 0) vector as a cross product in 2d
Definition: math_vector_functions_common_impl.hpp:465
void VecScaleAdd(vector_t &vOut, typename vector_t::value_type s1, const vector_t &v1, typename vector_t::value_type s2, const vector_t &v2)
Scales two Vectors, adds them and returns the sum in a third vector.
Definition: math_vector_functions_common_impl.hpp:265
vector_t::value_type VecTwoNormSq(const vector_t &v)
Definition: math_vector_functions_common_impl.hpp:585
void VecNormalize(vector_t &vOut, const vector_t &v)
scales a vector_t to unit length
Definition: math_vector_functions_common_impl.hpp:501
void VecSubtract(vector_t &vOut, const vector_t &v1, const vector_t &v2)
subtracts v2 from v1 and stores the result in a vOut
Definition: math_vector_functions_common_impl.hpp:226
vector_t::value_type VecDistanceSq(const vector_t &v1, const vector_t &v2)
returns the squared distance of two vector_ts.
Definition: math_vector_functions_common_impl.hpp:351
vector_t::value_type VecDistance(const vector_t &v1, const vector_t &v2)
returns the distance of two vector_ts.
Definition: math_vector_functions_common_impl.hpp:375
void VecAdd(vector_t &vOut, const vector_t &v1, const vector_t &v2)
adds two MathVector<N>s and stores the result in a third one
Definition: math_vector_functions_common_impl.hpp:185
void VecScale(vector_t &vOut, const vector_t &v, typename vector_t::value_type s)
scales a MathVector<N>
Definition: math_vector_functions_common_impl.hpp:252
void VecCross(vector_t &vOut, const vector_t &v1, const vector_t &v2)
calculates the cross product of two Vectors of dimension 3. It makes no sense to use VecCross for vec...
Definition: math_vector_functions_common_impl.hpp:437
vector_t::value_type VecDot(const vector_t &v1, const vector_t &v2)
returns the dot-product of two vector_ts
Definition: math_vector_functions_common_impl.hpp:385
const number PI
Definition: math_constants.h:45
const number SMALL
Definition: math_constants.h:41
const number SMALL_SQ
Definition: math_constants.h:42
Definition: file_io_grdecl.cpp:60