[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; }
|
---|
[506] | 36 | bool is_identity() const
|
---|
| 37 | {
|
---|
| 38 | return m_position == vec3() && m_orientation == quat();
|
---|
| 39 | }
|
---|
[230] | 40 |
|
---|
| 41 | public:
|
---|
| 42 | void move( const vec3& heading, float distance )
|
---|
| 43 | {
|
---|
[454] | 44 | m_position += math::normalize( heading ) * distance;
|
---|
[230] | 45 | }
|
---|
| 46 | void translate( const vec3& absolute )
|
---|
| 47 | {
|
---|
| 48 | m_position += absolute;
|
---|
| 49 | }
|
---|
[454] | 50 | void rotate( const vec3& axis, f32 angle )
|
---|
| 51 | {
|
---|
| 52 | quat temp( angle, axis );
|
---|
| 53 | m_orientation = temp * m_orientation;
|
---|
| 54 | }
|
---|
[230] | 55 | void set( const mat4& from )
|
---|
| 56 | {
|
---|
| 57 | m_orientation = quat( from );
|
---|
| 58 | m_position = vec3( from[3] );
|
---|
| 59 | }
|
---|
| 60 | mat4 extract() const
|
---|
| 61 | {
|
---|
[454] | 62 | mat4 result = math::mat4_cast( m_orientation );
|
---|
[230] | 63 | result[3] = vec4( m_position, 1.0f );
|
---|
| 64 | return result;
|
---|
| 65 | }
|
---|
[259] | 66 | transform inverse() const
|
---|
| 67 | {
|
---|
[454] | 68 | quat new_orient( math::inverse( m_orientation ) );
|
---|
[259] | 69 | // TODO: simplify
|
---|
[454] | 70 | return transform( -math::mat3_cast(new_orient) * m_position, new_orient );
|
---|
[259] | 71 | }
|
---|
| 72 |
|
---|
| 73 | transform& operator*=(const transform& rhs)
|
---|
| 74 | {
|
---|
| 75 | m_position = m_position + m_orientation * rhs.m_position;
|
---|
| 76 | m_orientation = m_orientation * rhs.m_orientation;
|
---|
| 77 | return *this;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[261] | 80 | vec3 transformed( const vec3& v ) const
|
---|
[259] | 81 | {
|
---|
| 82 | return m_orientation * v + m_position;
|
---|
| 83 | }
|
---|
[482] | 84 |
|
---|
| 85 | transform scaled( float v ) const
|
---|
| 86 | {
|
---|
| 87 | return transform( v * m_position, v * m_orientation );
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[230] | 90 | private:
|
---|
| 91 | vec3 m_position;
|
---|
| 92 | quat m_orientation;
|
---|
| 93 | };
|
---|
| 94 |
|
---|
[259] | 95 | inline transform operator*(transform lhs, const transform& rhs)
|
---|
| 96 | {
|
---|
| 97 | lhs *= rhs;
|
---|
| 98 | return lhs;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | inline vec3 operator*(const vec3 lhs, const transform& rhs)
|
---|
| 102 | {
|
---|
| 103 | return rhs.transformed( lhs );
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[482] | 106 | inline transform operator*( float value, const transform& lhs )
|
---|
| 107 | {
|
---|
| 108 | return lhs.scaled( value );
|
---|
| 109 | }
|
---|
| 110 |
|
---|
[282] | 111 | template <> struct enum_to_type< TRANSFORM > { typedef transform type; };
|
---|
| 112 | template <> struct type_to_enum< transform > { static const datatype type = TRANSFORM; };
|
---|
[259] | 113 |
|
---|
[486] | 114 | namespace math
|
---|
| 115 | {
|
---|
| 116 |
|
---|
| 117 | inline transform lerp( const transform& a, const transform& b, float value )
|
---|
| 118 | {
|
---|
| 119 | return transform(
|
---|
| 120 | math::lerp( a.get_position(), b.get_position(), value ),
|
---|
| 121 | math::lerp( a.get_orientation(), b.get_orientation(), value )
|
---|
| 122 | );
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | }
|
---|
| 126 |
|
---|
[230] | 127 | }
|
---|
| 128 |
|
---|
[319] | 129 | #endif // NV_CORE_TRANSFORM_HH
|
---|