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