Panda3D
Loading...
Searching...
No Matches
eggTransform.I
Go to the documentation of this file.
1/**
2 * PANDA 3D SOFTWARE
3 * Copyright (c) Carnegie Mellon University. All rights reserved.
4 *
5 * All use of this software is subject to the terms of the revised BSD
6 * license. You should have received a copy of this license along
7 * with this source code in a file named "LICENSE."
8 *
9 * @file eggTransform.I
10 * @author drose
11 * @date 2002-06-21
12 */
13
14/**
15 *
16 */
17INLINE EggTransform::Component::
18Component(EggTransform::ComponentType type, double number) :
19 _type(type),
20 _number(number)
21{
22 _vec2 = nullptr;
23 _vec3 = nullptr;
24 _mat3 = nullptr;
25 _mat4 = nullptr;
26}
27
28/**
29 *
30 */
31INLINE EggTransform::Component::
32Component(const EggTransform::Component &copy) :
33 _type(copy._type),
34 _number(copy._number)
35{
36 _vec2 = nullptr;
37 _vec3 = nullptr;
38 _mat3 = nullptr;
39 _mat4 = nullptr;
40 if (copy._vec2 != nullptr) {
41 _vec2 = new LVector2d(*copy._vec2);
42 }
43 if (copy._vec3 != nullptr) {
44 _vec3 = new LVector3d(*copy._vec3);
45 }
46 if (copy._mat3 != nullptr) {
47 _mat3 = new LMatrix3d(*copy._mat3);
48 }
49 if (copy._mat4 != nullptr) {
50 _mat4 = new LMatrix4d(*copy._mat4);
51 }
52}
53
54/**
55 *
56 */
57INLINE void EggTransform::Component::
58operator = (const EggTransform::Component &copy) {
59 _type = copy._type;
60 _number = copy._number;
61 if (_vec2 != nullptr) {
62 delete _vec2;
63 _vec2 = nullptr;
64 }
65 if (_vec3 != nullptr) {
66 delete _vec3;
67 _vec3 = nullptr;
68 }
69 if (_mat3 != nullptr) {
70 delete _mat3;
71 _mat3 = nullptr;
72 }
73 if (_mat4 != nullptr) {
74 delete _mat4;
75 _mat4 = nullptr;
76 }
77 if (copy._vec2 != nullptr) {
78 _vec2 = new LVecBase2d(*copy._vec2);
79 }
80 if (copy._vec3 != nullptr) {
81 _vec3 = new LVecBase3d(*copy._vec3);
82 }
83 if (copy._mat3 != nullptr) {
84 _mat3 = new LMatrix3d(*copy._mat3);
85 }
86 if (copy._mat4 != nullptr) {
87 _mat4 = new LMatrix4d(*copy._mat4);
88 }
89}
90
91/**
92 *
93 */
94INLINE EggTransform::Component::
95~Component() {
96 if (_vec2 != nullptr) {
97 delete _vec2;
98 }
99 if (_vec3 != nullptr) {
100 delete _vec3;
101 }
102 if (_mat3 != nullptr) {
103 delete _mat3;
104 }
105 if (_mat4 != nullptr) {
106 delete _mat4;
107 }
108}
109
110/**
111 * Resets the transform to empty, identity.
112 */
113INLINE void EggTransform::
115 internal_clear_transform();
116 transform_changed();
117}
118
119/**
120 * Appends an arbitrary 3x3 matrix to the current transform.
121 */
122INLINE void EggTransform::
123add_matrix3(const LMatrix3d &mat) {
124 internal_add_matrix(mat);
125 transform_changed();
126}
127
128/**
129 * Appends an arbitrary 4x4 matrix to the current transform.
130 */
131INLINE void EggTransform::
132add_matrix4(const LMatrix4d &mat) {
133 internal_add_matrix(mat);
134 transform_changed();
135}
136
137/**
138 * Returns true if the transform is nonempty, false if it is empty (no
139 * transform components have been added). This is true for either a 2-d or a
140 * 3-d transform.
141 */
142INLINE bool EggTransform::
143has_transform() const {
144 return !_components.empty();
145}
146
147/**
148 * Returns true if the transform is specified as a 2-d transform, e.g. with a
149 * 3x3 matrix, or false if it is specified as a 3-d transform (with a 4x4
150 * matrix), or not specified at all.
151 *
152 * Normally, EggTextures have a 2-d matrix (but occasionally they use a 3-d
153 * matrix), and EggGroups always have a 3-d matrix.
154 */
155INLINE bool EggTransform::
156has_transform2d() const {
157 return has_transform() && _is_transform_2d;
158}
159
160/**
161 * Sets the overall transform as a 3x3 matrix. This completely replaces
162 * whatever componentwise transform may have been defined.
163 */
164INLINE void EggTransform::
165set_transform2d(const LMatrix3d &mat) {
166 internal_set_transform(mat);
167 transform_changed();
168}
169
170/**
171 * Returns true if the transform is specified as a 3-d transform, e.g. with a
172 * 4x4 matrix, or false if it is specified as a 2-d transform (with a 2x2
173 * matrix), or not specified at all.
174 *
175 * Normally, EggTextures have a 3-d matrix (but occasionally they use a 3-d
176 * matrix), and EggGroups always have a 3-d matrix.
177 */
178INLINE bool EggTransform::
179has_transform3d() const {
180 return has_transform() && !_is_transform_2d;
181}
182
183/**
184 * Sets the overall transform as a 4x4 matrix. This completely replaces
185 * whatever componentwise transform may have been defined.
186 */
187INLINE void EggTransform::
188set_transform3d(const LMatrix4d &mat) {
189 internal_set_transform(mat);
190 transform_changed();
191}
192
193/**
194 * Returns the overall transform as a 3x3 matrix. It is an error to call this
195 * if has_transform3d() is true.
196 */
197INLINE LMatrix3d EggTransform::
198get_transform2d() const {
199 nassertr(!has_transform3d(), LMatrix3d::ident_mat());
200 const LMatrix4d &t = _transform;
201 return LMatrix3d(t(0, 0), t(0, 1), t(0, 3),
202 t(1, 0), t(1, 1), t(1, 3),
203 t(3, 0), t(3, 1), t(3, 3));
204}
205
206/**
207 * Returns the overall transform as a 4x4 matrix. It is valid to call this
208 * even if has_transform2d() is true; in this case, the 3x3 transform will be
209 * expanded to a 4x4 matrix.
210 */
211INLINE const LMatrix4d &EggTransform::
212get_transform3d() const {
213 return _transform;
214}
215
216/**
217 * Returns true if the described transform is identity, false otherwise.
218 */
219INLINE bool EggTransform::
220transform_is_identity() const {
221 return _components.empty() ||
222 _transform.almost_equal(LMatrix4d::ident_mat(), 0.0001);
223}
224
225/**
226 * Returns the number of components that make up the transform.
227 */
229get_num_components() const {
230 return _components.size();
231}
232
233/**
234 * Returns the type of the nth component.
235 */
236INLINE EggTransform::ComponentType EggTransform::
237get_component_type(int n) const {
238 nassertr(n >= 0 && n < (int)_components.size(), CT_invalid);
239 return _components[n]._type;
240}
241
242/**
243 * Returns the solitary number associated with the nth component. In the case
244 * of a rotation, this is the angle in degrees to rotate; in the case of
245 * uniform scale, this is the amount of the scale. Other types do not use
246 * this property.
247 */
248INLINE double EggTransform::
249get_component_number(int n) const {
250 nassertr(n >= 0 && n < (int)_components.size(), 0.0);
251 return _components[n]._number;
252}
253
254/**
255 * Returns the 2-component vector associated with the nth component. This may
256 * be the translate vector, rotate axis, or non-uniform scale. It is an error
257 * to call this if the component type does not use a 2-d vector property.
258 */
259INLINE const LVecBase2d &EggTransform::
260get_component_vec2(int n) const {
261 nassertr(n >= 0 && n < (int)_components.size(), LVector2d::zero());
262 nassertr(_components[n]._vec2 != nullptr, LVector2d::zero());
263 return *_components[n]._vec2;
264}
265
266/**
267 * Returns the 3-component vector associated with the nth component. This may
268 * be the translate vector, rotate axis, or non-uniform scale. It is an error
269 * to call this if the component type does not use a 3-d vector property.
270 */
271INLINE const LVecBase3d &EggTransform::
272get_component_vec3(int n) const {
273 nassertr(n >= 0 && n < (int)_components.size(), LVector3d::zero());
274 nassertr(_components[n]._vec3 != nullptr, LVector3d::zero());
275 return *_components[n]._vec3;
276}
277
278/**
279 * Returns the 3x3 matrix associated with the nth component. It is an error
280 * to call this if the component type is not CT_matrix3.
281 */
282INLINE const LMatrix3d &EggTransform::
283get_component_mat3(int n) const {
284 nassertr(n >= 0 && n < (int)_components.size(), LMatrix3d::ident_mat());
285 nassertr(_components[n]._mat3 != nullptr, LMatrix3d::ident_mat());
286 return *_components[n]._mat3;
287}
288
289/**
290 * Returns the 4x4 matrix associated with the nth component. It is an error
291 * to call this if the component type is not CT_matrix4.
292 */
293INLINE const LMatrix4d &EggTransform::
294get_component_mat4(int n) const {
295 nassertr(n >= 0 && n < (int)_components.size(), LMatrix4d::ident_mat());
296 nassertr(_components[n]._mat4 != nullptr, LMatrix4d::ident_mat());
297 return *_components[n]._mat4;
298}
299
300/**
301 * Sets the overall transform without calling transform_changed().
302 */
303INLINE void EggTransform::
304internal_set_transform(const LMatrix3d &mat) {
305 internal_clear_transform();
306 internal_add_matrix(mat);
307}
308
309/**
310 * Sets the overall transform without calling transform_changed().
311 */
312INLINE void EggTransform::
313internal_set_transform(const LMatrix4d &mat) {
314 internal_clear_transform();
315 internal_add_matrix(mat);
316}
const LMatrix4d & get_transform3d() const
Returns the overall transform as a 4x4 matrix.
bool has_transform() const
Returns true if the transform is nonempty, false if it is empty (no transform components have been ad...
const LVecBase3d & get_component_vec3(int n) const
Returns the 3-component vector associated with the nth component.
ComponentType get_component_type(int n) const
Returns the type of the nth component.
bool has_transform3d() const
Returns true if the transform is specified as a 3-d transform, e.g.
void clear_transform()
Resets the transform to empty, identity.
void set_transform3d(const LMatrix4d &mat)
Sets the overall transform as a 4x4 matrix.
const LVecBase2d & get_component_vec2(int n) const
Returns the 2-component vector associated with the nth component.
bool has_transform2d() const
Returns true if the transform is specified as a 2-d transform, e.g.
int get_num_components() const
Returns the number of components that make up the transform.
const LMatrix3d & get_component_mat3(int n) const
Returns the 3x3 matrix associated with the nth component.
void add_matrix4(const LMatrix4d &mat)
Appends an arbitrary 4x4 matrix to the current transform.
const LMatrix4d & get_component_mat4(int n) const
Returns the 4x4 matrix associated with the nth component.
void set_transform2d(const LMatrix3d &mat)
Sets the overall transform as a 3x3 matrix.
double get_component_number(int n) const
Returns the solitary number associated with the nth component.
LMatrix3d get_transform2d() const
Returns the overall transform as a 3x3 matrix.
bool transform_is_identity() const
Returns true if the described transform is identity, false otherwise.
void add_matrix3(const LMatrix3d &mat)
Appends an arbitrary 3x3 matrix to the current transform.