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