scimesh 0.3.2
Headless CPU-only 3D software renderer for scientific mesh visualization
Loading...
Searching...
No Matches
scimesh::Mesh Struct Reference

A 3D triangle mesh using an indexed face set representation. More...

#include <mesh.h>

Collaboration diagram for scimesh::Mesh:

Public Member Functions

bool has_colors () const
 Does the mesh have per-vertex colors?
 
bool has_face_colors () const
 Does the mesh have per-face colors?
 
bool has_normals () const
 Does the mesh have per-vertex normals?
 
bool has_uvs () const
 Does the mesh have texture coordinates?
 
bool has_texture () const
 Does the mesh have a texture image?
 
bool is_valid () const
 Check whether the mesh is in a valid, renderable state.
 
bool empty () const
 Is the mesh empty (no renderable geometry)?
 
void compute_bounding_box (Vec3 &min_bound, Vec3 &max_bound) const
 Compute the axis-aligned bounding box (AABB) of the mesh.
 

Public Attributes

Required data
std::vector< Vec3vertices
 3D vertex positions.
 
std::vector< Triangletriangles
 Triangle index triplets.
 
Optional per-vertex data
std::vector< Colorcolors
 Per-vertex RGBA colors.
 
std::vector< Colorface_colors
 Per-face RGBA colors.
 
std::vector< Vec3normals
 Per-vertex surface normals (unit-length direction vectors).
 
std::vector< Vec2uvs
 Per-vertex texture coordinates (UVs).
 
Optional texture
Image texture
 An optional texture image mapped via UV coordinates.
 
Default appearance
Color default_color = DEFAULT_COLOR
 Fallback color when no per-vertex or per-face color is set.
 
bool has_transparency = false
 Whether the mesh contains any transparent fragments.
 

Detailed Description

A 3D triangle mesh using an indexed face set representation.

Overview

The Mesh is the heart of scimesh. It holds:

  • vertices — 3D positions (required)
  • triangles — which three vertices form each face (required)
  • colors — per-vertex RGBA colors (optional, for vertex coloring)
  • face_colors — per-face RGBA colors (optional, for flat face coloring)
  • normals — per-vertex surface normals (optional, for lighting)
  • uvs — texture coordinates (optional, for texture mapping)
  • texture — an Image used as a texture map (optional)

The mesh uses an indexed face set. This means vertices are stored exactly once, and triangles store indices into the vertex array. This saves memory when vertices are shared by multiple triangles.

Construction

There is no special constructor — create an empty mesh and populate it:

Mesh m;
m.vertices = { {0,0,0}, {1,0,0}, {0,1,0} }; // three points
m.triangles = { {0, 1, 2} }; // one triangle
m.colors = { {1,0,0}, {0,1,0}, {0,0,1} }; // red, green, blue vertices
A 3D triangle mesh using an indexed face set representation.
Definition mesh.h:76
std::vector< Color > colors
Per-vertex RGBA colors.
Definition mesh.h:107
std::vector< Vec3 > vertices
3D vertex positions.
Definition mesh.h:86
std::vector< Triangle > triangles
Triangle index triplets.
Definition mesh.h:94

Or use one of the generator functions:

Mesh sphere = scimesh::generate_sphere({0,0,0}, 1.0f, 32, {0.2f, 0.4f, 0.8f});
Mesh generate_sphere(const Vec3 &center, float radius, int segments, const Color &color)
Generate a UV-sphere (latitude/longitude tessellation).

Or load from a file:

Mesh brain = scimesh::obj_io::read_obj("/data/brain.obj");
Mesh skull = scimesh::ply_io::read_ply("/data/skull.ply");
Mesh read_obj(const std::string &path)
Load a mesh from a Wavefront OBJ file.
Definition obj_io.cpp:8
Mesh read_ply(const std::string &path)
Load a mesh from a Stanford PLY file.
Definition ply_io.cpp:12

Validation

Call is_valid() before rendering to detect common problems:

  • missing vertices or triangles
  • out-of-bounds triangle indices
  • NaN or infinite vertex positions
  • mismatched array sizes (e.g., colors but not one per vertex)
See also
generate_sphere(), generate_cuboid(), generate_torus()
read_obj(), read_ply(), read_stl()
Renderer, Triangle, Color

Definition at line 76 of file mesh.h.

Member Function Documentation

◆ compute_bounding_box()

void scimesh::Mesh::compute_bounding_box ( Vec3 min_bound,
Vec3 max_bound 
) const
inline

Compute the axis-aligned bounding box (AABB) of the mesh.

The bounding box is the smallest box aligned with the X, Y, and Z axes that contains all mesh vertices. Output is written to the two Vec3 reference parameters.

Parameters
[out]min_boundThe corner with the smallest x, y, z values.
[out]max_boundThe corner with the largest x, y, z values.
Example
Vec3 bmin, bmax;
mesh.compute_bounding_box(bmin, bmax);
Vec3 center = (bmin + bmax) * 0.5f; // geometric center
Vec3 size = bmax - bmin; // dimensions
glm::vec3 Vec3
3-component floating-point vector (xyz).
Definition types.h:46
See also
Scene::compute_bounding_box()

Definition at line 289 of file mesh.h.

◆ empty()

bool scimesh::Mesh::empty ( ) const
inline

Is the mesh empty (no renderable geometry)?

Returns
true if there are no vertices or no triangles.
See also
is_valid()

Definition at line 265 of file mesh.h.

◆ has_colors()

bool scimesh::Mesh::has_colors ( ) const
inline

Does the mesh have per-vertex colors?

Returns
true if colors is not empty.
See also
colors

Definition at line 177 of file mesh.h.

◆ has_face_colors()

bool scimesh::Mesh::has_face_colors ( ) const
inline

Does the mesh have per-face colors?

Returns
true if face_colors is not empty.
See also
face_colors

Definition at line 182 of file mesh.h.

◆ has_normals()

bool scimesh::Mesh::has_normals ( ) const
inline

Does the mesh have per-vertex normals?

Returns
true if normals is not empty.
See also
normals, compute_vertex_normals()

Definition at line 187 of file mesh.h.

◆ has_texture()

bool scimesh::Mesh::has_texture ( ) const
inline

Does the mesh have a texture image?

Returns
true if texture.width > 0.
See also
texture, uvs

Definition at line 197 of file mesh.h.

◆ has_uvs()

bool scimesh::Mesh::has_uvs ( ) const
inline

Does the mesh have texture coordinates?

Returns
true if uvs is not empty.
See also
uvs, texture

Definition at line 192 of file mesh.h.

◆ is_valid()

bool scimesh::Mesh::is_valid ( ) const
inline

Check whether the mesh is in a valid, renderable state.

This performs several checks:

  1. Geometry exists: vertices and triangles are non-empty.
  2. Index bounds: all triangle indices are within [0, vertices.size()).
  3. No degenerate data: no NaN or infinite values in vertex positions.
  4. Array consistency: if optional arrays (colors, face_colors, normals, uvs) are present, they must have the expected size (one element per vertex or per face, as appropriate).
Why would I call this?
After constructing a mesh manually, or after loading one from a file, call is_valid() to catch errors before rendering. A mesh that fails validation will either produce garbage output or crash the renderer.
Example
if (!m.is_valid()) {
std::cerr << "Mesh failed validation — check your file!\n";
return;
}
// safe to render now
bool is_valid() const
Check whether the mesh is in a valid, renderable state.
Definition mesh.h:231
Returns
true if the mesh passes all checks.
See also
empty(), compute_bounding_box()

Definition at line 231 of file mesh.h.

Member Data Documentation

◆ colors

std::vector<Color> scimesh::Mesh::colors

Per-vertex RGBA colors.

If non-empty, must have exactly vertices.size() elements. Used for vertex-colored rendering (interpolated across triangles).

See also
Color, has_colors(), face_colors

Definition at line 107 of file mesh.h.

◆ default_color

Color scimesh::Mesh::default_color = DEFAULT_COLOR

Fallback color when no per-vertex or per-face color is set.

Defaults to DEFAULT_COLOR (neutral light gray). You can change this to give a mesh a uniform color without populating colors.

See also
DEFAULT_COLOR

Definition at line 160 of file mesh.h.

◆ face_colors

std::vector<Color> scimesh::Mesh::face_colors

Per-face RGBA colors.

If non-empty, must have exactly triangles.size() elements. Overrides per-vertex colors when present (flat shading by face).

See also
Color, has_face_colors(), colors

Definition at line 115 of file mesh.h.

◆ has_transparency

bool scimesh::Mesh::has_transparency = false

Whether the mesh contains any transparent fragments.

When true, the rasterizer enables alpha blending. Set this manually if your mesh's colors have alpha < 1.0.

Definition at line 166 of file mesh.h.

◆ normals

std::vector<Vec3> scimesh::Mesh::normals

Per-vertex surface normals (unit-length direction vectors).

If non-empty, must have exactly vertices.size() elements. Normals are used for lighting calculations (Blinn-Phong shading).

You can compute normals automatically with compute_vertex_normals().

See also
Vec3, has_normals(), compute_vertex_normals()

Definition at line 125 of file mesh.h.

◆ texture

Image scimesh::Mesh::texture

An optional texture image mapped via UV coordinates.

When set (width > 0), the rasterizer samples this image using the mesh's UV coordinates to color each pixel.

See also
Image, uvs, has_texture()

Definition at line 147 of file mesh.h.

◆ triangles

std::vector<Triangle> scimesh::Mesh::triangles

Triangle index triplets.

Each element {v0, v1, v2} points into vertices[]. A valid mesh must have at least one triangle.

See also
Triangle, vertices

Definition at line 94 of file mesh.h.

◆ uvs

std::vector<Vec2> scimesh::Mesh::uvs

Per-vertex texture coordinates (UVs).

If non-empty, must have exactly vertices.size() elements. UV coordinates range from (0,0) at bottom-left to (1,1) at top-right. Only used when a texture image is also set.

See also
Vec2, has_uvs(), texture

Definition at line 134 of file mesh.h.

◆ vertices

std::vector<Vec3> scimesh::Mesh::vertices

3D vertex positions.

Every mesh must have vertices. Each element is a Vec3 (x, y, z). Triangles reference these by index.

See also
Triangle, Vec3

Definition at line 86 of file mesh.h.


The documentation for this struct was generated from the following file: