URL: https://graphicscomputing.fr/cgp/reference/vec3/
Description: 3D vector of floats (positions, directions, colors).

# vec3

C++ type: `numarray_stack<float,3>`.
3D vector of floats (positions, directions, colors).

Fields:

- `T x` - First coordinate of the vector.
- `T y` - Second coordinate of the vector.
- `T z` - Third coordinate of the vector.

Functions taking it:

- `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.
- `T const& get(numarray_stack<T,3> const& data)` - Component idx with the index checked at compile time (static_assert).
- `std::string type_str(numarray_stack<T,3> const&)` - Type name as a string, e.g. "numarray_stack3<int>".
- `std::string type_str(numarray_stack3<float> const&)` - Type name as a string: "vec3".
- `vec3 cross(vec3 const& a, vec3 const& b)` - Cross product (vec3 only).
- `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.
- `mat3 tensor_product(vec3 const& a, vec3 const& b)` - Outer product a * transpose(b), as a 3x3 matrix.
- `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).
- `affine_rt rotation_around_center(rotation_transform const& R, vec3 const& center)` - Rigid transform that applies the rotation R around center instead of the origin, mapping p to R(p - center) + center.
- `rotation_transform rotation_axis_angle(vec3 const& axis, float angle)` - Rotation of a given angle in radians around an axis (normalized internally).
- `frame camera_frame_look_at(vec3 const& eye, vec3 const& center, vec3 const& up)` - Camera frame placed at eye and looking toward center (local -z axis), with its y axis aligned as much as possible with up.
- `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.
- `numarray<vec3> curve_primitive_circle(float radius = 1.0f, vec3 const& center = {0,0,0}, vec3 const& normal = {0,0,1}, int N_sample = 20)` - Closed circle polyline of the given radius and center in the plane orthogonal to the unit normal, the last sample repeating the first one.
- `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_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_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.
- `void opengl_uniform(opengl_shader_structure const& shader, std::string const& name, vec3 const& value, bool expected = true)` - Send a vec3 uniform to the shader; warns if the uniform is not found and expected is true.
- `void draw_wireframe(hierarchy_mesh_drawable const& drawable, environment_generic_structure const& environment = environment_generic_structure(), vec3 const& color = { 0,0,1 }, int instance_count = 1, bool expected_uniforms = true, uniform_generic_structure const& additional_uniforms = uniform_generic_structure())` - Draw the triangle edges of every node of the hierarchy in a flat color (no effect with Emscripten/WebGL).
- `void draw_wireframe(mesh_drawable const& drawable, environment_generic_structure const& environment = environment_generic_structure(), vec3 const& color = {0,0,1}, int instance_count = 1, bool expected_uniforms = true, uniform_generic_structure const& additional_uniforms = uniform_generic_structure())` - Draw the triangle edges of a mesh_drawable in a flat color, slightly offset toward the camera (no effect with Emscripten/WebGL).
- `void draw_wireframe(triangles_drawable const& drawable, environment_generic_structure const& environment = environment_generic_structure(), vec3 const& color = {0,0,1}, uniform_generic_structure const& additional_uniforms = uniform_generic_structure())` - Draw the triangle edges of a triangles_drawable in a flat color, slightly offset toward the camera (no effect with Emscripten/WebGL).
- `intersection_structure picking_plane_orthogonal_to_camera(vec2 const& screen_position, vec3 const& plane_position, camera_generic_base const& camera, camera_projection_perspective const& projection)` - Intersects the camera ray through the screen position (relative coordinates) with the plane through plane_position orthogonal to the front direction of the camera.

Functions returning it:

- `numarray_stack<T,N2> const& get(matrix_stack<T,N1,N2> const& data)` - Row idx1 as a vector, with the index checked at compile time (static_assert).
- `vec3 camera_ray_direction(mat4 const& camera_frame, mat4 const& perspective_inverse, vec2 const& screen_position)` - Unit 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.
- `vec3 translation_in_plane(vec2 const& translation, rotation_transform const& plane_orientation)` - 3D vector of a 2D translation expressed in the xy plane of the given orientation.
- `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).

Operators:

- `vec3 operator*(affine const& T, vec3 const& p)` - Apply the affine transform to a point: rotation * (scaling * scaling_xyz * p) + translation (scaling_xyz stretches the local axes).
- `vec3 operator*(affine_rt const& T, vec3 const& p)` - Apply the rigid transform to a point: rotation * p + translation.
- `affine_rt operator+(vec3 const& tr, affine_rt const& T)` - Copy of the rigid transform with tr added to its translation.
- `affine_rt operator+(affine_rt const& T, vec3 const& tr)` - Copy of the rigid transform with tr added to its translation.
- `affine_rt operator-(affine_rt const& T, vec3 const& tr)` - Copy of the rigid transform with tr subtracted from its translation.
- `vec3 operator*(affine_rts const& T, vec3 const& p)` - Apply the transform to a point: scaling * (rotation * p) + translation.
- `affine_rts operator+(vec3 const& tr, affine_rts const& T)` - Copy of the transform with tr added to its translation.
- `affine_rts operator+(affine_rts const& T, vec3 const& tr)` - Copy of the transform with tr added to its translation.
- `affine_rts operator-(affine_rts const& T, vec3 const& tr)` - Copy of the transform with tr subtracted from its translation.
- `frame operator+(frame const& f, vec3 const& tr)` - Copy of the frame with its position translated by tr.
- `frame operator+(vec3 const& tr, frame const& f)` - Copy of the frame with its position translated by tr.
- `frame operator-(frame const& f, vec3 const& tr)` - Copy of the frame with its position translated by -tr.
- `vec3 operator*(rotation_transform const& r, vec3 const& p)` - Vector rotated by the rotation.
