Geom.cpp
Go to the documentation of this file.
1 
7 #include "Geom.h"
8 
9 
11 {
12  x = 0.0;
13  y = 0.0;
14 }
15 
16 
17 Point::Point(float x, float y)
18 {
19  this->x = x;
20  this->y = y;
21 }
22 
23 
24 float Point::X() const
25 {
26  return x;
27 }
28 
29 
30 float Point::Y() const
31 {
32  return y;
33 }
34 
35 
36 Point::Point(const Point &obj)
37 {
38  this->x = obj.x;
39  this->y = obj.y;
40 }
41 
42 
43 std::pair<float, float> Point::GetPair()
44 {
45  return {x, y};
46 }
47 
48 std::string Point::ToString() const
49 {
50  return "x: " + std::to_string(x) + "\ty: "
51  + std::to_string(y) + "\n";
52 
53 }
54 
55 bool Vertex::IsConvex() const
56 {
57  return convex;
58 }
59 
60 
61 void Vertex::SetConvex(bool cvx)
62 {
63  convex = cvx;
64 }
65 
66 using namespace Utils;
67 
69 {
70  auto v = L2 - L1;
71  auto w = P - L1;
72  float c1, c2;
73  if (( c1 = w.ScalarProduct(v)) <= 0.0f)
74  {
75  return (P - L1).SquaredEuclideanNorm();
76  }
77  if ((c2 = v.ScalarProduct(v)) <= c1 )
78  {
79  return (P - L2).SquaredEuclideanNorm();
80  }
81 
82  float b = c1 / c2;
83  Point Pb = L1 + v * b;
84  return (P - Pb).SquaredEuclideanNorm();
85 }
86 
87 
88 bool Utils::linearProgram1(const std::vector<Line> &lines, unsigned long curr, float radius, const Vector &optVelocity,
89  bool directionOpt, Vector &result)
90 {
91  float dotProduct = lines[curr].liesOn.ScalarProduct(lines[curr].dir);
92  float discriminant = dotProduct * dotProduct + radius * radius - lines[curr].liesOn.SquaredEuclideanNorm();
93 
94  if(discriminant < 0.0f)
95  {
96  // Максимальная скорость не позволяет удовлетворить это условие
97  return false;
98  }
99 
100  float sqrtDiscriminant = std::sqrt(discriminant);
101  float tLeft = -dotProduct - sqrtDiscriminant;
102  float tRight = -dotProduct + sqrtDiscriminant;
103 
104  for(int i = 0; i < curr; ++i)
105  {
106 
107  const float denominator = lines[curr].dir.Det(lines[i].dir);
108  const float numerator = lines[i].dir.Det(lines[curr].liesOn - lines[i].liesOn);
109 
110  if(std::fabs(denominator) <= CN_EPS)
111  {
112  // Текущая и сравниваемая линии параллельны
113  if(numerator < 0.0f)
114  {
115  return false;
116  }
117  else
118  {
119  continue;
120  }
121  }
122 
123  const float t = numerator / denominator;
124 
125  if(denominator >= 0.0f)
126  {
127  /* Line i bounds line lineNo on the right. */
128  tRight = std::min(tRight, t);
129  }
130  else
131  {
132  /* Line i bounds line lineNo on the left. */
133  tLeft = std::max(tLeft, t);
134  }
135 
136  if(tLeft > tRight)
137  {
138  return false;
139  }
140  }
141 
142  if(directionOpt)
143  {
144  /* Optimize direction. */
145  if(optVelocity.ScalarProduct(lines[curr].dir) > 0.0f)
146  {
147  /* Take right extreme. */
148  result = lines[curr].liesOn + lines[curr].dir * tRight;
149  }
150  else
151  {
152  /* Take left extreme. */
153  result = lines[curr].liesOn + lines[curr].dir * tLeft;
154  }
155  }
156  else
157  {
158  /* Optimize closest point. */
159  const float t = lines[curr].dir.ScalarProduct(optVelocity - lines[curr].liesOn);
160 
161  if(t < tLeft)
162  {
163  result = lines[curr].liesOn + lines[curr].dir * tLeft;
164  }
165  else if(t > tRight)
166  {
167  result = lines[curr].liesOn + lines[curr].dir * tRight;
168  }
169  else
170  {
171  result = lines[curr].liesOn + lines[curr].dir * t;
172  }
173  }
174 
175  return true;
176 }
177 
178 
179 unsigned long int Utils::linearProgram2(const std::vector<Line> &lines, float radius, const Vector &optVelocity, bool directionOpt,
180  Vector &result)
181 {
182  if(directionOpt)
183  {
184  result = optVelocity * radius;
185  }
186  else if(optVelocity.SquaredEuclideanNorm() > radius * radius)
187  {
188  result = (optVelocity / optVelocity.EuclideanNorm()) * radius;
189  }
190  else
191  {
192  result = optVelocity;
193  }
194 
195  for(unsigned long int i = 0; i < lines.size(); ++i)
196  {
197 
198  if(lines[i].dir.Det(lines[i].liesOn - result) > 0.0f)
199  {
200  const Vector tempResult = result;
201 
202  if(!linearProgram1(lines, i, radius, optVelocity, directionOpt, result))
203  {
204  result = tempResult;
205  return i;
206  }
207  }
208  }
209 
210  return lines.size();
211 }
212 
213 
214 void Utils::linearProgram3(const std::vector<Line> &lines, size_t numObstLines, size_t beginLine, float radius, Vector &result)
215 {
216  float distance = 0.0f;
217 
218  for(size_t i = beginLine; i < lines.size(); ++i)
219  {
220  if(lines[i].dir.Det(lines[i].liesOn - result) > distance)
221  {
222  /* Result does not satisfy constraint of line i. */
223  std::vector<Line> projLines(lines.begin(), lines.begin() + static_cast<ptrdiff_t>(numObstLines));
224 
225  for(size_t j = numObstLines; j < i; ++j)
226  {
227  Line line;
228 
229  float determinant = lines[i].dir.Det(lines[j].dir);
230 
231  if(std::fabs(determinant) <= CN_EPS)
232  {
233  /* Line i and line j are parallel. */
234  if(lines[i].dir.ScalarProduct(lines[j].dir) > 0.0f)
235  {
236  /* Line i and line j point in the same direction. */
237  continue;
238  }
239  else
240  {
241  /* Line i and line j point in opposite direction. */
242  line.liesOn = (lines[i].liesOn + lines[j].liesOn) * 0.5f;
243  }
244  }
245  else
246  {
247  line.liesOn = lines[i].liesOn + lines[i].dir * (lines[j].dir.Det(lines[i].liesOn - lines[j].liesOn) / determinant);
248  }
249 
250 
251  line.dir = (lines[j].dir - lines[i].dir);
252  line.dir = line.dir / line.dir.EuclideanNorm();
253  projLines.push_back(line);
254  }
255 
256  const Vector tempResult = result;
257 
258  if(linearProgram2(projLines, radius, Vector(-lines[i].dir.Y(), lines[i].dir.X()), true, result) < projLines.size())
259  {
260  result = tempResult;
261  }
262 
263  distance = lines[i].dir.Det(lines[i].liesOn - result);
264  }
265  }
266 }
267 
268 
269 
float SquaredEuclideanNorm() const
Computes squared euclidean norm of vector.
Definition: Geom.h:480
bool linearProgram1(const std::vector< Line > &lines, unsigned long curr, float radius, const Vector &optVelocity, bool directionOpt, Vector &result)
Solves a one-dimensional linear program on a specified line subject to linear constraints defined by ...
Definition: Geom.cpp:88
Vector dir
direction vector of line.
Definition: Geom.h:205
void SetConvex(bool cvx)
Sets convexity of vertex.
Definition: Geom.cpp:61
The class defines a line in 2D space.
Definition: Geom.h:202
File contains Node, Point, Line, Vertex, ObstacleSegment classes and some methods and functions imple...
float Y() const
Returns Y-coordinate of the point.
Definition: Geom.cpp:30
The Point class defines a position (or euclidean vector from (0,0)) in 2D space.
Definition: Geom.h:61
#define Vector
Vector type definition.
Definition: Const.h:11
float x
X-coordinate of the point.
Definition: Geom.h:192
std::string ToString() const
Creates STL string, which contains x,y values.
Definition: Geom.cpp:48
bool IsConvex() const
Return convexity of vertex.
Definition: Geom.cpp:55
Point()
Point default constructor.
Definition: Geom.cpp:10
void linearProgram3(const std::vector< Line > &lines, size_t numObstLines, size_t beginLine, float radius, Vector &result)
Solves a two-dimensional linear program subject to linear constraints defined by lines and a circular...
Definition: Geom.cpp:214
float y
Y-coordinate of the point.
Definition: Geom.h:193
unsigned long int linearProgram2(const std::vector< Line > &lines, float radius, const Vector &optVelocity, bool directionOpt, Vector &result)
Solves a two-dimensional linear program subject to linear constraints defined by lines and a circular...
Definition: Geom.cpp:179
float X() const
Returns X-coordinate of the point.
Definition: Geom.cpp:24
The set of utility functions.
Definition: Geom.h:329
float SqPointSegDistance(Point L1, Point L2, Point P)
Computes squared euclidean distance from point to line segment.
Definition: Geom.cpp:68
std::pair< float, float > GetPair()
Creates STL pair (x,y) from point.
Definition: Geom.cpp:43
Point liesOn
point on line.
Definition: Geom.h:206
#define CN_EPS
Epsilon for float number operations definition.
Definition: Const.h:14


ORCAStar
Author(s): Stepan Drgachev
autogenerated on Wed Jul 15 2020 16:13:14