source: cpp/frams/model/modelobj.cpp @ 732

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

Added support for "checkpoints" (intermediate phases of development of the Model when converting between genetic encodings). See Model.checkpoint() and conv_f1.cpp for an example.

File size: 7.2 KB
Line 
1#include "modelobj.h"
2#include <frams/vm/classes/genoobj.h>
3#include <frams/util/extvalue.h>
4#include <frams/vm/classes/3dobject.h>
5
6#define FIELDSTRUCT ModelObj
7ParamEntry modelobj_paramtab[] =
8{
9        { "Model", 1, 21, "Model", },
10        { "se", 0, PARAM_NOSTATIC, "startenergy", "f", FIELD(startenergy), },
11        { "Vstyle", 0, PARAM_NOSTATIC, "vis_style", "s", FIELD(vis_style), },
12
13        { "geno", 0, PARAM_NOSTATIC | PARAM_READONLY, "Geno", "oGeno", GETONLY(geno), },
14        { "newFromString", 0, 0, "Create a new object", "p oModel(s genotype)", PROCEDURE(p_newfromstring), },
15        { "newFromGeno", 0, 0, "Create a new object", "p oModel(oGeno)", PROCEDURE(p_newfromgeno), },
16        { "newWithCheckpoints", 0, 0, "Create a new object", "p oModel(x Geno object or string genotype)", PROCEDURE(p_newwithcheckpoints), "Creates a Model with the \"Checkpoints\" option enabled. Genotype converters supporting Checkpoints provide a sequence of Models that reflects development stages of the creature (this sequence is used purely for debugging and visualization of phenotype growth/development). Checkpoint Models can be accessed using getCheckpoint(i) for i ranging from 0 to numcheckpoints-1. Models created without the Checkpoint option and Models coming from unsupported converters have numcheckpoints=0." },
17
18        { "numparts", 0, PARAM_DONTSAVE | PARAM_NOSTATIC | PARAM_READONLY, "Number of parts", "d", GETONLY(numparts), },
19        { "numjoints", 0, PARAM_DONTSAVE | PARAM_NOSTATIC | PARAM_READONLY, "Number of joints", "d", GETONLY(numjoints), },
20        { "numneurons", 0, PARAM_DONTSAVE | PARAM_NOSTATIC | PARAM_READONLY, "Number of neurons", "d", GETONLY(numneurons), },
21        { "numconnections", 0, PARAM_DONTSAVE | PARAM_NOSTATIC | PARAM_READONLY, "Number of neuron connections", "d", GETONLY(numconnections), },
22
23        { "getPart", 0, PARAM_USERHIDDEN | PARAM_NOSTATIC, "getPart (static model information)", "p oPart(d index)", PROCEDURE(p_getpart), },
24        { "getJoint", 0, PARAM_USERHIDDEN | PARAM_NOSTATIC, "getJoint (static model information)", "p oJoint(d index)", PROCEDURE(p_getjoint), },
25        { "getNeuroDef", 0, PARAM_USERHIDDEN | PARAM_NOSTATIC, "getNeuroDef", "p oNeuroDef(d index)", PROCEDURE(p_getneuro), },
26
27        { "size_x", 0, PARAM_READONLY | PARAM_NOSTATIC | PARAM_DEPRECATED, "Bounding box x size", "f", FIELD(size.x), "(size_x,size_y,size_z) are dimensions of the axis-aligned bounding box of the creature, including imaginary Part sizes (Part.s, usually 1.0). A creature consisting of a single default part has the size of (2.0,2.0,2.0) - twice the Part.s value (like a sphere diameter is twice its radius).\nSee also: Creature.moveAbs" },
28        { "size_y", 0, PARAM_READONLY | PARAM_NOSTATIC | PARAM_DEPRECATED, "Bounding box y size", "f", FIELD(size.y), "See Model.size_x" },
29        { "size_z", 0, PARAM_READONLY | PARAM_NOSTATIC | PARAM_DEPRECATED, "Bounding box z size", "f", FIELD(size.z), "See Model.size_x" },
30        { "bboxSize", 0, PARAM_READONLY | PARAM_NOSTATIC, "Bounding box size", "oXYZ", GETONLY(bboxsize) },
31        { "numcheckpoints", 0, PARAM_DONTSAVE | PARAM_READONLY | PARAM_NOSTATIC, "Number of checkpoints", "d", GETONLY(numcheckpoints) },
32        { "getCheckpoint", 0, PARAM_USERHIDDEN | PARAM_NOSTATIC, "getCheckpoint", "p oModel(d index)", PROCEDURE(p_getcheckpoint),
33        "Checkpoint Model objects are only valid as long as the parent Model object exists.\n"
34        "See also: Model.newWithCheckpoints()\n\n"
35        "// incorrect usage - calling getCheckpoint() on a temporary object:\n"
36        "var c=Model.newWithCheckpoints(\"XXX\").getCheckpoint(1).genotype.geno;\n\n"
37        "// correct usage - keeping the parent Model reference in 'm':\n"
38        "var m=Model.newWithCheckpoints(\"XXX\");\n"
39        "var c=m.getCheckpoint(1).genotype.geno;\n"
40        },
41        { "shape_type", 0, PARAM_DONTSAVE | PARAM_NOSTATIC | PARAM_READONLY, "Shape type", "d 0 3 ~Unknown~Illegal~Ball-and-stick~Solids", GETONLY(shape_type) },
42        { "solid_model", 0, PARAM_DONTSAVE | PARAM_NOSTATIC | PARAM_READONLY, "Solid shapes model", "oModel", GETONLY(solid_model), "Conversion of this Model to solid shapes. Note! Only available when this Model has shape_type==2 (Ball-and-stick)." },
43
44        { 0, 0, 0, },
45};
46#undef FIELDSTRUCT
47
48void ModelObj::get_geno(ExtValue *ret)
49{
50        Geno *g;
51        if ((!geno.isValid()) && isValid())
52                g = new Geno(getF0Geno());
53        else
54                g = new Geno(geno);
55        ret->setObject(GenoObj::makeDynamicObjectAndDecRef(g));
56}
57
58void ModelObj::p_newfromstring(ExtValue *args, ExtValue *ret)
59{
60        *ret = makeDynamicObject(new Model(Geno(args[0].getString())));
61}
62
63void ModelObj::p_newfromgeno(ExtValue *args, ExtValue *ret)
64{
65        Geno *g = GenoObj::fromObject(args[0].getObject());
66        if (g)
67                *ret = makeDynamicObject(new Model(*g));
68        else
69                ret->setEmpty();
70}
71
72void ModelObj::p_newwithcheckpoints(ExtValue *args, ExtValue *ret)
73{
74        Model *m = NULL;
75        if (args[0].getType() == TString)
76                m = new Model(Geno(args[0].getString()), false, true);
77        else
78        {
79                Geno *g = GenoObj::fromObject(args[0].getObject(), false);
80                if (g)
81                        m = new Model(*g, false, true);
82                else
83                        logPrintf("Model", "newWithCheckpoints", LOG_ERROR, "Geno or string expected, %s found", args[0].typeDescription().c_str());
84        }
85
86        if (m != NULL)
87                *ret = makeDynamicObject(m);
88        else
89                ret->setEmpty();
90}
91
92Param& ModelObj::getStaticParam()
93{
94#ifdef __CODEGUARD__
95        static ModelObj static_modelobj;
96        static Param static_modelparam(modelobj_paramtab, &static_modelobj);
97#else
98        static Param static_modelparam(modelobj_paramtab);
99#endif
100        return static_modelparam;
101}
102
103Param& ModelObj::getDynamicParam()
104{
105        static Param dynamic_modelparam(modelobj_paramtab);
106        return dynamic_modelparam;
107}
108
109ParamInterface* ModelObj::getInterface()
110{
111        return &getStaticParam();
112}
113
114ExtObject ModelObj::makeStaticObject(Model* m)
115{
116        return ExtObject(&getStaticParam(), (void*)m);
117}
118
119ExtObject ModelObj::makeDynamicObject(Model* m)
120{
121        return ExtObject(&getDynamicParam(), (DestrBase*)m);
122}
123
124Model* ModelObj::fromObject(const ExtValue& v, bool warn)
125{
126        return (Model*)v.getObjectTarget(getStaticParam().getName(), warn);
127}
128
129void ModelObj::p_getpart(PARAMPROCARGS)
130{
131        int i = args->getInt();
132        if ((i < 0) || (i >= getPartCount()))
133        {
134                ret->setEmpty();
135                return;
136        }
137        ret->setObject(ExtObject(&Part::getStaticParam(), getPart(i)));
138}
139
140void ModelObj::p_getjoint(PARAMPROCARGS)
141{
142        int i = args->getInt();
143        if ((i < 0) || (i >= getJointCount()))
144        {
145                ret->setEmpty();
146                return;
147        }
148        ret->setObject(ExtObject(&Joint::getStaticParam(), getJoint(i)));
149}
150
151void ModelObj::p_getneuro(PARAMPROCARGS)
152{
153        int i = args->getInt();
154        if ((i < 0) || (i >= getNeuroCount()))
155        {
156                ret->setEmpty();
157                return;
158        }
159        ret->setObject(ExtObject(&Neuro::getStaticParam(), getNeuro(i)));
160}
161
162void ModelObj::get_bboxsize(ExtValue *ret)
163{
164        *ret = Pt3D_Ext::makeDynamicObject(new Pt3D_Ext(size));
165}
166
167void ModelObj::p_getcheckpoint(PARAMPROCARGS)
168{
169        int i = args->getInt();
170        if ((i < 0) || (i >= getCheckpointCount()))
171        {
172                ret->setEmpty();
173                return;
174        }
175        ret->setObject(makeStaticObject(getCheckpoint(i)));
176}
177
178void ModelObj::get_solid_model(ExtValue *ret)
179{
180        if (getShapeType() != Model::SHAPE_BALL_AND_STICK)
181                ret->setEmpty();
182        Model *m = new Model;
183        m->open();
184        m->buildUsingSolidShapeTypes(*this);
185        m->close();
186        *ret = makeDynamicObject(m);
187}
188
189void ModelObj::get_shape_type(ExtValue *ret)
190{
191        ret->setInt(getShapeType());
192}
Note: See TracBrowser for help on using the repository browser.