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