source: cpp/frams/model/modelparts.cpp @ 899

Last change on this file since 899 was 899, checked in by Maciej Komosinski, 4 years ago

Code formatting

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