fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / FileIO / Collada / OSGColladaInstanceController.cpp
blobe4de3b6792233fd9feac24ec615c685a98b69e90
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2009 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 #if __GNUC__ >= 4 || __GNUC_MINOR__ >=3
40 #pragma GCC diagnostic ignored "-Wold-style-cast"
41 #endif
43 #include "OSGColladaInstanceController.h"
45 #ifdef OSG_WITH_COLLADA
47 #include "OSGColladaLog.h"
49 #include <dom/domInstance_controller.h>
50 #include <dom/domNode.h>
51 #include <dom/domInstance_node.h>
52 #include <dae/daeSIDResolver.h>
54 OSG_BEGIN_NAMESPACE
56 ColladaElementRegistrationHelper ColladaInstanceController::_regHelper(
57 &ColladaInstanceController::create, "instance_controller");
60 ColladaElementTransitPtr
61 ColladaInstanceController::create(daeElement *elem, ColladaGlobal *global)
63 return ColladaElementTransitPtr(
64 new ColladaInstanceController(elem, global));
67 void
68 ColladaInstanceController::read(ColladaElement *colElemParent)
70 OSG_COLLADA_LOG(("ColladaInstanceController::read\n"));
72 ColladaControllerRefPtr colCtrl = getTargetElem();
74 if(colCtrl == NULL)
76 colCtrl = dynamic_pointer_cast<ColladaController>(
77 ColladaElementFactory::the()->create(
78 getTargetDOMElem(), getGlobal()));
80 colCtrl->read(this);
83 domInstance_controllerRef instCtrl =
84 getDOMElementAs<domInstance_controller>();
85 domBind_materialRef bindMat = instCtrl->getBind_material();
87 if(bindMat != NULL)
89 Inherited::readBindMaterial(bindMat);
91 else
94 SWARNING << "ColladaInstanceController::read: "
95 << "No <bind_material> found." << std::endl;
98 readSkeleton();
101 const ColladaInstanceController::SkeletonRootStore &
102 ColladaInstanceController::getSkeletonRoots(void)
104 return _skelRoots;
107 domNode *
108 ColladaInstanceController::findJointNode(const std::string &jointSid)
110 domNode *retVal = NULL;
111 SkeletonRootStoreConstIt sIt = _skelRoots.begin();
112 SkeletonRootStoreConstIt sEnd = _skelRoots.end ();
114 for(UInt32 i = 0; sIt != sEnd; ++sIt, ++i)
116 daeSidRef jointRef(jointSid, *sIt);
118 retVal = daeSafeCast<domNode>(jointRef.resolve().elt);
120 if(retVal != NULL)
122 OSG_COLLADA_LOG(("ColladaInstanceController::findJointNode: "
123 "Found joint [%s] relative to root [%d][%s]\n",
124 jointSid.c_str(), i,
125 ((*sIt)->getId() != NULL ? (*sIt)->getId() : "")));
127 break;
129 else
131 OSG_COLLADA_LOG(("ColladaInstanceController::findJointNode: "
132 "joint [%s] not found below root [%d][%s]\n",
133 jointSid.c_str(), i,
134 ((*sIt)->getId() != NULL ? (*sIt)->getId() : "")));
138 return retVal;
141 ColladaController *
142 ColladaInstanceController::getTargetElem(void) const
144 ColladaController *retVal = NULL;
145 daeElementRef targetElem = getTargetDOMElem();
147 if(targetElem != NULL)
149 retVal = getUserDataAs<ColladaController>(targetElem);
152 return retVal;
155 daeElement *
156 ColladaInstanceController::getTargetDOMElem(void) const
158 domControllerRef retVal = NULL;
159 domInstance_controllerRef instCtrl =
160 getDOMElementAs<domInstance_controller>();
162 if(instCtrl->getUrl().getElement() != NULL)
164 retVal = daeSafeCast<domController>(instCtrl->getUrl().getElement());
167 return retVal;
170 ColladaInstanceController::ColladaInstanceController(
171 daeElement *elem, ColladaGlobal *global)
173 : Inherited (elem, global)
174 , _skelRoots()
178 ColladaInstanceController::~ColladaInstanceController(void)
182 void
183 ColladaInstanceController::readSkeleton(void)
185 domInstance_controllerRef instCtrl =
186 getDOMElementAs<domInstance_controller>();
187 const domInstance_controller::domSkeleton_Array &skels =
188 instCtrl->getSkeleton_array();
190 for(UInt32 i = 0; i < skels.getCount(); ++i)
192 OSG_COLLADA_LOG(("ColladaInstanceController::readSkeleton: "
193 "Skeleton root node: [%s]\n",
194 skels[i]->getValue().getURI()));
196 daeURI skelURI = skels[i]->getValue();
197 domNode *skelNode = daeSafeCast<domNode>(skelURI.getElement());
199 if(skelNode == NULL)
201 SWARNING << "ColladaInstanceController::readSkeleton: "
202 << "<skeleton> tag does not refer to a <node>, URI ["
203 << skelURI.str() << "]. Ignored." << std::endl;
204 continue;
207 _skelRoots.push_back(skelNode);
211 #if 0 // obsolete, using daeSidRef instead
212 domNode *
213 ColladaInstanceController::findJointNode(
214 const std::string &jointSid, domNode *currNode)
216 if(currNode == NULL)
217 return NULL;
219 domNode *retVal = NULL;
220 std::string currSid = currNode->getSid();
222 if(jointSid == currSid)
224 retVal = currNode;
226 else
228 const daeElementRefArray &contents = currNode->getContents();
230 for(UInt32 i = 0; i < contents.getCount(); ++i)
232 if(contents[i]->typeID() == domNode::ID())
234 retVal = findJointNode(
235 jointSid, daeSafeCast<domNode>(contents[i]));
238 else if(contents[i]->typeID() == domInstance_node::ID())
240 retVal = findJointNode(
241 jointSid, daeSafeCast<domInstance_node>(contents[i]));
244 if(retVal != NULL)
245 break;
249 return retVal;
252 domNode *
253 ColladaInstanceController::findJointNode(
254 const std::string &jointSid, domInstance_node *currNode)
256 if(currNode == NULL)
257 return NULL;
259 domNode *targetNode =
260 daeSafeCast<domNode>(currNode->getUrl().getElement());
262 return findJointNode(jointSid, targetNode);
264 #endif // obsolete, using daeSidRef instead
266 OSG_END_NAMESPACE
268 #endif // OSG_WITH_COLLADA