Panda3D
 All Classes Functions Variables Enumerations
eggCrop.cxx
1 // Filename: eggCrop.cxx
2 // Created by: drose (10Jun02)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "eggCrop.h"
16 
17 #include "eggGroupNode.h"
18 #include "eggPrimitive.h"
19 #include "eggVertex.h"
20 #include "dcast.h"
21 #include "pystub.h"
22 
23 ////////////////////////////////////////////////////////////////////
24 // Function: EggCrop::Constructor
25 // Access: Public
26 // Description:
27 ////////////////////////////////////////////////////////////////////
28 EggCrop::
29 EggCrop() {
30  set_program_brief("crop geometry in an .egg file");
31  set_program_description
32  ("egg-crop strips out all parts of an egg file that fall outside of an "
33  "arbitrary bounding volume, specified with a minimum and maximum point "
34  "in world coordinates.");
35 
36  add_option
37  ("min", "x,y,z", 0,
38  "Specify the minimum point.",
39  &EggCrop::dispatch_double_triple, &_got_min, &_min[0]);
40 
41  add_option
42  ("max", "x,y,z", 0,
43  "Specify the maximum point.",
44  &EggCrop::dispatch_double_triple, &_got_max, &_max[0]);
45 }
46 
47 ////////////////////////////////////////////////////////////////////
48 // Function: EggCrop::post_command_line
49 // Access: Public, Virtual
50 // Description: This is called after the command line has been
51 // completely processed, and it gives the program a
52 // chance to do some last-minute processing and
53 // validation of the options and arguments. It should
54 // return true if everything is fine, false if there is
55 // an error.
56 ////////////////////////////////////////////////////////////////////
57 bool EggCrop::
59  if (!_got_min || !_got_max) {
60  nout << "You must specify both a minimum and a maximum bounds.\n";
61  return false;
62  }
63 
64  return true;
65 }
66 
67 ////////////////////////////////////////////////////////////////////
68 // Function: EggCrop::run
69 // Access: Public
70 // Description:
71 ////////////////////////////////////////////////////////////////////
72 void EggCrop::
73 run() {
74  int num_removed = strip_prims(_data);
75  nout << "Removed " << num_removed << " primitives.\n";
76 
77  _data->remove_unused_vertices(true);
79 }
80 
81 
82 ////////////////////////////////////////////////////////////////////
83 // Function: EggCrop::strip_prims
84 // Access: Private
85 // Description: Recursively walks the scene graph, looking for
86 // primitives that exceed the specified bounding volume,
87 // and removes them. Returns the number of primitives
88 // removed.
89 ////////////////////////////////////////////////////////////////////
90 int EggCrop::
91 strip_prims(EggGroupNode *group) {
92  int num_removed = 0;
93 
94  EggGroupNode::iterator ci;
95  ci = group->begin();
96  while (ci != group->end()) {
97  EggNode *child = (*ci);
98  bool all_in = true;
99 
100  if (child->is_of_type(EggPrimitive::get_class_type())) {
101  EggPrimitive *prim = DCAST(EggPrimitive, child);
102  EggPrimitive::iterator vi;
103  for (vi = prim->begin(); vi != prim->end() && all_in; ++vi) {
104  EggVertex *vert = (*vi);
105  LPoint3d pos = vert->get_pos3();
106 
107  all_in = (pos[0] >= _min[0] && pos[0] <= _max[0] &&
108  pos[1] >= _min[1] && pos[1] <= _max[1] &&
109  pos[2] >= _min[2] && pos[2] <= _max[2]);
110 
111  }
112  }
113 
114  if (!all_in) {
115  // Reject this primitive.
116  ci = group->erase(ci);
117  num_removed++;
118  } else {
119  // Keep this primitive.
120  if (child->is_of_type(EggGroupNode::get_class_type())) {
121  EggGroupNode *group_child = DCAST(EggGroupNode, child);
122  num_removed += strip_prims(group_child);
123  }
124  ++ci;
125  }
126  }
127 
128  return num_removed;
129 }
130 
131 
132 int main(int argc, char *argv[]) {
133  // A call to pystub() to force libpystub.so to be linked in.
134  pystub();
135 
136  EggCrop prog;
137  prog.parse_command_line(argc, argv);
138  prog.run();
139  return 0;
140 }
A base class for any of a number of kinds of geometry primitives: polygons, point lights...
Definition: eggPrimitive.h:51
virtual void parse_command_line(int argc, char **argv)
Dispatches on each of the options on the command line, and passes the remaining parameters to handle_...
A base class for nodes in the hierarchy that are not leaf nodes.
Definition: eggGroupNode.h:51
bool is_of_type(TypeHandle handle) const
Returns true if the current object is or derives from the indicated type.
Definition: typedObject.I:63
LVertexd get_pos3() const
Valid if get_num_dimensions() returns 3 or 4.
Definition: eggVertex.I:160
A program to read an egg file and write an equivalent egg file, possibly performing some minor operat...
Definition: eggCrop.h:30
Any one-, two-, three-, or four-component vertex, possibly with attributes such as a normal...
Definition: eggVertex.h:41
virtual bool post_command_line()
This is called after the command line has been completely processed, and it gives the program a chanc...
Definition: eggCrop.cxx:58
void write_egg_file()
Writes out the egg file as the normal result of the program.
Definition: eggWriter.cxx:193
This is a three-component point in space (as opposed to a three-component vector, which represents a ...
Definition: lpoint3.h:531
A base class for things that may be directly added into the egg hierarchy.
Definition: eggNode.h:38