Mercurial > hg > orthanc-stone
annotate OrthancStone/Sources/Scene2D/ScenePoint2D.cpp @ 1994:2f242d231a22
cppcheck
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 31 Oct 2022 22:21:39 +0100 |
parents | 7053b8a0aaec |
children | 07964689cb0b |
rev | line source |
---|---|
1804 | 1 /** |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
1871
7053b8a0aaec
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1870
diff
changeset
|
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium |
7053b8a0aaec
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1870
diff
changeset
|
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
1804 | 7 * |
8 * This program is free software: you can redistribute it and/or | |
9 * modify it under the terms of the GNU Lesser General Public License | |
10 * as published by the Free Software Foundation, either version 3 of | |
11 * the License, or (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, but | |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 * Lesser General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU Lesser General Public | |
19 * License along with this program. If not, see | |
20 * <http://www.gnu.org/licenses/>. | |
21 **/ | |
22 | |
23 | |
24 #include "ScenePoint2D.h" | |
25 | |
26 | |
27 namespace OrthancStone | |
28 { | |
29 ScenePoint2D ScenePoint2D::Apply(const AffineTransform2D& t) const | |
30 { | |
31 double x = x_; | |
32 double y = y_; | |
33 t.Apply(x, y); | |
34 return ScenePoint2D(x, y); | |
35 } | |
36 | |
37 | |
38 const ScenePoint2D ScenePoint2D::operator-(const ScenePoint2D& a) const | |
39 { | |
40 ScenePoint2D v; | |
41 v.x_ = x_ - a.x_; | |
42 v.y_ = y_ - a.y_; | |
43 return v; | |
44 } | |
45 | |
46 | |
47 const ScenePoint2D ScenePoint2D::operator+(const ScenePoint2D& a) const | |
48 { | |
49 ScenePoint2D v; | |
50 v.x_ = x_ + a.x_; | |
51 v.y_ = y_ + a.y_; | |
52 return v; | |
53 } | |
54 | |
55 | |
56 const ScenePoint2D ScenePoint2D::operator*(double a) const | |
57 { | |
58 ScenePoint2D v; | |
59 v.x_ = x_ * a; | |
60 v.y_ = y_ * a; | |
61 return v; | |
62 } | |
63 | |
64 | |
65 const ScenePoint2D ScenePoint2D::operator/(double a) const | |
66 { | |
67 ScenePoint2D v; | |
68 v.x_ = x_ / a; | |
69 v.y_ = y_ / a; | |
70 return v; | |
71 } | |
72 | |
73 | |
74 void ScenePoint2D::MidPoint(ScenePoint2D& result, | |
75 const ScenePoint2D& a, | |
76 const ScenePoint2D& b) | |
77 { | |
78 result.x_ = 0.5 * (a.x_ + b.x_); | |
79 result.y_ = 0.5 * (a.y_ + b.y_); | |
80 } | |
81 | |
82 | |
83 double ScenePoint2D::Dot(const ScenePoint2D& a, const ScenePoint2D& b) | |
84 { | |
85 return a.x_ * b.x_ + a.y_ * b.y_; | |
86 } | |
87 | |
88 | |
89 double ScenePoint2D::SquaredMagnitude(const ScenePoint2D& v) | |
90 { | |
91 return v.x_ * v.x_ + v.y_ * v.y_; | |
92 } | |
93 | |
94 | |
95 double ScenePoint2D::Magnitude(const ScenePoint2D& v) | |
96 { | |
97 double squaredMagnitude = SquaredMagnitude(v); | |
98 | |
99 if (LinearAlgebra::IsCloseToZero(squaredMagnitude)) | |
100 { | |
101 return 0.0; | |
102 } | |
103 else | |
104 { | |
105 return sqrt(squaredMagnitude); | |
106 } | |
107 } | |
108 | |
109 | |
110 double ScenePoint2D::SquaredDistancePtPt(const ScenePoint2D& a, const ScenePoint2D& b) | |
111 { | |
112 ScenePoint2D n = b - a; | |
113 return Dot(n, n); | |
114 } | |
115 | |
116 | |
117 double ScenePoint2D::DistancePtPt(const ScenePoint2D& a, const ScenePoint2D& b) | |
118 { | |
119 double squaredDist = SquaredDistancePtPt(a, b); | |
120 return sqrt(squaredDist); | |
121 } | |
122 | |
123 | |
124 double ScenePoint2D::SquaredDistancePtSegment(const ScenePoint2D& a, const ScenePoint2D& b, const ScenePoint2D& p) | |
125 { | |
126 // Rewritten from https://www.randygaul.net/2014/07/23/distance-point-to-line-segment/ | |
127 | |
128 ScenePoint2D n = b - a; | |
129 ScenePoint2D pa = a - p; | |
130 | |
131 double c = Dot(n, pa); | |
132 | |
133 // Closest point is a | |
134 if (c > 0.0) | |
135 { | |
136 return Dot(pa, pa); | |
137 } | |
138 | |
139 ScenePoint2D bp = p - b; | |
140 | |
141 // Closest point is b | |
142 if (Dot(n, bp) > 0.0) | |
143 { | |
144 return Dot(bp, bp); | |
145 } | |
146 | |
147 // if segment length is very short, we approximate distance to the | |
148 // distance with a | |
149 double nq = Dot(n, n); | |
150 if (LinearAlgebra::IsCloseToZero(nq)) | |
151 { | |
152 // segment is very small: approximate distance from point to segment | |
153 // with distance from p to a | |
154 return Dot(pa, pa); | |
155 } | |
156 else | |
157 { | |
158 // Closest point is between a and b | |
159 ScenePoint2D e = pa - n * (c / nq); | |
160 return Dot(e, e); | |
161 } | |
162 } | |
163 } |