Changeset 714 for cpp


Ignore:
Timestamp:
11/05/17 21:45:20 (6 years ago)
Author:
Maciej Komosinski
Message:
  • Setting default NeuroClass? active/genactive in preconfigured genetics
  • genactive becomes bool (was int)
  • Code formatting
Location:
cpp/frams
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/genetics/preconfigured.h

    r551 r714  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2017  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    1515class PreconfiguredGenetics
    1616{
    17   public:
    18 DefaultGenoConvManager gcm;
    19 GenMan genman;
    20 Geno::Validators validators;
    21 ModelGenoValidator model_validator; //validation through conversion
     17public:
     18        DefaultGenoConvManager gcm;
     19        GenMan genman;
     20        Geno::Validators validators;
     21        ModelGenoValidator model_validator; //validation through conversion
    2222
    23 PreconfiguredGenetics()
     23        PreconfiguredGenetics()
    2424        {
    25         gcm.addDefaultConverters(); //without converters, the application would only handle "format 0" genotypes
    26         if (Geno::useConverters(&gcm)!=NULL)
    27                 logPrintf("PreconfiguredGenetics", "init", LOG_WARN, "Geno converters already configured"); //someone is using multiple PreconfiguredGenetics objects? (or other potentially unsafe configuration)
    28         if (Geno::useValidators(&validators)!=NULL)
    29                 logPrintf("PreconfiguredGenetics", "init", LOG_WARN, "Geno validators already configured");
    30         validators+=&genman; //primary validation: use the extended validity checking (through dedicated genetic operators)
    31         validators+=&model_validator; //secondary validation: this simple validator handles all cases when there is no dedicated genetic validation operator, but a converter for a particular format is available. Converters may be less strict in detecting invalid genotypes but using them and checking whether they produced a valid f0 genotype is also some way to tell whether the initial genotype was valid. Otherwise, without dedicated genetic validation operator, we would have no validity check at all.
     25                gcm.addDefaultConverters(); //without converters, the application would only handle "format 0" genotypes
     26                if (Geno::useConverters(&gcm) != NULL)
     27                        logPrintf("PreconfiguredGenetics", "init", LOG_WARN, "Geno converters already configured"); //someone is using multiple PreconfiguredGenetics objects? (or other potentially unsafe configuration)
     28                if (Geno::useValidators(&validators) != NULL)
     29                        logPrintf("PreconfiguredGenetics", "init", LOG_WARN, "Geno validators already configured");
     30                static const char* genactive_classes[] = { "N", "G", "T", "S", "*", "|", "@", NULL };
     31                NeuroClass::resetActive();
     32                NeuroClass::setGenActive(genactive_classes);
     33                validators += &genman; //primary validation: use the extended validity checking (through dedicated genetic operators)
     34                validators += &model_validator; //secondary validation: this simple validator handles all cases when there is no dedicated genetic validation operator, but a converter for a particular format is available. Converters may be less strict in detecting invalid genotypes but using them and checking whether they produced a valid f0 genotype is also some way to tell whether the initial genotype was valid. Otherwise, without dedicated genetic validation operator, we would have no validity check at all.
    3235        }
    3336
    34 ~PreconfiguredGenetics()
     37        ~PreconfiguredGenetics()
    3538        {
    36         Geno::useConverters(NULL);
    37         Geno::useValidators(NULL);
     39                Geno::useConverters(NULL);
     40                Geno::useValidators(NULL);
    3841        }
    3942};
  • cpp/frams/model/modelparts.cpp

    r640 r714  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2017  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    3030
    3131template<>
    32 char ModelUserTags::reg[5]={0};
     32char ModelUserTags::reg[5] = { 0 };
    3333
    3434/////////////////////////
    3535
    3636PartBase::~PartBase()
    37 {if (mapped) delete mapped;}
     37{
     38        if (mapped) delete mapped;
     39}
    3840
    3941void PartBase::notifyMappingChange()
    4042{
    41 if (owner) owner->partmappingchanged=1;
     43        if (owner) owner->partmappingchanged = 1;
    4244}
    4345
    4446void PartBase::setMapping(const IRange &r)
    4547{
    46 if (mapped) (*mapped)=r;
    47 else mapped=new MultiRange(r);
    48 notifyMappingChange();
     48        if (mapped) (*mapped) = r;
     49        else mapped = new MultiRange(r);
     50        notifyMappingChange();
    4951}
    5052
    5153void PartBase::clearMapping()
    5254{
    53 if (mapped) {delete mapped; mapped=0;}
     55        if (mapped) { delete mapped; mapped = 0; }
    5456}
    5557
    5658void PartBase::addMapping(const IRange &r)
    5759{
    58 if (mapped) mapped->add(r);
    59 else mapped=new MultiRange(r);
    60 notifyMappingChange();
     60        if (mapped) mapped->add(r);
     61        else mapped = new MultiRange(r);
     62        notifyMappingChange();
    6163}
    6264
    6365void PartBase::setMapping(const MultiRange &mr)
    6466{
    65 if (mapped) (*mapped)=mr;
    66 else mapped=new MultiRange(mr);
    67 notifyMappingChange();
     67        if (mapped) (*mapped) = mr;
     68        else mapped = new MultiRange(mr);
     69        notifyMappingChange();
    6870}
    6971
    7072void PartBase::addMapping(const MultiRange &mr)
    7173{
    72 if (mapped) mapped->add(mr);
    73 else mapped=new MultiRange(mr);
    74 notifyMappingChange();
    75 }
    76 
    77 void PartBase::setInfo(const SString& name,const SString& value)
    78 {
    79 strSetField(info,name,value);
    80 }
    81 
    82 void PartBase::setInfo(const SString& name,int value)
    83 {
    84 setInfo(name,SString::valueOf(value));
    85 }
    86 
    87 void PartBase::setInfo(const SString& name,double value)
    88 {
    89 setInfo(name,SString::valueOf(value));
     74        if (mapped) mapped->add(mr);
     75        else mapped = new MultiRange(mr);
     76        notifyMappingChange();
     77}
     78
     79void PartBase::setInfo(const SString& name, const SString& value)
     80{
     81        strSetField(info, name, value);
     82}
     83
     84void PartBase::setInfo(const SString& name, int value)
     85{
     86        setInfo(name, SString::valueOf(value));
     87}
     88
     89void PartBase::setInfo(const SString& name, double value)
     90{
     91        setInfo(name, SString::valueOf(value));
    9092}
    9193
    9294SString PartBase::getInfo(const SString& name)
    9395{
    94 return strGetField(info,name);
     96        return strGetField(info, name);
    9597}
    9698
    9799/////////////////////////
    98100
    99 NeuroClass::NeuroClass(ParamEntry *_props,SString _description,
    100                        int _prefinputs,int _prefoutput,int _preflocation,
    101                        int *_vectordata,bool own_vd,int vhints)
     101NeuroClass::NeuroClass(ParamEntry *_props, SString _description,
     102        int _prefinputs, int _prefoutput, int _preflocation,
     103        int *_vectordata, bool own_vd, int vhints)
    102104        :ownedvectordata(own_vd),
    103          name(_props->name),longname(_props->id),description(_description),
    104          props(_props),ownedprops(false),
    105          prefinputs(_prefinputs),
    106          prefoutput(_prefoutput),
    107          preflocation(_preflocation),
    108          vectordata(_vectordata),
    109          visualhints(vhints),impl_count(0),/*impl(0),*/active(1),genactive(0)
     105        name(_props->name), longname(_props->id), description(_description),
     106        props(_props), ownedprops(false),
     107        prefinputs(_prefinputs),
     108        prefoutput(_prefoutput),
     109        preflocation(_preflocation),
     110        vectordata(_vectordata),
     111        visualhints(vhints), impl_count(0),/*impl(0),*/active(1), genactive(0)
    110112{}
    111113
    112114NeuroClass::~NeuroClass()
    113115{
    114 setSymbolGlyph(0,0);
    115 if (props && ownedprops)
    116         ParamObject::freeParamTab(props);
     116        setSymbolGlyph(0, 0);
     117        if (props && ownedprops)
     118                ParamObject::freeParamTab(props);
    117119}
    118120
    119121NeuroClass::NeuroClass()
    120122        :ownedvectordata(0),
    121          name("Invalid"),
    122          props(empty_paramtab),ownedprops(false),
    123          prefinputs(0), prefoutput(0),
    124          preflocation(0), vectordata(0),
    125          visualhints(0),impl_count(0), /*impl(0),*/ active(1), genactive(0)
     123        name("Invalid"),
     124        props(empty_paramtab), ownedprops(false),
     125        prefinputs(0), prefoutput(0),
     126        preflocation(0), vectordata(0),
     127        visualhints(0), impl_count(0), /*impl(0),*/ active(1), genactive(0)
    126128{}
    127129
     130void NeuroClass::resetActive()
     131{
     132        for (int i = 0; i < Neuro::getClassCount(); i++)
     133        {
     134                Neuro::getClass(i)->genactive = 0;
     135                Neuro::getClass(i)->active = 1;
     136        }
     137}
     138
     139void NeuroClass::setGenActive(const char* genactive_classes[])
     140{
     141        for (const char** n = genactive_classes; *n; n++)
     142        {
     143                NeuroClass* cls = Neuro::getClass(*n);
     144                if (cls) cls->genactive = 1;
     145        }
     146}
     147
    128148SString NeuroClass::getSummary()
    129149{
    130 SString t;
    131 t=getDescription();
    132 if (t.len()) t+="\n\n";
    133 t+="Characteristics:\n";
    134 if(getPreferredInputs())
    135         {
    136         if (getPreferredInputs()<0) t+="   supports any number of inputs\n";
    137         else if (getPreferredInputs()==1) t+="   uses single input\n";
    138         else t+=SString::sprintf("   uses %d inputs\n",getPreferredInputs());
    139         }
    140 else t+="   does not use inputs\n";
    141 if(getPreferredOutput())
    142         t+="   provides output value\n";
    143 else
    144         t+="   does not provide output value\n";
    145 switch(getPreferredLocation())
    146         {
    147         case 0: t+="   does not require location in body\n"; break;
    148         case 1: t+="   should be located on a Part\n"; break;
    149         case 2: t+="   should be located on a Joint\n"; break;
    150         }
    151 Param p=getProperties();
    152 if (p.getPropCount())
    153         {
    154         if (t.len()) t+="\n\n";
    155         t+="Properties:\n";
    156         const char *h;
     150        SString t;
     151        t = getDescription();
     152        if (t.len()) t += "\n\n";
     153        t += "Characteristics:\n";
     154        if (getPreferredInputs())
     155        {
     156                if (getPreferredInputs() < 0) t += "   supports any number of inputs\n";
     157                else if (getPreferredInputs() == 1) t += "   uses single input\n";
     158                else t += SString::sprintf("   uses %d inputs\n", getPreferredInputs());
     159        }
     160        else t += "   does not use inputs\n";
     161        if (getPreferredOutput())
     162                t += "   provides output value\n";
     163        else
     164                t += "   does not provide output value\n";
     165        switch (getPreferredLocation())
     166        {
     167        case 0: t += "   does not require location in body\n"; break;
     168        case 1: t += "   should be located on a Part\n"; break;
     169        case 2: t += "   should be located on a Joint\n"; break;
     170        }
     171        Param p = getProperties();
     172        if (p.getPropCount())
     173        {
     174                if (t.len()) t += "\n\n";
     175                t += "Properties:\n";
     176                const char *h;
     177                int i;
     178                for (i = 0; i < p.getPropCount(); i++)
     179                {
     180                        if (i) t += "\n";
     181                        t += "   "; t += p.name(i); t += " ("; t += p.id(i); t += ") ";
     182                        t += p.describeType(i);
     183                        if (h = p.help(i)) if (*h) { t += " - "; t += h; }
     184                }
     185        }
     186        return t;
     187}
     188
     189/////////////////////////
     190
     191/////////////////////////////////////
     192
     193Neuro::Neuro(double _state, double _inertia, double _force, double _sigmo)
     194        :PartBase(getDefaultStyle()), state(_state)
     195#ifdef MODEL_V1_COMPATIBLE
     196        ,inertia(_inertia),force(_force),sigmo(_sigmo)
     197#endif
     198{
     199#ifdef MODEL_V1_COMPATIBLE
     200        olditems=0;
     201#endif
     202        flags = 0;
     203        myclass = 0;
     204        knownclass = 1;
     205        part_refno = -1; joint_refno = -1;
     206}
     207
     208Neuro::Neuro(void) :PartBase(getDefaultStyle())
     209{
     210        defassign();
     211        state = 0.0;
     212        myclass = NULL;
     213        myclassname = "N";//default d="N" but f0.def is unable to set this (d is GETSET, not a regular FIELD)
     214        knownclass = 0;
     215        refno = 0;
     216        pos = Pt3D_0; rot = Pt3D_0;
     217        parent = 0; part = 0; joint = 0;
     218        parentcount = 0;
     219#ifdef MODEL_V1_COMPATIBLE
     220        olditems=0;
     221#endif
     222        flags = 0;
     223        part_refno = -1; joint_refno = -1;
     224}
     225
     226
     227Neuro::~Neuro()
     228{
     229#ifdef MODEL_V1_COMPATIBLE
     230        if (olditems) delete olditems;
     231#endif
    157232        int i;
    158         for(i=0;i<p.getPropCount();i++)
    159                 {
    160                 if (i) t+="\n";
    161                 t+="   "; t+=p.name(i); t+=" ("; t+=p.id(i); t+=") ";
    162                 t+=p.describeType(i);
    163                 if (h=p.help(i)) if (*h) {t+=" - "; t+=h;}
    164                 }
    165         }
    166 return t;
    167 }
    168 
    169 /////////////////////////
    170 
    171 /////////////////////////////////////
    172 
    173 Neuro::Neuro(double _state,double _inertia,double _force,double _sigmo)
    174         :PartBase(getDefaultStyle()),state(_state)
     233        for (i = 0; i < inputs.size(); i++)
     234        {
     235                NInput &ni = inputs(i);
     236                if (ni.info) delete ni.info;
     237        }
     238}
     239
     240SString** Neuro::inputInfo(int i)
     241{
     242        if (i >= getInputCount()) return 0;
     243        return &inputs(i).info;
     244}
     245
     246void Neuro::setInputInfo(int i, const SString& name, const SString &value)
     247{
     248        SString **s = inputInfo(i);
     249        if (!s) return;
     250        if (!*s) *s = new SString();
     251        strSetField(**s, name, value);
     252}
     253
     254void Neuro::setInputInfo(int i, const SString& name, int value)
     255{
     256        setInputInfo(i, name, SString::valueOf(value));
     257}
     258
     259void Neuro::setInputInfo(int i, const SString& name, double value)
     260{
     261        setInputInfo(i, name, SString::valueOf(value));
     262}
     263
     264SString Neuro::getInputInfo(int i)
     265{
     266        SString **s = inputInfo(i);
     267        if (!s) return SString();
     268        if (!*s) return SString();
     269        return **s;
     270}
     271
     272SString Neuro::getInputInfo(int i, const SString& name)
     273{
     274        SString **s = inputInfo(i);
     275        if (!s) return SString();
     276        if (!*s) return SString();
     277        return strGetField(**s, name);
     278}
     279
     280void Neuro::operator=(const Neuro& src)
     281{
     282        refno = src.refno;
    175283#ifdef MODEL_V1_COMPATIBLE
    176 ,inertia(_inertia),force(_force),sigmo(_sigmo)
     284        neuro_refno=-1;
     285        conn_refno=-1;
     286        force=src.force;
     287        sigmo=src.sigmo;
     288        inertia=src.inertia;
     289        weight=src.weight;
     290        olditems=0;
    177291#endif
    178 {
    179 #ifdef MODEL_V1_COMPATIBLE
    180 olditems=0;
    181 #endif
    182 flags=0;
    183 myclass=0;
    184 knownclass=1;
    185 part_refno=-1; joint_refno=-1;
    186 }
    187 
    188 Neuro::Neuro(void):PartBase(getDefaultStyle())
    189 {
    190 defassign();
    191 state=0.0;
    192 myclass=NULL;
    193 myclassname="N";//default d="N" but f0.def is unable to set this (d is GETSET, not a regular FIELD)
    194 knownclass=0;
    195 refno=0;
    196 pos=Pt3D_0; rot=Pt3D_0;
    197 parent=0; part=0; joint=0;
    198 parentcount=0;
    199 #ifdef MODEL_V1_COMPATIBLE
    200 olditems=0;
    201 #endif
    202 flags=0;
    203 part_refno=-1; joint_refno=-1;
    204 }
    205 
    206 
    207 Neuro::~Neuro()
    208 {
    209 #ifdef MODEL_V1_COMPATIBLE
    210 if (olditems) delete olditems;
    211 #endif
    212 int i;
    213 for(i=0;i<inputs.size();i++)
    214         {
    215         NInput &ni=inputs(i);
    216         if (ni.info) delete ni.info;
    217         }
    218 }
    219 
    220 SString** Neuro::inputInfo(int i)
    221 {
    222 if (i>=getInputCount()) return 0;
    223 return &inputs(i).info;
    224 }
    225 
    226 void Neuro::setInputInfo(int i,const SString& name,const SString &value)
    227 {
    228 SString **s=inputInfo(i);
    229 if (!s) return;
    230 if (!*s) *s=new SString();
    231 strSetField(**s,name,value);
    232 }
    233 
    234 void Neuro::setInputInfo(int i,const SString& name,int value)
    235 {
    236 setInputInfo(i,name,SString::valueOf(value));
    237 }
    238 
    239 void Neuro::setInputInfo(int i,const SString& name,double value)
    240 {
    241 setInputInfo(i,name,SString::valueOf(value));
    242 }
    243 
    244 SString Neuro::getInputInfo(int i)
    245 {
    246 SString **s=inputInfo(i);
    247 if (!s) return SString();
    248 if (!*s) return SString();
    249 return **s;
    250 }
    251 
    252 SString Neuro::getInputInfo(int i,const SString& name)
    253 {
    254 SString **s=inputInfo(i);
    255 if (!s) return SString();
    256 if (!*s) return SString();
    257 return strGetField(**s,name);
    258 }
    259 
    260 void Neuro::operator=(const Neuro& src)
    261 {
    262 refno=src.refno;
    263 #ifdef MODEL_V1_COMPATIBLE
    264 neuro_refno=-1;
    265 conn_refno=-1;
    266 force=src.force;
    267 sigmo=src.sigmo;
    268 inertia=src.inertia;
    269 weight=src.weight;
    270 olditems=0;
    271 #endif
    272 state=src.state;
    273 part_refno=-1;
    274 joint_refno=-1;
    275 pos=src.pos; rot=src.rot;
    276 parent=0; part=0; joint=0;
    277 parentcount=0;
    278 flags=0;
    279 myclass=src.myclass;
    280 knownclass=src.knownclass;
    281 myclassname=src.myclassname;
    282 myclassparams=src.myclassparams;
     292        state = src.state;
     293        part_refno = -1;
     294        joint_refno = -1;
     295        pos = src.pos; rot = src.rot;
     296        parent = 0; part = 0; joint = 0;
     297        parentcount = 0;
     298        flags = 0;
     299        myclass = src.myclass;
     300        knownclass = src.knownclass;
     301        myclassname = src.myclassname;
     302        myclassparams = src.myclassparams;
    283303}
    284304
    285305void Neuro::attachToPart(int i)
    286 {attachToPart((i>=0)?owner->getPart(i):0);}
     306{
     307        attachToPart((i >= 0) ? owner->getPart(i) : 0);
     308}
    287309
    288310void Neuro::attachToJoint(int i)
    289 {attachToJoint((i>=0)?owner->getJoint(i):0);}
     311{
     312        attachToJoint((i >= 0) ? owner->getJoint(i) : 0);
     313}
    290314
    291315int Neuro::getClassCount()
    292 {return NeuroLibrary::staticlibrary.getClassCount();}
     316{
     317        return NeuroLibrary::staticlibrary.getClassCount();
     318}
    293319
    294320NeuroClass* Neuro::getClass(int classindex)
    295 {return NeuroLibrary::staticlibrary.getClass(classindex);}
     321{
     322        return NeuroLibrary::staticlibrary.getClass(classindex);
     323}
    296324
    297325NeuroClass* Neuro::getClass(const SString& classname)
    298 {return NeuroLibrary::staticlibrary.findClass(classname);}
     326{
     327        return NeuroLibrary::staticlibrary.findClass(classname);
     328}
    299329
    300330int Neuro::getClassIndex(const NeuroClass*nc)
    301 {return NeuroLibrary::staticlibrary.classes.find((void*)nc);}
     331{
     332        return NeuroLibrary::staticlibrary.classes.find((void*)nc);
     333}
    302334
    303335NeuroClass* Neuro::getClass()
    304336{
    305 checkClass();
    306 return myclass;
     337        checkClass();
     338        return myclass;
    307339}
    308340
    309341void Neuro::setClass(NeuroClass* cl)
    310342{
    311 myclass=cl;
    312 myclassname=cl->getName();
    313 knownclass=1;
     343        myclass = cl;
     344        myclassname = cl->getName();
     345        knownclass = 1;
    314346}
    315347
    316348SString Neuro::getClassName(int classindex)
    317349{
    318 NeuroClass *cl=NeuroLibrary::staticlibrary.getClass(classindex);
    319 return cl? cl->getName() : SString();
     350        NeuroClass *cl = NeuroLibrary::staticlibrary.getClass(classindex);
     351        return cl ? cl->getName() : SString();
    320352}
    321353
    322354void Neuro::setDetails(const SString& details)
    323355{
    324 int colon=details.indexOf(':');
    325 if (colon>=0) {myclassname=details.substr(0,colon); myclassparams=details.substr(colon+1);}
    326 else {myclassname=details; myclassparams=0;}
    327 knownclass=0;
     356        int colon = details.indexOf(':');
     357        if (colon >= 0) { myclassname = details.substr(0, colon); myclassparams = details.substr(colon + 1); }
     358        else { myclassname = details; myclassparams = 0; }
     359        knownclass = 0;
    328360}
    329361
    330362SString Neuro::getDetails()
    331363{
    332 SString ret=getClassName();
    333 if (myclassparams.len()) {if (!ret.len()) ret="N"; ret+=":"; ret+=myclassparams;}
    334 return ret;
     364        SString ret = getClassName();
     365        if (myclassparams.len()) { if (!ret.len()) ret = "N"; ret += ":"; ret += myclassparams; }
     366        return ret;
    335367}
    336368
    337369void Neuro::checkClass()
    338370{
    339 if (knownclass) return;
    340 myclass=getClass(myclassname);
    341 knownclass=1;
     371        if (knownclass) return;
     372        myclass = getClass(myclassname);
     373        knownclass = 1;
    342374}
    343375
    344376SyntParam Neuro::classProperties(bool handle_defaults_when_saving)
    345377{
    346 NeuroClass *cl=getClass();
    347 ParamEntry *pe = cl ? cl->getParamTab() : emptyParamTab;
    348 return SyntParam(pe,&myclassparams,handle_defaults_when_saving);
     378        NeuroClass *cl = getClass();
     379        ParamEntry *pe = cl ? cl->getParamTab() : emptyParamTab;
     380        return SyntParam(pe, &myclassparams, handle_defaults_when_saving);
    349381}
    350382
    351383SString Neuro::getClassName()
    352384{
    353 return myclassname;
     385        return myclassname;
    354386}
    355387
    356388void Neuro::setClassName(const SString& clazz)
    357389{
    358 myclassname=clazz;
    359 knownclass=0;
    360 }
    361 
    362 int Neuro::addInput(Neuro* child,double weight,const SString *info)
    363 {
    364 inputs+=NInput(child,weight,(info&&(info->len()))?new SString(*info):0);
    365 child->parentcount++;
    366 if (child->parentcount==1) {child->parent=this;}
    367 return inputs.size()-1;
     390        myclassname = clazz;
     391        knownclass = 0;
     392}
     393
     394int Neuro::addInput(Neuro* child, double weight, const SString *info)
     395{
     396        inputs += NInput(child, weight, (info && (info->len())) ? new SString(*info) : 0);
     397        child->parentcount++;
     398        if (child->parentcount == 1) { child->parent = this; }
     399        return inputs.size() - 1;
    368400}
    369401
    370402int Neuro::findInput(Neuro* child) const
    371403{
    372 for(int i=0;i<inputs.size();i++)
    373         if (inputs(i).n==child) return i;
    374 return -1;
    375 }
    376 
    377 Neuro* Neuro::getInput(int i,double &weight) const
    378 {
    379 if (i>=getInputCount()) return 0;
    380 NInput &inp=inputs(i);
    381 weight=inp.weight;
    382 return inp.n;
     404        for (int i = 0; i < inputs.size(); i++)
     405                if (inputs(i).n == child) return i;
     406        return -1;
     407}
     408
     409Neuro* Neuro::getInput(int i, double &weight) const
     410{
     411        if (i >= getInputCount()) return 0;
     412        NInput &inp = inputs(i);
     413        weight = inp.weight;
     414        return inp.n;
    383415}
    384416
    385417double Neuro::getInputWeight(int i) const
    386418{
    387 return inputs(i).weight;
    388 }
    389 
    390 void Neuro::setInputWeight(int i,double w)
    391 {
    392 inputs(i).weight=w;
    393 }
    394 
    395 void Neuro::setInput(int i,Neuro* n)
    396 {
    397 NInput &inp=inputs(i);
    398 inp.n=n;
    399 }
    400 
    401 void Neuro::setInput(int i,Neuro* n,double w)
    402 {
    403 NInput &inp=inputs(i);
    404 inp.n=n;
    405 inp.weight=w;
     419        return inputs(i).weight;
     420}
     421
     422void Neuro::setInputWeight(int i, double w)
     423{
     424        inputs(i).weight = w;
     425}
     426
     427void Neuro::setInput(int i, Neuro* n)
     428{
     429        NInput &inp = inputs(i);
     430        inp.n = n;
     431}
     432
     433void Neuro::setInput(int i, Neuro* n, double w)
     434{
     435        NInput &inp = inputs(i);
     436        inp.n = n;
     437        inp.weight = w;
    406438}
    407439
    408440void Neuro::removeInput(int refno)
    409441{
    410 Neuro *child=getInput(refno);
    411 child->parentcount--;
    412 if (child->parent==this) child->parent=0;
    413 SString *s=inputs(refno).info;
    414 if (s) delete s;
    415 inputs.remove(refno);
     442        Neuro *child = getInput(refno);
     443        child->parentcount--;
     444        if (child->parent == this) child->parent = 0;
     445        SString *s = inputs(refno).info;
     446        if (s) delete s;
     447        inputs.remove(refno);
    416448}
    417449
    418450int Neuro::removeInput(Neuro* child)
    419451{
    420 int i=findInput(child);
    421 if (i>=0) removeInput(i);
    422 return i;
     452        int i = findInput(child);
     453        if (i >= 0) removeInput(i);
     454        return i;
    423455}
    424456
    425457int Neuro::getOutputsCount() const
    426458{
    427    int c=0;
    428    for(int i=0;i<owner->getNeuroCount();i++)
    429      for(int j=0;j<owner->getNeuro(i)->getInputCount();j++) c+=owner->getNeuro(i)->getInput(j)==this;
    430    return c;
     459        int c = 0;
     460        for (int i = 0; i < owner->getNeuroCount(); i++)
     461                for (int j = 0; j < owner->getNeuro(i)->getInputCount(); j++) c += owner->getNeuro(i)->getInput(j) == this;
     462        return c;
    431463}
    432464
    433465int Neuro::isOldEffector()
    434466{
    435 static SString bend("|"),rot("@");
    436 return ((getClassName()==bend)||(getClassName()==rot));
     467        static SString bend("|"), rot("@");
     468        return ((getClassName() == bend) || (getClassName() == rot));
    437469}
    438470
    439471int Neuro::isOldReceptor()
    440472{
    441 static SString g("G"),t("T"),s("S");
    442 return ((getClassName()==g)||(getClassName()==t)||(getClassName()==s));
     473        static SString g("G"), t("T"), s("S");
     474        return ((getClassName() == g) || (getClassName() == t) || (getClassName() == s));
    443475}
    444476
    445477int Neuro::isOldNeuron()
    446478{
    447 static SString n("N");
    448 return (getClassName()==n);
     479        static SString n("N");
     480        return (getClassName() == n);
    449481}
    450482
    451483int Neuro::isNNConnection()
    452484{
    453 static SString conn("-");
    454 return (getClassName()==conn);
    455 }
    456 
    457 int Neuro::findInputs(SList& result,const char* classname,const Part* part,const Joint* joint) const
    458 {
    459 Neuro *nu;
    460 SString cn(classname);
    461 int n0=result.size();
    462 for(int i=0;nu=getInput(i);i++)
    463         {
    464         if (part)
    465                 if (nu->part != part) continue;
    466         if (joint)
    467                 if (nu->joint != joint) continue;
    468         if (classname)
    469                 if (nu->getClassName() != cn) continue;
    470         result+=(void*)nu;
    471         }
    472 return result.size()-n0;
    473 }
    474 
    475 int Neuro::findOutputs(SList& result,const char* classname,const Part* part,const Joint* joint) const
     485        static SString conn("-");
     486        return (getClassName() == conn);
     487}
     488
     489int Neuro::findInputs(SList& result, const char* classname, const Part* part, const Joint* joint) const
     490{
     491        Neuro *nu;
     492        SString cn(classname);
     493        int n0 = result.size();
     494        for (int i = 0; nu = getInput(i); i++)
     495        {
     496                if (part)
     497                        if (nu->part != part) continue;
     498                if (joint)
     499                        if (nu->joint != joint) continue;
     500                if (classname)
     501                        if (nu->getClassName() != cn) continue;
     502                result += (void*)nu;
     503        }
     504        return result.size() - n0;
     505}
     506
     507int Neuro::findOutputs(SList& result, const char* classname, const Part* part, const Joint* joint) const
    476508{ // not very efficient...
    477 Neuro *nu,*inp;
    478 SString cn(classname);
    479 SList found;
    480 int n0=result.size();
    481 for(int i=0;nu=getModel().getNeuro(i);i++)
    482         {
    483         if (part)
    484                 if (nu->part != part) continue;
    485         if (joint)
    486                 if (nu->joint != joint) continue;
    487         if (classname)
    488                 if (inp->getClassName() != cn) continue;
    489         for (int j=0;inp=nu->getInput(j);j++)
    490                 if (inp==this)
     509        Neuro *nu, *inp;
     510        SString cn(classname);
     511        SList found;
     512        int n0 = result.size();
     513        for (int i = 0; nu = getModel().getNeuro(i); i++)
     514        {
     515                if (part)
     516                        if (nu->part != part) continue;
     517                if (joint)
     518                        if (nu->joint != joint) continue;
     519                if (classname)
     520                        if (inp->getClassName() != cn) continue;
     521                for (int j = 0; inp = nu->getInput(j); j++)
     522                        if (inp == this)
    491523                        {
    492                         result+=(void*)nu;
     524                        result += (void*)nu;
    493525                        break;
    494526                        }
    495527        }
    496 return result.size()-n0;
     528        return result.size() - n0;
    497529}
    498530
    499531void Neuro::get_inputCount(PARAMGETARGS)
    500 {ret->setInt(inputs.size());}
    501 
    502 void Neuro::p_getInputNeuroDef(ExtValue *args,ExtValue *ret)
    503 {
    504 int i=args->getInt();
    505 if ((i<0)||(i>=inputs.size()))
    506         ret->setEmpty();
    507 else
    508         ret->setObject(ExtObject(&Neuro::getStaticParam(),inputs(i).n));
    509 }
    510 
    511 void Neuro::p_getInputWeight(ExtValue *args,ExtValue *ret)
    512 {
    513 int i=args->getInt();
    514 if ((i<0)||(i>=inputs.size()))
    515         ret->setEmpty();
    516 else
    517         ret->setDouble(inputs(i).weight);
    518 }
    519 
    520 void Neuro::p_getInputNeuroIndex(ExtValue *args,ExtValue *ret)
    521 {
    522 int i=args->getInt();
    523 if ((i<0)||(i>=inputs.size()))
    524         ret->setInt(-1);
    525 else
    526         ret->setInt(inputs(i).n->refno);
     532{
     533        ret->setInt(inputs.size());
     534}
     535
     536void Neuro::p_getInputNeuroDef(ExtValue *args, ExtValue *ret)
     537{
     538        int i = args->getInt();
     539        if ((i < 0) || (i >= inputs.size()))
     540                ret->setEmpty();
     541        else
     542                ret->setObject(ExtObject(&Neuro::getStaticParam(), inputs(i).n));
     543}
     544
     545void Neuro::p_getInputWeight(ExtValue *args, ExtValue *ret)
     546{
     547        int i = args->getInt();
     548        if ((i < 0) || (i >= inputs.size()))
     549                ret->setEmpty();
     550        else
     551                ret->setDouble(inputs(i).weight);
     552}
     553
     554void Neuro::p_getInputNeuroIndex(ExtValue *args, ExtValue *ret)
     555{
     556        int i = args->getInt();
     557        if ((i < 0) || (i >= inputs.size()))
     558                ret->setInt(-1);
     559        else
     560                ret->setInt(inputs(i).n->refno);
    527561}
    528562
     
    530564{
    531565#ifndef SDK_WITHOUT_FRAMS
    532 NeuroClassExt::makeStaticObject(ret,getClass());
     566        NeuroClassExt::makeStaticObject(ret, getClass());
    533567#endif
    534568}
     
    538572void OldItems::buildlist()
    539573{ // guaranteed to work only for old NN layouts
    540  // (neurons,neuro connections, old receptors and effectors)
    541 if (listok) return;
    542  // inputs can contain both neuroitem connections (details="") and direct neuron references (details="N")
    543  // in OldItems we create neuroitems freom direct references
    544 for(int i=0;i<neuro.getInputCount();i++)
    545         {
    546         float w;
    547         Neuro *n=neuro.getInput(i,w);
    548         if (n->isOldNeuron())
     574        // (neurons,neuro connections, old receptors and effectors)
     575        if (listok) return;
     576        // inputs can contain both neuroitem connections (details="") and direct neuron references (details="N")
     577        // in OldItems we create neuroitems freom direct references
     578        for(int i=0;i<neuro.getInputCount();i++)
     579        {
     580                float w;
     581                Neuro *n=neuro.getInput(i,w);
     582                if (n->isOldNeuron())
    549583                {
    550                 Neuro *ni=new Neuro();
    551                 ni->setClassName("-");
    552                 ni->weight=w;
    553                 ni->neuro_refno=neuro.refno;
    554                 ni->conn_refno=n->refno;
    555                 items+=ni;
    556                 syntitems+=ni;
     584                        Neuro *ni=new Neuro();
     585                        ni->setClassName("-");
     586                        ni->weight=w;
     587                        ni->neuro_refno=neuro.refno;
     588                        ni->conn_refno=n->refno;
     589                        items+=ni;
     590                        syntitems+=ni;
    557591                }
    558         else
     592                else
    559593                {
    560                 items+=n;
    561                 n->weight=w;
     594                        items+=n;
     595                        n->weight=w;
    562596                }
    563597        }
    564 SList outputs;
    565 neuro.findOutputs(outputs);
    566 FOREACH(Neuro*,n,outputs)
    567         {
    568         if (n->isNNConnection() || n->isOldNeuron())
    569                 outputs-=n;
    570         }
    571 items+=outputs;
    572 listok=1;
     598        SList outputs;
     599        neuro.findOutputs(outputs);
     600        FOREACH(Neuro*,n,outputs)
     601        {
     602                if (n->isNNConnection() || n->isOldNeuron())
     603                        outputs-=n;
     604        }
     605        items+=outputs;
     606        listok=1;
    573607}
    574608
    575609void OldItems::freelist()
    576610{
    577 FOREACH(Neuro*,n,syntitems)
    578         delete n;
    579 syntitems.clear();
    580 items.clear();
    581 listok=0;
     611        FOREACH(Neuro*,n,syntitems)
     612                delete n;
     613        syntitems.clear();
     614        items.clear();
     615        listok=0;
    582616}
    583617
    584618int OldItems::getItemCount()
    585619{
    586 buildlist();
    587 return items.size();
     620        buildlist();
     621        return items.size();
    588622}
    589623
    590624NeuroItem *OldItems::getNeuroItem(int i)
    591625{
    592 buildlist();
    593 return (NeuroItem*)items(i);
     626        buildlist();
     627        return (NeuroItem*)items(i);
    594628}
    595629
    596630NeuroItem *OldItems::addNewNeuroItem()
    597631{
    598 Neuro *nu=neuro.getModel().addNewNeuro();
    599 nu->setClassName("-");
    600 if (listok) items+=nu;
    601 neuro.addInput(nu);
    602 return (NeuroItem*)nu;
     632        Neuro *nu=neuro.getModel().addNewNeuro();
     633        nu->setClassName("-");
     634        if (listok) items+=nu;
     635        neuro.addInput(nu);
     636        return (NeuroItem*)nu;
    603637}
    604638
    605639int OldItems::findNeuroItem(NeuroItem *ni)
    606640{
    607 buildlist();
    608 return items.find((void*)ni);
     641        buildlist();
     642        return items.find((void*)ni);
    609643}
    610644#endif
     
    613647
    614648SString Part::getDefaultStyle()
    615 {return SString("part");}
     649{
     650        return SString("part");
     651}
    616652SString Joint::getDefaultStyle()
    617 {return SString("joint");}
     653{
     654        return SString("joint");
     655}
    618656/*
    619657const SString& Neuro::getDefaultStyle()
     
    623661*/
    624662SString Neuro::getDefaultStyle()
    625 {return SString("neuro");}
    626 
    627 Part::Part(enum Shape s):PartBase(getDefaultStyle())
    628 {
    629 o=Orient_1;
    630 p=Pt3D_0;
    631 rot=Pt3D_0;
    632 flags=0;
    633 defassign();
    634 shape=s;
    635 mass=1;
     663{
     664        return SString("neuro");
     665}
     666
     667Part::Part(enum Shape s) :PartBase(getDefaultStyle())
     668{
     669        o = Orient_1;
     670        p = Pt3D_0;
     671        rot = Pt3D_0;
     672        flags = 0;
     673        defassign();
     674        shape = s;
     675        mass = 1;
    636676}
    637677
    638678void Part::operator=(const Part& src)
    639679{
    640 p=src.p; o=src.o;
    641 flags=src.flags;
    642 mass=src.mass; density=src.density;
    643 friction=src.friction;
    644 ingest=src.ingest;
    645 assim=src.assim;
    646 size=src.size;
    647 rot=src.rot;
    648 refno=src.refno;
    649 vcolor=src.vcolor;
    650 vsize=src.vsize;
    651 vis_style=src.vis_style;
    652 shape=src.shape;
    653 scale=src.scale;
    654 hollow=src.hollow;
     680        p = src.p; o = src.o;
     681        flags = src.flags;
     682        mass = src.mass; density = src.density;
     683        friction = src.friction;
     684        ingest = src.ingest;
     685        assim = src.assim;
     686        size = src.size;
     687        rot = src.rot;
     688        refno = src.refno;
     689        vcolor = src.vcolor;
     690        vsize = src.vsize;
     691        vis_style = src.vis_style;
     692        shape = src.shape;
     693        scale = src.scale;
     694        hollow = src.hollow;
    655695}
    656696
    657697void Part::setOrient(const Orient &_o)
    658698{
    659 o=_o;
    660 rot.getAngles(o.x,o.z);
     699        o = _o;
     700        rot.getAngles(o.x, o.z);
    661701}
    662702
    663703void Part::setRot(const Pt3D &r)
    664704{
    665 rot=r;
    666 o=Orient_1;
    667 o.rotate(rot);
    668 }
    669 
    670 void Part::setPositionAndRotationFromAxis(const Pt3D &p1,const Pt3D &p2)
    671 {
    672 Pt3D x=p2-p1;
    673 Pt3D dir(x.y,x.z,x.x);
    674 p=p1+x*0.5;
    675 rot.getAngles(x,dir);
     705        rot = r;
     706        o = Orient_1;
     707        o.rotate(rot);
     708}
     709
     710void Part::setPositionAndRotationFromAxis(const Pt3D &p1, const Pt3D &p2)
     711{
     712        Pt3D x = p2 - p1;
     713        Pt3D dir(x.y, x.z, x.x);
     714        p = p1 + x*0.5;
     715        rot.getAngles(x, dir);
    676716}
    677717
    678718Param& Part::getStaticParam()
    679719{
    680 static Param p(f0_part_paramtab,0,"Part");
    681 return p;
     720        static Param p(f0_part_paramtab, 0, "Part");
     721        return p;
    682722}
    683723
     
    685725///////////////////////////
    686726
    687 Joint::Joint():PartBase(getDefaultStyle())
    688 {
    689 rot=Pt3D_0;
    690 defassign();
    691 d.x=JOINT_DELTA_MARKER;
    692 d.y=JOINT_DELTA_MARKER;
    693 d.z=JOINT_DELTA_MARKER;
    694 part1=0; part2=0;
    695 flags=0;
    696 usedelta=0;
     727Joint::Joint() :PartBase(getDefaultStyle())
     728{
     729        rot = Pt3D_0;
     730        defassign();
     731        d.x = JOINT_DELTA_MARKER;
     732        d.y = JOINT_DELTA_MARKER;
     733        d.z = JOINT_DELTA_MARKER;
     734        part1 = 0; part2 = 0;
     735        flags = 0;
     736        usedelta = 0;
    697737}
    698738
    699739void Joint::operator=(const Joint& src)
    700740{
    701 rot=src.rot;
    702 d=src.d;
    703 shape=src.shape;
    704 stamina=src.stamina;
    705 stif=src.stif; rotstif=src.rotstif;
    706 vis_style=src.vis_style;
    707 vcolor=src.vcolor;
    708 part1=0; part2=0;
    709 flags=src.flags;
    710 usedelta=src.usedelta;
    711 refno=src.refno;
    712 }
    713 
    714 void Joint::attachToParts(Part* p1,Part* p2)
    715 {
    716 part1=p1;
    717 part2=p2;
    718 if (p1 && p2)
    719         {
    720         o=rot;
    721         if (usedelta)
     741        rot = src.rot;
     742        d = src.d;
     743        shape = src.shape;
     744        stamina = src.stamina;
     745        stif = src.stif; rotstif = src.rotstif;
     746        vis_style = src.vis_style;
     747        vcolor = src.vcolor;
     748        part1 = 0; part2 = 0;
     749        flags = src.flags;
     750        usedelta = src.usedelta;
     751        refno = src.refno;
     752}
     753
     754void Joint::attachToParts(Part* p1, Part* p2)
     755{
     756        part1 = p1;
     757        part2 = p2;
     758        if (p1 && p2)
     759        {
     760                o = rot;
     761                if (usedelta)
    722762                {
    723                 p1->o.transform(p2->o,o);
    724 //              p2->o.x=p1->o/o.x; p2->o.y=p1->o/o.y; p2->o.z=p1->o/o.z;
    725                 p2->p=p2->o.transform(d)+p1->p;
     763                        p1->o.transform(p2->o, o);
     764                        //              p2->o.x=p1->o/o.x; p2->o.y=p1->o/o.y; p2->o.z=p1->o/o.z;
     765                        p2->p = p2->o.transform(d) + p1->p;
    726766                }
    727767        }
    728768}
    729769
    730 void Joint::attachToParts(int p1,int p2)
    731 {
    732 attachToParts((p1>=0)?owner->getPart(p1):0,(p2>=0)?owner->getPart(p2):0);
     770void Joint::attachToParts(int p1, int p2)
     771{
     772        attachToParts((p1 >= 0) ? owner->getPart(p1) : 0, (p2 >= 0) ? owner->getPart(p2) : 0);
    733773}
    734774
    735775void Joint::resetDelta()
    736776{
    737 d=Pt3D(JOINT_DELTA_MARKER,JOINT_DELTA_MARKER,JOINT_DELTA_MARKER);
     777        d = Pt3D(JOINT_DELTA_MARKER, JOINT_DELTA_MARKER, JOINT_DELTA_MARKER);
    738778}
    739779
    740780void Joint::resetDeltaMarkers()
    741781{
    742 if (d.x==JOINT_DELTA_MARKER) d.x=0;
    743 if (d.y==JOINT_DELTA_MARKER) d.y=0;
    744 if (d.z==JOINT_DELTA_MARKER) d.z=0;
     782        if (d.x == JOINT_DELTA_MARKER) d.x = 0;
     783        if (d.y == JOINT_DELTA_MARKER) d.y = 0;
     784        if (d.z == JOINT_DELTA_MARKER) d.z = 0;
    745785}
    746786
    747787void Joint::useDelta(bool use)
    748788{
    749 usedelta=use;
     789        usedelta = use;
    750790}
    751791
    752792bool Joint::isDelta()
    753793{
    754 return usedelta;
     794        return usedelta;
    755795}
    756796
    757797Param& Joint::getStaticParam()
    758798{
    759 static Param p(f0_joint_paramtab,0,"Joint");
    760 return p;
     799        static Param p(f0_joint_paramtab, 0, "Joint");
     800        return p;
    761801}
    762802
     
    772812ParamEntry f0_old_neuro_tab[]=
    773813{
    774 {"Connections",2,6,"n",},
    775 {"Other properties",},
    776 {"p",0,0,"part ref#","d",FIELD(part_refno),},
    777 {"j",0,0,"joint ref#","d",FIELD(joint_refno),},
    778 {"s",1,0,"state","f",FIELD(state),},
    779 {"in",1,0,"Inertia","f",FIELD(inertia),},
    780 {"fo",1,0,"Force","f",FIELD(force),},
    781 {"si",1,0,"Sigmoid","f",FIELD(sigmo),},
    782 {0,0,0,},
     814        {"Connections",2,6,"n",},
     815        {"Other properties",},
     816        {"p",0,0,"part ref#","d",FIELD(part_refno),},
     817        {"j",0,0,"joint ref#","d",FIELD(joint_refno),},
     818        {"s",1,0,"state","f",FIELD(state),},
     819        {"in",1,0,"Inertia","f",FIELD(inertia),},
     820        {"fo",1,0,"Force","f",FIELD(force),},
     821        {"si",1,0,"Sigmoid","f",FIELD(sigmo),},
     822        {0,0,0,},
    783823};
    784824#undef FIELDSTRUCT
     
    787827ParamEntry f0_neuroitem_paramtab[]=
    788828{
    789 {"Connections",3,12,"ni",},
    790 {"Geometry",},
    791 {"Other",},
    792 {"n",0,0,"neuron ref#","d",FIELD(neuro_refno),},
    793 {"c",2,0,"connection ref#","d",FIELD(conn_refno),},
    794 {"w",2,0,"connection weight","f",FIELD(weight),},
    795 {"p",0,0,"part ref#","d",FIELD(part_refno),},
    796 {"j",0,0,"joint ref#","d",FIELD(joint_refno),},
    797 {"px",1,0,"position.x","f",FIELD(pos.x),},
    798 {"py",1,0,"position.y","f",FIELD(pos.y),},
    799 {"pz",1,0,"position.z","f",FIELD(pos.z),},
    800 {"rx",1,0,"rotation.x","f",FIELD(rot.x),},
    801 {"ry",1,0,"rotation.y","f",FIELD(rot.y),},
    802 {"rz",1,0,"rotation.z","f",FIELD(rot.z),},
    803 {"d",2,0,"item details","s",GETSET(details),},
    804 {0,0,0,},
     829        {"Connections",3,12,"ni",},
     830        {"Geometry",},
     831        {"Other",},
     832        {"n",0,0,"neuron ref#","d",FIELD(neuro_refno),},
     833        {"c",2,0,"connection ref#","d",FIELD(conn_refno),},
     834        {"w",2,0,"connection weight","f",FIELD(weight),},
     835        {"p",0,0,"part ref#","d",FIELD(part_refno),},
     836        {"j",0,0,"joint ref#","d",FIELD(joint_refno),},
     837        {"px",1,0,"position.x","f",FIELD(pos.x),},
     838        {"py",1,0,"position.y","f",FIELD(pos.y),},
     839        {"pz",1,0,"position.z","f",FIELD(pos.z),},
     840        {"rx",1,0,"rotation.x","f",FIELD(rot.x),},
     841        {"ry",1,0,"rotation.y","f",FIELD(rot.y),},
     842        {"rz",1,0,"rotation.z","f",FIELD(rot.z),},
     843        {"d",2,0,"item details","s",GETSET(details),},
     844        {0,0,0,},
    805845};
    806846#undef FIELDSTRUCT
     
    809849////////////////////////////////////////
    810850
    811 ParamEntry Neuro::emptyParamTab[]=
    812 {
    813 {"Undefined Neuro",1,0,"?",},
    814 {0,0,0,},
     851ParamEntry Neuro::emptyParamTab[] =
     852{
     853        { "Undefined Neuro", 1, 0, "?", },
     854        { 0, 0, 0, },
    815855};
    816856
    817857Param Part::extraProperties()
    818858{
    819 return Param(f0_part_xtra_paramtab,this);
     859        return Param(f0_part_xtra_paramtab, this);
    820860}
    821861
    822862Param Joint::extraProperties()
    823863{
    824 return Param(f0_joint_xtra_paramtab,this);
     864        return Param(f0_joint_xtra_paramtab, this);
    825865}
    826866
    827867Param Neuro::extraProperties()
    828868{
    829 return Param(f0_neuro_xtra_paramtab,this);
     869        return Param(f0_neuro_xtra_paramtab, this);
    830870}
    831871
    832872Param Part::properties()
    833873{
    834 return Param(f0_part_paramtab,this);
     874        return Param(f0_part_paramtab, this);
    835875}
    836876
    837877Param Joint::properties()
    838878{
    839 return Param(usedelta?f0_joint_paramtab:f0_nodeltajoint_paramtab,this);
     879        return Param(usedelta ? f0_joint_paramtab : f0_nodeltajoint_paramtab, this);
    840880}
    841881
    842882Param Neuro::properties()
    843883{
    844 return Param(f0_neuro_paramtab,this);
    845 }
    846 
    847 class NeuroExtParamTab: public ParamTab
    848 {
    849   public:
    850 NeuroExtParamTab():ParamTab(f0_neuro_paramtab)
     884        return Param(f0_neuro_paramtab, this);
     885}
     886
     887class NeuroExtParamTab : public ParamTab
     888{
     889public:
     890        NeuroExtParamTab() :ParamTab(f0_neuro_paramtab)
    851891        {
    852892#define FIELDSTRUCT NeuroExt
    853         ParamEntry entry={"class",2,0,"neuro class","s",GETSET(neuroclass)};
     893                ParamEntry entry = { "class", 2, 0, "neuro class", "s", GETSET(neuroclass) };
    854894#undef FIELDSTRUCT
    855         add(&entry);
     895                add(&entry);
    856896
    857897#define FIELDSTRUCT Neuro
    858         ParamEntry entry2={"state",2,0,"state","f",FIELD(state)};
     898                ParamEntry entry2 = { "state", 2, 0, "state", "f", FIELD(state) };
    859899#undef FIELDSTRUCT
    860         add(&entry2);
     900                add(&entry2);
    861901        }
    862902};
     
    864904Param& Neuro::getStaticParam()
    865905{
    866 static Param p(f0_neuro_paramtab,0,"NeuroDef");
    867 return p;
     906        static Param p(f0_neuro_paramtab, 0, "NeuroDef");
     907        return p;
    868908}
    869909
     
    872912NeuroConn::NeuroConn()
    873913{
    874 defassign();
     914        defassign();
    875915}
    876916
     
    879919ParamEntry *NeuroExt::getParamTab()
    880920{
    881 static NeuroExtParamTab tab;
    882 return tab.getParamTab();
     921        static NeuroExtParamTab tab;
     922        return tab.getParamTab();
    883923}
    884924
    885925void NeuroExt::get_neuroclass(PARAMGETARGS)
    886 {ret->setString(getClassName());}
     926{
     927        ret->setString(getClassName());
     928}
    887929
    888930int NeuroExt::set_neuroclass(PARAMSETARGS)
    889 {setClassName(arg->getString());return PSET_CHANGED;}
    890 
    891 
     931{
     932        setClassName(arg->getString()); return PSET_CHANGED;
     933}
  • cpp/frams/model/modelparts.h

    r544 r714  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2017  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    2626class MultiRange;
    2727
    28 typedef UserTags<Model,void*,5> ModelUserTags;
     28typedef UserTags<Model, void*, 5> ModelUserTags;
    2929
    3030/** Common base for model elements. */
     
    3232{
    3333public:
    34 SString vis_style;
    35 PartBase(const SString& s):vis_style(s),mapped(0) {}
    36 ~PartBase();
    37 static SString getDefaultStyle(){return SString("none");}
    38 MultiRange *mapped;
    39 enum PartBaseFlags { Selected=1 };
    40 int flags;
    41 Model *owner;   ///< backlink to the model
    42 
    43 SString info;
    44 
    45 Model &getModel() const {return *owner;}
    46 
    47 ModelUserTags userdata;
    48 
    49 void notifyMappingChange();
    50 
    51 void clearMapping();
    52 MultiRange* getMapping() {return mapped;}
    53 void setMapping(const IRange &mr);
    54 void addMapping(const IRange &mr);
    55 void setMapping(const MultiRange &mr);
    56 void addMapping(const MultiRange &mr);
    57 
    58 void setInfo(const SString& name,const SString& value);
    59 void setInfo(const SString& name,int value);
    60 void setInfo(const SString& name,double value);
    61 SString getInfo(const SString& name);
     34        SString vis_style;
     35        PartBase(const SString& s) :vis_style(s), mapped(0) {}
     36        ~PartBase();
     37        static SString getDefaultStyle(){ return SString("none"); }
     38        MultiRange *mapped;
     39        enum PartBaseFlags { Selected = 1 };
     40        int flags;
     41        Model *owner;   ///< backlink to the model
     42
     43        SString info;
     44
     45        Model &getModel() const { return *owner; }
     46
     47        ModelUserTags userdata;
     48
     49        void notifyMappingChange();
     50
     51        void clearMapping();
     52        MultiRange* getMapping() { return mapped; }
     53        void setMapping(const IRange &mr);
     54        void addMapping(const IRange &mr);
     55        void setMapping(const MultiRange &mr);
     56        void addMapping(const MultiRange &mr);
     57
     58        void setInfo(const SString& name, const SString& value);
     59        void setInfo(const SString& name, int value);
     60        void setInfo(const SString& name, double value);
     61        SString getInfo(const SString& name);
    6262};
    6363
     
    6565/// You can use this class for querying and adjusting constructed
    6666/// model properties
    67 class Part: public PartBase
    68 {
    69 friend class Model;
    70 static SString getDefaultStyle();
    71 Part(double _mass,double _size,double _density,double _friction,double _ingest,double _assim)
    72           :PartBase(getDefaultStyle()),mass(_mass),size(_size),density(_density),friction(_friction),ingest(_ingest),assim(_assim)
     67class Part : public PartBase
     68{
     69        friend class Model;
     70        static SString getDefaultStyle();
     71        Part(double _mass, double _size, double _density, double _friction, double _ingest, double _assim)
     72                :PartBase(getDefaultStyle()), mass(_mass), size(_size), density(_density), friction(_friction), ingest(_ingest), assim(_assim)
    7373        {}
    74 void defassign();
    75 public:
    76 // base properties - have special meaning and therefore are often accessed directly for convenience
    77 Pt3D p;    ///< 3d coordinates of the part
    78 Orient o;  ///< orientation in 3d space (rotation matrix)
    79 /// ParamInterface object is preferred way to get/set other properties.
    80 Param extraProperties();
    81 Param properties();
    82 paInt refno;
    83 Pt3D rot;///< rotation angles
    84 
    85 ///
    86 paInt shape;///default=old Framsticks compatible, do not mix with shapes>0
    87 enum Shape {SHAPE_BALL_AND_STICK=0, SHAPE_ELLIPSOID=1, SHAPE_CUBOID=2, SHAPE_CYLINDER=3};
    88 double mass,size,density,friction,ingest,assim,hollow;
    89 Pt3D scale;
    90 Pt3D food;
    91 //SList points; // collistion points
    92 //Slist neurons; // "select * from owner->neurons where part=this" ;-)
    93 
    94 Pt3D vcolor;
    95 double vsize;
    96 
    97 Part(enum Shape s=SHAPE_BALL_AND_STICK);
    98 Part(const Part& src):PartBase(getDefaultStyle()) {operator=(src);}
    99 void operator=(const Part& src);
    100 
    101 void setPositionAndRotationFromAxis(const Pt3D &p1,const Pt3D &p2);
    102 void setOrient(const Orient &o);///< set part.o and calculates part.rot (rotation angles)
    103 void setRot(const Pt3D &r);///< set part.rot (rotation angles) and calculate part.o
    104 
    105 static Param& getStaticParam();
     74        void defassign();
     75public:
     76        // base properties - have special meaning and therefore are often accessed directly for convenience
     77        Pt3D p;    ///< 3d coordinates of the part
     78        Orient o;  ///< orientation in 3d space (rotation matrix)
     79        /// ParamInterface object is preferred way to get/set other properties.
     80        Param extraProperties();
     81        Param properties();
     82        paInt refno;
     83        Pt3D rot;///< rotation angles
     84
     85        ///
     86        paInt shape;///default=old Framsticks compatible, do not mix with shapes>0
     87        enum Shape { SHAPE_BALL_AND_STICK = 0, SHAPE_ELLIPSOID = 1, SHAPE_CUBOID = 2, SHAPE_CYLINDER = 3 };
     88        double mass, size, density, friction, ingest, assim, hollow;
     89        Pt3D scale;
     90        Pt3D food;
     91        //SList points; // collistion points
     92        //Slist neurons; // "select * from owner->neurons where part=this" ;-)
     93
     94        Pt3D vcolor;
     95        double vsize;
     96
     97        Part(enum Shape s = SHAPE_BALL_AND_STICK);
     98        Part(const Part& src) :PartBase(getDefaultStyle()) { operator=(src); }
     99        void operator=(const Part& src);
     100
     101        void setPositionAndRotationFromAxis(const Pt3D &p1, const Pt3D &p2);
     102        void setOrient(const Orient &o);///< set part.o and calculates part.rot (rotation angles)
     103        void setRot(const Pt3D &r);///< set part.rot (rotation angles) and calculate part.o
     104
     105        static Param& getStaticParam();
    106106};
    107107
    108108/// Imaginary connection between two parts.
    109109/// Joint has no mass nor intertia but can transfer forces.
    110 class Joint: public PartBase
    111 {
    112 friend class Model;
    113 SString getDefaultStyle();
    114 Joint(double _stamina,double _stif,double _rotstif,double _d)
    115         :PartBase(getDefaultStyle()),stamina(_stamina),stif(_stif),rotstif(_rotstif)
    116         {d=Pt3D(_d,0,0);}
    117 void defassign();
    118 void resetDeltaMarkers();
    119 public:
    120 // base properties:
    121 paInt p1_refno,p2_refno; ///< parts' reference numbers
    122 
    123 Part *part1,*part2;     ///< references to parts
    124 class Pt3D d;           ///< position delta between parts
    125 class Pt3D rot; ///< orientation delta between parts expressed as 3 angles
    126 enum Shape {SHAPE_BALL_AND_STICK=0, SHAPE_FIXED=1};
    127 paInt shape;///< ball-and-stick=old Framsticks compatible, creates a physical rod between parts (cylinder or cuboid), do not mix with shape>0,  fixed=merge parts into one physical entity
    128 
    129 Joint();
    130 Joint(const Joint& src):PartBase(getDefaultStyle()) {operator=(src);}
    131 void operator=(const Joint& src);
    132 
    133 /** connect two parts with this joint.
    134     p2 position will be adjusted if delta option is in effect.
    135     @see isDelta()
    136   */
    137 void attachToParts(Part* p1,Part* p2);
    138 /// @see attachToParts(Part*,Part*)
    139 void attachToParts(int p1,int p2);
    140 
    141 /** discard delta information but don't disable delta flag.
    142     delta will be calculated from parts positions during final consistency check.
    143  */
    144 void resetDelta();
    145 
    146 /** enable or disable delta option.
    147     delta value is not changed.
    148  */
    149 void useDelta(bool use);
    150 
    151 /** @return true if delta option is in effect.
    152     @see useDelta(), resetDelta(), useDelta()
    153 */
    154 bool isDelta();
    155 
    156 /// ParamInterface object is preferred way to get/set other properties.
    157 Param extraProperties();
    158 Param properties();
    159 
    160 // do not touch these:
    161 paInt refno; ///< this joint's reference number
    162 double stamina;
    163 double stif,rotstif;    ///< stiffness for moving and bending forces
    164 class Orient o; ///< orientation delta between parts as rotation matrix
    165 /** flag: generated f0 should include delta data.
    166     set by 'singlestep' if j: attributes use delta option */
    167 bool usedelta;
    168 Pt3D vcolor;
    169 
    170 static Param& getStaticParam();
     110class Joint : public PartBase
     111{
     112        friend class Model;
     113        SString getDefaultStyle();
     114        Joint(double _stamina, double _stif, double _rotstif, double _d)
     115                :PartBase(getDefaultStyle()), stamina(_stamina), stif(_stif), rotstif(_rotstif)
     116        {
     117                d = Pt3D(_d, 0, 0);
     118        }
     119        void defassign();
     120        void resetDeltaMarkers();
     121public:
     122        // base properties:
     123        paInt p1_refno, p2_refno; ///< parts' reference numbers
     124
     125        Part *part1, *part2;    ///< references to parts
     126        class Pt3D d;           ///< position delta between parts
     127        class Pt3D rot; ///< orientation delta between parts expressed as 3 angles
     128        enum Shape { SHAPE_BALL_AND_STICK = 0, SHAPE_FIXED = 1 };
     129        paInt shape;///< ball-and-stick=old Framsticks compatible, creates a physical rod between parts (cylinder or cuboid), do not mix with shape>0,  fixed=merge parts into one physical entity
     130
     131        Joint();
     132        Joint(const Joint& src) :PartBase(getDefaultStyle()) { operator=(src); }
     133        void operator=(const Joint& src);
     134
     135        /** connect two parts with this joint.
     136                p2 position will be adjusted if delta option is in effect.
     137                @see isDelta()
     138                */
     139        void attachToParts(Part* p1, Part* p2);
     140        /// @see attachToParts(Part*,Part*)
     141        void attachToParts(int p1, int p2);
     142
     143        /** discard delta information but don't disable delta flag.
     144                delta will be calculated from parts positions during final consistency check.
     145                */
     146        void resetDelta();
     147
     148        /** enable or disable delta option.
     149                delta value is not changed.
     150                */
     151        void useDelta(bool use);
     152
     153        /** @return true if delta option is in effect.
     154                @see useDelta(), resetDelta(), useDelta()
     155                */
     156        bool isDelta();
     157
     158        /// ParamInterface object is preferred way to get/set other properties.
     159        Param extraProperties();
     160        Param properties();
     161
     162        // do not touch these:
     163        paInt refno; ///< this joint's reference number
     164        double stamina;
     165        double stif, rotstif;   ///< stiffness for moving and bending forces
     166        class Orient o; ///< orientation delta between parts as rotation matrix
     167        /** flag: generated f0 should include delta data.
     168                set by 'singlestep' if j: attributes use delta option */
     169        bool usedelta;
     170        Pt3D vcolor;
     171
     172        static Param& getStaticParam();
    171173};
    172174
     
    177179class NeuroClass;
    178180
    179 typedef UserTags<NeuroClass,void*,5> NeuroClassUserTags;
     181typedef UserTags<NeuroClass, void*, 5> NeuroClassUserTags;
    180182
    181183/** Information about neuron class.
     
    183185class NeuroClass
    184186{
    185 bool ownedvectordata;
    186 void operator=(const NeuroClass& nosuchthich){}
    187   public:
    188 SString name,longname,description;
    189 ParamEntry *props;
    190 bool ownedprops;//< destructor will free props using ParamObject::freeParamTab
    191 paInt prefinputs,prefoutput;
    192 paInt preflocation;
    193 int *vectordata;
    194 paInt visualhints;
    195 
    196 //void *impl;
    197 int impl_count;
    198 bool active;
    199 int genactive;
    200 NeuroClassUserTags userdata;
    201 
    202 //////////////////////
    203 ~NeuroClass();
    204 NeuroClass();
    205 NeuroClass(ParamEntry *_props,SString _description,
    206            int _prefinputs,int _prefoutput,int _preflocation,int *_vectordata,bool own_vd=1,int vhints=0);
    207 /** class name for use in Neuro::setClassName(), Neuro::setDetails() (former 'moredata' field),
    208     eg. "N","-",G" */
    209 const SString& getName() {return name;}
    210 /** human friendly name, eg. "Neuron","Link","Gyroscope"  */
    211 const SString& getLongName() {return longname;}
    212 /** long description */
    213 const SString& getDescription() {return description;}
    214 ParamEntry* getParamTab() {return props;}
    215 
    216 /** NeuroClass specific properties, recognized by all neurons of this class */
    217 Param getProperties() {return Param(props);}
    218 
    219 /** preferred number of inputs, -1 = no preference (any number will go).
    220     extra inputs may be ignored by the object (depends on the class).
    221  */
    222 int getPreferredInputs() {return (int)prefinputs;}
    223 
    224 /** @return 0 if this object doesn't provide useful output signal. */
    225 int getPreferredOutput() {return (int)prefoutput;}
    226 
    227 /** @return 0 if the object doesn't need any assignment to the body element.
    228     @return 1 = it likes to be attached to the Part ( @see Neuro::attachToPart() )
    229     @return 2 = the object prefers to have the Joint ( @see Neuro::attachToJoint() )
    230  */
    231 int getPreferredLocation() {return (int)preflocation;}
    232 /** vector drawing to be used in neuro net diagram.
    233     interpretation:
    234        {   
    235            LEN = datalength (excluding this number)
    236            NL = number_of_lines
    237 line#1 ->  NS = number_of_segments, x1,y1, x2,y2, ... xNS-1,yNS-1,
    238          ...
    239 line#NL -> NS = number_of_segments, x1,y1, x2,y2, ... xNS-1,yNS-1,
    240        }
    241  */
    242 int* getSymbolGlyph()
    243  {return vectordata;}
    244 void setSymbolGlyph(int *data,bool owned=1)
    245  {if (vectordata&&ownedvectordata) delete []vectordata;
    246  vectordata=data; ownedvectordata=owned;}
    247 /** additional information about how the neuron should be drawn
    248     used by structure view (and maybe some other components).
    249     return value is defined by the enum Hint
    250     @see enum Hint
    251  */
    252 int getVisualHints()
    253  {return (int)visualhints;}
    254 
    255 enum Hint
    256         /** don't draw neurons of this class */
    257  { Invisible=1,
    258         /** don't draw classname label below the neuron */
    259    DontShowClass=2,
    260         /** draw the neuron at the first part when attached to joint (default is in the middle) */
    261    AtFirstPart=4,
    262         /** draw the neuron at the second part when attached to joint (default is in the middle) */
    263    AtSecondPart=8,
    264         /** use effector colour for this neuro unit */
    265    EffectorClass=16,
    266         /** use receptor colour for this neuro unit */
    267    ReceptorClass=32,
    268    V1BendMuscle=64,
    269    V1RotMuscle=128,
    270    LinearMuscle=256
    271  };
    272 
    273 /** textual summary, automatically generated from other properties (like the neuro class tooltip) */
    274 SString getSummary();
     187        bool ownedvectordata;
     188        void operator=(const NeuroClass& nosuchthich){}
     189public:
     190        SString name, longname, description;
     191        ParamEntry *props;
     192        bool ownedprops;//< destructor will free props using ParamObject::freeParamTab
     193        paInt prefinputs, prefoutput;
     194        paInt preflocation;
     195        int *vectordata;
     196        paInt visualhints;
     197
     198        //void *impl;
     199        int impl_count;
     200        bool active;
     201        bool genactive;
     202        NeuroClassUserTags userdata;
     203
     204        //////////////////////
     205        ~NeuroClass();
     206        NeuroClass();
     207        NeuroClass(ParamEntry *_props, SString _description,
     208                int _prefinputs, int _prefoutput, int _preflocation, int *_vectordata, bool own_vd = 1, int vhints = 0);
     209        /** class name for use in Neuro::setClassName(), Neuro::setDetails() (former 'moredata' field),
     210                eg. "N","-",G" */
     211        const SString& getName() { return name; }
     212        /** human friendly name, eg. "Neuron","Link","Gyroscope"  */
     213        const SString& getLongName() { return longname; }
     214        /** long description */
     215        const SString& getDescription() { return description; }
     216        ParamEntry* getParamTab() { return props; }
     217
     218        /** NeuroClass specific properties, recognized by all neurons of this class */
     219        Param getProperties() { return Param(props); }
     220
     221        /** preferred number of inputs, -1 = no preference (any number will go).
     222                extra inputs may be ignored by the object (depends on the class).
     223                */
     224        int getPreferredInputs() { return (int)prefinputs; }
     225
     226        /** @return 0 if this object doesn't provide useful output signal. */
     227        int getPreferredOutput() { return (int)prefoutput; }
     228
     229        /** @return 0 if the object doesn't need any assignment to the body element.
     230                @return 1 = it likes to be attached to the Part ( @see Neuro::attachToPart() )
     231                @return 2 = the object prefers to have the Joint ( @see Neuro::attachToJoint() )
     232                */
     233        int getPreferredLocation() { return (int)preflocation; }
     234        /** vector drawing to be used in neuro net diagram.
     235                interpretation:
     236                {
     237                LEN = datalength (excluding this number)
     238                NL = number_of_lines
     239                line#1 ->  NS = number_of_segments, x1,y1, x2,y2, ... xNS-1,yNS-1,
     240                ...
     241                line#NL -> NS = number_of_segments, x1,y1, x2,y2, ... xNS-1,yNS-1,
     242                }
     243                */
     244        int* getSymbolGlyph()
     245        {
     246                return vectordata;
     247        }
     248        void setSymbolGlyph(int *data, bool owned = 1)
     249        {
     250                if (vectordata&&ownedvectordata) delete[]vectordata;
     251                vectordata = data; ownedvectordata = owned;
     252        }
     253        /** additional information about how the neuron should be drawn
     254                used by structure view (and maybe some other components).
     255                return value is defined by the enum Hint
     256                @see enum Hint
     257                */
     258        int getVisualHints()
     259        {
     260                return (int)visualhints;
     261        }
     262
     263        enum Hint
     264                /** don't draw neurons of this class */
     265        {
     266                Invisible = 1,
     267                /** don't draw classname label below the neuron */
     268                DontShowClass = 2,
     269                /** draw the neuron at the first part when attached to joint (default is in the middle) */
     270                AtFirstPart = 4,
     271                /** draw the neuron at the second part when attached to joint (default is in the middle) */
     272                AtSecondPart = 8,
     273                /** use effector colour for this neuro unit */
     274                EffectorClass = 16,
     275                /** use receptor colour for this neuro unit */
     276                ReceptorClass = 32,
     277                V1BendMuscle = 64,
     278                V1RotMuscle = 128,
     279                LinearMuscle = 256
     280        };
     281
     282        /** textual summary, automatically generated from other properties (like the neuro class tooltip) */
     283        SString getSummary();
     284
     285        static void resetActive(); ///< set default values of active and genactive for all classes
     286        static void setGenActive(const char* classes[]); ///< set genactive for specified classes
    275287};
    276288
     
    283295class OldItems
    284296{
    285 Neuro &neuro;
    286 SList syntitems; ///< to be deleted
    287 SList items;
    288 int listok;
    289   public:
    290 OldItems(Neuro &n):neuro(n),listok(0) {}
    291 ~OldItems() {freelist();}
    292 void buildlist();
    293 void freelist();
    294 
    295 int getItemCount();
    296 NeuroItem *getNeuroItem(int i);
    297 NeuroItem *addNewNeuroItem();
    298 int findNeuroItem(NeuroItem *n);
     297        Neuro &neuro;
     298        SList syntitems; ///< to be deleted
     299        SList items;
     300        int listok;
     301public:
     302        OldItems(Neuro &n):neuro(n),listok(0) {}
     303        ~OldItems() {freelist();}
     304        void buildlist();
     305        void freelist();
     306
     307        int getItemCount();
     308        NeuroItem *getNeuroItem(int i);
     309        NeuroItem *addNewNeuroItem();
     310        int findNeuroItem(NeuroItem *n);
    299311};
    300312#endif
    301313
    302314/** Single processing unit in Framsticks NN.  */
    303 class Neuro: public PartBase
    304 {
    305 friend class Model;
    306 static SString getDefaultStyle();
    307 
    308 struct NInput { Neuro *n; double weight; SString *info;
    309         NInput(Neuro *_n,double w,SString *i=0):n(_n),weight(w),info(i) {} };
    310 
    311 SListTempl<NInput> inputs;
    312 
    313 NeuroClass *myclass;
    314 bool knownclass;
    315 SString myclassname, myclassparams;
    316 /** set myclass and make knownclass=true */
    317 void checkClass();
    318 SString** inputInfo(int i);
    319 void defassign();
    320 
    321 public:
    322 enum NeuroFlags { HoldState=2 };
    323 Param properties();
    324 Param extraProperties();
    325 
    326 void setInputInfo(int i,const SString& name,const SString &value);
    327 void setInputInfo(int i,const SString& name,int value);
    328 void setInputInfo(int i,const SString& name,double value);
    329 SString getInputInfo(int i);
    330 SString getInputInfo(int i,const SString& name);
    331 
    332 NeuroClass* getClass();
    333 void setClass(NeuroClass*);
    334 
    335 SString getClassParams() {return myclassparams;}
    336 void setClassParams(const SString& cp) {myclassparams=cp;}
    337 
    338 SString getClassName();
    339 void setClassName(const SString& clazz);
    340 
    341 /** return neuro unit details encoded as <CLASS> ":" <PROPERTIES>
    342    
    343     new Neuro can be created as root object (without parent) or can be
    344     the child of existing Neuro. Children of the Neuro are its inputs.
    345     Standard Framsticks neuron calculates the sum of all input units - other processing
    346     units don't have to treat them equally and can even ignore some of them.
    347     There are hints about expected inputs in the class database, @see getClass
    348 
    349     Application should not assume anything about classes and its properties
    350     except for two standard classes: (information about all current classes
    351     can be retrieved with getClass/getClassProperties methods)
    352     - getClassName()="N" is the standard Framsticks neuron, accepts any number of inputs,
    353       compatible with old Neuro object
    354     - getClassName()="-" is the neuron link, compatible with old Neuro-Neuro link
    355       (NeuroItem with empty details)
    356     Empty details defaults to "-" if the parent unit is specified,
    357     and "N" if the unit has no parent.
    358  */
    359 SString getDetails();
    360 
    361 /** details = classname + ":" + classparams
    362     @see getDetails()
    363  */
    364 void setDetails(const SString&);
     315class Neuro : public PartBase
     316{
     317        friend class Model;
     318        static SString getDefaultStyle();
     319
     320        struct NInput {
     321                Neuro *n; double weight; SString *info;
     322                NInput(Neuro *_n, double w, SString *i = 0) :n(_n), weight(w), info(i) {}
     323        };
     324
     325        SListTempl<NInput> inputs;
     326
     327        NeuroClass *myclass;
     328        bool knownclass;
     329        SString myclassname, myclassparams;
     330        /** set myclass and make knownclass=true */
     331        void checkClass();
     332        SString** inputInfo(int i);
     333        void defassign();
     334
     335public:
     336        enum NeuroFlags { HoldState = 2 };
     337        Param properties();
     338        Param extraProperties();
     339
     340        void setInputInfo(int i, const SString& name, const SString &value);
     341        void setInputInfo(int i, const SString& name, int value);
     342        void setInputInfo(int i, const SString& name, double value);
     343        SString getInputInfo(int i);
     344        SString getInputInfo(int i, const SString& name);
     345
     346        NeuroClass* getClass();
     347        void setClass(NeuroClass*);
     348
     349        SString getClassParams() { return myclassparams; }
     350        void setClassParams(const SString& cp) { myclassparams = cp; }
     351
     352        SString getClassName();
     353        void setClassName(const SString& clazz);
     354
     355        /** return neuro unit details encoded as <CLASS> ":" <PROPERTIES>
     356
     357                new Neuro can be created as root object (without parent) or can be
     358                the child of existing Neuro. Children of the Neuro are its inputs.
     359                Standard Framsticks neuron calculates the sum of all input units - other processing
     360                units don't have to treat them equally and can even ignore some of them.
     361                There are hints about expected inputs in the class database, @see getClass
     362
     363                Application should not assume anything about classes and its properties
     364                except for two standard classes: (information about all current classes
     365                can be retrieved with getClass/getClassProperties methods)
     366                - getClassName()="N" is the standard Framsticks neuron, accepts any number of inputs,
     367                compatible with old Neuro object
     368                - getClassName()="-" is the neuron link, compatible with old Neuro-Neuro link
     369                (NeuroItem with empty details)
     370                Empty details defaults to "-" if the parent unit is specified,
     371                and "N" if the unit has no parent.
     372                */
     373        SString getDetails();
     374
     375        /** details = classname + ":" + classparams
     376                @see getDetails()
     377                */
     378        void setDetails(const SString&);
    365379
    366380#define STATRICKCLASS Neuro
    367 PARAMGETDEF(details) {arg1->setString(getDetails());}
    368 PARAMSETDEF(details) {setDetails(arg1->getString());return PSET_CHANGED;}
    369 PARAMGETDEF(inputCount);
    370 PARAMPROCDEF(p_getInputNeuroDef);
    371 PARAMPROCDEF(p_getInputNeuroIndex);
    372 PARAMPROCDEF(p_getInputWeight);
    373 PARAMGETDEF(classObject);
     381        PARAMGETDEF(details) { arg1->setString(getDetails()); }
     382        PARAMSETDEF(details) { setDetails(arg1->getString()); return PSET_CHANGED; }
     383        PARAMGETDEF(inputCount);
     384        PARAMPROCDEF(p_getInputNeuroDef);
     385        PARAMPROCDEF(p_getInputNeuroIndex);
     386        PARAMPROCDEF(p_getInputWeight);
     387        PARAMGETDEF(classObject);
    374388#undef STATRICKCLASS
    375389
    376 ///@param handle_defaults_when_saving see SyntParam
    377 SyntParam classProperties(bool handle_defaults_when_saving=true);
    378 // base properties:
    379 paInt refno; ///< unique reference number (former 'neuro' refno)
    380 
    381 paInt part_refno; ///< can be used by some items as the part ref#
    382 paInt joint_refno; ///< can be used by some items as the joint ref#
    383 
    384 Pt3D pos,rot;   ///< default = zero
    385 
    386 ModelUserTags userdata;
    387 
    388 Neuro();
    389 Neuro(double _state,double _inertia,double _force,double _sigmo);
    390 Neuro(const Neuro& src):PartBase(getDefaultStyle()) {operator=(src);}
    391 
    392 ~Neuro();
    393 
    394 void operator=(const Neuro& src);
    395 
    396 /** Attach this Neuro to the specified Part or detach it from the body if p==NULL.
    397     Neuro can be attached to either Part or Joint, but not both.
    398     @see getPart()
    399  */
    400 void attachToPart(Part* p) {part=p; joint=0;}
    401 
    402 /** Attach this Neuro to the specified Joint or detach it from the body if p==NULL.
    403     Neuro can be attached to either Part or Joint, but not both.
    404     @see getJoint()
    405  */
    406 void attachToJoint(Joint* j) {joint=j; part=0;}
    407 
    408 void attachToPart(int i);
    409 void attachToJoint(int i);
    410 
    411 /** @return Part the Neuro is attached to, or NULL if it has no defined location on the body.
    412     @see attachToPart()
    413  */
    414 Part *getPart() {return part;}
    415 
    416 /** @return Joint the Neuro is attached to, or NULL if it has no defined location on the body.
    417     @see attachToJoint()
    418  */
    419 Joint *getJoint() {return joint;}
    420 
    421 int isOldEffector();
    422 int isOldReceptor();
    423 int isOldNeuron();
    424 int isNNConnection();
    425 
    426 /** @return the number of inputs connected to this Neuro.
    427     Functions like getInput(), getInputWeight() will accept connection number [0..InputCount-1]
    428  */
    429 int getInputCount() const {return inputs.size();}
    430 
    431 /// @return the number of output connections (including possible self-connections)
    432 int getOutputsCount() const;
    433 
    434 /** @return the Neuro connected as i-th input */
    435 Neuro* getInput(int i) const {return (i>=inputs.size())?0:inputs(i).n;}
    436 /** @return the Neuro connected as i-th input.
    437     @param weight
    438  */
    439 Neuro* getInput(int i,double &weight) const;
    440 /** @return connectin weight for i-th input */
    441 double getInputWeight(int i) const;
    442 /** change connection weight for i-th input */
    443 void setInputWeight(int i,double weight);
    444 /** connect i-th input with another neuron */
    445 void setInput(int i,Neuro*n);
    446 /** connect i-th input with another neuron */
    447 void setInput(int i,Neuro*n,double weight);
    448 /** add new input. @return its reference number */
    449 int addInput(Neuro* child,double weight=1.0,const SString* info=0);
    450 /** @return reference number [0..InputCount-1] of the input
    451    or -1 if 'child' is not connected with this Neuro.*/
    452 int findInput(Neuro* child) const;
    453 void removeInput(paInt refno);
    454 /**    @return reference number of the child connection, like findInput() */
    455 int removeInput(Neuro* child);
    456 
    457 int findInputs(SList& result,const char* classname=0,const Part* part=0,const Joint* joint=0) const;
    458 int findOutputs(SList& result,const char* classname=0,const Part* part=0,const Joint* joint=0) const;
    459 
    460 /* class database retrieval */
    461 static int getClassCount();
    462 /** @return Neuro class name.
    463     @param classindex 0 .. getClassCount()
    464  */
    465 static SString getClassName(int classindex);
    466 static NeuroClass* getClass(int classindex);
    467 static NeuroClass* getClass(const SString& classname);
    468 static int getClassIndex(const NeuroClass*nc);
     390        ///@param handle_defaults_when_saving see SyntParam
     391        SyntParam classProperties(bool handle_defaults_when_saving = true);
     392        // base properties:
     393        paInt refno; ///< unique reference number (former 'neuro' refno)
     394
     395        paInt part_refno; ///< can be used by some items as the part ref#
     396        paInt joint_refno; ///< can be used by some items as the joint ref#
     397
     398        Pt3D pos, rot;  ///< default = zero
     399
     400        ModelUserTags userdata;
     401
     402        Neuro();
     403        Neuro(double _state, double _inertia, double _force, double _sigmo);
     404        Neuro(const Neuro& src) :PartBase(getDefaultStyle()) { operator=(src); }
     405
     406        ~Neuro();
     407
     408        void operator=(const Neuro& src);
     409
     410        /** Attach this Neuro to the specified Part or detach it from the body if p==NULL.
     411                Neuro can be attached to either Part or Joint, but not both.
     412                @see getPart()
     413                */
     414        void attachToPart(Part* p) { part = p; joint = 0; }
     415
     416        /** Attach this Neuro to the specified Joint or detach it from the body if p==NULL.
     417                Neuro can be attached to either Part or Joint, but not both.
     418                @see getJoint()
     419                */
     420        void attachToJoint(Joint* j) { joint = j; part = 0; }
     421
     422        void attachToPart(int i);
     423        void attachToJoint(int i);
     424
     425        /** @return Part the Neuro is attached to, or NULL if it has no defined location on the body.
     426                @see attachToPart()
     427                */
     428        Part *getPart() { return part; }
     429
     430        /** @return Joint the Neuro is attached to, or NULL if it has no defined location on the body.
     431                @see attachToJoint()
     432                */
     433        Joint *getJoint() { return joint; }
     434
     435        int isOldEffector();
     436        int isOldReceptor();
     437        int isOldNeuron();
     438        int isNNConnection();
     439
     440        /** @return the number of inputs connected to this Neuro.
     441                Functions like getInput(), getInputWeight() will accept connection number [0..InputCount-1]
     442                */
     443        int getInputCount() const { return inputs.size(); }
     444
     445        /// @return the number of output connections (including possible self-connections)
     446        int getOutputsCount() const;
     447
     448        /** @return the Neuro connected as i-th input */
     449        Neuro* getInput(int i) const { return (i >= inputs.size()) ? 0 : inputs(i).n; }
     450        /** @return the Neuro connected as i-th input.
     451                @param weight
     452                */
     453        Neuro* getInput(int i, double &weight) const;
     454        /** @return connectin weight for i-th input */
     455        double getInputWeight(int i) const;
     456        /** change connection weight for i-th input */
     457        void setInputWeight(int i, double weight);
     458        /** connect i-th input with another neuron */
     459        void setInput(int i, Neuro*n);
     460        /** connect i-th input with another neuron */
     461        void setInput(int i, Neuro*n, double weight);
     462        /** add new input. @return its reference number */
     463        int addInput(Neuro* child, double weight = 1.0, const SString* info = 0);
     464        /** @return reference number [0..InputCount-1] of the input
     465           or -1 if 'child' is not connected with this Neuro.*/
     466        int findInput(Neuro* child) const;
     467        void removeInput(paInt refno);
     468        /**    @return reference number of the child connection, like findInput() */
     469        int removeInput(Neuro* child);
     470
     471        int findInputs(SList& result, const char* classname = 0, const Part* part = 0, const Joint* joint = 0) const;
     472        int findOutputs(SList& result, const char* classname = 0, const Part* part = 0, const Joint* joint = 0) const;
     473
     474        /* class database retrieval */
     475        static int getClassCount();
     476        /** @return Neuro class name.
     477                @param classindex 0 .. getClassCount()
     478                */
     479        static SString getClassName(int classindex);
     480        static NeuroClass* getClass(int classindex);
     481        static NeuroClass* getClass(const SString& classname);
     482        static int getClassIndex(const NeuroClass*nc);
    469483
    470484#ifdef MODEL_V1_COMPATIBLE
    471 friend class OldItems;
    472 paInt neuro_refno; ///< parent ref# (called neuro_refno for compatibility with old Neuro class), @see moredata
    473 paInt conn_refno; ///< the other neuron ref# in N-N connections, can be used by some other items
    474 double weight; ///< weight of the N-N connection and (all?) receptors
    475 double inertia,force,sigmo; //!!!
    476 
    477 /** @deprecated provided only for compatibility with old Neuro/NeuroItem classes.
    478     use getInputCount() instead. @sa getInputCount() */
    479 int getItemCount() {return oldItems().getItemCount();}
    480 
    481 /** @deprecated provided only for compatibility with old Neuro/NeuroItem classes.
    482     use getInput() instead. @sa getInput() */
    483 NeuroItem* getNeuroItem(int i) {return oldItems().getNeuroItem(i);}
     485        friend class OldItems;
     486        paInt neuro_refno; ///< parent ref# (called neuro_refno for compatibility with old Neuro class), @see moredata
     487        paInt conn_refno; ///< the other neuron ref# in N-N connections, can be used by some other items
     488        double weight; ///< weight of the N-N connection and (all?) receptors
     489        double inertia,force,sigmo; //!!!
     490
     491        /** @deprecated provided only for compatibility with old Neuro/NeuroItem classes.
     492                use getInputCount() instead. @sa getInputCount() */
     493        int getItemCount() {return oldItems().getItemCount();}
     494
     495        /** @deprecated provided only for compatibility with old Neuro/NeuroItem classes.
     496                use getInput() instead. @sa getInput() */
     497        NeuroItem* getNeuroItem(int i) {return oldItems().getNeuroItem(i);}
    484498#endif
    485499
    486   protected:
     500protected:
    487501#ifdef MODEL_V1_COMPATIBLE
    488 /** old Neuro compatibility */
    489 OldItems* olditems;
    490 OldItems& oldItems() {if (!olditems) olditems=new OldItems(*this); return *olditems;}
    491 void invalidateOldItems() {if (olditems) olditems->freelist();}
     502        /** old Neuro compatibility */
     503        OldItems* olditems;
     504        OldItems& oldItems() {if (!olditems) olditems=new OldItems(*this); return *olditems;}
     505        void invalidateOldItems() {if (olditems) olditems->freelist();}
    492506#endif
    493507
    494   public:
    495 
    496 // not really private, but you should not access those directly
    497 double state;
    498 
    499 /** may reference parent neuron if parentcount is exacty 1. parent is invalid otherwise. @sa parentcount */
    500 Neuro *parent;
    501 int parentcount; ///< @sa parent
    502 
    503 Part *part;     ///< link to the Part
    504 Joint *joint;   ///< link to the Joint - required by some objects (eg.muscles)
    505 Orient o;       ///< rotation matrix calculated from "rot"
    506 static ParamEntry emptyParamTab[];
    507 static Param& getStaticParam();
     508public:
     509
     510        // not really private, but you should not access those directly
     511        double state;
     512
     513        /** may reference parent neuron if parentcount is exacty 1. parent is invalid otherwise. @sa parentcount */
     514        Neuro *parent;
     515        int parentcount; ///< @sa parent
     516
     517        Part *part;     ///< link to the Part
     518        Joint *joint;   ///< link to the Joint - required by some objects (eg.muscles)
     519        Orient o;       ///< rotation matrix calculated from "rot"
     520        static ParamEntry emptyParamTab[];
     521        static Param& getStaticParam();
    508522};
    509523
     
    515529{
    516530public:
    517 NeuroItem() {}
     531        NeuroItem() {}
    518532};
    519533#endif
    520534
    521 class NeuroExt: public Neuro
    522 {
    523   public:
     535class NeuroExt : public Neuro
     536{
     537public:
    524538#define STATRICKCLASS NeuroExt
    525 PARAMGETDEF(neuroclass);
    526 PARAMSETDEF(neuroclass);
     539        PARAMGETDEF(neuroclass);
     540        PARAMSETDEF(neuroclass);
    527541#undef STATRICKCLASS
    528 static ParamEntry *getParamTab();
     542        static ParamEntry *getParamTab();
    529543};
    530544
    531545class NeuroConn
    532546{
    533 void defassign();
    534   public:
    535 int n1_refno,n2_refno;
    536 double weight;
    537 SString info;
    538 NeuroConn();
    539 };
    540 
    541 extern ParamEntry f0_part_paramtab[],f0_joint_paramtab[],f0_nodeltajoint_paramtab[],f0_neuro_paramtab[],f0_neuroconn_paramtab[],f0_neuroitem_paramtab[];
     547        void defassign();
     548public:
     549        int n1_refno, n2_refno;
     550        double weight;
     551        SString info;
     552        NeuroConn();
     553};
     554
     555extern ParamEntry f0_part_paramtab[], f0_joint_paramtab[], f0_nodeltajoint_paramtab[], f0_neuro_paramtab[], f0_neuroconn_paramtab[], f0_neuroitem_paramtab[];
    542556
    543557#endif
  • cpp/frams/neuro/geneticneuroparam.cpp

    r286 r714  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2017  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    66#include <frams/neuro/neurolibrary.h>
    77
    8 GeneticNeuroParam::GeneticNeuroParam(const char* groupname,const char* myname,
    9                                    const char* prefix,const char* typ)
    10         :NeuroLibParam(groupname,myname,prefix),types(typ)
     8GeneticNeuroParam::GeneticNeuroParam(const char* groupname, const char* myname,
     9        const char* prefix, const char* typ)
     10        :NeuroLibParam(groupname, myname, prefix), types(typ)
    1111{}
    1212
    1313paInt GeneticNeuroParam::getInt(int i)
    14 {return Neuro::getClass(i)->genactive;}
     14{
     15        return Neuro::getClass(i)->genactive ? 1 : 0;
     16}
    1517
    16 int GeneticNeuroParam::setInt(int i,paInt v)
    17 {Neuro::getClass(i)->genactive=v;return PSET_CHANGED;}
     18int GeneticNeuroParam::setInt(int i, paInt v)
     19{
     20        Neuro::getClass(i)->genactive = (v != 0); return PSET_CHANGED;
     21}
    1822
  • cpp/frams/neuro/geneticneuroparam.h

    r286 r714  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2017  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    88#include "neurolibparam.h"
    99
    10 class GeneticNeuroParam: public NeuroLibParam
     10class GeneticNeuroParam : public NeuroLibParam
    1111{
    12   protected:
    13 const char* types;
    14   public:
    15 GeneticNeuroParam(const char* groupname,const char* myname, const char* prefix,const char* typ=0);
     12protected:
     13        const char* types;
     14public:
     15        GeneticNeuroParam(const char* groupname, const char* myname, const char* prefix, const char* typ = 0);
    1616
    17 paInt getInt(int i);
    18 int setInt(int i,paInt v);
    19 const char *type(int i) {return types?types:NeuroLibParam::type(i);}
     17        paInt getInt(int i);
     18        int setInt(int i, paInt v);
     19        const char *type(int i) { return types ? types : NeuroLibParam::type(i); }
    2020};
    2121
Note: See TracChangeset for help on using the changeset viewer.