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

Generate the figure that consists of multiple spheres merged into the single surface object without booleans

// Generate the figure that consists of multiple spheres merged into the single surface object without booleans
#include <CoreAPI.h>
//@config: Debug
// this is the class with parameters. It is used in UI and for the generating as well. Look the dialogs example.
class SpheresGenerator:public BaseClass {
public:
SpheresGenerator() {
numSpheres = 200;
FigureRadius = 30;
FigureRadiusVariation = 0.1;
SpheresRadius = 10;
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 surface spheres
void generate() {
// load the sphere mesh
m.Read("data/Samples/Sphere.obj");
// add new volume
auto current = coat::Scene::sculptRoot().addChild("Random spheres:" + coat::str::ToString(numSpheres));
auto volume = current.Volume();
// turn to voxels
volume.toSurface();
coat::Mesh summ;
// collect all meshes into the single mesh
for (int i = 0; i < numSpheres; i++) {
// create transformation matrix.
// scaling to the sphere radius
coat::mat4 transform = coat::mat4::Scaling(random(SpheresRadius, SpheresRadiusVariation));
// translate using the random vector
transform *= coat::mat4::Translation(coat::vec3::RandNormal() * random(FigureRadius, FigureRadiusVariation));
// transform and add the mesh to the summary mesh
summ.addTransformed(m, transform);
}
// merge the summary mesh into the scene
volume.mergeMesh(summ, coat::mat4::Identity, coat::BOOL_MERGE);
}
};
EXPORT
int main() {
// get to sculpt room
coat::ui::toRoom("Sculpt");
// create the generator object
SpheresGenerator sg;
// load generator settings if exist
sg.ReadFromFile("data/Temp/Spheres.json");
if(dlg.ok().cancel().params(&sg).show() == 1) {// ok pressed, buttons start from 1
// save settings
sg.WriteToFile("data/Temp/Spheres.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
The mesh reference.
Definition CoreAPI.h:401
void addTransformed(const Mesh &m, const mat4 &t)
concatenate the transformed mesh with the current one
bool Read(const char *name)
Load the mesh from the file.
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:3593
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()
pass the function/lambda that will be called when the button will be pressed. The button index (start...
static void toRoom(const char *name)
switch to the room
@ BOOL_MERGE
just merge, no booleans, it may be used only in surface mode
Definition CoreAPI.h:89
comms::cMat4 mat4
4x4 float matrix, see the cMat4
Definition CoreAPI.h:59