[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:
|
---|
| 19 | transform() {}
|
---|
| 20 | explicit transform( const vec3& a_position ) : m_position( a_position ) {}
|
---|
| 21 | explicit transform( const quat& a_orientation ) : m_orientation( a_orientation ) {}
|
---|
[282] | 22 | explicit transform( const mat4& a_matrix ) { set( a_matrix ); }
|
---|
[230] | 23 | transform( const vec3& a_position, const quat& a_orientation ) : m_position( a_position ), m_orientation( a_orientation ) {}
|
---|
| 24 |
|
---|
| 25 | void set_position( const vec3& a_position ) { m_position = a_position; }
|
---|
| 26 | void set_orientation( const quat& a_orientation ) { m_orientation = a_orientation; }
|
---|
[397] | 27 | void set_orientation( float radians, const vec3& axis )
|
---|
[454] | 28 | {
|
---|
| 29 | m_orientation = math::angle_axis( radians, axis );
|
---|
[397] | 30 | }
|
---|
[230] | 31 |
|
---|
| 32 | const vec3& get_position() const { return m_position; }
|
---|
| 33 | const quat& get_orientation() const { return m_orientation; }
|
---|
| 34 |
|
---|
| 35 | public:
|
---|
| 36 | void move( const vec3& heading, float distance )
|
---|
| 37 | {
|
---|
[454] | 38 | m_position += math::normalize( heading ) * distance;
|
---|
[230] | 39 | }
|
---|
| 40 | void translate( const vec3& absolute )
|
---|
| 41 | {
|
---|
| 42 | m_position += absolute;
|
---|
| 43 | }
|
---|
[454] | 44 | void rotate( const vec3& axis, f32 angle )
|
---|
| 45 | {
|
---|
| 46 | quat temp( angle, axis );
|
---|
| 47 | m_orientation = temp * m_orientation;
|
---|
| 48 | }
|
---|
[230] | 49 | void set( const mat4& from )
|
---|
| 50 | {
|
---|
| 51 | m_orientation = quat( from );
|
---|
| 52 | m_position = vec3( from[3] );
|
---|
| 53 | }
|
---|
| 54 | mat4 extract() const
|
---|
| 55 | {
|
---|
[454] | 56 | mat4 result = math::mat4_cast( m_orientation );
|
---|
[230] | 57 | result[3] = vec4( m_position, 1.0f );
|
---|
| 58 | return result;
|
---|
| 59 | }
|
---|
[259] | 60 | transform inverse() const
|
---|
| 61 | {
|
---|
[454] | 62 | quat new_orient( math::inverse( m_orientation ) );
|
---|
[259] | 63 | // TODO: simplify
|
---|
[454] | 64 | return transform( -math::mat3_cast(new_orient) * m_position, new_orient );
|
---|
[259] | 65 | }
|
---|
| 66 |
|
---|
| 67 | transform& operator*=(const transform& rhs)
|
---|
| 68 | {
|
---|
| 69 | m_position = m_position + m_orientation * rhs.m_position;
|
---|
| 70 | m_orientation = m_orientation * rhs.m_orientation;
|
---|
| 71 | return *this;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[261] | 74 | vec3 transformed( const vec3& v ) const
|
---|
[259] | 75 | {
|
---|
| 76 | return m_orientation * v + m_position;
|
---|
| 77 | }
|
---|
[482] | 78 |
|
---|
| 79 | transform scaled( float v ) const
|
---|
| 80 | {
|
---|
| 81 | return transform( v * m_position, v * m_orientation );
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[230] | 84 | private:
|
---|
| 85 | vec3 m_position;
|
---|
| 86 | quat m_orientation;
|
---|
| 87 | };
|
---|
| 88 |
|
---|
[259] | 89 | inline transform operator*(transform lhs, const transform& rhs)
|
---|
| 90 | {
|
---|
| 91 | lhs *= rhs;
|
---|
| 92 | return lhs;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | inline vec3 operator*(const vec3 lhs, const transform& rhs)
|
---|
| 96 | {
|
---|
| 97 | return rhs.transformed( lhs );
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[482] | 100 | inline transform operator*( float value, const transform& lhs )
|
---|
| 101 | {
|
---|
| 102 | return lhs.scaled( value );
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[282] | 105 | template <> struct enum_to_type< TRANSFORM > { typedef transform type; };
|
---|
| 106 | template <> struct type_to_enum< transform > { static const datatype type = TRANSFORM; };
|
---|
[259] | 107 |
|
---|
[230] | 108 | }
|
---|
| 109 |
|
---|
[319] | 110 | #endif // NV_CORE_TRANSFORM_HH
|
---|