Panda3D
 All Classes Functions Variables Enumerations
eggTransform.I
00001 // Filename: eggTransform.I
00002 // Created by:  drose (21Jun02)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 ////////////////////////////////////////////////////////////////////
00016 //     Function: EggTransform::Component::Constructor
00017 //       Access: Public
00018 //  Description: 
00019 ////////////////////////////////////////////////////////////////////
00020 INLINE EggTransform::Component::
00021 Component(EggTransform::ComponentType type, double number) :
00022   _type(type),
00023   _number(number)
00024 {
00025   _vec2 = (LVecBase2d *)NULL;
00026   _vec3 = (LVecBase3d *)NULL;
00027   _mat3 = (LMatrix3d *)NULL;
00028   _mat4 = (LMatrix4d *)NULL;
00029 }
00030 
00031 ////////////////////////////////////////////////////////////////////
00032 //     Function: EggTransform::Component::Copy Constructor
00033 //       Access: Public
00034 //  Description: 
00035 ////////////////////////////////////////////////////////////////////
00036 INLINE EggTransform::Component::
00037 Component(const EggTransform::Component &copy) :
00038   _type(copy._type),
00039   _number(copy._number)
00040 {
00041   _vec2 = (LVecBase2d *)NULL;
00042   _vec3 = (LVecBase3d *)NULL;
00043   _mat3 = (LMatrix3d *)NULL;
00044   _mat4 = (LMatrix4d *)NULL;
00045   if (copy._vec2 != (LVecBase2d *)NULL) {
00046     _vec2 = new LVector2d(*copy._vec2);
00047   }
00048   if (copy._vec3 != (LVecBase3d *)NULL) {
00049     _vec3 = new LVector3d(*copy._vec3);
00050   }
00051   if (copy._mat3 != (LMatrix3d *)NULL) {
00052     _mat3 = new LMatrix3d(*copy._mat3);
00053   }
00054   if (copy._mat4 != (LMatrix4d *)NULL) {
00055     _mat4 = new LMatrix4d(*copy._mat4);
00056   }
00057 }
00058 
00059 ////////////////////////////////////////////////////////////////////
00060 //     Function: EggTransform::Component::Copy Assignment Operator
00061 //       Access: Public
00062 //  Description: 
00063 ////////////////////////////////////////////////////////////////////
00064 INLINE void EggTransform::Component::
00065 operator = (const EggTransform::Component &copy) {
00066   _type = copy._type;
00067   _number = copy._number;
00068   if (_vec2 != (LVecBase2d *)NULL) {
00069     delete _vec2;
00070     _vec2 = (LVecBase2d *)NULL;
00071   }
00072   if (_vec3 != (LVecBase3d *)NULL) {
00073     delete _vec3;
00074     _vec3 = (LVecBase3d *)NULL;
00075   }
00076   if (_mat3 != (LMatrix3d *)NULL) {
00077     delete _mat3;
00078     _mat3 = (LMatrix3d *)NULL;
00079   }
00080   if (_mat4 != (LMatrix4d *)NULL) {
00081     delete _mat4;
00082     _mat4 = (LMatrix4d *)NULL;
00083   }
00084   if (copy._vec2 != (LVecBase2d *)NULL) {
00085     _vec2 = new LVecBase2d(*copy._vec2);
00086   }
00087   if (copy._vec3 != (LVecBase3d *)NULL) {
00088     _vec3 = new LVecBase3d(*copy._vec3);
00089   }
00090   if (copy._mat3 != (LMatrix3d *)NULL) {
00091     _mat3 = new LMatrix3d(*copy._mat3);
00092   }
00093   if (copy._mat4 != (LMatrix4d *)NULL) {
00094     _mat4 = new LMatrix4d(*copy._mat4);
00095   }
00096 }
00097 
00098 ////////////////////////////////////////////////////////////////////
00099 //     Function: EggTransform::Component::Destructor
00100 //       Access: Public
00101 //  Description: 
00102 ////////////////////////////////////////////////////////////////////
00103 INLINE EggTransform::Component::
00104 ~Component() {
00105   if (_vec2 != (LVecBase2d *)NULL) {
00106     delete _vec2;
00107   }
00108   if (_vec3 != (LVecBase3d *)NULL) {
00109     delete _vec3;
00110   }
00111   if (_mat3 != (LMatrix3d *)NULL) {
00112     delete _mat3;
00113   }
00114   if (_mat4 != (LMatrix4d *)NULL) {
00115     delete _mat4;
00116   }
00117 }
00118 
00119 ////////////////////////////////////////////////////////////////////
00120 //     Function: EggTransform::clear_transform
00121 //       Access: Public
00122 //  Description: Resets the transform to empty, identity.
00123 ////////////////////////////////////////////////////////////////////
00124 INLINE void EggTransform::
00125 clear_transform() {
00126   internal_clear_transform();
00127   transform_changed();
00128 }
00129 
00130 ////////////////////////////////////////////////////////////////////
00131 //     Function: EggTransform::add_matrix3
00132 //       Access: Public
00133 //  Description: Appends an arbitrary 3x3 matrix to the current
00134 //               transform.
00135 ////////////////////////////////////////////////////////////////////
00136 INLINE void EggTransform::
00137 add_matrix3(const LMatrix3d &mat) {
00138   internal_add_matrix(mat);
00139   transform_changed();
00140 }
00141 
00142 ////////////////////////////////////////////////////////////////////
00143 //     Function: EggTransform::add_matrix4
00144 //       Access: Public
00145 //  Description: Appends an arbitrary 4x4 matrix to the current
00146 //               transform.
00147 ////////////////////////////////////////////////////////////////////
00148 INLINE void EggTransform::
00149 add_matrix4(const LMatrix4d &mat) {
00150   internal_add_matrix(mat);
00151   transform_changed();
00152 }
00153 
00154 ////////////////////////////////////////////////////////////////////
00155 //     Function: EggTransform::has_transform
00156 //       Access: Public
00157 //  Description: Returns true if the transform is nonempty, false if
00158 //               it is empty (no transform components have been
00159 //               added).  This is true for either a 2-d or a 3-d
00160 //               transform.
00161 ////////////////////////////////////////////////////////////////////
00162 INLINE bool EggTransform::
00163 has_transform() const {
00164   return !_components.empty();
00165 }
00166 
00167 ////////////////////////////////////////////////////////////////////
00168 //     Function: EggTransform::has_transform2d
00169 //       Access: Public
00170 //  Description: Returns true if the transform is specified as a 2-d
00171 //               transform, e.g. with a 3x3 matrix, or false if it is
00172 //               specified as a 3-d transform (with a 4x4 matrix), or
00173 //               not specified at all.
00174 //
00175 //               Normally, EggTextures have a 2-d matrix (but
00176 //               occasionally they use a 3-d matrix), and EggGroups
00177 //               always have a 3-d matrix.
00178 ////////////////////////////////////////////////////////////////////
00179 INLINE bool EggTransform::
00180 has_transform2d() const {
00181   return has_transform() && _is_transform_2d;
00182 }
00183 
00184 ////////////////////////////////////////////////////////////////////
00185 //     Function: EggTransform::set_transform2d
00186 //       Access: Public
00187 //  Description: Sets the overall transform as a 3x3 matrix.  This
00188 //               completely replaces whatever componentwise transform
00189 //               may have been defined.
00190 ////////////////////////////////////////////////////////////////////
00191 INLINE void EggTransform::
00192 set_transform2d(const LMatrix3d &mat) {
00193   internal_set_transform(mat);
00194   transform_changed();
00195 }
00196 
00197 ////////////////////////////////////////////////////////////////////
00198 //     Function: EggTransform::has_transform3d
00199 //       Access: Public
00200 //  Description: Returns true if the transform is specified as a 3-d
00201 //               transform, e.g. with a 4x4 matrix, or false if it is
00202 //               specified as a 2-d transform (with a 2x2 matrix), or
00203 //               not specified at all.
00204 //
00205 //               Normally, EggTextures have a 3-d matrix (but
00206 //               occasionally they use a 3-d matrix), and EggGroups
00207 //               always have a 3-d matrix.
00208 ////////////////////////////////////////////////////////////////////
00209 INLINE bool EggTransform::
00210 has_transform3d() const {
00211   return has_transform() && !_is_transform_2d;
00212 }
00213 
00214 ////////////////////////////////////////////////////////////////////
00215 //     Function: EggTransform::set_transform3d
00216 //       Access: Public
00217 //  Description: Sets the overall transform as a 4x4 matrix.  This
00218 //               completely replaces whatever componentwise transform
00219 //               may have been defined.
00220 ////////////////////////////////////////////////////////////////////
00221 INLINE void EggTransform::
00222 set_transform3d(const LMatrix4d &mat) {
00223   internal_set_transform(mat);
00224   transform_changed();
00225 }
00226 
00227 ////////////////////////////////////////////////////////////////////
00228 //     Function: EggTransform::get_transform2d
00229 //       Access: Public
00230 //  Description: Returns the overall transform as a 3x3 matrix.  It is
00231 //               an error to call this if has_transform3d() is true.
00232 ////////////////////////////////////////////////////////////////////
00233 INLINE LMatrix3d EggTransform::
00234 get_transform2d() const {
00235   nassertr(!has_transform3d(), LMatrix3d::ident_mat());
00236   const LMatrix4d &t = _transform;
00237   return LMatrix3d(t(0, 0), t(0, 1), t(0, 3),
00238                    t(1, 0), t(1, 1), t(1, 3),
00239                    t(3, 0), t(3, 1), t(3, 3));
00240 }
00241 
00242 ////////////////////////////////////////////////////////////////////
00243 //     Function: EggTransform::get_transform3d
00244 //       Access: Public
00245 //  Description: Returns the overall transform as a 4x4 matrix.  It is
00246 //               valid to call this even if has_transform2d() is true;
00247 //               in this case, the 3x3 transform will be expanded to a
00248 //               4x4 matrix.
00249 ////////////////////////////////////////////////////////////////////
00250 INLINE const LMatrix4d &EggTransform::
00251 get_transform3d() const {
00252   return _transform;
00253 }
00254 
00255 ////////////////////////////////////////////////////////////////////
00256 //     Function: EggTransform::transform_is_identity
00257 //       Access: Public
00258 //  Description: Returns true if the described transform is identity,
00259 //               false otherwise.
00260 ////////////////////////////////////////////////////////////////////
00261 INLINE bool EggTransform::
00262 transform_is_identity() const {
00263   return _components.empty() ||
00264     _transform.almost_equal(LMatrix4d::ident_mat(), 0.0001);
00265 }
00266 
00267 ////////////////////////////////////////////////////////////////////
00268 //     Function: EggTransform::get_num_components
00269 //       Access: Public
00270 //  Description: Returns the number of components that make up the
00271 //               transform.
00272 ////////////////////////////////////////////////////////////////////
00273 INLINE int EggTransform::
00274 get_num_components() const {
00275   return _components.size();
00276 }
00277 
00278 ////////////////////////////////////////////////////////////////////
00279 //     Function: EggTransform::get_component_type
00280 //       Access: Public
00281 //  Description: Returns the type of the nth component.
00282 ////////////////////////////////////////////////////////////////////
00283 INLINE EggTransform::ComponentType EggTransform::
00284 get_component_type(int n) const {
00285   nassertr(n >= 0 && n < (int)_components.size(), CT_invalid);
00286   return _components[n]._type;
00287 }
00288 
00289 ////////////////////////////////////////////////////////////////////
00290 //     Function: EggTransform::get_component_number
00291 //       Access: Public
00292 //  Description: Returns the solitary number associated with the nth
00293 //               component.  In the case of a rotation, this is the
00294 //               angle in degrees to rotate; in the case of uniform
00295 //               scale, this is the amount of the scale.  Other types
00296 //               do not use this property.
00297 ////////////////////////////////////////////////////////////////////
00298 INLINE double EggTransform::
00299 get_component_number(int n) const {
00300   nassertr(n >= 0 && n < (int)_components.size(), 0.0);
00301   return _components[n]._number;
00302 }
00303 
00304 ////////////////////////////////////////////////////////////////////
00305 //     Function: EggTransform::get_component_vec2
00306 //       Access: Public
00307 //  Description: Returns the 2-component vector associated with the
00308 //               nth component.  This may be the translate vector,
00309 //               rotate axis, or non-uniform scale.  It is an error to
00310 //               call this if the component type does not use a 2-d
00311 //               vector property.
00312 ////////////////////////////////////////////////////////////////////
00313 INLINE const LVecBase2d &EggTransform::
00314 get_component_vec2(int n) const {
00315   nassertr(n >= 0 && n < (int)_components.size(), LVector2d::zero());
00316   nassertr(_components[n]._vec2 != (LVecBase2d *)NULL, LVector2d::zero());
00317   return *_components[n]._vec2;
00318 }
00319 
00320 ////////////////////////////////////////////////////////////////////
00321 //     Function: EggTransform::get_component_vec3
00322 //       Access: Public
00323 //  Description: Returns the 3-component vector associated with the
00324 //               nth component.  This may be the translate vector,
00325 //               rotate axis, or non-uniform scale.  It is an error to
00326 //               call this if the component type does not use a 3-d
00327 //               vector property.
00328 ////////////////////////////////////////////////////////////////////
00329 INLINE const LVecBase3d &EggTransform::
00330 get_component_vec3(int n) const {
00331   nassertr(n >= 0 && n < (int)_components.size(), LVector3d::zero());
00332   nassertr(_components[n]._vec3 != (LVecBase3d *)NULL, LVector3d::zero());
00333   return *_components[n]._vec3;
00334 }
00335 
00336 ////////////////////////////////////////////////////////////////////
00337 //     Function: EggTransform::get_component_mat3
00338 //       Access: Public
00339 //  Description: Returns the 3x3 matrix associated with the nth
00340 //               component.  It is an error to call this if the
00341 //               component type is not CT_matrix3.
00342 ////////////////////////////////////////////////////////////////////
00343 INLINE const LMatrix3d &EggTransform::
00344 get_component_mat3(int n) const {
00345   nassertr(n >= 0 && n < (int)_components.size(), LMatrix3d::ident_mat());
00346   nassertr(_components[n]._mat3 != (LMatrix3d *)NULL, LMatrix3d::ident_mat());
00347   return *_components[n]._mat3;
00348 }
00349 
00350 ////////////////////////////////////////////////////////////////////
00351 //     Function: EggTransform::get_component_mat4
00352 //       Access: Public
00353 //  Description: Returns the 4x4 matrix associated with the nth
00354 //               component.  It is an error to call this if the
00355 //               component type is not CT_matrix4.
00356 ////////////////////////////////////////////////////////////////////
00357 INLINE const LMatrix4d &EggTransform::
00358 get_component_mat4(int n) const {
00359   nassertr(n >= 0 && n < (int)_components.size(), LMatrix4d::ident_mat());
00360   nassertr(_components[n]._mat4 != (LMatrix4d *)NULL, LMatrix4d::ident_mat());
00361   return *_components[n]._mat4;
00362 }
00363 
00364 ////////////////////////////////////////////////////////////////////
00365 //     Function: EggTransform::internal_set_transform
00366 //       Access: Protected
00367 //  Description: Sets the overall transform without calling
00368 //               transform_changed().
00369 ////////////////////////////////////////////////////////////////////
00370 INLINE void EggTransform::
00371 internal_set_transform(const LMatrix3d &mat) {
00372   internal_clear_transform();
00373   internal_add_matrix(mat);
00374 }
00375 
00376 ////////////////////////////////////////////////////////////////////
00377 //     Function: EggTransform::internal_set_transform
00378 //       Access: Protected
00379 //  Description: Sets the overall transform without calling
00380 //               transform_changed().
00381 ////////////////////////////////////////////////////////////////////
00382 INLINE void EggTransform::
00383 internal_set_transform(const LMatrix4d &mat) {
00384   internal_clear_transform();
00385   internal_add_matrix(mat);
00386 }
 All Classes Functions Variables Enumerations