comparison Framework/Toolbox/OrientedBoundingBox.cpp @ 151:c5044bbfc303 wasm

CoordinateSystem3D::IntersectSegment()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 01 Feb 2018 17:37:03 +0100
parents fb7d602e7025
children a053ca7fa5c6
comparison
equal deleted inserted replaced
150:62670cc2bb50 151:c5044bbfc303
25 25
26 #include <cassert> 26 #include <cassert>
27 27
28 namespace OrthancStone 28 namespace OrthancStone
29 { 29 {
30 static bool IntersectPlaneAndSegment(Vector& p,
31 const Vector& normal,
32 double d,
33 const Vector& edgeFrom,
34 const Vector& edgeTo)
35 {
36 // http://geomalgorithms.com/a05-_intersect-1.html#Line-Plane-Intersection
37
38 // Check for parallel line and plane
39 Vector direction = edgeTo - edgeFrom;
40 double denominator = boost::numeric::ublas::inner_prod(direction, normal);
41
42 if (fabs(denominator) < 100.0 * std::numeric_limits<double>::epsilon())
43 {
44 return false;
45 }
46 else
47 {
48 // Compute intersection
49 double t = -(normal[0] * edgeFrom[0] +
50 normal[1] * edgeFrom[1] +
51 normal[2] * edgeFrom[2] + d) / denominator;
52
53 if (t >= 0 && t <= 1)
54 {
55 // The intersection lies inside edge segment
56 p = edgeFrom + t * direction;
57 return true;
58 }
59 else
60 {
61 return false;
62 }
63 }
64 }
65
66
67 OrientedBoundingBox::OrientedBoundingBox(const ImageBuffer3D& image) 30 OrientedBoundingBox::OrientedBoundingBox(const ImageBuffer3D& image)
68 { 31 {
69 unsigned int n = image.GetDepth(); 32 unsigned int n = image.GetDepth();
70 if (n < 1) 33 if (n < 1)
71 { 34 {
113 76
114 // Loop over all the 12 edges (segments) of the oriented 77 // Loop over all the 12 edges (segments) of the oriented
115 // bounding box, and check whether they intersect the plane 78 // bounding box, and check whether they intersect the plane
116 79
117 // X-aligned edges 80 // X-aligned edges
118 if (IntersectPlaneAndSegment(p, normal, d, 81 if (GeometryToolbox::IntersectPlaneAndSegment
119 c_ - u_ * hu_ - v_ * hv_ - w_ * hw_, 82 (p, normal, d,
120 c_ + u_ * hu_ - v_ * hv_ - w_ * hw_)) 83 c_ - u_ * hu_ - v_ * hv_ - w_ * hw_,
121 { 84 c_ + u_ * hu_ - v_ * hv_ - w_ * hw_))
122 points.push_back(p); 85 {
123 } 86 points.push_back(p);
124 87 }
125 if (IntersectPlaneAndSegment(p, normal, d, 88
126 c_ - u_ * hu_ + v_ * hv_ - w_ * hw_, 89 if (GeometryToolbox::IntersectPlaneAndSegment
127 c_ + u_ * hu_ + v_ * hv_ - w_ * hw_)) 90 (p, normal, d,
128 { 91 c_ - u_ * hu_ + v_ * hv_ - w_ * hw_,
129 points.push_back(p); 92 c_ + u_ * hu_ + v_ * hv_ - w_ * hw_))
130 } 93 {
131 94 points.push_back(p);
132 if (IntersectPlaneAndSegment(p, normal, d, 95 }
133 c_ - u_ * hu_ - v_ * hv_ + w_ * hw_, 96
134 c_ + u_ * hu_ - v_ * hv_ + w_ * hw_)) 97 if (GeometryToolbox::IntersectPlaneAndSegment
135 { 98 (p, normal, d,
136 points.push_back(p); 99 c_ - u_ * hu_ - v_ * hv_ + w_ * hw_,
137 } 100 c_ + u_ * hu_ - v_ * hv_ + w_ * hw_))
138 101 {
139 if (IntersectPlaneAndSegment(p, normal, d, 102 points.push_back(p);
140 c_ - u_ * hu_ + v_ * hv_ + w_ * hw_, 103 }
141 c_ + u_ * hu_ + v_ * hv_ + w_ * hw_)) 104
105 if (GeometryToolbox::IntersectPlaneAndSegment
106 (p, normal, d,
107 c_ - u_ * hu_ + v_ * hv_ + w_ * hw_,
108 c_ + u_ * hu_ + v_ * hv_ + w_ * hw_))
142 { 109 {
143 points.push_back(p); 110 points.push_back(p);
144 } 111 }
145 112
146 // Y-aligned edges 113 // Y-aligned edges
147 if (IntersectPlaneAndSegment(p, normal, d, 114 if (GeometryToolbox::IntersectPlaneAndSegment
148 c_ - u_ * hu_ - v_ * hv_ - w_ * hw_, 115 (p, normal, d,
149 c_ - u_ * hu_ + v_ * hv_ - w_ * hw_)) 116 c_ - u_ * hu_ - v_ * hv_ - w_ * hw_,
150 { 117 c_ - u_ * hu_ + v_ * hv_ - w_ * hw_))
151 points.push_back(p); 118 {
152 } 119 points.push_back(p);
153 120 }
154 if (IntersectPlaneAndSegment(p, normal, d, 121
155 c_ + u_ * hu_ - v_ * hv_ - w_ * hw_, 122 if (GeometryToolbox::IntersectPlaneAndSegment
156 c_ + u_ * hu_ + v_ * hv_ - w_ * hw_)) 123 (p, normal, d,
157 { 124 c_ + u_ * hu_ - v_ * hv_ - w_ * hw_,
158 points.push_back(p); 125 c_ + u_ * hu_ + v_ * hv_ - w_ * hw_))
159 } 126 {
160 127 points.push_back(p);
161 if (IntersectPlaneAndSegment(p, normal, d, 128 }
162 c_ - u_ * hu_ - v_ * hv_ + w_ * hw_, 129
163 c_ - u_ * hu_ + v_ * hv_ + w_ * hw_)) 130 if (GeometryToolbox::IntersectPlaneAndSegment
164 { 131 (p, normal, d,
165 points.push_back(p); 132 c_ - u_ * hu_ - v_ * hv_ + w_ * hw_,
166 } 133 c_ - u_ * hu_ + v_ * hv_ + w_ * hw_))
167 134 {
168 if (IntersectPlaneAndSegment(p, normal, d, 135 points.push_back(p);
169 c_ + u_ * hu_ - v_ * hv_ + w_ * hw_, 136 }
170 c_ + u_ * hu_ + v_ * hv_ + w_ * hw_)) 137
138 if (GeometryToolbox::IntersectPlaneAndSegment
139 (p, normal, d,
140 c_ + u_ * hu_ - v_ * hv_ + w_ * hw_,
141 c_ + u_ * hu_ + v_ * hv_ + w_ * hw_))
171 { 142 {
172 points.push_back(p); 143 points.push_back(p);
173 } 144 }
174 145
175 // Z-aligned edges 146 // Z-aligned edges
176 if (IntersectPlaneAndSegment(p, normal, d, 147 if (GeometryToolbox::IntersectPlaneAndSegment
177 c_ - u_ * hu_ - v_ * hv_ - w_ * hw_, 148 (p, normal, d,
178 c_ - u_ * hu_ - v_ * hv_ + w_ * hw_)) 149 c_ - u_ * hu_ - v_ * hv_ - w_ * hw_,
179 { 150 c_ - u_ * hu_ - v_ * hv_ + w_ * hw_))
180 points.push_back(p); 151 {
181 } 152 points.push_back(p);
182 153 }
183 if (IntersectPlaneAndSegment(p, normal, d, 154
184 c_ + u_ * hu_ - v_ * hv_ - w_ * hw_, 155 if (GeometryToolbox::IntersectPlaneAndSegment
185 c_ + u_ * hu_ - v_ * hv_ + w_ * hw_)) 156 (p, normal, d,
186 { 157 c_ + u_ * hu_ - v_ * hv_ - w_ * hw_,
187 points.push_back(p); 158 c_ + u_ * hu_ - v_ * hv_ + w_ * hw_))
188 } 159 {
189 160 points.push_back(p);
190 if (IntersectPlaneAndSegment(p, normal, d, 161 }
191 c_ - u_ * hu_ + v_ * hv_ - w_ * hw_, 162
192 c_ - u_ * hu_ + v_ * hv_ + w_ * hw_)) 163 if (GeometryToolbox::IntersectPlaneAndSegment
193 { 164 (p, normal, d,
194 points.push_back(p); 165 c_ - u_ * hu_ + v_ * hv_ - w_ * hw_,
195 } 166 c_ - u_ * hu_ + v_ * hv_ + w_ * hw_))
196 167 {
197 if (IntersectPlaneAndSegment(p, normal, d, 168 points.push_back(p);
198 c_ + u_ * hu_ + v_ * hv_ - w_ * hw_, 169 }
199 c_ + u_ * hu_ + v_ * hv_ + w_ * hw_)) 170
171 if (GeometryToolbox::IntersectPlaneAndSegment
172 (p, normal, d,
173 c_ + u_ * hu_ + v_ * hv_ - w_ * hw_,
174 c_ + u_ * hu_ + v_ * hv_ + w_ * hw_))
200 { 175 {
201 points.push_back(p); 176 points.push_back(p);
202 } 177 }
203 178
204 return true; 179 return true;