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

Last change on this file since 839 was 839, checked in by Maciej Komosinski, 5 years ago

Added extern template declarations for UserTags? reg[N] static members

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