Untitled
Loading...
Searching...
No Matches
utilities.c File Reference
#include "mathf.h"
#include "types.h"
#include <limits.h>
#include <math.h>
#include <stdlib.h>
#include "utilities.h"
#include <string.h>

Go to the source code of this file.

Macros

#define DEGREES_TO_RAD   (3.14159265358979323846 / 180.0)

Functions

int vector_alloc (Vector *v, size_t i)
int vector_push (Vector *v, void *o)
void * vector_expand (Vector *v, size_t i)
void init_vector (Vector *v, size_t i, size_t type_size)
void free_vector (Vector *v)
mat4 compose_2d_transform (vec2 transform, float angle, vec2 scale)
uint16_t float32_to_float16 (float f)
float mod289 (float x)
vec4 perm (vec4 x)
vec3 vec3_fract (vec3 x)
vec4 vec4_fract (vec4 x)
float fbm_noise (vec3 p)
float fbm (vec3 p)
bool test_sphere_aabb (vec3 light_pos, float light_radius, vec3 min, vec3 max)
void init_aabb (AABB *a, vec3 center, vec3 extents)
bool aabbs_intersect (AABB *a, AABB *b)
void calculate_aabb_extents (AABB *aabb)
void calculate_aabb_bounds (AABB *aabb)
float triangle_area (vec3 a, vec3 b, vec3 c)
bool is_similar_plane (vec3 n1, vec3 n2, vec3 p1, vec3 p2, float epsilon)
bool ray_intersects_triangle (vec3 origin, vec3 dir, vec3 a, vec3 b, vec3 c, float *out_t, vec3 *out_hit_point, vec3 *barycentric)
bool compute_barycentric (const vec2 uv0, const vec2 uv1, const vec2 uv2, const vec2 p, float *b0, float *b1, float *b2)
void compute_3d_barycentric (vec3 a, vec3 b, vec3 c, vec3 p, float *wA, float *wB, float *wC)
vec3 generate_bounce (uint32_t i, uint32_t N)
vec3 generate_sphere_dir (int i, int N)
AABB transform_aabb_by_position (AABB aabb, vec3 pos)
AABB transform_aabb_by_model_matrix (AABB aabb, mat4 mat)
AABB transform_aabb_by_model_matrix_rotation (AABB aabb, mat4 mat)
bool vec4_equal (vec4 a, vec4 b, float eps)
bool vec3_equal (vec3 a, vec3 b, float eps)
bool triangles_are_contiguous (Triangle *a, Triangle *b)
bool mesh_faces_are_contiguous (MeshFace *a, vec3 *a_verts, MeshFace *b, vec3 *b_verts)
int leftmost_point (vec3 *points, int count)
int leftmost_pointr (vec3 *points, int count)
size_t leftmost_pointi (vec3 *points, size_t *indices, size_t count)
size_t leftmost_pointri (vec3 *points, size_t *indices, size_t count)
size_t leftmost_pointv2 (vec2 *points, size_t count)
bool ccwv2 (vec2 p, vec2 a, vec2 b)
bool ccw (vec3 p, vec3 a, vec3 b)
bool ccwr (vec3 p, vec3 a, vec3 b)
void compute_convex_hull (vec3 *points, int count, vec3 *out_hull, int *out_count, vec3 normal)
void compute_hull_faces (vec3 *points, size_t *indices, size_t count, size_t *out_hull, size_t *out_count, vec3 normal)
void compute_aabb_from_points (vec3 *points, int point_count, AABB *aabb)
int clip_against_plane (const vec3 *in, int in_count, vec3 *out, vec3 plane_point, vec3 plane_normal)
int clip_pts_against_plane (ClipVertex *in, int in_count, ClipVertex *out, vec3 plane_point, vec3 plane_normal)
int clip_tri_against_plane (Vector *points, Vector *faces, vec3 plane, vec3 plane_point, vec3 face_points[3], vec3 j_normal)
bool perform_clip (Vector *points, Vector *faces, vec3 planes[6], vec3 plane_points[6], vec3 face_points[3], vec3 normal)
void clip_triangle (Vector *points, Vector *faces, vec3 planes[6], vec3 plane_points[6], vec3 a, vec3 b, vec3 c, vec3 normal)
bool point_in_triangle (vec3 P, vec3 A, vec3 B, vec3 C)
vec3 project_point_plane (vec3 point, vec3 plane_point, vec3 normal)
vec3 closest_point_segment (vec3 p, vec3 a, vec3 b)
float point_to_segment_dist (vec2 p, vec2 a, vec2 b)
vec3 segment_plane_distance (vec3 proposed, vec3 pts[3], vec3 normal)
vec3 point_triangle_distance (vec3 proposed, vec3 pts[3], vec3 normal)
vec3 closest_point_triangle (vec3 p, vec3 a, vec3 b, vec3 c)
vec3 spherical_to_cartesian (float azimuth_deg, float elevation_deg, float radius)

Macro Definition Documentation

◆ DEGREES_TO_RAD

#define DEGREES_TO_RAD   (3.14159265358979323846 / 180.0)

Definition at line 1381 of file utilities.c.

Referenced by spherical_to_cartesian().

Function Documentation

◆ vector_alloc()

int vector_alloc ( Vector * v,
size_t i )

Definition at line 12 of file utilities.c.

References Vector::buffer, Vector::type_size, and vector_alloc().

Referenced by vector_alloc(), vector_expand(), and vector_push().

◆ vector_push()

int vector_push ( Vector * v,
void * o )

◆ vector_expand()

◆ init_vector()

◆ free_vector()

void free_vector ( Vector * v)

Definition at line 90 of file utilities.c.

References Vector::buffer, and free_vector().

Referenced by free_vector().

◆ compose_2d_transform()

◆ float32_to_float16()

uint16_t float32_to_float16 ( float f)

Definition at line 110 of file utilities.c.

References float32_to_float16().

Referenced by float32_to_float16().

◆ mod289()

float mod289 ( float x)

Definition at line 133 of file utilities.c.

References mod289().

Referenced by mod289(), and perm().

◆ perm()

vec4 perm ( vec4 x)

◆ vec3_fract()

vec3 vec3_fract ( vec3 x)

Definition at line 160 of file utilities.c.

References vec3_fract(), vec3::x, vec3::y, and vec3::z.

Referenced by vec3_fract().

◆ vec4_fract()

vec4 vec4_fract ( vec4 x)

Definition at line 175 of file utilities.c.

References vec4_fract(), vec4::w, vec4::x, vec4::y, and vec4::z.

Referenced by fbm_noise(), and vec4_fract().

◆ fbm_noise()

◆ fbm()

float fbm ( vec3 p)

Definition at line 221 of file utilities.c.

References fbm(), fbm_noise(), vec3_add(), and vec3_scale().

Referenced by compute_hull_centroid(), fbm(), and precompute_fbm_texture().

◆ test_sphere_aabb()

bool test_sphere_aabb ( vec3 light_pos,
float light_radius,
vec3 min,
vec3 max )

Definition at line 246 of file utilities.c.

References test_sphere_aabb(), vec3_clamp(), vec3_dot(), and vec3_sub().

◆ init_aabb()

void init_aabb ( AABB * a,
vec3 center,
vec3 extents )

◆ aabbs_intersect()

◆ calculate_aabb_extents()

◆ calculate_aabb_bounds()

◆ triangle_area()

float triangle_area ( vec3 a,
vec3 b,
vec3 c )

◆ is_similar_plane()

bool is_similar_plane ( vec3 n1,
vec3 n2,
vec3 p1,
vec3 p2,
float epsilon )

Definition at line 330 of file utilities.c.

References is_similar_plane(), and vec3_dot().

Referenced by compute_mesh_hulls(), and is_similar_plane().

◆ ray_intersects_triangle()

bool ray_intersects_triangle ( vec3 origin,
vec3 dir,
vec3 a,
vec3 b,
vec3 c,
float * out_t,
vec3 * out_hit_point,
vec3 * barycentric )

◆ compute_barycentric()

bool compute_barycentric ( const vec2 uv0,
const vec2 uv1,
const vec2 uv2,
const vec2 p,
float * b0,
float * b1,
float * b2 )

Definition at line 385 of file utilities.c.

References compute_barycentric(), vec2::x, and vec2::y.

Referenced by compute_barycentric(), and compute_hull_centroid().

◆ compute_3d_barycentric()

void compute_3d_barycentric ( vec3 a,
vec3 b,
vec3 c,
vec3 p,
float * wA,
float * wB,
float * wC )

Definition at line 409 of file utilities.c.

References compute_3d_barycentric(), vec3_cross(), vec3_length(), and vec3_sub().

◆ generate_bounce()

vec3 generate_bounce ( uint32_t i,
uint32_t N )

Definition at line 429 of file utilities.c.

References generate_bounce(), M_PI, and rand().

Referenced by compute_hull_centroid(), and generate_bounce().

◆ generate_sphere_dir()

vec3 generate_sphere_dir ( int i,
int N )

Definition at line 449 of file utilities.c.

References generate_sphere_dir(), and M_PI.

◆ transform_aabb_by_position()

AABB transform_aabb_by_position ( AABB aabb,
vec3 pos )

◆ transform_aabb_by_model_matrix()

◆ transform_aabb_by_model_matrix_rotation()

◆ vec4_equal()

bool vec4_equal ( vec4 a,
vec4 b,
float eps )

Definition at line 525 of file utilities.c.

References vec4_equal(), vec4::w, vec4::x, vec4::y, and vec4::z.

Referenced by check_equivalencyv4(), compute_hull_centroid(), and vec4_equal().

◆ vec3_equal()

◆ triangles_are_contiguous()

bool triangles_are_contiguous ( Triangle * a,
Triangle * b )

◆ mesh_faces_are_contiguous()

bool mesh_faces_are_contiguous ( MeshFace * a,
vec3 * a_verts,
MeshFace * b,
vec3 * b_verts )

◆ leftmost_point()

int leftmost_point ( vec3 * points,
int count )

Definition at line 611 of file utilities.c.

References leftmost_point().

Referenced by compute_convex_hull(), and leftmost_point().

◆ leftmost_pointr()

int leftmost_pointr ( vec3 * points,
int count )

Definition at line 621 of file utilities.c.

References leftmost_pointr().

Referenced by compute_convex_hull(), and leftmost_pointr().

◆ leftmost_pointi()

size_t leftmost_pointi ( vec3 * points,
size_t * indices,
size_t count )

Definition at line 631 of file utilities.c.

References leftmost_pointi().

Referenced by leftmost_pointi().

◆ leftmost_pointri()

size_t leftmost_pointri ( vec3 * points,
size_t * indices,
size_t count )

Definition at line 641 of file utilities.c.

References leftmost_pointri().

Referenced by leftmost_pointri().

◆ leftmost_pointv2()

size_t leftmost_pointv2 ( vec2 * points,
size_t count )

Definition at line 651 of file utilities.c.

References leftmost_pointv2().

Referenced by compute_hull_faces(), and leftmost_pointv2().

◆ ccwv2()

bool ccwv2 ( vec2 p,
vec2 a,
vec2 b )

Definition at line 662 of file utilities.c.

References ccwv2(), vec2::x, and vec2::y.

Referenced by ccwv2(), and compute_hull_faces().

◆ ccw()

bool ccw ( vec3 p,
vec3 a,
vec3 b )

Definition at line 667 of file utilities.c.

References ccw(), vec3::x, and vec3::y.

Referenced by ccw(), and compute_convex_hull().

◆ ccwr()

bool ccwr ( vec3 p,
vec3 a,
vec3 b )

Definition at line 671 of file utilities.c.

References ccwr(), vec3::y, and vec3::z.

Referenced by ccwr(), and compute_convex_hull().

◆ compute_convex_hull()

void compute_convex_hull ( vec3 * points,
int count,
vec3 * out_hull,
int * out_count,
vec3 normal )

Definition at line 676 of file utilities.c.

References ccw(), ccwr(), compute_convex_hull(), leftmost_point(), leftmost_pointr(), and vec3::z.

Referenced by compute_convex_hull().

◆ compute_hull_faces()

void compute_hull_faces ( vec3 * points,
size_t * indices,
size_t count,
size_t * out_hull,
size_t * out_count,
vec3 normal )

◆ compute_aabb_from_points()

void compute_aabb_from_points ( vec3 * points,
int point_count,
AABB * aabb )

◆ clip_against_plane()

int clip_against_plane ( const vec3 * in,
int in_count,
vec3 * out,
vec3 plane_point,
vec3 plane_normal )

Definition at line 794 of file utilities.c.

References A, B, clip_against_plane(), vec3_add(), vec3_dot(), vec3_scale(), and vec3_sub().

Referenced by clip_against_plane(), and perform_clip().

◆ clip_pts_against_plane()

int clip_pts_against_plane ( ClipVertex * in,
int in_count,
ClipVertex * out,
vec3 plane_point,
vec3 plane_normal )

◆ clip_tri_against_plane()

int clip_tri_against_plane ( Vector * points,
Vector * faces,
vec3 plane,
vec3 plane_point,
vec3 face_points[3],
vec3 j_normal )

◆ perform_clip()

bool perform_clip ( Vector * points,
Vector * faces,
vec3 planes[6],
vec3 plane_points[6],
vec3 face_points[3],
vec3 normal )

◆ clip_triangle()

void clip_triangle ( Vector * points,
Vector * faces,
vec3 planes[6],
vec3 plane_points[6],
vec3 a,
vec3 b,
vec3 c,
vec3 normal )

◆ point_in_triangle()

bool point_in_triangle ( vec3 P,
vec3 A,
vec3 B,
vec3 C )

Definition at line 1204 of file utilities.c.

References A, B, C, point_in_triangle(), vec3_dot(), and vec3_sub().

Referenced by point_in_triangle().

◆ project_point_plane()

vec3 project_point_plane ( vec3 point,
vec3 plane_point,
vec3 normal )

◆ closest_point_segment()

◆ point_to_segment_dist()

float point_to_segment_dist ( vec2 p,
vec2 a,
vec2 b )

Definition at line 1256 of file utilities.c.

References point_to_segment_dist(), vec2::x, and vec2::y.

Referenced by point_to_segment_dist().

◆ segment_plane_distance()

vec3 segment_plane_distance ( vec3 proposed,
vec3 pts[3],
vec3 normal )

Definition at line 1273 of file utilities.c.

References segment_plane_distance(), vec3_add(), vec3_dot(), vec3_scale(), and vec3_sub().

Referenced by segment_plane_distance().

◆ point_triangle_distance()

vec3 point_triangle_distance ( vec3 proposed,
vec3 pts[3],
vec3 normal )

◆ closest_point_triangle()

vec3 closest_point_triangle ( vec3 p,
vec3 a,
vec3 b,
vec3 c )

◆ spherical_to_cartesian()

vec3 spherical_to_cartesian ( float azimuth_deg,
float elevation_deg,
float radius )

Definition at line 1382 of file utilities.c.

References DEGREES_TO_RAD, and spherical_to_cartesian().

Referenced by spherical_to_cartesian().