Mercurial > hg > orthanc-stone
annotate Framework/Toolbox/CoordinateSystem3D.cpp @ 157:2309e8d86efe wasm
IntersectPlaneAndLine
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 09 Feb 2018 16:00:29 +0100 |
parents | 2e023be0563c |
children | a053ca7fa5c6 |
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:
122
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 | |
110
53025eecbc95
renamed SliceGeometry as CoordinateSystem3D
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
89
diff
changeset
|
22 #include "CoordinateSystem3D.h" |
0 | 23 |
24 #include "GeometryToolbox.h" | |
25 | |
113
2eca030792aa
using the Orthanc Framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
110
diff
changeset
|
26 #include <Core/Logging.h> |
2eca030792aa
using the Orthanc Framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
110
diff
changeset
|
27 #include <Core/Toolbox.h> |
2eca030792aa
using the Orthanc Framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
110
diff
changeset
|
28 #include <Core/OrthancException.h> |
0 | 29 |
30 namespace OrthancStone | |
31 { | |
110
53025eecbc95
renamed SliceGeometry as CoordinateSystem3D
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
89
diff
changeset
|
32 void CoordinateSystem3D::CheckAndComputeNormal() |
0 | 33 { |
34 // DICOM expects normal vectors to define the axes: "The row and | |
35 // column direction cosine vectors shall be normal, i.e., the dot | |
36 // product of each direction cosine vector with itself shall be | |
37 // unity." | |
38 // http://dicom.nema.org/medical/dicom/current/output/chtml/part03/sect_C.7.6.2.html | |
39 if (!GeometryToolbox::IsNear(boost::numeric::ublas::norm_2(axisX_), 1.0) || | |
40 !GeometryToolbox::IsNear(boost::numeric::ublas::norm_2(axisY_), 1.0)) | |
41 { | |
42 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
43 } | |
44 | |
45 // The vectors within "Image Orientation Patient" must be | |
46 // orthogonal, according to the DICOM specification: "The row and | |
47 // column direction cosine vectors shall be orthogonal, i.e., | |
48 // their dot product shall be zero." | |
49 // http://dicom.nema.org/medical/dicom/current/output/chtml/part03/sect_C.7.6.2.html | |
50 if (!GeometryToolbox::IsCloseToZero(boost::numeric::ublas::inner_prod(axisX_, axisY_))) | |
51 { | |
52 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
53 } | |
54 | |
55 GeometryToolbox::CrossProduct(normal_, axisX_, axisY_); | |
56 | |
157
2309e8d86efe
IntersectPlaneAndLine
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
152
diff
changeset
|
57 d_ = -(normal_[0] * origin_[0] + normal_[1] * origin_[1] + normal_[2] * origin_[2]); |
2309e8d86efe
IntersectPlaneAndLine
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
152
diff
changeset
|
58 |
0 | 59 // Just a sanity check, it should be useless by construction |
60 assert(GeometryToolbox::IsNear(boost::numeric::ublas::norm_2(normal_), 1.0)); | |
61 } | |
62 | |
63 | |
110
53025eecbc95
renamed SliceGeometry as CoordinateSystem3D
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
89
diff
changeset
|
64 void CoordinateSystem3D::SetupCanonical() |
0 | 65 { |
66 GeometryToolbox::AssignVector(origin_, 0, 0, 0); | |
67 GeometryToolbox::AssignVector(axisX_, 1, 0, 0); | |
68 GeometryToolbox::AssignVector(axisY_, 0, 1, 0); | |
69 CheckAndComputeNormal(); | |
70 } | |
71 | |
72 | |
110
53025eecbc95
renamed SliceGeometry as CoordinateSystem3D
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
89
diff
changeset
|
73 CoordinateSystem3D::CoordinateSystem3D(const Vector& origin, |
53025eecbc95
renamed SliceGeometry as CoordinateSystem3D
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
89
diff
changeset
|
74 const Vector& axisX, |
53025eecbc95
renamed SliceGeometry as CoordinateSystem3D
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
89
diff
changeset
|
75 const Vector& axisY) : |
0 | 76 origin_(origin), |
77 axisX_(axisX), | |
78 axisY_(axisY) | |
79 { | |
80 CheckAndComputeNormal(); | |
81 } | |
82 | |
83 | |
110
53025eecbc95
renamed SliceGeometry as CoordinateSystem3D
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
89
diff
changeset
|
84 void CoordinateSystem3D::Setup(const std::string& imagePositionPatient, |
53025eecbc95
renamed SliceGeometry as CoordinateSystem3D
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
89
diff
changeset
|
85 const std::string& imageOrientationPatient) |
0 | 86 { |
87 std::string tmpPosition = Orthanc::Toolbox::StripSpaces(imagePositionPatient); | |
88 std::string tmpOrientation = Orthanc::Toolbox::StripSpaces(imageOrientationPatient); | |
89 | |
90 Vector orientation; | |
91 if (!GeometryToolbox::ParseVector(origin_, tmpPosition) || | |
92 !GeometryToolbox::ParseVector(orientation, tmpOrientation) || | |
93 origin_.size() != 3 || | |
94 orientation.size() != 6) | |
95 { | |
96 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
97 } | |
98 | |
99 axisX_.resize(3); | |
100 axisX_[0] = orientation[0]; | |
101 axisX_[1] = orientation[1]; | |
102 axisX_[2] = orientation[2]; | |
103 | |
104 axisY_.resize(3); | |
105 axisY_[0] = orientation[3]; | |
106 axisY_[1] = orientation[4]; | |
107 axisY_[2] = orientation[5]; | |
108 | |
109 CheckAndComputeNormal(); | |
110 } | |
111 | |
112 | |
110
53025eecbc95
renamed SliceGeometry as CoordinateSystem3D
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
89
diff
changeset
|
113 CoordinateSystem3D::CoordinateSystem3D(const OrthancPlugins::IDicomDataset& dicom) |
0 | 114 { |
32 | 115 std::string a, b; |
116 | |
117 if (dicom.GetStringValue(a, OrthancPlugins::DICOM_TAG_IMAGE_POSITION_PATIENT) && | |
118 dicom.GetStringValue(b, OrthancPlugins::DICOM_TAG_IMAGE_ORIENTATION_PATIENT)) | |
0 | 119 { |
32 | 120 Setup(a, b); |
0 | 121 } |
122 else | |
123 { | |
124 SetupCanonical(); | |
125 } | |
122
e3433dabfb8d
refactoring DicomStructureSet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
113
diff
changeset
|
126 } |
e3433dabfb8d
refactoring DicomStructureSet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
113
diff
changeset
|
127 |
e3433dabfb8d
refactoring DicomStructureSet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
113
diff
changeset
|
128 |
e3433dabfb8d
refactoring DicomStructureSet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
113
diff
changeset
|
129 CoordinateSystem3D::CoordinateSystem3D(const Orthanc::DicomMap& dicom) |
e3433dabfb8d
refactoring DicomStructureSet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
113
diff
changeset
|
130 { |
e3433dabfb8d
refactoring DicomStructureSet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
113
diff
changeset
|
131 std::string a, b; |
e3433dabfb8d
refactoring DicomStructureSet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
113
diff
changeset
|
132 |
e3433dabfb8d
refactoring DicomStructureSet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
113
diff
changeset
|
133 if (dicom.CopyToString(a, Orthanc::DICOM_TAG_IMAGE_POSITION_PATIENT, false) && |
e3433dabfb8d
refactoring DicomStructureSet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
113
diff
changeset
|
134 dicom.CopyToString(b, Orthanc::DICOM_TAG_IMAGE_ORIENTATION_PATIENT, false)) |
e3433dabfb8d
refactoring DicomStructureSet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
113
diff
changeset
|
135 { |
e3433dabfb8d
refactoring DicomStructureSet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
113
diff
changeset
|
136 Setup(a, b); |
e3433dabfb8d
refactoring DicomStructureSet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
113
diff
changeset
|
137 } |
e3433dabfb8d
refactoring DicomStructureSet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
113
diff
changeset
|
138 else |
e3433dabfb8d
refactoring DicomStructureSet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
113
diff
changeset
|
139 { |
e3433dabfb8d
refactoring DicomStructureSet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
113
diff
changeset
|
140 SetupCanonical(); |
e3433dabfb8d
refactoring DicomStructureSet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
113
diff
changeset
|
141 } |
e3433dabfb8d
refactoring DicomStructureSet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
113
diff
changeset
|
142 } |
0 | 143 |
144 | |
110
53025eecbc95
renamed SliceGeometry as CoordinateSystem3D
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
89
diff
changeset
|
145 Vector CoordinateSystem3D::MapSliceToWorldCoordinates(double x, |
53025eecbc95
renamed SliceGeometry as CoordinateSystem3D
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
89
diff
changeset
|
146 double y) const |
0 | 147 { |
148 return origin_ + x * axisX_ + y * axisY_; | |
149 } | |
150 | |
151 | |
110
53025eecbc95
renamed SliceGeometry as CoordinateSystem3D
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
89
diff
changeset
|
152 double CoordinateSystem3D::ProjectAlongNormal(const Vector& point) const |
0 | 153 { |
154 return boost::numeric::ublas::inner_prod(point, normal_); | |
155 } | |
156 | |
157 | |
110
53025eecbc95
renamed SliceGeometry as CoordinateSystem3D
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
89
diff
changeset
|
158 void CoordinateSystem3D::ProjectPoint(double& offsetX, |
53025eecbc95
renamed SliceGeometry as CoordinateSystem3D
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
89
diff
changeset
|
159 double& offsetY, |
53025eecbc95
renamed SliceGeometry as CoordinateSystem3D
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
89
diff
changeset
|
160 const Vector& point) const |
0 | 161 { |
162 // Project the point onto the slice | |
163 Vector projection; | |
164 GeometryToolbox::ProjectPointOntoPlane(projection, point, normal_, origin_); | |
165 | |
166 // As the axes are orthonormal vectors thanks to | |
167 // CheckAndComputeNormal(), the following dot products give the | |
168 // offset of the origin of the slice wrt. the origin of the | |
169 // reference plane https://en.wikipedia.org/wiki/Vector_projection | |
170 offsetX = boost::numeric::ublas::inner_prod(axisX_, projection - origin_); | |
171 offsetY = boost::numeric::ublas::inner_prod(axisY_, projection - origin_); | |
172 } | |
151
c5044bbfc303
CoordinateSystem3D::IntersectSegment()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
173 |
c5044bbfc303
CoordinateSystem3D::IntersectSegment()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
174 |
152 | 175 bool CoordinateSystem3D::IntersectSegment(Vector& p, |
151
c5044bbfc303
CoordinateSystem3D::IntersectSegment()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
176 const Vector& edgeFrom, |
c5044bbfc303
CoordinateSystem3D::IntersectSegment()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
177 const Vector& edgeTo) const |
c5044bbfc303
CoordinateSystem3D::IntersectSegment()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
178 { |
157
2309e8d86efe
IntersectPlaneAndLine
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
152
diff
changeset
|
179 return GeometryToolbox::IntersectPlaneAndSegment(p, normal_, d_, edgeFrom, edgeTo); |
2309e8d86efe
IntersectPlaneAndLine
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
152
diff
changeset
|
180 } |
2309e8d86efe
IntersectPlaneAndLine
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
152
diff
changeset
|
181 |
2309e8d86efe
IntersectPlaneAndLine
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
152
diff
changeset
|
182 |
2309e8d86efe
IntersectPlaneAndLine
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
152
diff
changeset
|
183 bool CoordinateSystem3D::IntersectLine(Vector& p, |
2309e8d86efe
IntersectPlaneAndLine
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
152
diff
changeset
|
184 const Vector& origin, |
2309e8d86efe
IntersectPlaneAndLine
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
152
diff
changeset
|
185 const Vector& direction) const |
2309e8d86efe
IntersectPlaneAndLine
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
152
diff
changeset
|
186 { |
2309e8d86efe
IntersectPlaneAndLine
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
152
diff
changeset
|
187 return GeometryToolbox::IntersectPlaneAndLine(p, normal_, d_, origin, direction); |
151
c5044bbfc303
CoordinateSystem3D::IntersectSegment()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
188 } |
0 | 189 } |