Hermite spline english_20161201_jintaeks

18 views

Published on

Hermite Spline implementation from the scratch.

Published in: Engineering
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
18
On SlideShare
0
From Embeds
0
Number of Embeds
10
Actions
Shares
0
Downloads
0
Comments
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

Γ—