1 /*
2 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
4 
5 Copyright (c) 2006-2019, assimp team
6 
7 
8 All rights reserved.
9 
10 Redistribution and use of this software in source and binary forms,
11 with or without modification, are permitted provided that the
12 following conditions are met:
13 
14 * Redistributions of source code must retain the above
15   copyright notice, this list of conditions and the
16   following disclaimer.
17 
18 * Redistributions in binary form must reproduce the above
19   copyright notice, this list of conditions and the
20   following disclaimer in the documentation and/or other
21   materials provided with the distribution.
22 
23 * Neither the name of the assimp team, nor the names of its
24   contributors may be used to endorse or promote products
25   derived from this software without specific prior
26   written permission of the assimp team.
27 
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 
40 ----------------------------------------------------------------------
41 */
42 
43 /** @file  FBXProperties.cpp
44  *  @brief Implementation of the FBX dynamic properties system
45  */
46 
47 #ifndef ASSIMP_BUILD_NO_FBX_IMPORTER
48 
49 #include "FBXTokenizer.h"
50 #include "FBXParser.h"
51 #include "FBXDocument.h"
52 #include "FBXDocumentUtil.h"
53 #include "FBXProperties.h"
54 
55 namespace Assimp {
56 namespace FBX {
57 
58     using namespace Util;
59 
60 // ------------------------------------------------------------------------------------------------
Property()61 Property::Property()
62 {
63 }
64 
65 // ------------------------------------------------------------------------------------------------
~Property()66 Property::~Property()
67 {
68 }
69 
70 namespace {
71 
72 // ------------------------------------------------------------------------------------------------
73 // read a typed property out of a FBX element. The return value is NULL if the property cannot be read.
ReadTypedProperty(const Element & element)74 Property* ReadTypedProperty(const Element& element)
75 {
76     ai_assert(element.KeyToken().StringContents() == "P");
77 
78     const TokenList& tok = element.Tokens();
79     ai_assert(tok.size() >= 5);
80 
81     const std::string& s = ParseTokenAsString(*tok[1]);
82     const char* const cs = s.c_str();
83     if (!strcmp(cs,"KString")) {
84         return new TypedProperty<std::string>(ParseTokenAsString(*tok[4]));
85     }
86     else if (!strcmp(cs,"bool") || !strcmp(cs,"Bool")) {
87         return new TypedProperty<bool>(ParseTokenAsInt(*tok[4]) != 0);
88     }
89     else if (!strcmp(cs, "int") || !strcmp(cs, "Int") || !strcmp(cs, "enum") || !strcmp(cs, "Enum")) {
90         return new TypedProperty<int>(ParseTokenAsInt(*tok[4]));
91     }
92     else if (!strcmp(cs, "ULongLong")) {
93         return new TypedProperty<uint64_t>(ParseTokenAsID(*tok[4]));
94     }
95     else if (!strcmp(cs, "KTime")) {
96         return new TypedProperty<int64_t>(ParseTokenAsInt64(*tok[4]));
97     }
98     else if (!strcmp(cs,"Vector3D") ||
99         !strcmp(cs,"ColorRGB") ||
100         !strcmp(cs,"Vector") ||
101         !strcmp(cs,"Color") ||
102         !strcmp(cs,"Lcl Translation") ||
103         !strcmp(cs,"Lcl Rotation") ||
104         !strcmp(cs,"Lcl Scaling")
105         ) {
106         return new TypedProperty<aiVector3D>(aiVector3D(
107             ParseTokenAsFloat(*tok[4]),
108             ParseTokenAsFloat(*tok[5]),
109             ParseTokenAsFloat(*tok[6]))
110         );
111     }
112     else if (!strcmp(cs,"double") || !strcmp(cs,"Number") || !strcmp(cs,"Float") || !strcmp(cs,"FieldOfView") || !strcmp( cs, "UnitScaleFactor" ) ) {
113         return new TypedProperty<float>(ParseTokenAsFloat(*tok[4]));
114     }
115     return NULL;
116 }
117 
118 
119 // ------------------------------------------------------------------------------------------------
120 // peek into an element and check if it contains a FBX property, if so return its name.
PeekPropertyName(const Element & element)121 std::string PeekPropertyName(const Element& element)
122 {
123     ai_assert(element.KeyToken().StringContents() == "P");
124     const TokenList& tok = element.Tokens();
125     if(tok.size() < 4) {
126         return "";
127     }
128 
129     return ParseTokenAsString(*tok[0]);
130 }
131 
132 } //! anon
133 
134 
135 // ------------------------------------------------------------------------------------------------
PropertyTable()136 PropertyTable::PropertyTable()
137 : templateProps()
138 , element()
139 {
140 }
141 
142 // ------------------------------------------------------------------------------------------------
PropertyTable(const Element & element,std::shared_ptr<const PropertyTable> templateProps)143 PropertyTable::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 // ------------------------------------------------------------------------------------------------
~PropertyTable()172 PropertyTable::~PropertyTable()
173 {
174     for(PropertyMap::value_type& v : props) {
175         delete v.second;
176     }
177 }
178 
179 
180 // ------------------------------------------------------------------------------------------------
Get(const std::string & name) const181 const 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 
GetUnparsedProperties() const207 DirectPropertyMap 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 } //! FBX
233 } //! Assimp
234 
235 #endif
236