| 1 |
// -*- related-file-name: "../../liblcdf/bezier.cc" -*- |
| 2 |
#ifndef LCDF_BEZIER_HH |
| 3 |
#define LCDF_BEZIER_HH |
| 4 |
#include <lcdf/point.hh> |
| 5 |
#include <lcdf/vector.hh> |
| 6 |
#include <string.h> |
| 7 |
|
| 8 |
class Bezier { public: |
| 9 |
|
| 10 |
Bezier() : _bb(-1) { } |
| 11 |
inline Bezier(Point p[4]) noexcept; |
| 12 |
inline Bezier(const Point &, const Point &, const Point &, const Point &) noexcept; |
| 13 |
|
| 14 |
const Point *points() const { return _p; } |
| 15 |
const Point &point(int i) const { assert(i>=0&&i<4); return _p[i]; } |
| 16 |
Point &mpoint(int i) { assert(i>=0&&i<4); _bb = -1; return _p[i]; } |
| 17 |
void set_point(int i, const Point &p) { mpoint(i) = p; } |
| 18 |
|
| 19 |
Point eval(double) const noexcept; |
| 20 |
bool is_flat(double) const noexcept; |
| 21 |
bool in_bb(const Point &, double) const noexcept; |
| 22 |
bool hit(const Point &, double) const noexcept; |
| 23 |
|
| 24 |
inline double bb_left() const noexcept; |
| 25 |
inline double bb_right() const noexcept; |
| 26 |
inline double bb_top() const noexcept; |
| 27 |
inline double bb_bottom() const noexcept; |
| 28 |
|
| 29 |
inline double bb_left_x() const noexcept; |
| 30 |
inline double bb_right_x() const noexcept; |
| 31 |
inline double bb_top_x() const noexcept; |
| 32 |
inline double bb_bottom_x() const noexcept; |
| 33 |
|
| 34 |
void halve(Bezier &, Bezier &) const noexcept; |
| 35 |
|
| 36 |
inline void segmentize(Vector<Point> &) const; |
| 37 |
void segmentize(Vector<Point> &, bool) const; |
| 38 |
|
| 39 |
static void fit(const Vector<Point> &, double, Vector<Bezier> &); |
| 40 |
|
| 41 |
private: |
| 42 |
|
| 43 |
Point _p[4]; |
| 44 |
mutable int _bb; |
| 45 |
|
| 46 |
void make_bb() const noexcept; |
| 47 |
inline void ensure_bb() const noexcept; |
| 48 |
|
| 49 |
double hit_recurse(const Point &, double, double, double, double, double) const noexcept; |
| 50 |
|
| 51 |
}; |
| 52 |
|
| 53 |
|
| 54 |
inline |
| 55 |
Bezier::Bezier(Point p[4]) noexcept |
| 56 |
: _bb(-1) |
| 57 |
{ |
| 58 |
memcpy(_p, p, sizeof(Point) * 4); |
| 59 |
} |
| 60 |
|
| 61 |
inline |
| 62 |
Bezier::Bezier(const Point &p0, const Point &p1, const Point &p2, const Point &p3) noexcept |
| 63 |
{ |
| 64 |
_p[0] = p0; |
| 65 |
_p[1] = p1; |
| 66 |
_p[2] = p2; |
| 67 |
_p[3] = p3; |
| 68 |
_bb = -1; |
| 69 |
} |
| 70 |
|
| 71 |
inline void |
| 72 |
Bezier::ensure_bb() const noexcept |
| 73 |
{ |
| 74 |
if (_bb < 0) |
| 75 |
make_bb(); |
| 76 |
} |
| 77 |
|
| 78 |
inline double |
| 79 |
Bezier::bb_top_x() const noexcept |
| 80 |
{ |
| 81 |
return _p[(_bb >> 4) & 3].y; |
| 82 |
} |
| 83 |
|
| 84 |
inline double |
| 85 |
Bezier::bb_left_x() const noexcept |
| 86 |
{ |
| 87 |
return _p[(_bb >> 2) & 3].x; |
| 88 |
} |
| 89 |
|
| 90 |
inline double |
| 91 |
Bezier::bb_bottom_x() const noexcept |
| 92 |
{ |
| 93 |
return _p[(_bb >> 6) & 3].y; |
| 94 |
} |
| 95 |
|
| 96 |
inline double |
| 97 |
Bezier::bb_right_x() const noexcept |
| 98 |
{ |
| 99 |
return _p[(_bb >> 0) & 3].x; |
| 100 |
} |
| 101 |
|
| 102 |
inline double |
| 103 |
Bezier::bb_top() const noexcept |
| 104 |
{ |
| 105 |
ensure_bb(); |
| 106 |
return bb_top_x(); |
| 107 |
} |
| 108 |
|
| 109 |
inline double |
| 110 |
Bezier::bb_left() const noexcept |
| 111 |
{ |
| 112 |
ensure_bb(); |
| 113 |
return bb_left_x(); |
| 114 |
} |
| 115 |
|
| 116 |
inline double |
| 117 |
Bezier::bb_bottom() const noexcept |
| 118 |
{ |
| 119 |
ensure_bb(); |
| 120 |
return bb_bottom_x(); |
| 121 |
} |
| 122 |
|
| 123 |
inline double |
| 124 |
Bezier::bb_right() const noexcept |
| 125 |
{ |
| 126 |
ensure_bb(); |
| 127 |
return bb_right_x(); |
| 128 |
} |
| 129 |
|
| 130 |
inline void |
| 131 |
Bezier::segmentize(Vector<Point> &v) const |
| 132 |
{ |
| 133 |
segmentize(v, v.size() == 0 || v.back() != _p[0]); |
| 134 |
} |
| 135 |
|
| 136 |
#endif |