OpenGL VRML W3D            
  Home Tutorials Books Applications Tools Docs Models Textures  

 

              Bugs

3D Maths for CG   

  3D Maths for CG

Index

Vectors
Cross Product
Dot Product
Vector Projection

Lines Rays and Planes
Lines and Rays
Planes

Intersection
Ray Sphere Intersecion
Ray Triangle Intersection

Interpolation
Catmull-Rom Spline


Google

OpenGLTutorials @ Lighthouse3d.com

Led Shader
View Frustum Culling
GLSL Tutorial
Maths Tutorial
Billboarding Tutorial
Picking Tutorial
Terrain Tutorial
Display Lists Tutorial
GLUT Tutorial

   
[Previous: Ray Triangle Intersection] [Next]

3D Maths for CG


Interpolation - Catmull-Rom Spline


A cubic spline. In the current context we're using it for interpolation purposes in 1-D. The Catmull-Rom spline takes a set of keyframe values to describe a smooth piecewise cubic curve that passes through all the keys. In order to use this routine we need four keyframe values. So we have v0,v1,v2, and v3, plus a parameter that specifies a key, x between v1 and v2 to interpolate. The return value, f(x) is the value associated with the key x.


    	f(x) = [1, x, x^2, x^3] * M * [v0, v1, v2, v3] 


where M is defined as:

The image bellow shows an example of a curve between v1 and v2. The curve was obtained calling the function bellow varying x between 0.0 and 1.0.


    
	for (x=0;x<=1;x+=0.01)
		printf("%f\n",catmullRomSpline(x,v0,v1,v2,v3));



The following C code performs the required operation. The return value is the value for the parameter key x. The remaining parameters are the four values as mentione above. Note that x is assumed to be between v1 and v2 . The value for x must be between 0.0 and 1.0. If x = 0.0 then the return value is v1. For x = 1.0, the return value is v2.


    
/* Coefficients for Matrix M */
#define M11	 0.0	
#define M12	 1.0
#define M13	 0.0
#define M14	 0.0
#define M21	-0.5
#define M22	 0.0
#define M23	 0.5
#define M24	 0.0
#define M31	 1.0
#define M32	-2.5
#define M33	 2.0
#define M34	-0.5
#define M41	-0.5
#define M42	 1.5
#define M43	-1.5
#define M44	 0.5

double catmullRomSpline(float x, float v0,float v1,
				float v2,float v3) {

	double c1,c2,c3,c4;

	c1 =  	      M12*v1;
	c2 = M21*v0          + M23*v2;
	c3 = M31*v0 + M32*v1 + M33*v2 + M34*v3;
	c4 = M41*v0 + M42*v1 + M43*v2 + M44*v3;

	return(((c4*x + c3)*x +c2)*x + c1);
}



Further information about the formulas can be found at On-Line Geometric Modeling Notes.

[Previous: Ray Triangle Intersection] [Next]

       


Site designed and maintained by António Ramires Fernandes
Your comments, suggestions and references to further material are welcome!

Lighthouse 3D privacy statement