Panda3D
 All Classes Functions Variables Enumerations
eggTransform.I
1 // Filename: eggTransform.I
2 // Created by: drose (21Jun02)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 ////////////////////////////////////////////////////////////////////
16 // Function: EggTransform::Component::Constructor
17 // Access: Public
18 // Description:
19 ////////////////////////////////////////////////////////////////////
20 INLINE EggTransform::Component::
21 Component(EggTransform::ComponentType type, double number) :
22  _type(type),
23  _number(number)
24 {
25  _vec2 = (LVecBase2d *)NULL;
26  _vec3 = (LVecBase3d *)NULL;
27  _mat3 = (LMatrix3d *)NULL;
28  _mat4 = (LMatrix4d *)NULL;
29 }
30 
31 ////////////////////////////////////////////////////////////////////
32 // Function: EggTransform::Component::Copy Constructor
33 // Access: Public
34 // Description:
35 ////////////////////////////////////////////////////////////////////
36 INLINE EggTransform::Component::
37 Component(const EggTransform::Component &copy) :
38  _type(copy._type),
39  _number(copy._number)
40 {
41  _vec2 = (LVecBase2d *)NULL;
42  _vec3 = (LVecBase3d *)NULL;
43  _mat3 = (LMatrix3d *)NULL;
44  _mat4 = (LMatrix4d *)NULL;
45  if (copy._vec2 != (LVecBase2d *)NULL) {
46  _vec2 = new LVector2d(*copy._vec2);
47  }
48  if (copy._vec3 != (LVecBase3d *)NULL) {
49  _vec3 = new LVector3d(*copy._vec3);
50  }
51  if (copy._mat3 != (LMatrix3d *)NULL) {
52  _mat3 = new LMatrix3d(*copy._mat3);
53  }
54  if (copy._mat4 != (LMatrix4d *)NULL) {
55  _mat4 = new LMatrix4d(*copy._mat4);
56  }
57 }
58 
59 ////////////////////////////////////////////////////////////////////
60 // Function: EggTransform::Component::Copy Assignment Operator
61 // Access: Public
62 // Description:
63 ////////////////////////////////////////////////////////////////////
64 INLINE void EggTransform::Component::
65 operator = (const EggTransform::Component &copy) {
66  _type = copy._type;
67  _number = copy._number;
68  if (_vec2 != (LVecBase2d *)NULL) {
69  delete _vec2;
70  _vec2 = (LVecBase2d *)NULL;
71  }
72  if (_vec3 != (LVecBase3d *)NULL) {
73  delete _vec3;
74  _vec3 = (LVecBase3d *)NULL;
75  }
76  if (_mat3 != (LMatrix3d *)NULL) {
77  delete _mat3;
78  _mat3 = (LMatrix3d *)NULL;
79  }
80  if (_mat4 != (LMatrix4d *)NULL) {
81  delete _mat4;
82  _mat4 = (LMatrix4d *)NULL;
83  }
84  if (copy._vec2 != (LVecBase2d *)NULL) {
85  _vec2 = new LVecBase2d(*copy._vec2);
86  }
87  if (copy._vec3 != (LVecBase3d *)NULL) {
88  _vec3 = new LVecBase3d(*copy._vec3);
89  }
90  if (copy._mat3 != (LMatrix3d *)NULL) {
91  _mat3 = new LMatrix3d(*copy._mat3);
92  }
93  if (copy._mat4 != (LMatrix4d *)NULL) {
94  _mat4 = new LMatrix4d(*copy._mat4);
95  }
96 }
97 
98 ////////////////////////////////////////////////////////////////////
99 // Function: EggTransform::Component::Destructor
100 // Access: Public
101 // Description:
102 ////////////////////////////////////////////////////////////////////
103 INLINE EggTransform::Component::
104 ~Component() {
105  if (_vec2 != (LVecBase2d *)NULL) {
106  delete _vec2;
107  }
108  if (_vec3 != (LVecBase3d *)NULL) {
109  delete _vec3;
110  }
111  if (_mat3 != (LMatrix3d *)NULL) {
112  delete _mat3;
113  }
114  if (_mat4 != (LMatrix4d *)NULL) {
115  delete _mat4;
116  }
117 }
118 
119 ////////////////////////////////////////////////////////////////////
120 // Function: EggTransform::clear_transform
121 // Access: Public
122 // Description: Resets the transform to empty, identity.
123 ////////////////////////////////////////////////////////////////////
124 INLINE void EggTransform::
126  internal_clear_transform();
127  transform_changed();
128 }
129 
130 ////////////////////////////////////////////////////////////////////
131 // Function: EggTransform::add_matrix3
132 // Access: Public
133 // Description: Appends an arbitrary 3x3 matrix to the current
134 // transform.
135 ////////////////////////////////////////////////////////////////////
136 INLINE void EggTransform::
137 add_matrix3(const LMatrix3d &mat) {
138  internal_add_matrix(mat);
139  transform_changed();
140 }
141 
142 ////////////////////////////////////////////////////////////////////
143 // Function: EggTransform::add_matrix4
144 // Access: Public
145 // Description: Appends an arbitrary 4x4 matrix to the current
146 // transform.
147 ////////////////////////////////////////////////////////////////////
148 INLINE void EggTransform::
149 add_matrix4(const LMatrix4d &mat) {
150  internal_add_matrix(mat);
151  transform_changed();
152 }
153 
154 ////////////////////////////////////////////////////////////////////
155 // Function: EggTransform::has_transform
156 // Access: Public
157 // Description: Returns true if the transform is nonempty, false if
158 // it is empty (no transform components have been
159 // added). This is true for either a 2-d or a 3-d
160 // transform.
161 ////////////////////////////////////////////////////////////////////
162 INLINE bool EggTransform::
163 has_transform() const {
164  return !_components.empty();
165 }
166 
167 ////////////////////////////////////////////////////////////////////
168 // Function: EggTransform::has_transform2d
169 // Access: Public
170 // Description: Returns true if the transform is specified as a 2-d
171 // transform, e.g. with a 3x3 matrix, or false if it is
172 // specified as a 3-d transform (with a 4x4 matrix), or
173 // not specified at all.
174 //
175 // Normally, EggTextures have a 2-d matrix (but
176 // occasionally they use a 3-d matrix), and EggGroups
177 // always have a 3-d matrix.
178 ////////////////////////////////////////////////////////////////////
179 INLINE bool EggTransform::
181  return has_transform() && _is_transform_2d;
182 }
183 
184 ////////////////////////////////////////////////////////////////////
185 // Function: EggTransform::set_transform2d
186 // Access: Public
187 // Description: Sets the overall transform as a 3x3 matrix. This
188 // completely replaces whatever componentwise transform
189 // may have been defined.
190 ////////////////////////////////////////////////////////////////////
191 INLINE void EggTransform::
193  internal_set_transform(mat);
194  transform_changed();
195 }
196 
197 ////////////////////////////////////////////////////////////////////
198 // Function: EggTransform::has_transform3d
199 // Access: Public
200 // Description: Returns true if the transform is specified as a 3-d
201 // transform, e.g. with a 4x4 matrix, or false if it is
202 // specified as a 2-d transform (with a 2x2 matrix), or
203 // not specified at all.
204 //
205 // Normally, EggTextures have a 3-d matrix (but
206 // occasionally they use a 3-d matrix), and EggGroups
207 // always have a 3-d matrix.
208 ////////////////////////////////////////////////////////////////////
209 INLINE bool EggTransform::
211  return has_transform() && !_is_transform_2d;
212 }
213 
214 ////////////////////////////////////////////////////////////////////
215 // Function: EggTransform::set_transform3d
216 // Access: Public
217 // Description: Sets the overall transform as a 4x4 matrix. This
218 // completely replaces whatever componentwise transform
219 // may have been defined.
220 ////////////////////////////////////////////////////////////////////
221 INLINE void EggTransform::
223  internal_set_transform(mat);
224  transform_changed();
225 }
226 
227 ////////////////////////////////////////////////////////////////////
228 // Function: EggTransform::get_transform2d
229 // Access: Public
230 // Description: Returns the overall transform as a 3x3 matrix. It is
231 // an error to call this if has_transform3d() is true.
232 ////////////////////////////////////////////////////////////////////
235  nassertr(!has_transform3d(), LMatrix3d::ident_mat());
236  const LMatrix4d &t = _transform;
237  return LMatrix3d(t(0, 0), t(0, 1), t(0, 3),
238  t(1, 0), t(1, 1), t(1, 3),
239  t(3, 0), t(3, 1), t(3, 3));
240 }
241 
242 ////////////////////////////////////////////////////////////////////
243 // Function: EggTransform::get_transform3d
244 // Access: Public
245 // Description: Returns the overall transform as a 4x4 matrix. It is
246 // valid to call this even if has_transform2d() is true;
247 // in this case, the 3x3 transform will be expanded to a
248 // 4x4 matrix.
249 ////////////////////////////////////////////////////////////////////
250 INLINE const LMatrix4d &EggTransform::
252  return _transform;
253 }
254 
255 ////////////////////////////////////////////////////////////////////
256 // Function: EggTransform::transform_is_identity
257 // Access: Public
258 // Description: Returns true if the described transform is identity,
259 // false otherwise.
260 ////////////////////////////////////////////////////////////////////
261 INLINE bool EggTransform::
263  return _components.empty() ||
264  _transform.almost_equal(LMatrix4d::ident_mat(), 0.0001);
265 }
266 
267 ////////////////////////////////////////////////////////////////////
268 // Function: EggTransform::get_num_components
269 // Access: Public
270 // Description: Returns the number of components that make up the
271 // transform.
272 ////////////////////////////////////////////////////////////////////
273 INLINE int EggTransform::
275  return _components.size();
276 }
277 
278 ////////////////////////////////////////////////////////////////////
279 // Function: EggTransform::get_component_type
280 // Access: Public
281 // Description: Returns the type of the nth component.
282 ////////////////////////////////////////////////////////////////////
283 INLINE EggTransform::ComponentType EggTransform::
284 get_component_type(int n) const {
285  nassertr(n >= 0 && n < (int)_components.size(), CT_invalid);
286  return _components[n]._type;
287 }
288 
289 ////////////////////////////////////////////////////////////////////
290 // Function: EggTransform::get_component_number
291 // Access: Public
292 // Description: Returns the solitary number associated with the nth
293 // component. In the case of a rotation, this is the
294 // angle in degrees to rotate; in the case of uniform
295 // scale, this is the amount of the scale. Other types
296 // do not use this property.
297 ////////////////////////////////////////////////////////////////////
298 INLINE double EggTransform::
299 get_component_number(int n) const {
300  nassertr(n >= 0 && n < (int)_components.size(), 0.0);
301  return _components[n]._number;
302 }
303 
304 ////////////////////////////////////////////////////////////////////
305 // Function: EggTransform::get_component_vec2
306 // Access: Public
307 // Description: Returns the 2-component vector associated with the
308 // nth component. This may be the translate vector,
309 // rotate axis, or non-uniform scale. It is an error to
310 // call this if the component type does not use a 2-d
311 // vector property.
312 ////////////////////////////////////////////////////////////////////
313 INLINE const LVecBase2d &EggTransform::
314 get_component_vec2(int n) const {
315  nassertr(n >= 0 && n < (int)_components.size(), LVector2d::zero());
316  nassertr(_components[n]._vec2 != (LVecBase2d *)NULL, LVector2d::zero());
317  return *_components[n]._vec2;
318 }
319 
320 ////////////////////////////////////////////////////////////////////
321 // Function: EggTransform::get_component_vec3
322 // Access: Public
323 // Description: Returns the 3-component vector associated with the
324 // nth component. This may be the translate vector,
325 // rotate axis, or non-uniform scale. It is an error to
326 // call this if the component type does not use a 3-d
327 // vector property.
328 ////////////////////////////////////////////////////////////////////
329 INLINE const LVecBase3d &EggTransform::
330 get_component_vec3(int n) const {
331  nassertr(n >= 0 && n < (int)_components.size(), LVector3d::zero());
332  nassertr(_components[n]._vec3 != (LVecBase3d *)NULL, LVector3d::zero());
333  return *_components[n]._vec3;
334 }
335 
336 ////////////////////////////////////////////////////////////////////
337 // Function: EggTransform::get_component_mat3
338 // Access: Public
339 // Description: Returns the 3x3 matrix associated with the nth
340 // component. It is an error to call this if the
341 // component type is not CT_matrix3.
342 ////////////////////////////////////////////////////////////////////
343 INLINE const LMatrix3d &EggTransform::
344 get_component_mat3(int n) const {
345  nassertr(n >= 0 && n < (int)_components.size(), LMatrix3d::ident_mat());
346  nassertr(_components[n]._mat3 != (LMatrix3d *)NULL, LMatrix3d::ident_mat());
347  return *_components[n]._mat3;
348 }
349 
350 ////////////////////////////////////////////////////////////////////
351 // Function: EggTransform::get_component_mat4
352 // Access: Public
353 // Description: Returns the 4x4 matrix associated with the nth
354 // component. It is an error to call this if the
355 // component type is not CT_matrix4.
356 ////////////////////////////////////////////////////////////////////
357 INLINE const LMatrix4d &EggTransform::
358 get_component_mat4(int n) const {
359  nassertr(n >= 0 && n < (int)_components.size(), LMatrix4d::ident_mat());
360  nassertr(_components[n]._mat4 != (LMatrix4d *)NULL, LMatrix4d::ident_mat());
361  return *_components[n]._mat4;
362 }
363 
364 ////////////////////////////////////////////////////////////////////
365 // Function: EggTransform::internal_set_transform
366 // Access: Protected
367 // Description: Sets the overall transform without calling
368 // transform_changed().
369 ////////////////////////////////////////////////////////////////////
370 INLINE void EggTransform::
371 internal_set_transform(const LMatrix3d &mat) {
372  internal_clear_transform();
373  internal_add_matrix(mat);
374 }
375 
376 ////////////////////////////////////////////////////////////////////
377 // Function: EggTransform::internal_set_transform
378 // Access: Protected
379 // Description: Sets the overall transform without calling
380 // transform_changed().
381 ////////////////////////////////////////////////////////////////////
382 INLINE void EggTransform::
383 internal_set_transform(const LMatrix4d &mat) {
384  internal_clear_transform();
385  internal_add_matrix(mat);
386 }
void clear_transform()
Resets the transform to empty, identity.
Definition: eggTransform.I:125
const LMatrix4d & get_transform3d() const
Returns the overall transform as a 4x4 matrix.
Definition: eggTransform.I:251
This is a 4-by-4 transform matrix.
Definition: lmatrix.h:4716
void set_transform2d(const LMatrix3d &mat)
Sets the overall transform as a 3x3 matrix.
Definition: eggTransform.I:192
This is a two-component vector offset.
Definition: lvector2.h:416
This is the base class for all two-component vectors and points.
Definition: lvecBase2.h:1241
const LMatrix3d & get_component_mat3(int n) const
Returns the 3x3 matrix associated with the nth component.
Definition: eggTransform.I:344
LMatrix3d get_transform2d() const
Returns the overall transform as a 3x3 matrix.
Definition: eggTransform.I:234
static const LVector3d & zero()
Returns a zero-length vector.
Definition: lvector3.h:915
This is a 3-by-3 transform matrix.
Definition: lmatrix.h:4375
bool almost_equal(const LMatrix4d &other, double threshold) const
Returns true if two matrices are memberwise equal within a specified tolerance.
Definition: lmatrix.cxx:2058
const LMatrix4d & get_component_mat4(int n) const
Returns the 4x4 matrix associated with the nth component.
Definition: eggTransform.I:358
void set_transform3d(const LMatrix4d &mat)
Sets the overall transform as a 4x4 matrix.
Definition: eggTransform.I:222
bool has_transform3d() const
Returns true if the transform is specified as a 3-d transform, e.g.
Definition: eggTransform.I:210
static const LMatrix4d & ident_mat()
Returns an identity matrix.
Definition: lmatrix.h:5168
const LVecBase3d & get_component_vec3(int n) const
Returns the 3-component vector associated with the nth component.
Definition: eggTransform.I:330
static const LVector2d & zero()
Returns a zero-length vector.
Definition: lvector2.h:543
double get_component_number(int n) const
Returns the solitary number associated with the nth component.
Definition: eggTransform.I:299
This is the base class for all three-component vectors and points.
Definition: lvecBase3.h:1455
int get_num_components() const
Returns the number of components that make up the transform.
Definition: eggTransform.I:274
bool has_transform() const
Returns true if the transform is nonempty, false if it is empty (no transform components have been ad...
Definition: eggTransform.I:163
This is a three-component vector distance (as opposed to a three-component point, which represents a ...
Definition: lvector3.h:746
void add_matrix3(const LMatrix3d &mat)
Appends an arbitrary 3x3 matrix to the current transform.
Definition: eggTransform.I:137
bool has_transform2d() const
Returns true if the transform is specified as a 2-d transform, e.g.
Definition: eggTransform.I:180
ComponentType get_component_type(int n) const
Returns the type of the nth component.
Definition: eggTransform.I:284
static const LMatrix3d & ident_mat()
Returns an identity matrix.
Definition: lmatrix.h:7128
void add_matrix4(const LMatrix4d &mat)
Appends an arbitrary 4x4 matrix to the current transform.
Definition: eggTransform.I:149
const LVecBase2d & get_component_vec2(int n) const
Returns the 2-component vector associated with the nth component.
Definition: eggTransform.I:314
bool transform_is_identity() const
Returns true if the described transform is identity, false otherwise.
Definition: eggTransform.I:262