Ignore:
Timestamp:
07/20/20 16:37:38 (4 years ago)
Author:
Maciej Komosinski
Message:

fS: faster collision detection, depends on "geometry" algorithms

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/genetics/fS/fS_general.cpp

    r1006 r1017  
    1616int fS_Genotype::precision = 4;
    1717bool fS_Genotype::TURN_WITH_ROTATION = false;
     18std::map<string, double> Node::minValues;
     19std::map<string, double> Node::defaultValues;
     20std::map<string, double> Node::maxValues;
    1821
    1922void Node::prepareParams()
    2023{
    21         defaultValues = {
    22                         {INGESTION, Model::getDefPart().ingest},
    23                         {FRICTION,  Model::getDefPart().friction},
    24                         {STIFFNESS, Model::getDefJoint().stif},
    25                         {ROT_X,     0.0},
    26                         {ROT_Y,     0.0},
    27                         {ROT_Z,     0.0},
    28                         {RX,        0.0},
    29                         {RY,        0.0},
    30                         {RZ,        0.0},
    31                         {SIZE,      1.0},
    32                         {SIZE_X,    Model::getDefPart().scale.x},
    33                         {SIZE_Y,    Model::getDefPart().scale.y},
    34                         {SIZE_Z,    Model::getDefPart().scale.z}
    35         };
     24        if(minValues.empty())
     25        {
     26                minValues = {
     27                                {INGESTION, Model::getMinPart().ingest},
     28                                {FRICTION,  Model::getMinPart().friction},
     29                                {ROT_X,     -M_PI},
     30                                {ROT_Y,     -M_PI},
     31                                {ROT_Z,     -M_PI},
     32                                {RX,        -M_PI},
     33                                {RY,        -M_PI},
     34                                {RZ,        -M_PI},
     35                                {SIZE,      0.01},
     36                                {SIZE_X,    Model::getMinPart().scale.x},
     37                                {SIZE_Y,    Model::getMinPart().scale.y},
     38                                {SIZE_Z,    Model::getMinPart().scale.z}
     39                };
     40        }
     41
     42        if(maxValues.empty())
     43        {
     44                maxValues = {
     45                                {INGESTION, Model::getMaxPart().ingest},
     46                                {FRICTION,  Model::getMaxPart().friction},
     47                                {ROT_X,     M_PI},
     48                                {ROT_Y,     M_PI},
     49                                {ROT_Z,     M_PI},
     50                                {RX,        M_PI},
     51                                {RY,        M_PI},
     52                                {RZ,        M_PI},
     53                                {SIZE,      100.0},
     54                                {SIZE_X,    Model::getMaxPart().scale.x},
     55                                {SIZE_Y,    Model::getMaxPart().scale.y},
     56                                {SIZE_Z,    Model::getMaxPart().scale.z}
     57                };
     58        }
     59        if(defaultValues.empty())
     60        {
     61                defaultValues = {
     62                                {INGESTION, Model::getDefPart().ingest},
     63                                {FRICTION,  Model::getDefPart().friction},
     64                                {ROT_X,     0.0},
     65                                {ROT_Y,     0.0},
     66                                {ROT_Z,     0.0},
     67                                {RX,        0.0},
     68                                {RY,        0.0},
     69                                {RZ,        0.0},
     70                                {SIZE,      1.0},
     71                                {SIZE_X,    Model::getDefPart().scale.x},
     72                                {SIZE_Y,    Model::getDefPart().scale.y},
     73                                {SIZE_Z,    Model::getDefPart().scale.z}
     74                };
     75        }
    3676}
    3777
     
    5898        fr = _state->fr;
    5999        s = _state->s;
    60         stif = _state->stif;
    61100}
    62101
     
    259298        if(paramsEndIndex == -1)
    260299                throw fS_Exception("Lacking param end sign", restOfGenotype.start);
     300
    261301        for (int i = 0; i < int(separators.size()) - 1; i++)
    262302        {
     
    297337}
    298338
    299 double Node::getParam(string key)
     339double Node::getParam(const string &key)
    300340{
    301341        auto item = params.find(key);
    302342        if (item != params.end())
    303343                return item->second;
    304         else
    305                 return defaultValues.at(key);
    306 }
    307 
    308 
    309 void Node::getState(State *_state)
     344        return defaultValues.at(key);
     345}
     346
     347double Node::getParam(const string &key, double defaultValue)
     348{
     349        auto item = params.find(key);
     350        if (item != params.end())
     351                return item->second;
     352        return defaultValue;
     353}
     354
     355
     356void Node::getState(State *_state, bool calculateLocation)
    310357{
    311358        if (state != nullptr)
     
    328375                else if (mod == MODIFIERS[2])
    329376                        state->s *= multiplier;
    330                 else if (mod == MODIFIERS[3])
    331                         state->stif *= multiplier;
    332         }
    333 
    334         if (parent != nullptr)
     377        }
     378
     379        if (parent != nullptr && calculateLocation)
    335380        {
    336381                // Rotate
     
    341386        }
    342387        for (int i = 0; i < int(children.size()); i++)
    343                 children[i]->getState(state);
     388                children[i]->getState(state, calculateLocation);
    344389}
    345390
     
    388433}
    389434
    390 Pt3D Node::calculateSize()
     435void Node::calculateSize(Pt3D &scale)
    391436{
    392437        double sizeMultiplier = getParam(SIZE) * state->s;
    393         double sx = getParam(SIZE_X) * sizeMultiplier;
    394         double sy = getParam(SIZE_Y) * sizeMultiplier;
    395         double sz = getParam(SIZE_Z) * sizeMultiplier;
    396         return Pt3D(sx, sy, sz);
     438        scale.x = getParam(SIZE_X) * sizeMultiplier;
     439        scale.y = getParam(SIZE_Y) * sizeMultiplier;
     440        scale.z = getParam(SIZE_Z) * sizeMultiplier;
    397441}
    398442
     
    400444{
    401445        double result;
    402         Pt3D size = calculateSize();
     446        Pt3D size;
     447        calculateSize(size);
    403448        double radiiProduct = size.x * size.y * size.z;
    404449        switch (partType)
     
    421466bool Node::isPartSizeValid()
    422467{
    423         Pt3D size = calculateSize();
     468        Pt3D size;
     469        calculateSize(size);
    424470        double volume = calculateVolume();
    425471        Part_MinMaxDef minP = Model::getMinPart();
     
    449495Pt3D Node::getVectorRotation()
    450496{
    451         return Pt3D(getParam(ROT_X), getParam(ROT_Y), getParam(ROT_Z));
     497        return Pt3D(getParam(ROT_X, 0.0), getParam(ROT_Y, 0.0), getParam(ROT_Z, 0.0));
    452498}
    453499
    454500Pt3D Node::getRotation()
    455501{
    456         Pt3D rotation = Pt3D(getParam(RX), getParam(RY), getParam(RZ));
     502        Pt3D rotation = Pt3D(getParam(RX, 0.0), getParam(RY, 0.0), getParam(RZ, 0.0));
    457503        if(fS_Genotype::TURN_WITH_ROTATION)
    458504                rotation += getVectorRotation();
     
    492538{
    493539        part = new Part(partType);
    494         part->p = Pt3D(state->location.x,
    495                                    state->location.y,
    496                                    state->location.z);
     540        part->p = Pt3D(state->location);
    497541
    498542        part->friction = getParam(FRICTION) * state->fr;
    499543        part->ingest = getParam(INGESTION) * state->ing;
    500         Pt3D size = calculateSize();
    501         part->scale.x = size.x;
    502         part->scale.y = size.y;
    503         part->scale.z = size.z;
     544        calculateSize(part->scale);
    504545        part->setRot(getRotation());
    505546}
     
    508549{
    509550        Joint *j = new Joint();
    510         j->stif = getParam(STIFFNESS) * state->stif;
    511         j->rotstif = j->stif;
    512 
    513551        j->attachToParts(parent->part, part);
    514552        switch (joint)
     
    650688}
    651689
    652 void fS_Genotype::getState()
     690void fS_Genotype::getState(bool calculateLocation)
    653691{
    654692        State *initialState = new State(Pt3D(0), Pt3D(1, 0, 0));
    655         startNode->getState(initialState);
     693        startNode->getState(initialState, calculateLocation);
    656694}
    657695
     
    662700        model.open(using_checkpoints);
    663701
    664         getState();
     702        getState(true);
    665703        startNode->buildModel(model, nullptr);
    666704        buildNeuroConnections(model);
     
    808846int fS_Genotype::checkValidityOfPartSizes()
    809847{
    810         getState();
     848        getState(false);
    811849        vector<Node*> nodes = getAllNodes();
    812850        for (int i = 0; i < int(nodes.size()); i++)
Note: See TracChangeset for help on using the changeset viewer.