Add ClusterShadingStage
[opensg.git] / Source / System / GraphOp / OSGPruneGraphOp.cpp
blob0271d90f8e95784e5d4a2ed0153d0b6633f55c5c
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2002 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 \*---------------------------------------------------------------------------*/
40 /***************************************************************************\
41 * Includes *
42 \***************************************************************************/
44 #include "OSGPruneGraphOp.h"
45 #include "OSGGraphOpFactory.h"
47 OSG_USING_NAMESPACE
49 /***************************************************************************\
50 * Description *
51 \***************************************************************************/
53 /*! \class OSG::PruneGraphOp
54 \ingroup GrpSystemNodeCoresDrawablesGeometry
56 Removes nodes of size smaller than a given threshold from the scene.
59 namespace
61 //! Register the GraphOp with the factory
62 static bool registerOp(void)
64 GraphOpRefPtr newOp = PruneGraphOp::create();
66 GraphOpFactory::the()->registerOp(newOp);
67 return true;
70 static OSG::StaticInitFuncWrapper registerOpWrapper(registerOp);
72 } // namespace
75 PruneGraphOp::PruneGraphOp(float size, Method method, const char* name)
76 : GraphOp(name)
77 , _size (size)
78 , _method(method)
82 PruneGraphOp::~PruneGraphOp(void)
86 PruneGraphOpTransitPtr
87 PruneGraphOp::create(float size, Method method)
89 return PruneGraphOpTransitPtr(new PruneGraphOp(size, method));
92 GraphOpTransitPtr PruneGraphOp::clone(void)
94 return GraphOpTransitPtr(new PruneGraphOp(_size, _method));
97 void PruneGraphOp::setParams(const std::string params)
99 ParamSet ps(params);
101 ps("size", _size);
103 std::string m;
104 ps("method", m);
106 if(m.length())
108 if(m.find("volume") || m.find("VOLUME"))
110 _method = VOLUME;
112 else if(m.find("sum_of_dimensions") || m.find("SUM_OF_DIMENSIONS") ||
113 m.find("sum"))
115 _method = SUM_OF_DIMENSIONS;
117 else
119 FWARNING(("GeoTypeGraphOp: method '%s' unknown.\n", m.c_str()));
123 std::string out = ps.getUnusedParams();
124 if(out.length())
126 FWARNING(("PruneGraphOp doesn't have parameters '%s'.\n",
127 out.c_str()));
131 std::string PruneGraphOp::usage(void)
133 return
134 "Prune: Remove small objects\n"
135 " Removes nodes of size smaller than a given threshold from the scene\n"
136 "Params: name (type, default)\n"
137 " method (string, SUM_OF_DIMENSIONS): \n"
138 " VOLUME: measure volume of Node\n"
139 " SUM_OF_DIMENSIONS: add upp the individual dims\n"
140 " size (Real32, 1.0f): \n"
141 " threshold value. Nodes smaller than this will be\n"
142 " removed\n";
145 Action::ResultE PruneGraphOp::traverseEnter(Node * const node)
147 return isTooSmall(node) ? Action::Skip : Action::Continue;
150 Action::ResultE PruneGraphOp::traverseLeave(Node * const node, Action::ResultE res)
152 for(UInt32 i = 0; i < node->getNChildren(); ++i)
154 if(isTooSmall(node->getChild(i)))
156 node->subChild(i);
157 --i;
161 return res;
164 bool PruneGraphOp::isTooSmall(Node * const node)
166 return getSize(node) < _size;
169 float PruneGraphOp::getSize(Node * const node)
171 const BoxVolume& bv = node->editVolume(true);
173 if(_method == VOLUME)
175 return bv.getScalarVolume();
177 else if(_method == SUM_OF_DIMENSIONS)
179 Pnt3f min, max;
180 bv.getBounds(min, max);
181 Vec3f diff = max - min;
182 return diff[0] + diff[1] + diff[2];
184 else
186 SWARNING << "Unknown size calculation method" << std::endl;
187 return 0;