Skip to main content

openusd_rs/
gf.rs

1//! Graphics Foundations
2
3use half::f16;
4
5#[repr(C)]
6#[derive(Clone, Copy, Debug, Default)]
7pub struct Vec2<T> {
8	pub x: T,
9	pub y: T,
10}
11
12impl<T> Vec2<T> {
13	pub fn new(x: T, y: T) -> Self {
14		Self { x, y }
15	}
16}
17
18#[repr(C)]
19#[derive(Clone, Copy, Debug, Default)]
20pub struct Vec3<T> {
21	pub x: T,
22	pub y: T,
23	pub z: T,
24}
25
26impl<T> Vec3<T> {
27	pub fn new(x: T, y: T, z: T) -> Self {
28		Self { x, y, z }
29	}
30}
31
32#[repr(C)]
33#[derive(Clone, Copy, Debug, Default)]
34pub struct Vec4<T> {
35	pub x: T,
36	pub y: T,
37	pub z: T,
38	pub w: T,
39}
40
41impl<T> Vec4<T> {
42	pub fn new(x: T, y: T, z: T, w: T) -> Self {
43		Self { x, y, z, w }
44	}
45}
46
47pub type Vec2h = Vec2<f16>;
48pub type Vec2f = Vec2<f32>;
49pub type Vec2d = Vec2<f64>;
50pub type Vec2i = Vec2<i32>;
51
52pub type Vec3h = Vec3<f16>;
53pub type Vec3f = Vec3<f32>;
54pub type Vec3d = Vec3<f64>;
55pub type Vec3i = Vec3<i32>;
56
57pub type Vec4h = Vec4<f16>;
58pub type Vec4f = Vec4<f32>;
59pub type Vec4d = Vec4<f64>;
60pub type Vec4i = Vec4<i32>;
61
62impl From<Vec3h> for Vec3d {
63	fn from(v: Vec3h) -> Self {
64		Self {
65			x: v.x.into(),
66			y: v.y.into(),
67			z: v.z.into(),
68		}
69	}
70}
71
72impl From<Vec3f> for Vec3d {
73	fn from(v: Vec3f) -> Self {
74		Self {
75			x: v.x.into(),
76			y: v.y.into(),
77			z: v.z.into(),
78		}
79	}
80}
81
82#[repr(C)]
83#[derive(Clone, Copy, Debug, Default)]
84pub struct Quat<T> {
85	pub i: T,
86	pub j: T,
87	pub k: T,
88	pub w: T,
89}
90
91pub type Quath = Quat<f16>;
92pub type Quatf = Quat<f32>;
93pub type Quatd = Quat<f64>;
94
95impl From<Quath> for Quatd {
96	fn from(q: Quath) -> Self {
97		Self {
98			i: q.i.into(),
99			j: q.j.into(),
100			k: q.k.into(),
101			w: q.w.into(),
102		}
103	}
104}
105
106impl From<Quatf> for Quatd {
107	fn from(q: Quatf) -> Self {
108		Self {
109			i: q.i.into(),
110			j: q.j.into(),
111			k: q.k.into(),
112			w: q.w.into(),
113		}
114	}
115}
116
117#[repr(C)]
118#[derive(Clone, Copy, Debug, Default)]
119pub struct Matrix2d {
120	pub data: [[f64; 2]; 2],
121}
122
123impl Matrix2d {
124	pub fn from_diagonal(diagonal: Vec2d) -> Self {
125		Self {
126			data: [[diagonal.x, 0.0], [0.0, diagonal.y]],
127		}
128	}
129}
130
131#[repr(C)]
132#[derive(Clone, Copy, Debug, Default)]
133pub struct Matrix3d {
134	pub data: [[f64; 3]; 3],
135}
136
137impl Matrix3d {
138	pub fn from_diagonal(diagonal: Vec3d) -> Self {
139		Self {
140			data: [
141				[diagonal.x, 0.0, 0.0],
142				[0.0, diagonal.y, 0.0],
143				[0.0, 0.0, diagonal.z],
144			],
145		}
146	}
147}
148
149#[repr(C)]
150#[derive(Clone, Copy, Debug, Default)]
151pub struct Matrix4d {
152	pub data: [[f64; 4]; 4],
153}
154
155impl Matrix4d {
156	pub fn from_diagonal(diagonal: Vec4d) -> Self {
157		Self {
158			data: [
159				[diagonal.x, 0.0, 0.0, 0.0],
160				[0.0, diagonal.y, 0.0, 0.0],
161				[0.0, 0.0, diagonal.z, 0.0],
162				[0.0, 0.0, 0.0, diagonal.w],
163			],
164		}
165	}
166}
167
168// Not an official USD type, but useful for external interop.
169#[repr(C)]
170#[derive(Clone, Copy, Debug, Default)]
171pub struct Transform3d {
172	pub translation: Vec3d,
173	pub rotation: Quatd,
174	pub scale: Vec3d,
175}