summaryrefslogtreecommitdiffstats
path: root/libdimension/model
diff options
context:
space:
mode:
Diffstat (limited to 'libdimension/model')
-rw-r--r--libdimension/model/objects/cone.c38
-rw-r--r--libdimension/model/objects/cube.c19
-rw-r--r--libdimension/model/objects/sphere.c19
-rw-r--r--libdimension/model/objects/torus.c26
-rw-r--r--libdimension/model/objects/triangle.c9
-rw-r--r--libdimension/model/objects/triangle_fan.c29
-rw-r--r--libdimension/model/pigments/canvas_pigment.c5
-rw-r--r--libdimension/model/texture.c3
8 files changed, 71 insertions, 77 deletions
diff --git a/libdimension/model/objects/cone.c b/libdimension/model/objects/cone.c
index 26e59ca..60317bb 100644
--- a/libdimension/model/objects/cone.c
+++ b/libdimension/model/objects/cone.c
@@ -43,11 +43,11 @@ dmnsn_cone_intersection_fn(const dmnsn_object *object, dmnsn_ray l,
// Solve (x0 + nx*t)^2 + (z0 + nz*t)^2 == (((r2 - r1)*(y0 + ny*t) + r1 + r2)/2)^2
double poly[3], x[2];
- poly[2] = l.n.x*l.n.x + l.n.z*l.n.z - l.n.y*l.n.y*(r2 - r1)*(r2 - r1)/4.0;
- poly[1] = 2.0*(l.n.x*l.x0.x + l.n.z*l.x0.z)
- - l.n.y*(r2 - r1)*(l.x0.y*(r2 - r1) + r2 + r1)/2.0;
- poly[0] = l.x0.x*l.x0.x + l.x0.z*l.x0.z
- - (l.x0.y*(r2 - r1) + r2 + r1)*(l.x0.y*(r2 - r1) + r2 + r1)/4.0;
+ poly[2] = l.n.X*l.n.X + l.n.Z*l.n.Z - l.n.Y*l.n.Y*(r2 - r1)*(r2 - r1)/4.0;
+ poly[1] = 2.0*(l.n.X*l.x0.X + l.n.Z*l.x0.Z)
+ - l.n.Y*(r2 - r1)*(l.x0.Y*(r2 - r1) + r2 + r1)/2.0;
+ poly[0] = l.x0.X*l.x0.X + l.x0.Z*l.x0.Z
+ - (l.x0.Y*(r2 - r1) + r2 + r1)*(l.x0.Y*(r2 - r1) + r2 + r1)/4.0;
size_t n = dmnsn_polynomial_solve(poly, 2, x);
@@ -58,7 +58,7 @@ dmnsn_cone_intersection_fn(const dmnsn_object *object, dmnsn_ray l,
t = dmnsn_min(t, x[1]);
p = dmnsn_ray_point(l, t);
- if (p.y <= -1.0 || p.y >= 1.0) {
+ if (p.Y <= -1.0 || p.Y >= 1.0) {
t = dmnsn_max(x[0], x[1]);
p = dmnsn_ray_point(l, t);
}
@@ -66,9 +66,9 @@ dmnsn_cone_intersection_fn(const dmnsn_object *object, dmnsn_ray l,
p = dmnsn_ray_point(l, t);
}
- if (t >= 0.0 && p.y >= -1.0 && p.y <= 1.0) {
- double r = ((r2 - r1)*p.y + r1 + r2)/2.0;
- dmnsn_vector norm = dmnsn_new_vector(p.x, -r*(r2 - r1)/2.0, p.z);
+ if (t >= 0.0 && p.Y >= -1.0 && p.Y <= 1.0) {
+ double r = ((r2 - r1)*p.Y + r1 + r2)/2.0;
+ dmnsn_vector norm = dmnsn_new_vector(p.X, -r*(r2 - r1)/2.0, p.Z);
intersection->t = t;
intersection->normal = norm;
return true;
@@ -84,9 +84,9 @@ dmnsn_cone_inside_fn(const dmnsn_object *object, dmnsn_vector point)
{
const dmnsn_cone *cone = (const dmnsn_cone *)object;
double r1 = cone->r1, r2 = cone->r2;
- double r = (point.y*(r2 - r1) + r1 + r2)/2.0;
- return point.x*point.x + point.z*point.z < r*r
- && point.y > -1.0 && point.y < 1.0;
+ double r = (point.Y*(r2 - r1) + r1 + r2)/2.0;
+ return point.X*point.X + point.Z*point.Z < r*r
+ && point.Y > -1.0 && point.Y < 1.0;
}
/// Cone bounding callback.
@@ -118,12 +118,12 @@ static bool
dmnsn_cone_cap_intersection_fn(const dmnsn_object *object, dmnsn_ray l,
dmnsn_intersection *intersection)
{
- if (l.n.y != 0.0) {
+ if (l.n.Y != 0.0) {
const dmnsn_cone_cap *cap = (const dmnsn_cone_cap *)object;
double r = cap->r;
- double t = -l.x0.y/l.n.y;
+ double t = -l.x0.Y/l.n.Y;
dmnsn_vector p = dmnsn_ray_point(l, t);
- if (t >= 0.0 && p.x*p.x + p.z*p.z <= r*r) {
+ if (t >= 0.0 && p.X*p.X + p.Z*p.Z <= r*r) {
intersection->t = t;
intersection->normal = dmnsn_new_vector(0.0, -1.0, 0.0);
return true;
@@ -188,12 +188,8 @@ dmnsn_new_cone(dmnsn_pool *pool, double r1, double r2, bool open)
// Implement closed cones as a union with the caps
dmnsn_object *cap1 = dmnsn_new_cone_cap(pool, r1);
dmnsn_object *cap2 = dmnsn_new_cone_cap(pool, r2);
- cap1->intrinsic_trans = dmnsn_translation_matrix(
- dmnsn_new_vector(0.0, -1.0, 0.0)
- );
- cap2->intrinsic_trans = dmnsn_translation_matrix(
- dmnsn_new_vector(0.0, +1.0, 0.0)
- );
+ cap1->intrinsic_trans = dmnsn_translation_matrix(dmnsn_new_vector(0.0, -1.0, 0.0));
+ cap2->intrinsic_trans = dmnsn_translation_matrix(dmnsn_new_vector(0.0, +1.0, 0.0));
// Flip the normal around for the top cap
cap2->intrinsic_trans.n[1][1] = -1.0;
diff --git a/libdimension/model/objects/cube.c b/libdimension/model/objects/cube.c
index 7d6fe0f..0434d89 100644
--- a/libdimension/model/objects/cube.c
+++ b/libdimension/model/objects/cube.c
@@ -23,6 +23,7 @@
* Cubes.
*/
+#include "internal.h"
#include "dimension/model.h"
#include <math.h>
@@ -36,8 +37,8 @@ dmnsn_cube_intersection_fn(const dmnsn_object *cube, dmnsn_ray ray,
dmnsn_vector nmin, nmax;
double tmin, tmax;
- double tx1 = (-1.0 - ray.x0.x)/ray.n.x;
- double tx2 = (+1.0 - ray.x0.x)/ray.n.x;
+ double tx1 = (-1.0 - ray.x0.X)/ray.n.X;
+ double tx2 = (+1.0 - ray.x0.X)/ray.n.X;
if (tx1 < tx2) {
tmin = tx1;
@@ -54,8 +55,8 @@ dmnsn_cube_intersection_fn(const dmnsn_object *cube, dmnsn_ray ray,
if (tmin > tmax)
return false;
- double ty1 = (-1.0 - ray.x0.y)/ray.n.y;
- double ty2 = (+1.0 - ray.x0.y)/ray.n.y;
+ double ty1 = (-1.0 - ray.x0.Y)/ray.n.Y;
+ double ty2 = (+1.0 - ray.x0.Y)/ray.n.Y;
if (ty1 < ty2) {
if (ty1 > tmin) {
@@ -80,8 +81,8 @@ dmnsn_cube_intersection_fn(const dmnsn_object *cube, dmnsn_ray ray,
if (tmin > tmax)
return false;
- double tz1 = (-1.0 - ray.x0.z)/ray.n.z;
- double tz2 = (+1.0 - ray.x0.z)/ray.n.z;
+ double tz1 = (-1.0 - ray.x0.Z)/ray.n.Z;
+ double tz2 = (+1.0 - ray.x0.Z)/ray.n.Z;
if (tz1 < tz2) {
if (tz1 > tmin) {
@@ -124,9 +125,9 @@ dmnsn_cube_intersection_fn(const dmnsn_object *cube, dmnsn_ray ray,
static bool
dmnsn_cube_inside_fn(const dmnsn_object *cube, dmnsn_vector point)
{
- return point.x > -1.0 && point.x < 1.0
- && point.y > -1.0 && point.y < 1.0
- && point.z > -1.0 && point.z < 1.0;
+ return point.X > -1.0 && point.X < 1.0
+ && point.Y > -1.0 && point.Y < 1.0
+ && point.Z > -1.0 && point.Z < 1.0;
}
/// Boundary callback for a cube.
diff --git a/libdimension/model/objects/sphere.c b/libdimension/model/objects/sphere.c
index e1ca784..2c8a8ef 100644
--- a/libdimension/model/objects/sphere.c
+++ b/libdimension/model/objects/sphere.c
@@ -29,8 +29,7 @@
/// Sphere intersection callback.
static bool
-dmnsn_sphere_intersection_fn(const dmnsn_object *sphere, dmnsn_ray l,
- dmnsn_intersection *intersection)
+dmnsn_sphere_intersection_fn(const dmnsn_object *sphere, dmnsn_ray l, dmnsn_intersection *intersection)
{
// Solve (x0 + nx*t)^2 + (y0 + ny*t)^2 + (z0 + nz*t)^2 == 1
double poly[3], x[2];
@@ -58,12 +57,12 @@ dmnsn_sphere_intersection_fn(const dmnsn_object *sphere, dmnsn_ray l,
static bool
dmnsn_sphere_inside_fn(const dmnsn_object *sphere, dmnsn_vector point)
{
- return point.x*point.x + point.y*point.y + point.z*point.z < 1.0;
+ return point.X*point.X + point.Y*point.Y + point.Z*point.Z < 1.0;
}
/// Helper for sphere bounding box calculation.
static inline double
-dmnsn_implicit_dot(double row[4])
+dmnsn_implicit_dot(const double row[4])
{
double ret = 0.0;
for (int i = 0; i < 3; ++i) {
@@ -84,18 +83,18 @@ dmnsn_sphere_bounding_fn(const dmnsn_object *object, dmnsn_matrix trans)
double cx = trans.n[0][3];
double dx = sqrt(dmnsn_implicit_dot(trans.n[0]));
- box.min.x = cx - dx;
- box.max.x = cx + dx;
+ box.min.X = cx - dx;
+ box.max.X = cx + dx;
double cy = trans.n[1][3];
double dy = sqrt(dmnsn_implicit_dot(trans.n[1]));
- box.min.y = cy - dy;
- box.max.y = cy + dy;
+ box.min.Y = cy - dy;
+ box.max.Y = cy + dy;
double cz = trans.n[2][3];
double dz = sqrt(dmnsn_implicit_dot(trans.n[2]));
- box.min.z = cz - dz;
- box.max.z = cz + dz;
+ box.min.Z = cz - dz;
+ box.max.Z = cz + dz;
return box;
}
diff --git a/libdimension/model/objects/torus.c b/libdimension/model/objects/torus.c
index b4baebd..e4894b3 100644
--- a/libdimension/model/objects/torus.c
+++ b/libdimension/model/objects/torus.c
@@ -41,18 +41,18 @@ dmnsn_torus_bound_intersection(const dmnsn_torus *torus, dmnsn_ray l)
double rmax2 = rmax*rmax, rmin2 = rmin*rmin;
// Try the caps first
- double tlower = (-r - l.x0.y)/l.n.y;
- double tupper = (+r - l.x0.y)/l.n.y;
+ double tlower = (-r - l.x0.Y)/l.n.Y;
+ double tupper = (+r - l.x0.Y)/l.n.Y;
dmnsn_vector lower = dmnsn_ray_point(l, tlower);
dmnsn_vector upper = dmnsn_ray_point(l, tupper);
- double ldist2 = lower.x*lower.x + lower.z*lower.z;
- double udist2 = upper.x*upper.x + upper.z*upper.z;
+ double ldist2 = lower.X*lower.X + lower.Z*lower.Z;
+ double udist2 = upper.X*upper.X + upper.Z*upper.Z;
if ((ldist2 < rmin2 || ldist2 > rmax2) && (udist2 < rmin2 || udist2 > rmax2)) {
// No valid intersection with the caps, try the cylinder walls
- double dist2 = l.x0.x*l.x0.x + l.x0.z*l.x0.z;
+ double dist2 = l.x0.X*l.x0.X + l.x0.Z*l.x0.Z;
double bigcyl[3], smallcyl[3];
- bigcyl[2] = smallcyl[2] = l.n.x*l.n.x + l.n.z*l.n.z;
- bigcyl[1] = smallcyl[1] = 2.0*(l.n.x*l.x0.x + l.n.z*l.x0.z);
+ bigcyl[2] = smallcyl[2] = l.n.X*l.n.X + l.n.Z*l.n.Z;
+ bigcyl[1] = smallcyl[1] = 2.0*(l.n.X*l.x0.X + l.n.Z*l.x0.Z);
bigcyl[0] = dist2 - rmax2;
smallcyl[0] = dist2 - rmin2;
@@ -63,7 +63,7 @@ dmnsn_torus_bound_intersection(const dmnsn_torus *torus, dmnsn_ray l)
size_t i;
for (i = 0; i < n; ++i) {
dmnsn_vector p = dmnsn_ray_point(l, x[i]);
- if (p.y >= -r && p.y <= r)
+ if (p.Y >= -r && p.Y <= r)
break;
}
@@ -90,8 +90,8 @@ dmnsn_torus_intersection_fn(const dmnsn_object *object, dmnsn_ray l,
}
// This bit of algebra here is correct
- dmnsn_vector x0mod = dmnsn_new_vector(l.x0.x, -l.x0.y, l.x0.z);
- dmnsn_vector nmod = dmnsn_new_vector(l.n.x, -l.n.y, l.n.z);
+ dmnsn_vector x0mod = dmnsn_new_vector(l.x0.X, -l.x0.Y, l.x0.Z);
+ dmnsn_vector nmod = dmnsn_new_vector(l.n.X, -l.n.Y, l.n.Z);
double nn = dmnsn_vector_dot(l.n, l.n);
double nx0 = dmnsn_vector_dot(l.n, l.x0);
double x0x0 = dmnsn_vector_dot(l.x0, l.x0);
@@ -123,7 +123,7 @@ dmnsn_torus_intersection_fn(const dmnsn_object *object, dmnsn_ray l,
dmnsn_vector p = dmnsn_ray_point(l, t);
dmnsn_vector center = dmnsn_vector_mul(
R,
- dmnsn_vector_normalized(dmnsn_new_vector(p.x, 0.0, p.z))
+ dmnsn_vector_normalized(dmnsn_new_vector(p.X, 0.0, p.Z))
);
dmnsn_vector normal = dmnsn_vector_sub(p, center);
@@ -137,8 +137,8 @@ static bool
dmnsn_torus_inside_fn(const dmnsn_object *object, dmnsn_vector point)
{
const dmnsn_torus *torus = (const dmnsn_torus *)object;
- double dmajor = torus->major - sqrt(point.x*point.x + point.z*point.z);
- return dmajor*dmajor + point.y*point.y < torus->minor*torus->minor;
+ double dmajor = torus->major - sqrt(point.X*point.X + point.Z*point.Z);
+ return dmajor*dmajor + point.Y*point.Y < torus->minor*torus->minor;
}
/// Torus bounding callback.
diff --git a/libdimension/model/objects/triangle.c b/libdimension/model/objects/triangle.c
index 5af3301..87372d7 100644
--- a/libdimension/model/objects/triangle.c
+++ b/libdimension/model/objects/triangle.c
@@ -33,16 +33,15 @@ static inline bool
dmnsn_ray_triangle_intersection(dmnsn_ray l, double *t, double *u, double *v)
{
// See the change of basis in dmnsn_triangle_basis()
- *t = -l.x0.z/l.n.z;
- *u = l.x0.x + (*t)*l.n.x;
- *v = l.x0.y + (*t)*l.n.y;
+ *t = -l.x0.Z/l.n.Z;
+ *u = l.x0.X + (*t)*l.n.X;
+ *v = l.x0.Y + (*t)*l.n.Y;
return *t >= 0.0 && *u >= 0.0 && *v >= 0.0 && *u + *v <= 1.0;
}
/// Triangle intersection callback.
DMNSN_HOT static bool
-dmnsn_triangle_intersection_fn(const dmnsn_object *object, dmnsn_ray l,
- dmnsn_intersection *intersection)
+dmnsn_triangle_intersection_fn(const dmnsn_object *object, dmnsn_ray l, dmnsn_intersection *intersection)
{
double t, u, v;
if (dmnsn_ray_triangle_intersection(l, &t, &u, &v)) {
diff --git a/libdimension/model/objects/triangle_fan.c b/libdimension/model/objects/triangle_fan.c
index 93768a9..ddca581 100644
--- a/libdimension/model/objects/triangle_fan.c
+++ b/libdimension/model/objects/triangle_fan.c
@@ -40,9 +40,9 @@ static inline dmnsn_vector
dmnsn_change_basis(const double coeffs[6], dmnsn_vector v)
{
return dmnsn_new_vector(
- coeffs[0]*v.x + coeffs[1]*v.z + v.y,
- coeffs[2]*v.x + coeffs[3]*v.z,
- coeffs[4]*v.x + coeffs[5]*v.z
+ coeffs[0]*v.X + coeffs[1]*v.Z + v.Y,
+ coeffs[2]*v.X + coeffs[3]*v.Z,
+ coeffs[4]*v.X + coeffs[5]*v.Z
);
}
@@ -51,9 +51,9 @@ static inline dmnsn_vector
dmnsn_change_normal_basis(const double coeffs[6], dmnsn_vector n)
{
return dmnsn_new_vector(
- coeffs[0]*n.x + coeffs[2]*n.y + coeffs[4]*n.z,
- n.x,
- coeffs[1]*n.x + coeffs[3]*n.y + coeffs[5]*n.z
+ coeffs[0]*n.X + coeffs[2]*n.Y + coeffs[4]*n.Z,
+ n.X,
+ coeffs[1]*n.X + coeffs[3]*n.Y + coeffs[5]*n.Z
);
}
@@ -80,12 +80,11 @@ dmnsn_compress_coeffs(double coeffs[6], dmnsn_matrix incremental)
static inline dmnsn_matrix
dmnsn_decompress_coeffs(const double coeffs[6])
{
- dmnsn_matrix incremental = dmnsn_new_matrix(
+ return dmnsn_new_matrix(
coeffs[0], 1.0, coeffs[1], 0.0,
coeffs[2], 0.0, coeffs[3], 0.0,
coeffs[4], 0.0, coeffs[5], 0.0
);
- return incremental;
}
/// Make a change-of-basis matrix for a triangle.
@@ -100,9 +99,9 @@ dmnsn_triangle_basis(dmnsn_vector a, dmnsn_vector ab, dmnsn_vector ac)
static inline bool
dmnsn_ray_triangle_intersection(dmnsn_ray l, double *t, double *u, double *v)
{
- *t = -l.x0.z/l.n.z;
- *u = l.x0.x + (*t)*l.n.x;
- *v = l.x0.y + (*t)*l.n.y;
+ *t = -l.x0.Z/l.n.Z;
+ *u = l.x0.X + (*t)*l.n.X;
+ *v = l.x0.Y + (*t)*l.n.Y;
return *t >= 0.0 && *u >= 0.0 && *v >= 0.0 && *u + *v <= 1.0;
}
@@ -149,7 +148,7 @@ dmnsn_triangle_fan_inside_fn(const dmnsn_object *object, dmnsn_vector point)
return false;
}
-/// Computes the bounding box for the first triangle
+/// Computes the bounding box for the first triangle.
static inline dmnsn_aabb
dmnsn_bound_first_triangle(dmnsn_matrix trans)
{
@@ -336,9 +335,9 @@ dmnsn_new_smooth_triangle_fan(dmnsn_pool *pool, dmnsn_vector vertices[], dmnsn_v
nc = dmnsn_vector_normalized(dmnsn_transform_normal(Pabc, normals[i + 3]));
dmnsn_vector nac = dmnsn_vector_sub(nc, na);
- coeffs[6] = nac.x;
- coeffs[7] = nac.y;
- coeffs[8] = nac.z;
+ coeffs[6] = nac.X;
+ coeffs[7] = nac.Y;
+ coeffs[8] = nac.Z;
P = newP;
}
diff --git a/libdimension/model/pigments/canvas_pigment.c b/libdimension/model/pigments/canvas_pigment.c
index bb83b0a..c44ffa6 100644
--- a/libdimension/model/pigments/canvas_pigment.c
+++ b/libdimension/model/pigments/canvas_pigment.c
@@ -23,6 +23,7 @@
* Image maps.
*/
+#include "internal.h"
#include "dimension/model.h"
/// Canvas pigment type.
@@ -38,8 +39,8 @@ dmnsn_canvas_pigment_fn(const dmnsn_pigment *pigment, dmnsn_vector v)
const dmnsn_canvas_pigment *canvas_pigment = (const dmnsn_canvas_pigment *)pigment;
dmnsn_canvas *canvas = canvas_pigment->canvas;
- size_t x = llround((fmod(v.x, 1.0) + 1.0)*(canvas->width - 1));
- size_t y = llround((fmod(v.y, 1.0) + 1.0)*(canvas->height - 1));
+ size_t x = llround((fmod(v.X, 1.0) + 1.0)*(canvas->width - 1));
+ size_t y = llround((fmod(v.Y, 1.0) + 1.0)*(canvas->height - 1));
return dmnsn_canvas_get_pixel(canvas, x%canvas->width, y%canvas->height);
}
diff --git a/libdimension/model/texture.c b/libdimension/model/texture.c
index b7eb7ef..dc25ac9 100644
--- a/libdimension/model/texture.c
+++ b/libdimension/model/texture.c
@@ -45,8 +45,7 @@ dmnsn_texture_initialize(dmnsn_texture *texture)
texture->trans_inv = dmnsn_matrix_inverse(texture->trans);
if (!texture->pigment->initialized) {
- texture->pigment->trans = dmnsn_matrix_mul(texture->trans,
- texture->pigment->trans);
+ texture->pigment->trans = dmnsn_matrix_mul(texture->trans, texture->pigment->trans);
dmnsn_pigment_initialize(texture->pigment);
}
}