VAUtils.cpp 8.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
/*
 *  --------------------------------------------------------------------------------------------
 *
 *    VVV        VVV A           Virtual Acoustics (VA) | http://www.virtualacoustics.org
 *     VVV      VVV AAA          Licensed under the Apache License, Version 2.0
 *      VVV    VVV   AAA
 *       VVV  VVV     AAA        Copyright 2015-2017
 *        VVVVVV       AAA       Institute of Technical Acoustics (ITA)
 *         VVVV         AAA      RWTH Aachen University
 *
 *  --------------------------------------------------------------------------------------------
 */

Jonas Stienen's avatar
Jonas Stienen committed
14
15
16
17
18
#include "VAUtils.h"

#include "../Scene/VAMotionState.h"

#include <ITAConstants.h>
Jonas Stienen's avatar
Jonas Stienen committed
19
#include <ITAFileSystemUtils.h>
Jonas Stienen's avatar
Jonas Stienen committed
20
21
22
23
24
#include <ITANumericUtils.h>
#include <ITAStringUtils.h>
#include <ITAConfigUtils.h>

#include <VistaBase/VistaTimeUtils.h>
25
#include <VistaBase/VistaQuaternion.h>
Jonas Stienen's avatar
Jonas Stienen committed
26
27
28
29
30
31
32

#include <cassert>

#ifdef WIN32
#include <windows.h>
#endif

Jonas Stienen's avatar
Jonas Stienen committed
33

Jonas Stienen's avatar
Jonas Stienen committed
34
35
36
37
38
void VASleep( int iMilliseconds )
{
	VistaTimeUtils::Sleep( iMilliseconds );
}

39
40
long getCurrentThreadID()
{
Jonas Stienen's avatar
Jonas Stienen committed
41
42
43
44
45
46
47
48
#ifdef WIN32
	return GetCurrentThreadId();
#else
	assert( !"Not implemented" );
	return -1;
#endif
}

49
50
float getCurrentProcessSystemLoad()
{
Jonas Stienen's avatar
Jonas Stienen committed
51
52
53
54
55
	/*
	FILETIME ftSysIdle, ftSysKernel, ftSysUser;
	FILETIME ftProcCreation, ftProcExit, ftProcKernel, ftProcUser;

	if (SUCCEEDED(GetSystemTimes(&ftSysIdle, &ftSysKernel, &ftSysUser)) &&
56
	SUCCEEDED(GetProcessTimes(GetCurrentProcess(), &ftProcCreation, &ftProcExit, &ftProcKernel, &ftProcUser)))
Jonas Stienen's avatar
Jonas Stienen committed
57
58
59
60
61
62
63
	{

	}
	*/
	return 0.0f;
}

64
65
66
CVAException getDefaultUnexpectedVAException()
{
	return CVAException( CVAException::UNSPECIFIED, "An unspecified exception occured. Please contact the developer." ); \
Jonas Stienen's avatar
Jonas Stienen committed
67
68
}

69
CVAException convert2VAException( const ITAException& e )
Jonas Stienen's avatar
Jonas Stienen committed
70
71
72
73
74
{
	// TODO: Refine
	return CVAException( CVAException::UNSPECIFIED, e.sModule + ": " + e.sReason );
}

75
76
CVAException convert2VAException( const VistaExceptionBase& e )
{
Jonas Stienen's avatar
Jonas Stienen committed
77
	// TODO: Refine
78
	return CVAException( CVAException::UNSPECIFIED, e.GetExceptionText() );
Jonas Stienen's avatar
Jonas Stienen committed
79
80
}

81
void ConvertQuaternionToViewUp( const VAQuat& qOrient, VAVec3& v3View, VAVec3& v3Up )
Jonas Stienen's avatar
Jonas Stienen committed
82
{
83
84
85
86
87
	VistaQuaternion qVistaQuat;
	qVistaQuat[ Vista::X ] = float( qOrient.x );
	qVistaQuat[ Vista::Y ] = float( qOrient.y );
	qVistaQuat[ Vista::Z ] = float( qOrient.z );
	qVistaQuat[ Vista::W ] = float( qOrient.w );
Jonas Stienen's avatar
Jonas Stienen committed
88

89
90
	const VistaVector3D vVistaView = qVistaQuat.GetViewDir();
	v3View.Set( vVistaView[ Vista::X ], vVistaView[ Vista::Y ], vVistaView[ Vista::Z ] );
Jonas Stienen's avatar
Jonas Stienen committed
91

92
93
	const VistaVector3D vVistaUp = qVistaQuat.GetUpDir();
	v3Up.Set( vVistaUp[ Vista::X ], vVistaUp[ Vista::Y ], vVistaUp[ Vista::Z ] );
Jonas Stienen's avatar
Jonas Stienen committed
94
95
}

96
void ConvertViewUpToQuaternion( const VAVec3& vView, const VAVec3& vUp, VAQuat& qOrient )
Jonas Stienen's avatar
Jonas Stienen committed
97
{
98
99
	const VistaVector3D vVistaView( float( vView.x ), float( vView.y ), float( vView.z ) );
	const VistaVector3D vVistaUp( float( vUp.x ), float( vUp.y ), float( vUp.z ) );
Jonas Stienen's avatar
Jonas Stienen committed
100

101
102
103
104
105
106
	VistaQuaternion qVistaQuat;
	qVistaQuat.SetFromViewAndUpDir( vVistaView, vVistaUp );
	qOrient.x = qVistaQuat[ Vista::X ];
	qOrient.y = qVistaQuat[ Vista::Y ];
	qOrient.z = qVistaQuat[ Vista::Z ];
	qOrient.w = qVistaQuat[ Vista::W ];
Jonas Stienen's avatar
Jonas Stienen committed
107
108
}

109
110
std::string correctPathForLUA( const std::string& sPath )
{
Jonas Stienen's avatar
Jonas Stienen committed
111
	// Convert into system path
112
	std::string s = correctPath( sPath );
Jonas Stienen's avatar
Jonas Stienen committed
113
114

	// Einfache Backslashes in doppelte Backslashes umwandeln
115
116
	std::string sSingleSeperator = std::string( 1, PATH_SEPARATOR );
	std::string sDoubleSeperator = std::string( 1, PATH_SEPARATOR ) + std::string( 1, PATH_SEPARATOR );
Jonas Stienen's avatar
Jonas Stienen committed
117
	size_t pos = 0;
118
119
120
	while( ( pos = s.find( sSingleSeperator, pos ) ) != std::string::npos )
	{
		s.replace( pos, 1, sDoubleSeperator.c_str(), 2 );
Jonas Stienen's avatar
Jonas Stienen committed
121
122
123
124
125
126
		++++pos;
	}

	return s;
}

127
128
129
CVAStructValue interpretStructKey( const std::string& s )
{
	ITAConversion conv( ITAConversion::STRICT_MODE );
Jonas Stienen's avatar
Jonas Stienen committed
130
131
132
	bool b;
	int i;
	double d;
133
134
135
136
137
138
	if( conv.StringToBool( s, b ) )
		return CVAStructValue( b );
	if( conv.StringToInt( s, i ) )
		return CVAStructValue( i );
	if( conv.StringToDouble( s, d ) )
		return CVAStructValue( d );
Jonas Stienen's avatar
Jonas Stienen committed
139
	// Fallback solution: Interpret as string
140
	return CVAStructValue( s );
Jonas Stienen's avatar
Jonas Stienen committed
141
142
}

143
void LoadStructFromINIFIle( const std::string& sFilePath, CVAStruct& oData )
144
{
145
146
	if( !doesFileExist( sFilePath ) )
		VA_EXCEPT2( FILE_NOT_FOUND, std::string( "INI file \"" ) + sFilePath + std::string( "\" not found" ) );
147

Jonas Stienen's avatar
Jonas Stienen committed
148
	oData.Clear();
149
	std::vector<std::string> vsSections = INIFileGetSections( sFilePath );
Jonas Stienen's avatar
Jonas Stienen committed
150

151
152
	for( std::vector<std::string>::iterator it = vsSections.begin(); it != vsSections.end(); ++it )
	{
Jonas Stienen's avatar
Jonas Stienen committed
153
		std::string& sSection( *it );
154
		std::vector<std::string> vsKeys = INIFileGetKeys( sFilePath, sSection );
155

Jonas Stienen's avatar
Jonas Stienen committed
156
		// Spezialfall: Section [] nach Root abbilden
157
158
159
		if( it->empty() )
		{
			for( std::vector<std::string>::iterator jt = vsKeys.begin(); jt != vsKeys.end(); ++jt )
Jonas Stienen's avatar
Jonas Stienen committed
160
161
162
			{
				std::string& sKey( *jt );
				if( oData.HasKey( sKey ) )
163
164
					VA_EXCEPT2( INVALID_PARAMETER, "Uniqueness violation in " + sFilePath + ": multiple detection of key '" + sKey + "' in section '" + sSection + "'" );
				oData[ sKey ] = interpretStructKey( INIFileReadString( sFilePath, sSection, sKey ) );
Jonas Stienen's avatar
Jonas Stienen committed
165
			}
166
167
168
		}
		else
		{
Jonas Stienen's avatar
Jonas Stienen committed
169
			CVAStruct oSubData;
170
			for( std::vector<std::string>::iterator jt = vsKeys.begin(); jt != vsKeys.end(); ++jt )
Jonas Stienen's avatar
Jonas Stienen committed
171
172
173
			{
				std::string& sKey( *jt );
				if( oData.HasKey( sKey ) )
174
175
					VA_EXCEPT2( INVALID_PARAMETER, "Uniqueness violation in " + sFilePath + ": multiple detection of key '" + sKey + "' in section '" + sSection + "'" );
				oSubData[ *jt ] = interpretStructKey( INIFileReadString( sFilePath, sSection, sKey ) );
Jonas Stienen's avatar
Jonas Stienen committed
176
			}
177
			oData[ sSection ] = oSubData;
Jonas Stienen's avatar
Jonas Stienen committed
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
void StoreStructToINIFile( const std::string& sFilePath, const CVAStruct& oData )
{
	CVAStruct::const_iterator cit = oData.Begin();
	while( cit != oData.End() )
	{
		const std::string& sKey( cit->first );
		const CVAStructValue& oValue( cit->second );
		cit++;
		

		// Root-level plain key-value pairs, a bit unusual though

		switch( oValue.GetDatatype() )
		{
		case CVAStructValue::BOOL:
		{
			INIFileWriteBool( sKey, bool( oValue ) );
			break;
		}
		case CVAStructValue::INT:
		{
			INIFileWriteInt( sKey, int( oValue ) );
			break;
		}
		case CVAStructValue::DOUBLE:
		{
			INIFileWriteDouble( sKey, double( oValue ) );
			break;
		}
		case CVAStructValue::STRING:
		{
			INIFileWriteString( sKey, std::string( oValue ) );
			break;
		}
		case CVAStructValue::STRUCT:
		{
			const std::string& sSection( sKey );
			const CVAStruct& oSection( oValue );

			CVAStruct::const_iterator cit_section = oSection.Begin();
			while( cit_section != oSection.End() )
			{
				const std::string& sKeyInSection( cit_section->first );
				const CVAStructValue& oSectionValue( cit_section->second );
				cit_section++;


				// Section-level key-value pairs

				switch( oSectionValue.GetDatatype() )
				{
				case CVAStructValue::BOOL:
				{
					INIFileWriteBool( sFilePath, sSection, sKeyInSection, bool( oSectionValue ) );
					break;
				}
				case CVAStructValue::INT:
				{
					INIFileWriteInt( sFilePath, sSection, sKeyInSection, int( oSectionValue ) );
					break;
				}
				case CVAStructValue::DOUBLE:
				{
					INIFileWriteDouble( sFilePath, sSection, sKeyInSection, double( oSectionValue ) );
					break;
				}
				case CVAStructValue::STRING:
				{
					INIFileWriteString( sFilePath, sSection, sKeyInSection, std::string( oSectionValue ) );
					break;
				}
				}
			}
			break;
		}
		}
	}
}

Jonas Stienen's avatar
Jonas Stienen committed
261
262
263
264
double GetAzimuthOnTarget_DEG( const VAVec3& vOriginPos, const VAVec3& vView, const VAVec3& vUp, const VAVec3& vTargetPos )
{
	VAVec3 vDir = vTargetPos - vOriginPos;
	vDir.Norm();
265
	VAVec3 vViewMinusZ = vView * ( -1.0f ); // local z axis
Jonas Stienen's avatar
Jonas Stienen committed
266
267
268
269
270
271
272
273
274
275
276
277
278
279
	const VAVec3 vRight = vViewMinusZ.Cross( vUp ); // local x axis
	const double dAzimuthAngleDeg = atan2( vDir.Dot( vRight ), vDir.Dot( vView ) ) * 180.0f / ITAConstants::PI_D;
	return ( ( dAzimuthAngleDeg < 0.0f ) ? ( dAzimuthAngleDeg + 360.0f ) : dAzimuthAngleDeg );
}

double GetElevationOnTarget_DEG( const VAVec3& vOriginPos, const VAVec3& vView, const VAVec3& vUp, const VAVec3& vTargetPos )
{
	VAVec3 vDir = vTargetPos - vOriginPos;
	vDir.Norm();
	const VAVec3 vRight = vView.Cross( vUp );
	const double dElevationAngleDeg = asin( vDir.Dot( vUp ) ) * 180.0f / ITAConstants::PI_D;
	return dElevationAngleDeg;
}

280
281
void SetCoreEventParams( CVAEvent& oEvent, const CVAMotionState* pMotionState )
{
Jonas Stienen's avatar
Jonas Stienen committed
282
283
284
285
	assert( pMotionState );
	oEvent.vPos = pMotionState->GetPosition();
	oEvent.vView = pMotionState->GetView();
	oEvent.vUp = pMotionState->GetUp();
286
	oEvent.oOrientation = pMotionState->GetOrientation();
287
	oEvent.qHATO = pMotionState->GetHeadAboveTorsoOrientation();
Jonas Stienen's avatar
Jonas Stienen committed
288
}