KrisLibrary  1.0.0
d/misc.h
Go to the documentation of this file.
1 #ifndef MATH3D_MISC_H
2 #define MATH3D_MISC_H
3 
4 #include "primitives.h"
5 #include <assert.h>
6 
15 namespace Math3D {
16 
19 
20 inline bool IsFinite(const Vector2& x) { return Math::IsFinite(x.x)&&Math::IsFinite(x.y); }
21 inline bool IsFinite(const Vector3& x) { return Math::IsFinite(x.x)&&Math::IsFinite(x.y)&&Math::IsFinite(x.z); }
22 inline bool IsFinite(const Vector4& x) { return Math::IsFinite(x.x)&&Math::IsFinite(x.y)&&Math::IsFinite(x.z)&&Math::IsFinite(x.w); }
23 inline bool IsFinite(const Matrix2& R)
24 {
25  for(int i=0;i<2;i++)
26  for(int j=0;j<2;j++)
27  if(!Math::IsFinite(R(i,j))) return false;
28  return true;
29 }
30 inline bool IsFinite(const Matrix3& R)
31 {
32  for(int i=0;i<3;i++)
33  for(int j=0;j<3;j++)
34  if(!Math::IsFinite(R(i,j))) return false;
35  return true;
36 }
37 inline bool IsFinite(const Matrix4& R)
38 {
39  for(int i=0;i<4;i++)
40  for(int j=0;j<4;j++)
41  if(!Math::IsFinite(R(i,j))) return false;
42  return true;
43 }
44 
45 inline bool IsInf(const Vector2& x) { return Math::IsInf(x.x)||Math::IsInf(x.y); }
46 inline bool IsInf(const Vector3& x) { return Math::IsInf(x.x)||Math::IsInf(x.y)||Math::IsInf(x.z); }
47 inline bool IsInf(const Vector4& x) { return Math::IsInf(x.x)||Math::IsInf(x.y)||Math::IsInf(x.z)||Math::IsInf(x.w); }
48 inline bool IsInf(const Matrix2& R)
49 {
50  for(int i=0;i<2;i++)
51  for(int j=0;j<2;j++)
52  if(Math::IsInf(R(i,j))) return true;
53  return false;
54 }
55 inline bool IsInf(const Matrix3& R)
56 {
57  for(int i=0;i<3;i++)
58  for(int j=0;j<3;j++)
59  if(Math::IsInf(R(i,j))) return true;
60  return false;
61 }
62 inline bool IsInf(const Matrix4& R)
63 {
64  for(int i=0;i<4;i++)
65  for(int j=0;j<4;j++)
66  if(Math::IsInf(R(i,j))) return true;
67  return false;
68 }
69 
70 inline bool FuzzyZero(const Vector2& x,Real tol=Epsilon) { return Math::FuzzyZero(x.x,tol)&&Math::FuzzyZero(x.y,tol); }
71 inline bool FuzzyZero(const Vector3& x,Real tol=Epsilon) { return Math::FuzzyZero(x.x,tol)&&Math::FuzzyZero(x.y,tol)&&Math::FuzzyZero(x.z,tol); }
72 inline bool FuzzyZero(const Vector4& x,Real tol=Epsilon) { return Math::FuzzyZero(x.x,tol)&&Math::FuzzyZero(x.y,tol)&&Math::FuzzyZero(x.z,tol)&&Math::FuzzyZero(x.w,tol); }
73 
74 template<class V>
75 inline bool NormLess(const V& x, Real d) { return x.normSquared() < Sqr(d); }
76 template<class V>
77 inline bool NormLEQ(const V& x, Real d) { return x.normSquared() <= Sqr(d); }
78 template<class V>
79 inline bool NormGreater(const V& x, Real d) { return x.normSquared() > Sqr(d); }
80 template<class V>
81 inline bool NormGEQ(const V& x, Real d) { return x.normSquared() <= Sqr(d); }
82 
83 template<class V>
84 inline bool DistanceLess(const V& x, const V& y, Real d) { return x.distanceSquared(y) < Sqr(d); }
85 template<class V>
86 inline bool DistanceLEQ(const V& x, const V& y, Real d) { return x.distanceSquared(y) <= Sqr(d); }
87 template<class V>
88 inline bool DistanceGreater(const V& x, const V& y, Real d) { return x.distanceSquared(y) > Sqr(d); }
89 template<class V>
90 inline bool DistanceGEQ(const V& x, const V& y, Real d) { return x.distanceSquared(y) <= Sqr(d); }
91 
98 inline Real Orient2D(const Vector2& p0, const Vector2& p1, const Vector2& p2)
99 {
100  return (p1.x - p0.x)*(p2.y - p0.y) - (p2.x - p0.x)*(p1.y - p0.y);
101 }
102 
103 inline Real DistanceSquared2D(Real x1,Real y1,Real x2,Real y2)
104 {
105  return Sqr(x1-x2)+Sqr(y1-y2);
106 }
107 
108 inline Real Distance2D(Real x1,Real y1,Real x2,Real y2)
109 {
110  return Sqrt(DistanceSquared2D(x1,y1,x2,y2));
111 }
112 
113 inline bool IsSymmetric(const Matrix2& R,Real tol=Epsilon)
114 {
115  return FuzzyEquals(R(0,1),R(1,0),tol);
116 }
117 
118 inline bool IsSymmetric(const Matrix3& R,Real tol=Epsilon)
119 {
120  return FuzzyEquals(R(0,1),R(1,0),tol) && FuzzyEquals(R(0,2),R(2,0),tol) && FuzzyEquals(R(1,2),R(2,1),tol);
121 }
122 
123 inline bool IsSymmetric(const Matrix4& R,Real tol=Epsilon)
124 {
125  for(int i=0;i<4;i++)
126  for(int j=0;j<i;j++)
127  if(!FuzzyEquals(R(i,j),R(j,i),tol)) return false;
128  return true;
129 }
130 
131 inline bool IsUpperTriangular(const Matrix2& R,Real tol=Epsilon)
132 {
133  return Math::FuzzyZero(R(1,0),tol);
134 }
135 
136 inline bool IsUpperTriangular(const Matrix3& R,Real tol=Epsilon)
137 {
138  return Math::FuzzyZero(R(1,0),tol) && Math::FuzzyZero(R(2,0),tol) && Math::FuzzyZero(R(2,1),tol);
139 }
140 
141 inline bool IsUpperTriangular(const Matrix4& R,Real tol=Epsilon)
142 {
143  for(int i=0;i<4;i++)
144  for(int j=0;j<i;j++)
145  if(!Math::FuzzyZero(R(i,j),tol)) return false;
146  return true;
147 }
148 
151 } //namespace Math3D
152 
153 #endif
A 2x2 matrix class.
Definition: math3d/primitives.h:333
int IsInf(double x)
Returns +1 if x is +inf, -1 if x is -inf, and 0 otherwise.
Definition: infnan.cpp:40
Real Orient2D(const Vector2 &p0, const Vector2 &p1, const Vector2 &p2)
Orientation test for 2d points.
Definition: d/misc.h:98
Class declarations for useful 3D math types.
A 4x4 matrix class.
Definition: math3d/primitives.h:626
Contains all the definitions in the Math3D package.
Definition: AnyGeometry.h:12
A 3x3 matrix class.
Definition: math3d/primitives.h:469
A 2D vector class.
Definition: math3d/primitives.h:41
bool FuzzyEquals(double a, double b, double eps=dEpsilon)
Returns true if a and b are within +/- eps.
Definition: math.h:222
int IsFinite(double x)
Returns nonzero unless x is infinite or a NaN.
Definition: infnan.cpp:23