KrisLibrary  1.0.0
rayprimitives.h
Go to the documentation of this file.
1 #ifndef GEOMETRY_RAYPRIMITIVES_H
2 #define GEOMETRY_RAYPRIMITIVES_H
3 
4 #include "primitives.h"
5 
12 namespace Geometry {
13 
16 
18 struct PointRay2D : public Point2D
19 {
20  bool isRay;
21 };
22 
24 struct PointRay3D : public Point3D
25 {
26  bool isRay;
27 };
28 
37 inline Real HomogeneousCmp(Real a, bool awZero, Real b, bool bwZero)
38 {
39  if(awZero == bwZero) return a-b;
40  else if(awZero) {
41  if(a == 0) return -b;
42  else return a;
43  }
44  else if(bwZero) {
45  if(b == 0) return -a;
46  else return b;
47  }
48 }
49 
51 inline Real HomogeneousSub(Real a, bool awZero, Real b, bool bwZero)
52 {
53  if(awZero == bwZero) return a-b;
54  else if(awZero) return a;
55  else return -b;
56 }
57 
58 
61 inline bool LexicalRay2DOrder (const PointRay2D& p1,const PointRay2D& p2)
62 {
63  if(p1.isRay != p2.isRay) {
64  if(p1.isRay) {
65  if(p1.x < 0) return true;
66  else if(p1.x > 0) return false;
67  if(p2.x > 0) return true;
68  else if(p2.x < 0) return false;
69 
70  return (p1.y < 0);
71  }
72  else return !LexicalRay2DOrder(p2,p1);
73  }
74  if(p1.x < p2.x) return true;
75  else if(p1.x > p2.x) return false;
76  return (p1.y < p2.y);
77 }
78 
81 inline bool LexicalRay3DOrder (const PointRay3D& p1,const PointRay3D& p2)
82 {
83  if(p1.isRay != p2.isRay) {
84  if(p1.isRay) {
85  if(p1.x < 0) return true;
86  else if(p1.x > 0) return false;
87  if(p2.x > 0) return true;
88  else if(p2.x < 0) return false;
89 
90  if(p1.y < 0) return true;
91  else if(p1.y > 0) return false;
92  if(p2.y > 0) return true;
93  else if(p2.y < 0) return false;
94 
95  return (p1.z < 0);
96  }
97  else return !LexicalRay3DOrder(p2,p1);
98  }
99  if(p1.x < p2.x) return true;
100  else if(p1.x > p2.x) return false;
101  if(p1.y < p2.y) return true;
102  else if(p1.y > p2.y) return false;
103  return (p1.z < p2.z);
104 }
105 
108 inline Real OrientRay2D(const PointRay2D& p0, const PointRay2D& p1, const PointRay2D& p2)
109 {
110  //shift it so p0 isn't a ray (if possible)
111  if(p0.isRay) {
112  if(p1.isRay) {
113  if(p2.isRay)
114  return (p1.x - p0.x)*(p2.y - p0.y) - (p2.x - p0.x)*(p1.y - p0.y);
115  else
116  return OrientRay2D(p2,p0,p1);
117  }
118  else
119  return OrientRay2D(p1,p2,p0);
120  }
121  Real bx = p1.isRay ? p1.x : p1.x - p0.x;
122  Real by = p1.isRay ? p1.y : p1.y - p0.y;
123  Real cx = p2.isRay ? p2.x : p2.x - p0.x;
124  Real cy = p2.isRay ? p2.y : p2.y - p0.y;
125  return bx*cy-cx*by;
126 }
127 
130 } //namespace Geometry
131 
132 namespace std {
133 
134 inline ostream& operator << (ostream& out, const Geometry::PointRay2D& pt)
135 {
136  out<<pt.x<<" "<<pt.y;
137  if(pt.isRay) out<<" (ray)";
138  return out;
139 }
140 
141 
142 }
143 
144 #endif
145 
A 3D point or infinite ray. Note: rays should be normalized unit vectors.
Definition: rayprimitives.h:24
A 3D vector class.
Definition: math3d/primitives.h:136
Real HomogeneousCmp(Real a, bool awZero, Real b, bool bwZero)
Comparison of 1D points in homogeneous coordinates.
Definition: rayprimitives.h:37
Definition: rayprimitives.h:132
Real OrientRay2D(const PointRay2D &p0, const PointRay2D &p1, const PointRay2D &p2)
Definition: rayprimitives.h:108
Contains ordering primitives used in geometry computations.
Real HomogeneousSub(Real a, bool awZero, Real b, bool bwZero)
Subtraction of 1D points in homogeneous coordinates.
Definition: rayprimitives.h:51
A 2D point or infinite ray. Note: rays should be normalized unit vectors.
Definition: rayprimitives.h:18
bool LexicalRay2DOrder(const PointRay2D &p1, const PointRay2D &p2)
Definition: rayprimitives.h:61
A 2D vector class.
Definition: math3d/primitives.h:41
bool LexicalRay3DOrder(const PointRay3D &p1, const PointRay3D &p2)
Definition: rayprimitives.h:81
Contains all definitions in the Geometry package.
Definition: AnyGeometry.cpp:26