KrisLibrary  1.0.0
d/interpolate.h
1 #ifndef MATH3D_INTERPOLATE_H
2 #define MATH3D_INTERPOLATE_H
3 
4 #include "primitives.h"
6 
7 namespace Math3D {
8 
9 inline void interpolate(const Vector2& a, const Vector2& b, Real u, Vector2& x)
10 {
11  x.mul(a,One-u);
12  x.madd(b,u);
13 }
14 
15 inline void interpolate(const Vector3& a, const Vector3& b, Real u, Vector3& x)
16 {
17  x.mul(a,One-u);
18  x.madd(b,u);
19 }
20 
21 inline void interpolate(const Vector4& a, const Vector4& b, Real u, Vector4& x)
22 {
23  x.mul(a,One-u);
24  x.madd(b,u);
25 }
26 
27 /*
28 
29 inline void interpolate(const Matrix2& a, const Matrix2& b, Real u, Matrix2& x)
30 {
31  x.mul(a,One-u);
32  x.madd(b,u);
33 }
34 
35 inline void interpolate(const Matrix3& a, const Matrix3& b, Real u, Matrix3& x)
36 {
37  x.mul(a,One-u);
38  x.madd(b,u);
39 }
40 
41 inline void interpolate(const Matrix4& a, const Matrix4& b, Real u, Matrix4& x)
42 {
43  x.mul(a,One-u);
44  x.madd(b,u);
45 }
46 
47 */
48 
49 void interpolateRotation(const Matrix2& a, const Matrix2& b, Real u, Matrix2& x);
50 
51 void interpolateRotation(const Matrix3& a, const Matrix3& b, Real u, Matrix3& x);
52 
53 inline void interpolate(const RigidTransform2D& a, const RigidTransform2D& b, Real u, RigidTransform2D& x)
54 {
55  interpolate(a.t,b.t,u,x.t);
56  interpolateRotation(a.R,b.R,u,x.R);
57 }
58 
59 inline void interpolate(const RigidTransform& a, const RigidTransform& b, Real u, RigidTransform& x)
60 {
61  interpolate(a.t,b.t,u,x.t);
62  interpolateRotation(a.R,b.R,u,x.R);
63 }
64 
65 //interpolateDirection assumes that a and b are normalized direction vectors
66 //and are not on opposite sides of the sphere
67 template <class VectorT>
68 void interpolateDirection(const VectorT& a,const VectorT& b,Real u,VectorT& out)
69 {
70  Real d = dot(a,b);
71  if(d == One) { //axes are the same axis
72  out.set(b);
73  return;
74  }
75  else if(d == -One) { //axes are opposing axis
76  interpolate(a,b,u,out);
77  return;
78  }
79 
80  Real theta = Acos(d);
81  Real sininv = Sin(theta);
82  sininv = One/sininv;
83 
84  //out = (Sin((One-t)*theta)*sininv) * a + (Sin(t*theta)*sininv) * b;
85  Real a_coeff = Sin((One-u)*theta)*sininv;
86  Real b_coeff = Sin(u*theta)*sininv;
87  out.mul(a, a_coeff);
88  out.madd(b, b_coeff);
89 }
90 
92 inline Real SegmentZeroCrossing(Real a,Real b)
93 {
94  if(a == b) return Zero;
95  return a/(a-b);
96 }
97 
99 inline Real SegmentCrossing(Real a,Real b,Real x0)
100 {
101  if(a == b) return Zero;
102  return (a-x0)/(a-b);
103 }
104 
105 } //namespace Math3D
106 
107 #endif
Real SegmentCrossing(Real a, Real b, Real x0)
if segment is x = a + u*(b-a), returns the value u s.t. x=x0
Definition: d/interpolate.h:99
Class declarations for useful 3D math types.
Contains all the definitions in the Math3D package.
Definition: AnyGeometry.h:13
Real SegmentZeroCrossing(Real a, Real b)
if segment is x = a + u*(b-a), returns the value u s.t. x=0
Definition: d/interpolate.h:92
Convenience functions for linear interpolation of vectors and matrices.