00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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
00028
00029
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
00051
00052
00053
00054
00055
00056
00057
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
00071
00072
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
00086
00087
00088
00089
00090
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
00118 ci = group->erase(ci);
00119 num_removed++;
00120 } else {
00121
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 }