annotate Framework/Scene2D/ScenePoint2D.h @ 1510:1005c1cbe4dd

fix
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 07 Jul 2020 09:45:27 +0200
parents 7ec8fea061b9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
584
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Stone of Orthanc
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
1270
2d8ab34c8c91 upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
584
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * modify it under the terms of the GNU Affero General Public License
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * as published by the Free Software Foundation, either version 3 of
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * the License, or (at your option) any later version.
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * Affero General Public License for more details.
865
a29c13497557 Added operators to ScenePoint2D + highlight support on MouseOver for measuring tools
Benjamin Golinvaux <bgo@osimis.io>
parents: 699
diff changeset
16 *
584
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * You should have received a copy of the GNU Affero General Public License
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 **/
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 #pragma once
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 #include "../Toolbox/AffineTransform2D.h"
865
a29c13497557 Added operators to ScenePoint2D + highlight support on MouseOver for measuring tools
Benjamin Golinvaux <bgo@osimis.io>
parents: 699
diff changeset
25 #include "../Toolbox/LinearAlgebra.h"
699
5c551f078c18 Merge from default
Benjamin Golinvaux <bgo@osimis.io>
parents: 698
diff changeset
26
584
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 namespace OrthancStone
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 {
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 class ScenePoint2D
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 {
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 private:
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 double x_;
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 double y_;
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 public:
596
b716763571ad IPointerTracker
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 584
diff changeset
36 ScenePoint2D() :
b716763571ad IPointerTracker
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 584
diff changeset
37 x_(0),
b716763571ad IPointerTracker
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 584
diff changeset
38 y_(0)
b716763571ad IPointerTracker
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 584
diff changeset
39 {
b716763571ad IPointerTracker
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 584
diff changeset
40 }
b716763571ad IPointerTracker
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 584
diff changeset
41
584
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 ScenePoint2D(double x,
865
a29c13497557 Added operators to ScenePoint2D + highlight support on MouseOver for measuring tools
Benjamin Golinvaux <bgo@osimis.io>
parents: 699
diff changeset
43 double y) :
584
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 x_(x),
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 y_(y)
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 {
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 }
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 double GetX() const
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 {
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 return x_;
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 }
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 double GetY() const
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 {
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 return y_;
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 }
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 ScenePoint2D Apply(const AffineTransform2D& t) const
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 {
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 double x = x_;
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 double y = y_;
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 t.Apply(x, y);
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 return ScenePoint2D(x, y);
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 }
865
a29c13497557 Added operators to ScenePoint2D + highlight support on MouseOver for measuring tools
Benjamin Golinvaux <bgo@osimis.io>
parents: 699
diff changeset
66
1160
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
67 const ScenePoint2D operator-(const ScenePoint2D& a) const
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
68 {
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
69 ScenePoint2D v;
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
70 v.x_ = x_ - a.x_;
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
71 v.y_ = y_ - a.y_;
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
72
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
73 return v;
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
74 }
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
75
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
76 const ScenePoint2D operator+(const ScenePoint2D& a) const
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
77 {
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
78 ScenePoint2D v;
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
79 v.x_ = x_ + a.x_;
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
80 v.y_ = y_ + a.y_;
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
81
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
82 return v;
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
83 }
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
84
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
85 const ScenePoint2D operator*(double a) const
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
86 {
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
87 ScenePoint2D v;
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
88 v.x_ = x_ * a;
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
89 v.y_ = y_ * a;
865
a29c13497557 Added operators to ScenePoint2D + highlight support on MouseOver for measuring tools
Benjamin Golinvaux <bgo@osimis.io>
parents: 699
diff changeset
90
1160
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
91 return v;
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
92 }
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
93
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
94 const ScenePoint2D operator/(double a) const
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
95 {
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
96 ScenePoint2D v;
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
97 v.x_ = x_ / a;
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
98 v.y_ = y_ / a;
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
99
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
100 return v;
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
101 }
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
102
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
103 static void MidPoint(ScenePoint2D& result, const ScenePoint2D& a, const ScenePoint2D& b)
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
104 {
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
105 result.x_ = 0.5 * (a.x_ + b.x_);
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
106 result.y_ = 0.5 * (a.y_ + b.y_);
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
107 }
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
108
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
109 static double Dot(const ScenePoint2D& a, const ScenePoint2D& b)
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
110 {
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
111 return a.x_ * b.x_ + a.y_ * b.y_;
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
112 }
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
113
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
114 static double SquaredMagnitude(const ScenePoint2D& v)
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
115 {
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
116 return v.x_ * v.x_ + v.y_ * v.y_;
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
117 }
901
240359ab1651 Added Dot + magnitude + couple helpers in ScenePoint2D + Added ability to target several sets of 5-layer text packs stored in LayerHolder
Benjamin Golinvaux <bgo@osimis.io>
parents: 866
diff changeset
118
1160
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
119 static double Magnitude(const ScenePoint2D& v)
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
120 {
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
121 double squaredMagnitude = SquaredMagnitude(v);
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
122 if (LinearAlgebra::IsCloseToZero(squaredMagnitude))
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
123 return 0.0;
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
124 return sqrt(squaredMagnitude);
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
125 }
901
240359ab1651 Added Dot + magnitude + couple helpers in ScenePoint2D + Added ability to target several sets of 5-layer text packs stored in LayerHolder
Benjamin Golinvaux <bgo@osimis.io>
parents: 866
diff changeset
126
1160
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
127 static double SquaredDistancePtPt(const ScenePoint2D& a, const ScenePoint2D& b)
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
128 {
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
129 ScenePoint2D n = b - a;
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
130 return Dot(n, n);
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
131 }
865
a29c13497557 Added operators to ScenePoint2D + highlight support on MouseOver for measuring tools
Benjamin Golinvaux <bgo@osimis.io>
parents: 699
diff changeset
132
1160
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
133 static double DistancePtPt(const ScenePoint2D& a, const ScenePoint2D& b)
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
134 {
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
135 double squaredDist = SquaredDistancePtPt(a, b);
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
136 return sqrt(squaredDist);
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
137 }
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
138
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
139 /**
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
140 Distance from point p to [a,b] segment
901
240359ab1651 Added Dot + magnitude + couple helpers in ScenePoint2D + Added ability to target several sets of 5-layer text packs stored in LayerHolder
Benjamin Golinvaux <bgo@osimis.io>
parents: 866
diff changeset
141
1160
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
142 Rewritten from https://www.randygaul.net/2014/07/23/distance-point-to-line-segment/
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
143 */
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
144 static double SquaredDistancePtSegment(const ScenePoint2D& a, const ScenePoint2D& b, const ScenePoint2D& p)
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
145 {
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
146 ScenePoint2D n = b - a;
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
147 ScenePoint2D pa = a - p;
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
148
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
149 double c = Dot(n, pa);
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
150
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
151 // Closest point is a
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
152 if (c > 0.0)
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
153 return Dot(pa, pa);
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
154
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
155 ScenePoint2D bp = p - b;
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
156
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
157 // Closest point is b
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
158 if (Dot(n, bp) > 0.0)
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
159 return Dot(bp, bp);
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
160
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
161 // if segment length is very short, we approximate distance to the
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
162 // distance with a
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
163 double nq = Dot(n, n);
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
164 if (LinearAlgebra::IsCloseToZero(nq))
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
165 {
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
166 // segment is very small: approximate distance from point to segment
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
167 // with distance from p to a
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
168 return Dot(pa, pa);
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
169 }
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
170 else
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
171 {
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
172 // Closest point is between a and b
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
173 ScenePoint2D e = pa - n * (c / nq);
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
174 return Dot(e, e);
59906485896f fix EOL in ScenePoint2D.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 901
diff changeset
175 }
865
a29c13497557 Added operators to ScenePoint2D + highlight support on MouseOver for measuring tools
Benjamin Golinvaux <bgo@osimis.io>
parents: 699
diff changeset
176 }
584
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 };
434ceeb0bcab layers: InfoPanel, Polyline, Texture
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 }
865
a29c13497557 Added operators to ScenePoint2D + highlight support on MouseOver for measuring tools
Benjamin Golinvaux <bgo@osimis.io>
parents: 699
diff changeset
179