File: daeSTLDatabase.cpp

package info (click to toggle)
collada-dom 2.4.4%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 17,096 kB
  • sloc: cpp: 156,849; php: 4,567; makefile: 38; sh: 32; python: 14
file content (680 lines) | stat: -rw-r--r-- 17,235 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
/*
* Copyright 2006 Sony Computer Entertainment Inc.
*
* Licensed under the MIT Open Source License, for details please see license.txt or the website
* http://www.opensource.org/licenses/mit-license.php
*
*/ 

#include <modules/daeSTLDatabase.h>
#include <dae/daeMetaElement.h>

using namespace std;

daeSTLDatabase::daeSTLDatabase(DAE& dae) : daeDatabase(dae)
{ }

daeSTLDatabase::~daeSTLDatabase()
{
	clear();
}

daeInt daeSTLDatabase::setMeta(daeMetaElement *_topMeta)
{
	topMeta = _topMeta;
	return DAE_OK;
}

daeBool
daeSTLDatabase::isDocumentLoaded(daeString name)
{
	daeDocument* document = getDocument(name);
	if(document)
		return(true);
	else
		return(false);
}

// Element Types of all Elements
daeUInt daeSTLDatabase::getTypeCount()
{
	return (daeUInt)elements.size();
}


daeString daeSTLDatabase::getTypeName(daeUInt index)
{
	daeUInt count = 0;
	
	map<string, vector< daeElement* > >::iterator iter = elements.begin();
	map<string, vector< daeElement* > >::iterator end = elements.end();
	while ( iter != end )
	{
		if ( count == index )
		{
			return (*iter).first.c_str();
		}
		++count;
		++iter;
	}

	return NULL;
}

// Documents
daeInt daeSTLDatabase::insertDocument(const char *name, daeElement* dom, daeDocument** document, bool zaeRootDocument, const std::string& extractedFileURI)
{
	return createDocument( name, dom, document, zaeRootDocument, extractedFileURI );
}
daeInt daeSTLDatabase::createDocument(const char *name, daeElement* dom, daeDocument** document, bool zaeRootDocument, const std::string& extractedFileURI)
{
	// If a document already exists with the same name, error
	if(isDocumentLoaded(name))
	{
		if (document)
			*document = NULL;
		return DAE_ERR_COLLECTION_ALREADY_EXISTS;
	}
	
	// Make a new document
	daeDocument *newDocument = new daeDocument(dae, zaeRootDocument, extractedFileURI);
	newDocument->getDocumentURI()->setURI(name);
	newDocument->setDomRoot(dom);
	// Push the connection into the database
	documents.push_back(newDocument);
	
	if (document)
		*document = newDocument;

	return DAE_OK;
}
// !!!GAC revised version of insertDocument, creates a domCollada and fills it in for you.
daeInt daeSTLDatabase::insertDocument(const char *name, daeDocument** document)
{
	return createDocument( name, document );
}
daeInt daeSTLDatabase::createDocument(const char *name, daeDocument** document)
{

	// If a document already exists with the same name, error
	if(isDocumentLoaded(name))
	{
		if (document)
			*document = NULL;
		return DAE_ERR_COLLECTION_ALREADY_EXISTS;
	}
	// Make the new document
	daeDocument *newDocument = new daeDocument(dae);
	// Make a domCOLLADA to be the root of this new document (this makes a reference so the domCOLLADA won't delete itself
	daeElementRef myCOLLADA = topMeta->create();
	myCOLLADA->setDocument(newDocument);
	newDocument->getDocumentURI()->setURI(name);
	newDocument->setDomRoot(myCOLLADA);

	// Add this document to the list.
	documents.push_back(newDocument);
	// If the user gave us a place to put the document, send it back to them.
	if (document)
		*document = newDocument;
	
	return DAE_OK;
}

daeInt daeSTLDatabase::insertDocument( daeDocument *c ) {
	documents.push_back(c);
	insertElement( c, c->getDomRoot() );
	return DAE_OK;
}

daeInt daeSTLDatabase::removeDocument(daeDocument *document)
{	
	vector< daeDocument* >::iterator iter = documents.begin();
	while ( iter != documents.end() ) {
		if ( (*iter) == document ) {
			//delete all of its children
			removeElement( *iter, (*iter)->getDomRoot() );
            delete *iter; // sthomas (see bug 1466019)
			iter = documents.erase(iter);
		}
		else {
            iter++;
		}
	}
	return DAE_OK;
}

daeUInt daeSTLDatabase::getDocumentCount()
{
	return (daeUInt)documents.size();
}

daeDocument* daeSTLDatabase::getDocument(daeUInt index)
{
	if (index<documents.size())
		return (documents[index]);
	else
		return NULL;
}

daeDocument* daeSTLDatabase::getDocument(daeString name_, bool skipUriNormalization)
{
	string name = name_;
	if (!skipUriNormalization) {
		// Normalize the input string to an absolute URI with no fragment
		name = daeURI(dae, name, true).str();
	}

	// Try to find a document that matches
	daeDocument *document;
	int documentCount	= getDocumentCount();
	for (int i=0;i<documentCount;i++)
	{
		document = getDocument(i);
		if(document->getDocumentURI()->str() == name)
			return(document);
	}
	return(NULL);
}

daeString daeSTLDatabase::getDocumentName(daeUInt index)
{
	if (index<documents.size())
		return getDocument(index)->getDocumentURI()->getURI();
	else
		return NULL;
}

// Elements
daeInt daeSTLDatabase::insertElement(daeDocument* document,daeElement* element)
{
	insertChildren( document, element );
	
	map<string, vector< daeElement* > >::iterator iter = elements.find( string( element->getTypeName() ) );
	if ( iter != elements.end() )
	{
		(*iter).second.push_back( element );
	}
	else
	{
		vector< daeElement* > vec;
		vec.push_back( element );
		elements.insert( make_pair( string( element->getTypeName() ), vec ) );
	}

	// Insert into the type ID map
	typeMap.insert(make_pair(element->typeID(), element));

	//insert into IDMap if element has an ID. IDMap is used to speed up URI resolution
	if ( element->getID() != NULL ) {
		elementsIDMap.insert( make_pair( string( element->getID() ), element ) );
	}

	// Insert into sid map if the element has a sid
// 	string sid = element->getAttribute("sid");
// 	if (!sid.empty())
// 		sidMap.insert(sidMapPair(sid, element));

	dae.getSidRefCache().clear();

	return DAE_OK;
}

daeInt daeSTLDatabase::insertChildren( daeDocument *c, daeElement *element )
{
	daeElementRefArray era;
	element->getChildren( era );
	for ( unsigned int i = 0; i < era.getCount(); i++ ) {
		insertElement( c, era[i] );
	}
	return DAE_OK;
}

daeInt daeSTLDatabase::removeElement(daeDocument* document,daeElement* element)
{
	if ( !element ) {
		return DAE_ERR_INVALID_CALL;
	}
	removeChildren( document, element );
	
	map<string, vector< daeElement* > >::iterator iter = elements.find( string( element->getTypeName() ) );
	if ( iter != elements.end() )
	{
		vector< daeElement* > &vec = (*iter).second;
		vector< daeElement* >::iterator i = vec.begin();
		vector< daeElement* >::iterator end = vec.end();
		while( i != end )
		{
			if ( (*i) == element )
			{
				vec.erase( i );
				break;
			}
			++i;
		}
	}

	typeMapRange range = typeMap.equal_range(element->typeID());
	for (typeMapIter iter = range.first; iter != range.second; iter++) {
		if (iter->second == element) {
			typeMap.erase(iter);
			break;
		}
	}

	if ( element->getID() != NULL ) {
		idMapRange range = elementsIDMap.equal_range( string( element->getID() ) );
		multimap<string, daeElement* >::iterator iter = range.first;
		while( iter != range.second ) {
			if ( (*iter).second == element ) {
				elementsIDMap.erase( iter );
				break;
			}
			++iter;
		}
	}

// 	string sid = element->getAttribute("sid");
// 	if (!sid.empty()) {
// 		pair<sidMapIter, sidMapIter> range = sidMap.equal_range(sid);
// 		for (sidMapIter iter = range.first; iter != range.second; iter++) {
// 			if (iter->second == element) {
// 				sidMap.erase(iter);
// 				break;
// 			}
// 		}
// 	}

	dae.getSidRefCache().clear();
	
	return DAE_OK;
}

daeInt daeSTLDatabase::removeChildren( daeDocument *c, daeElement *element )
{
	daeElementRefArray era;
	element->getChildren( era );
	for ( unsigned int i = 0; i < era.getCount(); i++ ) {
		removeElement( c, era[i] );	
	}
	return DAE_OK;
}

daeInt daeSTLDatabase::changeElementID( daeElement* element, daeString newID )
{
	if ( !element ) {
		return DAE_ERR_INVALID_CALL;
	}

	// Remove the current entry in the ID map if the element has an ID
	if ( element->getID() != NULL ) {
		pair< multimap<string, daeElement* >::iterator, multimap<string, daeElement* >::iterator> range;
		range = elementsIDMap.equal_range( string( element->getID() ) );
		multimap<string, daeElement* >::iterator iter = range.first;
		while( iter != range.second ) {
			if ( (*iter).second == element ) {
				elementsIDMap.erase( iter );
				break;
			}
			++iter;
		}
	}

	// Add an entry to the ID map if the element will have an ID
	if ( newID != NULL ) {
		elementsIDMap.insert( make_pair( string( newID ), element ) );
	}

	dae.getSidRefCache().clear();

	return DAE_OK;
}

daeInt daeSTLDatabase::changeElementSID(daeElement* element, daeString newSID) {
	if (!element)
		return DAE_ERR_INVALID_CALL;

// 	// Remove the current entry in the sid map if the element has a sid
// 	string sid = element->getAttribute("sid");
// 	if (!sid.empty()) {
// 		pair<sidMapIter, sidMapIter> range = sidMap.equal_range(sid);
// 		for (sidMapIter iter = range.first; iter != range.second; iter++) {
// 			if (iter->second == element) {
// 				sidMap.erase(iter);
// 				break;
// 			}
// 		}
// 	}

// 	// Add an entry to the sid map if the element will have a sid
// 	if ( newSID != NULL )
// 		sidMap.insert(sidMapPair(newSID, element));

	dae.getSidRefCache().clear();

	return DAE_OK;
}

daeInt daeSTLDatabase::clear()
{
	elements.clear();
	typeMap.clear();
	elementsIDMap.clear();
	sidMap.clear();
	int i;
	for (i=0;i<(int)documents.size();i++)
		delete documents[i];
	documents.clear(); //this will free the daeElement
	dae.getRawRefCache().clear();
	dae.getSidRefCache().clear();
	return DAE_OK;
}

daeUInt daeSTLDatabase::getElementCount(daeString name,daeString type,daeString file)
{	
	// If none of the search keys was specified, return the total element count in the database
	if ( !name && !type && !file ) 
	{
		daeUInt count = 0;
		map< string, vector< daeElement*> >::iterator iter = elements.begin();
		map< string, vector< daeElement*> >::iterator end = elements.end();
		while( iter != end )
		{
			count += (daeUInt)(*iter).second.size();
			++iter;
		}
		return count;
	}

	if ( name ) 
	{ 
		// name specified
		int count = 0;
		if ( file ) 
		{ 
			// If a document URI was a search key (in file) resolve it to a text URI with no fragment
			daeURI tempURI(dae, file,true);
			daeDocument *col = getDocument( tempURI.getURI() );
			if ( col == NULL ) {
				return 0;
			}
			// a document was specified
			pair< multimap< string, daeElement* >::iterator, multimap< string, daeElement* >::iterator > range;
			range = elementsIDMap.equal_range( string( name ) );
			multimap< string, daeElement* >::iterator i = range.first;
			while ( i != range.second )
			{
				if ( col == (*i).second->getDocument() )
				{
					count++;
				}
				++i;
			}
			return count;
		}
		else 
		{ 
			//no file specified - just name
			return (daeUInt)elementsIDMap.count( string( name ) );
		}
	}

	if ( type ) 
	{ 
		// type specified
		map< string, vector< daeElement*> >::iterator iter = elements.find( string( type ) );
		if ( iter == elements.end() )
		{
			return 0;
		}

		int count = 0;
		if ( file ) 
		{ 
			// If a document URI was a search key (in file) resolve it to a text URI with no fragment
			daeURI tempURI(dae, file,true);
			daeDocument *col = getDocument( tempURI.getURI() );
			if ( col == NULL ) {
				return 0;
			}
			// a document was specified
			vector< daeElement* > &vec = (*iter).second;
			vector< daeElement* >::iterator i = vec.begin();
			vector< daeElement* >::iterator end = vec.end();
			while( i != end )
			{
				if ( col == (*i)->getDocument() )
				{
					++count;
				}
				++i;
			}
			return count;
		}
		else 
		{ 
			//no file specified - just type
			return (daeUInt)(*iter).second.size();
		}
	}

	//if you get here only a file was specified
	daeURI tempURI(dae, file,true);
	daeDocument *col = getDocument( tempURI.getURI() );
	if ( col == NULL ) {
		return 0;
	}
	//a document was specified
	int count = 0;
	map< string, vector< daeElement*> >::iterator iter = elements.begin();
	map< string, vector< daeElement*> >::iterator end = elements.end();
	while( iter != end )
	{
		vector< daeElement* > &vec = (*iter).second;
		vector< daeElement* >::iterator i = vec.begin();
		vector< daeElement* >::iterator end2 = vec.end();
		while( i != end2 )
		{
			if( col == (*i)->getDocument() )
			{
				++count;
			}
			++i;
		}
		++iter;
	}
	return count;

}

daeInt daeSTLDatabase::getElement(daeElement** pElement,daeInt index,daeString name,daeString type,daeString file)
{	
	// If the index is out of range, there can be no match
	if ( index < 0 ) 
	{
		return DAE_ERR_QUERY_NO_MATCH;
	}

	// If no name, type or file was specified we return the element at "index" - SLOW
	if ( !name && !type && !file ) 
	{
		daeUInt count = 0;
		map< string, vector< daeElement*> >::iterator iter = elements.begin();
		map< string, vector< daeElement*> >::iterator end = elements.end();
		while( iter != end )
		{
			count += (daeUInt)(*iter).second.size();
			if ( (daeInt)count > index )
			{
				*pElement = (*iter).second[index - (count - (*iter).second.size())] ;
				return DAE_OK;
			}
			++iter;
		}
		return DAE_ERR_QUERY_NO_MATCH;
	}
	
	if ( name ) 
	{ 
		//name specified
		int count = 0;
		if ( file ) 
		{ 
			// If a document URI was a search key (in file) resolve it to a text URI with no fragment
			daeURI tempURI(dae, file, true);
			daeDocument *col = getDocument( tempURI.getURI() );
			if ( col == NULL ) {
				*pElement = NULL;
				return DAE_ERR_QUERY_NO_MATCH;
			}
			//a document was specified
			pair< multimap< string, daeElement* >::iterator, multimap< string, daeElement* >::iterator> range;
			range = elementsIDMap.equal_range( string( name ) );
			multimap< string, daeElement* >::iterator i = range.first;
			while ( i != range.second )
			{
				if ( col == (*i).second->getDocument() )
				{
					if ( count == index )
					{
						*pElement = (*i).second;
						return DAE_OK;
					}
					count++;
				}
				++i;
			}
			*pElement = NULL;
			return DAE_ERR_QUERY_NO_MATCH;
		}
		else 
		{ 
			//no document specified
			multimap< string, daeElement* >::iterator i = elementsIDMap.find( string( name ) );
			if ( index > (daeInt)elementsIDMap.count( string( name ) ) || i == elementsIDMap.end() )
			{
				*pElement = NULL;
				return DAE_ERR_QUERY_NO_MATCH;
			}
			for ( int x = 0; x < index; x++ )
			{
				++i;
			}
			*pElement = i->second;
			return DAE_OK;
		}
	}
	
	if ( type ) 
	{ 
		map< string, vector< daeElement*> >::iterator iter = elements.find( string( type ) );
		if ( iter == elements.end() )
		{
			*pElement = NULL;
			return DAE_ERR_QUERY_NO_MATCH;
		}
		//type specified
		int count = 0;
		if ( file ) 
		{ 
			// If a document URI was a search key (in file) resolve it to a text URI with no fragment
			daeURI tempURI(dae, file, true);
			daeDocument *col = getDocument( tempURI.getURI() );
			if ( col == NULL ) {
				return DAE_ERR_QUERY_NO_MATCH;
			}
			//a document was specified
			// a document was specified
			vector< daeElement* > &vec = (*iter).second;
			vector< daeElement* >::iterator i = vec.begin();
			vector< daeElement* >::iterator end = vec.end();
			while( i != end )
			{
				if ( col == (*i)->getDocument() )
				{
					if ( count == index )
					{
						*pElement = (*i);
						return DAE_OK;
					}
					++count;
				}
				++i;
			}
			return DAE_ERR_QUERY_NO_MATCH;
		}
		else 
		{ 
			//no document specified
			if ( index >= (daeInt)(*iter).second.size() )
			{
				*pElement = NULL;
				return DAE_ERR_QUERY_NO_MATCH;
			}
			*pElement = (*iter).second[index];
			return DAE_OK;
		}
	}

	//if you get here only the file was specified - SLOW
	daeURI tempURI(dae, file, true);
	daeDocument *col = getDocument( tempURI.getURI() );
	if ( col == NULL ) {
		return DAE_ERR_QUERY_NO_MATCH;
	}
	//a document was specified
	int count = 0;
	map< string, vector< daeElement*> >::iterator iter = elements.begin();
	map< string, vector< daeElement*> >::iterator end = elements.end();
	while( iter != end )
	{
		vector< daeElement* > &vec = (*iter).second;
		vector< daeElement* >::iterator i = vec.begin();
		vector< daeElement* >::iterator end2 = vec.end();
		while( i != end2 )
		{
			if( col == (*i)->getDocument() ) 
			{
				if( count == index ) 
				{
					*pElement = (*i);
					return DAE_OK;
				}
				++count;
			}
			++i;
		}
		++iter;
	}
	return DAE_ERR_QUERY_NO_MATCH;

}

vector<daeElement*> daeSTLDatabase::idLookup(const string& id) {
	vector<daeElement*> matchingElements;
	idMapRange range = elementsIDMap.equal_range(id);
	for (idMapIter iter = range.first; iter != range.second; iter++)
		matchingElements.push_back(iter->second);
	return matchingElements;
}

void daeSTLDatabase::typeLookup(daeInt typeID,
                                vector<daeElement*>& matchingElements,
                                daeDocument* doc) {
	matchingElements.clear();
	typeMapRange range = typeMap.equal_range(typeID);
	for (typeMapIter iter = range.first; iter != range.second; iter++)
		if (!doc  ||  doc == iter->second->getDocument())
			matchingElements.push_back(iter->second);
}

void daeSTLDatabase::sidLookup(const string& sid,
                               vector<daeElement*>& matchingElements,
                               daeDocument* doc) {
	matchingElements.clear();
	if (!sid.empty()) {
		sidMapRange range = sidMap.equal_range(sid);
		for (sidMapIter iter = range.first; iter != range.second; iter++)
			if (!doc  ||  doc == iter->second->getDocument())
				matchingElements.push_back(iter->second);
	}
}