perlin
[vg.git] / vg_m.h
diff --git a/vg_m.h b/vg_m.h
index 4844eeb20b2664eb5521a86065f309f92cb2ce5a..2a5082d9b813c53687fb60254b47b5c141836caa 100644 (file)
--- a/vg_m.h
+++ b/vg_m.h
@@ -269,6 +269,11 @@ static inline void v3_add( v3f a, v3f b, v3f d )
    d[0] = a[0]+b[0]; d[1] = a[1]+b[1]; d[2] = a[2]+b[2];
 }
 
+static inline void v3i_add( v3i a, v3i b, v3i d )
+{
+   d[0] = a[0]+b[0]; d[1] = a[1]+b[1]; d[2] = a[2]+b[2];
+}
+
 static inline void v4_add( v4f a, v4f b, v4f d )
 {
    d[0] = a[0]+b[0]; 
@@ -282,6 +287,11 @@ static inline void v3_sub( v3f a, v3f b, v3f d )
    d[0] = a[0]-b[0]; d[1] = a[1]-b[1]; d[2] = a[2]-b[2];
 }
 
+static inline void v3i_sub( v3i a, v3i b, v3i d )
+{
+   d[0] = a[0]-b[0]; d[1] = a[1]-b[1]; d[2] = a[2]-b[2];
+}
+
 static inline void v3_mul( v3f a, v3f b, v3f d )
 {
    d[0] = a[0]*b[0]; d[1] = a[1]*b[1]; d[2] = a[2]*b[2];
@@ -1302,6 +1312,22 @@ int plane_intersect2( v4f a, v4f b, v3f p, v3f n )
    return 1;
 }
 
+static int plane_segment( v4f plane, v3f a, v3f b, v3f co )
+{
+   float d0 = v3_dot( a, plane ) - plane[3],
+         d1 = v3_dot( b, plane ) - plane[3];
+
+   if( d0*d1 < 0.0f )
+   {
+      float tot = 1.0f/( fabsf(d0)+fabsf(d1) );
+
+      v3_muls( a, fabsf(d1) * tot, co );
+      v3_muladds( co, b, fabsf(d0) * tot, co );
+      return 1;
+   }
+
+   return 0;
+}
 
 static inline double plane_polarity( double p[4], double a[3] )
 {
@@ -1360,8 +1386,7 @@ static inline void q_inv( v4f q, v4f d )
 
 static inline void q_nlerp( v4f a, v4f b, float t, v4f d )
 {
-   if( v4_dot(a,b) < 0.0f )
-   {
+   if( v4_dot(a,b) < 0.0f ){
       v4_muls( b, -1.0f, d );
       v4_lerp( a, d, t, d );
    }
@@ -1455,6 +1480,18 @@ static void m3x3_q( m3x3f m, v4f q )
    }
 }
 
+static void q_mulv( v4f q, v3f v, v3f d )
+{
+   v3f v1, v2;
+
+   v3_muls( q, 2.0f*v3_dot(q,v), v1 );
+   v3_muls( v, q[3]*q[3] - v3_dot(q,q), v2 );
+   v3_add( v1, v2, v1 );
+   v3_cross( q, v, v2 );
+   v3_muls( v2, 2.0f*q[3], v2 );
+   v3_add( v1, v2, d );
+}
+
 enum contact_type
 {
    k_contact_type_default,
@@ -1549,6 +1586,15 @@ VG_STATIC float closest_segment_segment( v3f p1, v3f q1, v3f p2, v3f q2,
    return v3_length2( v0 );
 }
 
+VG_STATIC int point_inside_aabb( boxf box, v3f point )
+{
+   if((point[0]<=box[1][0]) && (point[1]<=box[1][1]) && (point[2]<=box[1][2]) &&
+      (point[0]>=box[0][0]) && (point[1]>=box[0][1]) && (point[2]>=box[0][2]) )
+      return 1;
+   else
+      return 0;
+}
+
 VG_STATIC void closest_point_aabb( v3f p, boxf box, v3f dest )
 {
    v3_maxv( p, box[0], dest );
@@ -1999,6 +2045,7 @@ static int spherecast_triangle( v3f tri[3],
 
 static inline float vg_randf(void)
 {
+   /* TODO: replace with our own rand */
    return (float)rand()/(float)(RAND_MAX);
 }
 
@@ -2036,4 +2083,13 @@ static void eval_bezier_time( v3f p0, v3f p1, v3f h0, v3f h1, float t, v3f p )
    v3_muladds( p, p0, 3.0f*tt  -ttt -3.0f*t +1.0f, p );
 }
 
+static void eval_bezier3( v3f p0, v3f p1, v3f p2, float t, v3f p )
+{
+   float u = 1.0f-t;
+
+   v3_muls( p0, u*u, p );
+   v3_muladds( p, p1, 2.0f*u*t, p );
+   v3_muladds( p, p2, t*t, p );
+}
+
 #endif /* VG_M_H */