Panda3D
 All Classes Functions Variables Enumerations
geomNode.I
1 // Filename: geomNode.I
2 // Created by: drose (23Feb02)
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 
16 ////////////////////////////////////////////////////////////////////
17 // Function: GeomNode::set_preserved
18 // Access: Published
19 // Description: Sets the "preserved" flag. When this is true, the
20 // GeomNode will be left untouched by any flatten
21 // operations.
22 ////////////////////////////////////////////////////////////////////
23 INLINE void GeomNode::
24 set_preserved(bool value) {
25  _preserved = value;
26 }
27 
28 ////////////////////////////////////////////////////////////////////
29 // Function: GeomNode::get_preserved
30 // Access: Published
31 // Description: Returns the "preserved" flag. When this is true, the
32 // GeomNode will be left untouched by any flatten
33 // operations.
34 ////////////////////////////////////////////////////////////////////
35 INLINE bool GeomNode::
36 get_preserved() const {
37  return _preserved;
38 }
39 
40 ////////////////////////////////////////////////////////////////////
41 // Function: GeomNode::get_num_geoms
42 // Access: Published
43 // Description: Returns the number of geoms in the node.
44 ////////////////////////////////////////////////////////////////////
45 INLINE int GeomNode::
46 get_num_geoms() const {
47  CDReader cdata(_cycler);
48  return cdata->get_geoms()->size();
49 }
50 
51 
52 ////////////////////////////////////////////////////////////////////
53 // Function: GeomNode::get_geom
54 // Access: Published
55 // Description: Returns the nth geom of the node. This object should
56 // not be modified, since the same object might be
57 // shared between multiple different GeomNodes, but see
58 // modify_geom().
59 ////////////////////////////////////////////////////////////////////
60 INLINE CPT(Geom) GeomNode::
61 get_geom(int n) const {
62  CDReader cdata(_cycler);
63  CPT(GeomList) geoms = cdata->get_geoms();
64  nassertr(n >= 0 && n < (int)geoms->size(), NULL);
65  return (*geoms)[n]._geom.get_read_pointer();
66 }
67 
68 ////////////////////////////////////////////////////////////////////
69 // Function: GeomNode::modify_geom
70 // Access: Published
71 // Description: Returns the nth geom of the node, suitable for
72 // modifying it. If the nth Geom has multiple reference
73 // counts to it, reassigns it to an identical copy
74 // first, and returns the new copy--this provides a
75 // "copy on write" that ensures that the Geom that is
76 // returned is unique to this GeomNode and is not shared
77 // with any other GeomNodes.
78 //
79 // Note that if this method is called in a downstream
80 // stage (for instance, during cull or draw), then it
81 // will propagate the new list of Geoms upstream all the
82 // way to pipeline stage 0, which may step on changes
83 // that were made independently in pipeline stage 0.
84 // Use with caution.
85 ////////////////////////////////////////////////////////////////////
86 INLINE PT(Geom) GeomNode::
87 modify_geom(int n) {
88  CDWriter cdata(_cycler, true);
89  PT(GeomList) geoms = cdata->modify_geoms();
90  nassertr(n >= 0 && n < (int)geoms->size(), NULL);
91  mark_internal_bounds_stale();
92  return (*geoms)[n]._geom.get_write_pointer();
93 }
94 
95 ////////////////////////////////////////////////////////////////////
96 // Function: GeomNode::get_geom_state
97 // Access: Published
98 // Description: Returns the RenderState associated with the nth geom
99 // of the node. This is just the RenderState directly
100 // associated with the Geom; the actual state in which
101 // the Geom is rendered will also be affected by
102 // RenderStates that appear on the scene graph in nodes
103 // above this GeomNode.
104 ////////////////////////////////////////////////////////////////////
105 INLINE const RenderState *GeomNode::
106 get_geom_state(int n) const {
107  CDReader cdata(_cycler);
108  CPT(GeomList) geoms = cdata->get_geoms();
109  nassertr(n >= 0 && n < (int)geoms->size(), NULL);
110  return (*geoms)[n]._state;
111 }
112 
113 ////////////////////////////////////////////////////////////////////
114 // Function: GeomNode::set_geom_state
115 // Access: Published
116 // Description: Changes the RenderState associated with the nth geom
117 // of the node. This is just the RenderState directly
118 // associated with the Geom; the actual state in which
119 // the Geom is rendered will also be affected by
120 // RenderStates that appear on the scene graph in nodes
121 // above this GeomNode.
122 //
123 // Note that if this method is called in a downstream
124 // stage (for instance, during cull or draw), then it
125 // will propagate the new list of Geoms upstream all the
126 // way to pipeline stage 0, which may step on changes
127 // that were made independently in pipeline stage 0.
128 // Use with caution.
129 ////////////////////////////////////////////////////////////////////
130 INLINE void GeomNode::
131 set_geom_state(int n, const RenderState *state) {
132  CDWriter cdata(_cycler, true);
133  PT(GeomList) geoms = cdata->modify_geoms();
134  nassertv(n >= 0 && n < (int)geoms->size());
135  (*geoms)[n]._state = state;
136 }
137 
138 ////////////////////////////////////////////////////////////////////
139 // Function: GeomNode::remove_geom
140 // Access: Published
141 // Description: Removes the nth geom from the node.
142 ////////////////////////////////////////////////////////////////////
143 INLINE void GeomNode::
144 remove_geom(int n) {
145  CDWriter cdata(_cycler);
146  PT(GeomList) geoms = cdata->modify_geoms();
147  nassertv(n >= 0 && n < (int)geoms->size());
148 
149  geoms->erase(geoms->begin() + n);
151 }
152 
153 ////////////////////////////////////////////////////////////////////
154 // Function: GeomNode::remove_all_geoms
155 // Access: Published
156 // Description: Removes all the geoms from the node at once.
157 ////////////////////////////////////////////////////////////////////
158 INLINE void GeomNode::
160  CDWriter cdata(_cycler);
161  cdata->set_geoms(new GeomList);
163 }
164 
165 ////////////////////////////////////////////////////////////////////
166 // Function: GeomNode::get_default_collide_mask
167 // Access: Published, Static
168 // Description: Returns the default into_collide_mask assigned to new
169 // GeomNodes.
170 ////////////////////////////////////////////////////////////////////
171 INLINE CollideMask GeomNode::
173  return default_geom_node_collide_mask;
174 }
175 
176 ////////////////////////////////////////////////////////////////////
177 // Function: GeomNode::count_name
178 // Access: Private
179 // Description: Increments the count for the indicated InternalName.
180 ////////////////////////////////////////////////////////////////////
181 INLINE void GeomNode::
182 count_name(GeomNode::NameCount &name_count, const InternalName *name) {
183  pair<NameCount::iterator, bool> result =
184  name_count.insert(NameCount::value_type(name, 1));
185  if (!result.second) {
186  (*result.first).second++;
187  }
188 }
189 
190 ////////////////////////////////////////////////////////////////////
191 // Function: GeomNode::get_name_count
192 // Access: Private
193 // Description: Returns the count for the indicated InternalName.
194 ////////////////////////////////////////////////////////////////////
195 INLINE int GeomNode::
196 get_name_count(const GeomNode::NameCount &name_count, const InternalName *name) {
197  NameCount::const_iterator ni;
198  ni = name_count.find(name);
199  if (ni != name_count.end()) {
200  return (*ni).second;
201  }
202  return 0;
203 }
204 
205 ////////////////////////////////////////////////////////////////////
206 // Function: GeomNode::get_geoms
207 // Access: Published
208 // Description: Returns an object that can be used to walk through
209 // the list of geoms of the node. When you intend to
210 // visit multiple geoms, using this is slightly
211 // faster than calling get_geom() directly on the
212 // GeomNode, since this object avoids reopening the
213 // PipelineCycler each time.
214 //
215 // This object also protects you from self-modifying
216 // loops (e.g. adding or removing geoms during
217 // traversal), since a virtual copy of the geoms is
218 // made ahead of time. The virtual copy is fast--it is
219 // a form of copy-on-write, so the list is not actually
220 // copied unless it is modified during the traversal.
221 ////////////////////////////////////////////////////////////////////
223 get_geoms(Thread *current_thread) const {
224  CDReader cdata(_cycler, current_thread);
225  return Geoms(cdata);
226 }
227 
228 ////////////////////////////////////////////////////////////////////
229 // Function: GeomNode::GeomEntry::Constructor
230 // Access: Public
231 // Description:
232 ////////////////////////////////////////////////////////////////////
233 INLINE GeomNode::GeomEntry::
234 GeomEntry(Geom *geom, const RenderState *state) :
235  _geom(geom),
236  _state(state)
237 {
238 }
239 
240 ////////////////////////////////////////////////////////////////////
241 // Function: GeomNode::CData::Constructor
242 // Access: Public
243 // Description:
244 ////////////////////////////////////////////////////////////////////
245 INLINE GeomNode::CData::
246 CData() :
247  _geoms(new GeomNode::GeomList)
248 {
249 }
250 
251 ////////////////////////////////////////////////////////////////////
252 // Function: GeomNode::CData::get_geoms
253 // Access: Public
254 // Description: Returns a read-only pointer to the _geoms list.
255 ////////////////////////////////////////////////////////////////////
256 INLINE CPT(GeomNode::GeomList) GeomNode::CData::
257 get_geoms() const {
258  return _geoms.get_read_pointer();
259 }
260 
261 ////////////////////////////////////////////////////////////////////
262 // Function: GeomNode::CData::modify_geoms
263 // Access: Public
264 // Description: Returns a modifiable, unique pointer to the _geoms
265 // list.
266 ////////////////////////////////////////////////////////////////////
267 INLINE PT(GeomNode::GeomList) GeomNode::CData::
268 modify_geoms() {
269  return _geoms.get_write_pointer();
270 }
271 
272 ////////////////////////////////////////////////////////////////////
273 // Function: GeomNode::CData::set_geoms
274 // Access: Public
275 // Description: Replaces the _geoms list with a new list.
276 ////////////////////////////////////////////////////////////////////
277 INLINE void GeomNode::CData::
278 set_geoms(GeomNode::GeomList *geoms) {
279  _geoms = geoms;
280 }
281 
282 ////////////////////////////////////////////////////////////////////
283 // Function: GeomNode::Geoms::Constructor
284 // Access: Public
285 // Description:
286 ////////////////////////////////////////////////////////////////////
287 INLINE GeomNode::Geoms::
288 Geoms() {
289 }
290 
291 ////////////////////////////////////////////////////////////////////
292 // Function: GeomNode::Geoms::Constructor
293 // Access: Public
294 // Description:
295 ////////////////////////////////////////////////////////////////////
296 INLINE GeomNode::Geoms::
297 Geoms(const GeomNode::CData *cdata) :
298  _geoms(cdata->get_geoms())
299 {
300 }
301 
302 ////////////////////////////////////////////////////////////////////
303 // Function: GeomNode::Geoms::Copy Constructor
304 // Access: Public
305 // Description:
306 ////////////////////////////////////////////////////////////////////
307 INLINE GeomNode::Geoms::
308 Geoms(const GeomNode::Geoms &copy) :
309  _geoms(copy._geoms)
310 {
311 }
312 
313 ////////////////////////////////////////////////////////////////////
314 // Function: GeomNode::Geoms::Copy Assignment Operator
315 // Access: Public
316 // Description:
317 ////////////////////////////////////////////////////////////////////
318 INLINE void GeomNode::Geoms::
319 operator = (const GeomNode::Geoms &copy) {
320  _geoms = copy._geoms;
321 }
322 
323 #ifdef USE_MOVE_SEMANTICS
324 ////////////////////////////////////////////////////////////////////
325 // Function: GeomNode::Geoms::Move Constructor
326 // Access: Public
327 // Description:
328 ////////////////////////////////////////////////////////////////////
329 INLINE GeomNode::Geoms::
330 Geoms(GeomNode::Geoms &&from) NOEXCEPT :
331  _geoms(move(from._geoms))
332 {
333 }
334 
335 ////////////////////////////////////////////////////////////////////
336 // Function: GeomNode::Geoms::Move Assignment Operator
337 // Access: Public
338 // Description:
339 ////////////////////////////////////////////////////////////////////
340 INLINE void GeomNode::Geoms::
341 operator = (GeomNode::Geoms &&from) NOEXCEPT {
342  _geoms = move(from._geoms);
343 }
344 #endif // USE_MOVE_SEMANTICS
345 
346 ////////////////////////////////////////////////////////////////////
347 // Function: GeomNode::Geoms::get_num_geoms
348 // Access: Public
349 // Description: Returns the number of geoms of the node.
350 ////////////////////////////////////////////////////////////////////
351 INLINE int GeomNode::Geoms::
352 get_num_geoms() const {
353  nassertr(!_geoms.is_null(), 0);
354  return _geoms->size();
355 }
356 
357 ////////////////////////////////////////////////////////////////////
358 // Function: GeomNode::Geoms::get_geom
359 // Access: Public
360 // Description: Returns the nth geom of the node. This object should
361 // not be modified, since the same object might be
362 // shared between multiple different GeomNodes.
363 ////////////////////////////////////////////////////////////////////
364 INLINE CPT(Geom) GeomNode::Geoms::
365 get_geom(int n) const {
366  nassertr(!_geoms.is_null(), NULL);
367  nassertr(n >= 0 && n < (int)_geoms->size(), NULL);
368  return (*_geoms)[n]._geom.get_read_pointer();
369 }
370 
371 ////////////////////////////////////////////////////////////////////
372 // Function: GeomNode::Geoms::get_geom_state
373 // Access: Public
374 // Description: Returns the RenderState associated with the nth geom
375 // of the node. This is just the RenderState directly
376 // associated with the Geom; the actual state in which
377 // the Geom is rendered will also be affected by
378 // RenderStates that appear on the scene graph in nodes
379 // above this GeomNode.
380 ////////////////////////////////////////////////////////////////////
381 INLINE const RenderState *GeomNode::Geoms::
382 get_geom_state(int n) const {
383  nassertr(!_geoms.is_null(), NULL);
384  nassertr(n >= 0 && n < (int)_geoms->size(), NULL);
385  return (*_geoms)[n]._state;
386 }
This is our own Panda specialization on the default STL map.
Definition: pmap.h:52
static CollideMask get_default_collide_mask()
Returns the default into_collide_mask assigned to new GeomNodes.
Definition: geomNode.I:172
Geoms get_geoms(Thread *current_thread=Thread::get_current_thread()) const
Returns an object that can be used to walk through the list of geoms of the node. ...
Definition: geomNode.I:223
This template class calls PipelineCycler::read_unlocked(), and then provides a transparent read-only ...
void mark_internal_bounds_stale(Thread *current_thread=Thread::get_current_thread())
Should be called by a derived class to mark the internal bounding volume stale, so that compute_inter...
Definition: pandaNode.cxx:2467
const RenderState * get_geom_state(int n) const
Returns the RenderState associated with the nth geom of the node.
Definition: geomNode.I:382
void remove_geom(int n)
Removes the nth geom from the node.
Definition: geomNode.I:144
A container for geometry primitives.
Definition: geom.h:58
This template class calls PipelineCycler::write() in the constructor and PipelineCycler::release_writ...
This represents a unique collection of RenderAttrib objects that correspond to a particular renderabl...
Definition: renderState.h:53
bool get_preserved() const
Returns the &quot;preserved&quot; flag.
Definition: geomNode.I:36
A thread; that is, a lightweight process.
Definition: thread.h:51
This is similar to RefCountObj, but it implements a CopyOnWriteObject inheritance instead of a Refere...
int get_num_geoms() const
Returns the number of geoms of the node.
Definition: geomNode.I:352
void set_geom_state(int n, const RenderState *state)
Changes the RenderState associated with the nth geom of the node.
Definition: geomNode.I:131
A general bitmask class.
Definition: bitMask.h:35
A node that holds Geom objects, renderable pieces of geometry.
Definition: geomNode.h:37
int get_num_geoms() const
Returns the number of geoms in the node.
Definition: geomNode.I:46
void set_preserved(bool value)
Sets the &quot;preserved&quot; flag.
Definition: geomNode.I:24
void remove_all_geoms()
Removes all the geoms from the node at once.
Definition: geomNode.I:159