Mercurial > hg > orthanc-stone
comparison Framework/Scene2D/ScenePoint2D.h @ 865:a29c13497557
Added operators to ScenePoint2D + highlight support on MouseOver for measuring tools
author | Benjamin Golinvaux <bgo@osimis.io> |
---|---|
date | Tue, 25 Jun 2019 15:24:13 +0200 |
parents | 5c551f078c18 |
children | c71ef52602a0 |
comparison
equal
deleted
inserted
replaced
864:ae3eccd0f545 | 865:a29c13497557 |
---|---|
11 * | 11 * |
12 * This program is distributed in the hope that it will be useful, but | 12 * This program is distributed in the hope that it will be useful, but |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | 13 * WITHOUT ANY WARRANTY; without even the implied warranty of |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 * Affero General Public License for more details. | 15 * Affero General Public License for more details. |
16 * | 16 * |
17 * You should have received a copy of the GNU Affero General Public License | 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/>. | 18 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 **/ | 19 **/ |
20 | 20 |
21 | 21 |
22 #pragma once | 22 #pragma once |
23 | 23 |
24 #include "../Toolbox/AffineTransform2D.h" | 24 #include "../Toolbox/AffineTransform2D.h" |
25 | 25 #include "../Toolbox/LinearAlgebra.h" |
26 | 26 |
27 namespace OrthancStone | 27 namespace OrthancStone |
28 { | 28 { |
29 class ScenePoint2D | 29 class ScenePoint2D |
30 { | 30 { |
38 y_(0) | 38 y_(0) |
39 { | 39 { |
40 } | 40 } |
41 | 41 |
42 ScenePoint2D(double x, | 42 ScenePoint2D(double x, |
43 double y) : | 43 double y) : |
44 x_(x), | 44 x_(x), |
45 y_(y) | 45 y_(y) |
46 { | 46 { |
47 } | 47 } |
48 | 48 |
61 double x = x_; | 61 double x = x_; |
62 double y = y_; | 62 double y = y_; |
63 t.Apply(x, y); | 63 t.Apply(x, y); |
64 return ScenePoint2D(x, y); | 64 return ScenePoint2D(x, y); |
65 } | 65 } |
66 | |
67 const ScenePoint2D operator-(const ScenePoint2D& a) const | |
68 { | |
69 ScenePoint2D v; | |
70 v.x_ = x_ - a.x_; | |
71 v.y_ = y_ - a.y_; | |
72 | |
73 return v; | |
74 } | |
75 | |
76 const ScenePoint2D operator*(double a) const | |
77 { | |
78 ScenePoint2D v; | |
79 v.x_ = x_ * a; | |
80 v.y_ = y_ * a; | |
81 | |
82 return v; | |
83 } | |
84 | |
85 static double Dot(const ScenePoint2D& a, const ScenePoint2D& b) | |
86 { | |
87 return a.x_ * b.x_ + a.y_ * b.y_; | |
88 } | |
89 | |
90 static double SquaredDistancePtPt(const ScenePoint2D& a, const ScenePoint2D& b) | |
91 { | |
92 ScenePoint2D n = b - a; | |
93 return Dot(n, n); | |
94 } | |
95 | |
96 /** | |
97 Distance from point p to [a,b] segment | |
98 | |
99 Rewritten from https://www.randygaul.net/2014/07/23/distance-point-to-line-segment/ | |
100 */ | |
101 static double SquaredDistancePtSegment(const ScenePoint2D& a, const ScenePoint2D& b, const ScenePoint2D& p) | |
102 { | |
103 ScenePoint2D n = b - a; | |
104 ScenePoint2D pa = a - p; | |
105 | |
106 double c = Dot(n, pa); | |
107 | |
108 // Closest point is a | |
109 if (c > 0.0) | |
110 return Dot(pa, pa); | |
111 | |
112 ScenePoint2D bp = p - b; | |
113 | |
114 // Closest point is b | |
115 if (Dot(n, bp) > 0.0) | |
116 return Dot(bp, bp); | |
117 | |
118 // if segment length is very short, we approximate distance to the | |
119 // distance with a | |
120 double nq = Dot(n, n); | |
121 if (LinearAlgebra::IsCloseToZero(nq)) | |
122 { | |
123 // segment is very small: approximate distance from point to segment | |
124 // with distance from p to a | |
125 return Dot(pa, pa); | |
126 } | |
127 else | |
128 { | |
129 // Closest point is between a and b | |
130 ScenePoint2D e = pa - n * (c / nq); | |
131 return Dot(e, e); | |
132 } | |
133 } | |
66 }; | 134 }; |
67 } | 135 } |
136 |