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