From c7cbd0d288083ad7ae132ac6627cf93ec5b3aed5 Mon Sep 17 00:00:00 2001 From: Sakarias Johansson Date: Sun, 16 Apr 2023 14:32:42 +0200 Subject: =?UTF-8?q?=E2=9C=A8=20Add=20support=20for=20camera=20configuratio?= =?UTF-8?q?n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also decided to not use glam and just do the math myself. Just didn't want an entire library for such a small thing. Probably goint to replace Vec3 at some point, just not now. --- racer-tracer/src/vec3.rs | 48 +++++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 19 deletions(-) (limited to 'racer-tracer/src/vec3.rs') diff --git a/racer-tracer/src/vec3.rs b/racer-tracer/src/vec3.rs index 6877eea..40c8453 100644 --- a/racer-tracer/src/vec3.rs +++ b/racer-tracer/src/vec3.rs @@ -1,6 +1,3 @@ -//TODO: Replace this with glam. -use glam::f32::Vec3 as FVec3; - use std::{fmt, ops}; use serde::Deserialize; @@ -121,6 +118,35 @@ impl Vec3 { let s = 1e-8; self.data[0].abs() < s && self.data[1].abs() < s && self.data[2].abs() < s } + + fn hamilton_product(a: [f64; 4], e: [f64; 4]) -> [f64; 4] { + [ + a[0] * e[0] - a[1] * e[1] - a[2] * e[2] - a[3] * e[3], + a[0] * e[1] + a[1] * e[0] + a[2] * e[3] - a[3] * e[2], + a[0] * e[2] - a[1] * e[3] + a[2] * e[0] + a[3] * e[1], + a[0] * e[3] + a[1] * e[2] - a[2] * e[1] + a[3] * e[0], + ] + } + + fn get_rotation(degrees: f64, axis: &Vec3) -> ([f64; 4], [f64; 4]) { + let hd = degrees * 0.5; + let rot = [ + hd.cos(), + hd.sin() * *axis.x(), + hd.sin() * *axis.y(), + hd.sin() * *axis.z(), + ]; + (rot, [rot[0], -rot[1], -rot[2], -rot[3]]) + } + + pub fn rotate(&mut self, degrees: f64, axis: &Vec3) { + let p = [0.0, self.data[0], self.data[1], self.data[2]]; + let (r, r_neg) = Vec3::get_rotation(degrees, axis); + let rpr_neg = Vec3::hamilton_product(Vec3::hamilton_product(r, p), r_neg); + self.data[0] = rpr_neg[1]; + self.data[1] = rpr_neg[2]; + self.data[2] = rpr_neg[3]; + } } pub fn dot(v1: &Vec3, v2: &Vec3) -> f64 { @@ -329,22 +355,6 @@ impl fmt::Display for Vec3 { } } -impl From for Vec3 { - fn from(v: FVec3) -> Self { - Vec3::new(v.x as f64, v.y as f64, v.z as f64) - } -} - -impl From for FVec3 { - fn from(v: Vec3) -> Self { - FVec3 { - x: v.data[0] as f32, - y: v.data[1] as f32, - z: v.data[2] as f32, - } - } -} - impl std::fmt::Debug for Vec3 { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("Vec3").field("data", &self.data).finish() -- cgit v1.2.3