rev |
line source |
nuclear@16
|
1 /*
|
nuclear@16
|
2 curvedraw - a simple program to draw curves
|
nuclear@16
|
3 Copyright (C) 2015 John Tsiombikas <nuclear@member.fsf.org>
|
nuclear@16
|
4
|
nuclear@16
|
5 This program is free software: you can redistribute it and/or modify
|
nuclear@16
|
6 it under the terms of the GNU General Public License as published by
|
nuclear@16
|
7 the Free Software Foundation, either version 3 of the License, or
|
nuclear@16
|
8 (at your option) any later version.
|
nuclear@16
|
9
|
nuclear@16
|
10 This program is distributed in the hope that it will be useful,
|
nuclear@16
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
nuclear@16
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
nuclear@16
|
13 GNU General Public License for more details.
|
nuclear@16
|
14
|
nuclear@16
|
15 You should have received a copy of the GNU General Public License
|
nuclear@16
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
|
nuclear@16
|
17 */
|
nuclear@0
|
18 #ifndef CURVE_H_
|
nuclear@0
|
19 #define CURVE_H_
|
nuclear@0
|
20
|
nuclear@0
|
21 #include <vector>
|
nuclear@0
|
22 #include <vmath/vmath.h>
|
nuclear@0
|
23
|
nuclear@0
|
24 enum CurveType {
|
nuclear@0
|
25 CURVE_LINEAR,
|
nuclear@0
|
26 CURVE_HERMITE,
|
nuclear@0
|
27 CURVE_BSPLINE
|
nuclear@0
|
28 };
|
nuclear@0
|
29
|
nuclear@0
|
30 class Curve {
|
nuclear@0
|
31 private:
|
nuclear@12
|
32 std::vector<Vector4> cp;
|
nuclear@0
|
33 CurveType type;
|
nuclear@0
|
34
|
nuclear@12
|
35 // bounding box
|
nuclear@12
|
36 mutable Vector3 bbmin, bbmax;
|
nuclear@12
|
37 mutable bool bbvalid;
|
nuclear@12
|
38
|
nuclear@12
|
39 void calc_bounds() const;
|
nuclear@12
|
40 void inval_bounds() const;
|
nuclear@12
|
41
|
nuclear@0
|
42 public:
|
nuclear@0
|
43 Curve(CurveType type = CURVE_HERMITE);
|
nuclear@12
|
44 Curve(const Vector4 *cp, int numcp, CurveType type = CURVE_HERMITE); // homogenous
|
nuclear@12
|
45 Curve(const Vector3 *cp, int numcp, CurveType type = CURVE_HERMITE); // 3D points, w=1
|
nuclear@12
|
46 Curve(const Vector2 *cp, int numcp, CurveType type = CURVE_HERMITE); // 2D points, z=0, w=1
|
nuclear@0
|
47
|
nuclear@0
|
48 void set_type(CurveType type);
|
nuclear@0
|
49 CurveType get_type() const;
|
nuclear@0
|
50
|
nuclear@12
|
51 void add_point(const Vector4 &p);
|
nuclear@12
|
52 void add_point(const Vector3 &p, float weight = 1.0f);
|
nuclear@0
|
53 void add_point(const Vector2 &p, float weight = 1.0f);
|
nuclear@0
|
54 bool remove_point(int idx);
|
nuclear@0
|
55
|
nuclear@12
|
56 void clear(); // remove all control points
|
nuclear@12
|
57 bool empty() const; // true if 0 control points
|
nuclear@12
|
58 int size() const; // returns number of control points
|
nuclear@12
|
59 // access operators for control points
|
nuclear@12
|
60 Vector4 &operator [](int idx);
|
nuclear@12
|
61 const Vector4 &operator [](int idx) const;
|
nuclear@12
|
62 const Vector4 &get_point(int idx) const;
|
nuclear@0
|
63
|
nuclear@12
|
64 Vector3 get_point3(int idx) const;
|
nuclear@12
|
65 Vector2 get_point2(int idx) const;
|
nuclear@0
|
66 float get_weight(int idx) const;
|
nuclear@0
|
67
|
nuclear@12
|
68 bool set_point(int idx, const Vector3 &p, float weight = 1.0f);
|
nuclear@0
|
69 bool set_point(int idx, const Vector2 &p, float weight = 1.0f);
|
nuclear@0
|
70 bool set_weight(int idx, float weight);
|
nuclear@2
|
71 // move point without changing its weight
|
nuclear@12
|
72 bool move_point(int idx, const Vector3 &p);
|
nuclear@2
|
73 bool move_point(int idx, const Vector2 &p);
|
nuclear@12
|
74
|
nuclear@12
|
75 int nearest_point(const Vector3 &p) const;
|
nuclear@12
|
76 // nearest control point on the 2D plane z=0
|
nuclear@12
|
77 int nearest_point(const Vector2 &p) const;
|
nuclear@0
|
78
|
nuclear@12
|
79 /* get_bbox returns the axis-aligned bounding box of the curve's
|
nuclear@12
|
80 * control points.
|
nuclear@12
|
81 * NOTE: hermite curves can go outside of the bounding box of their control points
|
nuclear@12
|
82 * NOTE: lazy calculation of bounds is performed, use calc_bbox in multithreaded programs
|
nuclear@12
|
83 */
|
nuclear@12
|
84 void get_bbox(Vector3 *bbmin, Vector3 *bbmax) const;
|
nuclear@12
|
85 void calc_bbox(Vector3 *bbmin, Vector3 *bbmax) const;
|
nuclear@12
|
86 // normalize the curve's bounds to coincide with the unit cube
|
nuclear@12
|
87 void normalize();
|
nuclear@12
|
88
|
nuclear@12
|
89 // project a point to the curve (nearest point on the curve)
|
nuclear@13
|
90 Vector3 proj_point(const Vector3 &p, float refine_thres = 0.01) const;
|
nuclear@13
|
91 // equivalent to (proj_point(p) - p).length()
|
nuclear@12
|
92 float distance(const Vector3 &p) const;
|
nuclear@13
|
93 // equivalent to fabs((proj_point(p) - p).length_sq())
|
nuclear@13
|
94 float distance_sq(const Vector3 &p) const;
|
nuclear@12
|
95
|
nuclear@12
|
96 Vector3 interpolate_segment(int a, int b, float t) const;
|
nuclear@12
|
97 Vector3 interpolate(float t) const;
|
nuclear@12
|
98 Vector2 interpolate2(float t) const;
|
nuclear@12
|
99 Vector3 operator ()(float t) const;
|
nuclear@0
|
100 };
|
nuclear@0
|
101
|
nuclear@0
|
102 #endif // CURVE_H_
|