CGPdocs

mesh

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

module 11_meshsource: mesh.hpp
A mesh of two triangles in memory - one entry per vertex in position, normal, color and uv, three vertex indices per triangle in connectivity.
A mesh of two triangles in memory - one entry per vertex in position, normal, color and uv, three vertex indices per triangle in connectivity. Tap to enlarge.

Fields and methods

Members of the struct mesh. Most of what you do with a CGP type goes through the free functions listed below.

Fields

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

Methods

Fills the missing per-vertex fields (normals computed from the triangles, white colors, uv (0,0)) and returns the mesh. src
Appends the vertices and triangles of another mesh, shifting its triangle indices by the current number of vertices, and returns the mesh. src
Swaps the first two indices of every triangle to reverse its orientation, without modifying the stored normals, and returns the mesh. src
Recomputes the per-vertex normals as the normalized sum of the unit normals of the adjacent non-degenerate triangles, and returns the mesh. src
mesh& translate(vec3 const& t)Adds t to every position, leaving the normals unchanged, and returns the mesh.2 overloads src
mesh& translate(float tx, float ty, float tz)Adds the translation (tx,ty,tz) to every position, leaving the normals unchanged, and returns the mesh. src
mesh& scale(float s)Multiplies the positions by s about the origin (a negative s also flips the normals and the triangle orientation), and returns the mesh.2 overloads src
mesh& scale(float sx, float sy, float sz)Multiplies the x, y and z coordinates of every position by sx, sy and sz, recomputes the normals (reversing the triangles when sx*sy*sz < 0) and returns the mesh. src
Rotates the positions and normals by angle in radians around axis through the origin, and returns the mesh. src
mesh& apply_transform(mat3 const& M)Multiplies every position by the 3x3 matrix M, recomputes the normals from the triangles with normal_update, and returns the mesh.5 overloads src
mesh& apply_transform(mat4 const& M)Applies the 4x4 matrix M to every position as a point, with the division by w, recomputes the normals with normal_update, and returns the mesh. src
mesh& apply_transform(affine const& M)Applies the affine transform to every position, maps each normal by the rotation and the inverse of scaling_xyz (negated for a negative scaling) before renormalizing it, reverses the triangles of a reflection, and returns the mesh. src
mesh& apply_transform(affine_rt const& M)Applies the rotation and translation to every position and the rotation to every normal, and returns the mesh. src
mesh& apply_transform(affine_rts const& M)Applies the rotation, translation and uniform scaling to every position and the rotation to every normal; a negative scaling also negates the normals and reverses the triangles; returns the mesh. src
Translates the positions so that the center of their bounding box is at the origin, and returns the mesh. src
Scales the positions about the origin so that the largest side of the bounding box is 1 (call centered() first to fit a unit cube), and returns the mesh. src
Writes the minimal and maximal corners of the axis-aligned bounding box of the positions in p_min and p_max (asserts that the mesh has a position). src

Functions taking a mesh

Compute 1

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.

Convert 2

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".

I/O 1

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 a mesh

Construct 14

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_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_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_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_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_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_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_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 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_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_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_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_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_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.

Compute 1

Documented on another page

  • marching_cubeExtracts 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.on grid_3D

I/O 2

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).

Documented on another page

  • mesh_load_file_objLoad 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).on numarray

Operators

None.

CGP - Computer Graphics Programming.