File: GLTFMesh.cpp

package info (click to toggle)
collada2gltf 20140924-4
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 4,672 kB
  • sloc: cpp: 33,757; ansic: 5,313; python: 29; makefile: 6
file content (213 lines) | stat: -rw-r--r-- 8,721 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright (c) 2012, Motorola Mobility, Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//  * Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
//  * Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//  * Neither the name of the Motorola Mobility, Inc. nor the names of its
//    contributors may be used to endorse or promote products derived from this
//    software without specific prior written permission.
//
//  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


#include "GLTF.h"
#include "../helpers/geometryHelpers.h"

using namespace rapidjson;
#if __cplusplus <= 199711L
using namespace std::tr1;
#endif
using namespace std;

namespace GLTF
{
    GLTFMesh::GLTFMesh() : JSONObject() , _subMeshes(nullptr) {
        this->_subMeshes = nullptr;
        this->_remapTableForPositions = nullptr;
        this->_ID = GLTFUtils::generateIDForType("mesh");
    }
    
    GLTFMesh::~GLTFMesh() {
        if (this->_remapTableForPositions)
            free(this->_remapTableForPositions);
    }

    //copy that retains elements
    GLTFMesh::GLTFMesh(const GLTFMesh &mesh) : JSONObject(), _subMeshes(nullptr) {
        this->_remapTableForPositions = nullptr;
        GLTFMesh *meshPtr = const_cast <GLTFMesh*>(&mesh);
        this->setPrimitives(meshPtr->getPrimitives());
        this->_semanticToMeshAttributes = mesh._semanticToMeshAttributes;
        this->_ID = mesh._ID;
        this->setName(meshPtr->getName());
    }

    shared_ptr<GLTFMesh> GLTFMesh::clone() {
        
        shared_ptr<GLTFMesh> clonedMesh(new GLTFMesh());
        
        clonedMesh->setID(this->getID());
        clonedMesh->setName(this->getName());
        clonedMesh->_semanticToMeshAttributes = this->_semanticToMeshAttributes;
        
        JSONValueVectorRef primitives =  this->getPrimitives()->values();
        for (size_t i = 0 ; i < primitives.size() ; i++) {
            shared_ptr <GLTFPrimitive> primitive = static_pointer_cast<GLTFPrimitive>(primitives[i]);
            clonedMesh->appendPrimitive(primitive->clone());
        }
        
        return clonedMesh;
    }

    shared_ptr <MeshAttributeVector> GLTFMesh::meshAttributes() {
        shared_ptr <MeshAttributeVector> meshAttributes(new MeshAttributeVector());
        vector <GLTF::Semantic> allSemantics = this->allSemantics();
        std::map<string, unsigned int> semanticAndSetToIndex;
        
        for (unsigned int i = 0 ; i < allSemantics.size() ; i++) {
            GLTF::Semantic semantic = allSemantics[i];
            size_t attributesCount = this->getMeshAttributesCountForSemantic(semantic);
            for (size_t j = 0 ; j < attributesCount ; j++) {
                shared_ptr <GLTF::GLTFAccessor> selectedMeshAttribute = this->getMeshAttribute(semantic, j);
                unsigned int indexSet = j;
                std::string semanticIndexSetKey = keyWithSemanticAndSet(semantic, indexSet);
                unsigned int size = (unsigned int)meshAttributes->size();
                semanticAndSetToIndex[semanticIndexSetKey] = size;
                
                meshAttributes->push_back(selectedMeshAttribute);
            }
        }
        return meshAttributes;
    }
    
    bool GLTFMesh::appendPrimitive(shared_ptr <GLTF::GLTFPrimitive> primitive) {
        shared_ptr<JSONArray> primitives = this->createArrayIfNeeded(kPrimitives);
        primitives->appendValue(primitive);
        return true;
    }
    
    bool GLTFMesh::hasSemantic(Semantic semantic) {
        return this->_semanticToMeshAttributes.count(semantic) > 0;
    }
    
    size_t GLTFMesh::getMeshAttributesCountForSemantic(Semantic semantic) {
        return this->_semanticToMeshAttributes[semantic].size();
    }
    
    shared_ptr<GLTFAccessor> GLTFMesh::getMeshAttribute(Semantic semantic, size_t indexOfSet) {
        IndexSetToMeshAttributeHashmap& hasmap = this->_semanticToMeshAttributes[semantic];
		return hasmap[(unsigned int)indexOfSet];
    }

    void GLTFMesh::setMeshAttribute(Semantic semantic, size_t indexOfSet, shared_ptr<GLTFAccessor> meshAttribute) {
        IndexSetToMeshAttributeHashmap& hasmap = this->_semanticToMeshAttributes[semantic];
		hasmap[(unsigned int)indexOfSet] = meshAttribute;
    }
    
    vector <GLTF::Semantic> GLTFMesh::allSemantics() {
        vector <GLTF::Semantic> allSemantics;
        
        SemanticToMeshAttributeHashmap::const_iterator meshAttributeIterator;
        
        //FIXME: consider turn this search into a method for mesh
        for (meshAttributeIterator = this->_semanticToMeshAttributes.begin() ; meshAttributeIterator != this->_semanticToMeshAttributes.end() ; meshAttributeIterator++) {
            //(*it).first;             // the key value (of type Key)
            //(*it).second;            // the mapped value (of type T)
            allSemantics.push_back((*meshAttributeIterator).first);
        }
        
        return allSemantics;
    }
    
    std::string GLTFMesh::getID() {
        return _ID;
    }
    
    void GLTFMesh::setID(std::string ID) {
        this->_ID = ID;
    }
    
    std::string GLTFMesh::getName() {
        return this->getString(kName);
    }

    void GLTFMesh::setName(std::string name) {
        this->setString(kName, name);
    }

    size_t GLTFMesh::getPrimitivesCount() {
        size_t count = 0;
        shared_ptr<JSONArray> primitives = this->getPrimitives();
        if (primitives) {
            JSONValueVectorRef values = primitives->values();
            count = values.size();
        }
        return count;
    }
    
    shared_ptr<JSONArray> GLTFMesh::getPrimitives() {
        return this->getArray(kPrimitives);
    }

    void GLTFMesh::setPrimitives(shared_ptr<JSONArray> primitives) {
        this->setValue(kPrimitives, primitives);
    }

    shared_ptr<JSONObject> GLTFMesh::getExtensions() {
        return this->createObjectIfNeeded(kExtensions);
    }
    
    void GLTFMesh::setRemapTableForPositions(unsigned int* remapTableForPositions) {
        this->_remapTableForPositions = remapTableForPositions;
    }
    
    unsigned int* GLTFMesh::getRemapTableForPositions() {
        return this->_remapTableForPositions;
    }
    
    void GLTFMesh::resolveAttributes() {
        shared_ptr <GLTF::JSONArray> primitivesArray(new GLTF::JSONArray());
        JSONValueVector primitives = this->getPrimitives()->values();
        unsigned int primitivesCount =  (unsigned int)primitives.size();
        for (unsigned int i = 0 ; i < primitivesCount ; i++) {
            shared_ptr<GLTF::GLTFPrimitive> primitive = static_pointer_cast<GLTFPrimitive>(primitives[i]);
                        
            shared_ptr <GLTF::JSONObject> attributes(new GLTF::JSONObject());
            primitive->setValue(kAttributes, attributes);
            
            size_t count = primitive->getVertexAttributesCount();
            for (size_t j = 0 ; j < count ; j++) {
				GLTF::Semantic semantic = primitive->getSemanticAtIndex((unsigned int)j);
                std::string semanticAndSet = GLTFUtils::getStringForSemantic(semantic);
                unsigned int indexOfSet = 0;
                if ((semantic == GLTF::TEXCOORD) || (semantic == GLTF::COLOR)) {
					indexOfSet = primitive->getIndexOfSetAtIndex((unsigned int)j);
                    semanticAndSet += "_" + GLTFUtils::toString(indexOfSet);
                }
                attributes->setString(semanticAndSet, this->getMeshAttribute(semantic, indexOfSet)->getID());
            }
            
            primitivesArray->appendValue(primitive);
        }
    }
    
    std::string GLTFMesh::valueType() {
        return "mesh";
    }

}