My Project
Loading...
Searching...
No Matches
CBezier.h
Go to the documentation of this file.
1//==============================================================================
2/*
3 Software License Agreement (BSD License)
4 Copyright (c) 2003-2024, CHAI3D
5 (www.chai3d.org)
6
7 All rights reserved.
8
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions
11 are met:
12
13 * Redistributions of source code must retain the above copyright
14 notice, this list of conditions and the following disclaimer.
15
16 * Redistributions in binary form must reproduce the above
17 copyright notice, this list of conditions and the following
18 disclaimer in the documentation and/or other materials provided
19 with the distribution.
20
21 * Neither the name of CHAI3D nor the names of its contributors may
22 be used to endorse or promote products derived from this software
23 without specific prior written permission.
24
25 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 POSSIBILITY OF SUCH DAMAGE.
37
38 \author <http://www.chai3d.org>
39 \author Francois Conti
40 \version 3.3.0
41*/
42//==============================================================================
43
44//------------------------------------------------------------------------------
45#ifndef CBezierH
46#define CBezierH
47//------------------------------------------------------------------------------
48#include "math/CMaths.h"
49//------------------------------------------------------------------------------
50
51//------------------------------------------------------------------------------
52namespace chai3d {
53//------------------------------------------------------------------------------
54
55//==============================================================================
63//==============================================================================
64
65//------------------------------------------------------------------------------
69//------------------------------------------------------------------------------
70
72
73//==============================================================================
87//==============================================================================
88inline cVector3d cEvalBezierCurve(const cVector3d *a_controlPoints, double a_t)
89{
90 const cVector3d *P = a_controlPoints;
91 const double t = a_t;
92
93 double b0 = (1 - t) * (1 - t) * (1 - t);
94 double b1 = 3 * t * (1 - t) * (1 - t);
95 double b2 = 3 * t * t * (1 - t);
96 double b3 = t * t * t;
97
98 return (P[0] * b0 + P[1] * b1 + P[2] * b2 + P[3] * b3);
99}
100
101
102//==============================================================================
117//==============================================================================
118inline cVector3d cEvalBezierPatch(const cVector3d *a_controlPoints, double a_u, double a_v)
119{
120 cVector3d uCurve[4];
121 for (int i = 0; i < 4; ++i)
122 uCurve[i] = cEvalBezierCurve(a_controlPoints + 4 * i, a_u);
123
124 return (cEvalBezierCurve(uCurve, a_v));
125}
126
127
128//==============================================================================
142//==============================================================================
143inline cVector3d cDerivBezier(const cVector3d *a_controlPoints, double a_t)
144{
145 const cVector3d *P = a_controlPoints;
146 const double t = a_t;
147
148 return ((-3 * (1 - t) * (1 - t) * P[0] +
149 (3 * (1 - t) * (1 - t) - 6 * t * (1 - t)) * P[1] +
150 (6 * t * (1 - t) - 3 * t * t) * P[2] +
151 3 * t * t * P[3]));
152}
153
154
155//==============================================================================
171//==============================================================================
172inline cVector3d cDerivUBezier(const cVector3d *a_controlPoints, double a_u, double a_v)
173{
174 cVector3d P[4];
175 cVector3d vCurve[4];
176 for (int i = 0; i < 4; ++i) {
177 P[0] = a_controlPoints[i];
178 P[1] = a_controlPoints[4 + i];
179 P[2] = a_controlPoints[8 + i];
180 P[3] = a_controlPoints[12 + i];
181 vCurve[i] = cEvalBezierCurve(P, a_v);
182 }
183
184 return (cDerivBezier(vCurve, a_u));
185}
186
187
188//==============================================================================
204//==============================================================================
205inline cVector3d cDerivVBezier(const cVector3d *a_controlPoints, double a_u, double a_v)
206{
207 cVector3d uCurve[4];
208 for (int i = 0; i < 4; ++i) {
209 uCurve[i] = cEvalBezierCurve(a_controlPoints + 4 * i, a_u);
210 }
211
212 return cDerivBezier(uCurve, a_v);
213}
214
215
216//==============================================================================
231//==============================================================================
232inline cVector3d cSurfaceNormalBezier(const cVector3d *a_controlPoints, double a_u, double a_v)
233{
234 cVector3d du = cDerivUBezier(a_controlPoints, a_u, a_v);
235 cVector3d dv = cDerivVBezier(a_controlPoints, a_u, a_v);
236
237 cVector3d normal = cCross(du, dv);
238 normal.normalize();
239
240 return (normal);
241}
242
243
245
246//------------------------------------------------------------------------------
247} // namespace chai3d
248//------------------------------------------------------------------------------
249
250//------------------------------------------------------------------------------
251#endif // CBezierH
252//------------------------------------------------------------------------------
Implements general math utility functions.
Definition CAudioBuffer.cpp:57
cVector3d cCross(const cVector3d &a_vector1, const cVector3d &a_vector2)
This function computes the cross product between two vectors.
Definition CMaths.h:944
cVector3d cEvalBezierPatch(const cVector3d *a_controlPoints, double a_u, double a_v)
This function determines the position of a point along a Bezier patch.
Definition CBezier.h:118
cVector3d cEvalBezierCurve(const cVector3d *a_controlPoints, double a_t)
This function determines the position of a point along a Bezier curve.
Definition CBezier.h:88
cVector3d cSurfaceNormalBezier(const cVector3d *a_controlPoints, double a_u, double a_v)
This function determines the surface normal at a point on a Bezier patch.
Definition CBezier.h:232
cVector3d cDerivUBezier(const cVector3d *a_controlPoints, double a_u, double a_v)
This function determines the derivative of a point on a Bezier patch along the u parametric direction...
Definition CBezier.h:172
cVector3d cDerivVBezier(const cVector3d *a_controlPoints, double a_u, double a_v)
This function determines the derivative of a point on a Bezier patch along the v parametric direction...
Definition CBezier.h:205
cVector3d cDerivBezier(const cVector3d *a_controlPoints, double a_t)
This function determines the derivative of a point along a Bezier curve.
Definition CBezier.h:143
This class implements a 3D vector.
Definition CVector3d.h:89
void normalize()
This method normalizes this vector to length 1.
Definition CVector3d.h:1034