curvedraw

view src/curve.h @ 16:7f795f7fecd6

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