fbxsdk/fileio/collada/fbxcolladaelement.h Source File

fbxcolladaelement.h
Go to the documentation of this file.
1 /****************************************************************************************
2 
3  Copyright (C) 2015 Autodesk, Inc.
4  All rights reserved.
5 
6  Use of this software is subject to the terms of the Autodesk license agreement
7  provided at the time of installation or download, or which otherwise accompanies
8  this software in either electronic or hard copy form.
9 
10 ****************************************************************************************/
11 
13 #ifndef _FBXSDK_FILEIO_COLLADA_ELEMENT_H_
14 #define _FBXSDK_FILEIO_COLLADA_ELEMENT_H_
15 
16 #include <fbxsdk.h>
17 
18 #include <fbxsdk/fbxsdk_nsbegin.h>
19 
20 // Utility functions to convert type to array tag used in COLLADA source element
21 template <typename T>
22 inline const FbxString TypeToArrayTag()
23 {
25 }
26 
27 template <>
29 {
31 }
32 
33 template <>
35 {
37 }
38 
39 template <>
41 {
43 }
44 
45 // Utility functions to convert type to parameter tag used in COLLADA source element
46 template <typename T>
48 {
49  return COLLADA_FLOAT_TYPE;
50 }
51 
52 template <>
54 {
55  return COLLADA_BOOL_TYPE;
56 }
57 
58 template <>
60 {
61  return COLLADA_INT_TYPE;
62 }
63 
64 template <>
66 {
67  return COLLADA_NAME_TYPE;
68 }
69 
70 //----------------------------------------------------------------------------//
71 
75 {
77  ElementContentAccessor(xmlNode * pElement);
78  virtual ~ElementContentAccessor();
79 
80  template <typename TYPE>
81  bool GetNext(TYPE * pData)
82  {
83  return FromString(pData, mPointer, &mPointer);
84  }
85 
86  template <typename TYPE>
87  int GetArray(TYPE * pArray,
88  int pSourceUnitOffset = 0, int pSourceUnitValidCount = 1, int pSourceUnitSize = 1,
89  int pDestUnitOffset = 0, int pDestUnitValidCount = 1, int pDestUnitSize = 1,
90  TYPE pDefaultValue = TYPE())
91  {
92  if (pArray)
93  {
94  return FromStringToArray(mPointer, pArray,
95  pSourceUnitOffset, pSourceUnitValidCount, pSourceUnitSize,
96  pDestUnitOffset, pDestUnitValidCount, pDestUnitSize, pDefaultValue);
97  }
98  return 0;
99  }
100 
101  xmlChar * mContent;
102  const char * mPointer;
103 };
104 
105 //----------------------------------------------------------------------------//
106 
109 template <typename TYPE>
111 {
112  SourceElementContentAccessor(xmlNode * pSourceElement)
113  : mCount(0), mStride(1), mOffset(0)
114  {
115  bool lReadCount = true;
116  xmlNode* lTechniqueElement = DAE_FindChildElementByTag(pSourceElement, COLLADA_TECHNIQUE_COMMON_ELEMENT);
117  if (lTechniqueElement)
118  {
119  xmlNode* lAccessorElement = DAE_FindChildElementByTag(lTechniqueElement, COLLADA_ACCESSOR_STRUCTURE);
120  FBX_ASSERT(lAccessorElement);
121  if (!lAccessorElement)
122  return;
123 
127  lReadCount = false;
128  }
129 
130  xmlNode * lDataArrayElement = DAE_FindChildElementByTag(pSourceElement,
131  TypeToArrayTag<TYPE>());
132  // Some COLLADA exporters use IDREF_array instead of Name_array
133  if (!lDataArrayElement && TypeToArrayTag<TYPE>() == COLLADA_NAME_ARRAY_STRUCTURE)
134  lDataArrayElement = DAE_FindChildElementByTag(pSourceElement, COLLADA_IDREF_ARRAY_STRUCTURE);
135  FBX_ASSERT(lDataArrayElement);
136 
137  if (lDataArrayElement && lReadCount)
139 
140  mContent = xmlNodeGetContent(lDataArrayElement);
141  mPointer = (const char *)mContent;
142  }
143 
144  int mCount;
145  int mStride;
146  int mOffset;
147 };
148 
149 //----------------------------------------------------------------------------//
150 
154 {
155 public:
156  enum
157  {
159  };
160 
161  // The name of user property in FBX which is used to preserve the ID of COLLADA element
162  static const char* smID_PROPERTY_NAME;
163 
166  ElementBase();
167  virtual ~ElementBase();
168 
171  void SetXMLElement(xmlNode * pElement) { mXMLElement = pElement; }
172  xmlNode * GetXMLElement() const { return mXMLElement; }
173 
177  const FbxString & GetID() const;
178 
183  const FbxSystemUnit * GetUnit() const;
184 
185 private:
186  xmlNode * mXMLElement;
187  mutable FbxString * mID;
188  mutable FbxSystemUnit * mUnit;
189 };
190 
195 inline const FbxString URL(const FbxString & pID)
196 {
197  return FbxString("#") + pID;
198 }
199 
210 template <typename T>
211 xmlNode * AddSourceElement(xmlNode * pParentElement, const char * pID,
212  const T * pData, int pCount, int pStride = 1)
213 {
214  FBX_ASSERT(pParentElement && pData);
215  if (!pParentElement || !pData)
216  return NULL;
217 
218  xmlNode * lSourceElement = DAE_AddChildElement(pParentElement, COLLADA_SOURCE_STRUCTURE);
219  DAE_AddAttribute(lSourceElement, COLLADA_ID_PROPERTY, pID);
220 
221  FbxString lContent;
222  const int lDataCount = pCount * pStride;
223  for (int lIndex = 0; lIndex < lDataCount; ++lIndex)
224  {
225  lContent += ToString(pData[lIndex]);
226  if (lIndex != lDataCount - 1)
227  lContent += " ";
228  }
229  const FbxString lArrayID = FbxString(pID) + "-array";
230  xmlNode * lArrayElement = DAE_AddChildElement(lSourceElement, TypeToArrayTag<T>(), lContent);
231  DAE_AddAttribute(lArrayElement, COLLADA_ID_PROPERTY, lArrayID);
232  DAE_AddAttribute(lArrayElement, COLLADA_COUNT_PROPERTY, lDataCount);
233 
234  xmlNode * lTechniqueCommonElement = DAE_AddChildElement(lSourceElement,
236  xmlNode * lAccessElement = DAE_AddChildElement(lTechniqueCommonElement,
238  DAE_AddAttribute(lAccessElement, COLLADA_SOURCE_PROPERTY, URL(lArrayID));
239  DAE_AddAttribute(lAccessElement, COLLADA_COUNT_PROPERTY, pCount);
240  DAE_AddAttribute(lAccessElement, COLLADA_STRIDE_PROPERTY, pStride);
241 
242  for (int lStrideIndex = 0; lStrideIndex < pStride; ++lStrideIndex)
243  {
244  xmlNode * lParamElement = DAE_AddChildElement(lAccessElement, COLLADA_PARAMETER_STRUCTURE);
245  DAE_AddAttribute(lParamElement, COLLADA_TYPE_PROPERTY, TypeToParameterTag<T>());
246  }
247 
248  return lSourceElement;
249 }
250 
257 template <typename TYPE> FbxLayerElementArray * PopulateLayerElementDirectArray(FbxLayerElement * pLayerElement, xmlNode * pSourceElement, int pSize)
258 {
259  SourceElementContentAccessor<TYPE> lSourceElementAccessor(pSourceElement);
260 
261  FbxLayerElementTemplate<TYPE> * lLayerElement = (FbxLayerElementTemplate<TYPE> *)pLayerElement;
264  lLayerElement->GetDirectArray().SetCount(lSourceElementAccessor.mCount);
265 
266  TYPE * lArray = NULL;
267  lArray = lLayerElement->GetDirectArray().GetLocked(lArray);
268  lSourceElementAccessor.GetArray((double *)lArray, 0, pSize,
269  lSourceElementAccessor.mStride, 0, pSize, sizeof(TYPE)/sizeof(double), 1.0);
270  lLayerElement->GetDirectArray().Release(&lArray, lArray);
271 
272  return &(lLayerElement->GetIndexArray());
273 }
274 
275 #include <fbxsdk/fbxsdk_nsend.h>
276 
277 #endif /* _FBXSDK_FILEIO_COLLADA_ELEMENT_H_ */
FbxLayerElementArrayTemplate< int > & GetIndexArray() const
Returns the index array of Layer Elements.
Definition: fbxlayer.h:1043
#define COLLADA_FLOAT_ARRAY_STRUCTURE
void SetXMLElement(xmlNode *pElement)
Access for XML element.
#define COLLADA_ACCESSOR_STRUCTURE
const FbxSystemUnit * GetUnit() const
Get the unit of the element, which takes effect in this element and its children elements.
void SetReferenceMode(EReferenceMode pReferenceMode)
Sets the Reference Mode.
Definition: fbxlayer.h:179
const FbxString URL(const FbxString &pID)
Convert from ID to URL, just add a prefix "#".
const FbxString & GetID() const
Get the ID of the element.
const FbxString ToString(const T &pValue)
A struct for convenient access to the content of COLLADA source element.
#define COLLADA_COUNT_PROPERTY
bool GetNext(TYPE *pData)
#define COLLADA_BOOL_TYPE
#define COLLADA_PARAMETER_STRUCTURE
bool FromString(T *pDest, const char *pSourceBegin, const char **pSourceEnd=((void *) 0))
Convert part of the source string into destination type.
#define NULL
Definition: fbxarch.h:210
#define COLLADA_STRIDE_PROPERTY
#define COLLADA_OFFSET_PROPERTY
FbxLayerElementArrayTemplate< Type > & GetDirectArray() const
Returns the direct array of Layer Elements.
Definition: fbxlayer.h:1023
Utility class to manipulate strings.
Definition: fbxstring.h:66
#define COLLADA_SOURCE_STRUCTURE
xmlNode * AddSourceElement(xmlNode *pParentElement, const char *pID, const T *pData, int pCount, int pStride=1)
Convert the array data to a source element under specific parent element.
void SetMappingMode(EMappingMode pMappingMode)
Sets the Mapping Mode.
Definition: fbxlayer.h:174
const FbxString TypeToParameterTag< bool >()
#define COLLADA_ID_PROPERTY
int GetArray(TYPE *pArray, int pSourceUnitOffset=0, int pSourceUnitValidCount=1, int pSourceUnitSize=1, int pDestUnitOffset=0, int pDestUnitValidCount=1, int pDestUnitSize=1, TYPE pDefaultValue=TYPE())
const FbxString TypeToArrayTag< bool >()
#define COLLADA_IDREF_ARRAY_STRUCTURE
#define COLLADA_INT_TYPE
const FbxString TypeToParameterTag()
xmlNode * DAE_AddChildElement(xmlNode *pParentElement, const char *pTag, const T &pContent)
Add a child element with specific content.
const FbxString TypeToArrayTag()
FbxLayerElementArray is the base class for FbxLayerElementArrayTemplate, it provides lock handling an...
Definition: fbxlayer.h:325
This class complements the FbxLayerElement class.
Definition: fbxlayer.h:1015
#define COLLADA_TYPE_PROPERTY
#define COLLADA_SOURCE_PROPERTY
ElementBase()
Constructor & Destructor.
const FbxString TypeToArrayTag< FbxString >()
#define COLLADA_BOOL_ARRAY_STRUCTURE
virtual ~ElementContentAccessor()
#define COLLADA_FLOAT_TYPE
static const char * smID_PROPERTY_NAME
const FbxString TypeToArrayTag< int >()
This class describes the units of measurement used within a particular scene.
Definition: fbxsystemunit.h:31
#define COLLADA_NAME_ARRAY_STRUCTURE
const FbxString DAE_GetElementAttributeValue(xmlNode *pElement, const char *pAttributeName)
Get the value of an attribute of an element.
#define COLLADA_TECHNIQUE_COMMON_ELEMENT
const FbxString TypeToParameterTag< FbxString >()
xmlNode * GetXMLElement() const
int FromStringToArray(const char *pString, TYPE *pArray, int pSourceUnitOffset, int pSourceValidUnitCount, int pSourceGroupSize, int pDestUnitOffset, int pDestValidUnitCount, int pDestGroupSize, TYPE pDefaultValue=TYPE())
Parse the string into an array.
virtual ~ElementBase()
A struct for convenient access to the content of common COLLADA element.
Representing a common COLLADA element.
#define COLLADA_INT_ARRAY_STRUCTURE
#define COLLADA_NAME_TYPE
FbxLayerElementArray * PopulateLayerElementDirectArray(FbxLayerElement *pLayerElement, xmlNode *pSourceElement, int pSize)
Populate the layer element with direct array and return index array for later use.
SourceElementContentAccessor(xmlNode *pSourceElement)
Base class for elements of layers (FbxLayer).
Definition: fbxlayer.h:38
const FbxString TypeToParameterTag< int >()
xmlAttr * DAE_AddAttribute(xmlNode *pElement, const FbxString &pAttributeName, const T &pAttributeValue)
Add an attribute for a element.
xmlNode * DAE_FindChildElementByTag(xmlNode *pParentElement, const char *pTag, xmlNode *pFindFrom=((void *) 0))
Find a child element with a given tag.