root/trunk/Source/System/GraphOp/OSGMaterialMergeGraphOp.cpp

Revision 1387, 7.1 kB (checked in by cneumann, 4 months ago)

Merge branch 'merge_graph_op'

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
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 \*---------------------------------------------------------------------------*/
38
39
40 /***************************************************************************\
41 *                             Includes                                    *
42 \***************************************************************************/
43
44 #include <sstream>
45
46 #include "OSGMaterialMergeGraphOp.h"
47 #include "OSGGraphOpFactory.h"
48 #include "OSGFieldContainerUtils.h"
49
50 #include <boost/next_prior.hpp>
51
52 OSG_BEGIN_NAMESPACE
53
54 /***************************************************************************\
55  *                            Description                                  *
56 \***************************************************************************/
57
58 /*! \class OSG::MaterialMergeGraphOp
59     \ingroup GrpSystemNodeCoresDrawablesGeometry
60
61     Merges equivalent materials in a scene.
62 */
63
64 namespace
65 {
66
67 //! Register the GraphOp with the factory
68 static bool registerOp(void)
69 {
70     GraphOpFactory::the()->registerOp(new MaterialMergeGraphOp);
71     return true;
72 }
73
74 static OSG::StaticInitFuncWrapper registerOpWrapper(registerOp);
75
76 } // namespace
77
78 MaterialMergeGraphOp::MaterialMergeGraphOp(const char* name)
79     : GraphOp(name)
80 {
81 }
82
83 MaterialMergeGraphOp::~MaterialMergeGraphOp(void)
84 {
85 }
86
87 GraphOpTransitPtr MaterialMergeGraphOp::create(void)
88 {
89     return GraphOpTransitPtr(new MaterialMergeGraphOp());
90 }
91
92 bool MaterialMergeGraphOp::traverse(Node *node)
93 {
94     // Find the materials.
95     if(!GraphOp::traverse(node))
96     {
97         return false;
98     }
99
100     SINFO << "Number of materials before merge: "
101           << _materialMap.size() << std::endl;
102
103     // Now do the merge.
104     MaterialObjectMap::iterator mmIt = _materialMap.begin();
105    
106     for (; mmIt != _materialMap.end(); ++mmIt)
107     {
108         Material                    *currentMat  = mmIt->first;
109         MaterialObjectList          &currentList = mmIt->second;
110         MaterialObjectMap::iterator  mmWalker    = boost::next(mmIt);
111        
112         while(mmWalker != _materialMap.end())
113         {
114             // Store the next iterator in case we have to delete
115             // 'walker' from the map.
116             MaterialObjectMap::iterator nextStep = boost::next(mmWalker);
117
118             if(compareContainerEqual(currentMat, mmWalker->first))
119             {
120                 // Set the new objects to have the current material,
121                 // and move the objects to the current list.
122                 MaterialObjectList::iterator mlIt  = mmWalker->second.begin();
123                 MaterialObjectList::iterator mlEnd = mmWalker->second.end  ();
124                
125                 for (; mlIt != mlEnd; ++mlIt)
126                 {
127                     mlIt->setMaterial(currentMat);
128                     currentList.push_back(*mlIt);
129                 }
130                
131                 _materialMap.erase(mmWalker);
132             }
133
134             mmWalker = nextStep;
135         }
136     }
137
138     SINFO << "Number of materials after merge: " << _materialMap.size() << std::endl;
139     return true;
140 }
141
142
143 void MaterialMergeGraphOp::setParams(const std::string params)
144 {
145     ParamSet ps(params);   
146    
147     std::string out = ps.getUnusedParams();
148     if(out.length())
149     {
150         FWARNING(("MaterialMergeGraphOp doesn't have parameters '%s'.\n",
151                 out.c_str()));
152     }
153 }
154
155 std::string MaterialMergeGraphOp::usage(void)
156 {
157     return
158     "MaterialMerge: merge Materials in given subtree\n"
159     "  Tries to find and merge equiavlent Materials to reduce the number\n"
160     "  of Materials used.\n"
161     ;
162 }
163
164 Action::ResultE MaterialMergeGraphOp::traverseEnter(Node * const node)
165 {
166     if(isInExcludeList(node))
167         return Action::Skip;
168    
169     if(isInPreserveList(node))
170         return Action::Continue;
171    
172     MaterialDrawable *md = dynamic_cast<MaterialDrawable *>(node->getCore());
173     if(md != NULL)
174     {
175         addObject(MaterialObject(md));
176         return Action::Continue;
177     }
178    
179     MaterialGroup *mg = dynamic_cast<MaterialGroup *>(node->getCore());
180     if(mg != NULL)
181     {
182         addObject(MaterialObject(mg));
183         return Action::Continue;
184     }
185
186     // Otherwise, keep looking.
187     return Action::Continue;
188 }
189
190 Action::ResultE MaterialMergeGraphOp::traverseLeave(Node * const node, Action::ResultE res)
191 {
192     return res;
193 }
194
195 void MaterialMergeGraphOp::addObject(MaterialObject m)
196 {
197     Material *mat = m.getMaterial();
198     if (mat == NULL)
199         return;
200
201     _materialMap[mat].push_back(m);
202 }
203
204 OSG_END_NAMESPACE
Note: See TracBrowser for help on using the browser.