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

pandatool/src/eggprogs/eggCrop.cxx

Go to the documentation of this file.
00001 // Filename: eggCrop.cxx
00002 // Created by:  drose (10Jun02)
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 "eggCrop.h"
00020 
00021 #include "eggGroupNode.h"
00022 #include "eggPrimitive.h"
00023 #include "eggVertex.h"
00024 #include "dcast.h"
00025 
00026 ////////////////////////////////////////////////////////////////////
00027 //     Function: EggCrop::Constructor
00028 //       Access: Public
00029 //  Description:
00030 ////////////////////////////////////////////////////////////////////
00031 EggCrop::
00032 EggCrop() {
00033   set_program_description
00034     ("egg-crop strips out all parts of an egg file that fall outside of an "
00035      "arbitrary bounding volume, specified with a minimum and maximum point "
00036      "in world coordinates.");
00037 
00038   add_option
00039     ("min", "x,y,z", 0,
00040      "Specify the minimum point.",
00041      &EggCrop::dispatch_double_triple, &_got_min, &_min[0]);
00042 
00043   add_option
00044     ("max", "x,y,z", 0,
00045      "Specify the maximum point.",
00046      &EggCrop::dispatch_double_triple, &_got_max, &_max[0]);
00047 }
00048 
00049 ////////////////////////////////////////////////////////////////////
00050 //     Function: EggCrop::post_command_line
00051 //       Access: Public, Virtual
00052 //  Description: This is called after the command line has been
00053 //               completely processed, and it gives the program a
00054 //               chance to do some last-minute processing and
00055 //               validation of the options and arguments.  It should
00056 //               return true if everything is fine, false if there is
00057 //               an error.
00058 ////////////////////////////////////////////////////////////////////
00059 bool EggCrop::
00060 post_command_line() {
00061   if (!_got_min || !_got_max) {
00062     nout << "You must specify both a minimum and a maximum bounds.\n";
00063     return false;
00064   }
00065     
00066   return true;
00067 }
00068 
00069 ////////////////////////////////////////////////////////////////////
00070 //     Function: EggCrop::run
00071 //       Access: Public
00072 //  Description:
00073 ////////////////////////////////////////////////////////////////////
00074 void EggCrop::
00075 run() {
00076   int num_removed = strip_prims(&_data);
00077   nout << "Removed " << num_removed << " primitives.\n";
00078 
00079   _data.remove_unused_vertices();
00080   write_egg_file();
00081 }
00082 
00083 
00084 ////////////////////////////////////////////////////////////////////
00085 //     Function: EggCrop::strip_prims
00086 //       Access: Private
00087 //  Description: Recursively walks the scene graph, looking for
00088 //               primitives that exceed the specified bounding volume,
00089 //               and removes them.  Returns the number of primitives
00090 //               removed.
00091 ////////////////////////////////////////////////////////////////////
00092 int EggCrop::
00093 strip_prims(EggGroupNode *group) {
00094   int num_removed = 0;
00095 
00096   EggGroupNode::iterator ci;
00097   ci = group->begin();
00098   while (ci != group->end()) {
00099     EggNode *child = (*ci);
00100     bool all_in = true;
00101 
00102     if (child->is_of_type(EggPrimitive::get_class_type())) {
00103       EggPrimitive *prim = DCAST(EggPrimitive, child);
00104       EggPrimitive::iterator vi;
00105       for (vi = prim->begin(); vi != prim->end() && all_in; ++vi) {
00106         EggVertex *vert = (*vi);
00107         LPoint3d pos = vert->get_pos3();
00108 
00109         all_in = (pos[0] >= _min[0] && pos[0] <= _max[0] &&
00110                   pos[1] >= _min[1] && pos[1] <= _max[1] &&
00111                   pos[2] >= _min[2] && pos[2] <= _max[2]);
00112           
00113       }
00114     }
00115 
00116     if (!all_in) {
00117       // Reject this primitive.
00118       ci = group->erase(ci);
00119       num_removed++;
00120     } else {
00121       // Keep this primitive.
00122       if (child->is_of_type(EggGroupNode::get_class_type())) {
00123         EggGroupNode *group_child = DCAST(EggGroupNode, child);
00124         num_removed += strip_prims(group_child);
00125       }
00126       ++ci;
00127     }
00128   }
00129 
00130   return num_removed;
00131 }
00132 
00133 
00134 int main(int argc, char *argv[]) {
00135   EggCrop prog;
00136   prog.parse_command_line(argc, argv);
00137   prog.run();
00138   return 0;
00139 }

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