[395] | 1 | // Copyright (C) 2012-2015 ChaosForge Ltd
|
---|
[230] | 2 | // http://chaosforge.org/
|
---|
| 3 | //
|
---|
[395] | 4 | // This file is part of Nova libraries.
|
---|
| 5 | // For conditions of distribution and use, see copying.txt file in root folder.
|
---|
[230] | 6 |
|
---|
[319] | 7 | #ifndef NV_CORE_TRANSFORM_HH
|
---|
| 8 | #define NV_CORE_TRANSFORM_HH
|
---|
[230] | 9 |
|
---|
[395] | 10 | #include <nv/common.hh>
|
---|
[368] | 11 | #include <nv/stl/math.hh>
|
---|
[230] | 12 |
|
---|
| 13 | namespace nv
|
---|
| 14 | {
|
---|
| 15 |
|
---|
| 16 | class transform
|
---|
| 17 | {
|
---|
| 18 | public:
|
---|
[486] | 19 | typedef float value_type;
|
---|
| 20 |
|
---|
[230] | 21 | transform() {}
|
---|
| 22 | explicit transform( const vec3& a_position ) : m_position( a_position ) {}
|
---|
| 23 | explicit transform( const quat& a_orientation ) : m_orientation( a_orientation ) {}
|
---|
[282] | 24 | explicit transform( const mat4& a_matrix ) { set( a_matrix ); }
|
---|
[230] | 25 | transform( const vec3& a_position, const quat& a_orientation ) : m_position( a_position ), m_orientation( a_orientation ) {}
|
---|
| 26 |
|
---|
| 27 | void set_position( const vec3& a_position ) { m_position = a_position; }
|
---|
| 28 | void set_orientation( const quat& a_orientation ) { m_orientation = a_orientation; }
|
---|
[397] | 29 | void set_orientation( float radians, const vec3& axis )
|
---|
[454] | 30 | {
|
---|
| 31 | m_orientation = math::angle_axis( radians, axis );
|
---|
[397] | 32 | }
|
---|
[230] | 33 |
|
---|
| 34 | const vec3& get_position() const { return m_position; }
|
---|
| 35 | const quat& get_orientation() const { return m_orientation; }
|
---|
| 36 |
|
---|
| 37 | public:
|
---|
| 38 | void move( const vec3& heading, float distance )
|
---|
| 39 | {
|
---|
[454] | 40 | m_position += math::normalize( heading ) * distance;
|
---|
[230] | 41 | }
|
---|
| 42 | void translate( const vec3& absolute )
|
---|
| 43 | {
|
---|
| 44 | m_position += absolute;
|
---|
| 45 | }
|
---|
[454] | 46 | void rotate( const vec3& axis, f32 angle )
|
---|
| 47 | {
|
---|
| 48 | quat temp( angle, axis );
|
---|
| 49 | m_orientation = temp * m_orientation;
|
---|
| 50 | }
|
---|
[230] | 51 | void set( const mat4& from )
|
---|
| 52 | {
|
---|
| 53 | m_orientation = quat( from );
|
---|
| 54 | m_position = vec3( from[3] );
|
---|
| 55 | }
|
---|
| 56 | mat4 extract() const
|
---|
| 57 | {
|
---|
[454] | 58 | mat4 result = math::mat4_cast( m_orientation );
|
---|
[230] | 59 | result[3] = vec4( m_position, 1.0f );
|
---|
| 60 | return result;
|
---|
| 61 | }
|
---|
[259] | 62 | transform inverse() const
|
---|
| 63 | {
|
---|
[454] | 64 | quat new_orient( math::inverse( m_orientation ) );
|
---|
[259] | 65 | // TODO: simplify
|
---|
[454] | 66 | return transform( -math::mat3_cast(new_orient) * m_position, new_orient );
|
---|
[259] | 67 | }
|
---|
| 68 |
|
---|
| 69 | transform& operator*=(const transform& rhs)
|
---|
| 70 | {
|
---|
| 71 | m_position = m_position + m_orientation * rhs.m_position;
|
---|
| 72 | m_orientation = m_orientation * rhs.m_orientation;
|
---|
| 73 | return *this;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[261] | 76 | vec3 transformed( const vec3& v ) const
|
---|
[259] | 77 | {
|
---|
| 78 | return m_orientation * v + m_position;
|
---|
| 79 | }
|
---|
[482] | 80 |
|
---|
| 81 | transform scaled( float v ) const
|
---|
| 82 | {
|
---|
| 83 | return transform( v * m_position, v * m_orientation );
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[230] | 86 | private:
|
---|
| 87 | vec3 m_position;
|
---|
| 88 | quat m_orientation;
|
---|
| 89 | };
|
---|
| 90 |
|
---|
[259] | 91 | inline transform operator*(transform lhs, const transform& rhs)
|
---|
| 92 | {
|
---|
| 93 | lhs *= rhs;
|
---|
| 94 | return lhs;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | inline vec3 operator*(const vec3 lhs, const transform& rhs)
|
---|
| 98 | {
|
---|
| 99 | return rhs.transformed( lhs );
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[482] | 102 | inline transform operator*( float value, const transform& lhs )
|
---|
| 103 | {
|
---|
| 104 | return lhs.scaled( value );
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[282] | 107 | template <> struct enum_to_type< TRANSFORM > { typedef transform type; };
|
---|
| 108 | template <> struct type_to_enum< transform > { static const datatype type = TRANSFORM; };
|
---|
[259] | 109 |
|
---|
[486] | 110 | namespace math
|
---|
| 111 | {
|
---|
| 112 |
|
---|
| 113 | inline transform lerp( const transform& a, const transform& b, float value )
|
---|
| 114 | {
|
---|
| 115 | return transform(
|
---|
| 116 | math::lerp( a.get_position(), b.get_position(), value ),
|
---|
| 117 | math::lerp( a.get_orientation(), b.get_orientation(), value )
|
---|
| 118 | );
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[230] | 123 | }
|
---|
| 124 |
|
---|
[319] | 125 | #endif // NV_CORE_TRANSFORM_HH
|
---|