Mercurial > hg > orthanc-stone
annotate Framework/Toolbox/AffineTransform2D.cpp @ 984:35ed6812d639 toa2019090602
Build fix
author | Benjamin Golinvaux <bgo@osimis.io> |
---|---|
date | Fri, 06 Sep 2019 11:46:51 +0200 |
parents | 919226caca82 |
children | 7912de3a15e0 |
rev | line source |
---|---|
409 | 1 /** |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
439 | 5 * Copyright (C) 2017-2019 Osimis S.A., Belgium |
409 | 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 "AffineTransform2D.h" | |
23 | |
24 #include "ImageGeometry.h" | |
25 | |
26 #include <Core/Logging.h> | |
27 #include <Core/OrthancException.h> | |
28 | |
29 namespace OrthancStone | |
30 { | |
31 AffineTransform2D::AffineTransform2D() : | |
32 matrix_(LinearAlgebra::IdentityMatrix(3)) | |
33 { | |
34 } | |
35 | |
36 | |
37 AffineTransform2D::AffineTransform2D(const Matrix& m) | |
38 { | |
39 if (m.size1() != 3 || | |
40 m.size2() != 3) | |
41 { | |
42 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageSize); | |
43 } | |
44 | |
45 if (!LinearAlgebra::IsCloseToZero(m(2, 0)) || | |
46 !LinearAlgebra::IsCloseToZero(m(2, 1)) || | |
47 LinearAlgebra::IsCloseToZero(m(2, 2))) | |
48 { | |
49 LOG(ERROR) << "Cannot setup an AffineTransform2D with perspective effects"; | |
50 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
51 } | |
52 | |
53 matrix_ = m / m(2, 2); | |
54 } | |
55 | |
56 | |
57 void AffineTransform2D::Apply(double& x /* inout */, | |
58 double& y /* inout */) const | |
59 { | |
60 Vector p; | |
61 LinearAlgebra::AssignVector(p, x, y, 1); | |
62 | |
63 Vector q = LinearAlgebra::Product(matrix_, p); | |
64 | |
65 if (!LinearAlgebra::IsNear(q[2], 1.0)) | |
66 { | |
67 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
68 } | |
69 else | |
70 { | |
71 x = q[0]; | |
72 y = q[1]; | |
73 } | |
74 } | |
75 | |
76 | |
77 void AffineTransform2D::Apply(Orthanc::ImageAccessor& target, | |
78 const Orthanc::ImageAccessor& source, | |
79 ImageInterpolation interpolation, | |
80 bool clear) const | |
81 { | |
82 assert(LinearAlgebra::IsNear(matrix_(2, 0), 0) && | |
83 LinearAlgebra::IsNear(matrix_(2, 1), 0) && | |
84 LinearAlgebra::IsNear(matrix_(2, 2), 1)); | |
85 | |
86 ApplyAffineTransform(target, source, | |
87 matrix_(0, 0), matrix_(0, 1), matrix_(0, 2), | |
88 matrix_(1, 0), matrix_(1, 1), matrix_(1, 2), | |
89 interpolation, clear); | |
90 } | |
91 | |
92 | |
574
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
93 void AffineTransform2D::ConvertToOpenGLMatrix(float target[16], |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
94 unsigned int canvasWidth, |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
95 unsigned int canvasHeight) const |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
96 { |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
97 const AffineTransform2D t = AffineTransform2D::Combine( |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
98 CreateOpenGLClipspace(canvasWidth, canvasHeight), *this); |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
99 |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
100 const Matrix source = t.GetHomogeneousMatrix(); |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
101 |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
102 if (source.size1() != 3 || |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
103 source.size2() != 3) |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
104 { |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
105 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
106 } |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
107 |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
108 // "z" must be in the [-1,1] range, otherwise the texture does not show up |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
109 float z = 0; |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
110 |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
111 // Embed the 3x3 affine transform of the 2D plane into a 4x4 |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
112 // matrix (3D) for OpenGL. The matrix must be transposed. |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
113 |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
114 target[0] = static_cast<float>(source(0, 0)); |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
115 target[1] = static_cast<float>(source(1, 0)); |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
116 target[2] = 0; |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
117 target[3] = static_cast<float>(source(2, 0)); |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
118 target[4] = static_cast<float>(source(0, 1)); |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
119 target[5] = static_cast<float>(source(1, 1)); |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
120 target[6] = 0; |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
121 target[7] = static_cast<float>(source(2, 1)); |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
122 target[8] = 0; |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
123 target[9] = 0; |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
124 target[10] = -1; |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
125 target[11] = 0; |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
126 target[12] = static_cast<float>(source(0, 2)); |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
127 target[13] = static_cast<float>(source(1, 2)); |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
128 target[14] = -z; |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
129 target[15] = static_cast<float>(source(2, 2)); |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
130 } |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
131 |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
132 |
575
919226caca82
AffineTransform2D::ComputeZoom()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
574
diff
changeset
|
133 double AffineTransform2D::ComputeZoom() const |
919226caca82
AffineTransform2D::ComputeZoom()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
574
diff
changeset
|
134 { |
919226caca82
AffineTransform2D::ComputeZoom()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
574
diff
changeset
|
135 // Compute the length of the (0,0)-(1,1) diagonal (whose |
919226caca82
AffineTransform2D::ComputeZoom()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
574
diff
changeset
|
136 // length is sqrt(2)) instead of the (0,0)-(1,0) unit segment, |
919226caca82
AffineTransform2D::ComputeZoom()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
574
diff
changeset
|
137 // in order to cope with possible anisotropic zooming |
919226caca82
AffineTransform2D::ComputeZoom()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
574
diff
changeset
|
138 |
919226caca82
AffineTransform2D::ComputeZoom()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
574
diff
changeset
|
139 double x1 = 0; |
919226caca82
AffineTransform2D::ComputeZoom()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
574
diff
changeset
|
140 double y1 = 0; |
919226caca82
AffineTransform2D::ComputeZoom()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
574
diff
changeset
|
141 Apply(x1, y1); |
919226caca82
AffineTransform2D::ComputeZoom()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
574
diff
changeset
|
142 |
919226caca82
AffineTransform2D::ComputeZoom()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
574
diff
changeset
|
143 double x2 = 1; |
919226caca82
AffineTransform2D::ComputeZoom()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
574
diff
changeset
|
144 double y2 = 1; |
919226caca82
AffineTransform2D::ComputeZoom()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
574
diff
changeset
|
145 Apply(x2, y2); |
919226caca82
AffineTransform2D::ComputeZoom()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
574
diff
changeset
|
146 |
919226caca82
AffineTransform2D::ComputeZoom()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
574
diff
changeset
|
147 double dx = x2 - x1; |
919226caca82
AffineTransform2D::ComputeZoom()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
574
diff
changeset
|
148 double dy = y2 - y1; |
919226caca82
AffineTransform2D::ComputeZoom()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
574
diff
changeset
|
149 |
919226caca82
AffineTransform2D::ComputeZoom()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
574
diff
changeset
|
150 double zoom = sqrt(dx * dx + dy * dy) / sqrt(2.0); |
919226caca82
AffineTransform2D::ComputeZoom()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
574
diff
changeset
|
151 |
919226caca82
AffineTransform2D::ComputeZoom()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
574
diff
changeset
|
152 if (LinearAlgebra::IsCloseToZero(zoom)) |
919226caca82
AffineTransform2D::ComputeZoom()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
574
diff
changeset
|
153 { |
919226caca82
AffineTransform2D::ComputeZoom()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
574
diff
changeset
|
154 return 1; // Default value if transform is ill-conditioned |
919226caca82
AffineTransform2D::ComputeZoom()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
574
diff
changeset
|
155 } |
919226caca82
AffineTransform2D::ComputeZoom()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
574
diff
changeset
|
156 else |
919226caca82
AffineTransform2D::ComputeZoom()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
574
diff
changeset
|
157 { |
919226caca82
AffineTransform2D::ComputeZoom()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
574
diff
changeset
|
158 return zoom; |
919226caca82
AffineTransform2D::ComputeZoom()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
574
diff
changeset
|
159 } |
919226caca82
AffineTransform2D::ComputeZoom()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
574
diff
changeset
|
160 } |
919226caca82
AffineTransform2D::ComputeZoom()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
574
diff
changeset
|
161 |
919226caca82
AffineTransform2D::ComputeZoom()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
574
diff
changeset
|
162 |
409 | 163 AffineTransform2D AffineTransform2D::Invert(const AffineTransform2D& a) |
164 { | |
165 AffineTransform2D t; | |
166 LinearAlgebra::InvertMatrix(t.matrix_, a.matrix_); | |
167 return t; | |
168 } | |
169 | |
170 | |
171 AffineTransform2D AffineTransform2D::Combine(const AffineTransform2D& a, | |
172 const AffineTransform2D& b) | |
173 { | |
174 return AffineTransform2D(LinearAlgebra::Product(a.GetHomogeneousMatrix(), | |
175 b.GetHomogeneousMatrix())); | |
176 } | |
177 | |
178 | |
179 AffineTransform2D AffineTransform2D::Combine(const AffineTransform2D& a, | |
180 const AffineTransform2D& b, | |
181 const AffineTransform2D& c) | |
182 { | |
183 return AffineTransform2D(LinearAlgebra::Product(a.GetHomogeneousMatrix(), | |
184 b.GetHomogeneousMatrix(), | |
185 c.GetHomogeneousMatrix())); | |
186 } | |
187 | |
188 | |
189 AffineTransform2D AffineTransform2D::Combine(const AffineTransform2D& a, | |
190 const AffineTransform2D& b, | |
191 const AffineTransform2D& c, | |
192 const AffineTransform2D& d) | |
193 { | |
194 return AffineTransform2D(LinearAlgebra::Product(a.GetHomogeneousMatrix(), | |
195 b.GetHomogeneousMatrix(), | |
196 c.GetHomogeneousMatrix(), | |
197 d.GetHomogeneousMatrix())); | |
198 } | |
199 | |
200 | |
201 AffineTransform2D AffineTransform2D::CreateOffset(double dx, | |
202 double dy) | |
203 { | |
204 AffineTransform2D t; | |
205 t.matrix_(0, 2) = dx; | |
206 t.matrix_(1, 2) = dy; | |
207 | |
208 return t; | |
209 } | |
210 | |
211 | |
212 AffineTransform2D AffineTransform2D::CreateScaling(double sx, | |
213 double sy) | |
214 { | |
215 AffineTransform2D t; | |
216 t.matrix_(0, 0) = sx; | |
217 t.matrix_(1, 1) = sy; | |
218 | |
219 return t; | |
220 } | |
221 | |
222 | |
223 AffineTransform2D AffineTransform2D::CreateRotation(double angle) | |
224 { | |
225 double cosine = cos(angle); | |
226 double sine = sin(angle); | |
227 | |
228 AffineTransform2D t; | |
229 t.matrix_(0, 0) = cosine; | |
230 t.matrix_(0, 1) = -sine; | |
231 t.matrix_(1, 0) = sine; | |
232 t.matrix_(1, 1) = cosine; | |
233 | |
234 return t; | |
235 } | |
574
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
236 |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
237 |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
238 AffineTransform2D AffineTransform2D::CreateOpenGLClipspace(unsigned int canvasWidth, |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
239 unsigned int canvasHeight) |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
240 { |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
241 AffineTransform2D t; |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
242 t.matrix_(0, 0) = 2.0 / static_cast<double>(canvasWidth); |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
243 t.matrix_(0, 2) = -1.0; |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
244 t.matrix_(1, 1) = -2.0 / static_cast<double>(canvasHeight); |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
245 t.matrix_(1, 2) = 1.0; |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
246 |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
247 return t; |
911297a277c4
AffineTransform2D::ConvertToOpenGLMatrix()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
248 } |
409 | 249 } |