1 | // Copyright (C) 2012-2015 ChaosForge Ltd
|
---|
2 | // http://chaosforge.org/
|
---|
3 | //
|
---|
4 | // This file is part of Nova libraries.
|
---|
5 | // For conditions of distribution and use, see copying.txt file in root folder.
|
---|
6 |
|
---|
7 | #ifndef NV_CORE_TRANSFORM_HH
|
---|
8 | #define NV_CORE_TRANSFORM_HH
|
---|
9 |
|
---|
10 | #include <nv/common.hh>
|
---|
11 | #include <nv/stl/math.hh>
|
---|
12 |
|
---|
13 | namespace nv
|
---|
14 | {
|
---|
15 |
|
---|
16 | class transform
|
---|
17 | {
|
---|
18 | public:
|
---|
19 | typedef float value_type;
|
---|
20 |
|
---|
21 | transform() {}
|
---|
22 | explicit transform( const vec3& a_position ) : m_position( a_position ) {}
|
---|
23 | explicit transform( const quat& a_orientation ) : m_orientation( a_orientation ) {}
|
---|
24 | explicit transform( const mat4& a_matrix ) { set( a_matrix ); }
|
---|
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; }
|
---|
29 | void set_orientation( float radians, const vec3& axis )
|
---|
30 | {
|
---|
31 | m_orientation = math::angle_axis( radians, axis );
|
---|
32 | }
|
---|
33 |
|
---|
34 | const vec3& get_position() const { return m_position; }
|
---|
35 | const quat& get_orientation() const { return m_orientation; }
|
---|
36 | bool is_identity() const
|
---|
37 | {
|
---|
38 | return m_position == vec3() && m_orientation == quat();
|
---|
39 | }
|
---|
40 |
|
---|
41 | public:
|
---|
42 | void move( const vec3& heading, float distance )
|
---|
43 | {
|
---|
44 | m_position += math::normalize( heading ) * distance;
|
---|
45 | }
|
---|
46 | void translate( const vec3& absolute )
|
---|
47 | {
|
---|
48 | m_position += absolute;
|
---|
49 | }
|
---|
50 | void rotate( const vec3& axis, f32 angle )
|
---|
51 | {
|
---|
52 | quat temp( angle, axis );
|
---|
53 | m_orientation = temp * m_orientation;
|
---|
54 | }
|
---|
55 | void set( const mat4& from )
|
---|
56 | {
|
---|
57 | m_orientation = quat( from );
|
---|
58 | m_position = vec3( from[3] );
|
---|
59 | }
|
---|
60 | mat4 extract() const
|
---|
61 | {
|
---|
62 | mat4 result = math::mat4_cast( m_orientation );
|
---|
63 | result[3] = vec4( m_position, 1.0f );
|
---|
64 | return result;
|
---|
65 | }
|
---|
66 | transform inverse() const
|
---|
67 | {
|
---|
68 | quat new_orient( math::inverse( m_orientation ) );
|
---|
69 | // TODO: simplify
|
---|
70 | return transform( -math::mat3_cast(new_orient) * m_position, new_orient );
|
---|
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 |
|
---|
80 | vec3 transformed( const vec3& v ) const
|
---|
81 | {
|
---|
82 | return m_orientation * v + m_position;
|
---|
83 | }
|
---|
84 |
|
---|
85 | transform scaled( float v ) const
|
---|
86 | {
|
---|
87 | return transform( v * m_position, v * m_orientation );
|
---|
88 | }
|
---|
89 |
|
---|
90 | private:
|
---|
91 | vec3 m_position;
|
---|
92 | quat m_orientation;
|
---|
93 | };
|
---|
94 |
|
---|
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 |
|
---|
106 | inline transform operator*( float value, const transform& lhs )
|
---|
107 | {
|
---|
108 | return lhs.scaled( value );
|
---|
109 | }
|
---|
110 |
|
---|
111 | inline bool operator==( const transform& lhs, const transform& rhs )
|
---|
112 | {
|
---|
113 | return lhs.get_position() == rhs.get_position() && lhs.get_orientation() == rhs.get_orientation();
|
---|
114 | }
|
---|
115 |
|
---|
116 | inline bool operator!=( const transform& lhs, const transform& rhs )
|
---|
117 | {
|
---|
118 | return !( lhs == rhs );
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | template <> struct enum_to_type< TRANSFORM > { typedef transform type; };
|
---|
123 | template <> struct type_to_enum< transform > { static const datatype type = TRANSFORM; };
|
---|
124 |
|
---|
125 | namespace math
|
---|
126 | {
|
---|
127 |
|
---|
128 | inline transform lerp( const transform& a, const transform& b, float value )
|
---|
129 | {
|
---|
130 | return transform(
|
---|
131 | math::lerp( a.get_position(), b.get_position(), value ),
|
---|
132 | math::lerp( a.get_orientation(), b.get_orientation(), value )
|
---|
133 | );
|
---|
134 | }
|
---|
135 |
|
---|
136 | }
|
---|
137 |
|
---|
138 | }
|
---|
139 |
|
---|
140 | #endif // NV_CORE_TRANSFORM_HH
|
---|