CGPdocs

vec3

3D vector of floats (positions, directions, colors).

alias of numarray_stack<float,3>module 02_numarraysource: numarray_stack3.hpp
vec2, vec3 and vec4 store their floats x, y, z, w side by side (indices 0 to 3); the same element has four spellings, and the common calls are norm, normalize, dot and cross.
vec2, vec3 and vec4 store their floats x, y, z, w side by side (indices 0 to 3); the same element has four spellings, and the common calls are norm, normalize, dot and cross. Tap to enlarge.

Fields and methods

Members of the struct numarray_stack<T,3>. Most of what you do with a CGP type goes through the free functions listed below.

Fields

T xFirst coordinate of the vector.
T ySecond coordinate of the vector.
T zThird coordinate of the vector.

Methods

numarray_stack()Builds a vector of size 2, 3 or 4 with all coordinates value-initialized (zero for numbers).5 overloads src
numarray_stack(T const& x, T const& y, T const& z)Builds a 3D vector from its coordinates x, y and z. src
numarray_stack(T const& x, numarray_stack<T,2> const& yz)Builds a 3D vector from x and a 2D vector giving (y, z). src
numarray_stack(numarray_stack<T,2> const& xy, T const& z)Builds a 3D vector from a 2D vector giving (x, y) and z. src
numarray_stack(T1 const& x, T2 const& y, T3 const& z)Builds a 3D vector from three coordinates of other types, each converted with T(...). src
Number of components N, fixed at compile time. src
Sets the N components to the given value and returns the vector. src
Returns the element at index, bound-checked unless CGP_NO_DEBUG is defined. src
Returns a const reference to the element at the given index, with bound checking unless CGP_NO_DEBUG is defined. src
Returns a const reference to the element at the given index; bound-checked for sizes 2, 3 and 4 (vec2, vec3, vec4) unless CGP_NO_DEBUG is defined, but not checked for the other sizes N. src
Returns a const iterator to the first element (a plain pointer for sizes 2, 3 and 4). src
Returns a const iterator one past the last element (a plain pointer for sizes 2, 3 and 4). src
Returns a const iterator to the first element (a plain pointer for sizes 2, 3 and 4). src
Returns a const iterator one past the last element (a plain pointer for sizes 2, 3 and 4). src
Returns a const reference to the element at the given index, without bound checking. src
Returns a copy of the coordinates (x, y) as a 2D vector (for vec3 and vec4). src
Returns a copy of the coordinates (y, z) as a 2D vector (for vec3 and vec4). src
Returns a copy of the coordinates (x, z) as a 2D vector (for vec3 and vec4). src

Functions taking a vec3

Construct 18

Documented on another page

  • camera_frame_look_atCamera frame placed at eye and looking toward center (local -z axis), with its y axis aligned as much as possible with up.on frame
  • curve_primitive_circleClosed circle polyline of the given radius and center in the plane orthogonal to the unit normal, the last sample repeating the first one.on numarray
  • mesh_primitive_arrowArrow 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).on mesh
  • mesh_primitive_coneCone 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.on mesh
  • mesh_primitive_cubeAxis-aligned cube mesh of the given center and edge length, made of six independent two-triangle faces with flat normals.on mesh
  • mesh_primitive_cubic_gridHexahedron mesh given by its eight corners, each of its six faces sampled as a regular grid (Nx, Ny, Nz samples along each direction).on mesh
  • mesh_primitive_cylinderCylinder 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.on mesh
  • mesh_primitive_discDisc 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.on mesh
  • mesh_primitive_ellipsoidEllipsoid mesh with semi-axes scale.x, scale.y, scale.z centered at center, with normals and uv.on mesh
  • mesh_primitive_frameMesh 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.on mesh
  • mesh_primitive_gridBilinear 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)).on mesh
  • mesh_primitive_quadrangleQuadrangle 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.on mesh
  • mesh_primitive_sphereSphere 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.on mesh
  • mesh_primitive_tetrahedronTetrahedron mesh given by its four corners, made of four independent triangles with flat normals.on mesh
  • mesh_primitive_torusTorus mesh of major radius r_max and minor radius r_min, centered at center around the unit axis axis_orientation, with normals and uv.on mesh
  • mesh_primitive_triangleSingle-triangle mesh (p0,p1,p2) with a flat normal.on mesh
  • rotation_axis_angleRotation of a given angle in radians around an axis (normalized internally).on rotation_transform
  • tensor_productOuter product a * transpose(b), as a 3x3 matrix.on mat3

Transform 1

vec3 orthogonal_vector(vec3 const& v)

Unit vector orthogonal to v, built from its cross product with a coordinate axis; returns (1,0,0) for a zero vector.

Compute 7

vec3 cross(vec3 const& a, vec3 const& b)

Cross product (vec3 only).

intersection_structure intersection_ray_plane(
    vec3 const& ray_origin,
    vec3 const& ray_direction,
    vec3 const& plane_position,
    vec3 const& plane_normal
)

Intersection in front of the origin of a ray with a plane given by a point and a normal, invalid if the ray is parallel to the plane.

intersection_structure intersection_ray_sphere(
    vec3 const& ray_origin,
    vec3 const& ray_direction,
    vec3 const& sphere_center,
    float sphere_radius
)

First intersection in front of the origin of a ray (unit direction expected) with a sphere, with its position and outward normal.

intersection_structure intersection_ray_spheres_closest(
    vec3 const& ray_origin,
    vec3 const& ray_direction,
    numarray<vec3> const& sphere_centers,
    float sphere_radius,
    int* shape_index = nullptr
)

Closest intersection of a ray with a set of spheres of common radius, the index of the hit sphere (or -1) being written to shape_index if given.

float noise_perlin(
    vec3 const& p,
    int octave = 5,
    float persistency = 0.3f,
    float frequency_gain = 2.0f
)

Fractal 3D noise in [0,1] summing octave layers of simplex noise, each layer scaled in amplitude by persistency and in frequency by frequency_gain (defaults 5, 0.3, 2).

Documented on another page

  • picking_plane_orthogonal_to_cameraIntersects the camera ray through the screen position (relative coordinates) with the plane through plane_position orthogonal to the front direction of the camera.on vec2
  • rotation_around_centerRigid transform that applies the rotation R around center instead of the origin, mapping p to R(p - center) + center.on rotation_transform

Draw 3

Documented on another page

  • draw_wireframeDraw the triangle edges of every node of the hierarchy in a flat color (no effect with Emscripten/WebGL).on hierarchy_mesh_drawable
  • draw_wireframeDraw the triangle edges of a mesh_drawable in a flat color, slightly offset toward the camera (no effect with Emscripten/WebGL).on mesh_drawable
  • draw_wireframeDraw the triangle edges of a triangles_drawable in a flat color, slightly offset toward the camera (no effect with Emscripten/WebGL).on triangles_drawable

Convert 2

type_str

generic2 overloads
template <typename T>
std::string type_str(numarray_stack<T,3> const&)
source

Type name as a string, e.g. "numarray_stack3<int>".

std::string type_str(numarray_stack3<float> const&)
source

Type name as a string: "vec3".

Access 2

template <typename T, int N, typename INDEX_TYPE>
void check_index_bounds(INDEX_TYPE index, numarray_stack<T,3> const& data)

Raises an error if the index is outside [0, 2]; no-op when CGP_NO_DEBUG is defined.

get

genericsource
template <int idx, typename T>
T const& get(numarray_stack<T,3> const& data)
T& get(numarray_stack<T,3>& data)

Component idx with the index checked at compile time (static_assert).

Other 1

Documented on another page

  • opengl_uniformSend a vec3 uniform to the shader; warns if the uniform is not found and expected is true.on opengl_shader_structure

Functions returning a vec3

Construct 1

vec3 trackball_projection(float x, float y)

Point (x,y,z) projected on a virtual trackball: unit sphere near the center, hyperbolic sheet z=1/(2d) beyond a distance 1/sqrt(2).

Compute 2

Documented on another page

  • camera_ray_directionUnit world-space direction of the ray through a screen position given in normalized device coordinates [-1,1]^2, from the camera frame and the inverse projection matrix.on mat4
  • translation_in_plane3D vector of a 2D translation expressed in the xy plane of the given orientation.on vec2

Access 1

Documented on another page

  • getRow idx1 as a vector, with the index checked at compile time (static_assert).on matN

Operators

Show the 13 operators (+, -, *, ==, ...)

operator+

3 overloads
affine_rt operator+(vec3 const& tr, affine_rt const& T)
source

Copy of the rigid transform with tr added to its translation.

affine_rts operator+(vec3 const& tr, affine_rts const& T)
source

Copy of the transform with tr added to its translation.

frame operator+(vec3 const& tr, frame const& f)
source

Copy of the frame with its position translated by tr.

Documented on another page

  • operator*Apply the affine transform to a point: rotation * (scaling * scaling_xyz * p) + translation (scaling_xyz stretches the local axes).on affine
  • operator*Apply the rigid transform to a point: rotation * p + translation.on affine_rt
  • operator*Apply the transform to a point: scaling * (rotation * p) + translation.on affine_rts
  • operator*Vector rotated by the rotation.on rotation_transform
  • operator+Copy of the rigid transform with tr added to its translation.on affine_rt
  • operator+Copy of the transform with tr added to its translation.on affine_rts
  • operator+Copy of the frame with its position translated by tr.on frame
  • operator-Copy of the rigid transform with tr subtracted from its translation.on affine_rt
  • operator-Copy of the transform with tr subtracted from its translation.on affine_rts
  • operator-Copy of the frame with its position translated by -tr.on frame
CGP - Computer Graphics Programming.