text:
#include "simInternal.h"
#include "dynMaterialObject.h"
#include "app.h"
#include "tt.h"
#include <boost/format.hpp>

int CDynMaterialObject::_nextUniqueID=0;

CDynMaterialObject::CDynMaterialObject()
{
    _setDefaultParameters();
}

void CDynMaterialObject::_setDefaultParameters()
{
    _objectID=SIM_IDSTART_DYNMATERIAL_old;
    _objectName="usr_";

    _uniqueID=_nextUniqueID++;
    _bulletFloatParams.clear();
    _bulletIntParams.clear();
    _odeFloatParams.clear();
    _odeIntParams.clear();
    _vortexFloatParams.clear();
    _vortexIntParams.clear();
    _newtonFloatParams.clear();
    _newtonIntParams.clear();

    // Bullet parameters:
    // ----------------------------------------------------
    _bulletFloatParams.push_back(0.0f); // simi_bullet_body_restitution
    _bulletFloatParams.push_back(0.71f); // simi_bullet_body_oldfriction
    _bulletFloatParams.push_back(0.71f); // simi_bullet_body_friction
    _bulletFloatParams.push_back(0.0f); // simi_bullet_body_lineardamping
    _bulletFloatParams.push_back(0.0f); // simi_bullet_body_angulardamping
    _bulletFloatParams.push_back(0.1f); // simi_bullet_body_nondefaultcollisionmargingfactor
    _bulletFloatParams.push_back(0.002f); // simi_bullet_body_nondefaultcollisionmargingfactorconvex
    _bulletFloatParams.push_back(0.0f); // free
    // BULLET_SHAPE_FLOAT_PARAM_CNT_CURRENT=8

    int bulletBitCoded=simi_bullet_body_usenondefaultcollisionmarginconvex;
    // above is bit-coded: 1=_bulletStickyContact, 2=_bulletNonDefaultCollisionMargin, 4=_bulletNonDefaultCollisionMargin_forConvexAndNonPureShape, 8=_bulletAutoShrinkConvexMesh
    _bulletIntParams.push_back(bulletBitCoded); // simi_bullet_body_bitcoded
    _bulletIntParams.push_back(0); // free
    // BULLET_SHAPE_INT_PARAM_CNT_CURRENT=2
    // ----------------------------------------------------

    // ODE parameters:
    // ----------------------------------------------------
    _odeFloatParams.push_back(0.71f); // simi_ode_body_friction (box on 26.8deg inclined plane slightly slides (same mat. for box and plane)
    _odeFloatParams.push_back(0.2f); // simi_ode_body_softerp
    _odeFloatParams.push_back(0.0f); // simi_ode_body_softcfm
    _odeFloatParams.push_back(0.0f); // simi_ode_body_lineardamping
    _odeFloatParams.push_back(0.0f); // simi_ode_body_angulardamping
    _odeFloatParams.push_back(0.0f); // free
    // ODE_SHAPE_FLOAT_PARAM_CNT_CURRENT=6

    _odeIntParams.push_back(64); // simi_ode_body_maxcontacts
    int odeBitCoded=0; // not used for now
    _odeIntParams.push_back(odeBitCoded);
    _odeIntParams.push_back(0); // free
    // ODE_SHAPE_INT_PARAM_CNT_CURRENT=3
    // ----------------------------------------------------

    // Vortex parameters:
    // ----------------------------------------------------
    _vortexFloatParams.push_back(0.5f); // simi_vortex_body_primlinearaxisfriction
    _vortexFloatParams.push_back(0.5f); // simi_vortex_body_seclinearaxisfriction
    _vortexFloatParams.push_back(0.0f); // simi_vortex_body_primangularaxisfriction
    _vortexFloatParams.push_back(0.0f); // simi_vortex_body_secangularaxisfriction
    _vortexFloatParams.push_back(0.0f); // simi_vortex_body_normalangularaxisfriction

    _vortexFloatParams.push_back(1.1f); // simi_vortex_body_primlinearaxisstaticfrictionscale
    _vortexFloatParams.push_back(1.1f); // simi_vortex_body_seclinearaxisstaticfrictionscale
    _vortexFloatParams.push_back(1.0f); // simi_vortex_body_primangularaxisstaticfrictionscale
    _vortexFloatParams.push_back(1.0f); // simi_vortex_body_secangularaxisstaticfrictionscale
    _vortexFloatParams.push_back(1.0f); // simi_vortex_body_normalangularaxisstaticfrictionscale

    _vortexFloatParams.push_back(0.00000001f); // simi_vortex_body_compliance
    _vortexFloatParams.push_back(10000000.0f); // simi_vortex_body_damping (was 0.0f)
    _vortexFloatParams.push_back(0.0f); // simi_vortex_body_restitution
    _vortexFloatParams.push_back(0.5f); // simi_vortex_body_restitutionthreshold
    _vortexFloatParams.push_back(0.0f); // simi_vortex_body_adhesiveforce
    _vortexFloatParams.push_back(0.0f); // simi_vortex_body_linearvelocitydamping
    _vortexFloatParams.push_back(0.0f); // simi_vortex_body_angularvelocitydamping

    _vortexFloatParams.push_back(0.0f); // simi_vortex_body_primlinearaxisslide
    _vortexFloatParams.push_back(0.0f); // simi_vortex_body_seclinearaxisslide
    _vortexFloatParams.push_back(0.0f); // simi_vortex_body_primangularaxisslide
    _vortexFloatParams.push_back(0.0f); // simi_vortex_body_secangularaxisslide
    _vortexFloatParams.push_back(0.0f); // simi_vortex_body_normalangularaxisslide

    _vortexFloatParams.push_back(0.0f); // simi_vortex_body_primlinearaxisslip
    _vortexFloatParams.push_back(0.0f); // simi_vortex_body_seclinearaxisslip
    _vortexFloatParams.push_back(0.0f); // simi_vortex_body_primangularaxisslip
    _vortexFloatParams.push_back(0.0f); // simi_vortex_body_secangularaxisslip
    _vortexFloatParams.push_back(0.0f); // simi_vortex_body_normalangularaxisslip

    _vortexFloatParams.push_back(0.14f); // simi_vortex_body_autosleeplinearspeedthreshold
    _vortexFloatParams.push_back(0.045f); // simi_vortex_body_autosleeplinearaccelthreshold
    _vortexFloatParams.push_back(0.03f); // simi_vortex_body_autosleepangularspeedthreshold
    _vortexFloatParams.push_back(0.045f); // simi_vortex_body_autosleepangularaccelthreshold

    _vortexFloatParams.push_back(0.0f); // simi_vortex_body_skinthickness (was 0.002 before)
    _vortexFloatParams.push_back(0.01f); // simi_vortex_body_autoangulardampingtensionratio
    _vortexFloatParams.push_back(0.0f); // simi_vortex_body_primaxisvectorx
    _vortexFloatParams.push_back(0.0f); // simi_vortex_body_primaxisvectory
    _vortexFloatParams.push_back(1.0f); // simi_vortex_body_primaxisvectorz --> by default, primary axis aligned with Z axis (if possible)
    // VORTEX_SHAPE_FLOAT_PARAM_CNT_CURRENT=36

    _vortexIntParams.push_back(sim_vortex_bodyfrictionmodel_scaledboxfast); // simi_vortex_body_primlinearaxisfrictionmodel
    _vortexIntParams.push_back(sim_vortex_bodyfrictionmodel_scaledboxfast); // simi_vortex_body_seclinearaxisfrictionmodel
    _vortexIntParams.push_back(sim_vortex_bodyfrictionmodel_none); // simi_vortex_body_primangulararaxisfrictionmodel
    _vortexIntParams.push_back(sim_vortex_bodyfrictionmodel_none); // simi_vortex_body_secmangulararaxisfrictionmodel
    _vortexIntParams.push_back(sim_vortex_bodyfrictionmodel_none); // simi_vortex_body_normalmangulararaxisfrictionmodel

    _vortexIntParams.push_back(simi_vortex_body_fastmoving+simi_vortex_body_seclinaxissameasprimlinaxis+simi_vortex_body_secangaxissameasprimangaxis+simi_vortex_body_normangaxissameasprimangaxis); // simi_vortex_body_bitcoded
                                                // bit coded: 0set=treat pure shapes as convex shapes, 1set=treat convex shapes as random shapes, 2set=threat random shapes as terrain, 3set=fast moving, 4set=auto-slip,
                                                // 5set=sec. lin. axis uses same values as prim. lin. axis, 6set=sec. ang. axis uses same values as prim. ang. axis, 7set=norm. ang. axis uses same values as prim. ang. axis (bits 5-7 are not taken into account on the plugin side: they just serve as setting memory)
                                                // 8set=auto angular damping is on
    _vortexIntParams.push_back(10); // simi_vortex_body_autosleepsteplivethreshold
    _vortexIntParams.push_back(_uniqueID); // simi_vortex_body_materialuniqueid: the material unique ID. The ID is redefined in each session
    // VORTEX_SHAPE_INT_PARAM_CNT_CURRENT=8
    // ----------------------------------------------------

    // Newton parameters:
    // ----------------------------------------------------
    _newtonFloatParams.push_back(0.5f); // simi_newton_body_staticfriction
    _newtonFloatParams.push_back(0.5f); // simi_newton_body_kineticfriction
    _newtonFloatParams.push_back(0.0f); // simi_newton_body_restitution
    _newtonFloatParams.push_back(0.0f); // simi_newton_body_lineardrag
    _newtonFloatParams.push_back(0.0f); // simi_newton_body_angulardrag
    // NEWTON_SHAPE_FLOAT_PARAM_CNT_CURRENT=5

    int newtonBitCoded=0;
    // newtonBitCoded|=simi_newton_body_fastmoving; // for fast-moving objects
    _newtonIntParams.push_back(newtonBitCoded); // simi_newton_body_bitcoded
    // NEWTON_SHAPE_INT_PARAM_CNT_CURRENT=1
    // ----------------------------------------------------
}

CDynMaterialObject::~CDynMaterialObject()
{
}

void CDynMaterialObject::setObjectID(int newID)
{
    _objectID=newID;
}

int CDynMaterialObject::getObjectID()
{
    return(_objectID);
}

void CDynMaterialObject::setObjectName(const char* newName)
{
    _objectName=newName;
}

std::string CDynMaterialObject::getObjectName()
{
    return(getIndividualName());
}

std::string CDynMaterialObject::getIndividualName()
{
    return("<individual (i.e. not shared)>");
}

float CDynMaterialObject::getEngineFloatParam(int what,bool* ok)
{
    if (ok!=nullptr)
        ok[0]=true;
    if ((what>sim_bullet_body_float_start)&&(what<sim_bullet_body_float_end))
    {
        int w=what-sim_bullet_body_restitution+simi_bullet_body_restitution;
        return(_bulletFloatParams[w]);
    }
    if ((what>sim_ode_body_float_start)&&(what<sim_ode_body_float_end))
    {
        int w=what-sim_ode_body_friction+simi_ode_body_friction;
        return(_odeFloatParams[w]);
    }
    if ((what>sim_vortex_body_float_start)&&(what<sim_vortex_body_float_end))
    {
        int w=what-sim_vortex_body_primlinearaxisfriction+simi_vortex_body_primlinearaxisfriction;
        return(_vortexFloatParams[w]);
    }
    if ((what>sim_newton_body_float_start)&&(what<sim_newton_body_float_end))
    {
        int w=what-sim_newton_body_staticfriction+simi_newton_body_staticfriction;
        return(_newtonFloatParams[w]);
    }
    if (ok!=nullptr)
        ok[0]=false;
    return(0.0);
}

int CDynMaterialObject::getEngineIntParam(int what,bool* ok)
{
    if (ok!=nullptr)
        ok[0]=true;
    if ((what>sim_bullet_body_int_start)&&(what<sim_bullet_body_int_end))
    {
        int w=what-sim_bullet_body_bitcoded+simi_bullet_body_bitcoded;
        return(_bulletIntParams[w]);
    }
    if ((what>sim_ode_body_int_start)&&(what<sim_ode_body_int_end))
    {
        int w=what-sim_ode_body_maxcontacts+simi_ode_body_maxcontacts;
        return(_odeIntParams[w]);
    }
    if ((what>sim_vortex_body_int_start)&&(what<sim_vortex_body_int_end))
    {
        int w=what-sim_vortex_body_primlinearaxisfrictionmodel+simi_vortex_body_primlinearaxisfrictionmodel;
        return(_vortexIntParams[w]);
    }
    if ((what>sim_newton_body_int_start)&&(what<sim_newton_body_int_end))
    {
        int w=what-sim_newton_body_bitcoded+simi_newton_body_bitcoded;
        return(_newtonIntParams[w]);
    }
    if (ok!=nullptr)
        ok[0]=false;
    return(0);
}

bool CDynMaterialObject::getEngineBoolParam(int what,bool* ok)
{
    if (ok!=nullptr)
        ok[0]=true;
    if ((what>sim_bullet_body_bool_start)&&(what<sim_bullet_body_bool_end))
    {
        int b=1;
        int w=(what-sim_bullet_body_sticky);
        while (w>0) {b*=2; w--;}
        return((_bulletIntParams[simi_bullet_body_bitcoded]&b)!=0);
    }
    if ((what>sim_ode_body_bool_start)&&(what<sim_ode_body_bool_end))
    {
        // no bool params for now
        // search for obb11032016
        if (ok!=nullptr)
            ok[0]=false;
        return(0);
    }
    if ((what>sim_vortex_body_bool_start)&&(what<sim_vortex_body_bool_end))
    {
        int b=1;
        int w=(what-sim_vortex_body_pureshapesasconvex);
        while (w>0) {b*=2; w--;}
        return((_vortexIntParams[simi_vortex_body_bitcoded]&b)!=0);
    }
    if ((what>sim_newton_body_bool_start)&&(what<sim_newton_body_bool_end))
    {
        int b=1;
        int w=(what-sim_newton_body_fastmoving);
        while (w>0) {b*=2; w--;}
        return((_newtonIntParams[simi_newton_body_bitcoded]&b)!=0);
    }
    if (ok!=nullptr)
        ok[0]=false;
    return(0);
}

bool CDynMaterialObject::setEngineFloatParam(int what,float v)
{
    if ((what>sim_bullet_body_float_start)&&(what<sim_bullet_body_float_end))
    {
        int w=what-sim_bullet_body_restitution+simi_bullet_body_restitution;
        std::vector<float> fp;
        getBulletFloatParams(fp);
        fp[w]=v;
        setBulletFloatParams(fp);
        return(true);
    }
    if ((what>sim_ode_body_float_start)&&(what<sim_ode_body_float_end))
    {
        int w=what-sim_ode_body_friction+simi_ode_body_friction;
        std::vector<float> fp;
        getOdeFloatParams(fp);
        fp[w]=v;
        setOdeFloatParams(fp);
        return(true);
    }
    if ((what>sim_vortex_body_float_start)&&(what<sim_vortex_body_float_end))
    {
        int w=what-sim_vortex_body_primlinearaxisfriction+simi_vortex_body_primlinearaxisfriction;
        std::vector<float> fp;
        getVortexFloatParams(fp);
        fp[w]=v;
        setVortexFloatParams(fp);
        return(true);
    }
    if ((what>sim_newton_body_float_start)&&(what<sim_newton_body_float_end))
    {
        int w=what-sim_newton_body_staticfriction+simi_newton_body_staticfriction;
        std::vector<float> fp;
        getNewtonFloatParams(fp);
        fp[w]=v;
        setNewtonFloatParams(fp);
        return(true);
    }
    return(false);
}

bool CDynMaterialObject::setEngineIntParam(int what,int v)
{
    if ((what>sim_bullet_body_int_start)&&(what<sim_bullet_body_int_end))
    {
        int w=what-sim_bullet_body_bitcoded+simi_bullet_body_bitcoded;
        std::vector<int> ip;
        getBulletIntParams(ip);
        ip[w]=v;
        setBulletIntParams(ip);
        return(true);
    }
    if ((what>sim_ode_body_int_start)&&(what<sim_ode_body_int_end))
    {
        int w=what-sim_ode_body_maxcontacts+simi_ode_body_maxcontacts;
        std::vector<int> ip;
        getOdeIntParams(ip);
        ip[w]=v;
        setOdeIntParams(ip);
        return(true);
    }
    if ((what>sim_vortex_body_int_start)&&(what<sim_vortex_body_int_end))
    {
        int w=what-sim_vortex_body_primlinearaxisfrictionmodel+simi_vortex_body_primlinearaxisfrictionmodel;
        std::vector<int> ip;
        getVortexIntParams(ip);
        ip[w]=v;
        setVortexIntParams(ip);
        return(true);
    }
    if ((what>sim_newton_body_int_start)&&(what<sim_newton_body_int_end))
    {
        int w=what-sim_newton_body_bitcoded+simi_newton_body_bitcoded;
        std::vector<int> ip;
        getNewtonIntParams(ip);
        ip[w]=v;
        setNewtonIntParams(ip);
        return(true);
    }
    return(false);
}

bool CDynMaterialObject::setEngineBoolParam(int what,bool v)
{
    if ((what>sim_bullet_body_bool_start)&&(what<sim_bullet_body_bool_end))
    {
        int b=1;
        int w=(what-sim_bullet_body_sticky);
        while (w>0) {b*=2; w--;}
        _bulletIntParams[simi_bullet_body_bitcoded]|=b;
        if (!v)
            _bulletIntParams[simi_bullet_body_bitcoded]-=b;
        return(true);
    }
    if ((what>sim_ode_body_bool_start)&&(what<sim_ode_body_bool_end))
    {
        // no bool params yet
        // search for obb11032016
        return(false);
    }
    if ((what>sim_vortex_body_bool_start)&&(what<sim_vortex_body_bool_end))
    {
        int b=1;
        int w=(what-sim_vortex_body_pureshapesasconvex);
        while (w>0) {b*=2; w--;}
        _vortexIntParams[simi_vortex_body_bitcoded]|=b;
        if (!v)
            _vortexIntParams[simi_vortex_body_bitcoded]-=b;
        return(true);
    }
    if ((what>sim_newton_body_bool_start)&&(what<sim_newton_body_bool_end))
    {
        int b=1;
        int w=(what-sim_newton_body_fastmoving);
        while (w>0) {b*=2; w--;}
        _newtonIntParams[simi_newton_body_bitcoded]|=b;
        if (!v)
            _newtonIntParams[simi_newton_body_bitcoded]-=b;
        return(true);
    }
    return(false);
}

void CDynMaterialObject::getBulletFloatParams(std::vector<float>& p)
{
    p.assign(_bulletFloatParams.begin(),_bulletFloatParams.end());
}

void CDynMaterialObject::setBulletFloatParams(const std::vector<float>& p)
{
    for (size_t i=0;i<p.size();i++)
        _bulletFloatParams[i]=p[i];

    _bulletFloatParams[simi_bullet_body_restitution]=tt::getLimitedFloat(0.0f,10.0f,_bulletFloatParams[simi_bullet_body_restitution]); // restitution
    _bulletFloatParams[simi_bullet_body_oldfriction]=tt::getLimitedFloat(0.0f,1000.0f,_bulletFloatParams[simi_bullet_body_oldfriction]); // old friction
    _bulletFloatParams[simi_bullet_body_friction]=tt::getLimitedFloat(0.0f,1000.0f,_bulletFloatParams[simi_bullet_body_friction]); // new friction
    _bulletFloatParams[simi_bullet_body_lineardamping]=tt::getLimitedFloat(0.0f,10.0f,_bulletFloatParams[simi_bullet_body_lineardamping]); // lin. damp.
    _bulletFloatParams[simi_bullet_body_angulardamping]=tt::getLimitedFloat(0.0f,10.0f,_bulletFloatParams[simi_bullet_body_angulardamping]); // ang. damp.
    _bulletFloatParams[simi_bullet_body_nondefaultcollisionmargingfactor]=tt::getLimitedFloat(0.0001f,1000.0f,_bulletFloatParams[simi_bullet_body_nondefaultcollisionmargingfactor]); // _bulletNonDefaultCollisionMarginFactor
    _bulletFloatParams[simi_bullet_body_nondefaultcollisionmargingfactorconvex]=tt::getLimitedFloat(0.0001f,1000.0f,_bulletFloatParams[simi_bullet_body_nondefaultcollisionmargingfactorconvex]); // _bulletNonDefaultCollisionMarginFactor_forConvexAndNonPureShape
}

void CDynMaterialObject::getBulletIntParams(std::vector<int>& p)
{
    p.assign(_bulletIntParams.begin(),_bulletIntParams.end());
}

void CDynMaterialObject::setBulletIntParams(const std::vector<int>& p)
{
    for (size_t i=0;i<p.size();i++)
        _bulletIntParams[i]=p[i];
}

void CDynMaterialObject::getOdeFloatParams(std::vector<float>& p)
{
    p.assign(_odeFloatParams.begin(),_odeFloatParams.end());
}

void CDynMaterialObject::setOdeFloatParams(const std::vector<float>& p)
{
    for (size_t i=0;i<p.size();i++)
        _odeFloatParams[i]=p[i];

    _odeFloatParams[simi_ode_body_friction]=tt::getLimitedFloat(0.0f,1000.0f,_odeFloatParams[simi_ode_body_friction]); // friction
    _odeFloatParams[simi_ode_body_softerp]=tt::getLimitedFloat(0.0f,1.0f,_odeFloatParams[simi_ode_body_softerp]); // soft ERP
    _odeFloatParams[simi_ode_body_softcfm]=tt::getLimitedFloat(0.0f,1000000.0f,_odeFloatParams[simi_ode_body_softcfm]); // soft CFM
    _odeFloatParams[simi_ode_body_lineardamping]=tt::getLimitedFloat(0.0f,10.0f,_odeFloatParams[simi_ode_body_lineardamping]); // lin. damp.
    _odeFloatParams[simi_ode_body_angulardamping]=tt::getLimitedFloat(0.0f,10.0f,_odeFloatParams[simi_ode_body_angulardamping]); // ang. damp.
}

void CDynMaterialObject::getOdeIntParams(std::vector<int>& p)
{
    p.assign(_odeIntParams.begin(),_odeIntParams.end());
}

void CDynMaterialObject::setOdeIntParams(const std::vector<int>& p)
{
    for (size_t i=0;i<p.size();i++)
        _odeIntParams[i]=p[i];
    _odeIntParams[0]=tt::getLimitedInt(-64,64,_odeIntParams[simi_ode_body_maxcontacts]); // max contacts
}

void CDynMaterialObject::getVortexFloatParams(std::vector<float>& p)
{
    p.assign(_vortexFloatParams.begin(),_vortexFloatParams.end());
}

void CDynMaterialObject::setVortexFloatParams(const std::vector<float>& p)
{
    int l=int(p.size());
    if (l>int(_vortexFloatParams.size()))
        l=int(_vortexFloatParams.size()); // should normally never happen!
    for (int i=0;i<l;i++)
        _vortexFloatParams[i]=p[i];

    _vortexFloatParams[simi_vortex_body_skinthickness]=tt::getLimitedFloat(0.0f,10.0f,_vortexFloatParams[simi_vortex_body_skinthickness]); // skin thickness
    _vortexFloatParams[simi_vortex_body_autoangulardampingtensionratio]=tt::getLimitedFloat(0.0f,1000.0f,_vortexFloatParams[simi_vortex_body_autoangulardampingtensionratio]); // auto angular damping tension ratio
    // make sure the vector for the primary axis is normalized:
    C3Vector v(_vortexFloatParams[simi_vortex_body_primaxisvectorx],_vortexFloatParams[simi_vortex_body_primaxisvectory],_vortexFloatParams[simi_vortex_body_primaxisvectorz]);
    if (v.getLength()<0.01f)
        v=C3Vector::unitZVector;
    v.normalize();
    _vortexFloatParams[simi_vortex_body_primaxisvectorx]=v(0);
    _vortexFloatParams[simi_vortex_body_primaxisvectory]=v(1);
    _vortexFloatParams[simi_vortex_body_primaxisvectorz]=v(2);
}

void CDynMaterialObject::getVortexIntParams(std::vector<int>& p)
{
    _vortexIntParams[7]=_uniqueID;
    p.assign(_vortexIntParams.begin(),_vortexIntParams.end());
}

void CDynMaterialObject::setVortexIntParams(const std::vector<int>& p)
{
    int l=int(p.size());
    if (l>int(_vortexIntParams.size()))
        l=int(_vortexIntParams.size()); // should normally never happen!
    for (int i=0;i<l;i++)
        _vortexIntParams[i]=p[i];

    _vortexIntParams[simi_vortex_body_autosleepsteplivethreshold]=tt::getLimitedInt(1,10000,_vortexIntParams[simi_vortex_body_autosleepsteplivethreshold]); // auto-sleep steps
    _vortexIntParams[simi_vortex_body_materialuniqueid]=_uniqueID;
}

void CDynMaterialObject::getNewtonFloatParams(std::vector<float>& p)
{
    p.assign(_newtonFloatParams.begin(),_newtonFloatParams.end());
}

void CDynMaterialObject::setNewtonFloatParams(const std::vector<float>& p)
{
    int l=int(p.size());
    if (l>int(_newtonFloatParams.size()))
        l=int(_newtonFloatParams.size()); // should normally never happen!
    for (int i=0;i<l;i++)
        _newtonFloatParams[i]=p[i];
    _newtonFloatParams[simi_newton_body_staticfriction]=tt::getLimitedFloat(0.0f,2.0f,_newtonFloatParams[simi_newton_body_staticfriction]); // static friction
    _newtonFloatParams[simi_newton_body_kineticfriction]=tt::getLimitedFloat(0.0f,_newtonFloatParams[simi_newton_body_staticfriction],_newtonFloatParams[simi_newton_body_kineticfriction]); // kin. friction
    _newtonFloatParams[simi_newton_body_restitution]=tt::getLimitedFloat(0.0f,1.0f,_newtonFloatParams[simi_newton_body_restitution]); // restitution
    _newtonFloatParams[simi_newton_body_lineardrag]=tt::getLimitedFloat(0.0f,1.0f,_newtonFloatParams[simi_newton_body_lineardrag]); // lin. drag
    _newtonFloatParams[simi_newton_body_angulardrag]=tt::getLimitedFloat(0.0f,1.0f,_newtonFloatParams[simi_newton_body_angulardrag]); // ang. drag
}

void CDynMaterialObject::getNewtonIntParams(std::vector<int>& p)
{
    p.assign(_newtonIntParams.begin(),_newtonIntParams.end());
}

void CDynMaterialObject::setNewtonIntParams(const std::vector<int>& p)
{
    int l=int(p.size());
    if (l>int(_newtonIntParams.size()))
        l=int(_newtonIntParams.size()); // should normally never happen!
    for (int i=0;i<l;i++)
        _newtonIntParams[i]=p[i];
}

std::string CDynMaterialObject::getDefaultMaterialName(int defMatId)
{
    if (defMatId==sim_dynmat_default)
        return("defaultMaterial");
    if (defMatId==sim_dynmat_highfriction)
        return("highFrictionMaterial");
    if (defMatId==sim_dynmat_lowfriction)
        return("lowFrictionMaterial");
    if (defMatId==sim_dynmat_nofriction)
        return("noFrictionMaterial");
    if (defMatId==sim_dynmat_reststackgrasp)
        return("restStackGraspMaterial");
    if (defMatId==sim_dynmat_foot)
        return("footMaterial");
    if (defMatId==sim_dynmat_wheel)
        return("wheelMaterial");
    if (defMatId==sim_dynmat_gripper)
        return("gripperMaterial");
    if (defMatId==sim_dynmat_floor)
        return("floorMaterial");
    return("ERROR");
}
void CDynMaterialObject::generateDefaultMaterial(int defMatId)
{
    _setDefaultParameters();
    if (defMatId==sim_dynmat_default)
        _objectName="defaultMaterial";
    if (defMatId==sim_dynmat_highfriction)
    {
        _objectName="highFrictionMaterial";
        setEngineFloatParam(sim_bullet_body_oldfriction,1.0f);
        setEngineFloatParam(sim_bullet_body_friction,1.0f);
        setEngineFloatParam(sim_ode_body_friction,1.0f);
        setEngineFloatParam(sim_vortex_body_primlinearaxisfriction,1.0f);
        setEngineFloatParam(sim_vortex_body_seclinearaxisfriction,1.0f);
        setEngineFloatParam(sim_newton_body_staticfriction,2.0f);
        setEngineFloatParam(sim_newton_body_kineticfriction,2.0f);
    }
    if (defMatId==sim_dynmat_lowfriction)
    {
        _objectName="lowFrictionMaterial";
        setEngineFloatParam(sim_bullet_body_oldfriction,0.41f);
        setEngineFloatParam(sim_bullet_body_friction,0.20f);
        setEngineFloatParam(sim_ode_body_friction,0.41f);
        setEngineFloatParam(sim_vortex_body_primlinearaxisfriction,0.17f);
        setEngineFloatParam(sim_vortex_body_seclinearaxisfriction,0.17f);
        setEngineFloatParam(sim_newton_body_staticfriction,0.17f);
        setEngineFloatParam(sim_newton_body_kineticfriction,0.17f);
    }
    if (defMatId==sim_dynmat_nofriction)
    {
        _objectName="noFrictionMaterial";
        setEngineFloatParam(sim_bullet_body_oldfriction,0.0f);
        setEngineFloatParam(sim_bullet_body_friction,0.0f);
        setEngineFloatParam(sim_ode_body_friction,0.0f);
        setEngineIntParam(sim_vortex_body_primlinearaxisfrictionmodel,sim_vortex_bodyfrictionmodel_none);
        setEngineIntParam(sim_vortex_body_seclinearaxisfrictionmodel,sim_vortex_bodyfrictionmodel_none);
        setEngineFloatParam(sim_newton_body_staticfriction,0.0f);
        setEngineFloatParam(sim_newton_body_kineticfriction,0.0f);
    }
    if (defMatId==sim_dynmat_reststackgrasp)
    {
        _objectName="restStackGraspMaterial";
        setEngineFloatParam(sim_bullet_body_oldfriction,1.0f);
        setEngineFloatParam(sim_bullet_body_friction,0.5f);
        setEngineFloatParam(sim_ode_body_friction,1.0f);
        setEngineFloatParam(sim_vortex_body_primlinearaxisfriction,1.0f);
        setEngineFloatParam(sim_vortex_body_seclinearaxisfriction,1.0f);
        setEngineFloatParam(sim_vortex_body_primlinearaxisslip,0.0f);
        setEngineFloatParam(sim_vortex_body_seclinearaxisslip,0.0f);
        setEngineFloatParam(sim_vortex_body_skinthickness,0.002f);
        setEngineBoolParam(sim_vortex_body_autoangulardamping,true);
        setEngineFloatParam(sim_newton_body_staticfriction,1.0f);
        setEngineFloatParam(sim_newton_body_kineticfriction,1.0f);
        setEngineFloatParam(sim_newton_body_restitution,0.0f);
    }
    if (defMatId==sim_dynmat_foot)
    {
        _objectName="footMaterial";
        setEngineFloatParam(sim_bullet_body_oldfriction,1.0f);
        setEngineFloatParam(sim_bullet_body_friction,0.5f);
        setEngineFloatParam(sim_ode_body_friction,1.0f);
        setEngineFloatParam(sim_vortex_body_primlinearaxisfriction,1.0f);
        setEngineFloatParam(sim_vortex_body_seclinearaxisfriction,1.0f);
        setEngineFloatParam(sim_vortex_body_primlinearaxisslip,0.0f);
        setEngineFloatParam(sim_vortex_body_seclinearaxisslip,0.0f);
        setEngineFloatParam(sim_vortex_body_skinthickness,0.002f);
        setEngineFloatParam(sim_newton_body_staticfriction,1.0f);
        setEngineFloatParam(sim_newton_body_kineticfriction,1.0f);
        setEngineFloatParam(sim_newton_body_restitution,0.0f);
    }
    if (defMatId==sim_dynmat_wheel)
    {
        _objectName="wheelMaterial";
        setEngineFloatParam(sim_bullet_body_oldfriction,1.0f);
        setEngineFloatParam(sim_bullet_body_friction,1.0f);
        setEngineFloatParam(sim_ode_body_friction,1.0f);
        setEngineFloatParam(sim_vortex_body_primlinearaxisfriction,1.0f);
        setEngineFloatParam(sim_vortex_body_seclinearaxisfriction,1.0f);
        setEngineFloatParam(sim_vortex_body_skinthickness,0.0f);
        setEngineFloatParam(sim_newton_body_staticfriction,1.0f);
        setEngineFloatParam(sim_newton_body_kineticfriction,1.0f);
        setEngineFloatParam(sim_newton_body_restitution,0.0f);
    }
    if (defMatId==sim_dynmat_gripper)
    {
        _objectName="gripperMaterial";
        setEngineFloatParam(sim_bullet_body_oldfriction,1.0f);
        setEngineFloatParam(sim_bullet_body_friction,0.5f);
        setEngineFloatParam(sim_ode_body_friction,1.0f);
        setEngineFloatParam(sim_vortex_body_primlinearaxisfriction,1.0f);
        setEngineFloatParam(sim_vortex_body_seclinearaxisfriction,1.0f);
        setEngineFloatParam(sim_vortex_body_primlinearaxisslip,0.0f);
        setEngineFloatParam(sim_vortex_body_seclinearaxisslip,0.0f);
        setEngineFloatParam(sim_vortex_body_skinthickness,0.002f);
        setEngineFloatParam(sim_newton_body_staticfriction,1.0f);
        setEngineFloatParam(sim_newton_body_kineticfriction,1.0f);
        setEngineFloatParam(sim_newton_body_restitution,0.0f);
    }
    if (defMatId==sim_dynmat_floor)
    {
        _objectName="floorMaterial";
        setEngineFloatParam(sim_bullet_body_oldfriction,1.0f);
        setEngineFloatParam(sim_bullet_body_friction,1.0f);
        setEngineFloatParam(sim_ode_body_friction,1.0f);
        setEngineFloatParam(sim_vortex_body_primlinearaxisfriction,1.0f);
        setEngineFloatParam(sim_vortex_body_seclinearaxisfriction,1.0f);
        setEngineFloatParam(sim_vortex_body_primlinearaxisslip,0.0f);
        setEngineFloatParam(sim_vortex_body_seclinearaxisslip,0.0f);
        setEngineFloatParam(sim_vortex_body_skinthickness,0.002f);
        setEngineFloatParam(sim_newton_body_staticfriction,1.0f);
        setEngineFloatParam(sim_newton_body_kineticfriction,1.0f);
        setEngineFloatParam(sim_newton_body_restitution,0.0f);
    }
/*
    setObjectName("bulletMaterial_sticky_special"); // Bullet sticky contact
    setEngineFloatParam(sim_bullet_body_oldfriction,1.0f);
    setEngineFloatParam(sim_bullet_body_friction,0.25f);
    setEngineBoolParam(sim_bullet_body_sticky,true);

    setObjectName("vortexMaterial_axisXWithoutFriction");
    setEngineFloatParam(sim_vortex_body_primaxisvectorx,1.0f);
    setEngineFloatParam(sim_vortex_body_primaxisvectory,0.0f);
    setEngineFloatParam(sim_vortex_body_primaxisvectorz,0.0f);
    setEngineBoolParam(sim_vortex_body_seclinaxissameasprimlinaxis,false);
    setEngineIntParam(sim_vortex_body_primlinearaxisfrictionmodel,sim_vortex_bodyfrictionmodel_none);

    setObjectName("vortexMaterial_axisYWithoutFriction");
    setEngineFloatParam(sim_vortex_body_primaxisvectorx,0.0f);
    setEngineFloatParam(sim_vortex_body_primaxisvectory,1.0f);
    setEngineFloatParam(sim_vortex_body_primaxisvectorz,0.0f);
    setEngineBoolParam(sim_vortex_body_seclinaxissameasprimlinaxis,false);
    setEngineIntParam(sim_vortex_body_primlinearaxisfrictionmodel,sim_vortex_bodyfrictionmodel_none);

    setObjectName("vortexMaterial_axisZWithoutFriction");
    setEngineFloatParam(sim_vortex_body_primaxisvectorx,0.0f);
    setEngineFloatParam(sim_vortex_body_primaxisvectory,0.0f);
    setEngineFloatParam(sim_vortex_body_primaxisvectorz,1.0f);
    setEngineBoolParam(sim_vortex_body_seclinaxissameasprimlinaxis,false);
    setEngineIntParam(sim_vortex_body_primlinearaxisfrictionmodel,sim_vortex_bodyfrictionmodel_none);

    setObjectName("vortexMaterial_pureShapesAsVxConvexMesh");
    setEngineBoolParam(sim_vortex_body_pureshapesasconvex,true);

    setObjectName("vortexMaterial_convexShapesAsVxTriangleMeshBVTree");
    setEngineBoolParam(sim_vortex_body_convexshapesasrandom,true);

    setObjectName("vortexMaterial_randomShapesAsVxTriangleMeshUVGrid");
    setEngineBoolParam(sim_vortex_body_randomshapesasterrain,true);
*/
}

CDynMaterialObject* CDynMaterialObject::copyYourself()
{
    CDynMaterialObject* newObj=new CDynMaterialObject();
    newObj->_objectID=_objectID;
    newObj->_objectName=_objectName;
    newObj->_bulletFloatParams.assign(_bulletFloatParams.begin(),_bulletFloatParams.end());
    newObj->_bulletIntParams.assign(_bulletIntParams.begin(),_bulletIntParams.end());

    newObj->_odeFloatParams.assign(_odeFloatParams.begin(),_odeFloatParams.end());
    newObj->_odeIntParams.assign(_odeIntParams.begin(),_odeIntParams.end());

    newObj->_vortexFloatParams.assign(_vortexFloatParams.begin(),_vortexFloatParams.end());
    newObj->_vortexIntParams.assign(_vortexIntParams.begin(),_vortexIntParams.end());
    newObj->_vortexIntParams[7]=newObj->_uniqueID;

    newObj->_newtonFloatParams.assign(_newtonFloatParams.begin(),_newtonFloatParams.end());
    newObj->_newtonIntParams.assign(_newtonIntParams.begin(),_newtonIntParams.end());

    return(newObj);
}

void CDynMaterialObject::serialize(CSer& ar)
{
    if (ar.isBinary())
    {
        if (ar.isStoring())
        {       // Storing
            ar.storeDataName("Ipa");
            ar << _objectID;
            ar.flush();

            ar.storeDataName("Gon");
            ar << _objectName;
            ar.flush();

            ar.storeDataName("Bcv");
            unsigned char dummy=0;
            SIM_SET_CLEAR_BIT(dummy,0,true); // for backward compatibility (29/10/2016)
            ar << dummy;
            ar.flush();

            ar.storeDataName("Bul"); // keep for file write backw. compat. (09/03/2016)
            // ar << _bulletRestitution << _bulletFriction << _bulletLinearDamping << _bulletAngularDamping << _bulletNonDefaultCollisionMarginFactor << _bulletNonDefaultCollisionMarginFactor_forConvexAndNonPureShape;
            ar << _bulletFloatParams[simi_bullet_body_restitution] << _bulletFloatParams[simi_bullet_body_oldfriction] << _bulletFloatParams[simi_bullet_body_lineardamping] << _bulletFloatParams[simi_bullet_body_angulardamping] << _bulletFloatParams[simi_bullet_body_nondefaultcollisionmargingfactor] << _bulletFloatParams[simi_bullet_body_nondefaultcollisionmargingfactorconvex];
            ar.flush();

            ar.storeDataName("Od2"); // keep for file write backw. compat. (09/03/2016)
            // ar << _odeMaxContacts << _odeFriction << _odeSoftERP << _odeSoftCFM << _odeLinearDamping << _odeAngularDamping;
            ar << _odeIntParams[simi_ode_body_maxcontacts] << _odeFloatParams[simi_ode_body_friction] << _odeFloatParams[simi_ode_body_softerp] << _odeFloatParams[simi_ode_body_softcfm] << _odeFloatParams[simi_ode_body_lineardamping] << _odeFloatParams[simi_ode_body_angulardamping];
            ar.flush();

            ar.storeDataName("Vo4"); // vortex params:
            ar << int(_vortexFloatParams.size()) << int(_vortexIntParams.size());
            for (int i=0;i<int(_vortexFloatParams.size());i++)
                ar << _vortexFloatParams[i];
            for (int i=0;i<int(_vortexIntParams.size());i++)
                ar << _vortexIntParams[i];
            ar.flush();

            ar.storeDataName("Nw1"); // newton params:
            ar << int(_newtonFloatParams.size()) << int(_newtonIntParams.size());
            for (int i=0;i<int(_newtonFloatParams.size());i++)
                ar << _newtonFloatParams[i];
            for (int i=0;i<int(_newtonIntParams.size());i++)
                ar << _newtonIntParams[i];
            ar.flush();

            ar.storeDataName("Var"); // keep for file write backw. compat. (09/03/2016)
            unsigned char nothing=0;
            if (_bulletIntParams[simi_bullet_body_bitcoded]&simi_bullet_body_usenondefaultcollisionmargin)
                nothing|=1;
            if (_bulletIntParams[simi_bullet_body_bitcoded]&simi_bullet_body_sticky)
                nothing|=2;
            if (_bulletIntParams[simi_bullet_body_bitcoded]&simi_bullet_body_autoshrinkconvex)
                nothing|=4;
            if (_bulletIntParams[simi_bullet_body_bitcoded]&simi_bullet_body_usenondefaultcollisionmarginconvex)
                nothing|=8;
            ar << nothing;
            ar.flush();

            ar.storeDataName("BuN"); // Bullet params, keep after "Bul" and "Var"
            ar << int(_bulletFloatParams.size()) << int(_bulletIntParams.size());
            for (int i=0;i<int(_bulletFloatParams.size());i++)
                ar << _bulletFloatParams[i];
            for (int i=0;i<int(_bulletIntParams.size());i++)
                ar << _bulletIntParams[i];
            ar.flush();

            ar.storeDataName("OdN"); // Ode params, keep after "Od2"
            ar << int(_odeFloatParams.size()) << int(_odeIntParams.size());
            for (int i=0;i<int(_odeFloatParams.size());i++)
                ar << _odeFloatParams[i];
            for (int i=0;i<int(_odeIntParams.size());i++)
                ar << _odeIntParams[i];
            ar.flush();

            ar.storeDataName(SER_END_OF_OBJECT);
        }
        else
        {       // Loading
            int byteQuantity;
            std::string theName="";
            bool vortexDataLoaded=false;
            bool newtonDataLoaded=false;
            while (theName.compare(SER_END_OF_OBJECT)!=0)
            {
                theName=ar.readDataName();
                if (theName.compare(SER_END_OF_OBJECT)!=0)
                {
                    bool noHit=true;
                    if (theName.compare("Ipa")==0)
                    {
                        noHit=false;
                        ar >> byteQuantity;
                        ar >> _objectID;
                    }
                    if (theName.compare("Gon")==0)
                    {
                        noHit=false;
                        ar >> byteQuantity;
                        ar >> _objectName;
                    }
                    if (theName.compare("Bcv")==0)
                    {
                        noHit=false;
                        ar >> byteQuantity;
                        unsigned char dummy;
                        ar >> dummy;
                        // _isIndividualTag=SIM_IS_BIT_SET(dummy,0);
                    }
                    if (theName.compare("Bul")==0)
                    { // keep for backw. compat. (09/03/2016)
                        noHit=false;
                        ar >> byteQuantity;
                        // ar >> _bulletRestitution >> _bulletFriction >> _bulletLinearDamping >> _bulletAngularDamping >> _bulletNonDefaultCollisionMarginFactor >> _bulletNonDefaultCollisionMarginFactor_forConvexAndNonPureShape;
                        float fric;
                        ar >> _bulletFloatParams[simi_bullet_body_restitution] >> fric >> _bulletFloatParams[simi_bullet_body_lineardamping] >> _bulletFloatParams[simi_bullet_body_angulardamping] >> _bulletFloatParams[simi_bullet_body_nondefaultcollisionmargingfactor] >> _bulletFloatParams[simi_bullet_body_nondefaultcollisionmargingfactorconvex];
                        _bulletFloatParams[simi_bullet_body_oldfriction]=fric;
                        _bulletFloatParams[simi_bullet_body_friction]=tt::getLimitedFloat(0.0f,1.0f,fric); // sticky contacts have disappeared for the new Bullet, now everything is "sticky", so make sure it is not too sticky!
                    }
                    if (theName.compare("Ode")==0)
                    { // for backward compatibility (13/8/2015)
                        noHit=false;
                        ar >> byteQuantity;
                        // ar >> _odeMaxContacts >> _odeFriction >> _odeSoftERP >> _odeSoftCFM >> _odeLinearDamping >> _odeAngularDamping;
                        ar >> _odeIntParams[simi_ode_body_maxcontacts] >> _odeFloatParams[simi_ode_body_friction] >> _odeFloatParams[simi_ode_body_softerp] >> _odeFloatParams[simi_ode_body_softcfm] >> _odeFloatParams[simi_ode_body_lineardamping] >> _odeFloatParams[simi_ode_body_angulardamping];
                        _odeIntParams[0]=64;
                    }
                    if (theName.compare("Od2")==0)
                    { // keep for backw. compat. (09/03/2016)
                        noHit=false;
                        ar >> byteQuantity;
                        // ar >> _odeMaxContacts >> _odeFriction >> _odeSoftERP >> _odeSoftCFM >> _odeLinearDamping >> _odeAngularDamping;
                        ar >> _odeIntParams[0] >> _odeFloatParams[0] >> _odeFloatParams[1] >> _odeFloatParams[2] >> _odeFloatParams[3] >> _odeFloatParams[4];
                    }
                    if (theName.compare("BuN")==0)
                    { // Bullet params:
                        noHit=false;
                        ar >> byteQuantity;
                        int cnt1,cnt2;
                        ar >> cnt1 >> cnt2;

                        int cnt1_b=std::min<int>(int(_bulletFloatParams.size()),cnt1);
                        int cnt2_b=std::min<int>(int(_bulletIntParams.size()),cnt2);

                        float vf;
                        int vi;
                        for (int i=0;i<cnt1_b;i++)
                        { // new versions will always have same or more items in _bulletFloatParams already!
                            ar >> vf;
                            _bulletFloatParams[i]=vf;
                        }
                        for (int i=0;i<cnt1-cnt1_b;i++)
                        { // this serialization version is newer than what we know. Discard the unrecognized data:
                            ar >> vf;
                        }
                        for (int i=0;i<cnt2_b;i++)
                        { // new versions will always have same or more items in _bulletIntParams already!
                            ar >> vi;
                            _bulletIntParams[i]=vi;
                        }
                        for (int i=0;i<cnt2-cnt2_b;i++)
                        { // this serialization version is newer than what we know. Discard the unrecognized data:
                            ar >> vi;
                        }
                    }
                    if (theName.compare("OdN")==0)
                    { // Ode params:
                        noHit=false;
                        ar >> byteQuantity;
                        int cnt1,cnt2;
                        ar >> cnt1 >> cnt2;

                        int cnt1_b=std::min<int>(int(_odeFloatParams.size()),cnt1);
                        int cnt2_b=std::min<int>(int(_odeIntParams.size()),cnt2);

                        float vf;
                        int vi;
                        for (int i=0;i<cnt1_b;i++)
                        { // new versions will always have same or more items in _odeFloatParams already!
                            ar >> vf;
                            _odeFloatParams[i]=vf;
                        }
                        for (int i=0;i<cnt1-cnt1_b;i++)
                        { // this serialization version is newer than what we know. Discard the unrecognized data:
                            ar >> vf;
                        }
                        for (int i=0;i<cnt2_b;i++)
                        { // new versions will always have same or more items in _odeIntParams already!
                            ar >> vi;
                            _odeIntParams[i]=vi;
                        }
                        for (int i=0;i<cnt2-cnt2_b;i++)
                        { // this serialization version is newer than what we know. Discard the unrecognized data:
                            ar >> vi;
                        }
                    }
                    if (theName.compare("Vo4")==0)
                    { // vortex params:
                        noHit=false;
                        ar >> byteQuantity;
                        int cnt1,cnt2;
                        ar >> cnt1 >> cnt2;

                        int cnt1_b=std::min<int>(int(_vortexFloatParams.size()),cnt1);
                        int cnt2_b=std::min<int>(int(_vortexIntParams.size()),cnt2);

                        float vf;
                        int vi;
                        for (int i=0;i<cnt1_b;i++)
                        { // new versions will always have same or more items in _vortexFloatParams already!
                            ar >> vf;
                            _vortexFloatParams[i]=vf;
                        }
                        for (int i=0;i<cnt1-cnt1_b;i++)
                        { // this serialization version is newer than what we know. Discard the unrecognized data:
                            ar >> vf;
                        }
                        for (int i=0;i<cnt2_b;i++)
                        { // new versions will always have same or more items in _vortexIntParams already!
                            ar >> vi;
                            _vortexIntParams[i]=vi;
                        }
                        for (int i=0;i<cnt2-cnt2_b;i++)
                        { // this serialization version is newer than what we know. Discard the unrecognized data:
                            ar >> vi;
                            // was there by mistake until 5/8/2015: _vortexIntParams[i]=vi;
                        }
                        vortexDataLoaded=true;
                    }
                    if (theName.compare("Nw1")==0)
                    { // newton params:
                        noHit=false;
                        ar >> byteQuantity;
                        int cnt1,cnt2;
                        ar >> cnt1 >> cnt2;

                        int cnt1_b=std::min<int>(int(_newtonFloatParams.size()),cnt1);
                        int cnt2_b=std::min<int>(int(_newtonIntParams.size()),cnt2);

                        float vf;
                        int vi;
                        for (int i=0;i<cnt1_b;i++)
                        { // new versions will always have same or more items in _newtonFloatParams already!
                            ar >> vf;
                            _newtonFloatParams[i]=vf;
                        }
                        for (int i=0;i<cnt1-cnt1_b;i++)
                        { // this serialization version is newer than what we know. Discard the unrecognized data:
                            ar >> vf;
                        }
                        for (int i=0;i<cnt2_b;i++)
                        { // new versions will always have same or more items in _newtonIntParams already!
                            ar >> vi;
                            _newtonIntParams[i]=vi;
                        }
                        for (int i=0;i<cnt2-cnt2_b;i++)
                        { // this serialization version is newer than what we know. Discard the unrecognized data:
                            ar >> vi;
                        }
                        newtonDataLoaded=true;
                    }
                    if (theName=="Var")
                    { // keep for backw. compat. (09/03/2016)
                        noHit=false;
                        ar >> byteQuantity;
                        unsigned char nothing;
                        ar >> nothing;

                        if (nothing&1)
                            _bulletIntParams[simi_bullet_body_bitcoded]|=simi_bullet_body_usenondefaultcollisionmargin;
                        if (nothing&2)
                            _bulletIntParams[simi_bullet_body_bitcoded]|=simi_bullet_body_sticky;
                        if (nothing&4)
                            _bulletIntParams[simi_bullet_body_bitcoded]|=simi_bullet_body_autoshrinkconvex;
                        if (nothing&8)
                            _bulletIntParams[simi_bullet_body_bitcoded]|=simi_bullet_body_usenondefaultcollisionmarginconvex;
                        // Following is done after everything was loaded:
                        //  if (_bulletIntParams[simi_bullet_body_bitcoded]&simi_bullet_body_sticky)
                        //  _bulletFloatParams[simi_bullet_body_friction]=0.25f; // sticky contacts have disappeared for the new Bullet, we need to adjust for that
                    }
                    if (noHit)
                        ar.loadUnknownData();
                }
            }
            if (!vortexDataLoaded)
            { // keep for backward compatibility (16/10/2013)
                // Try to guess the friction we would need for Vortex:
                float averageFriction=0.0f;
                if (_bulletFloatParams[simi_bullet_body_oldfriction]>1.0f)
                    averageFriction+=1.0f;
                else
                    averageFriction+=_bulletFloatParams[simi_bullet_body_oldfriction];
                if (_odeFloatParams[simi_ode_body_friction]>1.0f)
                    averageFriction+=1.0f;
                else
                    averageFriction+=_odeFloatParams[simi_ode_body_friction];
                averageFriction*=0.5f;
                if (averageFriction<0.01f)
                {
                    _vortexIntParams[simi_vortex_body_primlinearaxisfrictionmodel]=sim_vortex_bodyfrictionmodel_none;
                    _vortexIntParams[simi_vortex_body_seclinearaxisfrictionmodel]=sim_vortex_bodyfrictionmodel_none;
                }
                else
                {
                    _vortexFloatParams[simi_vortex_body_primlinearaxisfriction]=averageFriction;
                    _vortexFloatParams[simi_vortex_body_seclinearaxisfriction]=averageFriction;
                }
            }
            if (!newtonDataLoaded)
            { // keep for backward compatibility (5/8/2015)
                // Try to guess the friction we would need for Newton:
                float averageFriction=0.0f;
                if (_bulletFloatParams[simi_bullet_body_oldfriction]>1.0f)
                    averageFriction+=1.0f;
                else
                    averageFriction+=_bulletFloatParams[simi_bullet_body_oldfriction];
                if (_odeFloatParams[simi_ode_body_friction]>1.0f)
                    averageFriction+=1.0f;
                else
                    averageFriction+=_odeFloatParams[simi_ode_body_friction];
                averageFriction*=0.5f;
                _newtonFloatParams[simi_newton_body_staticfriction]=averageFriction;
                _newtonFloatParams[simi_newton_body_kineticfriction]=averageFriction;
            }
        }
    }
    else
    {
        bool exhaustiveXml=( (ar.getFileType()!=CSer::filetype_csim_xml_simplescene_file)&&(ar.getFileType()!=CSer::filetype_csim_xml_simplemodel_file) );
        if (ar.isStoring())
        {
            if (exhaustiveXml)
            {
                ar.xmlAddNode_int("id",_objectID);
                ar.xmlAddNode_string("name",_objectName.c_str());
            }

            ar.xmlPushNewNode("engines");
            ar.xmlPushNewNode("bullet");
            ar.xmlAddNode_float("restitution",getEngineFloatParam(sim_bullet_body_restitution,nullptr));
            ar.xmlAddNode_float("friction",getEngineFloatParam(sim_bullet_body_friction,nullptr));
            ar.xmlAddNode_float("oldfriction",getEngineFloatParam(sim_bullet_body_oldfriction,nullptr));
            ar.xmlAddNode_float("lineardamping",getEngineFloatParam(sim_bullet_body_lineardamping,nullptr));
            ar.xmlAddNode_float("angulardamping",getEngineFloatParam(sim_bullet_body_angulardamping,nullptr));
            ar.xmlAddNode_float("nondefaultcollisionmargingfactor",getEngineFloatParam(sim_bullet_body_nondefaultcollisionmargingfactor,nullptr));
            ar.xmlAddNode_float("nondefaultcollisionmargingfactorconvex",getEngineFloatParam(sim_bullet_body_nondefaultcollisionmargingfactorconvex,nullptr));

            ar.xmlAddNode_bool("sticky",getEngineBoolParam(sim_bullet_body_sticky,nullptr));
            ar.xmlAddNode_bool("usenondefaultcollisionmargin",getEngineBoolParam(sim_bullet_body_usenondefaultcollisionmargin,nullptr));
            ar.xmlAddNode_bool("usenondefaultcollisionmarginconvex",getEngineBoolParam(sim_bullet_body_usenondefaultcollisionmarginconvex,nullptr));
            ar.xmlAddNode_bool("autoshrinkconvex",getEngineBoolParam(sim_bullet_body_autoshrinkconvex,nullptr));
            ar.xmlPopNode();

            ar.xmlPushNewNode("ode");
            ar.xmlAddNode_float("friction",getEngineFloatParam(sim_ode_body_friction,nullptr));
            ar.xmlAddNode_float("softerp",getEngineFloatParam(sim_ode_body_softerp,nullptr));
            ar.xmlAddNode_float("softcfm",getEngineFloatParam(sim_ode_body_softcfm,nullptr));
            ar.xmlAddNode_float("lineardamping",getEngineFloatParam(sim_ode_body_lineardamping,nullptr));
            ar.xmlAddNode_float("angulardamping",getEngineFloatParam(sim_ode_body_angulardamping,nullptr));

            ar.xmlAddNode_int("maxcontacts",getEngineIntParam(sim_ode_body_maxcontacts,nullptr));
            ar.xmlPopNode();

            ar.xmlPushNewNode("vortex");
            ar.xmlAddNode_float("primlinearaxisfriction",getEngineFloatParam(sim_vortex_body_primlinearaxisfriction,nullptr));
            ar.xmlAddNode_float("seclinearaxisfriction",getEngineFloatParam(sim_vortex_body_seclinearaxisfriction,nullptr));
            ar.xmlAddNode_float("primangularaxisfriction",getEngineFloatParam(sim_vortex_body_primangularaxisfriction,nullptr));
            ar.xmlAddNode_float("secangularaxisfriction",getEngineFloatParam(sim_vortex_body_secangularaxisfriction,nullptr));
            ar.xmlAddNode_float("normalangularaxisfriction",getEngineFloatParam(sim_vortex_body_normalangularaxisfriction,nullptr));
            ar.xmlAddNode_float("primlinearaxisstaticfrictionscale",getEngineFloatParam(sim_vortex_body_primlinearaxisstaticfrictionscale,nullptr));
            ar.xmlAddNode_float("seclinearaxisstaticfrictionscale",getEngineFloatParam(sim_vortex_body_seclinearaxisstaticfrictionscale,nullptr));
            ar.xmlAddNode_float("primangularaxisstaticfrictionscale",getEngineFloatParam(sim_vortex_body_primangularaxisstaticfrictionscale,nullptr));
            ar.xmlAddNode_float("secangularaxisstaticfrictionscale",getEngineFloatParam(sim_vortex_body_secangularaxisstaticfrictionscale,nullptr));
            ar.xmlAddNode_float("normalangularaxisstaticfrictionscale",getEngineFloatParam(sim_vortex_body_normalangularaxisstaticfrictionscale,nullptr));
            ar.xmlAddNode_float("compliance",getEngineFloatParam(sim_vortex_body_compliance,nullptr));
            ar.xmlAddNode_float("damping",getEngineFloatParam(sim_vortex_body_damping,nullptr));
            ar.xmlAddNode_float("restitution",getEngineFloatParam(sim_vortex_body_restitution,nullptr));
            ar.xmlAddNode_float("restitutionthreshold",getEngineFloatParam(sim_vortex_body_restitutionthreshold,nullptr));
            ar.xmlAddNode_float("adhesiveforce",getEngineFloatParam(sim_vortex_body_adhesiveforce,nullptr));
            ar.xmlAddNode_float("linearvelocitydamping",getEngineFloatParam(sim_vortex_body_linearvelocitydamping,nullptr));
            ar.xmlAddNode_float("angularvelocitydamping",getEngineFloatParam(sim_vortex_body_angularvelocitydamping,nullptr));
            ar.xmlAddNode_float("primlinearaxisslide",getEngineFloatParam(sim_vortex_body_primlinearaxisslide,nullptr));
            ar.xmlAddNode_float("seclinearaxisslide",getEngineFloatParam(sim_vortex_body_seclinearaxisslide,nullptr));
            ar.xmlAddNode_float("primangularaxisslide",getEngineFloatParam(sim_vortex_body_primangularaxisslide,nullptr));
            ar.xmlAddNode_float("secangularaxisslide",getEngineFloatParam(sim_vortex_body_secangularaxisslide,nullptr));
            ar.xmlAddNode_float("normalangularaxisslide",getEngineFloatParam(sim_vortex_body_normalangularaxisslide,nullptr));
            ar.xmlAddNode_float("primlinearaxisslip",getEngineFloatParam(sim_vortex_body_primlinearaxisslip,nullptr));
            ar.xmlAddNode_float("seclinearaxisslip",getEngineFloatParam(sim_vortex_body_seclinearaxisslip,nullptr));
            ar.xmlAddNode_float("primangularaxisslip",getEngineFloatParam(sim_vortex_body_primangularaxisslip,nullptr));
            ar.xmlAddNode_float("secangularaxisslip",getEngineFloatParam(sim_vortex_body_secangularaxisslip,nullptr));
            ar.xmlAddNode_float("normalangularaxisslip",getEngineFloatParam(sim_vortex_body_normalangularaxisslip,nullptr));
            ar.xmlAddNode_float("autosleeplinearspeedthreshold",getEngineFloatParam(sim_vortex_body_autosleeplinearspeedthreshold,nullptr));
            ar.xmlAddNode_float("autosleeplinearaccelthreshold",getEngineFloatParam(sim_vortex_body_autosleeplinearaccelthreshold,nullptr));
            ar.xmlAddNode_float("autosleepangularspeedthreshold",getEngineFloatParam(sim_vortex_body_autosleepangularspeedthreshold,nullptr));
            ar.xmlAddNode_float("autosleepangularaccelthreshold",getEngineFloatParam(sim_vortex_body_autosleepangularaccelthreshold,nullptr));
            ar.xmlAddNode_float("skinthickness",getEngineFloatParam(sim_vortex_body_skinthickness,nullptr));
            ar.xmlAddNode_float("autoangulardampingtensionratio",getEngineFloatParam(sim_vortex_body_autoangulardampingtensionratio,nullptr));
            ar.xmlAddNode_float("primaxisvectorx",getEngineFloatParam(sim_vortex_body_primaxisvectorx,nullptr));
            ar.xmlAddNode_float("primaxisvectory",getEngineFloatParam(sim_vortex_body_primaxisvectory,nullptr));
            ar.xmlAddNode_float("primaxisvectorz",getEngineFloatParam(sim_vortex_body_primaxisvectorz,nullptr));

            ar.xmlAddNode_int("primlinearaxisfrictionmodel",getEngineIntParam(sim_vortex_body_primlinearaxisfrictionmodel,nullptr));
            ar.xmlAddNode_int("seclinearaxisfrictionmodel",getEngineIntParam(sim_vortex_body_seclinearaxisfrictionmodel,nullptr));
            ar.xmlAddNode_int("primangulararaxisfrictionmodel",getEngineIntParam(sim_vortex_body_primangulararaxisfrictionmodel,nullptr));
            ar.xmlAddNode_int("secmangulararaxisfrictionmodel",getEngineIntParam(sim_vortex_body_secmangulararaxisfrictionmodel,nullptr));
            ar.xmlAddNode_int("normalmangulararaxisfrictionmodel",getEngineIntParam(sim_vortex_body_normalmangulararaxisfrictionmodel,nullptr));
            ar.xmlAddNode_int("autosleepsteplivethreshold",getEngineIntParam(sim_vortex_body_autosleepsteplivethreshold,nullptr));
            ar.xmlAddNode_int("materialuniqueid",getEngineIntParam(sim_vortex_body_materialuniqueid,nullptr));

            ar.xmlAddNode_bool("pureshapesasconvex",getEngineBoolParam(sim_vortex_body_pureshapesasconvex,nullptr));
            ar.xmlAddNode_bool("convexshapesasrandom",getEngineBoolParam(sim_vortex_body_convexshapesasrandom,nullptr));
            ar.xmlAddNode_bool("randomshapesasterrain",getEngineBoolParam(sim_vortex_body_randomshapesasterrain,nullptr));
            ar.xmlAddNode_bool("fastmoving",getEngineBoolParam(sim_vortex_body_fastmoving,nullptr));
            ar.xmlAddNode_bool("autoslip",getEngineBoolParam(sim_vortex_body_autoslip,nullptr));
            ar.xmlAddNode_bool("seclinaxissameasprimlinaxis",getEngineBoolParam(sim_vortex_body_seclinaxissameasprimlinaxis,nullptr));
            ar.xmlAddNode_bool("secangaxissameasprimangaxis",getEngineBoolParam(sim_vortex_body_secangaxissameasprimangaxis,nullptr));
            ar.xmlAddNode_bool("normangaxissameasprimangaxis",getEngineBoolParam(sim_vortex_body_normangaxissameasprimangaxis,nullptr));
            ar.xmlAddNode_bool("autoangulardamping",getEngineBoolParam(sim_vortex_body_autoangulardamping,nullptr));
            ar.xmlPopNode();

            ar.xmlPushNewNode("newton");
            ar.xmlAddNode_float("staticfriction",getEngineFloatParam(sim_newton_body_staticfriction,nullptr));
            ar.xmlAddNode_float("kineticfriction",getEngineFloatParam(sim_newton_body_kineticfriction,nullptr));
            ar.xmlAddNode_float("restitution",getEngineFloatParam(sim_newton_body_restitution,nullptr));
            ar.xmlAddNode_float("lineardrag",getEngineFloatParam(sim_newton_body_lineardrag,nullptr));
            ar.xmlAddNode_float("angulardrag",getEngineFloatParam(sim_newton_body_angulardrag,nullptr));

            ar.xmlAddNode_bool("fastmoving",getEngineBoolParam(sim_newton_body_fastmoving,nullptr));
            ar.xmlPopNode();

            ar.xmlPopNode();
        }
        else
        {
            if (exhaustiveXml)
            {
                ar.xmlGetNode_int("id",_objectID);
                ar.xmlGetNode_string("name",_objectName);
            }

            float v;
            int vi;
            bool vb;
            if (ar.xmlPushChildNode("engines",exhaustiveXml))
            {
                if (ar.xmlPushChildNode("bullet",exhaustiveXml))
                {
                    if (ar.xmlGetNode_float("restitution",v,exhaustiveXml)) setEngineFloatParam(sim_bullet_body_restitution,v);
                    if (ar.xmlGetNode_float("friction",v,exhaustiveXml)) setEngineFloatParam(sim_bullet_body_friction,v);
                    if (ar.xmlGetNode_float("oldfriction",v,exhaustiveXml)) setEngineFloatParam(sim_bullet_body_oldfriction,v);
                    if (ar.xmlGetNode_float("lineardamping",v,exhaustiveXml)) setEngineFloatParam(sim_bullet_body_lineardamping,v);
                    if (ar.xmlGetNode_float("angulardamping",v,exhaustiveXml)) setEngineFloatParam(sim_bullet_body_angulardamping,v);
                    if (ar.xmlGetNode_float("nondefaultcollisionmargingfactor",v,exhaustiveXml)) setEngineFloatParam(sim_bullet_body_nondefaultcollisionmargingfactor,v);
                    if (ar.xmlGetNode_float("nondefaultcollisionmargingfactorconvex",v,exhaustiveXml)) setEngineFloatParam(sim_bullet_body_nondefaultcollisionmargingfactorconvex,v);

                    if (ar.xmlGetNode_bool("sticky",vb,exhaustiveXml)) setEngineBoolParam(sim_bullet_body_sticky,vb);
                    if (ar.xmlGetNode_bool("usenondefaultcollisionmargin",vb,exhaustiveXml)) setEngineBoolParam(sim_bullet_body_usenondefaultcollisionmargin,vb);
                    if (ar.xmlGetNode_bool("usenondefaultcollisionmarginconvex",vb,exhaustiveXml)) setEngineBoolParam(sim_bullet_body_usenondefaultcollisionmarginconvex,vb);
                    if (ar.xmlGetNode_bool("autoshrinkconvex",vb,exhaustiveXml)) setEngineBoolParam(sim_bullet_body_autoshrinkconvex,vb);
                    ar.xmlPopNode();
                }

                if (ar.xmlPushChildNode("ode",exhaustiveXml))
                {
                    if (ar.xmlGetNode_float("friction",v,exhaustiveXml)) setEngineFloatParam(sim_ode_body_friction,v);
                    if (ar.xmlGetNode_float("softerp",v,exhaustiveXml)) setEngineFloatParam(sim_ode_body_softerp,v);
                    if (ar.xmlGetNode_float("softcfm",v,exhaustiveXml)) setEngineFloatParam(sim_ode_body_softcfm,v);
                    if (ar.xmlGetNode_float("lineardamping",v,exhaustiveXml)) setEngineFloatParam(sim_ode_body_lineardamping,v);
                    if (ar.xmlGetNode_float("angulardamping",v,exhaustiveXml)) setEngineFloatParam(sim_ode_body_angulardamping,v);

                    if (ar.xmlGetNode_int("maxcontacts",vi,exhaustiveXml)) setEngineIntParam(sim_ode_body_maxcontacts,vi);
                    ar.xmlPopNode();
                }

                if (ar.xmlPushChildNode("vortex",exhaustiveXml))
                {
                    if (ar.xmlGetNode_float("primlinearaxisfriction",v,exhaustiveXml)) setEngineFloatParam(sim_vortex_body_primlinearaxisfriction,v);
                    if (ar.xmlGetNode_float("seclinearaxisfriction",v,exhaustiveXml)) setEngineFloatParam(sim_vortex_body_seclinearaxisfriction,v);
                    if (ar.xmlGetNode_float("primangularaxisfriction",v,exhaustiveXml)) setEngineFloatParam(sim_vortex_body_primangularaxisfriction,v);
                    if (ar.xmlGetNode_float("secangularaxisfriction",v,exhaustiveXml)) setEngineFloatParam(sim_vortex_body_secangularaxisfriction,v);
                    if (ar.xmlGetNode_float("normalangularaxisfriction",v,exhaustiveXml)) setEngineFloatParam(sim_vortex_body_normalangularaxisfriction,v);
                    if (ar.xmlGetNode_float("primlinearaxisstaticfrictionscale",v,exhaustiveXml)) setEngineFloatParam(sim_vortex_body_primlinearaxisstaticfrictionscale,v);
                    if (ar.xmlGetNode_float("seclinearaxisstaticfrictionscale",v,exhaustiveXml)) setEngineFloatParam(sim_vortex_body_seclinearaxisstaticfrictionscale,v);
                    if (ar.xmlGetNode_float("primangularaxisstaticfrictionscale",v,exhaustiveXml)) setEngineFloatParam(sim_vortex_body_primangularaxisstaticfrictionscale,v);
                    if (ar.xmlGetNode_float("secangularaxisstaticfrictionscale",v,exhaustiveXml)) setEngineFloatParam(sim_vortex_body_secangularaxisstaticfrictionscale,v);
                    if (ar.xmlGetNode_float("normalangularaxisstaticfrictionscale",v,exhaustiveXml)) setEngineFloatParam(sim_vortex_body_normalangularaxisstaticfrictionscale,v);
                    if (ar.xmlGetNode_float("compliance",v,exhaustiveXml)) setEngineFloatParam(sim_vortex_body_compliance,v);
                    if (ar.xmlGetNode_float("damping",v,exhaustiveXml)) setEngineFloatParam(sim_vortex_body_damping,v);
                    if (ar.xmlGetNode_float("restitution",v,exhaustiveXml)) setEngineFloatParam(sim_vortex_body_restitution,v);
                    if (ar.xmlGetNode_float("restitutionthreshold",v,exhaustiveXml)) setEngineFloatParam(sim_vortex_body_restitutionthreshold,v);
                    if (ar.xmlGetNode_float("adhesiveforce",v,exhaustiveXml)) setEngineFloatParam(sim_vortex_body_adhesiveforce,v);
                    if (ar.xmlGetNode_float("linearvelocitydamping",v,exhaustiveXml)) setEngineFloatParam(sim_vortex_body_linearvelocitydamping,v);
                    if (ar.xmlGetNode_float("angularvelocitydamping",v,exhaustiveXml)) setEngineFloatParam(sim_vortex_body_angularvelocitydamping,v);
                    if (ar.xmlGetNode_float("primlinearaxisslide",v,exhaustiveXml)) setEngineFloatParam(sim_vortex_body_primlinearaxisslide,v);
                    if (ar.xmlGetNode_float("seclinearaxisslide",v,exhaustiveXml)) setEngineFloatParam(sim_vortex_body_seclinearaxisslide,v);
                    if (ar.xmlGetNode_float("primangularaxisslide",v,exhaustiveXml)) setEngineFloatParam(sim_vortex_body_primangularaxisslide,v);
                    if (ar.xmlGetNode_float("secangularaxisslide",v,exhaustiveXml)) setEngineFloatParam(sim_vortex_body_secangularaxisslide,v);
                    if (ar.xmlGetNode_float("normalangularaxisslide",v,exhaustiveXml)) setEngineFloatParam(sim_vortex_body_normalangularaxisslide,v);
                    if (ar.xmlGetNode_float("primlinearaxisslip",v,exhaustiveXml)) setEngineFloatParam(sim_vortex_body_primlinearaxisslip,v);
                    if (ar.xmlGetNode_float("seclinearaxisslip",v,exhaustiveXml)) setEngineFloatParam(sim_vortex_body_seclinearaxisslip,v);
                    if (ar.xmlGetNode_float("primangularaxisslip",v,exhaustiveXml)) setEngineFloatParam(sim_vortex_body_primangularaxisslip,v);
                    if (ar.xmlGetNode_float("secangularaxisslip",v,exhaustiveXml)) setEngineFloatParam(sim_vortex_body_secangularaxisslip,v);
                    if (ar.xmlGetNode_float("normalangularaxisslip",v,exhaustiveXml)) setEngineFloatParam(sim_vortex_body_normalangularaxisslip,v);
                    if (ar.xmlGetNode_float("autosleeplinearspeedthreshold",v,exhaustiveXml)) setEngineFloatParam(sim_vortex_body_autosleeplinearspeedthreshold,v);
                    if (ar.xmlGetNode_float("autosleeplinearaccelthreshold",v,exhaustiveXml)) setEngineFloatParam(sim_vortex_body_autosleeplinearaccelthreshold,v);
                    if (ar.xmlGetNode_float("autosleepangularspeedthreshold",v,exhaustiveXml)) setEngineFloatParam(sim_vortex_body_autosleepangularspeedthreshold,v);
                    if (ar.xmlGetNode_float("autosleepangularaccelthreshold",v,exhaustiveXml)) setEngineFloatParam(sim_vortex_body_autosleepangularaccelthreshold,v);
                    if (ar.xmlGetNode_float("skinthickness",v,exhaustiveXml)) setEngineFloatParam(sim_vortex_body_skinthickness,v);
                    if (ar.xmlGetNode_float("autoangulardampingtensionratio",v,exhaustiveXml)) setEngineFloatParam(sim_vortex_body_autoangulardampingtensionratio,v);
                    if (ar.xmlGetNode_float("primaxisvectorx",v,exhaustiveXml)) setEngineFloatParam(sim_vortex_body_primaxisvectorx,v);
                    if (ar.xmlGetNode_float("primaxisvectory",v,exhaustiveXml)) setEngineFloatParam(sim_vortex_body_primaxisvectory,v);
                    if (ar.xmlGetNode_float("primaxisvectorz",v,exhaustiveXml)) setEngineFloatParam(sim_vortex_body_primaxisvectorz,v);

                    if (ar.xmlGetNode_int("primlinearaxisfrictionmodel",vi,exhaustiveXml)) setEngineIntParam(sim_vortex_body_primlinearaxisfrictionmodel,vi);
                    if (ar.xmlGetNode_int("seclinearaxisfrictionmodel",vi,exhaustiveXml)) setEngineIntParam(sim_vortex_body_seclinearaxisfrictionmodel,vi);
                    if (ar.xmlGetNode_int("primangulararaxisfrictionmodel",vi,exhaustiveXml)) setEngineIntParam(sim_vortex_body_primangulararaxisfrictionmodel,vi);
                    if (ar.xmlGetNode_int("secmangulararaxisfrictionmodel",vi,exhaustiveXml)) setEngineIntParam(sim_vortex_body_secmangulararaxisfrictionmodel,vi);
                    if (ar.xmlGetNode_int("normalmangulararaxisfrictionmodel",vi,exhaustiveXml)) setEngineIntParam(sim_vortex_body_normalmangulararaxisfrictionmodel,vi);
                    if (ar.xmlGetNode_int("autosleepsteplivethreshold",vi,exhaustiveXml)) setEngineIntParam(sim_vortex_body_autosleepsteplivethreshold,vi);
                    if (ar.xmlGetNode_int("materialuniqueid",vi,exhaustiveXml)) setEngineIntParam(sim_vortex_body_materialuniqueid,vi);

                    if (ar.xmlGetNode_bool("pureshapesasconvex",vb,exhaustiveXml)) setEngineBoolParam(sim_vortex_body_pureshapesasconvex,vb);
                    if (ar.xmlGetNode_bool("convexshapesasrandom",vb,exhaustiveXml)) setEngineBoolParam(sim_vortex_body_convexshapesasrandom,vb);
                    if (ar.xmlGetNode_bool("randomshapesasterrain",vb,exhaustiveXml)) setEngineBoolParam(sim_vortex_body_randomshapesasterrain,vb);
                    if (ar.xmlGetNode_bool("fastmoving",vb,exhaustiveXml)) setEngineBoolParam(sim_vortex_body_fastmoving,vb);
                    if (ar.xmlGetNode_bool("autoslip",vb,exhaustiveXml)) setEngineBoolParam(sim_vortex_body_autoslip,vb);
                    if (ar.xmlGetNode_bool("seclinaxissameasprimlinaxis",vb,exhaustiveXml)) setEngineBoolParam(sim_vortex_body_seclinaxissameasprimlinaxis,vb);
                    if (ar.xmlGetNode_bool("secangaxissameasprimangaxis",vb,exhaustiveXml)) setEngineBoolParam(sim_vortex_body_secangaxissameasprimangaxis,vb);
                    if (ar.xmlGetNode_bool("normangaxissameasprimangaxis",vb,exhaustiveXml)) setEngineBoolParam(sim_vortex_body_normangaxissameasprimangaxis,vb);
                    if (ar.xmlGetNode_bool("autoangulardamping",vb,exhaustiveXml)) setEngineBoolParam(sim_vortex_body_autoangulardamping,vb);
                    ar.xmlPopNode();
                }

                if (ar.xmlPushChildNode("newton",exhaustiveXml))
                {
                    if (ar.xmlGetNode_float("staticfriction",v,exhaustiveXml)) setEngineFloatParam(sim_newton_body_staticfriction,v);
                    if (ar.xmlGetNode_float("kineticfriction",v,exhaustiveXml)) setEngineFloatParam(sim_newton_body_kineticfriction,v);
                    if (ar.xmlGetNode_float("restitution",v,exhaustiveXml)) setEngineFloatParam(sim_newton_body_restitution,v);
                    if (ar.xmlGetNode_float("lineardrag",v,exhaustiveXml)) setEngineFloatParam(sim_newton_body_lineardrag,v);
                    if (ar.xmlGetNode_float("angulardrag",v,exhaustiveXml)) setEngineFloatParam(sim_newton_body_angulardrag,v);

                    if (ar.xmlGetNode_bool("fastmoving",vb,exhaustiveXml)) setEngineBoolParam(sim_newton_body_fastmoving,vb);
                    ar.xmlPopNode();
                }
                ar.xmlPopNode();
            }
        }
    }
}

max_stars_repo_name: mdecourse/CoppeliaSimLib

max_issues_repo_name: mdecourse/CoppeliaSimLib

max_forks_repo_name: mdecourse/CoppeliaSimLib


text:
/*
Copyright 2020 Glen Joseph Fernandes
(glenjofe@gmail.com)

Distributed under the Boost Software License,
Version 1.0. (See accompanying file LICENSE_1_0.txt
or copy at http://www.boost.org/LICENSE_1_0.txt)
*/

#ifndef BOOST_TT_CONJUNCTION_HPP_INCLUDED
#define BOOST_TT_CONJUNCTION_HPP_INCLUDED

#include <boost/type_traits/conditional.hpp>
#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
#include <boost/type_traits/integral_constant.hpp>
#endif

namespace boost {

#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
template<class...>
struct conjunction
    : true_type { };

template<class T>
struct conjunction<T>
    : T { };

template<class T, class... U>
struct conjunction<T, U...>
    : conditional<bool(T::value), conjunction<U...>, T>::type { };
#else
template<class T, class U>
struct conjunction
    : conditional<bool(T::value), U, T>::type { };
#endif

} /* boost */

#endif

ext: hpp

max_stars_repo_name: kindlychung/mediasoup-sfu-cpp

max_issues_repo_name: kindlychung/mediasoup-sfu-cpp

max_forks_repo_name: kindlychung/mediasoup-sfu-cpp


text:
#include <fstream>
#include <string>

#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>

#include "ChessBoard.hpp"

#include "ChessPiece.hpp"
#include "pieces/Pawn.hpp"
#include "pieces/Queen.hpp"
#include "pieces/King.hpp"
#include "pieces/Rook.hpp"
#include "pieces/Bishop.hpp"
#include "pieces/Knight.hpp"

static const int DONT_CHECK_MOVE_ORDER = -1;

class DLL_LOCAL ChessBoard::ChessBoardImpl
{
public:
    ChessBoardImpl(bool empty = false, bool moveOrder = true)
        : _curMove(ChessPiece::WHITE), _moveOrder(moveOrder)
    {
        if (!empty)
            reset();
    }

    ChessBoardImpl(std::istream& is)
    {
        char curMove;
        is >> curMove;
        _curMove = ChessPiece::Color(curMove);
        _moveOrder = (curMove == DONT_CHECK_MOVE_ORDER) ? false : true;

        for (int i = 0; i < SIZE; ++i)
        {
            for (int j = 0; j < SIZE; ++j)
                _board[i][j] = ChessPiece::load(is);
        }
    }

    bool move(const ChessBoard& board, const Coord& from, const Coord& to)
    {
        // out of board check
        if (!isInBoard(from) || !isInBoard(to))
            return false;

        // empty coord check
        if (!_board[from.y][from.x])
            return false;

        // check players move order
        if (_moveOrder && _board[from.y][from.x]->color() != _curMove)
            return false;

        // occupied by own piece check
        if (_board[to.y][to.x] &&
            _board[to.y][to.x]->color() == _board[from.y][from.x]->color())
            return false;

        // piece movement rules check
        if (!_board[from.y][from.x]->canMove(board, from, to))
            return false;

        _board[to.y][to.x] = _board[from.y][from.x];
        _board[from.y][from.x].reset();

        if (_moveOrder)
            _curMove = (_curMove == ChessPiece::WHITE)? ChessPiece::BLACK : ChessPiece::WHITE;

        return true;
    }

    void add(const Coord& coord, const boost::shared_ptr<ChessPiece>& piece)
    {
        if (!isInBoard(coord))
            return;

        _board[coord.y][coord.x] = piece;
    }

    boost::shared_ptr<ChessPiece> get(const Coord& coord) const
    {
        if (!isInBoard(coord))
            return boost::shared_ptr<ChessPiece>();

        return _board[coord.y][coord.x];
    }

    void reset()
    {
        // delete all chesspieces
        for (int i = 2; i <= 5; ++i)
        {
            for (int j = 0; j < SIZE; ++j)
                _board[i][j].reset();
        }

        // create pawns
        for (int i = 0; i < SIZE; ++i)
        {
            _board[1][i] = boost::make_shared<Pawn>(ChessPiece::WHITE);
            _board[6][i] = boost::make_shared<Pawn>(ChessPiece::BLACK);
        }

        // create rooks
        _board[0][0] = boost::make_shared<Rook>(ChessPiece::WHITE);
        _board[0][7] = boost::make_shared<Rook>(ChessPiece::WHITE);
        _board[7][0] = boost::make_shared<Rook>(ChessPiece::BLACK);
        _board[7][7] = boost::make_shared<Rook>(ChessPiece::BLACK);

        // create knights
        _board[0][1] = boost::make_shared<Knight>(ChessPiece::WHITE);
        _board[0][6] = boost::make_shared<Knight>(ChessPiece::WHITE);
        _board[7][1] = boost::make_shared<Knight>(ChessPiece::BLACK);
        _board[7][6] = boost::make_shared<Knight>(ChessPiece::BLACK);

        // create bishops
        _board[0][2] = boost::make_shared<Bishop>(ChessPiece::WHITE);
        _board[0][5] = boost::make_shared<Bishop>(ChessPiece::WHITE);
        _board[7][2] = boost::make_shared<Bishop>(ChessPiece::BLACK);
        _board[7][5] = boost::make_shared<Bishop>(ChessPiece::BLACK);

        // create queens
        _board[0][3] = boost::make_shared<Queen>(ChessPiece::WHITE);
        _board[7][3] = boost::make_shared<Queen>(ChessPiece::BLACK);

        // create kings
        _board[0][4] = boost::make_shared<King>(ChessPiece::WHITE);
        _board[7][4] = boost::make_shared<King>(ChessPiece::BLACK);
    }

    void save(std::ostream& os) const
    {
        if (_moveOrder)
            os << char(_curMove);
        else
            os << char(DONT_CHECK_MOVE_ORDER);

        for (int i = 0; i < SIZE; ++i)
        {
            for (int j = 0; j < SIZE; ++j)
            {
                if (_board[i][j])
                    _board[i][j]->save(os);
                else
                    os << char(0);
            }
        }
    }

private:
    boost::shared_ptr<ChessPiece> _board[SIZE][SIZE];
    ChessPiece::Color _curMove;
    bool _moveOrder;

    static bool isInBoard(const Coord& coord)
    {
        return coord.y >= 0 && coord.y < SIZE
            && coord.x >= 0 && coord.x < SIZE;
    }
};

ChessBoard::ChessBoard(bool empty, bool moveOrder)
    : _impl(new ChessBoardImpl(empty, moveOrder))
{ }

ChessBoard::ChessBoard(std::istream& is)
    : _impl(new ChessBoardImpl(is))
{ }

ChessBoard::ChessBoard(const ChessBoard& cb)
{
    *_impl = *cb._impl;
}

ChessBoard& ChessBoard::operator=(const ChessBoard& cb)
{
    if (&cb == this)
        return *this;

    *_impl = *cb._impl;

    return *this;
}

ChessBoard::~ChessBoard()
{ /* required by smart pimpl pointer */ }

void ChessBoard::add(const Coord& coord, const boost::shared_ptr<ChessPiece>& piece)
{
    _impl->add(coord, piece);
}

bool ChessBoard::move(const Coord& from, const Coord& to)
{
    return _impl->move(*this, from, to);
}

boost::shared_ptr<ChessPiece> ChessBoard::get(const Coord& coord) const
{
    return _impl->get(coord);
}

void ChessBoard::reset()
{
    _impl->reset();
}

void ChessBoard::save(std::ostream& os) const
{
    _impl->save(os);
}

ext: cpp

max_stars_repo_name: malor/archive

max_issues_repo_name: malor/archive

max_forks_repo_name: malor/archive


text:
#include <Core/Assert.h>

#include <boost/stacktrace.hpp>

namespace Core {
namespace internal {
LogCategory AssertLog("assert");
}
std::string GetBacktraceAsString(uint32_t frameToSkip) {
    // constexpr uint32_t maxFrames = std::numeric_limits<uint32_t>::max();
    return boost::stacktrace::to_string(boost::stacktrace::stacktrace(frameToSkip, 9999));
}

void Abort() {
    Log::Critical(internal::AssertLog, "Backtrace:\n{}", GetBacktraceAsString(4));
    std::abort();
}

}    // namespace Core

max_stars_repo_name: ChewyGumball/bengine

max_issues_repo_name: ChewyGumball/bengine

max_forks_repo_name: ChewyGumball/bengine


text:
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2013 Christian Seiler <christian@iwakd.de>
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include "main.h"

#include <Eigen/CXX11/Core>

using Eigen::internal::is_same;
using Eigen::internal::type_list;
using Eigen::internal::numeric_list;
using Eigen::internal::gen_numeric_list;
using Eigen::internal::gen_numeric_list_reversed;
using Eigen::internal::gen_numeric_list_swapped_pair;
using Eigen::internal::gen_numeric_list_repeated;
using Eigen::internal::concat;
using Eigen::internal::mconcat;
using Eigen::internal::take;
using Eigen::internal::skip;
using Eigen::internal::slice;
using Eigen::internal::get;
using Eigen::internal::id_numeric;
using Eigen::internal::id_type;
using Eigen::internal::is_same_gf;
using Eigen::internal::apply_op_from_left;
using Eigen::internal::apply_op_from_right;
using Eigen::internal::contained_in_list;
using Eigen::internal::contained_in_list_gf;
using Eigen::internal::arg_prod;
using Eigen::internal::arg_sum;
using Eigen::internal::sum_op;
using Eigen::internal::product_op;
using Eigen::internal::array_reverse;
using Eigen::internal::array_sum;
using Eigen::internal::array_prod;
using Eigen::internal::array_reduce;
using Eigen::internal::array_zip;
using Eigen::internal::array_zip_and_reduce;
using Eigen::internal::array_apply;
using Eigen::internal::array_apply_and_reduce;
using Eigen::internal::repeat;
using Eigen::internal::instantiate_by_c_array;

struct dummy_a {};
struct dummy_b {};
struct dummy_c {};
struct dummy_d {};
struct dummy_e {};

// dummy operation for testing apply
template<typename A, typename B> struct dummy_op;
template<> struct dummy_op<dummy_a, dummy_b> { typedef dummy_c type; };
template<> struct dummy_op<dummy_b, dummy_a> { typedef dummy_d type; };
template<> struct dummy_op<dummy_b, dummy_c> { typedef dummy_a type; };
template<> struct dummy_op<dummy_c, dummy_b> { typedef dummy_d type; };
template<> struct dummy_op<dummy_c, dummy_a> { typedef dummy_b type; };
template<> struct dummy_op<dummy_a, dummy_c> { typedef dummy_d type; };
template<> struct dummy_op<dummy_a, dummy_a> { typedef dummy_e type; };
template<> struct dummy_op<dummy_b, dummy_b> { typedef dummy_e type; };
template<> struct dummy_op<dummy_c, dummy_c> { typedef dummy_e type; };

template<typename A, typename B> struct dummy_test { constexpr static bool value = false; constexpr static int global_flags = 0; };
template<> struct dummy_test<dummy_a, dummy_a>     { constexpr static bool value = true;  constexpr static int global_flags = 1; };
template<> struct dummy_test<dummy_b, dummy_b>     { constexpr static bool value = true;  constexpr static int global_flags = 2; };
template<> struct dummy_test<dummy_c, dummy_c>     { constexpr static bool value = true;  constexpr static int global_flags = 4; };

struct times2_op { template<typename A> static A run(A v) { return v * 2; } };

struct dummy_inst
{
  int c;

  dummy_inst() : c(0) {}
  explicit dummy_inst(int) : c(1) {}
  dummy_inst(int, int) : c(2) {}
  dummy_inst(int, int, int) : c(3) {}
  dummy_inst(int, int, int, int) : c(4) {}
  dummy_inst(int, int, int, int, int) : c(5) {}
};

static void test_gen_numeric_list()
{
  VERIFY((is_same<typename gen_numeric_list<int, 0>::type, numeric_list<int>>::value));
  VERIFY((is_same<typename gen_numeric_list<int, 1>::type, numeric_list<int, 0>>::value));
  VERIFY((is_same<typename gen_numeric_list<int, 2>::type, numeric_list<int, 0, 1>>::value));
  VERIFY((is_same<typename gen_numeric_list<int, 5>::type, numeric_list<int, 0, 1, 2, 3, 4>>::value));
  VERIFY((is_same<typename gen_numeric_list<int, 10>::type, numeric_list<int, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9>>::value));

  VERIFY((is_same<typename gen_numeric_list<int, 0, 42>::type, numeric_list<int>>::value));
  VERIFY((is_same<typename gen_numeric_list<int, 1, 42>::type, numeric_list<int, 42>>::value));
  VERIFY((is_same<typename gen_numeric_list<int, 2, 42>::type, numeric_list<int, 42, 43>>::value));
  VERIFY((is_same<typename gen_numeric_list<int, 5, 42>::type, numeric_list<int, 42, 43, 44, 45, 46>>::value));
  VERIFY((is_same<typename gen_numeric_list<int, 10, 42>::type, numeric_list<int, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51>>::value));

  VERIFY((is_same<typename gen_numeric_list_reversed<int, 0>::type, numeric_list<int>>::value));
  VERIFY((is_same<typename gen_numeric_list_reversed<int, 1>::type, numeric_list<int, 0>>::value));
  VERIFY((is_same<typename gen_numeric_list_reversed<int, 2>::type, numeric_list<int, 1, 0>>::value));
  VERIFY((is_same<typename gen_numeric_list_reversed<int, 5>::type, numeric_list<int, 4, 3, 2, 1, 0>>::value));
  VERIFY((is_same<typename gen_numeric_list_reversed<int, 10>::type, numeric_list<int, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0>>::value));

  VERIFY((is_same<typename gen_numeric_list_reversed<int, 0, 42>::type, numeric_list<int>>::value));
  VERIFY((is_same<typename gen_numeric_list_reversed<int, 1, 42>::type, numeric_list<int, 42>>::value));
  VERIFY((is_same<typename gen_numeric_list_reversed<int, 2, 42>::type, numeric_list<int, 43, 42>>::value));
  VERIFY((is_same<typename gen_numeric_list_reversed<int, 5, 42>::type, numeric_list<int, 46, 45, 44, 43, 42>>::value));
  VERIFY((is_same<typename gen_numeric_list_reversed<int, 10, 42>::type, numeric_list<int, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42>>::value));

  VERIFY((is_same<typename gen_numeric_list_swapped_pair<int, 0, 2, 3>::type, numeric_list<int>>::value));
  VERIFY((is_same<typename gen_numeric_list_swapped_pair<int, 1, 2, 3>::type, numeric_list<int, 0>>::value));
  VERIFY((is_same<typename gen_numeric_list_swapped_pair<int, 2, 2, 3>::type, numeric_list<int, 0, 1>>::value));
  VERIFY((is_same<typename gen_numeric_list_swapped_pair<int, 5, 2, 3>::type, numeric_list<int, 0, 1, 3, 2, 4>>::value));
  VERIFY((is_same<typename gen_numeric_list_swapped_pair<int, 10, 2, 3>::type, numeric_list<int, 0, 1, 3, 2, 4, 5, 6, 7, 8, 9>>::value));

  VERIFY((is_same<typename gen_numeric_list_swapped_pair<int, 0, 44, 45, 42>::type, numeric_list<int>>::value));
  VERIFY((is_same<typename gen_numeric_list_swapped_pair<int, 1, 44, 45, 42>::type, numeric_list<int, 42>>::value));
  VERIFY((is_same<typename gen_numeric_list_swapped_pair<int, 2, 44, 45, 42>::type, numeric_list<int, 42, 43>>::value));
  VERIFY((is_same<typename gen_numeric_list_swapped_pair<int, 5, 44, 45, 42>::type, numeric_list<int, 42, 43, 45, 44, 46>>::value));
  VERIFY((is_same<typename gen_numeric_list_swapped_pair<int, 10, 44, 45, 42>::type, numeric_list<int, 42, 43, 45, 44, 46, 47, 48, 49, 50, 51>>::value));

  VERIFY((is_same<typename gen_numeric_list_repeated<int, 0, 0>::type, numeric_list<int>>::value));
  VERIFY((is_same<typename gen_numeric_list_repeated<int, 1, 0>::type, numeric_list<int, 0>>::value));
  VERIFY((is_same<typename gen_numeric_list_repeated<int, 2, 0>::type, numeric_list<int, 0, 0>>::value));
  VERIFY((is_same<typename gen_numeric_list_repeated<int, 5, 0>::type, numeric_list<int, 0, 0, 0, 0, 0>>::value));
  VERIFY((is_same<typename gen_numeric_list_repeated<int, 10, 0>::type, numeric_list<int, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>>::value));
}

static void test_concat()
{
  VERIFY((is_same<typename concat<type_list<dummy_a, dummy_a>, type_list<>>::type, type_list<dummy_a, dummy_a>>::value));
  VERIFY((is_same<typename concat<type_list<>, type_list<dummy_a, dummy_a>>::type, type_list<dummy_a, dummy_a>>::value));
  VERIFY((is_same<typename concat<type_list<dummy_a, dummy_a>, type_list<dummy_a, dummy_a>>::type, type_list<dummy_a, dummy_a, dummy_a, dummy_a>>::value));
  VERIFY((is_same<typename concat<type_list<dummy_a, dummy_a>, type_list<dummy_b, dummy_c>>::type, type_list<dummy_a, dummy_a, dummy_b, dummy_c>>::value));
  VERIFY((is_same<typename concat<type_list<dummy_a>, type_list<dummy_b, dummy_c>>::type, type_list<dummy_a, dummy_b, dummy_c>>::value));

  VERIFY((is_same<typename concat<numeric_list<int, 0, 0>, numeric_list<int>>::type, numeric_list<int, 0, 0>>::value));
  VERIFY((is_same<typename concat<numeric_list<int>, numeric_list<int, 0, 0>>::type, numeric_list<int, 0, 0>>::value));
  VERIFY((is_same<typename concat<numeric_list<int, 0, 0>, numeric_list<int, 0, 0>>::type, numeric_list<int, 0, 0, 0, 0>>::value));
  VERIFY((is_same<typename concat<numeric_list<int, 0, 0>, numeric_list<int, 1, 2>>::type, numeric_list<int, 0, 0, 1, 2>>::value));
  VERIFY((is_same<typename concat<numeric_list<int, 0>, numeric_list<int, 1, 2>>::type, numeric_list<int, 0, 1, 2>>::value));

  VERIFY((is_same<typename mconcat<type_list<dummy_a>>::type, type_list<dummy_a>>::value));
  VERIFY((is_same<typename mconcat<type_list<dummy_a>, type_list<dummy_b>>::type, type_list<dummy_a, dummy_b>>::value));
  VERIFY((is_same<typename mconcat<type_list<dummy_a>, type_list<dummy_b>, type_list<dummy_c>>::type, type_list<dummy_a, dummy_b, dummy_c>>::value));
  VERIFY((is_same<typename mconcat<type_list<dummy_a>, type_list<dummy_b, dummy_c>>::type, type_list<dummy_a, dummy_b, dummy_c>>::value));
  VERIFY((is_same<typename mconcat<type_list<dummy_a, dummy_b>, type_list<dummy_c>>::type, type_list<dummy_a, dummy_b, dummy_c>>::value));

  VERIFY((is_same<typename mconcat<numeric_list<int, 0>>::type, numeric_list<int, 0>>::value));
  VERIFY((is_same<typename mconcat<numeric_list<int, 0>, numeric_list<int, 1>>::type, numeric_list<int, 0, 1>>::value));
  VERIFY((is_same<typename mconcat<numeric_list<int, 0>, numeric_list<int, 1>, numeric_list<int, 2>>::type, numeric_list<int, 0, 1, 2>>::value));
  VERIFY((is_same<typename mconcat<numeric_list<int, 0>, numeric_list<int, 1, 2>>::type, numeric_list<int, 0, 1, 2>>::value));
  VERIFY((is_same<typename mconcat<numeric_list<int, 0, 1>, numeric_list<int, 2>>::type, numeric_list<int, 0, 1, 2>>::value));
}

static void test_slice()
{
  typedef type_list<dummy_a, dummy_a, dummy_b, dummy_b, dummy_c, dummy_c> tl;
  typedef numeric_list<int, 0, 1, 2, 3, 4, 5> il;

  VERIFY((is_same<typename take<0, tl>::type, type_list<>>::value));
  VERIFY((is_same<typename take<1, tl>::type, type_list<dummy_a>>::value));
  VERIFY((is_same<typename take<2, tl>::type, type_list<dummy_a, dummy_a>>::value));
  VERIFY((is_same<typename take<3, tl>::type, type_list<dummy_a, dummy_a, dummy_b>>::value));
  VERIFY((is_same<typename take<4, tl>::type, type_list<dummy_a, dummy_a, dummy_b, dummy_b>>::value));
  VERIFY((is_same<typename take<5, tl>::type, type_list<dummy_a, dummy_a, dummy_b, dummy_b, dummy_c>>::value));
  VERIFY((is_same<typename take<6, tl>::type, type_list<dummy_a, dummy_a, dummy_b, dummy_b, dummy_c, dummy_c>>::value));

  VERIFY((is_same<typename take<0, il>::type, numeric_list<int>>::value));
  VERIFY((is_same<typename take<1, il>::type, numeric_list<int, 0>>::value));
  VERIFY((is_same<typename take<2, il>::type, numeric_list<int, 0, 1>>::value));
  VERIFY((is_same<typename take<3, il>::type, numeric_list<int, 0, 1, 2>>::value));
  VERIFY((is_same<typename take<4, il>::type, numeric_list<int, 0, 1, 2, 3>>::value));
  VERIFY((is_same<typename take<5, il>::type, numeric_list<int, 0, 1, 2, 3, 4>>::value));
  VERIFY((is_same<typename take<6, il>::type, numeric_list<int, 0, 1, 2, 3, 4, 5>>::value));

  VERIFY((is_same<typename skip<0, tl>::type, type_list<dummy_a, dummy_a, dummy_b, dummy_b, dummy_c, dummy_c>>::value));
  VERIFY((is_same<typename skip<1, tl>::type, type_list<dummy_a, dummy_b, dummy_b, dummy_c, dummy_c>>::value));
  VERIFY((is_same<typename skip<2, tl>::type, type_list<dummy_b, dummy_b, dummy_c, dummy_c>>::value));
  VERIFY((is_same<typename skip<3, tl>::type, type_list<dummy_b, dummy_c, dummy_c>>::value));
  VERIFY((is_same<typename skip<4, tl>::type, type_list<dummy_c, dummy_c>>::value));
  VERIFY((is_same<typename skip<5, tl>::type, type_list<dummy_c>>::value));
  VERIFY((is_same<typename skip<6, tl>::type, type_list<>>::value));

  VERIFY((is_same<typename skip<0, il>::type, numeric_list<int, 0, 1, 2, 3, 4, 5>>::value));
  VERIFY((is_same<typename skip<1, il>::type, numeric_list<int, 1, 2, 3, 4, 5>>::value));
  VERIFY((is_same<typename skip<2, il>::type, numeric_list<int, 2, 3, 4, 5>>::value));
  VERIFY((is_same<typename skip<3, il>::type, numeric_list<int, 3, 4, 5>>::value));
  VERIFY((is_same<typename skip<4, il>::type, numeric_list<int, 4, 5>>::value));
  VERIFY((is_same<typename skip<5, il>::type, numeric_list<int, 5>>::value));
  VERIFY((is_same<typename skip<6, il>::type, numeric_list<int>>::value));

  VERIFY((is_same<typename slice<0, 3, tl>::type, typename take<3, tl>::type>::value));
  VERIFY((is_same<typename slice<0, 3, il>::type, typename take<3, il>::type>::value));
  VERIFY((is_same<typename slice<1, 3, tl>::type, type_list<dummy_a, dummy_b, dummy_b>>::value));
  VERIFY((is_same<typename slice<1, 3, il>::type, numeric_list<int, 1, 2, 3>>::value));
}

static void test_get()
{
  typedef type_list<dummy_a, dummy_a, dummy_b, dummy_b, dummy_c, dummy_c> tl;
  typedef numeric_list<int, 4, 8, 15, 16, 23, 42> il;

  VERIFY((is_same<typename get<0, tl>::type, dummy_a>::value));
  VERIFY((is_same<typename get<1, tl>::type, dummy_a>::value));
  VERIFY((is_same<typename get<2, tl>::type, dummy_b>::value));
  VERIFY((is_same<typename get<3, tl>::type, dummy_b>::value));
  VERIFY((is_same<typename get<4, tl>::type, dummy_c>::value));
  VERIFY((is_same<typename get<5, tl>::type, dummy_c>::value));

  VERIFY_IS_EQUAL(((int)get<0, il>::value), 4);
  VERIFY_IS_EQUAL(((int)get<1, il>::value), 8);
  VERIFY_IS_EQUAL(((int)get<2, il>::value), 15);
  VERIFY_IS_EQUAL(((int)get<3, il>::value), 16);
  VERIFY_IS_EQUAL(((int)get<4, il>::value), 23);
  VERIFY_IS_EQUAL(((int)get<5, il>::value), 42);
}

static void test_id_helper(dummy_a a, dummy_a b, dummy_a c)
{
  (void)a;
  (void)b;
  (void)c;
}

template<int... ii>
static void test_id_numeric()
{
  test_id_helper(typename id_numeric<int, ii, dummy_a>::type()...);
}

template<typename... tt>
static void test_id_type()
{
  test_id_helper(typename id_type<tt, dummy_a>::type()...);
}

static void test_id()
{
  // don't call VERIFY here, just assume it works if it compiles
  // (otherwise it will complain that it can't find the function)
  test_id_numeric<1, 4, 6>();
  test_id_type<dummy_a, dummy_b, dummy_c>();
}

static void test_is_same_gf()
{
  VERIFY((!is_same_gf<dummy_a, dummy_b>::value));
  VERIFY((!!is_same_gf<dummy_a, dummy_a>::value));
  VERIFY_IS_EQUAL((!!is_same_gf<dummy_a, dummy_b>::global_flags), 0);
  VERIFY_IS_EQUAL((!!is_same_gf<dummy_a, dummy_a>::global_flags), 0);
}

static void test_apply_op()
{
  typedef type_list<dummy_a, dummy_b, dummy_c> tl;
  VERIFY((!!is_same<typename apply_op_from_left<dummy_op, dummy_a, tl>::type, type_list<dummy_e, dummy_c, dummy_d>>::value));
  VERIFY((!!is_same<typename apply_op_from_right<dummy_op, dummy_a, tl>::type, type_list<dummy_e, dummy_d, dummy_b>>::value));
}

static void test_contained_in_list()
{
  typedef type_list<dummy_a, dummy_b, dummy_c> tl;

  VERIFY((!!contained_in_list<is_same, dummy_a, tl>::value));
  VERIFY((!!contained_in_list<is_same, dummy_b, tl>::value));
  VERIFY((!!contained_in_list<is_same, dummy_c, tl>::value));
  VERIFY((!contained_in_list<is_same, dummy_d, tl>::value));
  VERIFY((!contained_in_list<is_same, dummy_e, tl>::value));

  VERIFY((!!contained_in_list_gf<dummy_test, dummy_a, tl>::value));
  VERIFY((!!contained_in_list_gf<dummy_test, dummy_b, tl>::value));
  VERIFY((!!contained_in_list_gf<dummy_test, dummy_c, tl>::value));
  VERIFY((!contained_in_list_gf<dummy_test, dummy_d, tl>::value));
  VERIFY((!contained_in_list_gf<dummy_test, dummy_e, tl>::value));

  VERIFY_IS_EQUAL(((int)contained_in_list_gf<dummy_test, dummy_a, tl>::global_flags), 1);
  VERIFY_IS_EQUAL(((int)contained_in_list_gf<dummy_test, dummy_b, tl>::global_flags), 2);
  VERIFY_IS_EQUAL(((int)contained_in_list_gf<dummy_test, dummy_c, tl>::global_flags), 4);
  VERIFY_IS_EQUAL(((int)contained_in_list_gf<dummy_test, dummy_d, tl>::global_flags), 0);
  VERIFY_IS_EQUAL(((int)contained_in_list_gf<dummy_test, dummy_e, tl>::global_flags), 0);
}

static void test_arg_reductions()
{
  VERIFY_IS_EQUAL(arg_sum(1,2,3,4), 10);
  VERIFY_IS_EQUAL(arg_prod(1,2,3,4), 24);
  VERIFY_IS_APPROX(arg_sum(0.5, 2, 5), 7.5);
  VERIFY_IS_APPROX(arg_prod(0.5, 2, 5), 5.0);
}

static void test_array_reverse_and_reduce()
{
  std::array<int, 6> a{{4, 8, 15, 16, 23, 42}};
  std::array<int, 6> b{{42, 23, 16, 15, 8, 4}};

  // there is no operator<< for std::array, so VERIFY_IS_EQUAL will
  // not compile
  VERIFY((array_reverse(a) == b));
  VERIFY((array_reverse(b) == a));
  VERIFY_IS_EQUAL((array_sum(a)), 108);
  VERIFY_IS_EQUAL((array_sum(b)), 108);
  VERIFY_IS_EQUAL((array_prod(a)), 7418880);
  VERIFY_IS_EQUAL((array_prod(b)), 7418880);
}

static void test_array_zip_and_apply()
{
  std::array<int, 6> a{{4, 8, 15, 16, 23, 42}};
  std::array<int, 6> b{{0, 1, 2, 3, 4, 5}};
  std::array<int, 6> c{{4, 9, 17, 19, 27, 47}};
  std::array<int, 6> d{{0, 8, 30, 48, 92, 210}};
  std::array<int, 6> e{{0, 2, 4, 6, 8, 10}};

  VERIFY((array_zip<sum_op>(a, b) == c));
  VERIFY((array_zip<product_op>(a, b) == d));
  VERIFY((array_apply<times2_op>(b) == e));
  VERIFY_IS_EQUAL((array_apply_and_reduce<sum_op, times2_op>(a)), 216);
  VERIFY_IS_EQUAL((array_apply_and_reduce<sum_op, times2_op>(b)), 30);
  VERIFY_IS_EQUAL((array_zip_and_reduce<product_op, sum_op>(a, b)), 14755932);
  VERIFY_IS_EQUAL((array_zip_and_reduce<sum_op, product_op>(a, b)), 388);
}

static void test_array_misc()
{
  std::array<int, 3> a3{{1, 1, 1}};
  std::array<int, 6> a6{{2, 2, 2, 2, 2, 2}};
  VERIFY((repeat<3, int>(1) == a3));
  VERIFY((repeat<6, int>(2) == a6));

  int data[5] = { 0, 1, 2, 3, 4 };
  VERIFY_IS_EQUAL((instantiate_by_c_array<dummy_inst, int, 0>(data).c), 0);
  VERIFY_IS_EQUAL((instantiate_by_c_array<dummy_inst, int, 1>(data).c), 1);
  VERIFY_IS_EQUAL((instantiate_by_c_array<dummy_inst, int, 2>(data).c), 2);
  VERIFY_IS_EQUAL((instantiate_by_c_array<dummy_inst, int, 3>(data).c), 3);
  VERIFY_IS_EQUAL((instantiate_by_c_array<dummy_inst, int, 4>(data).c), 4);
  VERIFY_IS_EQUAL((instantiate_by_c_array<dummy_inst, int, 5>(data).c), 5);
}

void test_cxx11_meta()
{
  CALL_SUBTEST(test_gen_numeric_list());
  CALL_SUBTEST(test_concat());
  CALL_SUBTEST(test_slice());
  CALL_SUBTEST(test_get());
  CALL_SUBTEST(test_id());
  CALL_SUBTEST(test_is_same_gf());
  CALL_SUBTEST(test_apply_op());
  CALL_SUBTEST(test_contained_in_list());
  CALL_SUBTEST(test_arg_reductions());
  CALL_SUBTEST(test_array_reverse_and_reduce());
  CALL_SUBTEST(test_array_zip_and_apply());
  CALL_SUBTEST(test_array_misc());
}

max_stars_repo_name: Achierius/SysSim

max_issues_repo_name: USCDataScience/cmu-fg-bg-similarity

max_forks_repo_name: USCDataScience/cmu-fg-bg-similarity


text:
/**
 * Copyright Soramitsu Co., Ltd. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

#ifndef KAGOME_JRPC_PROCESSOR_STUB_HPP
#define KAGOME_JRPC_PROCESSOR_STUB_HPP

#include "api/jrpc/value_converter.hpp"
#include <boost/noncopyable.hpp>
#include <mutex>

#include "api/jrpc/jrpc_processor.hpp"
#include "api/jrpc/jrpc_server_impl.hpp"
#include "api/service/state/state_api.hpp"

namespace kagome::api {

/**
 * @brief Lightweit implementation of API stub
 */
  class JrpcProcessorStub : public JRpcProcessor {
   public:
    JrpcProcessorStub(std::shared_ptr<JRpcServer> server,
                      std::shared_ptr<ApiStub> api)
        : api_{std::move(api)}, server_{std::move(server)} {
      BOOST_ASSERT(api_ != nullptr);
      BOOST_ASSERT(server_ != nullptr);
    }

    ~JrpcProcessorStub() override = default;

    void registerHandlers() override {
      server_->registerHandler(
          "echo",
          [this](const jsonrpc::Request::Parameters &params) -> jsonrpc::Value {
            auto &&nonce = parseParams(params);
            auto &&res = api_->echo(nonce);
            if (!res) {
              throw jsonrpc::Fault(res.error().message());
            }
            return jsonrpc::Value(res.value());
          });
    }

   private:
    std::shared_ptr<ApiStub> api_;
    std::shared_ptr<JRpcServer> server_;

    [[nodiscard]] int64_t parseParams(
        const jsonrpc::Request::Parameters &params) const {
      if (params.size() != 1) {
        throw jsonrpc::InvalidParametersFault("Incorrect number of params");
      }
      auto &param0 = params[0];
      if (!param0.IsInteger64() && !param0.IsInteger32()) {
        throw jsonrpc::InvalidParametersFault(
            "Single parameter must be integer");
      }
      return param0.AsInteger64();
    }
  };

}  // namespace kagome::api

#endif  // KAGOME_JRPC_PROCESSOR_STUB_HPP

max_stars_repo_name: FlorianFranzen/kagome

max_issues_repo_name: FlorianFranzen/kagome

max_forks_repo_name: FlorianFranzen/kagome