Mercurial > hg > orthanc-stone
comparison Framework/Toolbox/AffineTransform2D.cpp @ 409:99c9b3238008
AffineTransform2D
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 12 Nov 2018 15:38:11 +0100 |
parents | |
children | b70e9be013e4 |
comparison
equal
deleted
inserted
replaced
408:6834c236b36d | 409:99c9b3238008 |
---|---|
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 "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 | |
93 AffineTransform2D AffineTransform2D::Invert(const AffineTransform2D& a) | |
94 { | |
95 AffineTransform2D t; | |
96 LinearAlgebra::InvertMatrix(t.matrix_, a.matrix_); | |
97 return t; | |
98 } | |
99 | |
100 | |
101 AffineTransform2D AffineTransform2D::Combine(const AffineTransform2D& a, | |
102 const AffineTransform2D& b) | |
103 { | |
104 return AffineTransform2D(LinearAlgebra::Product(a.GetHomogeneousMatrix(), | |
105 b.GetHomogeneousMatrix())); | |
106 } | |
107 | |
108 | |
109 AffineTransform2D AffineTransform2D::Combine(const AffineTransform2D& a, | |
110 const AffineTransform2D& b, | |
111 const AffineTransform2D& c) | |
112 { | |
113 return AffineTransform2D(LinearAlgebra::Product(a.GetHomogeneousMatrix(), | |
114 b.GetHomogeneousMatrix(), | |
115 c.GetHomogeneousMatrix())); | |
116 } | |
117 | |
118 | |
119 AffineTransform2D AffineTransform2D::Combine(const AffineTransform2D& a, | |
120 const AffineTransform2D& b, | |
121 const AffineTransform2D& c, | |
122 const AffineTransform2D& d) | |
123 { | |
124 return AffineTransform2D(LinearAlgebra::Product(a.GetHomogeneousMatrix(), | |
125 b.GetHomogeneousMatrix(), | |
126 c.GetHomogeneousMatrix(), | |
127 d.GetHomogeneousMatrix())); | |
128 } | |
129 | |
130 | |
131 AffineTransform2D AffineTransform2D::CreateOffset(double dx, | |
132 double dy) | |
133 { | |
134 AffineTransform2D t; | |
135 t.matrix_(0, 2) = dx; | |
136 t.matrix_(1, 2) = dy; | |
137 | |
138 return t; | |
139 } | |
140 | |
141 | |
142 AffineTransform2D AffineTransform2D::CreateScaling(double sx, | |
143 double sy) | |
144 { | |
145 AffineTransform2D t; | |
146 t.matrix_(0, 0) = sx; | |
147 t.matrix_(1, 1) = sy; | |
148 | |
149 return t; | |
150 } | |
151 | |
152 | |
153 AffineTransform2D AffineTransform2D::CreateRotation(double angle) | |
154 { | |
155 double cosine = cos(angle); | |
156 double sine = sin(angle); | |
157 | |
158 AffineTransform2D t; | |
159 t.matrix_(0, 0) = cosine; | |
160 t.matrix_(0, 1) = -sine; | |
161 t.matrix_(1, 0) = sine; | |
162 t.matrix_(1, 1) = cosine; | |
163 | |
164 return t; | |
165 } | |
166 } |