24 #include <geometry/spline.h> 25 #include <geometry/hom_vector.h> 40 m_num_subdivisions = 0;
46 Spline::Spline(
const vector<HomPoint>& control_points)
47 : m_control_points(control_points)
49 m_num_subdivisions = 0;
52 construct_bezier_curves();
66 m_control_points = control_points;
70 construct_bezier_curves();
80 m_control_points[i] = point;
84 construct_bezier_curves();
90 const vector<HomPoint>&
93 return m_control_points;
102 return m_bezier_curves;
113 return m_bezier_curves[bezier_index].eval(t);
124 return m_bezier_curves[bezier_index].tangent_at_t(t);
136 unsigned int points_per_bezier = (
unsigned int) rint( powf(2.0, m_num_subdivisions) ) * 3;
137 unsigned int points_total = m_bezier_curves.size() * (points_per_bezier - 1) + 1;
139 if (point_index >= points_total)
140 {
return m_bezier_curves.back().tangent_at_t(1.0); }
143 unsigned int bezier_index = point_index / points_per_bezier;
144 unsigned int index = point_index - bezier_index * points_per_bezier;
146 return m_bezier_curves[bezier_index].tangent_at_t( index / (
float) points_per_bezier );
163 vector<HomPoint> approximation;
165 for ( vector<Bezier>::iterator bit = m_bezier_curves.begin();
166 bit != m_bezier_curves.end();
169 vector<HomPoint> points = bit->approximate(num_subdivisions);
170 vector<HomPoint>::iterator pit = points.begin();
172 if ( bit != m_bezier_curves.begin() )
177 for ( vector<HomPoint>::iterator iter = pit;
178 iter != points.end();
181 approximation.push_back( *iter );
185 m_num_subdivisions = num_subdivisions;
187 return approximation;
193 vector<HomPoint>::iterator iter;
194 for ( iter = m_control_points.begin();
195 iter != m_control_points.end();
206 construct_bezier_curves();
210 Spline::construct_bezier_curves()
212 m_bezier_curves.clear();
214 if ( 0 == m_control_points.size() )
217 vector<HomPoint>::iterator i = m_control_points.begin();
218 vector<HomPoint>::iterator prev = i;
219 vector<HomPoint>::iterator cur = ++i;
220 vector<HomPoint>::iterator next = ++i;
224 while ( cur != m_control_points.end() )
232 v = (*cur) - (*prev);
234 cp2 = (*prev) + v * (1.0 / 3.0);
235 cp3 = (*prev) + v * (2.0 / 3.0);
237 if ( next == m_control_points.end() )
243 v = (*next) - (*cur);
244 t = (*cur) + v * (1.0 / 3.0);
245 cp4 = cp3 + (t - cp3) * 0.5;
248 vector<HomPoint> control_points;
249 control_points.push_back(cp1);
250 control_points.push_back(cp2);
251 control_points.push_back(cp3);
252 control_points.push_back(cp4);
254 m_bezier_curves.push_back(
Bezier(control_points) );
void set_control_point(unsigned int i, const HomPoint &p)
Set a specific control point.
HomPoint eval(unsigned int bezier_index, float t)
Get a point on the curve for a specified segment and value t.
Fawkes library namespace.
std::vector< HomPoint > approximate(unsigned int num_subdivisions=4)
Get linear approximation of the curve.
void set_control_points(const std::vector< HomPoint > &control_points)
Set the control points.
virtual ~Spline()
Destructor.
const std::vector< Bezier > & get_bezier_curves() const
Get the Bezier curves.
virtual void register_primitives()
Here, a derived class should register its primitives (HomPoints and HomVectors) by calling add_primit...
HomVector tangent(unsigned int bezier_index, float t)
Compute the tangent vector at position t of the i-th Bezier curve.
Base class for homogeneous primitives (vector and point).
virtual void post_transform()
This method is called after the primitives are transformed.
const std::vector< HomPoint > & get_control_points() const
Get the control points.