Mercurial > hg > orthanc-stone
annotate Framework/Toolbox/GeometryToolbox.cpp @ 135:e2fe9352f240 wasm
upgrade to year 2018
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 02 Jan 2018 09:56:08 +0100 |
parents | a4d0b6c82b29 |
children | 2115530d3703 |
rev | line source |
---|---|
0 | 1 /** |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
135
e2fe9352f240
upgrade to year 2018
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
118
diff
changeset
|
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium |
0 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
47 | 8 * modify it under the terms of the GNU Affero General Public License |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
0 | 11 * |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
47 | 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
0 | 18 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 **/ | |
20 | |
21 | |
22 #include "GeometryToolbox.h" | |
23 | |
113
2eca030792aa
using the Orthanc Framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
47
diff
changeset
|
24 #include <Core/Logging.h> |
2eca030792aa
using the Orthanc Framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
47
diff
changeset
|
25 #include <Core/OrthancException.h> |
2eca030792aa
using the Orthanc Framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
47
diff
changeset
|
26 #include <Core/Toolbox.h> |
0 | 27 |
28 #include <stdio.h> | |
29 #include <boost/lexical_cast.hpp> | |
30 | |
31 namespace OrthancStone | |
32 { | |
33 namespace GeometryToolbox | |
34 { | |
35 void Print(const Vector& v) | |
36 { | |
37 for (size_t i = 0; i < v.size(); i++) | |
38 { | |
39 printf("%8.2f\n", v[i]); | |
40 } | |
41 printf("\n"); | |
42 } | |
43 | |
44 | |
45 bool ParseVector(Vector& target, | |
46 const std::string& value) | |
47 { | |
48 std::vector<std::string> items; | |
49 Orthanc::Toolbox::TokenizeString(items, value, '\\'); | |
50 | |
51 target.resize(items.size()); | |
52 | |
53 for (size_t i = 0; i < items.size(); i++) | |
54 { | |
55 try | |
56 { | |
57 target[i] = boost::lexical_cast<double>(Orthanc::Toolbox::StripSpaces(items[i])); | |
58 } | |
59 catch (boost::bad_lexical_cast&) | |
60 { | |
61 target.clear(); | |
62 return false; | |
63 } | |
64 } | |
65 | |
66 return true; | |
67 } | |
68 | |
69 | |
32 | 70 bool ParseVector(Vector& target, |
118
a4d0b6c82b29
using Orthanc::DicomMap instead of OrthancPlugins::DicomDatasetReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
113
diff
changeset
|
71 const Orthanc::DicomMap& dataset, |
a4d0b6c82b29
using Orthanc::DicomMap instead of OrthancPlugins::DicomDatasetReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
113
diff
changeset
|
72 const Orthanc::DicomTag& tag) |
32 | 73 { |
74 std::string value; | |
118
a4d0b6c82b29
using Orthanc::DicomMap instead of OrthancPlugins::DicomDatasetReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
113
diff
changeset
|
75 return (dataset.CopyToString(value, tag, false) && |
32 | 76 ParseVector(target, value)); |
77 } | |
78 | |
79 | |
0 | 80 void AssignVector(Vector& v, |
81 double v1, | |
82 double v2) | |
83 { | |
84 v.resize(2); | |
85 v[0] = v1; | |
86 v[1] = v2; | |
87 } | |
88 | |
89 | |
90 void AssignVector(Vector& v, | |
91 double v1, | |
92 double v2, | |
93 double v3) | |
94 { | |
95 v.resize(3); | |
96 v[0] = v1; | |
97 v[1] = v2; | |
98 v[2] = v3; | |
99 } | |
100 | |
101 | |
102 bool IsNear(double x, | |
103 double y) | |
104 { | |
105 // As most input is read as single-precision numbers, we take the | |
106 // epsilon machine for float32 into consideration to compare numbers | |
107 return IsNear(x, y, 10.0 * std::numeric_limits<float>::epsilon()); | |
108 } | |
109 | |
110 | |
111 void NormalizeVector(Vector& u) | |
112 { | |
113 double norm = boost::numeric::ublas::norm_2(u); | |
114 if (!IsCloseToZero(norm)) | |
115 { | |
116 u = u / norm; | |
117 } | |
118 } | |
119 | |
120 | |
121 void CrossProduct(Vector& result, | |
122 const Vector& u, | |
123 const Vector& v) | |
124 { | |
125 if (u.size() != 3 || | |
126 v.size() != 3) | |
127 { | |
128 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
129 } | |
130 | |
131 result.resize(3); | |
132 | |
133 result[0] = u[1] * v[2] - u[2] * v[1]; | |
134 result[1] = u[2] * v[0] - u[0] * v[2]; | |
135 result[2] = u[0] * v[1] - u[1] * v[0]; | |
136 } | |
137 | |
138 | |
139 void ProjectPointOntoPlane(Vector& result, | |
140 const Vector& point, | |
141 const Vector& planeNormal, | |
142 const Vector& planeOrigin) | |
143 { | |
144 double norm = boost::numeric::ublas::norm_2(planeNormal); | |
145 if (IsCloseToZero(norm)) | |
146 { | |
147 // Division by zero | |
148 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
149 } | |
150 | |
151 // Make sure the norm of the normal is 1 | |
152 Vector n; | |
153 n = planeNormal / norm; | |
154 | |
155 // Algebraic form of line–plane intersection, where the line passes | |
156 // through "point" along the direction "normal" (thus, l == n) | |
157 // https://en.wikipedia.org/wiki/Line%E2%80%93plane_intersection#Algebraic_form | |
158 result = boost::numeric::ublas::inner_prod(planeOrigin - point, n) * n + point; | |
159 } | |
160 | |
161 | |
162 | |
163 bool IsParallelOrOpposite(bool& isOpposite, | |
164 const Vector& u, | |
165 const Vector& v) | |
166 { | |
167 // The dot product of the two vectors gives the cosine of the angle | |
168 // between the vectors | |
169 // https://en.wikipedia.org/wiki/Dot_product | |
170 | |
171 double normU = boost::numeric::ublas::norm_2(u); | |
172 double normV = boost::numeric::ublas::norm_2(v); | |
173 | |
174 if (IsCloseToZero(normU) || | |
175 IsCloseToZero(normV)) | |
176 { | |
177 return false; | |
178 } | |
179 | |
180 double cosAngle = boost::numeric::ublas::inner_prod(u, v) / (normU * normV); | |
181 | |
182 // The angle must be zero, so the cosine must be almost equal to | |
183 // cos(0) == 1 (or cos(180) == -1 if allowOppositeDirection == true) | |
184 | |
185 if (IsCloseToZero(cosAngle - 1.0)) | |
186 { | |
187 isOpposite = false; | |
188 return true; | |
189 } | |
190 else if (IsCloseToZero(fabs(cosAngle) - 1.0)) | |
191 { | |
192 isOpposite = true; | |
193 return true; | |
194 } | |
195 else | |
196 { | |
197 return false; | |
198 } | |
199 } | |
200 | |
201 | |
202 bool IsParallel(const Vector& u, | |
203 const Vector& v) | |
204 { | |
205 bool isOpposite; | |
206 return (IsParallelOrOpposite(isOpposite, u, v) && | |
207 !isOpposite); | |
208 } | |
209 | |
210 | |
211 bool IntersectTwoPlanes(Vector& p, | |
212 Vector& direction, | |
213 const Vector& origin1, | |
214 const Vector& normal1, | |
215 const Vector& origin2, | |
216 const Vector& normal2) | |
217 { | |
218 // This is "Intersection of 2 Planes", possibility "(C) 3 Plane | |
219 // Intersect Point" of: | |
220 // http://geomalgorithms.com/a05-_intersect-1.html | |
221 | |
222 // The direction of the line of intersection is orthogonal to the | |
223 // normal of both planes | |
224 CrossProduct(direction, normal1, normal2); | |
225 | |
226 double norm = boost::numeric::ublas::norm_2(direction); | |
227 if (IsCloseToZero(norm)) | |
228 { | |
229 // The two planes are parallel or coincident | |
230 return false; | |
231 } | |
232 | |
233 double d1 = -boost::numeric::ublas::inner_prod(normal1, origin1); | |
234 double d2 = -boost::numeric::ublas::inner_prod(normal2, origin2); | |
235 Vector tmp = d2 * normal1 - d1 * normal2; | |
236 | |
237 CrossProduct(p, tmp, direction); | |
238 p /= norm; | |
239 | |
240 return true; | |
241 } | |
242 | |
243 | |
244 bool ClipLineToRectangle(double& x1, // Coordinates of the clipped line (out) | |
245 double& y1, | |
246 double& x2, | |
247 double& y2, | |
248 const double ax, // Two points defining the line (in) | |
249 const double ay, | |
250 const double bx, | |
251 const double by, | |
252 const double& xmin, // Coordinates of the rectangle (in) | |
253 const double& ymin, | |
254 const double& xmax, | |
255 const double& ymax) | |
256 { | |
257 // This is Skala algorithm for rectangles, "A new approach to line | |
258 // and line segment clipping in homogeneous coordinates" | |
259 // (2005). This is a direct, non-optimized translation of Algorithm | |
260 // 2 in the paper. | |
261 | |
262 static uint8_t tab1[16] = { 255 /* none */, | |
263 0, | |
264 0, | |
265 1, | |
266 1, | |
267 255 /* na */, | |
268 0, | |
269 2, | |
270 2, | |
271 0, | |
272 255 /* na */, | |
273 1, | |
274 1, | |
275 0, | |
276 0, | |
277 255 /* none */ }; | |
278 | |
279 | |
280 static uint8_t tab2[16] = { 255 /* none */, | |
281 3, | |
282 1, | |
283 3, | |
284 2, | |
285 255 /* na */, | |
286 2, | |
287 3, | |
288 3, | |
289 2, | |
290 255 /* na */, | |
291 2, | |
292 3, | |
293 1, | |
294 3, | |
295 255 /* none */ }; | |
296 | |
297 // Create the coordinates of the rectangle | |
298 Vector x[4]; | |
299 AssignVector(x[0], xmin, ymin, 1.0); | |
300 AssignVector(x[1], xmax, ymin, 1.0); | |
301 AssignVector(x[2], xmax, ymax, 1.0); | |
302 AssignVector(x[3], xmin, ymax, 1.0); | |
303 | |
304 // Move to homogoneous coordinates in 2D | |
305 Vector p; | |
306 | |
307 { | |
308 Vector a, b; | |
309 AssignVector(a, ax, ay, 1.0); | |
310 AssignVector(b, bx, by, 1.0); | |
311 CrossProduct(p, a, b); | |
312 } | |
313 | |
314 uint8_t c = 0; | |
315 | |
316 for (unsigned int k = 0; k < 4; k++) | |
317 { | |
318 if (boost::numeric::ublas::inner_prod(p, x[k]) >= 0) | |
319 { | |
320 c |= (1 << k); | |
321 } | |
322 } | |
323 | |
324 assert(c < 16); | |
325 | |
326 uint8_t i = tab1[c]; | |
327 uint8_t j = tab2[c]; | |
328 | |
329 if (i == 255 || j == 255) | |
330 { | |
331 return false; // No intersection | |
332 } | |
333 else | |
334 { | |
335 Vector a, b, e; | |
336 CrossProduct(e, x[i], x[(i + 1) % 4]); | |
337 CrossProduct(a, p, e); | |
338 CrossProduct(e, x[j], x[(j + 1) % 4]); | |
339 CrossProduct(b, p, e); | |
340 | |
341 // Go back to non-homogeneous coordinates | |
342 x1 = a[0] / a[2]; | |
343 y1 = a[1] / a[2]; | |
344 x2 = b[0] / b[2]; | |
345 y2 = b[1] / b[2]; | |
346 | |
347 return true; | |
348 } | |
349 } | |
32 | 350 |
351 | |
352 void GetPixelSpacing(double& spacingX, | |
353 double& spacingY, | |
118
a4d0b6c82b29
using Orthanc::DicomMap instead of OrthancPlugins::DicomDatasetReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
113
diff
changeset
|
354 const Orthanc::DicomMap& dicom) |
32 | 355 { |
356 Vector v; | |
357 | |
118
a4d0b6c82b29
using Orthanc::DicomMap instead of OrthancPlugins::DicomDatasetReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
113
diff
changeset
|
358 if (ParseVector(v, dicom, Orthanc::DICOM_TAG_PIXEL_SPACING)) |
32 | 359 { |
360 if (v.size() != 2 || | |
361 v[0] <= 0 || | |
362 v[1] <= 0) | |
363 { | |
364 LOG(ERROR) << "Bad value for PixelSpacing tag"; | |
365 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
366 } | |
367 else | |
368 { | |
369 spacingX = v[0]; | |
370 spacingY = v[1]; | |
371 } | |
372 } | |
373 else | |
374 { | |
375 // The "PixelSpacing" is of type 1C: It could be absent, use | |
376 // default value in such a case | |
377 spacingX = 1; | |
378 spacingY = 1; | |
379 } | |
380 } | |
0 | 381 } |
382 } |