Upcoming SlideShare
×

# Hermite spline english_20161201_jintaeks

18 views

Published on

Hermite Spline implementation from the scratch.

Published in: Engineering
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
18
On SlideShare
0
From Embeds
0
Number of Embeds
10
Actions
Shares
0
0
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Hermite spline english_20161201_jintaeks

1. 1. Mobile Game Programming: Hermite Spline jintaeks@dongseo.ac.kr Division of Digital Contents, DongSeo University. October 2016
2. 2. Type of functions Explicit function y= 2 π2 β π₯2 or y=- 2 π2 β π₯2 Implicit function x2+y2=R2 Parametric representation x=RcosΞΈ, y=RsinΞΈ (0β€ ΞΈ < 2Ο) 2
3. 3. Order of continuity οΌ The various order of parametric continuity can be described 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
4. 4. 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
5. 5. Ex) Quadratic spline 5
6. 6. Ex) Cubic spline 6
7. 7. Definition of spline οΌ A spline is a piecewise-polynomial real function S:[a,b] ο  R οΌ on an interval [a,b] composed of k subintervals [ti-1,ti] with a=t0<t1< β¦ <tk-1<tk=b. οΌ The restriction of S to an interval i is a polynomial Pi:[ti-1,ti]ο R, οΌ so that S(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
8. 8. 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
9. 9. 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
10. 10. οΌ a point Q(t) on a curve at parameter t can be defined a cubic equations for each value: Q(t)= π₯(π‘) π¦(π‘) = π π₯ π‘3 + ππ₯π‘2 + ππ₯π‘ + ππ¦ π π¦ π‘3 + ππ¦π‘2 + ππ¦π‘ + ππ¦ οΌ We can define coefficient matrix C and parameter matrix T, then Q(t) can be defined like this: T= π‘3 π‘2 π‘ 1 C= π π₯ π π₯ π π₯ π π₯ π π¦ π π¦ π π¦ π π¦ Q(t)= π₯(π‘) π¦(π‘) =πΆβ§π 10
11. 11. οΌ We will decompose C with 2Γ4 Geometry Matrix G and 4Γ4 Basis Matrix M. C=Gβ§M Q(t)= π₯(π‘) π¦(π‘) =πͺβ§π = πΊβ§Mβ§T = π1π₯ π2π₯ π3π₯ π4π₯ π1π¦ π2π¦ π3π¦ π4π¦ π11 π12 π13 π14 π21 π22 π23 π24 π31 π32 π33 π34 π41 π42 π43 π44 π‘3 π‘2 π‘ 1 11
12. 12. 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
13. 13. Hermite Spline οΌ Specified by its values and first derivatives at the end points of the corresponding domain interval Geometry matrix G G= π1π₯ π4π₯ π1π₯ π4π₯ π1π¦ π4π¦ π1π¦ π4π¦ 13
14. 14. Find Base matrix M οΌ Four conditions to find Base matrix M. οΌ Begin point is (P1x, P1y), and End point is (P4x, P4y). Q(0)= π1π₯ π1π¦ Q(1)= π4π₯ π4π¦ οΌ Derivative at Begin point is (R1x, R1y), and derivative at End point is (R4x, R4y). Q'(0)= π1π₯ π1π¦ Q'(1)= π4π₯ π4π¦ 14
15. 15. for two end points οΌ Q(0)= π1π₯ π1π¦ = πΊβ§Mβ§T = π1π₯ π4π₯ π1π₯ π4π₯ π1π¦ π4π¦ π1π¦ π4π¦ π11 π12 π13 π14 π21 π22 π23 π24 π31 π32 π33 π34 π41 π42 π43 π44 π‘3 π‘2 π‘ 1 =πΊβ§M 0 0 0 1 οΌ Q(1)= π4π₯ π4π¦ = πΊβ§M 1 1 1 1 15
16. 16. for two tangent vector οΌ Q'(0)= π1π₯ π1π¦ = πΊβ§Mβ§Tβ² = π1π₯ π4π₯ π1π₯ π4π₯ π1π¦ π4π¦ π1π¦ π4π¦ π11 π12 π13 π14 π21 π22 π23 π24 π31 π32 π33 π34 π41 π42 π43 π44 3π‘2 2π‘ 1 0 =πΊβ§M 0 0 1 0 οΌ Q'(1)= π4π₯ π4π¦ = πΊβ§M 3 2 1 0 16
17. 17. Base matrix M οΌ π1π₯ π4π₯ π1π₯ π4π₯ π1π¦ π4π¦ π1π¦ π4π¦ = πΊ = πΊβ§π 0 1 0 3 0 1 0 2 0 1 1 1 1 1 0 0 = πΊβ§πβ§π-1 0 1 0 3 0 1 0 2 0 1 1 1 1 1 0 0 =M-1 οΌ M= 2 β3 0 1 β2 3 0 0 1 β2 1 0 1 β1 0 0 17
18. 18. Blending function οΌ Q(t)= π₯(π‘) π¦(π‘) = πΊβ§Mβ§T = πΊβ§π΅ =(2t3β3t2+1) π1π₯ π1π¦ +(-2t3+3t2) π4π₯ π4π¦ +(t3-2t2+1) π1π₯ π1π¦ +(t3-t2) π4π₯ π4π¦ . 18 2 β3 0 1 β2 3 0 0 1 β2 1 0 1 β1 0 0 π‘3 π‘2 π‘1 0
19. 19. Meaning of tangent vector R 19 P1 P4 R4 R1
20. 20. Implement Hermite Spline class KHermiteCurve { public: float m_a; float m_b; float m_c; float m_d; 20
21. 21. 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
22. 22. /// 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
23. 23. /// 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
24. 24. KHermiteSpline2 class 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
25. 25. 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
26. 26. 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
27. 27. 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
28. 28. Combine Splines 28
29. 29. Line in 3D space οΌ Given two 3D points P1 and P2, we can define the line that passes through these points parametrically as P(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 expressed by the parametric equation P(t ) = S + tV οΌ Note that this equation is equivalent to previous equation if we let S = P1 and V = P2 β P1. 29
30. 30. Distance between a point and a line 30
31. 31. Spline path tracing demo 31