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

Last change on this file since 818 was 743, checked in by Maciej Komosinski, 7 years ago

Introduced different function names in C++ instead of overloading (with the same number of arguments) so that javascript is able to discriminate them

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