demotool/utilities.cc

163 lines
3.8 KiB
C++
Raw Normal View History

2017-09-30 10:37:45 +02:00
#include "externals.hh"
#include "utilities.hh"
void disableButton( )
{
ImGui::PushStyleColor( ImGuiCol_Button , ImVec4( .3 , .3 , .3 , 1 ) );
ImGui::PushStyleColor( ImGuiCol_ButtonHovered , ImVec4( .3 , .3 , .3 , 1 ) );
ImGui::PushStyleColor( ImGuiCol_ButtonActive , ImVec4( .3 , .3 , .3 , 1 ) );
2017-09-30 10:37:45 +02:00
}
/*----------------------------------------------------------------------------*/
void updateAngle(
__rw__ float& initial ,
__rd__ const float delta
)
{
initial = fmod( initial + delta + 540 , 360 ) - 180;
}
void anglesToMatrix(
__rd__ float const* angles ,
__wr__ float* matrix )
{
float c[3] , s[3];
for ( int i = 0 ; i < 3 ; i ++ ) {
const float a = M_PI * angles[ i ] / 180;
c[i] = cos( a );
s[i] = sin( a );
}
matrix[0] = c[1]*c[2];
matrix[1] = s[0]*s[1]*c[2] - c[0]*s[2];
matrix[2] = s[0]*s[2] + c[0]*s[1]*c[2];
matrix[3] = c[1]*s[2];
matrix[4] = c[0]*c[2] + s[0]*s[1]*s[2];
matrix[5] = c[0]*s[1]*s[2] - s[0]*c[2];
matrix[6] = -s[1];
matrix[7] = s[0]*c[1];
matrix[8] = c[0]*c[1];
}
2017-10-31 09:22:38 +01:00
/*----------------------------------------------------------------------------*/
std::string GetAbsolutePath(
__rd__ std::string const& path )
{
char* const rp( realpath( path.c_str( ) , nullptr ) );
if ( !rp ) {
return {};
}
const std::string rv( rp );
free( rp );
return rv;
}
std::string GetParentPath(
__rd__ std::string const& path )
{
char buffer[ path.length( ) + 1 ];
strcpy( buffer , path.c_str( ) );
char* const rp( realpath( dirname( buffer ) , nullptr ) );
if ( !rp ) {
return {};
}
const std::string rv( rp );
free( rp );
return rv;
}
2017-10-31 09:22:38 +01:00
/*= JSON =====================================================================*/
template void jsonAdd< double >(
2017-10-31 14:21:42 +01:00
__rw__ T_JSONObject& object ,
2017-10-31 09:22:38 +01:00
__rd__ std::string const& key ,
__rd__ double const& v );
2017-10-31 14:21:42 +01:00
template void jsonAdd< T_JSONObject >(
__rw__ T_JSONObject& object ,
2017-10-31 09:22:38 +01:00
__rd__ std::string const& key ,
2017-10-31 14:21:42 +01:00
__rd__ T_JSONObject const& v );
template void jsonAdd< T_JSONArray >(
__rw__ T_JSONObject& object ,
2017-10-31 09:22:38 +01:00
__rd__ std::string const& key ,
2017-10-31 14:21:42 +01:00
__rd__ T_JSONArray const& v );
2017-10-31 09:22:38 +01:00
template void jsonAdd< std::string >(
2017-10-31 14:21:42 +01:00
__rw__ T_JSONObject& object ,
2017-10-31 09:22:38 +01:00
__rd__ std::string const& key ,
__rd__ std::string const& v );
template< >
void jsonAdd< picojson::value >(
2017-10-31 14:21:42 +01:00
__rw__ T_JSONObject& object ,
2017-10-31 09:22:38 +01:00
__rd__ std::string const& key ,
__rd__ picojson::value const& v )
{
using T_Entry = std::pair< std::string , picojson::value >;
object.insert( T_Entry( key , v ) );
}
template< >
void jsonAdd< int >(
2017-10-31 14:21:42 +01:00
__rw__ T_JSONObject& object ,
2017-10-31 09:22:38 +01:00
__rd__ std::string const& key ,
__rd__ int const& v )
{
using T_Entry = std::pair< std::string , picojson::value >;
object.insert( T_Entry( key , picojson::value( double( v ) ) ) );
}
template< >
void jsonAdd< unsigned >(
2017-10-31 14:21:42 +01:00
__rw__ T_JSONObject& object ,
2017-10-31 09:22:38 +01:00
__rd__ std::string const& key ,
__rd__ unsigned const& v )
{
using T_Entry = std::pair< std::string , picojson::value >;
object.insert( T_Entry( key , picojson::value( double( v ) ) ) );
}
/*----------------------------------------------------------------------------*/
picojson::value jsonVector( float const* vector , const int nComponents )
{
using namespace picojson;
value::array a;
for ( auto i = 0 ; i < nComponents ; i ++ ) {
a.push_back( value( vector[ i ] ) );
}
return value( a );
}
/*----------------------------------------------------------------------------*/
void jsonGetVectorOpt(
__wr__ float* vector ,
__rd__ const size_t size ,
2017-10-31 14:21:42 +01:00
__rd__ T_JSONObject const& object ,
2017-10-31 09:22:38 +01:00
__rd__ char const* key )
{
using namespace picojson;
auto const* ptr( jsonGetOpt< array >( object , key ) );
if ( !ptr ) {
return;
}
auto const& a( *ptr );
if ( a.size( ) != size ) {
throw X_JsonGetFailed( );
}
int i = 0;
for ( auto const& av : a ) {
if ( !av.is< double >( ) ) {
throw X_JsonGetFailed( );
}
vector[ i ++ ] = float( av.get< double >( ) );
}
}