* 5.c Closest points
* 5.d Raycast & Spherecasts
* 5.e Curves
+ * 5.f Volumes
* 6. Statistics
* 6.a Random numbers
**/
return ((vg_ftu32(a)) & 0x7F800000U) != 0x7F800000U;
}
+static int v3_valid( v3f a ){
+ for( u32 i=0; i<3; i++ )
+ if( !vg_validf(a[i]) ) return 0;
+ return 1;
+}
+
/*
* -----------------------------------------------------------------------------
* Section 1. Scalar Operations
v3_muls( a, 1.f / v3_length( a ), a );
}
-static inline f32 vg_lerpf( f32 a, f32 b, f32 t )
-{
+static inline f32 vg_lerpf( f32 a, f32 b, f32 t ){
return a + t*(b-a);
}
return a + t*(b-a);
}
+static inline void vg_slewf( f32 *a, f32 b, f32 speed ){
+ f32 d = vg_signf( b-*a ),
+ c = *a + d*speed;
+ *a = vg_minf( b*d, c*d ) * d;
+}
+
+static inline f32 vg_smoothstepf( f32 x ){
+ return x*x*(3.0f - 2.0f*x);
+}
+
+
/* correctly lerp around circular period -pi -> pi */
static f32 vg_alerpf( f32 a, f32 b, f32 t )
{
v3_add( v1, v2, d );
}
+static void v3_tangent_basis( v3f n, v3f tx, v3f ty ){
+ /* Compute tangent basis (box2d) */
+ if( fabsf( n[0] ) >= 0.57735027f ){
+ tx[0] = n[1];
+ tx[1] = -n[0];
+ tx[2] = 0.0f;
+ }
+ else{
+ tx[0] = 0.0f;
+ tx[1] = n[2];
+ tx[2] = -n[1];
+ }
+
+ v3_normalize( tx );
+ v3_cross( n, tx, ty );
+}
+
+
/*
* -----------------------------------------------------------------------------
* Section 2.c 4D Vectors
static inline void q_normalize( v4f q )
{
- f32 s = 1.0f/ sqrtf(v4_dot(q,q));
- q[0] *= s;
- q[1] *= s;
- q[2] *= s;
- q[3] *= s;
+ f32 l2 = v4_dot(q,q);
+ if( l2 < 0.00001f ) q_identity( q );
+ else {
+ f32 s = 1.0f/sqrtf(l2);
+ q[0] *= s;
+ q[1] *= s;
+ q[2] *= s;
+ q[3] *= s;
+ }
}
static inline void q_inv( v4f q, v4f d )
d[3] = q[3]*s;
}
-static inline void q_nlerp( v4f a, v4f b, f32 t, v4f d )
-{
+static inline void q_nlerp( v4f a, v4f b, f32 t, v4f d ){
if( v4_dot(a,b) < 0.0f ){
- v4_muls( b, -1.0f, d );
- v4_lerp( a, d, t, d );
+ v4f c;
+ v4_muls( b, -1.0f, c );
+ v4_lerp( a, c, t, d );
}
else
v4_lerp( a, b, t, d );
m3x3_q( rot, q );
}
-static void m4x3_expand_aabb_point( m4x3f m, boxf box, v3f point )
-{
+static void m4x3_expand_aabb_point( m4x3f m, boxf box, v3f point ){
v3f v;
m4x3_mulv( m, point, v );
v3_maxv( box[1], v, box[1] );
}
-static void m4x3_transform_aabb( m4x3f m, boxf box )
-{
+static void m4x3_expand_aabb_aabb( m4x3f m, boxf boxa, boxf boxb ){
v3f a; v3f b;
-
- v3_copy( box[0], a );
- v3_copy( box[1], b );
- v3_fill( box[0], INFINITY );
- v3_fill( box[1], -INFINITY );
-
- m4x3_expand_aabb_point( m, box, (v3f){ a[0], a[1], a[2] } );
- m4x3_expand_aabb_point( m, box, (v3f){ a[0], b[1], a[2] } );
- m4x3_expand_aabb_point( m, box, (v3f){ b[0], b[1], a[2] } );
- m4x3_expand_aabb_point( m, box, (v3f){ b[0], a[1], a[2] } );
-
- m4x3_expand_aabb_point( m, box, (v3f){ a[0], a[1], b[2] } );
- m4x3_expand_aabb_point( m, box, (v3f){ a[0], b[1], b[2] } );
- m4x3_expand_aabb_point( m, box, (v3f){ b[0], b[1], b[2] } );
- m4x3_expand_aabb_point( m, box, (v3f){ b[0], a[1], b[2] } );
+ v3_copy( boxb[0], a );
+ v3_copy( boxb[1], b );
+ m4x3_expand_aabb_point( m, boxa, (v3f){ a[0], a[1], a[2] } );
+ m4x3_expand_aabb_point( m, boxa, (v3f){ a[0], b[1], a[2] } );
+ m4x3_expand_aabb_point( m, boxa, (v3f){ b[0], b[1], a[2] } );
+ m4x3_expand_aabb_point( m, boxa, (v3f){ b[0], a[1], a[2] } );
+ m4x3_expand_aabb_point( m, boxa, (v3f){ a[0], a[1], b[2] } );
+ m4x3_expand_aabb_point( m, boxa, (v3f){ a[0], b[1], b[2] } );
+ m4x3_expand_aabb_point( m, boxa, (v3f){ b[0], b[1], b[2] } );
+ m4x3_expand_aabb_point( m, boxa, (v3f){ b[0], a[1], b[2] } );
}
-
static inline void m4x3_lookat( m4x3f m, v3f pos, v3f target, v3f up )
{
v3f dir;
return 0;
}
-static inline void box_init_inf( boxf box )
-{
+static inline void box_init_inf( boxf box ){
v3_fill( box[0], INFINITY );
v3_fill( box[1], -INFINITY );
}
v3_muladds( p, p2, t*t, p );
}
+/*
+ * -----------------------------------------------------------------------------
+ * Section 5.f Volumes
+ * -----------------------------------------------------------------------------
+ */
+
+static float vg_sphere_volume( float radius ){
+ float r3 = radius*radius*radius;
+ return (4.0f/3.0f) * VG_PIf * r3;
+}
+
/*
* -----------------------------------------------------------------------------
* Section 6.a PSRNG and some distributions
* Programming," Vol. 2 (2nd Ed.) pp.102.
*/
vg_rand.mt[0] = seed & 0xffffffff;
- for( vg_rand.index=1; vg_rand.index<MT_STATE_VECTOR_LENGTH; vg_rand.index++ ){
+ for( vg_rand.index=1; vg_rand.index<MT_STATE_VECTOR_LENGTH; vg_rand.index++){
vg_rand.mt[vg_rand.index] =
(6069 * vg_rand.mt[vg_rand.index-1]) & 0xffffffff;
}