9
/* Need some kind of efficient iterator / position reference. */
12
* path consists of handles and commands
13
* reversing the handles and commands reverses the path
14
* affine transforming the handles affine transforms the path
16
* all methods are O(n) or better, and bounds are given for each method and function.
17
* library is as self contained as possible
21
* How do we show elements through the iterator?
35
unsigned const PathOpHandles[] = {1, 1, 2, 3, 4, 0, 0};
36
int const PathOpTail[] = {0, -1, -1, -1, -1, 0, 0};
40
std::vector<Point> handles;
41
std::vector<PathOp> cmd;
46
std::vector<Point>::const_iterator s, e;
47
std::vector<Point>::const_iterator begin() const {return s;}
48
std::vector<Point>::const_iterator end() const {return e;}
49
Point first() { return *s;}
50
Point last() { return e[-1];}
54
std::vector<Point>::const_iterator s,
55
std::vector<Point>::const_iterator e) : op(op), s(s), e(e) {
58
Point operator[](int i) {return s[i];}
60
void point_tangent_acc_at(double t, Point &p, Point &t, Point &a);
61
Point point_at(double t);
62
//Point tangent_at(double t);
63
bool nearest_location(Point p, double& dist, double& t);
68
std::vector<PathOp>::const_iterator c;
69
std::vector<Point>::const_iterator h;
72
PathConstIter(std::vector<PathOp>::const_iterator c,
73
std::vector<Point>::const_iterator h) :
75
void operator++() {h+=PathOpHandles[*c]; c++;}
76
void operator--() {c--; h-=PathOpHandles[*c];}
77
PathElem operator*() const {return PathElem(*c, h+PathOpTail[*c], h + PathOpHandles[*c]);}
79
std::vector<Point>::const_iterator begin() const {return h;}
80
std::vector<Point>::const_iterator end() const {return h + PathOpHandles[*c];}
81
PathOp cmd() { return *c;}
84
typedef PathConstIter const_iterator;
89
double t; // element specific meaning [0,1)
90
PathLocation(PathConstIter it, double t) : it(it), t(t) {}
93
PathConstIter begin() const { return PathConstIter(cmd.begin(), handles.begin());}
94
PathConstIter end() const { return PathConstIter(cmd.end(), handles.end());}
96
/** returns the point at the position given by walking along the path. */
97
PathLocation point_at_arc_length(double s);
99
/** return the last nearest point on the path. */
100
PathLocation nearest_location(Point p, double& dist);
102
/** return a new path over [begin, end). */
103
Path subpath(PathConstIter begin, PathConstIter end);
105
/** return a new path over [begin, end). */
106
Path subpath(PathLocation begin, PathLocation end);
108
/** compute the bounding box of this path. */
109
Maybe<Rect> bbox() const;
111
/** a new path with an extra node inserted at at without changing the curve. */
112
Path insert_nodes(PathLocation* b, PathLocation* e);
113
Path insert_node(PathLocation at);
115
/** coords of point on path. */
116
Point point_at(PathLocation at);
118
void point_tangent_acc_at (PathLocation at, Point & pos, Point & tgt, Point &acc);
120
void push_back(PathElem e);
121
void insert(PathConstIter before, PathConstIter s, PathConstIter e);
122
PathConstIter indexed_elem(int i) { // mainly for debugging
123
PathConstIter it = begin();
124
while(--i >= 0) ++it;
129
inline bool operator!=(const Path::PathConstIter &a, const Path::PathConstIter &b)
130
{ return (a.c!=b.c) || (a.h != b.h);}
132
//Path operator * (Path, Matrix);
134
template <class T> Path operator*(Path const &p, T const &m) {
138
pr.handles.reserve(p.handles.size());
140
for(unsigned i = 0; i < p.handles.size(); i++) {
141
pr.handles.push_back(p.handles[i]*m);
146
template <typename Point, unsigned order>
148
static inline Point compute(double t, Point *b) {
150
for ( unsigned i=0 ; i < order ; i++ ) {
151
child[i] = BezImpl<Point, 1>(t, b + i);
153
return BezImpl<Point, order-1>::compute(t, child);
157
template <typename Point>
158
struct BezImpl<Point, 1> {
159
static inline Point compute(double t, Point *b) {
160
return ( 1 - t ) * b[0] + t * b[1];
164
template <typename Point>
165
struct BezImpl<Point, 0> {
166
static inline Point compute(double t, Point *b) {
171
template <unsigned order, typename Point>
173
inline Point bezier_at(double t, Point *b) {
174
return BezImpl<Point, order>::compute(t, b);
179
#endif // SEEN_PATH_H
184
c-file-style:"stroustrup"
185
c-file-offsets:((innamespace . 0)(substatement-open . 0))
190
vim: filetype=c++:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :