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