diff --git a/vecmath.h b/vecmath.h index 8724700..05cd68f 100644 --- a/vecmath.h +++ b/vecmath.h @@ -2800,7 +2800,7 @@ VECMATH_INLINE void quat_squad_setup( vec4_t* out_a, vec4_t* out_b, vec4_t* out_ VECMATH_INLINE vec4_t quat_squad(vec4_t q1, vec4_t a, vec4_t b, vec4_t c, float t ) { return quat_slerp( quat_slerp( q1, c, t ), quat_slerp( a, b, t ), 2.0f * t * ( 1.0f - t ) ); } VECMATH_INLINE void quat_to_axis_angle( vec4_t q, vec3_t* out_axis, float* out_angle ) { if( out_angle ) *out_angle = 2.0f * vecmath_acos( q.w ); if( out_axis ) *out_axis = vec3( q.x, q.y, q.z ); } VECMATH_INLINE vec3_t quat_rotate_vector( vec3_t v, vec4_t q ) { vec3_t u = vec3( q.x, q.y, q.z ); float s = q.w; vec3_t t = vec3_mulf( vec3_cross( u, v ), 2.0f ); return vec3_add( v, vec3_add( vec3_mulf( t, s ), vec3_cross( u, t ) ) ); } -VECMATH_INLINE vec4_t quat_shortest_arc( vec3_t from, vec3_t to ) { vec3_t f = vec3_normalize( from ); vec3_t t = vec3_normalize( to ); float d = vec3_dot( f, t ); if ( d == 1.0f ) { return quat_identity( ); } if ( d == -1.0f ) { vec3_t axis = vecmath_abs( f.x ) < 1.0f ? vec3( 1, 0, 0 ) : vec3( 0, 1, 0 ); axis = vec3_normalize( vec3_cross( f, axis ) ); return quat_rotation_axis( axis, 3.14159265f ); } vec3_t axis = vec3_cross( f, t ); float s = vecmath_sqrt( ( 1.0f + d ) * 2.0f ); float invs = 1.0f / s; return vec4( axis.x * invs, axis.y * invs, axis.z * invs, 0.5f * s ); } +VECMATH_INLINE vec4_t quat_shortest_arc( vec3_t from, vec3_t to ) { vec3_t f = vec3_normalize( from ); vec3_t t = vec3_normalize( to ); float d = vec3_dot( f, t ); if ( d == 1.0f ) { return quat_identity( ); } if ( d == -1.0f ) { vec3_t axis = vecmath_abs( f.x ) < 1.0f ? vec3( 1.0f, 0.0f, 0.0f ) : vec3( 0.0f, 1.0f, 0.0f ); axis = vec3_normalize( vec3_cross( f, axis ) ); return quat_rotation_axis( axis, 3.14159265f ); } vec3_t axis = vec3_cross( f, t ); float s = vecmath_sqrt( ( 1.0f + d ) * 2.0f ); float invs = 1.0f / s; return vec4( axis.x * invs, axis.y * invs, axis.z * invs, 0.5f * s ); } VECMATH_INLINE vec4_t quat_from_mat33( mat33_t m ) { float trace = m.x.x + m.y.y + m.z.z; if ( trace > 0.0f ) { float s = vecmath_sqrt( trace + 1.0f ) * 2.0f; float invs = 1.0f / s; return vec4( ( m.y.z - m.z.y ) * invs, ( m.z.x - m.x.z ) * invs, ( m.x.y - m.y.x ) * invs, 0.25f * s ); } if ( m.x.x > m.y.y && m.x.x > m.z.z ) { float s = vecmath_sqrt( 1.0f + m.x.x - m.y.y - m.z.z ) * 2.0f; float invs = 1.0f / s; return vec4( 0.25f * s, ( m.x.y + m.y.x ) * invs, ( m.x.z + m.z.x ) * invs, ( m.y.z - m.z.y ) * invs ); } if ( m.y.y > m.z.z ) { float s = vecmath_sqrt( 1.0f + m.y.y - m.x.x - m.z.z ) * 2.0f; float invs = 1.0f / s; return vec4( ( m.x.y + m.y.x ) * invs, 0.25f * s, ( m.y.z + m.z.y ) * invs, ( m.z.x - m.x.z ) * invs ); } float s = vecmath_sqrt( 1.0f + m.z.z - m.x.x - m.y.y ) * 2.0f; float invs = 1.0f / s; return vec4( ( m.x.z + m.z.x ) * invs, ( m.y.z + m.z.y ) * invs, 0.25f * s, ( m.x.y - m.y.x ) * invs ); } VECMATH_INLINE vec4_t quat_from_mat44( mat44_t m ) { return quat_from_mat33( mat33( vec3( m.x.x, m.x.y, m.x.z ), vec3( m.y.x, m.y.y, m.y.z ), vec3( m.z.x, m.z.y, m.z.z ) ) ); } VECMATH_INLINE float quat_angle( vec4_t a, vec4_t b ) { float d = vec4_dot( a, b ); d = vecmath_clamp( d, -1.0f, 1.0f ); return 2.0f * vecmath_acos( vecmath_abs( d ) ); }