-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpolypath.h
More file actions
executable file
·159 lines (120 loc) · 4.03 KB
/
polypath.h
File metadata and controls
executable file
·159 lines (120 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#ifndef __POLYPATH_H
#define __POLYPATH_H
#include <cstdlib>
#include <cmath>
#include <vector>
#include <map>
#define POLYPATH_API
namespace polypath
{
class Vec2
{
public:
Vec2() {}
Vec2(const float& __x, const float& __y) : x(__x), y(__y) {}
Vec2(const Vec2& __v) : x(__v.x), y(__v.y) {}
Vec2& operator=(const Vec2& __v) { x=__v.x; y=__v.y; return *this; }
Vec2& set(const float& __x, const float& __y) { x = __x; y = __y; return *this; }
Vec2 operator+(const Vec2& __v) const { return Vec2(x+__v.x, y+__v.y); }
Vec2 operator-(const Vec2& __v) const { return Vec2(x-__v.x, y-__v.y); }
Vec2 operator*(const float& __c) const { return Vec2(x*__c, y*__c); }
Vec2 operator/(const float& __c) const { return (__c == float(0.0)) ? *this : Vec2(x/__c, y/__c); }
Vec2& operator+=(const Vec2& __v) { x+=__v.x; y+=__v.y; return *this; }
Vec2& operator*=(const float& __c) { x*=__c; y*=__c; return *this; }
Vec2& operator/=(const float& __c) { if (__c != float(0.0)) { x/=__c; y/=__c; } return *this; }
float dot(const Vec2& __v) const { return x*__v.x + y*__v.y; }
float det(const Vec2& __v) const { return x*__v.y - y*__v.x; }
float sqrmag() const { return dot(*this); }
float magnitude() const { return (float)std::sqrt(sqrmag()); };
float fast_mag() const { return (float)std::sqrt(sqrmag()); }; //TODO: fast sqrt
Vec2& normalize() { *this /= magnitude(); return *this; };
public:
float x, y;
};
//- PathStats --------------------------
struct PathStats
{
int nodes_searched;
int nodes_added;
int nodes_visited;
int nodes_left;
int links_source;
int links_dest;
float path_length;
float path_cost;
};
class Agent
{
public:
float radius;
public:
Agent();
//! Calculates the cost of turning.
/**
* The default implementation will return zero (no cost).
*/
virtual float calculateTurnCost(float deg) const;
};
/*
class AgentDef : public Agent
{
protected:
float turn_angle_limit_right; // positive
float turn_angle_limit_left; // negative
float turn_angle_cost_mult;
protected:
float calculateTurnCost(float deg) const;
}:
*/
class MapInst;
//- MapDef --------------------------
class POLYPATH_API MapDef
{
public:
MapDef();
~MapDef();
//! Adds new shape
/**
* @return Returns the identifier number for the shape.
*
* @note Note that the instances will NOT be updated.
* Use rebuildInstances when finished adding/removing shapes.
* @see removeShape, clearShapes, rebuildInstances
*/
int addShape(const std::vector<Vec2>& vlist);
//! Removes existing shape from the map.
/**
* @param shid Shape identifier number.
*
* @note Note that the instances will NOT be updated.
* Use rebuildInstances when finished adding/removing shapes.
* @see addShape, clearShapes, rebuildInstances
*/
void removeShape(int shid);
//! Clears all shapes from the map.
/**
* @note Note that the instances will NOT be updated.
* Use rebuildInstances when finished adding/removing shapes.
* @see addShape, removeShape, rebuildInstances
*/
void clearShapes();
void initInstance(float offset);
void clearInstances();
void rebuildInstances();
bool getInstanceOffsets(std::vector<float>& offs) const;
//Note: this could be slow and does a lot of memory allocations
bool getInstanceShapeVertices(float offset, std::vector<std::vector<Vec2> >& sverts) const;
int computePath(float offset, const Vec2& srcpt, const Vec2& dstpt,
std::vector<Vec2>* pathpoints, PathStats* pst) const;
int computePath(float offset, const Vec2& srcpt, const Vec2& dstpt,
std::vector<Vec2>* pathpoints, std::vector<Vec2>* visited, PathStats* pst) const;
void clear();
private:
std::map<int, std::vector<Vec2> > shapes; //!< Blueprint (original shapes)
std::map<float, MapInst*> maps; //!< Map list
int next_shid;
public:
bool clamp_max; //!< Use the largest offset found instead of bailing out
};
}; // namespace polypath
#endif // __POLYPATH_H