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