demotool/utilities.cc

162 lines
3.6 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(
2017-11-03 09:08:19 +01:00
float& initial ,
const float delta
2017-09-30 10:37:45 +02:00
)
{
initial = fmod( initial + delta + 540 , 360 ) - 180;
}
void anglesToMatrix(
2017-11-03 09:08:19 +01:00
float const* angles ,
float* matrix )
2017-09-30 10:37:45 +02:00
{
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
/*----------------------------------------------------------------------------*/
2017-11-03 09:08:19 +01:00
T_String GetAbsolutePath(
T_String const& path )
{
2017-11-03 09:08:19 +01:00
char* const rp( realpath( (char*) path.toOSString( ).data( ) , nullptr ) );
if ( !rp ) {
return {};
}
2017-11-03 09:08:19 +01:00
const T_String rv( rp );
free( rp );
return rv;
}
2017-11-03 09:08:19 +01:00
T_String GetParentPath(
T_String const& path )
{
2017-11-03 09:08:19 +01:00
auto buffer( path.toOSString( ) );
2017-11-03 09:08:19 +01:00
char* const rp( realpath( dirname( (char*) buffer.data( ) ) , nullptr ) );
if ( !rp ) {
return {};
}
2017-11-03 09:08:19 +01:00
const T_String rv( rp );
free( rp );
return rv;
}
2017-10-31 09:22:38 +01:00
/*= JSON =====================================================================*/
template void jsonAdd< double >(
2017-11-03 09:08:19 +01:00
T_JSONObject& object ,
std::string const& key ,
double const& v );
2017-10-31 14:21:42 +01:00
template void jsonAdd< T_JSONObject >(
2017-11-03 09:08:19 +01:00
T_JSONObject& object ,
std::string const& key ,
T_JSONObject const& v );
2017-10-31 14:21:42 +01:00
template void jsonAdd< T_JSONArray >(
2017-11-03 09:08:19 +01:00
T_JSONObject& object ,
std::string const& key ,
T_JSONArray const& v );
2017-10-31 09:22:38 +01:00
template void jsonAdd< std::string >(
2017-11-03 09:08:19 +01:00
T_JSONObject& object ,
std::string const& key ,
std::string const& v );
2017-10-31 09:22:38 +01:00
template< >
void jsonAdd< picojson::value >(
2017-11-03 09:08:19 +01:00
T_JSONObject& object ,
std::string const& key ,
picojson::value const& v )
2017-10-31 09:22:38 +01:00
{
using T_Entry = std::pair< std::string , picojson::value >;
object.insert( T_Entry( key , v ) );
}
template< >
void jsonAdd< int >(
2017-11-03 09:08:19 +01:00
T_JSONObject& object ,
std::string const& key ,
int const& v )
2017-10-31 09:22:38 +01:00
{
using T_Entry = std::pair< std::string , picojson::value >;
object.insert( T_Entry( key , picojson::value( double( v ) ) ) );
}
template< >
void jsonAdd< unsigned >(
2017-11-03 09:08:19 +01:00
T_JSONObject& object ,
std::string const& key ,
unsigned const& v )
2017-10-31 09:22:38 +01:00
{
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(
2017-11-03 09:08:19 +01:00
float* vector ,
const size_t size ,
T_JSONObject const& object ,
char const* key )
2017-10-31 09:22:38 +01:00
{
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 >( ) );
}
}