Developer Documentation

BezierCurveT_impl.hh
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4 * Copyright (c) 2001-2015, RWTH-Aachen University *
5 * Department of Computer Graphics and Multimedia *
6 * All rights reserved. *
7 * www.openflipper.org *
8 * *
9 *---------------------------------------------------------------------------*
10 * This file is part of OpenFlipper. *
11 *---------------------------------------------------------------------------*
12 * *
13 * Redistribution and use in source and binary forms, with or without *
14 * modification, are permitted provided that the following conditions *
15 * are met: *
16 * *
17 * 1. Redistributions of source code must retain the above copyright notice, *
18 * this list of conditions and the following disclaimer. *
19 * *
20 * 2. Redistributions in binary form must reproduce the above copyright *
21 * notice, this list of conditions and the following disclaimer in the *
22 * documentation and/or other materials provided with the distribution. *
23 * *
24 * 3. Neither the name of the copyright holder nor the names of its *
25 * contributors may be used to endorse or promote products derived from *
26 * this software without specific prior written permission. *
27 * *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41
42
43
44
45
46 //=============================================================================
47 //
48 // CLASS BezierCurve - IMPLEMENTATION
49 //
50 //=============================================================================
51
52 #define ACG_BEZIERCURVE_C
53
54 //== INCLUDES =================================================================
55
56
57 #include "BezierCurveT.hh"
58
59
60 //== IMPLEMENTATION ==========================================================
61
62
63 namespace ACG {
64
65
66 //-----------------------------------------------------------------------------
67
68
69 template <class Point>
70 Point
71 BezierCurveT<Point>::
72 operator()(Scalar _t) const
73 {
74 // copy controll points
75 std::vector<Point> b(*this);
76
77 unsigned int n = b.size()-1, k;
78 Scalar t0(1.0-_t), t1(_t);
79
80
81 // de Casteljau
82 unsigned int i, j;
83 for (i=0; i<n; ++i)
84 for (j=0, k=n-i; j<k; ++j)
85 b[j] = t0*b[j] + t1*b[j+1];
86
87
88 return b[0];
89 }
90
91
92 //-----------------------------------------------------------------------------
93
94
95 template <class Point>
96 void
97 BezierCurveT<Point>::
98 subdivide(Scalar _t, Self& _curve0, Self& _curve1) const
99 {
100 // copy controll points
101 std::vector<Point> b(*this);
102
103 int n = degree();
104 Scalar t0(1.0-_t), t1(_t);
105
106
107 _curve0.clear();
108 _curve0.reserve(n+1);
109 _curve1.clear();
110 _curve1.reserve(n+1);
111
112 std::vector<Point> tmp;
113 tmp.reserve(n+1);
114
115
116 // de Casteljau
117 int i, j, k;
118 for (i=0; i<n; ++i)
119 {
120 _curve0.push_back(b[0]);
121 tmp.push_back(b[n-i]);
122
123 for (j=0, k=n-i; j<k; ++j)
124 b[j] = t0*b[j] + t1*b[j+1];
125 }
126
127 _curve0.push_back(b[0]);
128 tmp.push_back(b[0]);
129
130
131 for (i=n; i>=0; --i)
132 _curve1.push_back(tmp[i]);
133
134
135 assert(_curve0.degree() == n);
136 assert(_curve1.degree() == n);
137 }
138
139
140 //=============================================================================
141 } // namespace ACG
142 //=============================================================================
ACG::BezierCurveT
Definition: BezierCurveT.hh:77
ACG
Namespace providing different geometric functions concerning angles.
Definition: BaseObjectData.hh:68
ACG::BezierCurveT::subdivide
void subdivide(Scalar _t, Self &_curve0, Self &_curve1) const
Definition: BezierCurveT_impl.hh:98
ACG::BezierCurveT::degree
unsigned int degree() const
return degree (= size()-1)
Definition: BezierCurveT.hh:94
ACG::BezierCurveT::operator()
Point operator()(Scalar _t) const
evaluate curve at parameter _t using deCasteljau
Definition: BezierCurveT_impl.hh:72