[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; |
---|
[528] | 645 | mass=1; |
---|
[109] | 646 | } |
---|
| 647 | |
---|
| 648 | void Part::operator=(const Part& src) |
---|
| 649 | { |
---|
| 650 | p=src.p; o=src.o; |
---|
| 651 | flags=src.flags; |
---|
| 652 | mass=src.mass; density=src.density; |
---|
| 653 | friction=src.friction; |
---|
| 654 | ingest=src.ingest; |
---|
| 655 | assim=src.assim; |
---|
| 656 | size=src.size; |
---|
| 657 | rot=src.rot; |
---|
| 658 | refno=src.refno; |
---|
| 659 | vcolor=src.vcolor; |
---|
| 660 | vsize=src.vsize; |
---|
[292] | 661 | vis_style=src.vis_style; |
---|
[109] | 662 | shape=src.shape; |
---|
[292] | 663 | scale=src.scale; |
---|
| 664 | hollow=src.hollow; |
---|
[109] | 665 | } |
---|
| 666 | |
---|
| 667 | void Part::setOrient(const Orient &_o) |
---|
| 668 | { |
---|
| 669 | o=_o; |
---|
| 670 | rot.getAngles(o.x,o.z); |
---|
| 671 | } |
---|
| 672 | |
---|
| 673 | void Part::setRot(const Pt3D &r) |
---|
| 674 | { |
---|
| 675 | rot=r; |
---|
| 676 | o=Orient_1; |
---|
| 677 | o.rotate(rot); |
---|
| 678 | } |
---|
| 679 | |
---|
| 680 | void Part::setPositionAndRotationFromAxis(const Pt3D &p1,const Pt3D &p2) |
---|
| 681 | { |
---|
| 682 | Pt3D x=p2-p1; |
---|
| 683 | Pt3D dir(x.y,x.z,x.x); |
---|
| 684 | p=p1+x*0.5; |
---|
| 685 | rot.getAngles(x,dir); |
---|
| 686 | } |
---|
| 687 | |
---|
| 688 | Param& Part::getStaticParam() |
---|
| 689 | { |
---|
| 690 | static Param p(f0_part_paramtab,0,"Part"); |
---|
| 691 | return p; |
---|
| 692 | } |
---|
| 693 | |
---|
| 694 | |
---|
| 695 | /////////////////////////// |
---|
| 696 | |
---|
| 697 | Joint::Joint():PartBase(getDefaultStyle()) |
---|
| 698 | { |
---|
| 699 | rot=Pt3D_0; |
---|
| 700 | defassign(); |
---|
| 701 | d.x=JOINT_DELTA_MARKER; |
---|
[114] | 702 | d.y=JOINT_DELTA_MARKER; |
---|
| 703 | d.z=JOINT_DELTA_MARKER; |
---|
[109] | 704 | part1=0; part2=0; |
---|
| 705 | flags=0; |
---|
| 706 | usedelta=0; |
---|
| 707 | } |
---|
| 708 | |
---|
| 709 | void Joint::operator=(const Joint& src) |
---|
| 710 | { |
---|
| 711 | rot=src.rot; |
---|
| 712 | d=src.d; |
---|
[292] | 713 | shape=src.shape; |
---|
[109] | 714 | stamina=src.stamina; |
---|
| 715 | stif=src.stif; rotstif=src.rotstif; |
---|
[292] | 716 | vis_style=src.vis_style; |
---|
| 717 | vcolor=src.vcolor; |
---|
[109] | 718 | part1=0; part2=0; |
---|
| 719 | flags=src.flags; |
---|
| 720 | usedelta=src.usedelta; |
---|
| 721 | refno=src.refno; |
---|
| 722 | } |
---|
| 723 | |
---|
| 724 | void Joint::attachToParts(Part* p1,Part* p2) |
---|
| 725 | { |
---|
| 726 | part1=p1; |
---|
| 727 | part2=p2; |
---|
| 728 | if (p1 && p2) |
---|
| 729 | { |
---|
| 730 | o=rot; |
---|
| 731 | if (usedelta) |
---|
| 732 | { |
---|
| 733 | p1->o.transform(p2->o,o); |
---|
| 734 | // p2->o.x=p1->o/o.x; p2->o.y=p1->o/o.y; p2->o.z=p1->o/o.z; |
---|
| 735 | p2->p=p2->o.transform(d)+p1->p; |
---|
| 736 | } |
---|
| 737 | } |
---|
| 738 | } |
---|
| 739 | |
---|
| 740 | void Joint::attachToParts(int p1,int p2) |
---|
| 741 | { |
---|
| 742 | attachToParts((p1>=0)?owner->getPart(p1):0,(p2>=0)?owner->getPart(p2):0); |
---|
| 743 | } |
---|
| 744 | |
---|
| 745 | void Joint::resetDelta() |
---|
| 746 | { |
---|
[114] | 747 | d=Pt3D(JOINT_DELTA_MARKER,JOINT_DELTA_MARKER,JOINT_DELTA_MARKER); |
---|
[109] | 748 | } |
---|
| 749 | |
---|
[114] | 750 | void Joint::resetDeltaMarkers() |
---|
[109] | 751 | { |
---|
[114] | 752 | if (d.x==JOINT_DELTA_MARKER) d.x=0; |
---|
| 753 | if (d.y==JOINT_DELTA_MARKER) d.y=0; |
---|
| 754 | if (d.z==JOINT_DELTA_MARKER) d.z=0; |
---|
[109] | 755 | } |
---|
| 756 | |
---|
[114] | 757 | void Joint::useDelta(bool use) |
---|
[109] | 758 | { |
---|
[114] | 759 | usedelta=use; |
---|
| 760 | } |
---|
| 761 | |
---|
| 762 | bool Joint::isDelta() |
---|
| 763 | { |
---|
[109] | 764 | return usedelta; |
---|
| 765 | } |
---|
| 766 | |
---|
| 767 | Param& Joint::getStaticParam() |
---|
| 768 | { |
---|
| 769 | static Param p(f0_joint_paramtab,0,"Joint"); |
---|
| 770 | return p; |
---|
| 771 | } |
---|
| 772 | |
---|
| 773 | |
---|
| 774 | ///////////////////////////////////////////////////////////////// |
---|
| 775 | ///////////////////////////////////////////////////////////////// |
---|
| 776 | |
---|
| 777 | #include F0_CLASSES_FILE |
---|
| 778 | |
---|
| 779 | #ifdef MODEL_V1_COMPATIBLE |
---|
| 780 | |
---|
| 781 | #define FIELDSTRUCT Neuro |
---|
| 782 | ParamEntry f0_old_neuro_tab[]= |
---|
| 783 | { |
---|
| 784 | {"Connections",2,6,"n",}, |
---|
| 785 | {"Other properties",}, |
---|
| 786 | {"p",0,0,"part ref#","d",FIELD(part_refno),}, |
---|
| 787 | {"j",0,0,"joint ref#","d",FIELD(joint_refno),}, |
---|
| 788 | {"s",1,0,"state","f",FIELD(state),}, |
---|
| 789 | {"in",1,0,"Inertia","f",FIELD(inertia),}, |
---|
| 790 | {"fo",1,0,"Force","f",FIELD(force),}, |
---|
| 791 | {"si",1,0,"Sigmoid","f",FIELD(sigmo),}, |
---|
| 792 | {0,0,0,}, |
---|
| 793 | }; |
---|
| 794 | #undef FIELDSTRUCT |
---|
| 795 | |
---|
| 796 | #define FIELDSTRUCT NeuroItem |
---|
| 797 | ParamEntry f0_neuroitem_paramtab[]= |
---|
| 798 | { |
---|
| 799 | {"Connections",3,12,"ni",}, |
---|
| 800 | {"Geometry",}, |
---|
| 801 | {"Other",}, |
---|
| 802 | {"n",0,0,"neuron ref#","d",FIELD(neuro_refno),}, |
---|
| 803 | {"c",2,0,"connection ref#","d",FIELD(conn_refno),}, |
---|
| 804 | {"w",2,0,"connection weight","f",FIELD(weight),}, |
---|
| 805 | {"p",0,0,"part ref#","d",FIELD(part_refno),}, |
---|
| 806 | {"j",0,0,"joint ref#","d",FIELD(joint_refno),}, |
---|
| 807 | {"px",1,0,"position.x","f",FIELD(pos.x),}, |
---|
| 808 | {"py",1,0,"position.y","f",FIELD(pos.y),}, |
---|
| 809 | {"pz",1,0,"position.z","f",FIELD(pos.z),}, |
---|
| 810 | {"rx",1,0,"rotation.x","f",FIELD(rot.x),}, |
---|
| 811 | {"ry",1,0,"rotation.y","f",FIELD(rot.y),}, |
---|
| 812 | {"rz",1,0,"rotation.z","f",FIELD(rot.z),}, |
---|
| 813 | {"d",2,0,"item details","s",GETSET(details),}, |
---|
| 814 | {0,0,0,}, |
---|
| 815 | }; |
---|
| 816 | #undef FIELDSTRUCT |
---|
| 817 | #endif |
---|
| 818 | |
---|
| 819 | //////////////////////////////////////// |
---|
| 820 | |
---|
| 821 | ParamEntry Neuro::emptyParamTab[]= |
---|
| 822 | { |
---|
| 823 | {"Undefined Neuro",1,0,"?",}, |
---|
| 824 | {0,0,0,}, |
---|
| 825 | }; |
---|
| 826 | |
---|
| 827 | Param Part::extraProperties() |
---|
| 828 | { |
---|
| 829 | return Param(f0_part_xtra_paramtab,this); |
---|
| 830 | } |
---|
| 831 | |
---|
| 832 | Param Joint::extraProperties() |
---|
| 833 | { |
---|
| 834 | return Param(f0_joint_xtra_paramtab,this); |
---|
| 835 | } |
---|
| 836 | |
---|
| 837 | Param Neuro::extraProperties() |
---|
| 838 | { |
---|
| 839 | return Param(f0_neuro_xtra_paramtab,this); |
---|
| 840 | } |
---|
| 841 | |
---|
| 842 | Param Part::properties() |
---|
| 843 | { |
---|
| 844 | return Param(f0_part_paramtab,this); |
---|
| 845 | } |
---|
| 846 | |
---|
| 847 | Param Joint::properties() |
---|
| 848 | { |
---|
| 849 | return Param(usedelta?f0_joint_paramtab:f0_nodeltajoint_paramtab,this); |
---|
| 850 | } |
---|
| 851 | |
---|
| 852 | Param Neuro::properties() |
---|
| 853 | { |
---|
| 854 | return Param(f0_neuro_paramtab,this); |
---|
| 855 | } |
---|
| 856 | |
---|
| 857 | class NeuroExtParamTab: public ParamTab |
---|
| 858 | { |
---|
| 859 | public: |
---|
| 860 | NeuroExtParamTab():ParamTab(f0_neuro_paramtab) |
---|
| 861 | { |
---|
| 862 | #define FIELDSTRUCT NeuroExt |
---|
| 863 | ParamEntry entry={"class",2,0,"neuro class","s",GETSET(neuroclass)}; |
---|
| 864 | #undef FIELDSTRUCT |
---|
| 865 | add(&entry); |
---|
| 866 | |
---|
| 867 | #define FIELDSTRUCT Neuro |
---|
| 868 | ParamEntry entry2={"state",2,0,"state","f",FIELD(state)}; |
---|
| 869 | #undef FIELDSTRUCT |
---|
| 870 | add(&entry2); |
---|
| 871 | } |
---|
| 872 | }; |
---|
| 873 | |
---|
| 874 | Param& Neuro::getStaticParam() |
---|
| 875 | { |
---|
| 876 | static Param p(f0_neuro_paramtab,0,"NeuroDef"); |
---|
| 877 | return p; |
---|
| 878 | } |
---|
| 879 | |
---|
| 880 | //////////////////////// |
---|
| 881 | |
---|
| 882 | NeuroConn::NeuroConn() |
---|
| 883 | { |
---|
| 884 | defassign(); |
---|
| 885 | } |
---|
| 886 | |
---|
| 887 | ////////////////////////////////////// |
---|
| 888 | |
---|
| 889 | ParamEntry *NeuroExt::getParamTab() |
---|
| 890 | { |
---|
| 891 | static NeuroExtParamTab tab; |
---|
| 892 | return tab.getParamTab(); |
---|
| 893 | } |
---|
| 894 | |
---|
| 895 | void NeuroExt::get_neuroclass(PARAMGETARGS) |
---|
| 896 | {ret->setString(getClassName());} |
---|
| 897 | |
---|
| 898 | int NeuroExt::set_neuroclass(PARAMSETARGS) |
---|
| 899 | {setClassName(arg->getString());return PSET_CHANGED;} |
---|
| 900 | |
---|
| 901 | |
---|