00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
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
00025
00026
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
00048
00049
00050
00051
00052
00053
00054
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
00068
00069
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
00083
00084
00085
00086
00087
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
00115 ci = group->erase(ci);
00116 num_removed++;
00117 } else {
00118
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
00133 pystub();
00134
00135 EggCrop prog;
00136 prog.parse_command_line(argc, argv);
00137 prog.run();
00138 return 0;
00139 }