33 #ifndef __H__UGMATH__MATH_UTIL__
34 #define __H__UGMATH__MATH_UTIL__
38 #include "../math_vector_matrix/math_vector.h"
39 #include "../math_vector_matrix/math_vector_functions.h"
40 #include "../ugmath_types.h"
41 #include "../../ug_config.h"
62 template <
class TNumber>
68 template <
class TNumber>
75 template <
class TNumber>
77 urand(TNumber lowerBound, TNumber upperBound);
82 template <
class TNumber>
84 clip(TNumber val, TNumber lowerBound, TNumber upperBound);
88 template <
class TNumber>
89 inline TNumber
sq(TNumber val);
117 template <
class vector_t>
123 template <
class vector_t>
151 const vector3& center,
size_t numPoints);
162 const vector3* pointSet,
size_t numPoints);
189 template <
class vector_t>
191 const vector_t& v0,
const vector_t& v1);
196 template <
class vector_t>
207 template <
class vector_t>
209 const vector_t& from,
const vector_t& dir);
218 template <
class vector_t>
220 const vector_t& from,
const vector_t& to);
231 template <
class vector_t>
236 template <
class vector_t>
238 const vector_t& v1,
const vector_t& v2);
249 template <
class vector_t>
252 const vector_t& dir);
254 template <
class vector_t>
257 const vector_t& from,
const vector_t& dir);
273 template <
class vector_t>
275 const vector_t& p,
const vector_t& v1,
const vector_t& v2,
276 const vector_t& v3,
const vector_t& n);
280 template <
class vector_t>
287 template <
class vector_t>
289 const vector_t& p,
const vector_t& n);
294 template <
class vector_t>
296 const vector_t& rayFrom,
const vector_t& rayDir,
297 const vector_t& p,
const vector_t& n);
309 template <
class vector_t>
311 const vector_t &p0,
const vector_t &dir0,
312 const vector_t &p1,
const vector_t &dir1);
330 template <
class vector_t>
332 const vector_t &p0,
const vector_t &p1,
333 const vector_t &vFrom,
const vector_t &vDir,
350 template <
class vector_t>
352 const vector_t &from0,
const vector_t &to0,
353 const vector_t &from1,
const vector_t &to1,
354 const number threshold = 0);
394 template <
class vector_t>
396 const vector_t& from1,
const vector_t& dir1,
397 const vector_t& from2,
const vector_t& dir2);
402 template <
class vector_t>
404 const vector_t& a1,
const vector_t& a2,
405 const vector_t& b1,
const vector_t& b2);
436 template <
class vector_t>
438 const vector_t &p0,
const vector_t &p1,
const vector_t &p2,
439 const vector_t &vFrom,
const vector_t &vDir,
456 template <
class vector_t>
inline
458 const vector_t &p1,
const vector_t &p2,
459 const vector_t &vFrom,
const vector_t &vDir);
476 template <
class vector_t>
478 const vector_t& boxMin,
const vector_t& boxMax,
488 template <
class vector_t>
490 const vector_t& boxMin,
const vector_t& boxMax);
497 template <
class vector_t>
499 const vector_t& v,
const vector_t& dir,
500 const vector_t& center,
number radius);
502 template <
class vector_t>
504 const vector_t& v1,
const vector_t& v2,
505 const vector_t& center,
number radius);
557 template <
class vector_t>
559 const vector_t& box2Min,
const vector_t& box2Max);
566 template <
class vector_t>
567 bool BoxBoundProbe(
const vector_t& v,
const vector_t& boxMin,
568 const vector_t& boxMax);
572 template <
class vector_t>
578 template <
class vector_t>
580 const vector_t& p3,
const vector_t& p4);
588 template <
class vector_t>
599 template <
class vector_t>
609 template <
class vector_t>
611 const vector_t& v1,
const vector_t& v2);
622 template <
class vector_t>
624 const vector_t& v1,
const vector_t& v2);
632 template <
class vector_t>
634 const vector_t& v1,
const vector_t& v2,
645 template <
class vector_t>
647 const vector_t& v2,
const vector_t& v3);
715 template <
class vector_t>
717 const vector_t& n,
const vector_t& r0);
#define UG_API
Definition: ug_config.h:65
double number
Definition: types.h:124
number CalculateTetrahedronVolume(const vector3 &a, const vector3 &b, const vector3 &c, const vector3 &d)
Definition: math_util.cpp:440
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 TransformPointSetTo2D(vector2 *pointSetOut, const vector3 *pointSet, size_t numPoints)
transforms points from 3d space to 2d space.
Definition: math_util.cpp:294
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
number CalculateOctahedronVolume(const vector3 &a, const vector3 &b, const vector3 &c, const vector3 &d, const vector3 &e, const vector3 &f)
Definition: math_util.cpp:525
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
number CalculatePyramidVolume(const vector3 &a, const vector3 &b, const vector3 &c, const vector3 &d, const vector3 &e)
Definition: math_util.cpp:465
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
bool RayRayIntersection3d(vector3 &aOut, vector3 &bOut, const vector3 &p0, const vector3 &dir0, const vector3 &p1, const vector3 &dir1)
intersects two 3d rays
Definition: math_util.cpp:331
bool ConstructOrthonormalSystem(matrix33 &matOut, const vector3 &v, size_t vColInd)
constructs a orthonormal matrix given a vector in 3d.
Definition: math_util.cpp:211
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
void CalculateCovarianceMatrix(matrix33 &matOut, const vector3 *pointSet, const vector3 ¢er, size_t numPoints)
Calculates the covariance matrix of a given point-set.
Definition: math_util.cpp:248
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
number CalculatePrismVolume(const vector3 &a, const vector3 &b, const vector3 &c, const vector3 &d, const vector3 &e, const vector3 &f)
Definition: math_util.cpp:485
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
bool RayCylinderIntersection(number &tMinOut, number &tMaxOut, const vector3 &rayFrom, const vector3 &rayDir, const vector3 &cylCenter, const vector3 &cylAxis, number cylRadius)
returns the parameter values at which a given ray intersects an infinite cylinder.
Definition: math_util.cpp:386
int BinomCoeff(int n, int k)
Returns the BinomialCoefficient.
Definition: math_util.cpp:553
TNumber deg_to_rad(TNumber deg)
Definition: math_util_impl.hpp:49
UG_API bool TriangleTriangleIntersection(const MathVector< 3 > &p0, const MathVector< 3 > &p1, const MathVector< 3 > &p2, const MathVector< 3 > &q0, const MathVector< 3 > &q1, const MathVector< 3 > &q2, MathVector< 3 > *ip1Out=NULL, MathVector< 3 > *ip2Out=NULL, number snapThreshold=SMALL)
checks whether two triangles intersect and returns the intervals, if they do.
Definition: tritri.cpp:390
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
UG_API bool TriangleBoxIntersection(const MathVector< 3 > &p0, const MathVector< 3 > &p1, const MathVector< 3 > &p2, const MathVector< 3 > &boxMin, const MathVector< 3 > &boxMax)
checks whether a triangle and an axis-aligned box intersect.
Definition: tri_box.cpp:370
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 TriangleCircumcenter(vector2 ¢erOut, const vector2 &p1, const vector2 &p2, const vector2 &p3)
Calculates the circumcenter of a triangle.
Definition: math_util.cpp:45
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
number CalculateHexahedronVolume(const vector3 &a, const vector3 &b, const vector3 &c, const vector3 &d, const vector3 &e, const vector3 &f, const vector3 &g, const vector3 &h)
Definition: math_util.cpp:503
bool LineLineIntersection3d(vector3 &aOut, vector3 &bOut, const vector3 &a1, const vector3 &a2, const vector3 &b1, const vector3 &b2)
intersects two 3d line segments
Definition: math_util.cpp:352
bool FindNormal(vector3 &normOut, const vector3 &v)
finds a normal to the given vector in 3d.
Definition: math_util.cpp:184
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
bool FindClosestPlane(vector3 ¢erOut, vector3 &normalOut, const vector3 *pointSet, size_t numPoints)
Finds the plane which minimizes the distance to the given point set.
Definition: math_util.cpp:268
TNumber rad_to_deg(TNumber rad)
Definition: math_util_impl.hpp:58
number DistanceLineToLine(const vector3 &a1, const vector3 &a2, const vector3 &b1, const vector3 &b2)
calculates the distance between two 3d line segments
Definition: math_util.cpp:369
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
MathVector< 2, number > vector2
a 2d vector
Definition: ugmath_types.h:69
MathVector< 3, number > vector3
a 3d vector
Definition: ugmath_types.h:72
MathMatrix< 3, 3, number > matrix33
a 3x3 matrix
Definition: ugmath_types.h:81
const number SMALL
Definition: math_constants.h:41