1/*
2Open Asset Import Library (assimp)
3----------------------------------------------------------------------
4
5Copyright (c) 2006-2017, assimp team
6
7All rights reserved.
8
9Redistribution and use of this software in source and binary forms,
10with or without modification, are permitted provided that the
11following conditions are met:
12
13* Redistributions of source code must retain the above
14 copyright notice, this list of conditions and the
15 following disclaimer.
16
17* Redistributions in binary form must reproduce the above
18 copyright notice, this list of conditions and the
19 following disclaimer in the documentation and/or other
20 materials provided with the distribution.
21
22* Neither the name of the assimp team, nor the names of its
23 contributors may be used to endorse or promote products
24 derived from this software without specific prior
25 written permission of the assimp team.
26
27THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38
39----------------------------------------------------------------------
40*/
41
42/** @file FBXProperties.cpp
43 * @brief Implementation of the FBX dynamic properties system
44 */
45
46#ifndef ASSIMP_BUILD_NO_FBX_IMPORTER
47
48#include "FBXTokenizer.h"
49#include "FBXParser.h"
50#include "FBXDocument.h"
51#include "FBXDocumentUtil.h"
52#include "FBXProperties.h"
53
54namespace Assimp {
55namespace FBX {
56
57 using namespace Util;
58
59// ------------------------------------------------------------------------------------------------
60Property::Property()
61{
62}
63
64// ------------------------------------------------------------------------------------------------
65Property::~Property()
66{
67}
68
69namespace {
70
71// ------------------------------------------------------------------------------------------------
72// read a typed property out of a FBX element. The return value is NULL if the property cannot be read.
73Property* ReadTypedProperty(const Element& element)
74{
75 ai_assert(element.KeyToken().StringContents() == "P");
76
77 const TokenList& tok = element.Tokens();
78 ai_assert(tok.size() >= 5);
79
80 const std::string& s = ParseTokenAsString(*tok[1]);
81 const char* const cs = s.c_str();
82 if (!strcmp(cs,"KString")) {
83 return new TypedProperty<std::string>(ParseTokenAsString(*tok[4]));
84 }
85 else if (!strcmp(cs,"bool") || !strcmp(cs,"Bool")) {
86 return new TypedProperty<bool>(ParseTokenAsInt(*tok[4]) != 0);
87 }
88 else if (!strcmp(cs, "int") || !strcmp(cs, "Int") || !strcmp(cs, "enum") || !strcmp(cs, "Enum")) {
89 return new TypedProperty<int>(ParseTokenAsInt(*tok[4]));
90 }
91 else if (!strcmp(cs, "ULongLong")) {
92 return new TypedProperty<uint64_t>(ParseTokenAsID(*tok[4]));
93 }
94 else if (!strcmp(cs, "KTime")) {
95 return new TypedProperty<int64_t>(ParseTokenAsInt64(*tok[4]));
96 }
97 else if (!strcmp(cs,"Vector3D") ||
98 !strcmp(cs,"ColorRGB") ||
99 !strcmp(cs,"Vector") ||
100 !strcmp(cs,"Color") ||
101 !strcmp(cs,"Lcl Translation") ||
102 !strcmp(cs,"Lcl Rotation") ||
103 !strcmp(cs,"Lcl Scaling")
104 ) {
105 return new TypedProperty<aiVector3D>(aiVector3D(
106 ParseTokenAsFloat(*tok[4]),
107 ParseTokenAsFloat(*tok[5]),
108 ParseTokenAsFloat(*tok[6]))
109 );
110 }
111 else if (!strcmp(cs,"double") || !strcmp(cs,"Number") || !strcmp(cs,"Float") || !strcmp(cs,"FieldOfView")) {
112 return new TypedProperty<float>(ParseTokenAsFloat(*tok[4]));
113 }
114 return NULL;
115}
116
117
118// ------------------------------------------------------------------------------------------------
119// peek into an element and check if it contains a FBX property, if so return its name.
120std::string PeekPropertyName(const Element& element)
121{
122 ai_assert(element.KeyToken().StringContents() == "P");
123 const TokenList& tok = element.Tokens();
124 if(tok.size() < 4) {
125 return "";
126 }
127
128 return ParseTokenAsString(*tok[0]);
129}
130
131} //! anon
132
133
134// ------------------------------------------------------------------------------------------------
135PropertyTable::PropertyTable()
136: templateProps()
137, element()
138{
139}
140
141
142// ------------------------------------------------------------------------------------------------
143PropertyTable::PropertyTable(const Element& element, std::shared_ptr<const PropertyTable> templateProps)
144: templateProps(templateProps)
145, element(&element)
146{
147 const Scope& scope = GetRequiredScope(element);
148 for(const ElementMap::value_type& v : scope.Elements()) {
149 if(v.first != "P") {
150 DOMWarning("expected only P elements in property table",v.second);
151 continue;
152 }
153
154 const std::string& name = PeekPropertyName(*v.second);
155 if(!name.length()) {
156 DOMWarning("could not read property name",v.second);
157 continue;
158 }
159
160 LazyPropertyMap::const_iterator it = lazyProps.find(name);
161 if (it != lazyProps.end()) {
162 DOMWarning("duplicate property name, will hide previous value: " + name,v.second);
163 continue;
164 }
165
166 lazyProps[name] = v.second;
167 }
168}
169
170
171// ------------------------------------------------------------------------------------------------
172PropertyTable::~PropertyTable()
173{
174 for(PropertyMap::value_type& v : props) {
175 delete v.second;
176 }
177}
178
179
180// ------------------------------------------------------------------------------------------------
181const Property* PropertyTable::Get(const std::string& name) const
182{
183 PropertyMap::const_iterator it = props.find(name);
184 if (it == props.end()) {
185 // hasn't been parsed yet?
186 LazyPropertyMap::const_iterator lit = lazyProps.find(name);
187 if(lit != lazyProps.end()) {
188 props[name] = ReadTypedProperty(*(*lit).second);
189 it = props.find(name);
190
191 ai_assert(it != props.end());
192 }
193
194 if (it == props.end()) {
195 // check property template
196 if(templateProps) {
197 return templateProps->Get(name);
198 }
199
200 return NULL;
201 }
202 }
203
204 return (*it).second;
205}
206
207DirectPropertyMap PropertyTable::GetUnparsedProperties() const
208{
209 DirectPropertyMap result;
210
211 // Loop through all the lazy properties (which is all the properties)
212 for(const LazyPropertyMap::value_type& element : lazyProps) {
213
214 // Skip parsed properties
215 if (props.end() != props.find(element.first)) continue;
216
217 // Read the element's value.
218 // Wrap the naked pointer (since the call site is required to acquire ownership)
219 // std::unique_ptr from C++11 would be preferred both as a wrapper and a return value.
220 std::shared_ptr<Property> prop = std::shared_ptr<Property>(ReadTypedProperty(*element.second));
221
222 // Element could not be read. Skip it.
223 if (!prop) continue;
224
225 // Add to result
226 result[element.first] = prop;
227 }
228
229 return result;
230}
231
232
233
234} //! FBX
235} //! Assimp
236
237#endif
238