Developer Documentation

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
AnimationHelper.cc
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 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49
50 #define ANIMATIONHELPER_CC
51
52 #include "AnimationHelper.hh"
53 #include <cmath>
54 #include <cassert>
55
56 #define APPROXIMATION_EPSILON 1.0e-09
57 #define VERYSMALL 1.0e-20
58 #define MAXIMUM_ITERATIONS 1000
59
60 template<typename Scalar>
61 Scalar AnimationHelper::clampTo01Interval(Scalar _value) {
62 if (_value < 0.0)
63 return 0.0;
64 else if (_value > 1.0)
65 return 1.0;
66 else
67 return _value;
68 }
69
70 template<typename Scalar>
71 Scalar AnimationHelper::abs(Scalar _value) {
72 if (_value < 0.0)
73 return _value * -1.0;
74 else
75 return _value;
76 }
77
78 template<typename Scalar>
79 float AnimationHelper::approximateCubicBezierParameter(Scalar _atX, Scalar _P0X, Scalar _P1X, Scalar _C0X, Scalar _C1X) {
80 if (_atX - _P0X < VERYSMALL)
81 return 0.0;
82
83 if (_P1X - _atX < VERYSMALL)
84 return 1.0;
85
86 long iterationStep = 0;
87
88 float u = 0.0f; float v = 1.0f;
89
90 //iteratively apply subdivision to approach value atX
91 while (iterationStep < MAXIMUM_ITERATIONS) {
92
93 // de Casteljau Subdivision.
94 Scalar a = (_P0X + _C0X)*0.5;
95 Scalar b = (_C0X + _C1X)*0.5;
96 Scalar c = (_C1X + _P1X)*0.5;
97 Scalar d = (a + b)*0.5;
98 Scalar e = (b + c)*0.5;
99 Scalar f = (d + e)*0.5; //this one is on the curve!
100
101 //The curve point is close enough to our wanted atX
102 if (abs<Scalar>(f - _atX) < APPROXIMATION_EPSILON)
103 return clampTo01Interval<Scalar>((u + v)*0.5f);
104
105 //dichotomy
106 if (f < _atX) {
107 _P0X = f;
108 _C0X = e;
109 _C1X = c;
110 u = (u + v)*0.5f;
111 } else {
112 _C0X = a; _C1X = d; _P1X = f; v = (u + v)*0.5f;
113 }
114
115 iterationStep++;
116 }
117
118 return clampTo01Interval<Scalar>((u + v)*0.5f);
119 }
120
121 template<typename Scalar>
122 std::vector<Scalar> AnimationHelper::evaluateBezier(float at, std::vector<Scalar> _P0, std::vector<Scalar> _P1, std::vector<Scalar> _C0, std::vector<Scalar> _C1) {
123 unsigned int size = _P0.size();
124 assert(size == _P1.size() && size == _C0.size() && size == _C1.size());
125
126 float s = at;
127 float sinv = (1-s);
128
129 std::vector<Scalar> result;
130
131 for (unsigned int i=0;i<size;++i) {
132 result.push_back( _P0[i]*sinv*sinv*sinv + 3*_C0[i]*s*sinv*sinv + 3*_C1[i]*s*s*sinv + _P1[i]*s*s*s );
133 }
134
135 return result;
136 }
AnimationHelper::approximateCubicBezierParameter
static float approximateCubicBezierParameter(Scalar _atX, Scalar _P0X, Scalar _P1X, Scalar _C0X, Scalar _C1X)
Approximates the parameter value for a Bezier curve to get a certain x value Does an iterative DeCast...
Definition: AnimationHelper.cc:79
AnimationHelper::evaluateBezier
static std::vector< Scalar > evaluateBezier(float at, std::vector< Scalar > _P0, std::vector< Scalar > _P1, std::vector< Scalar > _C0, std::vector< Scalar > _C1)
Evaluates the cubic Bezier curve parameterized by P0, P1, C0 and C1 at the parameter value "at"...
Definition: AnimationHelper.cc:122