annotate OrthancStone/Sources/Toolbox/FiniteProjectiveCamera.cpp @ 1677:51bab5188a13

start multiple preset windowings
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 23 Nov 2020 19:24:18 +0100
parents 52b8b96cb55f
children 9ac2a65d4172
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
161
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Stone of Orthanc
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
1270
2d8ab34c8c91 upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 811
diff changeset
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
161
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
1598
8563ea5d8ae4 relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1596
diff changeset
8 * modify it under the terms of the GNU Lesser General Public License
161
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * as published by the Free Software Foundation, either version 3 of
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * the License, or (at your option) any later version.
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
1598
8563ea5d8ae4 relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1596
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
8563ea5d8ae4 relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1596
diff changeset
15 * Lesser General Public License for more details.
1596
4fb8fdf03314 removed annoying whitespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1512
diff changeset
16 *
1598
8563ea5d8ae4 relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1596
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
8563ea5d8ae4 relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1596
diff changeset
18 * License along with this program. If not, see
8563ea5d8ae4 relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1596
diff changeset
19 * <http://www.gnu.org/licenses/>.
161
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 **/
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 #include "FiniteProjectiveCamera.h"
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24
188
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
25 #include "GeometryToolbox.h"
192
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
26 #include "SubpixelReader.h"
188
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
27
1455
30deba7bc8e2 simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1298
diff changeset
28 #include <Logging.h>
30deba7bc8e2 simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1298
diff changeset
29 #include <OrthancException.h>
30deba7bc8e2 simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1298
diff changeset
30 #include <Images/Image.h>
30deba7bc8e2 simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1298
diff changeset
31 #include <Images/ImageProcessing.h>
161
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 namespace OrthancStone
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 {
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 void FiniteProjectiveCamera::ComputeMInverse()
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 {
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 using namespace boost::numeric::ublas;
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 // inv(M) = inv(K * R) = inv(R) * inv(K) = R' * inv(K). This
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 // matrix is always invertible, by definition of finite
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 // projective cameras (page 157).
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 Matrix kinv;
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 LinearAlgebra::InvertUpperTriangularMatrix(kinv, k_);
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 minv_ = prod(trans(r_), kinv);
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 }
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 void FiniteProjectiveCamera::Setup(const Matrix& k,
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 const Matrix& r,
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 const Vector& c)
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 {
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 if (k.size1() != 3 ||
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 k.size2() != 3 ||
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 !LinearAlgebra::IsCloseToZero(k(1, 0)) ||
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 !LinearAlgebra::IsCloseToZero(k(2, 0)) ||
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 !LinearAlgebra::IsCloseToZero(k(2, 1)))
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 {
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 LOG(ERROR) << "Invalid intrinsic parameters";
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 }
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 if (r.size1() != 3 ||
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 r.size2() != 3)
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 {
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 LOG(ERROR) << "Invalid size for a 3D rotation matrix";
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 }
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 if (!LinearAlgebra::IsRotationMatrix(r, 100.0 * std::numeric_limits<float>::epsilon()))
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 {
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 LOG(ERROR) << "Invalid rotation matrix";
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 }
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 if (c.size() != 3)
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 {
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 LOG(ERROR) << "Invalid camera center";
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 }
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 k_ = k;
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 r_ = r;
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 c_ = c;
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 ComputeMInverse();
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86
188
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
87 Matrix tmp = LinearAlgebra::IdentityMatrix(3);
161
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 tmp.resize(3, 4);
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 tmp(0, 3) = -c[0];
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 tmp(1, 3) = -c[1];
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 tmp(2, 3) = -c[2];
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92
188
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
93 p_ = LinearAlgebra::Product(k, r, tmp);
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
94
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
95 assert(p_.size1() == 3 &&
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
96 p_.size2() == 4);
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
97
161
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 }
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 void FiniteProjectiveCamera::Setup(const Matrix& p)
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 {
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 if (p.size1() != 3 ||
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 p.size2() != 4)
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 {
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 LOG(ERROR) << "Invalid camera matrix";
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 }
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 p_ = p;
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 // M is the left 3x3 submatrix of "P"
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 Matrix m = p;
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 m.resize(3, 3);
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 // p4 is the last column of "P"
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 Vector p4(3);
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 p4[0] = p(0, 3);
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 p4[1] = p(1, 3);
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 p4[2] = p(2, 3);
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 // The RQ decomposition is explained on page 157
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 LinearAlgebra::RQDecomposition3x3(k_, r_, m);
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 ComputeMInverse();
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125
188
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
126 c_ = LinearAlgebra::Product(-minv_, p4);
161
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 }
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 FiniteProjectiveCamera::FiniteProjectiveCamera(const double k[9],
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 const double r[9],
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 const double c[3])
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 {
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 Matrix kk, rr;
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 Vector cc;
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 LinearAlgebra::FillMatrix(kk, 3, 3, k);
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 LinearAlgebra::FillMatrix(rr, 3, 3, r);
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 LinearAlgebra::FillVector(cc, 3, c);
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 Setup(kk, rr, cc);
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 }
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 FiniteProjectiveCamera::FiniteProjectiveCamera(const double p[12])
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 {
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 Matrix pp;
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 LinearAlgebra::FillMatrix(pp, 3, 4, p);
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 Setup(pp);
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150 }
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 Vector FiniteProjectiveCamera::GetRayDirection(double x,
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 double y) const
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 {
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 // This derives from Equation (6.14) on page 162, taking "mu =
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 // 1" and noticing that "-inv(M)*p4" corresponds to the camera
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 // center in finite projective cameras
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 // The (x,y) coordinates on the imaged plane, as an homogeneous vector
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 Vector xx(3);
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162 xx[0] = x;
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163 xx[1] = y;
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164 xx[2] = 1.0;
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 return boost::numeric::ublas::prod(minv_, xx);
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167 }
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171 static Vector SetupApply(const Vector& v,
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172 bool infinityAllowed)
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173 {
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 if (v.size() == 3)
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175 {
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 // Vector "v" in non-homogeneous coordinates, add the homogeneous component
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 Vector vv;
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 LinearAlgebra::AssignVector(vv, v[0], v[1], v[2], 1.0);
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 return vv;
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180 }
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181 else if (v.size() == 4)
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 {
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183 // Vector "v" is already in homogeneous coordinates
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 if (!infinityAllowed &&
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186 LinearAlgebra::IsCloseToZero(v[3]))
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187 {
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188 LOG(ERROR) << "Cannot apply a finite projective camera to a "
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189 << "point at infinity with this method";
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191 }
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193 return v;
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194 }
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195 else
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196 {
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197 LOG(ERROR) << "The input vector must represent a point in 3D";
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199 }
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200 }
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203 void FiniteProjectiveCamera::ApplyFinite(double& x,
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204 double& y,
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
205 const Vector& v) const
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
206 {
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207 Vector p = boost::numeric::ublas::prod(p_, SetupApply(v, false));
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
209 if (LinearAlgebra::IsCloseToZero(p[2]))
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210 {
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211 // Point at infinity: Should not happen with a finite input point
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
212 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
213 }
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
214 else
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
215 {
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216 x = p[0] / p[2];
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217 y = p[1] / p[2];
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
218 }
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
219 }
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
220
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
221
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
222 Vector FiniteProjectiveCamera::ApplyGeneral(const Vector& v) const
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
223 {
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
224 return boost::numeric::ublas::prod(p_, SetupApply(v, true));
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
225 }
188
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
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
228 static Vector AddHomogeneousCoordinate(const Vector& p)
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
229 {
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
230 assert(p.size() == 3);
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
231 return LinearAlgebra::CreateVector(p[0], p[1], p[2], 1);
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
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
235 FiniteProjectiveCamera::FiniteProjectiveCamera(const Vector& camera,
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
236 const Vector& principalPoint,
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
237 double angle,
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
238 unsigned int imageWidth,
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
239 unsigned int imageHeight,
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
240 double pixelSpacingX,
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
241 double pixelSpacingY)
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
242 {
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
243 if (camera.size() != 3 ||
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
244 principalPoint.size() != 3 ||
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
245 LinearAlgebra::IsCloseToZero(pixelSpacingX) ||
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
246 LinearAlgebra::IsCloseToZero(pixelSpacingY))
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
247 {
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
248 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
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
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
251 const double focal = boost::numeric::ublas::norm_2(camera - principalPoint);
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
252
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
253 if (LinearAlgebra::IsCloseToZero(focal))
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
254 {
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
255 LOG(ERROR) << "Camera lies on the image plane";
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
256 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
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
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
259 Matrix a;
189
964118e7e6de unit test for AlignVectorsWithRotation
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 188
diff changeset
260 GeometryToolbox::AlignVectorsWithRotation(a, camera - principalPoint,
964118e7e6de unit test for AlignVectorsWithRotation
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 188
diff changeset
261 LinearAlgebra::CreateVector(0, 0, -1));
188
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
262
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
263 Matrix r = LinearAlgebra::Product(GeometryToolbox::CreateRotationMatrixAlongZ(angle), a);
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
264
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
265 Matrix k = LinearAlgebra::ZeroMatrix(3, 3);
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
266 k(0,0) = focal / pixelSpacingX;
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
267 k(1,1) = focal / pixelSpacingY;
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
268 k(0,2) = static_cast<double>(imageWidth) / 2.0;
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
269 k(1,2) = static_cast<double>(imageHeight) / 2.0;
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
270 k(2,2) = 1;
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
271
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
272 Setup(k, r, camera);
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 {
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
275 // Sanity checks
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
276 Vector v1 = LinearAlgebra::Product(p_, AddHomogeneousCoordinate(camera));
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
277 Vector v2 = LinearAlgebra::Product(p_, AddHomogeneousCoordinate(principalPoint));
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
278
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
279 if (!LinearAlgebra::IsCloseToZero(v1[2]) || // Camera is mapped to singularity
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
280 LinearAlgebra::IsCloseToZero(v2[2]))
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
281 {
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
282 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
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
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
285 // 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
286 v2 /= v2[2];
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
287
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
288 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
289 !LinearAlgebra::IsNear(v2[1], static_cast<double>(imageHeight) / 2.0))
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
290 {
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
291 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
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 }
45b03b04a777 calibration of FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 161
diff changeset
294 }
192
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
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
297 template <Orthanc::PixelFormat TargetFormat,
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
298 Orthanc::PixelFormat SourceFormat,
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
299 bool MIP>
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
300 static void ApplyRaytracerInternal(Orthanc::ImageAccessor& target,
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
301 const FiniteProjectiveCamera& camera,
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
302 const ImageBuffer3D& source,
735
c3bbb130abc4 removing dependencies in ImageBuffer3D
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 693
diff changeset
303 const VolumeImageGeometry& geometry,
192
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
304 VolumeProjection projection)
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
305 {
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
306 if (source.GetFormat() != SourceFormat ||
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
307 target.GetFormat() != TargetFormat ||
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
308 !std::numeric_limits<float>::is_iec559 ||
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
309 sizeof(float) != 4)
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 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
312 }
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
313
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
314 LOG(WARNING) << "Input volume size: " << source.GetWidth() << "x"
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
315 << source.GetHeight() << "x" << source.GetDepth();
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
316 LOG(WARNING) << "Input pixel format: " << Orthanc::EnumerationToString(source.GetFormat());
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
317 LOG(WARNING) << "Output image size: " << target.GetWidth() << "x" << target.GetHeight();
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
318 LOG(WARNING) << "Output pixel format: " << Orthanc::EnumerationToString(target.GetFormat());
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
319
811
ffec76a5f7eb deprecating ParallelSlices
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 735
diff changeset
320 const unsigned int slicesCount = geometry.GetProjectionDepth(projection);
1640
52b8b96cb55f cleaning namespaces
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1598
diff changeset
321 const Vector pixelSpacing = geometry.GetVoxelDimensions(projection);
192
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
322 const unsigned int targetWidth = target.GetWidth();
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
323 const unsigned int targetHeight = target.GetHeight();
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
324
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
325 Orthanc::Image accumulator(Orthanc::PixelFormat_Float32, targetWidth, targetHeight, false);
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
326 Orthanc::Image counter(Orthanc::PixelFormat_Grayscale16, targetWidth, targetHeight, false);
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
327 Orthanc::ImageProcessing::Set(accumulator, 0);
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
328 Orthanc::ImageProcessing::Set(counter, 0);
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 typedef SubpixelReader<SourceFormat, ImageInterpolation_Nearest> SourceReader;
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
331
811
ffec76a5f7eb deprecating ParallelSlices
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 735
diff changeset
332 for (unsigned int z = 0; z < slicesCount; z++)
192
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
333 {
811
ffec76a5f7eb deprecating ParallelSlices
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 735
diff changeset
334 LOG(INFO) << "Applying raytracer on slice: " << z << "/" << slicesCount;
ffec76a5f7eb deprecating ParallelSlices
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 735
diff changeset
335
1640
52b8b96cb55f cleaning namespaces
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1598
diff changeset
336 CoordinateSystem3D slice = geometry.GetProjectionSlice(projection, z);
52b8b96cb55f cleaning namespaces
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1598
diff changeset
337 ImageBuffer3D::SliceReader sliceReader(source, projection, static_cast<unsigned int>(z));
192
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 SourceReader pixelReader(sliceReader.GetAccessor());
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 for (unsigned int y = 0; y < targetHeight; y++)
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
342 {
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
343 float *qacc = reinterpret_cast<float*>(accumulator.GetRow(y));
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
344 uint16_t *qcount = reinterpret_cast<uint16_t*>(counter.GetRow(y));
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 for (unsigned int x = 0; x < targetWidth; x++)
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
347 {
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
348 // Backproject the ray originating from the center of the target pixel
1640
52b8b96cb55f cleaning namespaces
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1598
diff changeset
349 Vector direction = camera.GetRayDirection(static_cast<double>(x + 0.5),
52b8b96cb55f cleaning namespaces
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1598
diff changeset
350 static_cast<double>(y + 0.5));
192
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
351
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
352 // Compute the 3D intersection of the ray with the slice plane
1640
52b8b96cb55f cleaning namespaces
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1598
diff changeset
353 Vector p;
192
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
354 if (slice.IntersectLine(p, camera.GetCenter(), direction))
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
355 {
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
356 // Compute the 2D coordinates of the intersections, in slice coordinates
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
357 double ix, iy;
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
358 slice.ProjectPoint(ix, iy, p);
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
359
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
360 ix /= pixelSpacing[0];
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
361 iy /= pixelSpacing[1];
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
362
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
363 // Read and accumulate the value of the pixel
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
364 float pixel;
693
9a474e90e832 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 684
diff changeset
365 if (pixelReader.GetFloatValue(
9a474e90e832 Fixed a bunch of truncation warnings in various parts of the library
Benjamin Golinvaux <bgo@osimis.io>
parents: 684
diff changeset
366 pixel, static_cast<float>(ix), static_cast<float>(iy)))
192
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
367 {
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
368 if (MIP)
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 // MIP rendering
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
371 if (*qcount == 0)
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 (*qacc) = pixel;
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
374 (*qcount) = 1;
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
375 }
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
376 else if (pixel > *qacc)
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 (*qacc) = pixel;
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 }
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
381 else
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
382 {
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
383 // Mean intensity
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
384 (*qacc) += pixel;
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
385 (*qcount) ++;
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 }
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
388 }
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 qacc++;
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
391 qcount++;
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 }
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
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
397 typedef Orthanc::PixelTraits<TargetFormat> TargetTraits;
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 // "Flatten" the accumulator image to create the target image
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
400 for (unsigned int y = 0; y < targetHeight; y++)
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
401 {
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
402 const float *qacc = reinterpret_cast<const float*>(accumulator.GetConstRow(y));
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
403 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
404 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
405
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
406 for (unsigned int x = 0; x < targetWidth; x++)
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
407 {
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
408 if (*qcount == 0)
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
409 {
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
410 TargetTraits::SetZero(*p);
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
411 }
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
412 else
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 TargetTraits::FloatToPixel(*p, *qacc / static_cast<float>(*qcount));
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
415 }
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
416
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
417 p++;
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
418 qacc++;
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
419 qcount++;
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 }
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
423
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
424
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
425 Orthanc::ImageAccessor*
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
426 FiniteProjectiveCamera::ApplyRaytracer(const ImageBuffer3D& source,
735
c3bbb130abc4 removing dependencies in ImageBuffer3D
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 693
diff changeset
427 const VolumeImageGeometry& geometry,
192
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
428 Orthanc::PixelFormat targetFormat,
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
429 unsigned int targetWidth,
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
430 unsigned int targetHeight,
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
431 bool mip) const
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
432 {
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
433 // TODO - We consider the axial projection of the volume, but we
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
434 // should choose the projection that is the "most perpendicular"
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
435 // to the line joining the camera center and the principal point
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
436 const VolumeProjection projection = VolumeProjection_Axial;
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
437
1298
8a0a62189f46 replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1270
diff changeset
438 std::unique_ptr<Orthanc::ImageAccessor> target
192
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
439 (new Orthanc::Image(targetFormat, targetWidth, targetHeight, false));
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
440
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
441 if (targetFormat == Orthanc::PixelFormat_Grayscale16 &&
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
442 source.GetFormat() == Orthanc::PixelFormat_Grayscale16 && mip)
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 ApplyRaytracerInternal<Orthanc::PixelFormat_Grayscale16,
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
445 Orthanc::PixelFormat_Grayscale16, true>
735
c3bbb130abc4 removing dependencies in ImageBuffer3D
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 693
diff changeset
446 (*target, *this, source, geometry, projection);
192
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
447 }
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
448 else if (targetFormat == Orthanc::PixelFormat_Grayscale16 &&
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
449 source.GetFormat() == Orthanc::PixelFormat_Grayscale16 && !mip)
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 ApplyRaytracerInternal<Orthanc::PixelFormat_Grayscale16,
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
452 Orthanc::PixelFormat_Grayscale16, false>
735
c3bbb130abc4 removing dependencies in ImageBuffer3D
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 693
diff changeset
453 (*target, *this, source, geometry, projection);
192
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 else
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
456 {
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
457 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented);
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
458 }
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
459
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
460 return target.release();
371da7fe2c0e FiniteProjectiveCamera::ApplyRaytracer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
461 }
161
197a5ddaf68c FiniteProjectiveCamera
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
462 }