Edit a mesh
Edit a mesh "in-place" is a little more complicated than create a mesh from scratch. First of all, you may have side-effects, although it can be helpful, it's sometimes hard to understand what we are currently doing on the mesh. But the difficulty lies mainly in adapting your code to whether you're modifying a connected or unconnected mesh. Following examples works on connected meshes. If you don't know what connectivity is, we invite you to read "Browse a mesh" page.
First of all, for all following examples we'll create a cube:
Info
If you want more info about creating a mesh, please refer to Create a mesh page.
// Init a Quad mesh
// We will create a cube
Quads m;
// Create and place 8 points
m.points.create_points(8);
m.points[0] = vec3(-1, -1, 1);
m.points[1] = vec3(1, -1, 1);
m.points[2] = vec3(1, -1, -1);
m.points[3] = vec3(-1, -1, -1);
m.points[4] = vec3(-1, 1, 1);
m.points[5] = vec3(1, 1, 1);
m.points[6] = vec3(1, 1, -1);
m.points[7] = vec3(-1, 1, -1);
// Create cube facets
m.create_facets(6);
// Link facet 0, vertex 0 with point 0
m.vert(0, 0) = 0;
// Link facet 0, vertex 1 with point 1
m.vert(0, 1) = 1;
// ...
m.vert(0, 2) = 2;
m.vert(0, 3) = 3;
// Link facet 1, vertex 0 with point 4
m.vert(1, 0) = 4;
// ...
m.vert(1, 1) = 5;
m.vert(1, 2) = 6;
m.vert(1, 3) = 7;
m.vert(2, 0) = 0;
m.vert(2, 1) = 1;
m.vert(2, 2) = 5;
m.vert(2, 3) = 4;
m.vert(3, 0) = 3;
m.vert(3, 1) = 2;
m.vert(3, 2) = 6;
m.vert(3, 3) = 7;
m.vert(4, 0) = 0;
m.vert(4, 1) = 4;
m.vert(4, 2) = 7;
m.vert(4, 3) = 3;
m.vert(5, 0) = 1;
m.vert(5, 1) = 5;
m.vert(5, 2) = 6;
m.vert(5, 3) = 2;
// Connect mesh
m.connect();
// --- SAVE ---
// Save cube
write_by_extension("cube.geogram", m);
You should obtain something which looks like a cube:
Warning
As you edit a connected mesh, you must access to conn
field. conn
field give an access to higher-level functions that lets you modify a mesh while updating connectivity automatically. If you try to update a connected mesh using low-level functions, an error is reported.
Delete facet
In a connected mesh, the only thing to do to remove a facet is to deactivate it. When a facet is deactivated, it still exists but is no longer used. To delete it completely, simply call compact()
on mesh. compact
delete all deactivated facets and eventually isolated vertices.
// Delete facet
// Deactivate facet 5
m.conn->active[5] = false;
// Remove deactivated facets
m.compact();
// Save cube without a face
write_by_extension("cube_without_face.geogram", m);
You should obtain something like this:
Create facet
To create a facet in a connected mesh, you just have to call create_facet
and give an initializer list as parameter which contains the vertices ids of facet.
// Create facet (fill hole)
m.conn->create_facet({1, 5, 6, 2});
// Save
write_by_extension("cube_filled.geogram", m);
You should obtain the same cube as previously:
Move a vertex
Moving a vertex is very simple, you just need to retrieve the point you wish to move and assign it a new position. Here we get 4 vertex of a cube facet and just move them on the y
axis:
// We move all points of facet 0
m.points[0] = vec3(-1, -2, 1);
m.points[1] = vec3(1, -2, 1);
m.points[2] = vec3(1, -2, -1);
m.points[3] = vec3(-1, -2, -1);
// Save
write_by_extension("cube_deform.geogram", m);
You should obtain a deformed cube, that isn't a cube anymore: