curvedraw

view src/curve.h @ 13:4da693339d99

- distance from curve - hover/selection of curves directly on the curve
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 20 Dec 2015 08:22:24 +0200
parents 84a647283237
children 7f795f7fecd6
line source
1 #ifndef CURVE_H_
2 #define CURVE_H_
4 #include <vector>
5 #include <vmath/vmath.h>
7 enum CurveType {
8 CURVE_LINEAR,
9 CURVE_HERMITE,
10 CURVE_BSPLINE
11 };
13 class Curve {
14 private:
15 std::vector<Vector4> cp;
16 CurveType type;
18 // bounding box
19 mutable Vector3 bbmin, bbmax;
20 mutable bool bbvalid;
22 void calc_bounds() const;
23 void inval_bounds() const;
25 public:
26 Curve(CurveType type = CURVE_HERMITE);
27 Curve(const Vector4 *cp, int numcp, CurveType type = CURVE_HERMITE); // homogenous
28 Curve(const Vector3 *cp, int numcp, CurveType type = CURVE_HERMITE); // 3D points, w=1
29 Curve(const Vector2 *cp, int numcp, CurveType type = CURVE_HERMITE); // 2D points, z=0, w=1
31 void set_type(CurveType type);
32 CurveType get_type() const;
34 void add_point(const Vector4 &p);
35 void add_point(const Vector3 &p, float weight = 1.0f);
36 void add_point(const Vector2 &p, float weight = 1.0f);
37 bool remove_point(int idx);
39 void clear(); // remove all control points
40 bool empty() const; // true if 0 control points
41 int size() const; // returns number of control points
42 // access operators for control points
43 Vector4 &operator [](int idx);
44 const Vector4 &operator [](int idx) const;
45 const Vector4 &get_point(int idx) const;
47 Vector3 get_point3(int idx) const;
48 Vector2 get_point2(int idx) const;
49 float get_weight(int idx) const;
51 bool set_point(int idx, const Vector3 &p, float weight = 1.0f);
52 bool set_point(int idx, const Vector2 &p, float weight = 1.0f);
53 bool set_weight(int idx, float weight);
54 // move point without changing its weight
55 bool move_point(int idx, const Vector3 &p);
56 bool move_point(int idx, const Vector2 &p);
58 int nearest_point(const Vector3 &p) const;
59 // nearest control point on the 2D plane z=0
60 int nearest_point(const Vector2 &p) const;
62 /* get_bbox returns the axis-aligned bounding box of the curve's
63 * control points.
64 * NOTE: hermite curves can go outside of the bounding box of their control points
65 * NOTE: lazy calculation of bounds is performed, use calc_bbox in multithreaded programs
66 */
67 void get_bbox(Vector3 *bbmin, Vector3 *bbmax) const;
68 void calc_bbox(Vector3 *bbmin, Vector3 *bbmax) const;
69 // normalize the curve's bounds to coincide with the unit cube
70 void normalize();
72 // project a point to the curve (nearest point on the curve)
73 Vector3 proj_point(const Vector3 &p, float refine_thres = 0.01) const;
74 // equivalent to (proj_point(p) - p).length()
75 float distance(const Vector3 &p) const;
76 // equivalent to fabs((proj_point(p) - p).length_sq())
77 float distance_sq(const Vector3 &p) const;
79 Vector3 interpolate_segment(int a, int b, float t) const;
80 Vector3 interpolate(float t) const;
81 Vector2 interpolate2(float t) const;
82 Vector3 operator ()(float t) const;
83 };
85 #endif // CURVE_H_