Updated GUI library
This commit is contained in:
77
code/lib/array.h
Normal file
77
code/lib/array.h
Normal file
@@ -0,0 +1,77 @@
|
||||
#ifndef _PIUMA_LIB_ARRAY_H_
|
||||
#define _PIUMA_LIB_ARRAY_H_
|
||||
|
||||
#include "types.h"
|
||||
#include "math.h"
|
||||
#include <assert.h>
|
||||
|
||||
template<typename T>
|
||||
struct Array
|
||||
{
|
||||
T *data = NULL;
|
||||
u64 count = 0;
|
||||
u64 capacity = 0;
|
||||
|
||||
inline void reserve(u64 new_capacity = 4)
|
||||
{
|
||||
new_capacity = maximum(new_capacity, count);
|
||||
if(new_capacity > capacity)
|
||||
{
|
||||
data = (T*)realloc(data, sizeof(T) * new_capacity);
|
||||
capacity = new_capacity;
|
||||
}
|
||||
}
|
||||
|
||||
inline void clear()
|
||||
{
|
||||
count = 0;
|
||||
}
|
||||
|
||||
inline void reset()
|
||||
{
|
||||
free(data);
|
||||
data = NULL;
|
||||
count = 0;
|
||||
capacity = 0;
|
||||
}
|
||||
|
||||
inline void push(T element)
|
||||
{
|
||||
if(count + 1 >= capacity)
|
||||
reserve(maximum(count + 1, capacity * 2));
|
||||
data[count++] = element;
|
||||
}
|
||||
|
||||
inline void push_unchecked(T element)
|
||||
{
|
||||
data[count++] = element;
|
||||
}
|
||||
|
||||
inline bool pop(T *element = NULL)
|
||||
{
|
||||
if(count <= 0)
|
||||
return false;
|
||||
count--;
|
||||
if(element) *element = data[count];
|
||||
return true;
|
||||
}
|
||||
|
||||
inline T pop_unchecked()
|
||||
{
|
||||
return data[--count];
|
||||
}
|
||||
|
||||
inline T &operator[](u64 index)
|
||||
{
|
||||
return data[index];
|
||||
}
|
||||
|
||||
inline T &last()
|
||||
{
|
||||
return data[count - 1];
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
@@ -37,9 +37,9 @@ struct Box
|
||||
inline bool is_inside(Box b, v3 p)
|
||||
{
|
||||
return
|
||||
(p.x < b.min.x || p.x > b.max.x) ||
|
||||
(p.y < b.min.y || p.y > b.max.y) ||
|
||||
(p.z < b.min.z || p.z > b.max.z);
|
||||
(p.x > b.min.x && p.x < b.max.x) &&
|
||||
(p.y > b.min.y && p.y < b.max.y) &&
|
||||
(p.z > b.min.z && p.z < b.max.z);
|
||||
}
|
||||
|
||||
inline bool overlaps(Box a, Box b)
|
||||
@@ -67,7 +67,7 @@ inline Box box_from_point_cloud(v3 *points, u32 count)
|
||||
Box box;
|
||||
box.min = points[0];
|
||||
box.max = points[0];
|
||||
for(u32 i = 0; i < count; i++)
|
||||
for(u32 i = 1; i < count; i++)
|
||||
{
|
||||
v3 p = points[i];
|
||||
box.min.x = minimum(box.min.x, p.x);
|
||||
@@ -302,6 +302,17 @@ inline m4 scale(f32 factor)
|
||||
}
|
||||
|
||||
|
||||
// Other geometric algebra
|
||||
inline void compute_basis(v3 a, v3 *b, v3 *c)
|
||||
{
|
||||
// from https://box2d.org/posts/2014/02/computing-a-basis/
|
||||
if(abs(a.x) >= 0.57735f)
|
||||
*b = {a.y, -a.x, 0.0f};
|
||||
else
|
||||
*b = {0.0f, a.z, -a.y};
|
||||
*b = normalize(*b);
|
||||
*c = cross(a, *b);
|
||||
}
|
||||
|
||||
|
||||
// Primitives
|
||||
|
||||
109
code/lib/math.h
109
code/lib/math.h
@@ -414,6 +414,11 @@ inline v3 V3(f32 e[3])
|
||||
return v3{e[0], e[1], e[2]};
|
||||
}
|
||||
|
||||
inline v3 V3(v4 v)
|
||||
{
|
||||
return v3{v.x / v.w, v.y / v.w, v.z / v.w};
|
||||
}
|
||||
|
||||
inline v3s V3S(v2s a, s32 z)
|
||||
{
|
||||
return {a.x, a.y, z};
|
||||
@@ -475,6 +480,11 @@ inline m3 M3(m4 m)
|
||||
return m3{ m.row[0].xyz, m.row[1].xyz, m.row[2].xyz };
|
||||
}
|
||||
|
||||
inline m4 M4(m3 m)
|
||||
{
|
||||
return m4{ V4(m.row[0], 0), V4(m.row[1], 0), V4(m.row[2], 0), v4{0,0,0,1} };
|
||||
}
|
||||
|
||||
|
||||
// Operators
|
||||
inline v2 operator+(v2 a)
|
||||
@@ -1079,38 +1089,6 @@ inline v4 & operator/=(v4 &a, f32 b)
|
||||
|
||||
|
||||
// Vector functions
|
||||
inline f32 length(v2 a)
|
||||
{
|
||||
return sqrt(square(a.x) + square(a.y));
|
||||
}
|
||||
|
||||
inline f32 length(v3 a)
|
||||
{
|
||||
return sqrt(square(a.x) + square(a.y) + square(a.z));
|
||||
}
|
||||
|
||||
inline f32 length(v4 a)
|
||||
{
|
||||
return sqrt(square(a.x) + square(a.y) + square(a.z) + square(a.w));
|
||||
}
|
||||
|
||||
|
||||
inline f32 distance(v2 a, v2 b)
|
||||
{
|
||||
return length(a - b);
|
||||
}
|
||||
|
||||
inline f32 distance(v3 a, v3 b)
|
||||
{
|
||||
return length(a - b);
|
||||
}
|
||||
|
||||
inline f32 distance(v4 a, v4 b)
|
||||
{
|
||||
return length(a - b);
|
||||
}
|
||||
|
||||
|
||||
inline f32 dot(v2 a, v2 b)
|
||||
{
|
||||
return a.x*b.x + a.y*b.y;
|
||||
@@ -1138,6 +1116,69 @@ inline v3 cross(v3 a, v3 b)
|
||||
}
|
||||
|
||||
|
||||
inline f32 length(v2 a)
|
||||
{
|
||||
return sqrt(dot(a,a));
|
||||
}
|
||||
|
||||
inline f32 length(v3 a)
|
||||
{
|
||||
return sqrt(dot(a,a));
|
||||
}
|
||||
|
||||
inline f32 length(v4 a)
|
||||
{
|
||||
return sqrt(dot(a,a));
|
||||
}
|
||||
|
||||
inline f32 length2(v2 a)
|
||||
{
|
||||
return dot(a,a);
|
||||
}
|
||||
|
||||
inline f32 length2(v3 a)
|
||||
{
|
||||
return dot(a,a);
|
||||
}
|
||||
|
||||
inline f32 length2(v4 a)
|
||||
{
|
||||
return dot(a,a);
|
||||
}
|
||||
|
||||
|
||||
inline f32 distance(v2 a, v2 b)
|
||||
{
|
||||
return length(a - b);
|
||||
}
|
||||
|
||||
inline f32 distance(v3 a, v3 b)
|
||||
{
|
||||
return length(a - b);
|
||||
}
|
||||
|
||||
inline f32 distance(v4 a, v4 b)
|
||||
{
|
||||
return length(a - b);
|
||||
}
|
||||
|
||||
|
||||
inline f32 distance2(v2 a, v2 b)
|
||||
{
|
||||
return length2(a - b);
|
||||
}
|
||||
|
||||
inline f32 distance2(v3 a, v3 b)
|
||||
{
|
||||
return length2(a - b);
|
||||
}
|
||||
|
||||
inline f32 distance2(v4 a, v4 b)
|
||||
{
|
||||
return length2(a - b);
|
||||
}
|
||||
|
||||
|
||||
inline v2 normalize(v2 a)
|
||||
{
|
||||
return a / length(a);
|
||||
@@ -1209,10 +1250,6 @@ inline m3 operator*(m3 a, m3 b)
|
||||
result.E[2][1] = dot(a.row[2], c1);
|
||||
result.E[2][2] = dot(a.row[2], c2);
|
||||
|
||||
result.E[3][0] = dot(a.row[3], c0);
|
||||
result.E[3][1] = dot(a.row[3], c1);
|
||||
result.E[3][2] = dot(a.row[3], c2);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user