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
Line 
1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2017  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
31template<> char ModelUserTags::reg[5] = { 0 };
32
33/////////////////////////
34
35PartBase::~PartBase()
36{
37        if (mapped) delete mapped;
38}
39
40void PartBase::notifyMappingChange()
41{
42        if (owner) owner->partmappingchanged = 1;
43}
44
45void PartBase::setMapping(const IRange &r)
46{
47        if (mapped) (*mapped) = r;
48        else mapped = new MultiRange(r);
49        notifyMappingChange();
50}
51
52void PartBase::clearMapping()
53{
54        if (mapped) { delete mapped; mapped = 0; }
55}
56
57void PartBase::addMapping(const IRange &r)
58{
59        if (mapped) mapped->add(r);
60        else mapped = new MultiRange(r);
61        notifyMappingChange();
62}
63
64void PartBase::setMapping(const MultiRange &mr)
65{
66        if (mapped) (*mapped) = mr;
67        else mapped = new MultiRange(mr);
68        notifyMappingChange();
69}
70
71void PartBase::addMapping(const MultiRange &mr)
72{
73        if (mapped) mapped->add(mr);
74        else mapped = new MultiRange(mr);
75        notifyMappingChange();
76}
77
78void PartBase::setInfo(const SString& name, const SString& value)
79{
80        strSetField(info, name, value);
81}
82
83void PartBase::setInfo(const SString& name, int value)
84{
85        setInfo(name, SString::valueOf(value));
86}
87
88void PartBase::setInfo(const SString& name, double value)
89{
90        setInfo(name, SString::valueOf(value));
91}
92
93SString PartBase::getInfo(const SString& name)
94{
95        return strGetField(info, name);
96}
97
98/////////////////////////
99
100NeuroClass::NeuroClass(ParamEntry *_props, SString _description,
101        int _prefinputs, int _prefoutput, int _preflocation,
102        int *_vectordata, bool own_vd, int vhints)
103        :ownedvectordata(own_vd),
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)
111{}
112
113NeuroClass::~NeuroClass()
114{
115        setSymbolGlyph(0, 0);
116        if (props && ownedprops)
117                ParamObject::freeParamTab(props);
118}
119
120NeuroClass::NeuroClass()
121        :ownedvectordata(0),
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)
127{}
128
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
147SString NeuroClass::getSummary()
148{
149        SString t;
150        t = getDescription();
151        if (t.len()) t += "\n\n";
152        t += "Characteristics:\n";
153        if (getPreferredInputs())
154        {
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());
158        }
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())
165        {
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;
169        }
170        Param p = getProperties();
171        if (p.getPropCount())
172        {
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++)
178                {
179                        if (i) t += "\n";
180                        t += "   "; t += p.name(i); t += " ("; t += p.id(i); t += ") ";
181                        t += p.friendlyTypeDescr(i);
182                        if (h = p.help(i)) if (*h) { t += " - "; t += h; }
183                }
184        }
185        return t;
186}
187
188/////////////////////////
189
190/////////////////////////////////////
191
192Neuro::Neuro(double _state, double _inertia, double _force, double _sigmo)
193        :PartBase(getDefaultStyle()), state(_state)
194{
195        flags = 0;
196        myclass = 0;
197        knownclass = 1;
198        part_refno = -1; joint_refno = -1;
199}
200
201Neuro::Neuro(void) :PartBase(getDefaultStyle())
202{
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;
214}
215
216
217Neuro::~Neuro()
218{
219        int i;
220        for (i = 0; i < inputs.size(); i++)
221        {
222                NInput &ni = inputs(i);
223                if (ni.info) delete ni.info;
224        }
225}
226
227SString** Neuro::inputInfo(int i)
228{
229        if (i >= getInputCount()) return 0;
230        return &inputs(i).info;
231}
232
233void Neuro::setInputInfo(int i, const SString& name, const SString &value)
234{
235        SString **s = inputInfo(i);
236        if (!s) return;
237        if (!*s) *s = new SString();
238        strSetField(**s, name, value);
239}
240
241void Neuro::setInputInfo(int i, const SString& name, int value)
242{
243        setInputInfo(i, name, SString::valueOf(value));
244}
245
246void Neuro::setInputInfo(int i, const SString& name, double value)
247{
248        setInputInfo(i, name, SString::valueOf(value));
249}
250
251SString Neuro::getInputInfo(int i)
252{
253        SString **s = inputInfo(i);
254        if (!s) return SString();
255        if (!*s) return SString();
256        return **s;
257}
258
259SString Neuro::getInputInfo(int i, const SString& name)
260{
261        SString **s = inputInfo(i);
262        if (!s) return SString();
263        if (!*s) return SString();
264        return strGetField(**s, name);
265}
266
267void Neuro::operator=(const Neuro& src)
268{
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;
281}
282
283void Neuro::attachToPart(int i)
284{
285        attachToPart((i >= 0) ? owner->getPart(i) : 0);
286}
287
288void Neuro::attachToJoint(int i)
289{
290        attachToJoint((i >= 0) ? owner->getJoint(i) : 0);
291}
292
293int Neuro::getClassCount()
294{
295        return NeuroLibrary::staticlibrary.getClassCount();
296}
297
298NeuroClass* Neuro::getClass(int classindex)
299{
300        return NeuroLibrary::staticlibrary.getClass(classindex);
301}
302
303NeuroClass* Neuro::getClass(const SString& classname)
304{
305        return NeuroLibrary::staticlibrary.findClass(classname);
306}
307
308int Neuro::getClassIndex(const NeuroClass*nc)
309{
310        return NeuroLibrary::staticlibrary.classes.find((void*)nc);
311}
312
313NeuroClass* Neuro::getClass()
314{
315        checkClass();
316        return myclass;
317}
318
319void Neuro::setClass(NeuroClass* cl)
320{
321        myclass = cl;
322        myclassname = cl->getName();
323        knownclass = 1;
324}
325
326SString Neuro::getClassName(int classindex)
327{
328        NeuroClass *cl = NeuroLibrary::staticlibrary.getClass(classindex);
329        return cl ? cl->getName() : SString();
330}
331
332void Neuro::setDetails(const SString& details)
333{
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;
338}
339
340SString Neuro::getDetails()
341{
342        SString ret = getClassName();
343        if (myclassparams.len()) { if (!ret.len()) ret = "N"; ret += ":"; ret += myclassparams; }
344        return ret;
345}
346
347void Neuro::checkClass()
348{
349        if (knownclass) return;
350        myclass = getClass(myclassname);
351        knownclass = 1;
352}
353
354SyntParam Neuro::classProperties(bool handle_defaults_when_saving)
355{
356        NeuroClass *cl = getClass();
357        ParamEntry *pe = cl ? cl->getParamTab() : emptyParamTab;
358        return SyntParam(pe, &myclassparams, handle_defaults_when_saving);
359}
360
361SString Neuro::getClassName()
362{
363        return myclassname;
364}
365
366void Neuro::setClassName(const SString& clazz)
367{
368        myclassname = clazz;
369        knownclass = 0;
370}
371
372int Neuro::addInput(Neuro* child, double weight, const SString *info)
373{
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;
378}
379
380int Neuro::findInput(Neuro* child) const
381{
382        for (int i = 0; i < inputs.size(); i++)
383                if (inputs(i).n == child) return i;
384        return -1;
385}
386
387Neuro* Neuro::getInput(int i, double &weight) const
388{
389        if (i >= getInputCount()) return 0;
390        NInput &inp = inputs(i);
391        weight = inp.weight;
392        return inp.n;
393}
394
395double Neuro::getInputWeight(int i) const
396{
397        return inputs(i).weight;
398}
399
400void Neuro::setInputWeight(int i, double w)
401{
402        inputs(i).weight = w;
403}
404
405void Neuro::setInput(int i, Neuro* n)
406{
407        NInput &inp = inputs(i);
408        inp.n = n;
409}
410
411void Neuro::setInput(int i, Neuro* n, double w)
412{
413        NInput &inp = inputs(i);
414        inp.n = n;
415        inp.weight = w;
416}
417
418void Neuro::removeInput(int refno)
419{
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);
426}
427
428int Neuro::removeInput(Neuro* child)
429{
430        int i = findInput(child);
431        if (i >= 0) removeInput(i);
432        return i;
433}
434
435int Neuro::getOutputsCount() const
436{
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;
441}
442
443int Neuro::isOldEffector()
444{
445        static SString bend("|"), rot("@");
446        return ((getClassName() == bend) || (getClassName() == rot));
447}
448
449int Neuro::isOldReceptor()
450{
451        static SString g("G"), t("T"), s("S");
452        return ((getClassName() == g) || (getClassName() == t) || (getClassName() == s));
453}
454
455int Neuro::isOldNeuron()
456{
457        static SString n("N");
458        return (getClassName() == n);
459}
460
461int Neuro::isNNConnection()
462{
463        static SString conn("-");
464        return (getClassName() == conn);
465}
466
467int Neuro::findInputs(SList& result, const char* classname, const Part* part, const Joint* joint) const
468{
469        Neuro *nu;
470        SString cn(classname);
471        int n0 = result.size();
472        for (int i = 0; nu = getInput(i); i++)
473        {
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;
481        }
482        return result.size() - n0;
483}
484
485int Neuro::findOutputs(SList& result, const char* classname, const Part* part, const Joint* joint) const
486{ // not very efficient...
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++)
492        {
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)
501                        {
502                        result += (void*)nu;
503                        break;
504                        }
505        }
506        return result.size() - n0;
507}
508
509void Neuro::get_inputCount(PARAMGETARGS)
510{
511        ret->setInt(inputs.size());
512}
513
514void Neuro::p_getInputNeuroDef(ExtValue *args, ExtValue *ret)
515{
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));
521}
522
523void Neuro::p_getInputWeight(ExtValue *args, ExtValue *ret)
524{
525        int i = args->getInt();
526        if ((i < 0) || (i >= inputs.size()))
527                ret->setEmpty();
528        else
529                ret->setDouble(inputs(i).weight);
530}
531
532void Neuro::p_getInputNeuroIndex(ExtValue *args, ExtValue *ret)
533{
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);
539}
540
541void Neuro::get_classObject(PARAMGETARGS)
542{
543#ifndef SDK_WITHOUT_FRAMS
544        NeuroClassExt::makeStaticObject(ret, getClass());
545#endif
546}
547
548///////////////////////////////////////
549
550SString Part::getDefaultStyle()
551{
552        return SString("part");
553}
554SString Joint::getDefaultStyle()
555{
556        return SString("joint");
557}
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()
565{
566        return SString("neuro");
567}
568
569Part::Part(enum Shape s) :PartBase(getDefaultStyle())
570{
571        o = Orient_1;
572        p = Pt3D_0;
573        rot = Pt3D_0;
574        flags = 0;
575        defassign();
576        shape = s;
577        mass = 1;
578}
579
580void Part::operator=(const Part& src)
581{
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;
597}
598
599void Part::setOrient(const Orient &_o)
600{
601        o = _o;
602        rot.getAngles(o.x, o.z);
603}
604
605void Part::setRot(const Pt3D &r)
606{
607        rot = r;
608        o = Orient_1;
609        o.rotate(rot);
610}
611
612void Part::setPositionAndRotationFromAxis(const Pt3D &p1, const Pt3D &p2)
613{
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);
618}
619
620Param& Part::getStaticParam()
621{
622        static Param p(f0_part_paramtab, 0, "Part");
623        return p;
624}
625
626
627///////////////////////////
628
629Joint::Joint() :PartBase(getDefaultStyle())
630{
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;
639}
640
641void Joint::operator=(const Joint& src)
642{
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;
654}
655
656void Joint::attachToParts(Part* p1, Part* p2)
657{
658        part1 = p1;
659        part2 = p2;
660        if (p1 && p2)
661        {
662                o = rot;
663                if (usedelta)
664                {
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;
668                }
669        }
670}
671
672void Joint::attachToParts(int p1, int p2)
673{
674        attachToParts((p1 >= 0) ? owner->getPart(p1) : 0, (p2 >= 0) ? owner->getPart(p2) : 0);
675}
676
677void Joint::resetDelta()
678{
679        d = Pt3D(JOINT_DELTA_MARKER, JOINT_DELTA_MARKER, JOINT_DELTA_MARKER);
680}
681
682void Joint::resetDeltaMarkers()
683{
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;
687}
688
689void Joint::useDelta(bool use)
690{
691        usedelta = use;
692}
693
694bool Joint::isDelta()
695{
696        return usedelta;
697}
698
699Param& Joint::getStaticParam()
700{
701        static Param p(f0_joint_paramtab, 0, "Joint");
702        return p;
703}
704
705/////////////////////////////////////////////////////////////////
706
707#include F0_CLASSES_FILE
708
709////////////////////////////////////////
710
711ParamEntry Neuro::emptyParamTab[] =
712{
713        { "Undefined Neuro", 1, 0, "?", },
714        { 0, 0, 0, },
715};
716
717Param Part::extraProperties()
718{
719        return Param(f0_part_xtra_paramtab, this);
720}
721
722Param Joint::extraProperties()
723{
724        return Param(f0_joint_xtra_paramtab, this);
725}
726
727Param Neuro::extraProperties()
728{
729        return Param(f0_neuro_xtra_paramtab, this);
730}
731
732Param Part::properties()
733{
734        return Param(f0_part_paramtab, this);
735}
736
737Param Joint::properties()
738{
739        return Param(usedelta ? f0_joint_paramtab : f0_nodeltajoint_paramtab, this);
740}
741
742Param Neuro::properties()
743{
744        return Param(f0_neuro_paramtab, this);
745}
746
747class NeuroExtParamTab : public ParamTab
748{
749public:
750        NeuroExtParamTab() :ParamTab(f0_neuro_paramtab)
751        {
752#define FIELDSTRUCT NeuroExt
753                ParamEntry entry = { "class", 2, 0, "neuro class", "s", GETSET(neuroclass) };
754#undef FIELDSTRUCT
755                add(&entry);
756
757#define FIELDSTRUCT Neuro
758                ParamEntry entry2 = { "state", 2, 0, "state", "f", FIELD(state) };
759#undef FIELDSTRUCT
760                add(&entry2);
761        }
762};
763
764Param& Neuro::getStaticParam()
765{
766        static Param p(f0_neuro_paramtab, 0, "NeuroDef");
767        return p;
768}
769
770////////////////////////
771
772NeuroConn::NeuroConn()
773{
774        defassign();
775}
776
777//////////////////////////////////////
778
779ParamEntry *NeuroExt::getParamTab()
780{
781        static NeuroExtParamTab tab;
782        return tab.getParamTab();
783}
784
785void NeuroExt::get_neuroclass(PARAMGETARGS)
786{
787        ret->setString(getClassName());
788}
789
790int NeuroExt::set_neuroclass(PARAMSETARGS)
791{
792        setClassName(arg->getString()); return PSET_CHANGED;
793}
Note: See TracBrowser for help on using the repository browser.