Developer Documentation

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 #define ANIMATIONHELPER_CC
45
46 #include "AnimationHelper.hh"
47 #include <cmath>
48 #include <cassert>
49
50 #define APPROXIMATION_EPSILON 1.0e-09
51 #define VERYSMALL 1.0e-20
52 #define MAXIMUM_ITERATIONS 1000
53
54 template<typename Scalar>
55 Scalar AnimationHelper::clampTo01Interval(Scalar _value) {
56 if (_value < 0.0)
57 return 0.0;
58 else if (_value > 1.0)
59 return 1.0;
60 else
61 return _value;
62 }
63
64 template<typename Scalar>
65 Scalar AnimationHelper::abs(Scalar _value) {
66 if (_value < 0.0)
67 return _value * -1.0;
68 else
69 return _value;
70 }
71
72 template<typename Scalar>
73 float AnimationHelper::approximateCubicBezierParameter(Scalar _atX, Scalar _P0X, Scalar _P1X, Scalar _C0X, Scalar _C1X) {
74 if (_atX - _P0X < VERYSMALL)
75 return 0.0;
76
77 if (_P1X - _atX < VERYSMALL)
78 return 1.0;
79
80 long iterationStep = 0;
81
82 float u = 0.0f; float v = 1.0f;
83
84 //iteratively apply subdivision to approach value atX
85 while (iterationStep < MAXIMUM_ITERATIONS) {
86
87 // de Casteljau Subdivision.
88 Scalar a = (_P0X + _C0X)*0.5;
89 Scalar b = (_C0X + _C1X)*0.5;
90 Scalar c = (_C1X + _P1X)*0.5;
91 Scalar d = (a + b)*0.5;
92 Scalar e = (b + c)*0.5;
93 Scalar f = (d + e)*0.5; //this one is on the curve!
94
95 //The curve point is close enough to our wanted atX
96 if (abs<Scalar>(f - _atX) < APPROXIMATION_EPSILON)
97 return clampTo01Interval<Scalar>((u + v)*0.5f);
98
99 //dichotomy
100 if (f < _atX) {
101 _P0X = f;
102 _C0X = e;
103 _C1X = c;
104 u = (u + v)*0.5f;
105 } else {
106 _C0X = a; _C1X = d; _P1X = f; v = (u + v)*0.5f;
107 }
108
109 iterationStep++;
110 }
111
112 return clampTo01Interval<Scalar>((u + v)*0.5f);
113 }
114
115 template<typename Scalar>
116 std::vector<Scalar> AnimationHelper::evaluateBezier(float at, std::vector<Scalar> _P0, std::vector<Scalar> _P1, std::vector<Scalar> _C0, std::vector<Scalar> _C1) {
117 unsigned int size = _P0.size();
118 assert(size == _P1.size() && size == _C0.size() && size == _C1.size());
119
120 float s = at;
121 float sinv = (1-s);
122
123 std::vector<Scalar> result;
124
125 for (unsigned int i=0;i<size;++i) {
126 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 );
127 }
128
129 return result;
130 }
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:116
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:73