Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

pandatool/src/eggcharbase/eggJointData.cxx

Go to the documentation of this file.
00001 // Filename: eggJointData.cxx
00002 // Created by:  drose (23Feb01)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 #include "eggJointData.h"
00020 #include "eggJointNodePointer.h"
00021 #include "eggMatrixTablePointer.h"
00022 
00023 #include "dcast.h"
00024 #include "eggGroup.h"
00025 #include "eggTable.h"
00026 #include "indent.h"
00027 
00028 
00029 ////////////////////////////////////////////////////////////////////
00030 //     Function: EggJointData::Constructor
00031 //       Access: Public
00032 //  Description:
00033 ////////////////////////////////////////////////////////////////////
00034 EggJointData::
00035 EggJointData(EggCharacterCollection *collection,
00036              EggCharacterData *char_data) :
00037   EggComponentData(collection, char_data)
00038 {
00039   _parent = (EggJointData *)NULL;
00040 }
00041 
00042 ////////////////////////////////////////////////////////////////////
00043 //     Function: EggJointData::find_joint
00044 //       Access: Public
00045 //  Description: Returns the first descendent joint found with the
00046 //               indicated name, or NULL if no joint has that name.
00047 ////////////////////////////////////////////////////////////////////
00048 EggJointData *EggJointData::
00049 find_joint(const string &name) {
00050   Children::const_iterator ci;
00051   for (ci = _children.begin(); ci != _children.end(); ++ci) {
00052     EggJointData *child = (*ci);
00053     if (child->get_name() == name) {
00054       return child;
00055     }
00056     EggJointData *result = child->find_joint(name);
00057     if (result != (EggJointData *)NULL) {
00058       return result;
00059     }
00060   }
00061 
00062   return (EggJointData *)NULL;
00063 }
00064 
00065 ////////////////////////////////////////////////////////////////////
00066 //     Function: EggJointData::get_num_frames
00067 //       Access: Public
00068 //  Description: Returns the number of frames of animation for this
00069 //               particular joint in the indicated model.
00070 ////////////////////////////////////////////////////////////////////
00071 int EggJointData::
00072 get_num_frames(int model_index) const {
00073   EggBackPointer *back = get_model(model_index);
00074   if (back == (EggBackPointer *)NULL) {
00075     return 0;
00076   }
00077 
00078   EggJointPointer *joint;
00079   DCAST_INTO_R(joint, back, 0);
00080 
00081   return joint->get_num_frames();
00082 }
00083 
00084 ////////////////////////////////////////////////////////////////////
00085 //     Function: EggJointData::get_frame
00086 //       Access: Public
00087 //  Description: Returns the local transform matrix corresponding to
00088 //               this joint position in the nth frame in the indicated
00089 //               model.
00090 ////////////////////////////////////////////////////////////////////
00091 LMatrix4d EggJointData::
00092 get_frame(int model_index, int n) const {
00093   EggBackPointer *back = get_model(model_index);
00094   if (back == (EggBackPointer *)NULL) {
00095     return LMatrix4d::ident_mat();
00096   }
00097 
00098   EggJointPointer *joint;
00099   DCAST_INTO_R(joint, back, LMatrix4d::ident_mat());
00100 
00101   return joint->get_frame(n);
00102 }
00103 
00104 ////////////////////////////////////////////////////////////////////
00105 //     Function: EggJointData::get_net_frame
00106 //       Access: Public
00107 //  Description: Returns the complete transform from the root
00108 //               corresponding to this joint position in the nth frame
00109 //               in the indicated model.
00110 ////////////////////////////////////////////////////////////////////
00111 LMatrix4d EggJointData::
00112 get_net_frame(int model_index, int n) const {
00113   LMatrix4d mat = get_frame(model_index, n);
00114   if (_parent != (EggJointData *)NULL) {
00115     mat = mat * _parent->get_net_frame(model_index, n);
00116   }
00117   return mat;
00118 }
00119 
00120 ////////////////////////////////////////////////////////////////////
00121 //     Function: EggJointData::do_rebuild
00122 //       Access: Public
00123 //  Description: Calls do_rebuild() on all models, and recursively on
00124 //               all joints at this node and below.  Returns true if
00125 //               all models returned true, false otherwise.
00126 ////////////////////////////////////////////////////////////////////
00127 bool EggJointData::
00128 do_rebuild() {
00129   bool all_ok = true;
00130 
00131   BackPointers::iterator bpi;
00132   for (bpi = _back_pointers.begin(); bpi != _back_pointers.end(); ++bpi) {
00133     EggBackPointer *back = (*bpi);
00134     if (back != (EggBackPointer *)NULL) {
00135       EggJointPointer *joint;
00136       DCAST_INTO_R(joint, back, false);
00137       if (!joint->do_rebuild()) {
00138         all_ok = false;
00139       }
00140     }
00141   }
00142 
00143   Children::iterator ci;
00144   for (ci = _children.begin(); ci != _children.end(); ++ci) {
00145     EggJointData *child = (*ci);
00146     if (!child->do_rebuild()) {
00147       all_ok = false;
00148     }
00149   }
00150 
00151   return all_ok;
00152 }
00153 
00154 ////////////////////////////////////////////////////////////////////
00155 //     Function: EggJointData::optimize
00156 //       Access: Public
00157 //  Description: Calls optimize() on all models, and recursively on
00158 //               all joints at this node and below.
00159 ////////////////////////////////////////////////////////////////////
00160 void EggJointData::
00161 optimize() {
00162   BackPointers::iterator bpi;
00163   for (bpi = _back_pointers.begin(); bpi != _back_pointers.end(); ++bpi) {
00164     EggBackPointer *back = (*bpi);
00165     if (back != (EggBackPointer *)NULL) {
00166       EggJointPointer *joint;
00167       DCAST_INTO_V(joint, back);
00168       joint->optimize();
00169     }
00170   }
00171 
00172   Children::iterator ci;
00173   for (ci = _children.begin(); ci != _children.end(); ++ci) {
00174     EggJointData *child = (*ci);
00175     child->optimize();
00176   }
00177 }
00178 
00179 ////////////////////////////////////////////////////////////////////
00180 //     Function: EggJointData::add_back_pointer
00181 //       Access: Public, Virtual
00182 //  Description: Adds the indicated model joint or anim table to the
00183 //               data.
00184 ////////////////////////////////////////////////////////////////////
00185 void EggJointData::
00186 add_back_pointer(int model_index, EggObject *egg_object) {
00187   if (egg_object->is_of_type(EggGroup::get_class_type())) {
00188     // It must be a <Joint>.
00189     EggJointNodePointer *joint = new EggJointNodePointer(egg_object);
00190     set_model(model_index, joint);
00191 
00192   } else if (egg_object->is_of_type(EggTable::get_class_type())) {
00193     // It's a <Table> with an "xform" child beneath it.
00194     EggMatrixTablePointer *xform = new EggMatrixTablePointer(egg_object);
00195     set_model(model_index, xform);
00196 
00197   } else {
00198     nout << "Invalid object added to joint for back pointer.\n";
00199   }
00200 }
00201 
00202 ////////////////////////////////////////////////////////////////////
00203 //     Function: EggJointData::write
00204 //       Access: Public, Virtual
00205 //  Description:
00206 ////////////////////////////////////////////////////////////////////
00207 void EggJointData::
00208 write(ostream &out, int indent_level) const {
00209   indent(out, indent_level)
00210     << "Joint " << get_name()
00211     << " (models:";
00212   int num_models = get_num_models();
00213   for (int model_index = 0; model_index < num_models; model_index++) {
00214     if (has_model(model_index)) {
00215       out << " " << model_index;
00216     }
00217   }
00218   out << ") {\n";
00219 
00220   Children::const_iterator ci;
00221   for (ci = _children.begin(); ci != _children.end(); ++ci) {
00222     (*ci)->write(out, indent_level + 2);
00223   }
00224 
00225   indent(out, indent_level) << "}\n";
00226 }

Generated on Fri May 2 03:18:44 2003 for Panda-Tool by doxygen1.3