Atoms Crowd  7.0.0
RVO2.h
1 #pragma once
2 // ===========================================================================
3 // Copyright (c) 2015 Toolchefs Ltd. All rights reserved.
4 //
5 // Use of this software is subject to the terms of the Toolchefs license
6 // agreement provided at the time of installation or download, or which
7 // otherwise accompanies this software in either electronic or hard copy form.
8 // ===========================================================================
9 
10 #include <Atoms/Agent.h>
11 #include <AtomsCore/AtomsMath.h>
12 
13 
14 namespace Atoms
15 {
16  namespace RVO2
17  {
18  inline float distSqPointLineSegment(const AtomsMath::Vector2f& a, const AtomsMath::Vector2f& b, const AtomsMath::Vector2f& c)
19  {
20  const float r = ((c - a).dot(b - a)) / (b - a).length2();
21 
22  if (r < 0.0f) {
23  return (c - a).length2();
24  }
25  else if (r > 1.0f) {
26  return (c - b).length2();
27  }
28  else {
29  return (c - (a + (b - a) * r)).length2();
30  }
31  }
32 
33  inline float distSqPointLineSegment(const AtomsMath::Vector3f& a, const AtomsMath::Vector3f& b, const AtomsMath::Vector3f& c)
34  {
35  const float r = ((c - a).dot(b - a)) / (b - a).length2();
36 
37  if (r < 0.0f) {
38  return (c - a).length2();
39  }
40  else if (r > 1.0f) {
41  return (c - b).length2();
42  }
43  else {
44  return (c - (a + (b - a) * r)).length2();
45  }
46  }
47 
48  inline float det(const AtomsMath::Vector2f& vector1, const AtomsMath::Vector2f& Vector2f)
49  {
50  return vector1.x * Vector2f.y - vector1.y * Vector2f.x;
51  }
52 
53  inline float leftOf(const AtomsMath::Vector2f& a, const AtomsMath::Vector2f& b, const AtomsMath::Vector2f& c)
54  {
55  return det(a - c, b - a);
56  }
57 
58  inline float sqr(float a)
59  {
60  return a * a;
61  }
62 
63  class ATOMS_EXPORT Obstacle;
64 
65  class ATOMS_EXPORT Line {
66  public:
67 
68  AtomsMath::Vector2f point;
69 
70  AtomsMath::Vector2f direction;
71  };
72 
73  class ATOMS_EXPORT ObstacleTreeNode {
74  public:
75 
76  ObstacleTreeNode* left;
77 
78  const Obstacle* obstacle;
79 
80  ObstacleTreeNode* right;
81  };
82 
83  class ATOMS_EXPORT KdTreeObstacle {
84  public:
85 
86  explicit KdTreeObstacle();
87 
88  ~KdTreeObstacle();
89 
90  void buildObstacleTree(std::vector<Obstacle*>& simObstacles);
91 
92  ObstacleTreeNode* buildObstacleTreeRecursive(std::vector<Obstacle*>& simObstacles, const std::vector<Obstacle*>& copyObstacles);
93 
94  void deleteObstacleTree(ObstacleTreeNode* node);
95 
96  void clear();
97 
98  void queryObstacleTreeRecursive(
99  const AtomsMath::Vector3f& agentPosition,
100  float rangeSq,
101  float heightRange,
102  const ObstacleTreeNode* node,
103  std::vector<std::pair<float, const Obstacle*>>& obstacleNeighbors) const;
104 
105  void insertObstacleNeighbor(
106  const Obstacle* obstacle,
107  const AtomsMath::Vector3f& agentPosition,
108  float rangeSq,
109  float heightRange,
110  std::vector<std::pair<float, const Obstacle*>>& obstacleNeighbors) const;
111 
112  ObstacleTreeNode* tree();
113 
114  ObstacleTreeNode* obstacleTree_;
115 
116  static const size_t MAX_LEAF_SIZE = 10;
117  };
118 
119  class ATOMS_EXPORT Obstacle {
120  public:
121 
122  Obstacle();
123 
124  enum NearTypeEnum { FIRST, MIDDLE, LAST };
125 
126  AtomsMath::Vector2f getP1() const;
127 
128  Obstacle::NearTypeEnum distanceSqToPoint(const AtomsMath::Vector2f& pt, AtomsMath::Vector2f& nearPt, float& distSq) const;
129 
130  bool isConvex_;
131  Obstacle* nextObstacle_;
132  AtomsMath::Vector3f pointW_;
133  AtomsMath::Vector2f point_;
134  Obstacle* prevObstacle_;
135  AtomsMath::Vector2f unitDir_;
136  AtomsMath::Vector3f unitDirW_;
137  float length_;
138  float lengthW_;
139  size_t id_;
140  };
141 
142  ATOMS_EXPORT size_t computeORCALinesTurning(
143  const AtomsCore::Vector2f& position,
144  const AtomsCore::Vector2f& velocity,
145  const AtomsCore::Vector2f& prefVelocity,
146  const std::vector<const Atoms::Agent*>& agentNeighbors,
147  const std::vector<std::pair<float, const Atoms::RVO2::Obstacle*>>& obstacleNeighbors,
148  float timeHorizon,
149  float timeHorizonObst,
150  float radius,
151  float maxSpeed,
152  float timeStep,
153  float fps,
154  float sceneScale,
155  float radiusMultiplier,
156  float obstacleRadiusMultiplier,
157  float turningBias,
158  bool isPedVO,
159  int step,
160  int steps,
161  std::vector<Atoms::RVO2::Line>& orcaLines,
162  AtomsMath::Vector2f& optVel,
163  AtomsMath::Vector2f& prefDir,
164  float& prefSpeed);
165 
166  ATOMS_EXPORT AtomsCore::Vector2f computeNewVelocity(
167  const AtomsCore::Vector2f& position,
168  const AtomsCore::Vector2f& velocity,
169  AtomsCore::Vector2f prefVelocity,
170  const std::vector<const Atoms::Agent*>& neighbors,
171  const std::vector<std::pair<float, const Atoms::RVO2::Obstacle*>>& obstacleNeighbors,
172  float timeHorizon,
173  float timeHorizonObst,
174  float radius,
175  float maxSpeed,
176  float timeStep,
177  float fps,
178  float sceneScale,
179  float radiusMultiplier,
180  float obstacleRadiusMultiplier,
181  float speedConst,
182  float turningBias,
183  bool isPedVO,
184  int step,
185  int steps);
186 
187  ATOMS_EXPORT void adaptPreferredVelocity(
188  AtomsCore::Vector2f& prefVelocity,
189  const AtomsCore::Vector2f& position,
190  const std::vector<const Atoms::Agent*>& agentNeighbors,
191  const std::vector<std::pair<float, const Atoms::RVO2::Obstacle*>>& obstacleNeighbors,
192  float sceneScale,
193  float speedConst);
194 
207  ATOMS_EXPORT bool linearProgram1(
208  const std::vector<Line>& lines,
209  size_t lineNo,
210  float radius,
211  const AtomsMath::Vector2f& optVelocity,
212  bool directionOpt,
213  float turnBias,
214  bool isPedVO,
215  AtomsMath::Vector2f& result);
216 
227  ATOMS_EXPORT size_t linearProgram2(
228  const std::vector<Line>& lines,
229  float radius,
230  const AtomsMath::Vector2f& optVelocity,
231  bool directionOpt,
232  float turnBias,
233  bool isPedVO,
234  AtomsMath::Vector2f& result);
235 
245  ATOMS_EXPORT void linearProgram3(
246  const std::vector<Line>& lines,
247  size_t numObstLines,
248  size_t beginLine,
249  float radius,
250  float turnBias,
251  bool isPedVO,
252  AtomsMath::Vector2f& result);
253 
254  }
255 }
Definition: RVO2.h:83
Definition: RVO2.h:65
Definition: RVO2.h:119
Definition: RVO2.h:73
AtomsMath::Vector2f Vector2f
Vector2 class.
Definition: AtomsMath.h:55
Atoms namespace.
Definition: Agent.h:29