URL: https://graphicscomputing.fr/cgp/reference/mesh/
Description: Triangle mesh stored on the CPU (positions, normals, colors, uv, connectivity).

# mesh

Triangle mesh stored on the CPU (positions, normals, colors, uv, connectivity).

Fields:

- `numarray<vec3> position` - Per-vertex 3D positions of the mesh, indexed by the triangles of connectivity.
- `numarray<vec3> normal` - Per-vertex normals, expected to have the same size as position (filled by fill_empty_field or normal_update when empty).
- `numarray<vec3> color` - Per-vertex RGB colors in [0,1], set to white by fill_empty_field when missing.
- `numarray<vec2> uv` - Per-vertex texture coordinates, set to (0,0) by fill_empty_field when missing.
- `numarray<uint3> connectivity` - Triangles of the mesh, each given as a triplet of indices into the per-vertex arrays.

Functions taking it:

- `bool mesh_check(mesh const& m)` - Check buffer sizes and triangle indices of a mesh, printing a warning for each problem found (including zero-length edges and unused vertices), and return false if a buffer or index is invalid.
- `std::string str(mesh const& m)` - Short summary of a mesh giving its number of vertices and triangles.
- `std::string type_str(mesh const&)` - Type name "mesh".
- `void mesh_save_file_obj(std::string const& filename, mesh const& m)` - Save a mesh as an .obj file with its positions, uv and normals (per-vertex colors are not stored).

Functions returning it:

- `mesh mesh_primitive_cylinder(float radius = 0.2f, vec3 const& p0 = {0,0,0}, vec3 const& p1 = {0,0,1}, int Nu = 10, int Nv = 20, bool is_closed = false)` - Cylinder mesh of the given radius around the axis [p0,p1], open by default or capped by two discs if is_closed is true, with normals and uv; Nu samples along the axis and Nv around it.
- `mesh mesh_primitive_triangle(vec3 const& p0 = {0,0,0}, vec3 const& p1 = {1,0,0}, vec3 const& p2 = {0,1,0})` - Single-triangle mesh (p0,p1,p2) with a flat normal.
- `mesh mesh_primitive_quadrangle(vec3 const& p00 = {0,0,0}, vec3 const& p10 = {1,0,0}, vec3 const& p11 = {1,1,0}, vec3 const& p01 = {0,1,0})` - Quadrangle through the corners p00, p10, p11, p01, made of the triangles (p00,p10,p11) and (p00,p11,p01), with uv (0,0), (1,0), (1,1), (0,1) at the corners.
- `mesh mesh_primitive_disc(float radius = 1.0f, vec3 const& center = {0,0,0}, vec3 const& normal = {0,0,1}, int N = 40)` - Disc of the given radius centered at center and orthogonal to normal, made of N vertices on the circle linked to a central vertex, with a constant normal and uv mapping the disc into the unit square.
- `mesh mesh_primitive_sphere(float radius = 1.0f, vec3 const& center = {0,0,0}, int Nu = 40, int Nv = 20)` - Sphere of the given radius and center sampled in spherical coordinates, Nu samples around the z axis and Nv from the south to the north pole, closed at the poles, with normals and uv.
- `mesh mesh_primitive_ellipsoid(vec3 scale = vec3{1.0f, 1.0f, 1.0f}, vec3 const& center = {0,0,0}, int Nu = 40, int Nv = 20)` - Ellipsoid mesh with semi-axes scale.x, scale.y, scale.z centered at center, with normals and uv.
- `mesh mesh_primitive_torus(float r_max = 1.0f, float r_min = 0.25f, vec3 const& center = {0,0,0}, vec3 const& axis_orientation = {0,0,1}, int Nu = 55, int Nv = 15)` - Torus mesh of major radius r_max and minor radius r_min, centered at center around the unit axis axis_orientation, with normals and uv.
- `mesh mesh_primitive_grid(vec3 const& p00 = {0,0,0}, vec3 const& p10 = {1,0,0}, vec3 const& p11 = {1,1,0}, vec3 const& p01 = {0,1,0}, int Nu = 10, int Nv = 10)` - Bilinear patch through the four corners p00, p10, p11, p01, sampled as an Nu x Nv grid, with normals and uv; sample (ku,kv) has index kv + Nv*ku and uv (ku/(Nu-1), kv/(Nv-1)).
- `mesh mesh_primitive_cubic_grid(vec3 const& p000 = {0,0,0}, vec3 const& p100 = {1,0,0}, vec3 const& p110 = {1,1,0}, vec3 const& p010 = {0,1,0}, vec3 const& p001 = {0,0,1}, vec3 const& p101 = {1,0,1}, vec3 const& p111 = {1,1,1}, vec3 const& p011 = {0,1,1}, int Nx = 10, int Ny = 10, int Nz = 10)` - Hexahedron mesh given by its eight corners, each of its six faces sampled as a regular grid (Nx, Ny, Nz samples along each direction).
- `mesh mesh_primitive_cube(vec3 const& center = {0,0,0}, float edge_length = 1.0f)` - Axis-aligned cube mesh of the given center and edge length, made of six independent two-triangle faces with flat normals.
- `mesh mesh_primitive_tetrahedron(vec3 const& p0 = {0,0,0}, vec3 const& p1 = {1,0,0}, vec3 const& p2 = {0,1,0}, vec3 const& p3 = {0,0,1})` - Tetrahedron mesh given by its four corners, made of four independent triangles with flat normals.
- `mesh mesh_primitive_cone(float radius = 0.5f, float height = 1.0f, vec3 const& center_of_base = {0,0,0}, vec3 const& axis_direction = {0,0,1}, bool is_closed_base = true, int Nu = 20, int Nv = 10)` - Cone mesh of base radius and height, with its base centered at center_of_base and pointing along the unit axis_direction, its base capped by a disc by default; Nu samples around the axis and Nv along the height.
- `mesh mesh_primitive_arrow(vec3 const& p0 = {0,0,0}, vec3 const& p1 = {1,0,0}, float cylinder_radius = 0.05f, float cone_height_scale = 4.0f, float cone_radius_scale = 2.5f, int N = 18)` - Arrow mesh from p0 to p1 made of a cylinder and a cone tip ending at p1, the cone length and radius being multiples of cylinder_radius (4 and 2.5 by default).
- `mesh mesh_primitive_frame(frame const& f = frame(), float scale = 1.0f, vec3 const& color_ux = {1,0,0}, vec3 const& color_uy = {0,1,0}, vec3 const& color_uz = {0,0,1}, vec3 const& color_sphere = {1,1,1}, float frame_thickness = 0.025f)` - Mesh of the frame f: three arrows of length scale along ux, uy and uz (red, green and blue by default) and a sphere at its origin, the arrow radius being frame_thickness*scale.
- `mesh marching_cube(grid_3D<float> const& field, spatial_domain_grid_3D const& domain, float iso)` - Extracts the iso-surface of value iso of a field sampled on the grid domain (same dimension) as a new mesh whose triangles share their vertices, with normals computed by fill_empty_field.
- `mesh mesh_load_file_obj(std::string const& filename)` - Load a single mesh from an .obj file, triangulating polygons and duplicating vertices where uv or normals differ (.mtl files are ignored).
- `mesh mesh_load_file_obj(std::string const& filename, numarray<numarray<int>>& vertex_correspondance)` - Load a mesh from an .obj file like mesh_load_file_obj, also returning for each file vertex the indices of the mesh vertices created from it (empty fields are not filled).
