comparison OrthancStone/Sources/Toolbox/AffineTransform2D.cpp @ 1512:244ad1e4e76a

reorganization of folders
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 07 Jul 2020 16:21:02 +0200
parents Framework/Toolbox/AffineTransform2D.cpp@30deba7bc8e2
children 6d14ed6163b1
comparison
equal deleted inserted replaced
1511:9dfeee74c1e6 1512:244ad1e4e76a
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-2020 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 "AffineTransform2D.h"
23
24 #include "ImageGeometry.h"
25
26 #include <Logging.h>
27 #include <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
93 void AffineTransform2D::ConvertToOpenGLMatrix(float target[16],
94 unsigned int canvasWidth,
95 unsigned int canvasHeight) const
96 {
97 const AffineTransform2D t = AffineTransform2D::Combine(
98 CreateOpenGLClipspace(canvasWidth, canvasHeight), *this);
99
100 const Matrix source = t.GetHomogeneousMatrix();
101
102 if (source.size1() != 3 ||
103 source.size2() != 3)
104 {
105 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
106 }
107
108 // "z" must be in the [-1,1] range, otherwise the texture does not show up
109 float z = 0;
110
111 // Embed the 3x3 affine transform of the 2D plane into a 4x4
112 // matrix (3D) for OpenGL. The matrix must be transposed.
113
114 target[0] = static_cast<float>(source(0, 0));
115 target[1] = static_cast<float>(source(1, 0));
116 target[2] = 0;
117 target[3] = static_cast<float>(source(2, 0));
118 target[4] = static_cast<float>(source(0, 1));
119 target[5] = static_cast<float>(source(1, 1));
120 target[6] = 0;
121 target[7] = static_cast<float>(source(2, 1));
122 target[8] = 0;
123 target[9] = 0;
124 target[10] = -1;
125 target[11] = 0;
126 target[12] = static_cast<float>(source(0, 2));
127 target[13] = static_cast<float>(source(1, 2));
128 target[14] = -z;
129 target[15] = static_cast<float>(source(2, 2));
130 }
131
132
133 double AffineTransform2D::ComputeZoom() const
134 {
135 // Compute the length of the (0,0)-(1,1) diagonal (whose
136 // length is sqrt(2)) instead of the (0,0)-(1,0) unit segment,
137 // in order to cope with possible anisotropic zooming
138
139 double x1 = 0;
140 double y1 = 0;
141 Apply(x1, y1);
142
143 double x2 = 1;
144 double y2 = 1;
145 Apply(x2, y2);
146
147 double dx = x2 - x1;
148 double dy = y2 - y1;
149
150 double zoom = sqrt(dx * dx + dy * dy) / sqrt(2.0);
151
152 if (LinearAlgebra::IsCloseToZero(zoom))
153 {
154 return 1; // Default value if transform is ill-conditioned
155 }
156 else
157 {
158 return zoom;
159 }
160 }
161
162
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 AffineTransform2D AffineTransform2D::Combine(const AffineTransform2D& a,
201 const AffineTransform2D& b,
202 const AffineTransform2D& c,
203 const AffineTransform2D& d,
204 const AffineTransform2D& e)
205 {
206 return AffineTransform2D(LinearAlgebra::Product(a.GetHomogeneousMatrix(),
207 b.GetHomogeneousMatrix(),
208 c.GetHomogeneousMatrix(),
209 d.GetHomogeneousMatrix(),
210 e.GetHomogeneousMatrix()));
211 }
212
213 AffineTransform2D AffineTransform2D::CreateOffset(double dx,
214 double dy)
215 {
216 AffineTransform2D t;
217 t.matrix_(0, 2) = dx;
218 t.matrix_(1, 2) = dy;
219
220 return t;
221 }
222
223
224 AffineTransform2D AffineTransform2D::CreateScaling(double sx,
225 double sy)
226 {
227 AffineTransform2D t;
228 t.matrix_(0, 0) = sx;
229 t.matrix_(1, 1) = sy;
230
231 return t;
232 }
233
234
235 AffineTransform2D AffineTransform2D::CreateRotation(double angle)
236 {
237 double cosine = cos(angle);
238 double sine = sin(angle);
239
240 AffineTransform2D t;
241 t.matrix_(0, 0) = cosine;
242 t.matrix_(0, 1) = -sine;
243 t.matrix_(1, 0) = sine;
244 t.matrix_(1, 1) = cosine;
245
246 return t;
247 }
248
249 AffineTransform2D AffineTransform2D::CreateRotation(double angle, // CW rotation
250 double cx, // rotation center
251 double cy) // rotation center
252 {
253 return Combine(
254 CreateOffset(cx, cy),
255 CreateRotation(angle),
256 CreateOffset(-cx, -cy)
257 );
258 }
259
260 AffineTransform2D AffineTransform2D::CreateOpenGLClipspace(unsigned int canvasWidth,
261 unsigned int canvasHeight)
262 {
263 AffineTransform2D t;
264 t.matrix_(0, 0) = 2.0 / static_cast<double>(canvasWidth);
265 t.matrix_(0, 2) = -1.0;
266 t.matrix_(1, 1) = -2.0 / static_cast<double>(canvasHeight);
267 t.matrix_(1, 2) = 1.0;
268
269 return t;
270 }
271 }