Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Transform.h « MT « include « solid « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b80dc1bc18b70fd2adaad5a3baf0249da019a10d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 * SOLID - Software Library for Interference Detection
 * 
 * Copyright (C) 2001-2003  Dtecta.  All rights reserved.
 *
 * This library may be distributed under the terms of the Q Public License
 * (QPL) as defined by Trolltech AS of Norway and appearing in the file
 * LICENSE.QPL included in the packaging of this file.
 *
 * This library may be distributed and/or modified under the terms of the
 * GNU General Public License (GPL) version 2 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.
 *
 * This library is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * Commercial use or any other use of this library not covered by either 
 * the QPL or the GPL requires an additional license from Dtecta. 
 * Please contact info@dtecta.com for enquiries about the terms of commercial
 * use of this library.
 */

#ifndef TRANSFORM_H
#define TRANSFORM_H

#include "Vector3.h"
#include "Matrix3x3.h"

namespace MT {

	template <typename Scalar>
	class Transform {
		enum { 
			TRANSLATION = 0x01,
			ROTATION    = 0x02,
			RIGID       = TRANSLATION | ROTATION,  
			SCALING     = 0x04,
			LINEAR      = ROTATION | SCALING,
			AFFINE      = TRANSLATION | LINEAR
		};
    
	public:
		Transform() {}
		
		template <typename Scalar2>
		explicit Transform(const Scalar2 *m) { setValue(m); }

		explicit Transform(const Quaternion<Scalar>& q, 
						   const Vector3<Scalar>& c = Vector3<Scalar>(Scalar(0), Scalar(0), Scalar(0))) 
			: m_basis(q),
			  m_origin(c),
			  m_type(RIGID)
		{}

		explicit Transform(const Matrix3x3<Scalar>& b, 
						   const Vector3<Scalar>& c = Vector3<Scalar>(Scalar(0), Scalar(0), Scalar(0)), 
						   unsigned int type = AFFINE)
			: m_basis(b),
			  m_origin(c),
			  m_type(type)
		{}

		Vector3<Scalar> operator()(const Vector3<Scalar>& x) const
		{
			return Vector3<Scalar>(m_basis[0].dot(x) + m_origin[0], 
								   m_basis[1].dot(x) + m_origin[1], 
								   m_basis[2].dot(x) + m_origin[2]);
		}
    
		Vector3<Scalar> operator*(const Vector3<Scalar>& x) const
		{
			return (*this)(x);
		}

		Matrix3x3<Scalar>&       getBasis()          { return m_basis; }
		const Matrix3x3<Scalar>& getBasis()    const { return m_basis; }

		Vector3<Scalar>&         getOrigin()         { return m_origin; }
		const Vector3<Scalar>&   getOrigin()   const { return m_origin; }

		Quaternion<Scalar> getRotation() const { return m_basis.getRotation(); }
		template <typename Scalar2>
		void setValue(const Scalar2 *m) 
		{
			m_basis.setValue(m);
			m_origin.setValue(&m[12]);
			m_type = AFFINE;
		}

		template <typename Scalar2>
		void getValue(Scalar2 *m) const 
		{
			m_basis.getValue(m);
			m_origin.getValue(&m[12]);
			m[15] = Scalar2(1.0);
		}

		void setOrigin(const Vector3<Scalar>& origin) 
		{ 
			m_origin = origin;
			m_type |= TRANSLATION;
		}

		void setBasis(const Matrix3x3<Scalar>& basis)
		{ 
			m_basis = basis;
			m_type |= LINEAR;
		}

		void setRotation(const Quaternion<Scalar>& q)
		{
			m_basis.setRotation(q);
			m_type = (m_type & ~LINEAR) | ROTATION;
		}

    	void scale(const Vector3<Scalar>& scaling)
		{
			m_basis = m_basis.scaled(scaling);
			m_type |= SCALING;
		}
    
		void setIdentity()
		{
			m_basis.setIdentity();
			m_origin.setValue(Scalar(0.0), Scalar(0.0), Scalar(0.0));
			m_type = 0x0;
		}
		
		bool isIdentity() const { return m_type == 0x0; }
    
		Transform<Scalar>& operator*=(const Transform<Scalar>& t) 
		{
			m_origin += m_basis * t.m_origin;
			m_basis *= t.m_basis;
			m_type |= t.m_type; 
			return *this;
		}

		Transform<Scalar> inverse() const
		{ 
			Matrix3x3<Scalar> inv = (m_type & SCALING) ? 
				                    m_basis.inverse() : 
				                    m_basis.transpose();
			
			return Transform<Scalar>(inv, inv * -m_origin, m_type);
		}

		Transform<Scalar> inverseTimes(const Transform<Scalar>& t) const;  

		Transform<Scalar> operator*(const Transform<Scalar>& t) const;

	private:
		
		Matrix3x3<Scalar> m_basis;
		Vector3<Scalar>   m_origin;
		unsigned int      m_type;
	};


	template <typename Scalar>
	inline Transform<Scalar> 
	Transform<Scalar>::inverseTimes(const Transform<Scalar>& t) const  
	{
		Vector3<Scalar> v = t.getOrigin() - m_origin;
		if (m_type & SCALING) 
		{
			Matrix3x3<Scalar> inv = m_basis.inverse();
			return Transform<Scalar>(inv * t.getBasis(), inv * v, 
									 m_type | t.m_type);
		}
		else 
		{
			return Transform<Scalar>(m_basis.transposeTimes(t.m_basis),
									 v * m_basis, m_type | t.m_type);
		}
	}

	template <typename Scalar>
	inline Transform<Scalar> 
	Transform<Scalar>::operator*(const Transform<Scalar>& t) const
	{
		return Transform<Scalar>(m_basis * t.m_basis, 
								 (*this)(t.m_origin), 
								 m_type | t.m_type);
	}	
}

#endif