3DCoat Core API
The 3DCoat API documentation.
Loading...
Searching...
No Matches
metaballs.cpp

The metaballs example

// The metaballs example
#include <CoreAPI.h>
//@config: Release
// this is the class with parameters. It is used in UI and for the generating as well. Look the dialogs example.
class Metaballs:public BaseClass {
public:
Metaballs() {
numSpheres = 70;
FigureRadius = 80;
FigureRadiusVariation = 0.1;
SpheresRadius = 6;
SpheresRadiusVariation = 0.5;
}
int numSpheres;
float FigureRadius;
float FigureRadiusVariation;
float SpheresRadius;
float SpheresRadiusVariation;
// this is class registration, look dialogs example
SERIALIZE() {
SLIDER(numSpheres, "numSpheres", 1, 1000);
FSLIDER(FigureRadius, "FigureRadius", 1, 100, 1, false);
FSLIDER(FigureRadiusVariation, "FigureRadiusVariation", 0, 1, 100, false);
FSLIDER(SpheresRadius, "SpheresRadius", 1, 100, 1, false);
FSLIDER(SpheresRadiusVariation, "SpheresRadiusVariation", 0, 1, 100, false);
}
// generate random value with variation
float random(float value, float variation) {
return value * (1.0f + variation * rand() / 32768.0f);
}
// generate the random spheres
void generate() {
// add new volume
auto current = coat::Scene::sculptRoot().addChild("Methaballs:" + coat::str::ToString(numSpheres));
auto volume = current.Volume();
// turn to voxels
volume.toVoxels();
for (int i = 0; i < numSpheres; i++) {
radius.Add(random(SpheresRadius, SpheresRadiusVariation));
centers.Add(coat::vec3::RandNormal() * random(FigureRadius, FigureRadiusVariation));
}
// This function generates the object based on the volumetric formula f(vector) = 0.5
// It requires also the set of start points.
volume.makeVoxelFigure(
[&](coat::vec3 pos) {
float value = 0;
for (int i = 0; i < centers.Count(); i++) {
// ball radius
float r = radius[i];
// the square of the distance
float d = pos.distanceSq(centers[i]) - radius[i];
// the denominator for the formula below
float den = d + 16.0f * r - r * r;
if (den < 0.01f)den = 0.01f;
// the summ
value += 32.0f * r / den;
}
// the cut threshold
value -= 2.5f;
return value;
}, centers);
}
};
EXPORT
int main() {
// get to sculpt room
coat::ui::toRoom("Sculpt");
// create the generator object
Metaballs sg;
// load generator settings if exist
sg.ReadFromFile("data/Temp/Balls.json");
if(dlg.ok().cancel().params(&sg).show() == 1) {// ok pressed, buttons start from 1
// save settings
sg.WriteToFile("data/Temp/Balls.json");
// generate the figure
sg.generate();
};
return 0;
}
Use this class for build a class for UI or serialization. see class_reg.h for details about the class...
Definition BaseClass.h:91
SceneElement addChild(const char *name) const
add the child element of the same nature
static SceneElement sculptRoot()
get the root of all sculpt objects
the rich dialog. You may customize it, show your custom parameters and custom buttons.
Definition CoreAPI.h:3821
dialog & params(BaseClass *params)
The important core feature. BaseClass allows to create the custom controls in the dialog....
dialog & ok()
add Ok button
dialog & cancel()
add Cancel button
int show()
Show the dialog. This is usually the last command in the chain.
static void toRoom(const char *name, bool Force=false)
switch to the room
comms::cVec3 vec3
3D - float vector, see the cVec3
Definition CoreAPI.h:50
comms::cList< X > list
the array template, see cList
Definition CoreAPI.h:70