File: Assimp.cpp

package info (click to toggle)
spring 88.0%2Bdfsg1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 41,524 kB
  • sloc: cpp: 343,114; ansic: 38,414; python: 12,257; java: 12,203; awk: 5,748; sh: 1,204; xml: 997; perl: 405; objc: 192; makefile: 181; php: 134; sed: 2
file content (628 lines) | stat: -rwxr-xr-x 19,704 bytes parent folder | download
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
/*
---------------------------------------------------------------------------
Open Asset Import Library (ASSIMP)
---------------------------------------------------------------------------

Copyright (c) 2006-2010, ASSIMP Development Team

All rights reserved.

Redistribution and use of this software 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 ASSIMP team, nor the names of its
  contributors may be used to endorse or promote products
  derived from this software without specific prior
  written permission of the ASSIMP Development Team.

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 THE COPYRIGHT 
OWNER OR CONTRIBUTORS 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.
---------------------------------------------------------------------------
*/
/** @file  Assimp.cpp
 *  @brief Implementation of the Plain-C API
 */

#include "AssimpPCH.h"
#include "../include/assimp.h"

#include "GenericProperty.h"
#include "CInterfaceIOWrapper.h"
#include "Importer.h"

// ------------------------------------------------------------------------------------------------
#ifdef AI_C_THREADSAFE
#	include <boost/thread/thread.hpp>
#	include <boost/thread/mutex.hpp>
#endif
// ------------------------------------------------------------------------------------------------
using namespace Assimp;

namespace Assimp
{

	/** Stores the importer objects for all active import processes */
	typedef std::map<const aiScene*, Assimp::Importer*> ImporterMap;

	/** Stores the LogStream objects for all active C log streams */
	struct mpred {
		bool operator  () (const aiLogStream& s0, const aiLogStream& s1) const  {
			return s0.callback<s1.callback&&s0.user<s1.user;
		}
	};
	typedef std::map<aiLogStream, Assimp::LogStream*, mpred> LogStreamMap;

	/** Stores the LogStream objects allocated by #aiGetPredefinedLogStream */
	typedef std::list<Assimp::LogStream*> PredefLogStreamMap;

	/** Local storage of all active import processes */
	static ImporterMap gActiveImports;

	/** Local storage of all active log streams */
	static LogStreamMap gActiveLogStreams;

	/** Local storage of LogStreams allocated by #aiGetPredefinedLogStream */
	static PredefLogStreamMap gPredefinedStreams;

	/** Error message of the last failed import process */
	static std::string gLastErrorString;

	/** Verbose logging active or not? */
	static aiBool gVerboseLogging = false;
}

/** Configuration properties */
static ImporterPimpl::IntPropertyMap gIntProperties;
static ImporterPimpl::FloatPropertyMap gFloatProperties;
static ImporterPimpl::StringPropertyMap	gStringProperties;

#ifdef AI_C_THREADSAFE
/** Global mutex to manage the access to the importer map */
static boost::mutex gMutex;

/** Global mutex to manage the access to the logstream map */
static boost::mutex gLogStreamMutex;
#endif




// ------------------------------------------------------------------------------------------------
// Custom LogStream implementation for the C-API
class LogToCallbackRedirector : public LogStream
{
public:
	LogToCallbackRedirector(const aiLogStream& s) 
		: stream (s)	{
			ai_assert(NULL != s.callback);
	}

	~LogToCallbackRedirector()	{
#ifdef AI_C_THREADSAFE
		boost::mutex::scoped_lock lock(gLogStreamMutex);
#endif
		// (HACK) Check whether the 'stream.user' pointer points to a
		// custom LogStream allocated by #aiGetPredefinedLogStream.
		// In this case, we need to delete it, too. Of course, this 
		// might cause strange problems, but the chance is quite low.

		PredefLogStreamMap::iterator it = std::find(gPredefinedStreams.begin(), 
			gPredefinedStreams.end(), (Assimp::LogStream*)stream.user);

		if (it != gPredefinedStreams.end()) {
			delete *it;
			gPredefinedStreams.erase(it);
		}
	}

	/** @copydoc LogStream::write */
	void write(const char* message)	{
		stream.callback(message,stream.user);
	}

private:
	aiLogStream stream;
};

// ------------------------------------------------------------------------------------------------
void ReportSceneNotFoundError()
{
	DefaultLogger::get()->error("Unable to find the Assimp::Importer for this aiScene. "
		"Are you playing fools with us? Don't mix cpp and c API. Thanks.");

	assert(false);
}

// ------------------------------------------------------------------------------------------------
// Reads the given file and returns its content. 
const aiScene* aiImportFile( const char* pFile, unsigned int pFlags)
{
	return aiImportFileEx(pFile,pFlags,NULL);
}

// ------------------------------------------------------------------------------------------------
const aiScene* aiImportFileEx( const char* pFile, unsigned int pFlags, 
	aiFileIO* pFS)
{
	ai_assert(NULL != pFile);

	const aiScene* scene = NULL;
	ASSIMP_BEGIN_EXCEPTION_REGION();

	// create an Importer for this file
	Assimp::Importer* imp = new Assimp::Importer();

#ifdef AI_C_THREADSAFE
	boost::mutex::scoped_lock lock(gMutex);
#endif
	// copy the global property lists to the Importer instance
	imp->pimpl->mIntProperties = gIntProperties;
	imp->pimpl->mFloatProperties = gFloatProperties;
	imp->pimpl->mStringProperties = gStringProperties;

#ifdef AI_C_THREADSAFE
	lock.unlock();
#endif

	// setup a custom IO system if necessary
	if (pFS)	{
		imp->SetIOHandler( new CIOSystemWrapper (pFS) );
	}

	// and have it read the file
	scene = imp->ReadFile( pFile, pFlags);

	// if succeeded, place it in the collection of active processes
	if( scene)	{
#ifdef AI_C_THREADSAFE
		lock.lock();
#endif
		gActiveImports[scene] = imp;
	} 
	else	{
		// if failed, extract error code and destroy the import
		gLastErrorString = imp->GetErrorString();
		delete imp;
	}

	// return imported data. If the import failed the pointer is NULL anyways
	ASSIMP_END_EXCEPTION_REGION(const aiScene*);
	return scene;
}

// ------------------------------------------------------------------------------------------------
const aiScene* aiImportFileFromMemory( 
	const char* pBuffer,
	unsigned int pLength,
	unsigned int pFlags,
	const char* pHint)
{
	ai_assert(NULL != pBuffer && 0 != pLength);

	const aiScene* scene = NULL;
	ASSIMP_BEGIN_EXCEPTION_REGION();

	// create an Importer for this file
	Assimp::Importer* imp = new Assimp::Importer();

#ifdef AI_C_THREADSAFE
	boost::mutex::scoped_lock lock(gMutex);
#endif
	// copy the global property lists to the Importer instance
	imp->pimpl->mIntProperties = gIntProperties;
	imp->pimpl->mFloatProperties = gFloatProperties;
	imp->pimpl->mStringProperties = gStringProperties;

#ifdef AI_C_THREADSAFE
	lock.unlock();
#endif

	// and have it read the file from the memory buffer
	scene = imp->ReadFileFromMemory( pBuffer, pLength, pFlags,pHint);

	// if succeeded, place it in the collection of active processes
	if( scene)	{
#ifdef AI_C_THREADSAFE
		lock.lock();
#endif
		gActiveImports[scene] = imp;
	} 
	else	{
		// if failed, extract error code and destroy the import
		gLastErrorString = imp->GetErrorString();
		delete imp;
	}
	// return imported data. If the import failed the pointer is NULL anyways
	ASSIMP_END_EXCEPTION_REGION(const aiScene*);
	return scene;
}

// ------------------------------------------------------------------------------------------------
// Releases all resources associated with the given import process. 
void aiReleaseImport( const aiScene* pScene)
{
	if (!pScene) {
		return;
	}

	ASSIMP_BEGIN_EXCEPTION_REGION();

#ifdef AI_C_THREADSAFE
	boost::mutex::scoped_lock lock(gMutex);
#endif
	
	// find the importer associated with this data
	ImporterMap::iterator it = gActiveImports.find( pScene);
	// it should be there... else the user is playing fools with us
	if( it == gActiveImports.end())	{
		ReportSceneNotFoundError();
		return;
	}

	// kill the importer, the data dies with it
	delete it->second;
	gActiveImports.erase( it);
	ASSIMP_END_EXCEPTION_REGION(void);
}

// ------------------------------------------------------------------------------------------------
ASSIMP_API const aiScene* aiApplyPostProcessing(const aiScene* pScene,
	unsigned int pFlags)
{
	const aiScene* sc = NULL;
	

	ASSIMP_BEGIN_EXCEPTION_REGION();

#ifdef AI_C_THREADSAFE
	boost::mutex::scoped_lock lock(gMutex);
#endif
	// find the importer associated with this data
	ImporterMap::iterator it = gActiveImports.find( pScene);
	// it should be there... else the user is playing fools with us
	if( it == gActiveImports.end())	{
		ReportSceneNotFoundError();
		return NULL;
	}
#ifdef AI_C_THREADSAFE
	lock.unlock();
#endif
	sc = it->second->ApplyPostProcessing(pFlags);
#ifdef AI_C_THREADSAFE
	lock.lock();
#endif
	if (!sc) {
		// kill the importer, the data dies with it
		delete it->second;
		gActiveImports.erase( it);
		return NULL;
	}

	ASSIMP_END_EXCEPTION_REGION(const aiScene*);
	return sc;
}

// ------------------------------------------------------------------------------------------------
void CallbackToLogRedirector (const char* msg, char* dt)
{
	ai_assert(NULL != msg && NULL != dt);
	LogStream* s = (LogStream*)dt;

	s->write(msg);
}

// ------------------------------------------------------------------------------------------------
ASSIMP_API aiLogStream aiGetPredefinedLogStream(aiDefaultLogStream pStream,const char* file)
{
	aiLogStream sout;

	ASSIMP_BEGIN_EXCEPTION_REGION();
	LogStream* stream = LogStream::createDefaultStream(pStream,file);
	if (!stream) {
		sout.callback = NULL;
		sout.user = NULL;
	}
	else {
		sout.callback = &CallbackToLogRedirector;
		sout.user = (char*)stream;
	}
	gPredefinedStreams.push_back(stream);
	ASSIMP_END_EXCEPTION_REGION(aiLogStream);
	return sout;
}

// ------------------------------------------------------------------------------------------------
ASSIMP_API void aiAttachLogStream( const aiLogStream* stream )
{
	ASSIMP_BEGIN_EXCEPTION_REGION();

#ifdef AI_C_THREADSAFE
	boost::mutex::scoped_lock lock(gLogStreamMutex);
#endif

	LogStream* lg = new LogToCallbackRedirector(*stream);
	gActiveLogStreams[*stream] = lg;

	if (DefaultLogger::isNullLogger()) {
		DefaultLogger::create(NULL,(gVerboseLogging == AI_TRUE ? Logger::VERBOSE : Logger::NORMAL));
	}
	DefaultLogger::get()->attachStream(lg);
	ASSIMP_END_EXCEPTION_REGION(void);
}

// ------------------------------------------------------------------------------------------------
ASSIMP_API aiReturn aiDetachLogStream( const aiLogStream* stream)
{
	ASSIMP_BEGIN_EXCEPTION_REGION();

#ifdef AI_C_THREADSAFE
	boost::mutex::scoped_lock lock(gLogStreamMutex);
#endif
	// find the logstream associated with this data
	LogStreamMap::iterator it = gActiveLogStreams.find( *stream);
	// it should be there... else the user is playing fools with us
	if( it == gActiveLogStreams.end())	{
		return AI_FAILURE;
	}
	DefaultLogger::get()->detatchStream( it->second );
	delete it->second;

	gActiveLogStreams.erase( it);

	if (gActiveLogStreams.empty()) {
		DefaultLogger::kill();
	}
	ASSIMP_END_EXCEPTION_REGION(aiReturn);
	return AI_SUCCESS;
}

// ------------------------------------------------------------------------------------------------
ASSIMP_API void aiDetachAllLogStreams(void)
{
	ASSIMP_BEGIN_EXCEPTION_REGION();
#ifdef AI_C_THREADSAFE
	boost::mutex::scoped_lock lock(gLogStreamMutex);
#endif
	for (LogStreamMap::iterator it = gActiveLogStreams.begin(); it != gActiveLogStreams.end(); ++it) {
		DefaultLogger::get()->detatchStream( it->second );
		delete it->second;
	}
	gActiveLogStreams.clear();
	DefaultLogger::kill();
	ASSIMP_END_EXCEPTION_REGION(void);
}

// ------------------------------------------------------------------------------------------------
ASSIMP_API void aiEnableVerboseLogging(aiBool d)
{
	if (!DefaultLogger::isNullLogger()) {
		DefaultLogger::get()->setLogSeverity((d == AI_TRUE ? Logger::VERBOSE : Logger::NORMAL));
	}
	gVerboseLogging = d;
}

// ------------------------------------------------------------------------------------------------
// Returns the error text of the last failed import process. 
const char* aiGetErrorString()
{
	return gLastErrorString.c_str();
}

// ------------------------------------------------------------------------------------------------
// Returns the error text of the last failed import process. 
aiBool aiIsExtensionSupported(const char* szExtension)
{
	ai_assert(NULL != szExtension);
	aiBool candoit=AI_FALSE;
	ASSIMP_BEGIN_EXCEPTION_REGION();

#ifdef AI_C_THREADSAFE
	boost::mutex::scoped_lock lock(gMutex);
#endif

	if (!gActiveImports.empty())	{
		return ((*(gActiveImports.begin())).second->IsExtensionSupported( szExtension )) ? AI_TRUE : AI_FALSE;
	}

	// fixme: no need to create a temporary Importer instance just for that .. 
	Assimp::Importer tmp;
	candoit = tmp.IsExtensionSupported(std::string(szExtension)) ? AI_TRUE : AI_FALSE;

	ASSIMP_END_EXCEPTION_REGION(aiBool);
	return candoit;
}

// ------------------------------------------------------------------------------------------------
// Get a list of all file extensions supported by ASSIMP
void aiGetExtensionList(aiString* szOut)
{
	ai_assert(NULL != szOut);
	ASSIMP_BEGIN_EXCEPTION_REGION();

#ifdef AI_C_THREADSAFE
	boost::mutex::scoped_lock lock(gMutex);
#endif

	if (!gActiveImports.empty()) {
		(*(gActiveImports.begin())).second->GetExtensionList(*szOut);
		return;
	}
	// fixme: no need to create a temporary Importer instance just for that .. 
	Assimp::Importer tmp;
	tmp.GetExtensionList(*szOut);

	ASSIMP_END_EXCEPTION_REGION(void);
}

// ------------------------------------------------------------------------------------------------
// Get the memory requirements for a particular import.
void aiGetMemoryRequirements(const C_STRUCT aiScene* pIn,
	C_STRUCT aiMemoryInfo* in)
{
	ASSIMP_BEGIN_EXCEPTION_REGION();
#ifdef AI_C_THREADSAFE
	boost::mutex::scoped_lock lock(gMutex);
#endif

	// find the importer associated with this data
	ImporterMap::iterator it = gActiveImports.find( pIn);
	// it should be there... else the user is playing fools with us
	if( it == gActiveImports.end())	{
		ReportSceneNotFoundError();
		return;
	}
	// get memory statistics
#ifdef AI_C_THREADSAFE
	lock.unlock();
#endif
	it->second->GetMemoryRequirements(*in);
	ASSIMP_END_EXCEPTION_REGION(void);
}

// ------------------------------------------------------------------------------------------------
// Importer::SetPropertyInteger
ASSIMP_API void aiSetImportPropertyInteger(const char* szName, int value)
{
	ASSIMP_BEGIN_EXCEPTION_REGION();
#ifdef AI_C_THREADSAFE
	boost::mutex::scoped_lock lock(gMutex);
#endif
	SetGenericProperty<int>(gIntProperties,szName,value,NULL);
	ASSIMP_END_EXCEPTION_REGION(void);
}

// ------------------------------------------------------------------------------------------------
// Importer::SetPropertyFloat
ASSIMP_API void aiSetImportPropertyFloat(const char* szName, float value)
{
	ASSIMP_BEGIN_EXCEPTION_REGION();
#ifdef AI_C_THREADSAFE
	boost::mutex::scoped_lock lock(gMutex);
#endif
	SetGenericProperty<float>(gFloatProperties,szName,value,NULL);
	ASSIMP_END_EXCEPTION_REGION(void);
}

// ------------------------------------------------------------------------------------------------
// Importer::SetPropertyString
ASSIMP_API void aiSetImportPropertyString(const char* szName,
	const C_STRUCT aiString* st)
{
	if (!st) {
		return;
	}
	ASSIMP_BEGIN_EXCEPTION_REGION();
#ifdef AI_C_THREADSAFE
	boost::mutex::scoped_lock lock(gMutex);
#endif
	SetGenericProperty<std::string>(gStringProperties,szName,
		std::string( st->data ),NULL);
	ASSIMP_END_EXCEPTION_REGION(void);
}

// ------------------------------------------------------------------------------------------------
// Rotation matrix to quaternion
ASSIMP_API void aiCreateQuaternionFromMatrix(aiQuaternion* quat,const aiMatrix3x3* mat)
{
	ai_assert(NULL != quat && NULL != mat);
	*quat = aiQuaternion(*mat);
}

// ------------------------------------------------------------------------------------------------
// Matrix decomposition
ASSIMP_API void aiDecomposeMatrix(const aiMatrix4x4* mat,aiVector3D* scaling,
	aiQuaternion* rotation,
	aiVector3D* position)
{
	ai_assert(NULL != rotation && NULL != position && NULL != scaling && NULL != mat);
	mat->Decompose(*scaling,*rotation,*position);
}

// ------------------------------------------------------------------------------------------------
// Matrix transpose
ASSIMP_API void aiTransposeMatrix3(aiMatrix3x3* mat)
{
	ai_assert(NULL != mat);
	mat->Transpose();
}

// ------------------------------------------------------------------------------------------------
ASSIMP_API void aiTransposeMatrix4(aiMatrix4x4* mat)
{
	ai_assert(NULL != mat);
	mat->Transpose();
}

// ------------------------------------------------------------------------------------------------
// Vector transformation
ASSIMP_API void aiTransformVecByMatrix3(C_STRUCT aiVector3D* vec, 
	const C_STRUCT aiMatrix3x3* mat)
{
	ai_assert(NULL != mat && NULL != vec);
	*vec *= (*mat);
}

// ------------------------------------------------------------------------------------------------
ASSIMP_API void aiTransformVecByMatrix4(C_STRUCT aiVector3D* vec, 
	const C_STRUCT aiMatrix4x4* mat)
{
	ai_assert(NULL != mat && NULL != vec);
	*vec *= (*mat);
}

// ------------------------------------------------------------------------------------------------
// Matrix multiplication
ASSIMP_API void aiMultiplyMatrix4(
	C_STRUCT aiMatrix4x4* dst, 
	const C_STRUCT aiMatrix4x4* src)
{
	ai_assert(NULL != dst && NULL != src);
	*dst = (*dst) * (*src);
}

// ------------------------------------------------------------------------------------------------
ASSIMP_API void aiMultiplyMatrix3(
	C_STRUCT aiMatrix3x3* dst, 
	const C_STRUCT aiMatrix3x3* src)
{
	ai_assert(NULL != dst && NULL != src);
	*dst = (*dst) * (*src);
}

// ------------------------------------------------------------------------------------------------
// Matrix identity
ASSIMP_API void aiIdentityMatrix3(
	C_STRUCT aiMatrix3x3* mat)
{
	ai_assert(NULL != mat);
	*mat = aiMatrix3x3();
}

// ------------------------------------------------------------------------------------------------
ASSIMP_API void aiIdentityMatrix4(
	C_STRUCT aiMatrix4x4* mat)
{
	ai_assert(NULL != mat);
	*mat = aiMatrix4x4();
}