[286] | 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
| 2 | // Copyright (C) 1999-2015 Maciej Komosinski and Szymon Ulatowski. |
---|
| 3 | // See LICENSE.txt for details. |
---|
[109] | 4 | |
---|
| 5 | #include <stdlib.h> |
---|
| 6 | #include <math.h> |
---|
| 7 | #include <stdio.h> |
---|
| 8 | #include <string.h> |
---|
| 9 | #include <ctype.h> |
---|
| 10 | #include <time.h> |
---|
| 11 | #include <errno.h> |
---|
| 12 | |
---|
| 13 | /////////////////////////////// MODELPARTS.CPP /////////////// |
---|
| 14 | |
---|
| 15 | #include "modelparts.h" |
---|
| 16 | #include "model.h" |
---|
| 17 | |
---|
| 18 | #include <common/nonstd.h> |
---|
| 19 | #include <frams/param/param.h> |
---|
| 20 | #include <frams/neuro/neurolibrary.h> |
---|
| 21 | #include <frams/util/multirange.h> |
---|
| 22 | #include <frams/util/extvalue.h> |
---|
| 23 | #include <frams/param/paramobj.h> |
---|
| 24 | |
---|
| 25 | #include F0_DEFASSIGN_FILE |
---|
| 26 | |
---|
[288] | 27 | #ifndef SDK_WITHOUT_FRAMS |
---|
[109] | 28 | #include <frams/neuro/neuroclsobject.h> |
---|
| 29 | #endif |
---|
| 30 | |
---|
| 31 | template<> |
---|
| 32 | char ModelUserTags::reg[5]={0}; |
---|
| 33 | |
---|
| 34 | ///////////////////////// |
---|
| 35 | |
---|
| 36 | PartBase::~PartBase() |
---|
| 37 | {if (mapped) delete mapped;} |
---|
| 38 | |
---|
| 39 | void PartBase::notifyMappingChange() |
---|
| 40 | { |
---|
| 41 | if (owner) owner->partmappingchanged=1; |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | void PartBase::setMapping(const IRange &r) |
---|
| 45 | { |
---|
| 46 | if (mapped) (*mapped)=r; |
---|
| 47 | else mapped=new MultiRange(r); |
---|
| 48 | notifyMappingChange(); |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | void PartBase::clearMapping() |
---|
| 52 | { |
---|
| 53 | if (mapped) {delete mapped; mapped=0;} |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | void PartBase::addMapping(const IRange &r) |
---|
| 57 | { |
---|
| 58 | if (mapped) mapped->add(r); |
---|
| 59 | else mapped=new MultiRange(r); |
---|
| 60 | notifyMappingChange(); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | void PartBase::setMapping(const MultiRange &mr) |
---|
| 64 | { |
---|
| 65 | if (mapped) (*mapped)=mr; |
---|
| 66 | else mapped=new MultiRange(mr); |
---|
| 67 | notifyMappingChange(); |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | void PartBase::addMapping(const MultiRange &mr) |
---|
| 71 | { |
---|
| 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)); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | SString PartBase::getInfo(const SString& name) |
---|
| 93 | { |
---|
| 94 | return strGetField(info,name); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | ///////////////////////// |
---|
| 98 | |
---|
| 99 | NeuroClass::NeuroClass(ParamEntry *_props,SString _description, |
---|
| 100 | int _prefinputs,int _prefoutput,int _preflocation, |
---|
| 101 | int *_vectordata,bool own_vd,int vhints) |
---|
| 102 | :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) |
---|
| 110 | {} |
---|
| 111 | |
---|
| 112 | NeuroClass::~NeuroClass() |
---|
| 113 | { |
---|
| 114 | setSymbolGlyph(0,0); |
---|
| 115 | if (props && ownedprops) |
---|
| 116 | ParamObject::freeParamTab(props); |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | NeuroClass::NeuroClass() |
---|
| 120 | :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) |
---|
| 126 | {} |
---|
| 127 | |
---|
| 128 | SString NeuroClass::getSummary() |
---|
| 129 | { |
---|
| 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; |
---|
| 157 | 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 | switch(*p.type(i)) |
---|
| 163 | { |
---|
| 164 | case 'd': t+=" integer"; |
---|
[637] | 165 | {paInt a,b,c; int n=p.getMinMax(i,a,b,c); if (n>=2) t+=SString::sprintf(" %d..%d",a,b); if (n>=3) t+=SString::sprintf(" (default %d)",c);} |
---|
[109] | 166 | break; |
---|
| 167 | case 'f': t+=" float"; |
---|
[637] | 168 | {double a,b,c; int n=p.getMinMax(i,a,b,c); if (n>=2) t+=SString::sprintf(" %g..%g",a,b); if (n>=3) t+=SString::sprintf(" (default %g)",c);} |
---|
[109] | 169 | break; |
---|
[637] | 170 | case 's': t+=" string"; |
---|
| 171 | {int a,b; SString c; int n=p.getMinMax(i,a,b,c); if ((n>=2)&&(b>0)) t+=SString::sprintf(", max %d chars",b); if (n>=3) t+=SString::sprintf(" (default \"%s\")",c.c_str());} |
---|
| 172 | break; |
---|
[109] | 173 | case 'x': t+=" anything"; break; |
---|
| 174 | } |
---|
| 175 | if (h=p.help(i)) if (*h) {t+=" - "; t+=h;} |
---|
| 176 | } |
---|
| 177 | } |
---|
| 178 | return t; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | ///////////////////////// |
---|
| 182 | |
---|
| 183 | ///////////////////////////////////// |
---|
| 184 | |
---|
| 185 | Neuro::Neuro(double _state,double _inertia,double _force,double _sigmo) |
---|
| 186 | :PartBase(getDefaultStyle()),state(_state) |
---|
| 187 | #ifdef MODEL_V1_COMPATIBLE |
---|
| 188 | ,inertia(_inertia),force(_force),sigmo(_sigmo) |
---|
| 189 | #endif |
---|
| 190 | { |
---|
| 191 | #ifdef MODEL_V1_COMPATIBLE |
---|
| 192 | olditems=0; |
---|
| 193 | #endif |
---|
| 194 | flags=0; |
---|
| 195 | myclass=0; |
---|
| 196 | knownclass=1; |
---|
| 197 | part_refno=-1; joint_refno=-1; |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | Neuro::Neuro(void):PartBase(getDefaultStyle()) |
---|
| 201 | { |
---|
| 202 | defassign(); |
---|
| 203 | state=0.0; |
---|
| 204 | myclass=NULL; |
---|
| 205 | myclassname="N";//default d="N" but f0.def is unable to set this (d is GETSET, not a regular FIELD) |
---|
| 206 | knownclass=0; |
---|
| 207 | refno=0; |
---|
| 208 | pos=Pt3D_0; rot=Pt3D_0; |
---|
| 209 | parent=0; part=0; joint=0; |
---|
| 210 | parentcount=0; |
---|
| 211 | #ifdef MODEL_V1_COMPATIBLE |
---|
| 212 | olditems=0; |
---|
| 213 | #endif |
---|
| 214 | flags=0; |
---|
| 215 | part_refno=-1; joint_refno=-1; |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | |
---|
| 219 | Neuro::~Neuro() |
---|
| 220 | { |
---|
| 221 | #ifdef MODEL_V1_COMPATIBLE |
---|
| 222 | if (olditems) delete olditems; |
---|
| 223 | #endif |
---|
| 224 | int i; |
---|
| 225 | for(i=0;i<inputs.size();i++) |
---|
| 226 | { |
---|
| 227 | NInput &ni=inputs(i); |
---|
| 228 | if (ni.info) delete ni.info; |
---|
| 229 | } |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | SString** Neuro::inputInfo(int i) |
---|
| 233 | { |
---|
| 234 | if (i>=getInputCount()) return 0; |
---|
| 235 | return &inputs(i).info; |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | void Neuro::setInputInfo(int i,const SString& name,const SString &value) |
---|
| 239 | { |
---|
| 240 | SString **s=inputInfo(i); |
---|
| 241 | if (!s) return; |
---|
| 242 | if (!*s) *s=new SString(); |
---|
| 243 | strSetField(**s,name,value); |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | void Neuro::setInputInfo(int i,const SString& name,int value) |
---|
| 247 | { |
---|
| 248 | setInputInfo(i,name,SString::valueOf(value)); |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | void Neuro::setInputInfo(int i,const SString& name,double value) |
---|
| 252 | { |
---|
| 253 | setInputInfo(i,name,SString::valueOf(value)); |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | SString Neuro::getInputInfo(int i) |
---|
| 257 | { |
---|
| 258 | SString **s=inputInfo(i); |
---|
| 259 | if (!s) return SString(); |
---|
| 260 | if (!*s) return SString(); |
---|
| 261 | return **s; |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | SString Neuro::getInputInfo(int i,const SString& name) |
---|
| 265 | { |
---|
| 266 | SString **s=inputInfo(i); |
---|
| 267 | if (!s) return SString(); |
---|
| 268 | if (!*s) return SString(); |
---|
| 269 | return strGetField(**s,name); |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | void Neuro::operator=(const Neuro& src) |
---|
| 273 | { |
---|
| 274 | refno=src.refno; |
---|
| 275 | #ifdef MODEL_V1_COMPATIBLE |
---|
| 276 | neuro_refno=-1; |
---|
| 277 | conn_refno=-1; |
---|
| 278 | force=src.force; |
---|
| 279 | sigmo=src.sigmo; |
---|
| 280 | inertia=src.inertia; |
---|
| 281 | weight=src.weight; |
---|
| 282 | olditems=0; |
---|
| 283 | #endif |
---|
| 284 | state=src.state; |
---|
| 285 | part_refno=-1; |
---|
| 286 | joint_refno=-1; |
---|
| 287 | pos=src.pos; rot=src.rot; |
---|
| 288 | parent=0; part=0; joint=0; |
---|
| 289 | parentcount=0; |
---|
| 290 | flags=0; |
---|
| 291 | myclass=src.myclass; |
---|
| 292 | knownclass=src.knownclass; |
---|
| 293 | myclassname=src.myclassname; |
---|
| 294 | myclassparams=src.myclassparams; |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | void Neuro::attachToPart(int i) |
---|
| 298 | {attachToPart((i>=0)?owner->getPart(i):0);} |
---|
| 299 | |
---|
| 300 | void Neuro::attachToJoint(int i) |
---|
| 301 | {attachToJoint((i>=0)?owner->getJoint(i):0);} |
---|
| 302 | |
---|
| 303 | int Neuro::getClassCount() |
---|
| 304 | {return NeuroLibrary::staticlibrary.getClassCount();} |
---|
| 305 | |
---|
| 306 | NeuroClass* Neuro::getClass(int classindex) |
---|
| 307 | {return NeuroLibrary::staticlibrary.getClass(classindex);} |
---|
| 308 | |
---|
| 309 | NeuroClass* Neuro::getClass(const SString& classname) |
---|
| 310 | {return NeuroLibrary::staticlibrary.findClass(classname);} |
---|
| 311 | |
---|
| 312 | int Neuro::getClassIndex(const NeuroClass*nc) |
---|
| 313 | {return NeuroLibrary::staticlibrary.classes.find((void*)nc);} |
---|
| 314 | |
---|
| 315 | NeuroClass* Neuro::getClass() |
---|
| 316 | { |
---|
| 317 | checkClass(); |
---|
| 318 | return myclass; |
---|
| 319 | } |
---|
| 320 | |
---|
| 321 | void Neuro::setClass(NeuroClass* cl) |
---|
| 322 | { |
---|
| 323 | myclass=cl; |
---|
| 324 | myclassname=cl->getName(); |
---|
| 325 | knownclass=1; |
---|
| 326 | } |
---|
| 327 | |
---|
| 328 | SString Neuro::getClassName(int classindex) |
---|
| 329 | { |
---|
| 330 | NeuroClass *cl=NeuroLibrary::staticlibrary.getClass(classindex); |
---|
| 331 | return cl? cl->getName() : SString(); |
---|
| 332 | } |
---|
| 333 | |
---|
| 334 | void Neuro::setDetails(const SString& details) |
---|
| 335 | { |
---|
| 336 | int colon=details.indexOf(':'); |
---|
| 337 | if (colon>=0) {myclassname=details.substr(0,colon); myclassparams=details.substr(colon+1);} |
---|
| 338 | else {myclassname=details; myclassparams=0;} |
---|
| 339 | knownclass=0; |
---|
| 340 | } |
---|
| 341 | |
---|
| 342 | SString Neuro::getDetails() |
---|
| 343 | { |
---|
| 344 | SString ret=getClassName(); |
---|
| 345 | if (myclassparams.len()) {if (!ret.len()) ret="N"; ret+=":"; ret+=myclassparams;} |
---|
| 346 | return ret; |
---|
| 347 | } |
---|
| 348 | |
---|
| 349 | void Neuro::checkClass() |
---|
| 350 | { |
---|
| 351 | if (knownclass) return; |
---|
| 352 | myclass=getClass(myclassname); |
---|
| 353 | knownclass=1; |
---|
| 354 | } |
---|
| 355 | |
---|
| 356 | SyntParam Neuro::classProperties(bool handle_defaults_when_saving) |
---|
| 357 | { |
---|
| 358 | NeuroClass *cl=getClass(); |
---|
| 359 | ParamEntry *pe = cl ? cl->getParamTab() : emptyParamTab; |
---|
| 360 | return SyntParam(pe,&myclassparams,handle_defaults_when_saving); |
---|
| 361 | } |
---|
| 362 | |
---|
| 363 | SString Neuro::getClassName() |
---|
| 364 | { |
---|
| 365 | return myclassname; |
---|
| 366 | } |
---|
| 367 | |
---|
| 368 | void Neuro::setClassName(const SString& clazz) |
---|
| 369 | { |
---|
| 370 | myclassname=clazz; |
---|
| 371 | knownclass=0; |
---|
| 372 | } |
---|
| 373 | |
---|
| 374 | int Neuro::addInput(Neuro* child,double weight,const SString *info) |
---|
| 375 | { |
---|
| 376 | inputs+=NInput(child,weight,(info&&(info->len()))?new SString(*info):0); |
---|
| 377 | child->parentcount++; |
---|
| 378 | if (child->parentcount==1) {child->parent=this;} |
---|
| 379 | return inputs.size()-1; |
---|
| 380 | } |
---|
| 381 | |
---|
| 382 | int Neuro::findInput(Neuro* child) const |
---|
| 383 | { |
---|
| 384 | for(int i=0;i<inputs.size();i++) |
---|
| 385 | if (inputs(i).n==child) return i; |
---|
| 386 | return -1; |
---|
| 387 | } |
---|
| 388 | |
---|
| 389 | Neuro* Neuro::getInput(int i,double &weight) const |
---|
| 390 | { |
---|
| 391 | if (i>=getInputCount()) return 0; |
---|
| 392 | NInput &inp=inputs(i); |
---|
| 393 | weight=inp.weight; |
---|
| 394 | return inp.n; |
---|
| 395 | } |
---|
| 396 | |
---|
| 397 | double Neuro::getInputWeight(int i) const |
---|
| 398 | { |
---|
| 399 | return inputs(i).weight; |
---|
| 400 | } |
---|
| 401 | |
---|
| 402 | void Neuro::setInputWeight(int i,double w) |
---|
| 403 | { |
---|
| 404 | inputs(i).weight=w; |
---|
| 405 | } |
---|
| 406 | |
---|
| 407 | void Neuro::setInput(int i,Neuro* n) |
---|
| 408 | { |
---|
| 409 | NInput &inp=inputs(i); |
---|
| 410 | inp.n=n; |
---|
| 411 | } |
---|
| 412 | |
---|
| 413 | void Neuro::setInput(int i,Neuro* n,double w) |
---|
| 414 | { |
---|
| 415 | NInput &inp=inputs(i); |
---|
| 416 | inp.n=n; |
---|
| 417 | inp.weight=w; |
---|
| 418 | } |
---|
| 419 | |
---|
| 420 | void Neuro::removeInput(int refno) |
---|
| 421 | { |
---|
| 422 | Neuro *child=getInput(refno); |
---|
| 423 | child->parentcount--; |
---|
| 424 | if (child->parent==this) child->parent=0; |
---|
| 425 | SString *s=inputs(refno).info; |
---|
| 426 | if (s) delete s; |
---|
| 427 | inputs.remove(refno); |
---|
| 428 | } |
---|
| 429 | |
---|
| 430 | int Neuro::removeInput(Neuro* child) |
---|
| 431 | { |
---|
| 432 | int i=findInput(child); |
---|
| 433 | if (i>=0) removeInput(i); |
---|
| 434 | return i; |
---|
| 435 | } |
---|
| 436 | |
---|
| 437 | int Neuro::getOutputsCount() const |
---|
| 438 | { |
---|
| 439 | int c=0; |
---|
| 440 | for(int i=0;i<owner->getNeuroCount();i++) |
---|
| 441 | for(int j=0;j<owner->getNeuro(i)->getInputCount();j++) c+=owner->getNeuro(i)->getInput(j)==this; |
---|
| 442 | return c; |
---|
| 443 | } |
---|
| 444 | |
---|
| 445 | int Neuro::isOldEffector() |
---|
| 446 | { |
---|
| 447 | static SString bend("|"),rot("@"); |
---|
| 448 | return ((getClassName()==bend)||(getClassName()==rot)); |
---|
| 449 | } |
---|
| 450 | |
---|
| 451 | int Neuro::isOldReceptor() |
---|
| 452 | { |
---|
| 453 | static SString g("G"),t("T"),s("S"); |
---|
| 454 | return ((getClassName()==g)||(getClassName()==t)||(getClassName()==s)); |
---|
| 455 | } |
---|
| 456 | |
---|
| 457 | int Neuro::isOldNeuron() |
---|
| 458 | { |
---|
| 459 | static SString n("N"); |
---|
| 460 | return (getClassName()==n); |
---|
| 461 | } |
---|
| 462 | |
---|
| 463 | int Neuro::isNNConnection() |
---|
| 464 | { |
---|
| 465 | static SString conn("-"); |
---|
| 466 | return (getClassName()==conn); |
---|
| 467 | } |
---|
| 468 | |
---|
| 469 | int Neuro::findInputs(SList& result,const char* classname,const Part* part,const Joint* joint) const |
---|
| 470 | { |
---|
| 471 | Neuro *nu; |
---|
| 472 | SString cn(classname); |
---|
| 473 | int n0=result.size(); |
---|
| 474 | for(int i=0;nu=getInput(i);i++) |
---|
| 475 | { |
---|
| 476 | if (part) |
---|
| 477 | if (nu->part != part) continue; |
---|
| 478 | if (joint) |
---|
| 479 | if (nu->joint != joint) continue; |
---|
| 480 | if (classname) |
---|
| 481 | if (nu->getClassName() != cn) continue; |
---|
| 482 | result+=(void*)nu; |
---|
| 483 | } |
---|
| 484 | return result.size()-n0; |
---|
| 485 | } |
---|
| 486 | |
---|
| 487 | int Neuro::findOutputs(SList& result,const char* classname,const Part* part,const Joint* joint) const |
---|
| 488 | { // not very efficient... |
---|
| 489 | Neuro *nu,*inp; |
---|
| 490 | SString cn(classname); |
---|
| 491 | SList found; |
---|
| 492 | int n0=result.size(); |
---|
| 493 | for(int i=0;nu=getModel().getNeuro(i);i++) |
---|
| 494 | { |
---|
| 495 | if (part) |
---|
| 496 | if (nu->part != part) continue; |
---|
| 497 | if (joint) |
---|
| 498 | if (nu->joint != joint) continue; |
---|
| 499 | if (classname) |
---|
| 500 | if (inp->getClassName() != cn) continue; |
---|
| 501 | for (int j=0;inp=nu->getInput(j);j++) |
---|
| 502 | if (inp==this) |
---|
| 503 | { |
---|
| 504 | result+=(void*)nu; |
---|
| 505 | break; |
---|
| 506 | } |
---|
| 507 | } |
---|
| 508 | return result.size()-n0; |
---|
| 509 | } |
---|
| 510 | |
---|
| 511 | void Neuro::get_inputCount(PARAMGETARGS) |
---|
| 512 | {ret->setInt(inputs.size());} |
---|
| 513 | |
---|
| 514 | void Neuro::p_getInputNeuroDef(ExtValue *args,ExtValue *ret) |
---|
| 515 | { |
---|
| 516 | int i=args->getInt(); |
---|
| 517 | if ((i<0)||(i>=inputs.size())) |
---|
| 518 | ret->setEmpty(); |
---|
| 519 | else |
---|
| 520 | ret->setObject(ExtObject(&Neuro::getStaticParam(),inputs(i).n)); |
---|
| 521 | } |
---|
| 522 | |
---|
| 523 | void Neuro::p_getInputWeight(ExtValue *args,ExtValue *ret) |
---|
| 524 | { |
---|
| 525 | int i=args->getInt(); |
---|
| 526 | if ((i<0)||(i>=inputs.size())) |
---|
| 527 | ret->setEmpty(); |
---|
| 528 | else |
---|
| 529 | ret->setDouble(inputs(i).weight); |
---|
| 530 | } |
---|
| 531 | |
---|
| 532 | void Neuro::p_getInputNeuroIndex(ExtValue *args,ExtValue *ret) |
---|
| 533 | { |
---|
| 534 | int i=args->getInt(); |
---|
| 535 | if ((i<0)||(i>=inputs.size())) |
---|
| 536 | ret->setInt(-1); |
---|
| 537 | else |
---|
| 538 | ret->setInt(inputs(i).n->refno); |
---|
| 539 | } |
---|
| 540 | |
---|
| 541 | void Neuro::get_classObject(PARAMGETARGS) |
---|
| 542 | { |
---|
[288] | 543 | #ifndef SDK_WITHOUT_FRAMS |
---|
[109] | 544 | NeuroClassExt::makeStaticObject(ret,getClass()); |
---|
| 545 | #endif |
---|
| 546 | } |
---|
| 547 | |
---|
| 548 | /////// old items |
---|
| 549 | #ifdef MODEL_V1_COMPATIBLE |
---|
| 550 | void OldItems::buildlist() |
---|
| 551 | { // guaranteed to work only for old NN layouts |
---|
| 552 | // (neurons,neuro connections, old receptors and effectors) |
---|
| 553 | if (listok) return; |
---|
| 554 | // inputs can contain both neuroitem connections (details="") and direct neuron references (details="N") |
---|
| 555 | // in OldItems we create neuroitems freom direct references |
---|
| 556 | for(int i=0;i<neuro.getInputCount();i++) |
---|
| 557 | { |
---|
| 558 | float w; |
---|
| 559 | Neuro *n=neuro.getInput(i,w); |
---|
| 560 | if (n->isOldNeuron()) |
---|
| 561 | { |
---|
| 562 | Neuro *ni=new Neuro(); |
---|
| 563 | ni->setClassName("-"); |
---|
| 564 | ni->weight=w; |
---|
| 565 | ni->neuro_refno=neuro.refno; |
---|
| 566 | ni->conn_refno=n->refno; |
---|
| 567 | items+=ni; |
---|
| 568 | syntitems+=ni; |
---|
| 569 | } |
---|
| 570 | else |
---|
| 571 | { |
---|
| 572 | items+=n; |
---|
| 573 | n->weight=w; |
---|
| 574 | } |
---|
| 575 | } |
---|
| 576 | SList outputs; |
---|
| 577 | neuro.findOutputs(outputs); |
---|
| 578 | FOREACH(Neuro*,n,outputs) |
---|
| 579 | { |
---|
| 580 | if (n->isNNConnection() || n->isOldNeuron()) |
---|
| 581 | outputs-=n; |
---|
| 582 | } |
---|
| 583 | items+=outputs; |
---|
| 584 | listok=1; |
---|
| 585 | } |
---|
| 586 | |
---|
| 587 | void OldItems::freelist() |
---|
| 588 | { |
---|
| 589 | FOREACH(Neuro*,n,syntitems) |
---|
| 590 | delete n; |
---|
| 591 | syntitems.clear(); |
---|
| 592 | items.clear(); |
---|
| 593 | listok=0; |
---|
| 594 | } |
---|
| 595 | |
---|
| 596 | int OldItems::getItemCount() |
---|
| 597 | { |
---|
| 598 | buildlist(); |
---|
| 599 | return items.size(); |
---|
| 600 | } |
---|
| 601 | |
---|
| 602 | NeuroItem *OldItems::getNeuroItem(int i) |
---|
| 603 | { |
---|
| 604 | buildlist(); |
---|
| 605 | return (NeuroItem*)items(i); |
---|
| 606 | } |
---|
| 607 | |
---|
| 608 | NeuroItem *OldItems::addNewNeuroItem() |
---|
| 609 | { |
---|
| 610 | Neuro *nu=neuro.getModel().addNewNeuro(); |
---|
| 611 | nu->setClassName("-"); |
---|
| 612 | if (listok) items+=nu; |
---|
| 613 | neuro.addInput(nu); |
---|
| 614 | return (NeuroItem*)nu; |
---|
| 615 | } |
---|
| 616 | |
---|
| 617 | int OldItems::findNeuroItem(NeuroItem *ni) |
---|
| 618 | { |
---|
| 619 | buildlist(); |
---|
| 620 | return items.find((void*)ni); |
---|
| 621 | } |
---|
| 622 | #endif |
---|
| 623 | |
---|
| 624 | /////////////////////////////////////// |
---|
| 625 | |
---|
| 626 | SString Part::getDefaultStyle() |
---|
| 627 | {return SString("part");} |
---|
| 628 | SString Joint::getDefaultStyle() |
---|
| 629 | {return SString("joint");} |
---|
| 630 | /* |
---|
| 631 | const SString& Neuro::getDefaultStyle() |
---|
| 632 | {static SString s("neuro"); return s;} |
---|
| 633 | const SString& NeuroItem::getDefaultStyle() |
---|
| 634 | {static SString s("neuroitem"); return s;} |
---|
| 635 | */ |
---|
| 636 | SString Neuro::getDefaultStyle() |
---|
| 637 | {return SString("neuro");} |
---|
| 638 | |
---|
| 639 | Part::Part(enum Shape s):PartBase(getDefaultStyle()) |
---|
| 640 | { |
---|
| 641 | o=Orient_1; |
---|
| 642 | p=Pt3D_0; |
---|
| 643 | rot=Pt3D_0; |
---|
| 644 | flags=0; |
---|
| 645 | defassign(); |
---|
| 646 | shape=s; |
---|
[528] | 647 | mass=1; |
---|
[109] | 648 | } |
---|
| 649 | |
---|
| 650 | void Part::operator=(const Part& src) |
---|
| 651 | { |
---|
| 652 | p=src.p; o=src.o; |
---|
| 653 | flags=src.flags; |
---|
| 654 | mass=src.mass; density=src.density; |
---|
| 655 | friction=src.friction; |
---|
| 656 | ingest=src.ingest; |
---|
| 657 | assim=src.assim; |
---|
| 658 | size=src.size; |
---|
| 659 | rot=src.rot; |
---|
| 660 | refno=src.refno; |
---|
| 661 | vcolor=src.vcolor; |
---|
| 662 | vsize=src.vsize; |
---|
[292] | 663 | vis_style=src.vis_style; |
---|
[109] | 664 | shape=src.shape; |
---|
[292] | 665 | scale=src.scale; |
---|
| 666 | hollow=src.hollow; |
---|
[109] | 667 | } |
---|
| 668 | |
---|
| 669 | void Part::setOrient(const Orient &_o) |
---|
| 670 | { |
---|
| 671 | o=_o; |
---|
| 672 | rot.getAngles(o.x,o.z); |
---|
| 673 | } |
---|
| 674 | |
---|
| 675 | void Part::setRot(const Pt3D &r) |
---|
| 676 | { |
---|
| 677 | rot=r; |
---|
| 678 | o=Orient_1; |
---|
| 679 | o.rotate(rot); |
---|
| 680 | } |
---|
| 681 | |
---|
| 682 | void Part::setPositionAndRotationFromAxis(const Pt3D &p1,const Pt3D &p2) |
---|
| 683 | { |
---|
| 684 | Pt3D x=p2-p1; |
---|
| 685 | Pt3D dir(x.y,x.z,x.x); |
---|
| 686 | p=p1+x*0.5; |
---|
| 687 | rot.getAngles(x,dir); |
---|
| 688 | } |
---|
| 689 | |
---|
| 690 | Param& Part::getStaticParam() |
---|
| 691 | { |
---|
| 692 | static Param p(f0_part_paramtab,0,"Part"); |
---|
| 693 | return p; |
---|
| 694 | } |
---|
| 695 | |
---|
| 696 | |
---|
| 697 | /////////////////////////// |
---|
| 698 | |
---|
| 699 | Joint::Joint():PartBase(getDefaultStyle()) |
---|
| 700 | { |
---|
| 701 | rot=Pt3D_0; |
---|
| 702 | defassign(); |
---|
| 703 | d.x=JOINT_DELTA_MARKER; |
---|
[114] | 704 | d.y=JOINT_DELTA_MARKER; |
---|
| 705 | d.z=JOINT_DELTA_MARKER; |
---|
[109] | 706 | part1=0; part2=0; |
---|
| 707 | flags=0; |
---|
| 708 | usedelta=0; |
---|
| 709 | } |
---|
| 710 | |
---|
| 711 | void Joint::operator=(const Joint& src) |
---|
| 712 | { |
---|
| 713 | rot=src.rot; |
---|
| 714 | d=src.d; |
---|
[292] | 715 | shape=src.shape; |
---|
[109] | 716 | stamina=src.stamina; |
---|
| 717 | stif=src.stif; rotstif=src.rotstif; |
---|
[292] | 718 | vis_style=src.vis_style; |
---|
| 719 | vcolor=src.vcolor; |
---|
[109] | 720 | part1=0; part2=0; |
---|
| 721 | flags=src.flags; |
---|
| 722 | usedelta=src.usedelta; |
---|
| 723 | refno=src.refno; |
---|
| 724 | } |
---|
| 725 | |
---|
| 726 | void Joint::attachToParts(Part* p1,Part* p2) |
---|
| 727 | { |
---|
| 728 | part1=p1; |
---|
| 729 | part2=p2; |
---|
| 730 | if (p1 && p2) |
---|
| 731 | { |
---|
| 732 | o=rot; |
---|
| 733 | if (usedelta) |
---|
| 734 | { |
---|
| 735 | p1->o.transform(p2->o,o); |
---|
| 736 | // p2->o.x=p1->o/o.x; p2->o.y=p1->o/o.y; p2->o.z=p1->o/o.z; |
---|
| 737 | p2->p=p2->o.transform(d)+p1->p; |
---|
| 738 | } |
---|
| 739 | } |
---|
| 740 | } |
---|
| 741 | |
---|
| 742 | void Joint::attachToParts(int p1,int p2) |
---|
| 743 | { |
---|
| 744 | attachToParts((p1>=0)?owner->getPart(p1):0,(p2>=0)?owner->getPart(p2):0); |
---|
| 745 | } |
---|
| 746 | |
---|
| 747 | void Joint::resetDelta() |
---|
| 748 | { |
---|
[114] | 749 | d=Pt3D(JOINT_DELTA_MARKER,JOINT_DELTA_MARKER,JOINT_DELTA_MARKER); |
---|
[109] | 750 | } |
---|
| 751 | |
---|
[114] | 752 | void Joint::resetDeltaMarkers() |
---|
[109] | 753 | { |
---|
[114] | 754 | if (d.x==JOINT_DELTA_MARKER) d.x=0; |
---|
| 755 | if (d.y==JOINT_DELTA_MARKER) d.y=0; |
---|
| 756 | if (d.z==JOINT_DELTA_MARKER) d.z=0; |
---|
[109] | 757 | } |
---|
| 758 | |
---|
[114] | 759 | void Joint::useDelta(bool use) |
---|
[109] | 760 | { |
---|
[114] | 761 | usedelta=use; |
---|
| 762 | } |
---|
| 763 | |
---|
| 764 | bool Joint::isDelta() |
---|
| 765 | { |
---|
[109] | 766 | return usedelta; |
---|
| 767 | } |
---|
| 768 | |
---|
| 769 | Param& Joint::getStaticParam() |
---|
| 770 | { |
---|
| 771 | static Param p(f0_joint_paramtab,0,"Joint"); |
---|
| 772 | return p; |
---|
| 773 | } |
---|
| 774 | |
---|
| 775 | |
---|
| 776 | ///////////////////////////////////////////////////////////////// |
---|
| 777 | ///////////////////////////////////////////////////////////////// |
---|
| 778 | |
---|
| 779 | #include F0_CLASSES_FILE |
---|
| 780 | |
---|
| 781 | #ifdef MODEL_V1_COMPATIBLE |
---|
| 782 | |
---|
| 783 | #define FIELDSTRUCT Neuro |
---|
| 784 | ParamEntry f0_old_neuro_tab[]= |
---|
| 785 | { |
---|
| 786 | {"Connections",2,6,"n",}, |
---|
| 787 | {"Other properties",}, |
---|
| 788 | {"p",0,0,"part ref#","d",FIELD(part_refno),}, |
---|
| 789 | {"j",0,0,"joint ref#","d",FIELD(joint_refno),}, |
---|
| 790 | {"s",1,0,"state","f",FIELD(state),}, |
---|
| 791 | {"in",1,0,"Inertia","f",FIELD(inertia),}, |
---|
| 792 | {"fo",1,0,"Force","f",FIELD(force),}, |
---|
| 793 | {"si",1,0,"Sigmoid","f",FIELD(sigmo),}, |
---|
| 794 | {0,0,0,}, |
---|
| 795 | }; |
---|
| 796 | #undef FIELDSTRUCT |
---|
| 797 | |
---|
| 798 | #define FIELDSTRUCT NeuroItem |
---|
| 799 | ParamEntry f0_neuroitem_paramtab[]= |
---|
| 800 | { |
---|
| 801 | {"Connections",3,12,"ni",}, |
---|
| 802 | {"Geometry",}, |
---|
| 803 | {"Other",}, |
---|
| 804 | {"n",0,0,"neuron ref#","d",FIELD(neuro_refno),}, |
---|
| 805 | {"c",2,0,"connection ref#","d",FIELD(conn_refno),}, |
---|
| 806 | {"w",2,0,"connection weight","f",FIELD(weight),}, |
---|
| 807 | {"p",0,0,"part ref#","d",FIELD(part_refno),}, |
---|
| 808 | {"j",0,0,"joint ref#","d",FIELD(joint_refno),}, |
---|
| 809 | {"px",1,0,"position.x","f",FIELD(pos.x),}, |
---|
| 810 | {"py",1,0,"position.y","f",FIELD(pos.y),}, |
---|
| 811 | {"pz",1,0,"position.z","f",FIELD(pos.z),}, |
---|
| 812 | {"rx",1,0,"rotation.x","f",FIELD(rot.x),}, |
---|
| 813 | {"ry",1,0,"rotation.y","f",FIELD(rot.y),}, |
---|
| 814 | {"rz",1,0,"rotation.z","f",FIELD(rot.z),}, |
---|
| 815 | {"d",2,0,"item details","s",GETSET(details),}, |
---|
| 816 | {0,0,0,}, |
---|
| 817 | }; |
---|
| 818 | #undef FIELDSTRUCT |
---|
| 819 | #endif |
---|
| 820 | |
---|
| 821 | //////////////////////////////////////// |
---|
| 822 | |
---|
| 823 | ParamEntry Neuro::emptyParamTab[]= |
---|
| 824 | { |
---|
| 825 | {"Undefined Neuro",1,0,"?",}, |
---|
| 826 | {0,0,0,}, |
---|
| 827 | }; |
---|
| 828 | |
---|
| 829 | Param Part::extraProperties() |
---|
| 830 | { |
---|
| 831 | return Param(f0_part_xtra_paramtab,this); |
---|
| 832 | } |
---|
| 833 | |
---|
| 834 | Param Joint::extraProperties() |
---|
| 835 | { |
---|
| 836 | return Param(f0_joint_xtra_paramtab,this); |
---|
| 837 | } |
---|
| 838 | |
---|
| 839 | Param Neuro::extraProperties() |
---|
| 840 | { |
---|
| 841 | return Param(f0_neuro_xtra_paramtab,this); |
---|
| 842 | } |
---|
| 843 | |
---|
| 844 | Param Part::properties() |
---|
| 845 | { |
---|
| 846 | return Param(f0_part_paramtab,this); |
---|
| 847 | } |
---|
| 848 | |
---|
| 849 | Param Joint::properties() |
---|
| 850 | { |
---|
| 851 | return Param(usedelta?f0_joint_paramtab:f0_nodeltajoint_paramtab,this); |
---|
| 852 | } |
---|
| 853 | |
---|
| 854 | Param Neuro::properties() |
---|
| 855 | { |
---|
| 856 | return Param(f0_neuro_paramtab,this); |
---|
| 857 | } |
---|
| 858 | |
---|
| 859 | class NeuroExtParamTab: public ParamTab |
---|
| 860 | { |
---|
| 861 | public: |
---|
| 862 | NeuroExtParamTab():ParamTab(f0_neuro_paramtab) |
---|
| 863 | { |
---|
| 864 | #define FIELDSTRUCT NeuroExt |
---|
| 865 | ParamEntry entry={"class",2,0,"neuro class","s",GETSET(neuroclass)}; |
---|
| 866 | #undef FIELDSTRUCT |
---|
| 867 | add(&entry); |
---|
| 868 | |
---|
| 869 | #define FIELDSTRUCT Neuro |
---|
| 870 | ParamEntry entry2={"state",2,0,"state","f",FIELD(state)}; |
---|
| 871 | #undef FIELDSTRUCT |
---|
| 872 | add(&entry2); |
---|
| 873 | } |
---|
| 874 | }; |
---|
| 875 | |
---|
| 876 | Param& Neuro::getStaticParam() |
---|
| 877 | { |
---|
| 878 | static Param p(f0_neuro_paramtab,0,"NeuroDef"); |
---|
| 879 | return p; |
---|
| 880 | } |
---|
| 881 | |
---|
| 882 | //////////////////////// |
---|
| 883 | |
---|
| 884 | NeuroConn::NeuroConn() |
---|
| 885 | { |
---|
| 886 | defassign(); |
---|
| 887 | } |
---|
| 888 | |
---|
| 889 | ////////////////////////////////////// |
---|
| 890 | |
---|
| 891 | ParamEntry *NeuroExt::getParamTab() |
---|
| 892 | { |
---|
| 893 | static NeuroExtParamTab tab; |
---|
| 894 | return tab.getParamTab(); |
---|
| 895 | } |
---|
| 896 | |
---|
| 897 | void NeuroExt::get_neuroclass(PARAMGETARGS) |
---|
| 898 | {ret->setString(getClassName());} |
---|
| 899 | |
---|
| 900 | int NeuroExt::set_neuroclass(PARAMSETARGS) |
---|
| 901 | {setClassName(arg->getString());return PSET_CHANGED;} |
---|
| 902 | |
---|
| 903 | |
---|