| 1 |
/* -*- coding: utf-8 -*- */ |
| 2 |
/*! \page curves Bézier curves |
| 3 |
|
| 4 |
The claw::math::curve class is a C++ implementation of the |
| 5 |
parametrized curves known as |
| 6 |
<a href="http://en.wikipedia.org/wiki/B%C3%A9zier_curve">Bézier |
| 7 |
curves</a>. |
| 8 |
|
| 9 |
<!-- |
| 10 |
Table of content generated with: |
| 11 |
grep '\\\(sub\)*section' *.dox \ |
| 12 |
| cut -d' ' -f1-2 \ |
| 13 |
| sed 's:\\section: - \\ref:;s:\\subsection: - \\ref:' |
| 14 |
--> |
| 15 |
- \ref curve-introduction |
| 16 |
- \ref curve-curve-class |
| 17 |
- \ref curve-control-point-class |
| 18 |
- \ref curve-access-control-point |
| 19 |
- \ref curve-add-control-point |
| 20 |
- \ref curve-resolved-point-class |
| 21 |
- \ref curve-section-class |
| 22 |
- \ref curve-section-point-computation |
| 23 |
- \ref curve-section-properties-computation |
| 24 |
|
| 25 |
<!-- ----------------------------------------------------------------------- --> |
| 26 |
\section curve-introduction Introduction |
| 27 |
|
| 28 |
The Bézier curves are defined by a starting point |
| 29 |
<i>P<sub>0</sub></i>, an end point <i>P<sub>n</sub></i> and a sequence |
| 30 |
of points (<i>P<sub>1</sub>, …, P<sub>n-1</sub></i>) controlling the |
| 31 |
direction of the curve between <i>P<sub>0</sub></i> and |
| 32 |
<i>P<sub>n</sub></i>. The points of the curve are then defined for a |
| 33 |
given fraction <i>t ∈ </i>[0, 1] of the length of the curve by the |
| 34 |
following equations: |
| 35 |
\f[ B(t) = \sum_{j=0}^n \left( \begin{array}{c} |
| 36 |
n \\ |
| 37 |
j |
| 38 |
\end{array}\right) |
| 39 |
P_jt^j(1-t)^{n-j} \quad t \in [0, 1] \f] |
| 40 |
|
| 41 |
A \ref curve-curve-class "curve" in Claw is defined by a sequence of |
| 42 |
points <i>p<sub>j</sub></i> through which the curve goes. A \ref |
| 43 |
curve-section-class "section" <i>s<sub>j</sub></i> of the curve is then |
| 44 |
defined as two successive points (<i>p<sub>j</sub>, p<sub>j+1</sub></i>). |
| 45 |
The equations of Bézier, restricted to the cubic case (<i>n=3</i>), |
| 46 |
are used to describe the shape of each section. This shape is |
| 47 |
controlled by the output direction <i>o<sub>j</sub></i> of its first |
| 48 |
point and the input direction <i>i<sub>j+1</sub></i> of its last |
| 49 |
point. The equation of a section <i>s<sub>j</sub></i> is written as |
| 50 |
follows: |
| 51 |
\f[ B_j(t) = (1-t)^3p_j + 3(1-t)^2to_j + 3(1-t)t^2i_{j+1} + |
| 52 |
t^3p_{j+1} \quad t \in [0, 1] \f] |
| 53 |
|
| 54 |
One can then build complex curves by concatenating several sections. |
| 55 |
|
| 56 |
The library provides the following functionalities: |
| 57 |
|
| 58 |
- use any type for the coordinates, as long as their traits are defined, |
| 59 |
- defining a curve as a list of control points, |
| 60 |
- computing the point at a given <i>t</i> on a section of the curve, |
| 61 |
- computing all the points of the curve at a given x-coordinate, |
| 62 |
- computing the tangent of the curve at a given <i>t</i>. |
| 63 |
|
| 64 |
<!-- ----------------------------------------------------------------------- --> |
| 65 |
\section curve-curve-class The curve class |
| 66 |
|
| 67 |
The curve class is defined as follows: |
| 68 |
|
| 69 |
\code |
| 70 |
template< typename C, typename Traits = coordinate_traits<C> > |
| 71 |
class curve |
| 72 |
{ |
| 73 |
public: |
| 74 |
typedef C coordinate_type; |
| 75 |
|
| 76 |
typedef Traits traits_type; |
| 77 |
|
| 78 |
typedef typename traits_type::value_type value_type; |
| 79 |
|
| 80 |
// see below for the details |
| 81 |
class control_point; |
| 82 |
|
| 83 |
public: |
| 84 |
typedef implementation_defined iterator; |
| 85 |
|
| 86 |
typedef implementation_defined const_iterator; |
| 87 |
|
| 88 |
// see below for the details |
| 89 |
class section |
| 90 |
{ |
| 91 |
public: |
| 92 |
// see below for the details |
| 93 |
class resolved_point; |
| 94 |
|
| 95 |
}; // class section |
| 96 |
|
| 97 |
public: |
| 98 |
void push_back( const control_point& p ); |
| 99 |
void push_front( const control_point& p ); |
| 100 |
void insert( const iterator& pos, const control_point& p ); |
| 101 |
|
| 102 |
section get_section( const const_iterator& pos ) const; |
| 103 |
|
| 104 |
std::vector<typename section::resolved_point> |
| 105 |
get_point_at_x( value_type x, bool off_domain = false ) const; |
| 106 |
|
| 107 |
iterator begin(); |
| 108 |
iterator end(); |
| 109 |
const_iterator begin() const; |
| 110 |
const_iterator end() const; |
| 111 |
|
| 112 |
}; // class curve |
| 113 |
\endcode |
| 114 |
|
| 115 |
The curve class is parametrized by the type of the coordinates, named |
| 116 |
\c C, and the traits to use to access their properties, named \c Traits. |
| 117 |
|
| 118 |
The class claw::math::coordinate_2d is a good candidate for \c C, but |
| 119 |
any class can be used as long as Traits provides an implementation for |
| 120 |
the the following interface: |
| 121 |
|
| 122 |
\code |
| 123 |
struct traits_interface |
| 124 |
{ |
| 125 |
/** \brief The type of the coordinate. */ |
| 126 |
typedef implementation_defined coordinate_type; |
| 127 |
|
| 128 |
/** \brief The type of the components of the coordinate. */ |
| 129 |
typedef implementation_defined value_type; |
| 130 |
|
| 131 |
/** |
| 132 |
* \brief Get the component of a coordinate on the x-axis. |
| 133 |
* \param c The coordinate from which the component is taken. |
| 134 |
*/ |
| 135 |
static value_type get_x( const coordinate_type& c ); |
| 136 |
|
| 137 |
/** |
| 138 |
* \brief Get the component of a coordinate on the y-axis. |
| 139 |
* \param c The coordinate from which the component is taken. |
| 140 |
*/ |
| 141 |
static value_type get_y( const coordinate_type& c ); |
| 142 |
|
| 143 |
/** |
| 144 |
* \brief Create a new coordinate from with the components sets to the |
| 145 |
* provided values. |
| 146 |
* \param x The position on the x axis. |
| 147 |
* \param y The position on the y axis. |
| 148 |
*/ |
| 149 |
static coordinate_type make_coordinate( value_type x, value_type y ); |
| 150 |
|
| 151 |
}; // struct traits_interface |
| 152 |
\endcode |
| 153 |
|
| 154 |
<!-- ----------------------------------------------------------------------- --> |
| 155 |
\section curve-control-point-class The control_point class |
| 156 |
|
| 157 |
The \ref claw::math::curve::control_point "control point class" is the |
| 158 |
aggregation of a coordinate through which the curve goes (the position |
| 159 |
of the point), and two points defining the direction from which |
| 160 |
the curve respectively "enters and leaves" the point. Those input and |
| 161 |
output points are relative to the same origin than the curve's |
| 162 |
point (i.e. their coordinates are not relative to the curve's point |
| 163 |
itself). |
| 164 |
|
| 165 |
The interface of the class is as follows: |
| 166 |
\code |
| 167 |
class control_point |
| 168 |
{ |
| 169 |
public: |
| 170 |
control_point(); |
| 171 |
explicit control_point( const coordinate_type& p ); |
| 172 |
control_point |
| 173 |
( const coordinate_type& p, const coordinate_type& input_direction, |
| 174 |
const coordinate_type& output_direction ); |
| 175 |
|
| 176 |
const coordinate_type& get_position() const; |
| 177 |
const coordinate_type& get_input_direction() const; |
| 178 |
const coordinate_type& get_output_direction() const; |
| 179 |
|
| 180 |
}; // class control_point |
| 181 |
\endcode |
| 182 |
|
| 183 |
\b Note: building a control point as follows |
| 184 |
\code |
| 185 |
coordinate_type c; |
| 186 |
control_point p( c ) |
| 187 |
\endcode |
| 188 |
is equivalent to the following code |
| 189 |
\code |
| 190 |
coordinate_type c; |
| 191 |
control_point p( c, c, c ) |
| 192 |
\endcode |
| 193 |
|
| 194 |
<!-- ----------------------------------------------------------------------- --> |
| 195 |
\subsection curve-access-control-point Accessing the control points of a curve |
| 196 |
|
| 197 |
The control points of the curve can be accessed using iterators |
| 198 |
implementing the concept of ForwardIterator and BackwardIterator. The |
| 199 |
\c begin() and \c end() methods return an \c iterator respectively on |
| 200 |
the first control point of the curve or just past the last control |
| 201 |
point of the curve. If the call is done on a \c const \c curve then a |
| 202 |
\c const_iterator is returned. |
| 203 |
|
| 204 |
<!-- ----------------------------------------------------------------------- --> |
| 205 |
\subsection curve-add-control-point Adding a control point to a curve |
| 206 |
|
| 207 |
Several methods can be used to insert a control point in a curve: |
| 208 |
- \code void curve::insert( const iterator& pos, const control_point& p ) |
| 209 |
\endcode will add the control point \c p before the control point |
| 210 |
represented by the iterator \c pos. If \c pos == end(), then |
| 211 |
\c p is inserted at the end of the curve, |
| 212 |
- \code void curve::push_back( const control_point& p ) \endcode will add |
| 213 |
the control point \c p at the end of the curve. This is |
| 214 |
equivalent to \c curve::insert( curve::end(), p ), |
| 215 |
- \code void curve::push_front( const control_point& p ) \endcode will add |
| 216 |
the control point \c p at the beginning of the curve. This is |
| 217 |
equivalent to \c curve::insert( curve::begin(), p ). |
| 218 |
|
| 219 |
\b Note: Please note that inserting a point in the curve between the beginning |
| 220 |
and the end of a section will invalidate the section. |
| 221 |
|
| 222 |
<!-- ----------------------------------------------------------------------- --> |
| 223 |
\section curve-resolved-point-class The resolved_point class |
| 224 |
|
| 225 |
The \ref claw::math::curve::section::resolved_point |
| 226 |
"resolved point class" represents the result of the computation of a |
| 227 |
point on the curve from an other parameter than <i>t</i>. |
| 228 |
|
| 229 |
The interface of the class is defined as follows: |
| 230 |
\code |
| 231 |
class resolved_point |
| 232 |
{ |
| 233 |
public: |
| 234 |
const coordinate_type& get_position() const; |
| 235 |
const section& get_section() const; |
| 236 |
double get_date() const; |
| 237 |
|
| 238 |
}; // class resolved_point |
| 239 |
\endcode |
| 240 |
|
| 241 |
The interface presents the following methods: |
| 242 |
- \c get_position() returns the coordinates of the points on the |
| 243 |
curve, |
| 244 |
- \c get_section() returns the section on which the point has been |
| 245 |
found, |
| 246 |
- \c get_date() returns a value of <i>t</i> such that for a \c |
| 247 |
resolved_point p, the following applies: |
| 248 |
\code |
| 249 |
p.get_section().get_point_at( p.get_date() ) == p.get_position() |
| 250 |
\endcode |
| 251 |
|
| 252 |
<!-- ----------------------------------------------------------------------- --> |
| 253 |
\section curve-section-class The section class |
| 254 |
|
| 255 |
A section describes a part of a curve represented by two successive |
| 256 |
control points. One can access the section starting from the point |
| 257 |
represented by an iterator \c it in the curve \c c using this |
| 258 |
code: |
| 259 |
\code |
| 260 |
section s( c.get_section(it) ); |
| 261 |
\endcode |
| 262 |
|
| 263 |
The interface of the section class is as follows: |
| 264 |
\code |
| 265 |
class section |
| 266 |
{ |
| 267 |
public: |
| 268 |
typedef curve::const_iterator iterator_type; |
| 269 |
|
| 270 |
public: |
| 271 |
section( const iterator_type& origin, const iterator_type& end ); |
| 272 |
|
| 273 |
coordinate_type get_point_at( double t ) const; |
| 274 |
coordinate_type get_tangent_at( double t ) const; |
| 275 |
std::vector<resolved_point> |
| 276 |
get_point_at_x( value_type x, bool off_domain = false ) const; |
| 277 |
|
| 278 |
bool empty() const; |
| 279 |
|
| 280 |
}; // class section |
| 281 |
\endcode |
| 282 |
|
| 283 |
\subsection curve-section-point-computation Computing the points of the curve |
| 284 |
|
| 285 |
The main procedure to compute the coordinates of a point of the curve |
| 286 |
is to obtain a section of the curve and call |
| 287 |
\code |
| 288 |
section::get_point_at( double t ) |
| 289 |
\endcode |
| 290 |
to get the coordinates of the point at a given <i>t</i> on the |
| 291 |
section. |
| 292 |
|
| 293 |
If <i>t</i> is unknown, one can use the method |
| 294 |
\c get_point_at_x( value_type x, bool off_domain ) defined in claw::math::curve |
| 295 |
and claw::math::curve::section to obtain all the points of the curve |
| 296 |
or section having the given x-coordinate. If the argument |
| 297 |
\c off_domain is set to true, then the following applies: |
| 298 |
- for \c section::get_point_at_x(), the resolved points found with a |
| 299 |
date <i>t</i> such that <i>t < 0</i> or <i>1 < t</i> are kept, |
| 300 |
- for \c curve::get_point_at_x(), the points found at a date |
| 301 |
<i>t < 0</i> on the first section of the curve or at a date <i>t > |
| 302 |
1</i> on the last section of the curve are kept. |
| 303 |
|
| 304 |
\subsection curve-section-properties-computation Other properties of the curve. |
| 305 |
|
| 306 |
The \c section::get_tangent_at( double t ) returns the tangent to the |
| 307 |
curve at a given <i>t</i> on a given section. |
| 308 |
|
| 309 |
*/ |