32
Mobile Game Programming: Hermite Spline [email protected] Division of Digital Contents, DongSeo University. October 2016

Hermite spline english_20161201_jintaeks

Embed Size (px)

Citation preview

Page 1: Hermite spline english_20161201_jintaeks

Mobile Game Programming:

Hermite [email protected]

Division of Digital Contents, DongSeo University.October 2016

Page 2: Hermite spline english_20161201_jintaeks

Type of functionsExplicit functiony=or y=-

Implicit functionx2+y2=R2

Parametric representationx=Rcosθ, y=Rsinθ (0≤ θ < 2π)

2

Page 3: Hermite spline english_20161201_jintaeks

Order of continuity The various order of parametric continuity can be de-

scribed as follows:[5]

C−1: curves include discontinuities C0: curves are joined C1: first derivatives are continuous C2: first and second derivatives are continuous Cn: first through nth derivatives are continuous

3

Page 4: Hermite spline english_20161201_jintaeks

Spline In mathematics, a spline is a numeric 

function that is piecewise-defined by polynomial functions.– A flexible device which can be bent to the

desired shape is known as a flat spline. In interpolation problems, 

spline interpolation is often preferred to polynomial interpolation because it yields similar results to interpolating with higher degree polynomials while avoiding instability.

The most commonly used splines are cubic splines.

4

Page 5: Hermite spline english_20161201_jintaeks

Ex) Quadratic spline

5

Page 6: Hermite spline english_20161201_jintaeks

Ex) Cubic spline

6

Page 7: Hermite spline english_20161201_jintaeks

Definition of spline A spline is a piecewise-polynomial real function

S:[a,b] R on an interval [a,b] composed of k subintervals [t i-

1,ti] witha=t0<t1< … <tk-1<tk=b.

The restriction of S to an interval i is a polynomialPi:[ti-1,ti]R,

so thatS(t)=P1(t), t0<= t <= t1,S(t)=P2(t), t1<= t <= t2,…S(t)=Pk(t), tk-1<= t <= tk,

The highest order of the polynomials Pi(t) is said to be the order of the spline S.

7

Page 8: Hermite spline english_20161201_jintaeks

Cubic spline In numerical analysis, a cubic spline is a spline where

each piece is a third-degree polynomial. A cubic Hermite spline Specified by its values and

first derivatives at the end points of the corresponding domain interval.

8

Page 9: Hermite spline english_20161201_jintaeks

Unit interval (0,1) On the interval (0,1), given a starting point p0 at t = 0

and an ending point p1 at t = 1 with starting tangent m0 at t = 0 and ending tangent m1 at t = 1, the polynomial can be defined by

where t ∈ [0,1].

9

Page 10: Hermite spline english_20161201_jintaeks

a point Q(t) on a curve at parameter t can be defined a cubic equations for each value:

Q(t)== We can define coefficient matrix C and parameter matrix

T, then Q(t) can be defined like this:T=C=Q(t)==

10

Page 11: Hermite spline english_20161201_jintaeks

We will decompose C with 2×4 Geometry Matrix G and 4×4 Basis Matrix M.

C=G‧MQ(t)==M‧T=

11

Page 12: Hermite spline english_20161201_jintaeks

Geometry matrix G has an information about control data. in case of Hermite spline, it has two points and tangent

vectors for each point.– P1, P2, R1, R2

Base matrix M a coefficient matrix for the blending functions. blending function means M‧T. the summation of all blending functions is 1.

– it means that spline curve calculates the weighted average for control points.

12

Page 13: Hermite spline english_20161201_jintaeks

Hermite Spline Specified by its values and first derivatives at the end

points of the corresponding domain interval

Geometry matrix GG=

13

Page 14: Hermite spline english_20161201_jintaeks

Find Base matrix M Four conditions to find Base matrix M. Begin point is (P1x, P1y), and End point is (P4x, P4y).

Q(0)=Q(1)=

Derivative at Begin point is (R1x, R1y), and derivative at End point is (R4x, R4y).Q'(0)=Q'(1)=

14

Page 15: Hermite spline english_20161201_jintaeks

for two end points Q(0)=

= Q(1)==

15

Page 16: Hermite spline english_20161201_jintaeks

for two tangent vector Q'(0)=

= Q'(1)==

16

Page 17: Hermite spline english_20161201_jintaeks

Base matrix M = -1

=M-1

M=

17

Page 18: Hermite spline english_20161201_jintaeks

Blending function Q(t)=

+(-2t3+3t2)+(t3-2t2+1)+(t3-t2).

18

[2 −3 0 1−2 3 0 01 −2 1 01 −1 0 0 ] [

𝑡 3𝑡 2𝑡 10 ]

Page 19: Hermite spline english_20161201_jintaeks

Meaning of tangent vector R

19

P1 P4

R4

R1

Page 20: Hermite spline english_20161201_jintaeks

Implement Hermite Splineclass KHermiteCurve{public: float m_a; float m_b; float m_c; float m_d;

20

Page 21: Hermite spline english_20161201_jintaeks

class KHermiteCurve{public: /// constructor. /// @param p1: begin point /// @param p2: end point /// @param v1: tangent vector at begin point /// @param v2: tangent vector at end point KHermiteCurve(float p1, float p2, float v1, float v2) { m_d = p1; m_c = v1; m_b = -3.0f*p1 + 3.0f*p2 - 2.0f*v1 - v2; m_a = 2.0f*p1 - 2.0f*p2 + v1 + v2; }//KHermiteCurve()

21

Page 22: Hermite spline english_20161201_jintaeks

/// set coefficient of Hermite curve inline void Construct(float p1, float p2, float v1, float v2) { m_d = p1; m_c = v1; m_b = -3.0f*p1 + 3.0f*p2 - 2.0f*v1 - v2; m_a = 2.0f*p1 - 2.0f*p2 + v1 + v2; }//Construct()

/// calculate first derivative on parameter u. /// can be used to get tangent vector at u. /// derivative of equation t^3 + t^2 + t + 1 inline float CalculateDxDu(float u) const { return 3.0f*m_a*u*u + 2.0f*m_b*u + m_c; }//CalculateDxDu()

22

Page 23: Hermite spline english_20161201_jintaeks

/// get value at parameter u. inline float CalculateX(float u) const { float uu, uuu; uu = u * u; uuu = uu * u; return m_a*uuu + m_b*uu + m_c*u + m_d; }//CalculateX()

23

Page 24: Hermite spline english_20161201_jintaeks

KHermiteSpline2class KHermiteSpline2{private: KHermiteCurve m_aHermiteCurve[2];

public: /// constructor. KHermiteSpline2(){} KHermiteSpline2( const KVector& p0, const KVector& p1, const KVector& dp0, const KVector& dp1 ) { m_aHermiteCurve[ 0 ].Construct( p0.m_x, p1.m_x, dp0.m_x, dp1.m_x ); m_aHermiteCurve[ 1 ].Construct( p0.m_y, p1.m_y, dp0.m_y, dp1.m_y ); }//KHermiteSpline2()

24

Page 25: Hermite spline english_20161201_jintaeks

inline void Construct( const KVector& p0, const KVector& p1, const KVector& dp0, const KVector& dp1 ) { m_aHermiteCurve[ 0 ].Construct( p0.m_x, p1.m_x, dp0.m_x, dp1.m_x ); m_aHermiteCurve[ 1 ].Construct( p0.m_y, p1.m_y, dp0.m_y, dp1.m_y ); }//Construct()

25

Page 26: Hermite spline english_20161201_jintaeks

inline const KVector GetPosition( float u_ ) const { return KVector( m_aHermiteCurve[ 0 ].CalculateX( u_ ), m_aHermiteCurve[ 1 ].CalculateX( u_ ), 0.f ); }//GetPosition()

inline const KVector GetTangent( float u_ ) const { return KVector( m_aHermiteCurve[ 0 ].CalculateDxDu( u_ ), m_aHermiteCurve[ 1 ].CalculateDxDu( u_ ), 0.f ); }//GetTangent()

26

Page 27: Hermite spline english_20161201_jintaeks

Draw a tangent line at u const float u = 0.5f; KVector position = spline.GetPosition(u); KVector tangent = spline.GetTangent(u); DrawLine(hdc, position, position + tangent * 100.f);

27

Page 28: Hermite spline english_20161201_jintaeks

Combine Splines

28

Page 29: Hermite spline english_20161201_jintaeks

Line in 3D space Given two 3D points P1 and P2, we can define the line

that passes through these points parametrically asP(t ) = (1− t )P1 + tP2

A ray is a line having a single endpoint S and extending to infinity in a given direction V. Rays are typically ex-pressed by the parametric equation

P(t ) = S + tV Note that this equation is equivalent to previous equa-

tion if we let S = P1 and V = P2 − P1.

29

Page 30: Hermite spline english_20161201_jintaeks

Distance between a point and a line

30

Page 31: Hermite spline english_20161201_jintaeks

Spline path tracing demo

31

Page 32: Hermite spline english_20161201_jintaeks