Changeset 242 for cpp/frams/model/geometry/modelgeoclass.cpp
 Timestamp:
 05/07/14 20:41:18 (9 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

cpp/frams/model/geometry/modelgeoclass.cpp
r235 r242 5 5 6 6 #define FIELDSTRUCT ModelGeometry 7 static ParamEntry modelgeo_paramtab[] =7 static ParamEntry modelgeo_paramtab[] = 8 8 { 9 {"Creature: Geometry",1,5,"ModelGeometry", 10 "Example usage:\n" 11 "Simulator.print(ModelGeometry.forModel(Model.newFromString(\"//0\\np:sh=1\\n\")).area());\n" 12 "ModelGeometry.geodensity refers to the global simulator parameter (also available in GUI).\n" 13 "Local geodensity of individual ModelGeometry objects can also be changed, like this:\n" 14 "var mg=ModelGeometry.forModel(GenePools[0][0].getModel()); mg.geodensity=2; GenePools[0][0].user1=mg.area();\n"}, 15 {"geodensity",0,0,"Density","f 0.01 100.0 1.0",FIELD(density),"Affects the geometry calculation precision"}, //note: geodensity instead of density to make the name more unique  because of the unfortunate sim_params merging all configuration fields in a single namespace 16 {"forModel",0,PARAM_USERHIDDEN,"","p oModelGeometry(oModel)",PROCEDURE(p_formodel),"The returned ModelGeometry object can be used to calculate geometric properties of the associated model (volume, area, sizes). The density is copied from the current global ModelGeometry.geodensity on object creation."}, 17 {"volume",0,PARAM_NOSTATICPARAM_USERHIDDEN,"volume","p f()",PROCEDURE(p_volume),}, 18 {"area",0,PARAM_NOSTATICPARAM_USERHIDDEN,"area","p f()",PROCEDURE(p_area),}, 19 {"sizesAndAxes",0,PARAM_NOSTATICPARAM_USERHIDDEN,"sizesAndAxes","p oVector()",PROCEDURE(p_sizesandaxes),"The returned vector contains XYZ (sizes) and Orient (axes) objects."}, 20 21 {0,0,0,}, 9 { "Creature: Geometry", 1, 5, "ModelGeometry", 10 "Example usage:\n" 11 "Simulator.print(ModelGeometry.forModel(Model.newFromString(\"//0\\np:sh=1\\n\")).area());\n\n" 12 "ModelGeometry.geom_density refers to the global simulator parameter (also available in GUI).\n" 13 "To set geom_density for individual ModelGeometry objects:\n" 14 "var mg=ModelGeometry.forModel(GenePools[0][0].getModel()); mg.geom_density=2; GenePools[0][0].user1=mg.area();\n" }, 15 { "geom_density", 0, 0, "Density", "f 0.01 100.0 1.0", FIELD(density), "Affects the geometry calculation precision" }, //note: we used 'geom_density' instead of 'density' to make the name more unique  because sim_params merges all configuration fields in a single namespace. 16 { "forModel", 0, PARAM_USERHIDDEN, "", "p oModelGeometry(oModel)", PROCEDURE(p_formodel), "The returned ModelGeometry object can be used to calculate geometric properties (volume, area, sizes) of the associated model. The density is copied from the current global ModelGeometry.geom_density on object creation." }, 17 { "volume", 0, PARAM_NOSTATIC  PARAM_USERHIDDEN, "volume", "p f()", PROCEDURE(p_volume), }, 18 { "area", 0, PARAM_NOSTATIC  PARAM_USERHIDDEN, "area", "p f()", PROCEDURE(p_area), }, 19 { "sizesAndAxes", 0, PARAM_NOSTATIC  PARAM_USERHIDDEN, "sizesAndAxes", "p oVector()", PROCEDURE(p_sizesandaxes), "The returned vector contains XYZ (sizes) and Orient (axes) objects." }, 20 { 0, 0, 0, }, 22 21 }; 23 22 #undef FIELDSTRUCT 24 23 25 24 ExtObject ModelGeometry::makeDynamicObject(ModelGeometry* mg) 26 {return ExtObject(&mg>par,mg);} 25 { 26 return ExtObject(&mg>par, mg); 27 } 27 28 28 29 ModelGeometry::ModelGeometry(ModelObj *mo) 29 :par(modelgeo_paramtab,this)30 :par(modelgeo_paramtab, this) 30 31 { 31 model=mo; 32 if (model!=NULL) 33 model>incref(); 32 cached_for_density = 1; //invalid value, will be updated on first request 33 invalidateAllCached(); 34 model = mo; 35 if (model != NULL) 36 model>incref(); 34 37 } 35 38 36 39 ModelGeometry::~ModelGeometry() 37 40 { 38 if (model!=NULL)39 model>decref();41 if (model != NULL) 42 model>decref(); 40 43 } 41 44 42 void ModelGeometry::p_formodel(ExtValue *args,ExtValue *ret) 45 // Mark all 3 results as invalid. 46 // Validity of these 3 values must be maintained independently, 47 // as each of them is calculated by an individual call. 48 void ModelGeometry::invalidateAllCached() 43 49 { 44 Model *m=ModelObj::fromObject(*args); 45 if (m!=NULL) 46 { 47 ModelGeometry *mg=new ModelGeometry((ModelObj*)m); 48 mg>density=density; 49 ret>setObject(ModelGeometry::makeDynamicObject(mg)); 50 } 51 else 52 ret>setEmpty(); 50 cached_volume = 1; 51 cached_area = 1; 52 cached_sizes.x = 1; 53 53 } 54 54 55 void ModelGeometry::p_volume(ExtValue *args,ExtValue *ret) 55 // Invalidates cached results if a new density is requested 56 // (called in all geometry calculation functions) 57 void ModelGeometry::onDensityChanged() 56 58 { 57 ret>setDouble(ModelGeometryInfo::volume(*model,density)); 59 if (cached_for_density != density) 60 { 61 invalidateAllCached(); 62 cached_for_density = density; 63 } 58 64 } 59 65 60 void ModelGeometry::p_ area(ExtValue *args,ExtValue *ret)66 void ModelGeometry::p_formodel(ExtValue *args, ExtValue *ret) 61 67 { 62 ret>setDouble(ModelGeometryInfo::area(*model,density)); 68 Model *m = ModelObj::fromObject(*args); 69 if (m != NULL) 70 { 71 ModelGeometry *mg = new ModelGeometry((ModelObj*)m); 72 mg>density = density; 73 ret>setObject(ModelGeometry::makeDynamicObject(mg)); 74 } 75 else 76 ret>setEmpty(); 63 77 } 64 78 65 void ModelGeometry::p_ sizesandaxes(ExtValue *args,ExtValue *ret)79 void ModelGeometry::p_volume(ExtValue *args, ExtValue *ret) 66 80 { 67 Pt3D sizes; Orient axes; 68 ModelGeometryInfo::findSizesAndAxesOfModel(*model,density,sizes,axes); 69 VectorObject* n=new VectorObject; 70 n>data+=new ExtValue(Pt3D_Ext::makeDynamicObject(sizes)); 71 n>data+=new ExtValue(Orient_Ext::makeDynamicObject(new Orient_Ext(axes))); 72 ret>setObject(n>makeObject()); 81 onDensityChanged(); 82 if (cached_volume < 0) //calculate if invalid 83 cached_volume = ModelGeometryInfo::volume(*model, density); 84 ret>setDouble(cached_volume); 73 85 } 86 87 void ModelGeometry::p_area(ExtValue *args, ExtValue *ret) 88 { 89 onDensityChanged(); 90 if (cached_area < 0) //calculate if invalid 91 cached_area = ModelGeometryInfo::area(*model, density); 92 ret>setDouble(cached_area); 93 } 94 95 void ModelGeometry::p_sizesandaxes(ExtValue *args, ExtValue *ret) 96 { 97 onDensityChanged(); 98 if (cached_sizes.x < 0) //calculate if invalid 99 ModelGeometryInfo::findSizesAndAxesOfModel(*model, density, cached_sizes, cached_axes); 100 101 VectorObject* n = new VectorObject; 102 n>data += new ExtValue(Pt3D_Ext::makeDynamicObject(cached_sizes)); 103 n>data += new ExtValue(Orient_Ext::makeDynamicObject(new Orient_Ext(cached_axes))); 104 ret>setObject(n>makeObject()); 105 }
Note: See TracChangeset
for help on using the changeset viewer.