Mercurial > hg > orthanc-stone
annotate Framework/Toolbox/GeometryToolbox.h @ 1024:9889c819db01
fix
author | Alain Mazy <alain@mazy.be> |
---|---|
date | Fri, 04 Oct 2019 17:47:13 +0200 |
parents | 53cc787bd7bc |
children | 2d8ab34c8c91 |
rev | line source |
---|---|
0 | 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 |
0 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
47 | 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. | |
0 | 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 | |
47 | 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 | |
0 | 18 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 **/ | |
20 | |
21 | |
22 #pragma once | |
23 | |
158
a053ca7fa5c6
LinearAlgebra toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
157
diff
changeset
|
24 #include "LinearAlgebra.h" |
0 | 25 |
26 namespace OrthancStone | |
27 { | |
28 namespace GeometryToolbox | |
29 { | |
30 void ProjectPointOntoPlane(Vector& result, | |
31 const Vector& point, | |
32 const Vector& planeNormal, | |
33 const Vector& planeOrigin); | |
34 | |
1013
53cc787bd7bc
- Added an optimized ProjectPoint2 to CoordinateSystem3D. It has *not* replaced
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
35 /* |
53cc787bd7bc
- Added an optimized ProjectPoint2 to CoordinateSystem3D. It has *not* replaced
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
36 Alternated faster implementation (untested yet) |
53cc787bd7bc
- Added an optimized ProjectPoint2 to CoordinateSystem3D. It has *not* replaced
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
37 */ |
53cc787bd7bc
- Added an optimized ProjectPoint2 to CoordinateSystem3D. It has *not* replaced
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
38 void ProjectPointOntoPlane2(double& resultX, |
53cc787bd7bc
- Added an optimized ProjectPoint2 to CoordinateSystem3D. It has *not* replaced
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
39 double& resultY, |
53cc787bd7bc
- Added an optimized ProjectPoint2 to CoordinateSystem3D. It has *not* replaced
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
40 double& resultZ, |
53cc787bd7bc
- Added an optimized ProjectPoint2 to CoordinateSystem3D. It has *not* replaced
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
41 const Vector& point, |
53cc787bd7bc
- Added an optimized ProjectPoint2 to CoordinateSystem3D. It has *not* replaced
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
42 const Vector& planeNormal, |
53cc787bd7bc
- Added an optimized ProjectPoint2 to CoordinateSystem3D. It has *not* replaced
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
43 const Vector& planeOrigin); |
53cc787bd7bc
- Added an optimized ProjectPoint2 to CoordinateSystem3D. It has *not* replaced
Benjamin Golinvaux <bgo@osimis.io>
parents:
439
diff
changeset
|
44 |
0 | 45 bool IsParallel(const Vector& u, |
46 const Vector& v); | |
47 | |
48 bool IsParallelOrOpposite(bool& isOpposite, | |
49 const Vector& u, | |
50 const Vector& v); | |
51 | |
52 bool IntersectTwoPlanes(Vector& p, | |
53 Vector& direction, | |
54 const Vector& origin1, | |
55 const Vector& normal1, | |
56 const Vector& origin2, | |
57 const Vector& normal2); | |
58 | |
59 bool ClipLineToRectangle(double& x1, // Coordinates of the clipped line (out) | |
60 double& y1, | |
61 double& x2, | |
62 double& y2, | |
63 const double ax, // Two points defining the line (in) | |
64 const double ay, | |
65 const double bx, | |
66 const double by, | |
67 const double& xmin, // Coordinates of the rectangle (in) | |
68 const double& ymin, | |
69 const double& xmax, | |
70 const double& ymax); | |
32 | 71 |
72 void GetPixelSpacing(double& spacingX, | |
73 double& spacingY, | |
118
a4d0b6c82b29
using Orthanc::DicomMap instead of OrthancPlugins::DicomDatasetReader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
113
diff
changeset
|
74 const Orthanc::DicomMap& dicom); |
122
e3433dabfb8d
refactoring DicomStructureSet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
118
diff
changeset
|
75 |
e3433dabfb8d
refactoring DicomStructureSet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
118
diff
changeset
|
76 inline double ProjectAlongNormal(const Vector& point, |
e3433dabfb8d
refactoring DicomStructureSet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
118
diff
changeset
|
77 const Vector& normal) |
e3433dabfb8d
refactoring DicomStructureSet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
118
diff
changeset
|
78 { |
e3433dabfb8d
refactoring DicomStructureSet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
118
diff
changeset
|
79 return boost::numeric::ublas::inner_prod(point, normal); |
e3433dabfb8d
refactoring DicomStructureSet
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
118
diff
changeset
|
80 } |
140
2115530d3703
OrientedBoundingBox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
81 |
2115530d3703
OrientedBoundingBox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
82 Matrix CreateRotationMatrixAlongX(double a); |
2115530d3703
OrientedBoundingBox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
83 |
2115530d3703
OrientedBoundingBox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
84 Matrix CreateRotationMatrixAlongY(double a); |
2115530d3703
OrientedBoundingBox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
85 |
144
9b83f30fc1c0
bilinear and trilinear interpolation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
86 Matrix CreateRotationMatrixAlongZ(double a); |
9b83f30fc1c0
bilinear and trilinear interpolation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
87 |
165
8d50e6be565d
LinearAlgebra toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
88 Matrix CreateTranslationMatrix(double dx, |
8d50e6be565d
LinearAlgebra toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
89 double dy, |
8d50e6be565d
LinearAlgebra toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
90 double dz); |
8d50e6be565d
LinearAlgebra toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
91 |
8d50e6be565d
LinearAlgebra toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
92 Matrix CreateScalingMatrix(double sx, |
8d50e6be565d
LinearAlgebra toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
93 double sy, |
8d50e6be565d
LinearAlgebra toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
94 double sz); |
8d50e6be565d
LinearAlgebra toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
158
diff
changeset
|
95 |
151
c5044bbfc303
CoordinateSystem3D::IntersectSegment()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
150
diff
changeset
|
96 bool IntersectPlaneAndSegment(Vector& p, |
c5044bbfc303
CoordinateSystem3D::IntersectSegment()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
150
diff
changeset
|
97 const Vector& normal, |
c5044bbfc303
CoordinateSystem3D::IntersectSegment()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
150
diff
changeset
|
98 double d, |
c5044bbfc303
CoordinateSystem3D::IntersectSegment()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
150
diff
changeset
|
99 const Vector& edgeFrom, |
c5044bbfc303
CoordinateSystem3D::IntersectSegment()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
150
diff
changeset
|
100 const Vector& edgeTo); |
c5044bbfc303
CoordinateSystem3D::IntersectSegment()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
150
diff
changeset
|
101 |
157
2309e8d86efe
IntersectPlaneAndLine
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
156
diff
changeset
|
102 bool IntersectPlaneAndLine(Vector& p, |
2309e8d86efe
IntersectPlaneAndLine
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
156
diff
changeset
|
103 const Vector& normal, |
2309e8d86efe
IntersectPlaneAndLine
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
156
diff
changeset
|
104 double d, |
2309e8d86efe
IntersectPlaneAndLine
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
156
diff
changeset
|
105 const Vector& origin, |
2309e8d86efe
IntersectPlaneAndLine
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
156
diff
changeset
|
106 const Vector& direction); |
2309e8d86efe
IntersectPlaneAndLine
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
156
diff
changeset
|
107 |
189
964118e7e6de
unit test for AlignVectorsWithRotation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
177
diff
changeset
|
108 void AlignVectorsWithRotation(Matrix& r, |
964118e7e6de
unit test for AlignVectorsWithRotation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
177
diff
changeset
|
109 const Vector& a, |
964118e7e6de
unit test for AlignVectorsWithRotation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
177
diff
changeset
|
110 const Vector& b); |
964118e7e6de
unit test for AlignVectorsWithRotation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
177
diff
changeset
|
111 |
177 | 112 inline float ComputeBilinearInterpolationUnitSquare(float x, |
113 float y, | |
114 float f00, // source(0, 0) | |
115 float f01, // source(1, 0) | |
116 float f10, // source(0, 1) | |
117 float f11); // source(1, 1) | |
144
9b83f30fc1c0
bilinear and trilinear interpolation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
118 |
177 | 119 inline float ComputeTrilinearInterpolationUnitSquare(float x, |
120 float y, | |
121 float z, | |
122 float f000, // source(0, 0, 0) | |
123 float f001, // source(1, 0, 0) | |
124 float f010, // source(0, 1, 0) | |
125 float f011, // source(1, 1, 0) | |
126 float f100, // source(0, 0, 1) | |
127 float f101, // source(1, 0, 1) | |
128 float f110, // source(0, 1, 1) | |
129 float f111); // source(1, 1, 1) | |
0 | 130 }; |
131 } | |
144
9b83f30fc1c0
bilinear and trilinear interpolation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
132 |
9b83f30fc1c0
bilinear and trilinear interpolation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
133 |
177 | 134 float OrthancStone::GeometryToolbox::ComputeBilinearInterpolationUnitSquare(float x, |
135 float y, | |
136 float f00, | |
137 float f01, | |
138 float f10, | |
139 float f11) | |
144
9b83f30fc1c0
bilinear and trilinear interpolation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
140 { |
177 | 141 // This function only works within the unit square |
142 assert(x >= 0 && y >= 0 && x <= 1 && y <= 1); | |
144
9b83f30fc1c0
bilinear and trilinear interpolation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
143 |
9b83f30fc1c0
bilinear and trilinear interpolation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
144 // https://en.wikipedia.org/wiki/Bilinear_interpolation#Unit_square |
175
15d92d93738b
fix interpolation with negative coordinates
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
172
diff
changeset
|
145 return (f00 * (1.0f - x) * (1.0f - y) + |
15d92d93738b
fix interpolation with negative coordinates
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
172
diff
changeset
|
146 f01 * x * (1.0f - y) + |
15d92d93738b
fix interpolation with negative coordinates
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
172
diff
changeset
|
147 f10 * (1.0f - x) * y + |
15d92d93738b
fix interpolation with negative coordinates
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
172
diff
changeset
|
148 f11 * x * y); |
144
9b83f30fc1c0
bilinear and trilinear interpolation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
149 } |
9b83f30fc1c0
bilinear and trilinear interpolation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
150 |
9b83f30fc1c0
bilinear and trilinear interpolation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
151 |
177 | 152 float OrthancStone::GeometryToolbox::ComputeTrilinearInterpolationUnitSquare(float x, |
153 float y, | |
154 float z, | |
155 float f000, | |
156 float f001, | |
157 float f010, | |
158 float f011, | |
159 float f100, | |
160 float f101, | |
161 float f110, | |
162 float f111) | |
144
9b83f30fc1c0
bilinear and trilinear interpolation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
163 { |
9b83f30fc1c0
bilinear and trilinear interpolation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
164 // "In practice, a trilinear interpolation is identical to two |
9b83f30fc1c0
bilinear and trilinear interpolation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
165 // bilinear interpolation combined with a linear interpolation" |
9b83f30fc1c0
bilinear and trilinear interpolation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
166 // https://en.wikipedia.org/wiki/Trilinear_interpolation#Method |
177 | 167 float a = ComputeBilinearInterpolationUnitSquare(x, y, f000, f001, f010, f011); |
168 float b = ComputeBilinearInterpolationUnitSquare(x, y, f100, f101, f110, f111); | |
144
9b83f30fc1c0
bilinear and trilinear interpolation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
169 |
177 | 170 return (1.0f - z) * a + z * b; |
144
9b83f30fc1c0
bilinear and trilinear interpolation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
171 } |