source: trunk/nv/core/transform.hh @ 486

Last change on this file since 486 was 486, checked in by epyon, 9 years ago
  • mass update once again...
File size: 3.2 KB
Line 
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
13namespace 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
37        public:
38                void move( const vec3& heading, float distance )
39                {
40                        m_position += math::normalize( heading ) * distance;
41                }
42                void translate( const vec3& absolute )
43                {
44                        m_position += absolute;
45                }
46                void rotate( const vec3& axis, f32 angle )
47                {
48                        quat temp( angle, axis );
49                        m_orientation = temp * m_orientation;
50                }
51                void set( const mat4& from )
52                {
53                        m_orientation = quat( from );
54                        m_position    = vec3( from[3] );
55                }
56                mat4 extract() const
57                {
58                        mat4 result = math::mat4_cast( m_orientation );
59                        result[3] = vec4( m_position, 1.0f );
60                        return result;
61                }
62                transform inverse() const
63                {
64                        quat new_orient( math::inverse( m_orientation ) );
65                        // TODO: simplify
66                        return transform( -math::mat3_cast(new_orient) * m_position, new_orient );
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
76                vec3 transformed( const vec3& v ) const
77                {
78                        return m_orientation * v + m_position;
79                }
80
81                transform scaled( float v ) const
82                {
83                        return transform( v * m_position, v * m_orientation );
84                }
85
86        private:
87                vec3 m_position;
88                quat m_orientation;
89        };
90
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
102        inline transform operator*( float value, const transform& lhs )
103        {
104                return lhs.scaled( value );
105        }
106
107        template <> struct enum_to_type< TRANSFORM > { typedef transform type; };
108        template <> struct type_to_enum< transform > { static const datatype type = TRANSFORM; };
109
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
123}
124
125#endif // NV_CORE_TRANSFORM_HH
Note: See TracBrowser for help on using the repository browser.