Add ClusterShadingStage
[opensg.git] / Source / System / NodeCores / Drawables / Base / OSGDrawableStatsAttachment.cpp
blob8cabaf0dc6b1dc5482acf80b21a9d8898a4cb02a
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2006 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
18 * *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
40 // Includes
41 //---------------------------------------------------------------------------
43 #include <cstdlib>
44 #include <cstdio>
46 #include "OSGConfig.h"
48 #include "OSGDrawableStatsAttachment.h"
49 #include "OSGDrawable.h"
50 #include "OSGNode.h"
52 OSG_BEGIN_NAMESPACE
54 // Documentation for this class is emitted in the
55 // OSGDrawableStatsAttachmentBase.cpp file.
56 // To modify it, please change the .fcd file (OSGDrawableStatsAttachment.fcd) and
57 // regenerate the base file.
59 /***************************************************************************\
60 * Class variables *
61 \***************************************************************************/
63 /***************************************************************************\
64 * Class methods *
65 \***************************************************************************/
67 void DrawableStatsAttachment::initMethod(InitPhase ePhase)
69 Inherited::initMethod(ePhase);
71 if(ePhase == TypeObject::SystemPost)
77 /***************************************************************************\
78 * Instance methods *
79 \***************************************************************************/
81 /*-------------------------------------------------------------------------*\
82 - private -
83 \*-------------------------------------------------------------------------*/
85 /*----------------------- constructors & destructors ----------------------*/
87 DrawableStatsAttachment::DrawableStatsAttachment(void) :
88 Inherited()
92 DrawableStatsAttachment::DrawableStatsAttachment(const DrawableStatsAttachment &source) :
93 Inherited(source)
97 DrawableStatsAttachment::~DrawableStatsAttachment(void)
101 /*----------------------------- class specific ----------------------------*/
103 void DrawableStatsAttachment::changed(ConstFieldMaskArg whichField,
104 UInt32 origin,
105 BitVector details)
107 Inherited::changed(whichField, origin, details);
110 void DrawableStatsAttachment::dump( UInt32 ,
111 const BitVector ) const
113 SLOG << "Dump DrawableStatsAttachment NI" << std::endl;
116 void DrawableStatsAttachment::reset(void)
118 // Don't change it unless it's valid.
119 // Needed to protect intermediate results
120 if(!getValid())
121 return;
123 setVertices (0);
124 setPoints (0);
125 setLines (0);
126 setTriangles (0);
127 setPatches (0);
128 setProcessedAttributeBytes(0);
129 setStoredAttributeBytes (0);
131 setValid(false);
134 /*! Create a GeoStatsAttachment for the given Geometry
137 DrawableStatsAttachmentTransitPtr DrawableStatsAttachment::calcStatic(
138 Drawable *pDrawable)
140 DrawableStatsAttachmentUnrecPtr st = DrawableStatsAttachment::create();
142 if(pDrawable != NULL)
143 pDrawable->fill(st);
145 return DrawableStatsAttachmentTransitPtr(st);
148 /*! Add a new GeoStatsAttachment to the given \a obj.
151 DrawableStatsAttachmentTransitPtr DrawableStatsAttachment::addTo(
152 AttachmentContainer *obj)
154 DrawableStatsAttachmentTransitPtr st = DrawableStatsAttachment::create();
156 st->attachTo(obj);
158 return st;
161 /*! Attach the current GeoStatsAttachment to the given \a obj.
164 void DrawableStatsAttachment::attachTo(AttachmentContainer *obj)
166 if(getMFParents()->size())
168 FNOTICE(("GeoStatsAttachment::attachTo: "
169 "already attached, detaching!\n"));
171 while(getMFParents()->size())
173 AttachmentContainer *p =
174 dynamic_cast<AttachmentContainer *>(this->getParents(0));
176 p->subChangedFunctor(&DrawableStatsAttachment::invalidateFunctor);
177 p->subAttachment (this );
181 obj->addAttachment(this);
183 reset();
185 obj->addChangedFunctor(&DrawableStatsAttachment::invalidateFunctor,
186 "DrawableStats invalidator");
189 void DrawableStatsAttachment::validate(void)
191 commitChanges();
193 // Still valid? Do nothing.
194 if(getValid())
195 return;
197 AttachmentContainer *cont =
198 dynamic_cast<AttachmentContainer *>(this->getParents(0));
200 // Called on a non-AttachmentContainer?
201 if(cont == NULL)
202 return;
204 reset();
206 // Drawable?
207 Drawable *g = dynamic_cast<Drawable *>(cont);
209 if(g != NULL)
211 //calc(g);
212 g->fill(this);
215 // Node?
216 Node *n = dynamic_cast<Node *>(cont);
217 if(n != NULL)
219 // Validate the core
220 g = dynamic_cast<Drawable *>(n->getCore());
221 if(g != NULL)
223 DrawableStatsAttachmentUnrecPtr s = get(g);
225 if(s == NULL)
227 s = DrawableStatsAttachment::addTo(g);
230 s->validate();
232 *this += s;
233 setValid(false); // Not done yet.
236 // Validate all the children
237 for(UInt32 i = 0; i < n->getNChildren(); ++i)
239 Node *c = n->getChild(i);
241 DrawableStatsAttachmentUnrecPtr s = get(c);
243 if(s == NULL)
245 s = DrawableStatsAttachment::addTo(c);
248 s->validate();
250 *this += s;
251 setValid(false); // Not done yet.
254 setValid(true); // Done!
257 void DrawableStatsAttachment::operator +=(DrawableStatsAttachment *arg)
259 setVertices (getVertices() + arg->getVertices());
260 setPoints (getPoints() + arg->getPoints());
261 setLines (getLines() + arg->getLines());
262 setTriangles (getTriangles() + arg->getTriangles());
263 setPatches (getPatches () + arg->getPatches ());
264 setProcessedAttributeBytes(getProcessedAttributeBytes() +
265 arg->getProcessedAttributeBytes());
266 setStoredAttributeBytes (getStoredAttributeBytes() +
267 arg->getStoredAttributeBytes());
268 setValid(true);
271 void DrawableStatsAttachment::operator -=(DrawableStatsAttachment *arg)
273 setVertices (getVertices() - arg->getVertices());
274 setPoints (getPoints() - arg->getPoints());
275 setLines (getLines() - arg->getLines());
276 setTriangles (getTriangles() - arg->getTriangles());
277 setPatches (getPatches () - arg->getPatches ());
278 setProcessedAttributeBytes(getProcessedAttributeBytes() -
279 arg->getProcessedAttributeBytes());
280 setStoredAttributeBytes (getStoredAttributeBytes() -
281 arg->getStoredAttributeBytes());
282 setValid(true);
287 void DrawableStatsAttachment::invalidateFunctor(
288 FieldContainer *obj,
289 BitVector whichField,
290 UInt32 origin )
292 invalidate(obj);
295 void DrawableStatsAttachment::invalidate(FieldContainer *obj)
297 if(obj == NULL)
298 return;
300 AttachmentContainer *cont = dynamic_cast<AttachmentContainer *>(obj);
302 // Called on a non-AttachmentContainer?
303 if(cont == NULL)
304 return;
306 // Find the attachment
307 DrawableStatsAttachment *st = get(cont);
309 if(st == NULL) // Found the end of the chain
310 return;
312 // Invalidate it
313 st->reset();
315 // Traverse upwards
316 if(st->getMFParents()->size())
318 // Can't have more than 1
319 FieldContainer *p =
320 dynamic_cast<FieldContainer *>(st->getParents(0));
322 // Is this attached to a NodeCore?
323 NodeCore *c = dynamic_cast<NodeCore *>(p);
324 if(c != NULL)
326 MFParentFieldContainerPtr::const_iterator pnI;
328 for( pnI = c->getMFParents()->begin();
329 pnI != c->getMFParents()->end ();
330 ++pnI)
332 Node *node = dynamic_cast<Node *>(*pnI);
334 invalidate(node);
338 // Is this attached to a Node?
339 Node *n = dynamic_cast<Node *>(p);
341 if(n != NULL)
343 Node *par = n->getParent();
345 invalidate(par);
350 OSG_END_NAMESPACE