1use na::{Point3, Vector2, Vector3, Vector4};
2use ndarray as nd;
3extern crate nalgebra as na;
4pub type Color4f = [f32; 4];
5pub fn color_to_v4(color: &Color4f) -> Vector4f {
6 Vector4f::new(color[0], color[1], color[2], color[3])
7}
8pub fn u8arr_to_v4(color: [u8; 3]) -> Color4f {
9 [f32::from(color[0]) / 255., f32::from(color[1]) / 255., f32::from(color[2]) / 255., 1.]
10}
11pub type Vector2f = Vector2<f32>;
12pub type Point3f = Point3<f32>;
13pub type Point3d = Point3<f64>;
14pub type Vector3f = Vector3<f32>;
15pub type Vector4f = Vector4<f32>;
16pub type Vector4s = Vector4<u16>;
17pub type Vector4u = Vector4<u32>;
18pub fn addv2f_scaled(a: &Vector2f, b: &Vector2f, scale: f32) -> Vector2f {
19 Vector2::new(a.x + b.x * scale, a.y + b.y * scale)
20}
21pub fn subv3f(a: &Vector3f, b: &Vector3f) -> Vector3f {
22 Vector3::new(a.x - b.x, a.y - b.y, a.z - b.z)
23}
24pub fn addv3f(a: &Vector3f, b: &Vector3f) -> Vector3f {
25 Vector3::new(a.x + b.x, a.y + b.y, a.z + b.z)
26}
27pub fn addv3f_scaled(a: &Vector3f, b: &Vector3f, scale: f32) -> Vector3f {
28 Vector3::new(a.x + b.x * scale, a.y + b.y * scale, a.z + b.z * scale)
29}
30pub fn mulv3f(a: &Vector3f, s: f32) -> Vector3f {
31 Vector3::new(a.x * s, a.y * s, a.z * s)
32}
33pub fn mulv3d(a: &Vector3d, s: f64) -> Vector3d {
34 Vector3d::new(a.x * s, a.y * s, a.z * s)
35}
36pub fn len_sqrv3f(a: &Vector3f) -> f32 {
37 a.x * a.x + a.y * a.y + a.z * a.z
38}
39pub fn dotv3f(a: &Vector3f, b: &Vector3f) -> f32 {
40 a.x * b.x + a.y * b.y + a.z * b.z
41}
42pub fn vec_from_array_f(vertices: &nd::Array2<f32>, row_index: usize) -> Vector3f {
43 let row = vertices.row(row_index);
44 Vector3f::new(row[0], row[1], row[2])
45}
46pub fn array_to_vec3(arr: &Option<nd::Array2<f32>>) -> [f32; 3] {
47 let v = arr
48 .as_ref()
49 .unwrap_or(&ndarray::Array2::<f32>::zeros((1, 3)))
50 .index_axis(nd::Axis(0), 0)
51 .to_owned();
52 fixed_vec3(&v.to_vec())
53}
54pub fn vec_from_array3_f(vertices: &nd::Array3<f32>, col_index: usize, row_index: usize) -> Vector3f {
55 Vector3f::new(
56 vertices[[col_index, row_index, 0]],
57 vertices[[col_index, row_index, 1]],
58 vertices[[col_index, row_index, 2]],
59 )
60}
61pub fn vec_from_array0_f(vertices: &nd::Array2<f32>) -> Vector3f {
62 let row = vertices.row(0);
63 Vector3f::new(row[0], row[1], row[2])
64}
65pub fn set_vec_from_array_f(vertices: &nd::Array2<f32>, row_index: usize, v: &mut Vector3f) {
66 let row = vertices.row(row_index);
67 v.x = row[0];
68 v.y = row[1];
69 v.z = row[2];
70}
71pub fn vec_from_vec(v: &[f32]) -> Vector3f {
72 Vector3f::new(v[0], v[1], v[2])
73}
74pub fn vec_to_vec(v: &Vector3f) -> Vec<f32> {
75 vec![v.x, v.y, v.z]
76}
77pub fn to_fixed_vec3(v: &Vector3f) -> [f32; 3] {
78 [v.x, v.y, v.z]
79}
80pub fn fixed_vec3(v: &[f32]) -> [f32; 3] {
81 [v[0], v[1], v[2]]
82}
83pub fn vec_from_fixed(v: &[f32; 3]) -> Vector3f {
84 Vector3f::new(v[0], v[1], v[2])
85}
86pub type Line2D = Vec<Vector2f>;
87pub fn len_v2f(a: &Vector2f) -> f32 {
88 (a.x * a.x + a.y * a.y).sqrt()
89}
90pub type Vector2d = Vector2<f64>;
91pub type Vector3d = Vector3<f64>;
92pub fn v3d_from_v3f(v: &Vector3f) -> Vector3d {
93 Vector3d::new(f64::from(v.x), f64::from(v.y), f64::from(v.z))
94}
95#[allow(clippy::cast_possible_truncation)]
96pub fn v3f_from_v3d(v: &Vector3d) -> Vector3f {
97 Vector3f::new(v.x as f32, v.y as f32, v.z as f32)
98}
99pub fn p3d_from_v3d(v: &Vector3d) -> Point3d {
100 Point3d::new(v.x, v.y, v.z)
101}
102pub fn p3d_from_p3f(v: &Vector3f) -> Point3f {
103 Point3f::new(v.x, v.y, v.z)
104}
105#[allow(clippy::cast_possible_truncation)]
106pub fn p3f_from_p3d(v: &Vector3d) -> Point3f {
107 Point3f::new(v.x as f32, v.y as f32, v.z as f32)
108}
109pub fn p3f_from_v3f(v: &Vector3f) -> Point3f {
110 Point3f::new(v.x, v.y, v.z)
111}
112pub fn subv3d(a: &Vector3d, b: &Vector3d) -> Vector3d {
113 Vector3::new(a.x - b.x, a.y - b.y, a.z - b.z)
114}
115pub fn addv3d(a: &Vector3d, b: &Vector3d) -> Vector3d {
116 Vector3::new(a.x + b.x, a.y + b.y, a.z + b.z)
117}
118pub fn addv3d_scaled(a: &Vector3d, b: &Vector3d, scale: f64) -> Vector3d {
119 Vector3::new(a.x + b.x * scale, a.y + b.y * scale, a.z + b.z * scale)
120}
121pub fn subv3f3d(a: &Vector3f, b: &Vector3f) -> Vector3d {
122 Vector3::new(f64::from(a.x - b.x), f64::from(a.y - b.y), f64::from(a.z - b.z))
123}
124pub fn addv3f3d(a: &Vector3f, b: &Vector3f) -> Vector3d {
125 Vector3::new(f64::from(a.x + b.x), f64::from(a.y + b.y), f64::from(a.z + b.z))
126}
127pub fn len_sqrv3d(a: &Vector3d) -> f64 {
128 a.x * a.x + a.y * a.y + a.z * a.z
129}
130pub fn dotv3d(a: &Vector3d, b: &Vector3d) -> f64 {
131 a.x * b.x + a.y * b.y + a.z * b.z
132}
133pub fn vec_from_array_d(vertices: &nd::Array2<f32>, row_index: usize) -> Vector3d {
134 let row = vertices.row(row_index);
135 Vector3d::new(f64::from(row[0]), f64::from(row[1]), f64::from(row[2]))
136}
137pub fn set_vec_from_array_d(vertices: &nd::Array2<f32>, row_index: usize, v: &mut Vector3d) {
138 let row = vertices.row(row_index);
139 v.x = f64::from(row[0]);
140 v.y = f64::from(row[1]);
141 v.z = f64::from(row[2]);
142}
143pub type Matrix3f = na::SimilarityMatrix3<f32>;
144pub fn align_to_multiple_of_four(n: &mut usize) {
145 *n = (*n + 3) & !3;
146}
147pub fn to_padded_byte_vector<T: bytemuck::NoUninit>(vec: &[T]) -> Vec<u8> {
148 let arr_8: &[u8] = bytemuck::cast_slice(vec);
149 let mut new_vec = arr_8.to_vec();
150 while new_vec.len() % 4 != 0 {
151 new_vec.push(0);
152 }
153 new_vec
154}