1 /*
2     Copyright (c) 2008-2009 NetAllied Systems GmbH
3 
4     This file is part of COLLADASaxFrameworkLoader.
5 
6     Licensed under the MIT Open Source License,
7     for details please see LICENSE file or the website
8     http://www.opensource.org/licenses/mit-license.php
9 */
10 
11 #include "COLLADASaxFWLStableHeaders.h"
12 #include "COLLADASaxFWLLibraryKinematicsModelsLoader.h"
13 #include "COLLADASaxFWLFileLoader.h"
14 
15 #include "COLLADAFWTranslate.h"
16 #include "COLLADAFWRotate.h"
17 #include "COLLADAFWJoint.h"
18 
19 
20 namespace COLLADASaxFWL
21 {
22 
23     //------------------------------
LibraryKinematicsModelsLoader(IFilePartLoader * callingFilePartLoader)24 	LibraryKinematicsModelsLoader::LibraryKinematicsModelsLoader( IFilePartLoader* callingFilePartLoader )
25 		: FilePartLoader(callingFilePartLoader)
26 		, mCurrentKinematicsModel(0)
27 		, mCurrentJointInstance(0)
28 		, mCurrentAttachment(0)
29 	{
30 		FormulasLoader::setHandlingFilePartLoader(this);
31 		mJointsLoader.setHandlingFilePartLoader(this);
32 	}
33 
34     //------------------------------
~LibraryKinematicsModelsLoader()35 	LibraryKinematicsModelsLoader::~LibraryKinematicsModelsLoader()
36 	{
37 	}
38 
39     //------------------------------
getUniqueId()40     const COLLADAFW::UniqueId& LibraryKinematicsModelsLoader::getUniqueId ()
41     {
42         // TODO No uniqueId!
43         return COLLADAFW::UniqueId::INVALID;
44     }
45 
46 	//------------------------------
47 	template<class Transformationtype>
beginTransformation(const char * sid)48 	bool LibraryKinematicsModelsLoader::beginTransformation( const char* sid )
49 	{
50 		mTransformationLoader.beginTransformation<Transformationtype>();
51 		addToSidTree( 0, sid, mTransformationLoader.getCurrentTransformation());
52 		return true;
53 	}
54 
55 	//------------------------------
endTransformation()56 	bool LibraryKinematicsModelsLoader::endTransformation()
57 	{
58 		if ( mCurrentAttachment )
59 		{
60 			// we are a child of an attachment
61 			mCurrentAttachment->addTransformation( mTransformationLoader.getCurrentTransformation() );
62 		}
63 		else
64 		{
65 			// we are a child of a link
66 			KinematicLink* link = mLinkStack.top();
67 			link->addTransformation( mTransformationLoader.getCurrentTransformation() );
68 		}
69 		moveUpInSidTree();
70 		mTransformationLoader.endTransformation();
71 		return true;
72 	}
73 
74 	//------------------------------
beginAttachment(KinematicAttachment::Type attachmentType,const attachment_AttributeData & attributeData)75 	bool LibraryKinematicsModelsLoader::beginAttachment( KinematicAttachment::Type attachmentType, const attachment_AttributeData& attributeData )
76 	{
77 		String joint(attributeData.joint);
78 		mCurrentAttachment = new KinematicAttachment( attachmentType, joint);
79 		COLLADABU_ASSERT( !mLinkStack.empty() );
80 		KinematicLink* link = mLinkStack.top();
81 		link->addAttachment( mCurrentAttachment );
82 		return true;
83 	}
84 
85 	//------------------------------
endAttachment()86 	bool LibraryKinematicsModelsLoader::endAttachment()
87 	{
88 		mCurrentAttachment = 0;
89 		return true;
90 	}
91 
92 
93 	//------------------------------
end__library_kinematics_models()94 	bool LibraryKinematicsModelsLoader::end__library_kinematics_models()
95 	{
96 		moveUpInSidTree();
97 		finish();
98 		return true;
99 	}
100 
101 	//------------------------------
begin__kinematics_model(const kinematics_model__AttributeData & attributeData)102 	bool LibraryKinematicsModelsLoader::begin__kinematics_model( const kinematics_model__AttributeData& attributeData )
103 	{
104 		COLLADABU::URI uri(getFileUri());
105 		if ( attributeData.id )
106 		{
107 			uri.setFragment( attributeData.id );
108 		}
109 
110 		const char* name = 0;
111 		if ( attributeData.name )
112 		{
113 			name = attributeData.name;
114 		}
115 		else if ( attributeData.id )
116 		{
117 			name = attributeData.id;
118 		}
119 
120 		mCurrentKinematicsModel = new KinematicsModel(uri, name);
121 		SidTreeNode* sidTreeNode = addToSidTree( attributeData.id, 0, mCurrentKinematicsModel);
122 		mCurrentKinematicsModel->setSidTreeNode(sidTreeNode);
123 		return true;
124 	}
125 
126 	//------------------------------
end__kinematics_model()127 	bool LibraryKinematicsModelsLoader::end__kinematics_model()
128 	{
129 		getFileLoader()->addKinematicsModel( mCurrentKinematicsModel );
130 		mCurrentKinematicsModel = 0;
131 		moveUpInSidTree();
132 		return true;
133 	}
134 
135 	//------------------------------
begin__instance_joint(const instance_joint__AttributeData & attributeData)136 	bool LibraryKinematicsModelsLoader::begin__instance_joint( const instance_joint__AttributeData& attributeData )
137 	{
138 		// Get the unique id of the joint, that will replace this instance in the kinematics model
139 		COLLADAFW::UniqueId jointId = createUniqueId( COLLADAFW::Joint::ID() );
140 		mCurrentJointInstance = new KinematicInstance( attributeData.url, jointId );
141 		getFileLoader()->addInstanceJoint( mCurrentJointInstance );
142 		addToSidTree( 0, attributeData.sid, mCurrentJointInstance );
143 		return true;
144 	}
145 
146 	//------------------------------
end__instance_joint()147 	bool LibraryKinematicsModelsLoader::end__instance_joint()
148 	{
149 		mCurrentJointInstance = 0;
150 		moveUpInSidTree();
151 		return true;
152 	}
153 
154 	//------------------------------
begin__link(const link__AttributeData & attributeData)155 	bool LibraryKinematicsModelsLoader::begin__link( const link__AttributeData& attributeData )
156 	{
157 		KinematicLink* link = 0;
158 		if ( mLinkStack.empty() )
159 		{
160 			// this is a base link. Create a new link and add it to the kinematics model
161 			link = new KinematicLink();
162 			mCurrentKinematicsModel->addBaseLink(link);
163 		}
164 		else
165 		{
166 			// this is a child of the current attachment.
167 			COLLADABU_ASSERT(mCurrentAttachment);
168 			link = &mCurrentAttachment->getLink();
169 
170 		}
171 		addToSidTree(0, attributeData.sid, link);
172 		mLinkStack.push( link );
173 		return true;
174 	}
175 
176 	//------------------------------
end__link()177 	bool LibraryKinematicsModelsLoader::end__link()
178 	{
179 		moveUpInSidTree();
180 		mLinkStack.pop();
181 		return true;
182 	}
183 
184 	//------------------------------
begin__attachment_full(const attachment_AttributeData & attributeData)185 	bool LibraryKinematicsModelsLoader::begin__attachment_full( const attachment_AttributeData& attributeData )
186 	{
187 		return beginAttachment( KinematicAttachment::FULL, attributeData);
188 	}
189 
190 	//------------------------------
end__attachment_full()191 	bool LibraryKinematicsModelsLoader::end__attachment_full()
192 	{
193 		return endAttachment();
194 	}
195 
196 	//------------------------------
begin__attachment_start(const attachment_AttributeData & attributeData)197 	bool LibraryKinematicsModelsLoader::begin__attachment_start( const attachment_AttributeData& attributeData )
198 	{
199 		return beginAttachment( KinematicAttachment::START, attributeData);
200 	}
201 
202 	//------------------------------
end__attachment_start()203 	bool LibraryKinematicsModelsLoader::end__attachment_start()
204 	{
205 		return endAttachment();
206 	}
207 
208 	//------------------------------
begin__attachment_end(const attachment_AttributeData & attributeData)209 	bool LibraryKinematicsModelsLoader::begin__attachment_end( const attachment_AttributeData& attributeData )
210 	{
211 		return beginAttachment( KinematicAttachment::END, attributeData);
212 	}
213 
214 	//------------------------------
end__attachment_end()215 	bool LibraryKinematicsModelsLoader::end__attachment_end()
216 	{
217 		return endAttachment();
218 	}
219 
220 	//------------------------------
begin__translate(const translate__AttributeData & attributeData)221 	bool LibraryKinematicsModelsLoader::begin__translate( const translate__AttributeData& attributeData )
222 	{
223 		return beginTransformation<COLLADAFW::Translate>( attributeData.sid );
224 	}
225 
226 	//------------------------------
end__translate()227 	bool LibraryKinematicsModelsLoader::end__translate()
228 	{
229 		return endTransformation();
230 	}
231 
232 	//------------------------------
data__translate(const float * data,size_t length)233 	bool LibraryKinematicsModelsLoader::data__translate( const float* data, size_t length )
234 	{
235 		return mTransformationLoader.dataTranslate( data, length);
236 	}
237 
238 	//------------------------------
begin__rotate(const rotate__AttributeData & attributeData)239 	bool LibraryKinematicsModelsLoader::begin__rotate( const rotate__AttributeData& attributeData )
240 	{
241 		return beginTransformation<COLLADAFW::Rotate>( attributeData.sid );
242 	}
243 
244 	//------------------------------
end__rotate()245 	bool LibraryKinematicsModelsLoader::end__rotate()
246 	{
247 		return endTransformation();
248 	}
249 
250 	//------------------------------
data__rotate(const float * data,size_t length)251 	bool LibraryKinematicsModelsLoader::data__rotate( const float* data, size_t length )
252 	{
253 		return mTransformationLoader.dataRotate( data, length);
254 	}
255 
256 } // namespace COLLADASaxFWL
257