g2o
Loading...
Searching...
No Matches
simutils.cpp
Go to the documentation of this file.
1// g2o - General Graph Optimization
2// Copyright (C) 2011 R. Kuemmerle, G. Grisetti, H. Strasdat, W. Burgard
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
9// * Redistributions of source code must retain the above copyright notice,
10// this list of conditions and the following disclaimer.
11// * Redistributions in binary form must reproduce the above copyright
12// notice, this list of conditions and the following disclaimer in the
13// documentation and/or other materials provided with the distribution.
14//
15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27#include "simutils.h"
28
29namespace g2o {
30// -1: outside
31// 0: p1Clipped
32// 1: p2clipped
33// 2: inside
34// 3: all clipped
35using namespace Eigen;
36
37int clipSegmentCircle(Eigen::Vector2d& p1, Eigen::Vector2d& p2, double r) {
38 double r2 = r * r;
39 Eigen::Vector2d pBase = p1;
40 Eigen::Vector2d dp = p2 - p1;
41 double length = dp.norm();
42 dp.normalize();
43 double p = 2 * dp.dot(p1);
44 double q = p1.squaredNorm() - r2;
45 double disc = p * p - 4 * q;
46
47 if (disc <= 0) { // no intersection or single point intersection
48 return -1;
49 }
50 disc = sqrt(disc);
51
52 double t1 = .5 * (-p - disc);
53 double t2 = .5 * (-p + disc);
54
55 if (t1 > length || t2 < 0) return -1; // no intersection
56 bool clip1 = false;
57 bool clip2 = false;
58 if (t1 > 0) {
59 p1 = pBase + dp * t1;
60 clip1 = true;
61 }
62 if (t2 < length) {
63 p2 = pBase + dp * t1;
64 clip2 = true;
65 }
66 if (clip1)
67 if (clip2)
68 return 3;
69 else
70 return 0;
71 else if (clip2)
72 return 1;
73 return 2;
74}
75
76// -1: outside
77// 0: p1Clipped
78// 1: p2clipped
79// 2: inside
80
81int clipSegmentLine(Eigen::Vector2d& p1, Eigen::Vector2d& p2, double a,
82 double b, double c) {
83 bool p1inside = true;
84 bool p2inside = true;
85 if (a * p1.x() + b * p1.y() + c < 0) {
86 p1inside = false;
87 }
88 if (a * p2.x() + b * p2.y() + c < 0) {
89 p2inside = false;
90 }
91 if (p1inside && p2inside) return 2;
92 if (!p1inside && !p2inside) return -1;
93
94 Eigen::Vector2d dp = p2 - p1;
95 double den = a * dp.x() + b * dp.y();
96 if (den == 0) return -1;
97 double num = c + a * p1.x() + b * p1.y();
98 double t = -num / den;
99 if (p1inside) {
100 p2 = p1 + dp * t;
101 return 1;
102 }
103 p1 = p1 + dp * t;
104 return 0;
105}
106
107int clipSegmentFov(Eigen::Vector2d& p1, Eigen::Vector2d& p2, double min,
108 double max) {
109 bool clip1 = false, clip2 = false;
110 // normal to the first line
111 double amin = sin(min), bmin = -cos(min);
112 int minClip = clipSegmentLine(p1, p2, amin, bmin, 0);
113 switch (minClip) {
114 case -1:
115 return -1;
116 case 0:
117 clip1 = true;
118 break;
119 case 1:
120 clip2 = true;
121 break;
122 default:;
123 }
124 // normal to the second line
125 double amax = -sin(max), bmax = cos(max);
126 int maxClip = clipSegmentLine(p1, p2, amax, bmax, 0);
127 switch (maxClip) {
128 case -1:
129 return -1;
130 case 0:
131 clip1 = true;
132 break;
133 case 1:
134 clip2 = true;
135 break;
136 default:;
137 }
138 if (clip1)
139 if (clip2)
140 return 3;
141 else
142 return 0;
143 else if (clip2)
144 return 1;
145 return 2;
146}
147
148Eigen::Vector2d computeLineParameters(const Eigen::Vector2d& p1,
149 const Eigen::Vector2d& p2) {
150 Eigen::Vector2d lp;
151 Eigen::Vector2d dp = p2 - p1;
152 lp[0] = atan2(-dp.x(), dp.y());
153 Eigen::Vector2d n(cos(lp[0]), sin(lp[0]));
154 lp[1] = n.dot(p1 + p2) * .5;
155 return lp;
156}
157} // namespace g2o
Definition jet.h:938
Eigen::Vector2d computeLineParameters(const Eigen::Vector2d &p1, const Eigen::Vector2d &p2)
Definition simutils.cpp:148
int clipSegmentCircle(Eigen::Vector2d &p1, Eigen::Vector2d &p2, double r)
Definition simutils.cpp:37
int clipSegmentFov(Eigen::Vector2d &p1, Eigen::Vector2d &p2, double min, double max)
Definition simutils.cpp:107
int clipSegmentLine(Eigen::Vector2d &p1, Eigen::Vector2d &p2, double a, double b, double c)
Definition simutils.cpp:81