Mercurial > hg > orthanc-stone
annotate Framework/Toolbox/FiniteProjectiveCamera.cpp @ 254:abc1c6231947 am-2
cleanup
author | am@osimis.io |
---|---|
date | Tue, 03 Jul 2018 11:49:02 +0200 |
parents | 5412adf19980 |
children | b70e9be013e4 |
rev | line source |
---|---|
161 | 1 /** |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
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. | |
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 | |
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 | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 | |
22 #include "FiniteProjectiveCamera.h" | |
23 | |
188
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
24 #include "GeometryToolbox.h" |
192
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
25 #include "SubpixelReader.h" |
188
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
26 |
212
5412adf19980
resort to OrthancFramework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
201
diff
changeset
|
27 #include <Core/Logging.h> |
5412adf19980
resort to OrthancFramework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
201
diff
changeset
|
28 #include <Core/OrthancException.h> |
5412adf19980
resort to OrthancFramework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
201
diff
changeset
|
29 #include <Core/Images/Image.h> |
5412adf19980
resort to OrthancFramework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
201
diff
changeset
|
30 #include <Core/Images/ImageProcessing.h> |
161 | 31 |
32 namespace OrthancStone | |
33 { | |
34 void FiniteProjectiveCamera::ComputeMInverse() | |
35 { | |
36 using namespace boost::numeric::ublas; | |
37 | |
38 // inv(M) = inv(K * R) = inv(R) * inv(K) = R' * inv(K). This | |
39 // matrix is always invertible, by definition of finite | |
40 // projective cameras (page 157). | |
41 Matrix kinv; | |
42 LinearAlgebra::InvertUpperTriangularMatrix(kinv, k_); | |
43 minv_ = prod(trans(r_), kinv); | |
44 } | |
45 | |
46 | |
47 void FiniteProjectiveCamera::Setup(const Matrix& k, | |
48 const Matrix& r, | |
49 const Vector& c) | |
50 { | |
51 if (k.size1() != 3 || | |
52 k.size2() != 3 || | |
53 !LinearAlgebra::IsCloseToZero(k(1, 0)) || | |
54 !LinearAlgebra::IsCloseToZero(k(2, 0)) || | |
55 !LinearAlgebra::IsCloseToZero(k(2, 1))) | |
56 { | |
57 LOG(ERROR) << "Invalid intrinsic parameters"; | |
58 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
59 } | |
60 | |
61 if (r.size1() != 3 || | |
62 r.size2() != 3) | |
63 { | |
64 LOG(ERROR) << "Invalid size for a 3D rotation matrix"; | |
65 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
66 } | |
67 | |
68 if (!LinearAlgebra::IsRotationMatrix(r, 100.0 * std::numeric_limits<float>::epsilon())) | |
69 { | |
70 LOG(ERROR) << "Invalid rotation matrix"; | |
71 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
72 } | |
73 | |
74 if (c.size() != 3) | |
75 { | |
76 LOG(ERROR) << "Invalid camera center"; | |
77 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
78 } | |
79 | |
80 k_ = k; | |
81 r_ = r; | |
82 c_ = c; | |
83 | |
84 ComputeMInverse(); | |
85 | |
188
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
86 Matrix tmp = LinearAlgebra::IdentityMatrix(3); |
161 | 87 tmp.resize(3, 4); |
88 tmp(0, 3) = -c[0]; | |
89 tmp(1, 3) = -c[1]; | |
90 tmp(2, 3) = -c[2]; | |
91 | |
188
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
92 p_ = LinearAlgebra::Product(k, r, tmp); |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
93 |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
94 assert(p_.size1() == 3 && |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
95 p_.size2() == 4); |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
96 |
161 | 97 } |
98 | |
99 | |
100 void FiniteProjectiveCamera::Setup(const Matrix& p) | |
101 { | |
102 if (p.size1() != 3 || | |
103 p.size2() != 4) | |
104 { | |
105 LOG(ERROR) << "Invalid camera matrix"; | |
106 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
107 } | |
108 | |
109 p_ = p; | |
110 | |
111 // M is the left 3x3 submatrix of "P" | |
112 Matrix m = p; | |
113 m.resize(3, 3); | |
114 | |
115 // p4 is the last column of "P" | |
116 Vector p4(3); | |
117 p4[0] = p(0, 3); | |
118 p4[1] = p(1, 3); | |
119 p4[2] = p(2, 3); | |
120 | |
121 // The RQ decomposition is explained on page 157 | |
122 LinearAlgebra::RQDecomposition3x3(k_, r_, m); | |
123 ComputeMInverse(); | |
124 | |
188
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
125 c_ = LinearAlgebra::Product(-minv_, p4); |
161 | 126 } |
127 | |
128 | |
129 FiniteProjectiveCamera::FiniteProjectiveCamera(const double k[9], | |
130 const double r[9], | |
131 const double c[3]) | |
132 { | |
133 Matrix kk, rr; | |
134 Vector cc; | |
135 | |
136 LinearAlgebra::FillMatrix(kk, 3, 3, k); | |
137 LinearAlgebra::FillMatrix(rr, 3, 3, r); | |
138 LinearAlgebra::FillVector(cc, 3, c); | |
139 | |
140 Setup(kk, rr, cc); | |
141 } | |
142 | |
143 | |
144 FiniteProjectiveCamera::FiniteProjectiveCamera(const double p[12]) | |
145 { | |
146 Matrix pp; | |
147 LinearAlgebra::FillMatrix(pp, 3, 4, p); | |
148 Setup(pp); | |
149 } | |
150 | |
151 | |
152 Vector FiniteProjectiveCamera::GetRayDirection(double x, | |
153 double y) const | |
154 { | |
155 // This derives from Equation (6.14) on page 162, taking "mu = | |
156 // 1" and noticing that "-inv(M)*p4" corresponds to the camera | |
157 // center in finite projective cameras | |
158 | |
159 // The (x,y) coordinates on the imaged plane, as an homogeneous vector | |
160 Vector xx(3); | |
161 xx[0] = x; | |
162 xx[1] = y; | |
163 xx[2] = 1.0; | |
164 | |
165 return boost::numeric::ublas::prod(minv_, xx); | |
166 } | |
167 | |
168 | |
169 | |
170 static Vector SetupApply(const Vector& v, | |
171 bool infinityAllowed) | |
172 { | |
173 if (v.size() == 3) | |
174 { | |
175 // Vector "v" in non-homogeneous coordinates, add the homogeneous component | |
176 Vector vv; | |
177 LinearAlgebra::AssignVector(vv, v[0], v[1], v[2], 1.0); | |
178 return vv; | |
179 } | |
180 else if (v.size() == 4) | |
181 { | |
182 // Vector "v" is already in homogeneous coordinates | |
183 | |
184 if (!infinityAllowed && | |
185 LinearAlgebra::IsCloseToZero(v[3])) | |
186 { | |
187 LOG(ERROR) << "Cannot apply a finite projective camera to a " | |
188 << "point at infinity with this method"; | |
189 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
190 } | |
191 | |
192 return v; | |
193 } | |
194 else | |
195 { | |
196 LOG(ERROR) << "The input vector must represent a point in 3D"; | |
197 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
198 } | |
199 } | |
200 | |
201 | |
202 void FiniteProjectiveCamera::ApplyFinite(double& x, | |
203 double& y, | |
204 const Vector& v) const | |
205 { | |
206 Vector p = boost::numeric::ublas::prod(p_, SetupApply(v, false)); | |
207 | |
208 if (LinearAlgebra::IsCloseToZero(p[2])) | |
209 { | |
210 // Point at infinity: Should not happen with a finite input point | |
211 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
212 } | |
213 else | |
214 { | |
215 x = p[0] / p[2]; | |
216 y = p[1] / p[2]; | |
217 } | |
218 } | |
219 | |
220 | |
221 Vector FiniteProjectiveCamera::ApplyGeneral(const Vector& v) const | |
222 { | |
223 return boost::numeric::ublas::prod(p_, SetupApply(v, true)); | |
224 } | |
188
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
225 |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
226 |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
227 static Vector AddHomogeneousCoordinate(const Vector& p) |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
228 { |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
229 assert(p.size() == 3); |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
230 return LinearAlgebra::CreateVector(p[0], p[1], p[2], 1); |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
231 } |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
232 |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
233 |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
234 FiniteProjectiveCamera::FiniteProjectiveCamera(const Vector& camera, |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
235 const Vector& principalPoint, |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
236 double angle, |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
237 unsigned int imageWidth, |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
238 unsigned int imageHeight, |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
239 double pixelSpacingX, |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
240 double pixelSpacingY) |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
241 { |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
242 if (camera.size() != 3 || |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
243 principalPoint.size() != 3 || |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
244 LinearAlgebra::IsCloseToZero(pixelSpacingX) || |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
245 LinearAlgebra::IsCloseToZero(pixelSpacingY)) |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
246 { |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
247 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
248 } |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
249 |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
250 const double focal = boost::numeric::ublas::norm_2(camera - principalPoint); |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
251 |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
252 if (LinearAlgebra::IsCloseToZero(focal)) |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
253 { |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
254 LOG(ERROR) << "Camera lies on the image plane"; |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
255 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
256 } |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
257 |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
258 Matrix a; |
189
964118e7e6de
unit test for AlignVectorsWithRotation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
259 GeometryToolbox::AlignVectorsWithRotation(a, camera - principalPoint, |
964118e7e6de
unit test for AlignVectorsWithRotation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
260 LinearAlgebra::CreateVector(0, 0, -1)); |
188
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
261 |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
262 Matrix r = LinearAlgebra::Product(GeometryToolbox::CreateRotationMatrixAlongZ(angle), a); |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
263 |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
264 Matrix k = LinearAlgebra::ZeroMatrix(3, 3); |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
265 k(0,0) = focal / pixelSpacingX; |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
266 k(1,1) = focal / pixelSpacingY; |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
267 k(0,2) = static_cast<double>(imageWidth) / 2.0; |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
268 k(1,2) = static_cast<double>(imageHeight) / 2.0; |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
269 k(2,2) = 1; |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
270 |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
271 Setup(k, r, camera); |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
272 |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
273 { |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
274 // Sanity checks |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
275 Vector v1 = LinearAlgebra::Product(p_, AddHomogeneousCoordinate(camera)); |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
276 Vector v2 = LinearAlgebra::Product(p_, AddHomogeneousCoordinate(principalPoint)); |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
277 |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
278 if (!LinearAlgebra::IsCloseToZero(v1[2]) || // Camera is mapped to singularity |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
279 LinearAlgebra::IsCloseToZero(v2[2])) |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
280 { |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
281 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
282 } |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
283 |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
284 // The principal point must be mapped to the center of the image |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
285 v2 /= v2[2]; |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
286 |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
287 if (!LinearAlgebra::IsNear(v2[0], static_cast<double>(imageWidth) / 2.0) || |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
288 !LinearAlgebra::IsNear(v2[1], static_cast<double>(imageHeight) / 2.0)) |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
289 { |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
290 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
291 } |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
292 } |
45b03b04a777
calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
161
diff
changeset
|
293 } |
192
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
294 |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
295 |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
296 template <Orthanc::PixelFormat TargetFormat, |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
297 Orthanc::PixelFormat SourceFormat, |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
298 bool MIP> |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
299 static void ApplyRaytracerInternal(Orthanc::ImageAccessor& target, |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
300 const FiniteProjectiveCamera& camera, |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
301 const ImageBuffer3D& source, |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
302 VolumeProjection projection) |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
303 { |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
304 if (source.GetFormat() != SourceFormat || |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
305 target.GetFormat() != TargetFormat || |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
306 !std::numeric_limits<float>::is_iec559 || |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
307 sizeof(float) != 4) |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
308 { |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
309 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
310 } |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
311 |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
312 LOG(WARNING) << "Input volume size: " << source.GetWidth() << "x" |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
313 << source.GetHeight() << "x" << source.GetDepth(); |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
314 LOG(WARNING) << "Input pixel format: " << Orthanc::EnumerationToString(source.GetFormat()); |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
315 LOG(WARNING) << "Output image size: " << target.GetWidth() << "x" << target.GetHeight(); |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
316 LOG(WARNING) << "Output pixel format: " << Orthanc::EnumerationToString(target.GetFormat()); |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
317 |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
318 std::auto_ptr<OrthancStone::ParallelSlices> slices(source.GetGeometry(projection)); |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
319 const OrthancStone::Vector pixelSpacing = source.GetVoxelDimensions(projection); |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
320 const unsigned int targetWidth = target.GetWidth(); |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
321 const unsigned int targetHeight = target.GetHeight(); |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
322 |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
323 Orthanc::Image accumulator(Orthanc::PixelFormat_Float32, targetWidth, targetHeight, false); |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
324 Orthanc::Image counter(Orthanc::PixelFormat_Grayscale16, targetWidth, targetHeight, false); |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
325 Orthanc::ImageProcessing::Set(accumulator, 0); |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
326 Orthanc::ImageProcessing::Set(counter, 0); |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
327 |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
328 typedef SubpixelReader<SourceFormat, ImageInterpolation_Nearest> SourceReader; |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
329 |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
330 for (size_t z = 0; z < slices->GetSliceCount(); z++) |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
331 { |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
332 LOG(INFO) << "Applying raytracer on slice: " << z << "/" << slices->GetSliceCount(); |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
333 |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
334 const OrthancStone::CoordinateSystem3D& slice = slices->GetSlice(z); |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
335 OrthancStone::ImageBuffer3D::SliceReader sliceReader(source, projection, z); |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
336 |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
337 SourceReader pixelReader(sliceReader.GetAccessor()); |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
338 |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
339 for (unsigned int y = 0; y < targetHeight; y++) |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
340 { |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
341 float *qacc = reinterpret_cast<float*>(accumulator.GetRow(y)); |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
342 uint16_t *qcount = reinterpret_cast<uint16_t*>(counter.GetRow(y)); |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
343 |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
344 for (unsigned int x = 0; x < targetWidth; x++) |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
345 { |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
346 // Backproject the ray originating from the center of the target pixel |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
347 OrthancStone::Vector direction = camera.GetRayDirection(static_cast<double>(x + 0.5), |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
348 static_cast<double>(y + 0.5)); |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
349 |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
350 // Compute the 3D intersection of the ray with the slice plane |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
351 OrthancStone::Vector p; |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
352 if (slice.IntersectLine(p, camera.GetCenter(), direction)) |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
353 { |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
354 // Compute the 2D coordinates of the intersections, in slice coordinates |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
355 double ix, iy; |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
356 slice.ProjectPoint(ix, iy, p); |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
357 |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
358 ix /= pixelSpacing[0]; |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
359 iy /= pixelSpacing[1]; |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
360 |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
361 // Read and accumulate the value of the pixel |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
362 float pixel; |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
363 if (pixelReader.GetFloatValue(pixel, ix, iy)) |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
364 { |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
365 if (MIP) |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
366 { |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
367 // MIP rendering |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
368 if (*qcount == 0) |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
369 { |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
370 (*qacc) = pixel; |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
371 (*qcount) = 1; |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
372 } |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
373 else if (pixel > *qacc) |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
374 { |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
375 (*qacc) = pixel; |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
376 } |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
377 } |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
378 else |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
379 { |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
380 // Mean intensity |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
381 (*qacc) += pixel; |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
382 (*qcount) ++; |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
383 } |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
384 } |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
385 } |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
386 |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
387 qacc++; |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
388 qcount++; |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
389 } |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
390 } |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
391 } |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
392 |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
393 |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
394 typedef Orthanc::PixelTraits<TargetFormat> TargetTraits; |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
395 |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
396 // "Flatten" the accumulator image to create the target image |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
397 for (unsigned int y = 0; y < targetHeight; y++) |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
398 { |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
399 const float *qacc = reinterpret_cast<const float*>(accumulator.GetConstRow(y)); |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
400 const uint16_t *qcount = reinterpret_cast<const uint16_t*>(counter.GetConstRow(y)); |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
401 typename TargetTraits::PixelType *p = reinterpret_cast<typename TargetTraits::PixelType*>(target.GetRow(y)); |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
402 |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
403 for (unsigned int x = 0; x < targetWidth; x++) |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
404 { |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
405 if (*qcount == 0) |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
406 { |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
407 TargetTraits::SetZero(*p); |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
408 } |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
409 else |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
410 { |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
411 TargetTraits::FloatToPixel(*p, *qacc / static_cast<float>(*qcount)); |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
412 } |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
413 |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
414 p++; |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
415 qacc++; |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
416 qcount++; |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
417 } |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
418 } |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
419 } |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
420 |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
421 |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
422 Orthanc::ImageAccessor* |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
423 FiniteProjectiveCamera::ApplyRaytracer(const ImageBuffer3D& source, |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
424 Orthanc::PixelFormat targetFormat, |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
425 unsigned int targetWidth, |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
426 unsigned int targetHeight, |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
427 bool mip) const |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
428 { |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
429 // TODO - We consider the axial projection of the volume, but we |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
430 // should choose the projection that is the "most perpendicular" |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
431 // to the line joining the camera center and the principal point |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
432 const VolumeProjection projection = VolumeProjection_Axial; |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
433 |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
434 std::auto_ptr<Orthanc::ImageAccessor> target |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
435 (new Orthanc::Image(targetFormat, targetWidth, targetHeight, false)); |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
436 |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
437 if (targetFormat == Orthanc::PixelFormat_Grayscale16 && |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
438 source.GetFormat() == Orthanc::PixelFormat_Grayscale16 && mip) |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
439 { |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
440 ApplyRaytracerInternal<Orthanc::PixelFormat_Grayscale16, |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
441 Orthanc::PixelFormat_Grayscale16, true> |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
442 (*target, *this, source, projection); |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
443 } |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
444 else if (targetFormat == Orthanc::PixelFormat_Grayscale16 && |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
445 source.GetFormat() == Orthanc::PixelFormat_Grayscale16 && !mip) |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
446 { |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
447 ApplyRaytracerInternal<Orthanc::PixelFormat_Grayscale16, |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
448 Orthanc::PixelFormat_Grayscale16, false> |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
449 (*target, *this, source, projection); |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
450 } |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
451 else |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
452 { |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
453 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
454 } |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
455 |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
456 return target.release(); |
371da7fe2c0e
FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
189
diff
changeset
|
457 } |
161 | 458 } |