Skip to content

Commit 9165098

Browse files
committed
Add to_vec3 and to_pt3 helper methods
1 parent 99a323a commit 9165098

File tree

6 files changed

+32
-14
lines changed

6 files changed

+32
-14
lines changed

core/src/math/point.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,13 @@ where
134134
}
135135
}
136136

137+
impl Point2 {
138+
/// Converts `self` into a `Point3`, with z equal to 0.
139+
pub fn to_pt3(self) -> Point3 {
140+
pt3(self.x(), self.y(), 0.0)
141+
}
142+
}
143+
137144
impl<R, Sc, B> Point<R, Real<3, B>>
138145
where
139146
R: Index<usize, Output = Sc>,

core/src/math/vec.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,17 @@ where
266266
}
267267

268268
// TODO Make more general - requires a "Scalar::one()" method
269-
impl Vec2 {
269+
impl<B> Vec2<B> {
270+
/// Unit vector codirectional with the x-axis.
270271
pub const X: Self = vec2(1.0, 0.0);
272+
273+
/// Unit vector codirectional with the y-axis.
271274
pub const Y: Self = vec2(0.0, 1.0);
275+
276+
/// Converts `self` into a `Vec3`, with z equal to 0.
277+
pub fn to_vec3(self) -> Vec3<B> {
278+
vec3(self.x(), self.y(), 0.0)
279+
}
272280
}
273281

274282
impl<R, Sc, B> Vector<R, Real<3, B>>
@@ -335,8 +343,13 @@ where
335343

336344
// TODO Make more general
337345
impl Vec3 {
346+
/// Unit vector codirectional with the x-axis.
338347
pub const X: Self = vec3(1.0, 0.0, 0.0);
348+
349+
/// Unit vector codirectional with the y-axis.
339350
pub const Y: Self = vec3(0.0, 1.0, 0.0);
351+
352+
/// Unit vector codirectional with the z-axis.
340353
pub const Z: Self = vec3(0.0, 0.0, 1.0);
341354
}
342355

core/src/render/text.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use core::fmt;
33
use std::io;
44

55
use crate::geom::{vertex, Mesh, Tri};
6-
use crate::math::{pt2, pt3, vec2, vec3, Color3, Point2, Vec2};
6+
use crate::math::{pt2, vec2, vec3, Color3, Point2, Vec2};
77
use crate::util::buf::Buf2;
88

99
use super::tex::{Atlas, Layout, SamplerClamp, TexCoord};
@@ -75,8 +75,7 @@ impl Text {
7575
anchor.y() * glyph_h * rows as f32,
7676
);*/
7777
let offset = vec2(0.0, 0.0);
78-
let pos = *cursor - offset;
79-
let pos = pt3(pos.x(), pos.y(), 0.0);
78+
let pos = (*cursor - offset).to_pt3().to();
8079
let l = geom.verts.len();
8180

8281
geom.verts.extend([

demos/src/bin/bezier.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ fn main() {
3737
let approx = b.approximate(|err| err.len_sqr() < 1.0);
3838

3939
for Edge(p0, p1) in approx.edges() {
40-
let p0 = pt3(p0.x(), p0.y(), 0.0);
41-
let p1 = pt3(p1.x(), p1.y(), 0.0);
40+
let p0 = p0.to_pt3().to();
41+
let p1 = p1.to_pt3().to();
4242
line([vertex(p0, ()), vertex(p1, ())], |sl| {
4343
buf.color_buf[sl.y][sl.xs].fill(0xFF_FF_FF);
4444
})

demos/src/bin/sprites.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ use core::{array::from_fn, ops::ControlFlow::Continue};
33
use re::prelude::*;
44

55
use re::math::rand::{Distrib, PointsInUnitBall, Xorshift64};
6-
use re::render::{cam::Transform, render, Model, ModelToView, ViewToProj};
6+
use re::render::{cam::Transform, render, ModelToView, ViewToProj};
77
use re_front::minifb::Window;
88

99
fn main() {
10-
let verts: [Vec2<Model>; 4] = [
10+
let offsets: [Vec2<View>; 4] = [
1111
vec2(-1.0, -1.0),
1212
vec2(-1.0, 1.0),
1313
vec2(1.0, -1.0),
@@ -19,7 +19,7 @@ fn main() {
1919
let verts: Vec<Vertex3<Vec2<_>>> = PointsInUnitBall
2020
.samples(rng)
2121
.take(count)
22-
.flat_map(|pos| verts.map(|v| vertex(pos.to(), v)))
22+
.flat_map(|pos| offsets.map(|off| vertex(pos.to(), off)))
2323
.collect();
2424

2525
let tris: Vec<_> = (0..count)
@@ -35,7 +35,7 @@ fn main() {
3535
let shader = shader::new(
3636
|v: Vertex3<Vec2<_>>,
3737
(mv, proj): (&Mat4x4<ModelToView>, &Mat4x4<ViewToProj>)| {
38-
let vertex_pos = 0.008 * vec3(v.attrib.x(), v.attrib.y(), 0.0);
38+
let vertex_pos = 0.008 * v.attrib.to_vec3();
3939
let view_pos = mv.apply_pt(&v.pos) + vertex_pos;
4040
vertex(proj.apply(&view_pos), v.attrib)
4141
},

geom/src/solids/lathe.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ use core::ops::Range;
55

66
use re::geom::{vertex, Mesh, Normal2, Normal3, Polyline, Vertex, Vertex2};
77
use re::math::{
8-
polar, pt2, pt3, rotate_y, turns, vec2, vec3, Angle, Lerp, Parametric,
9-
Vary, Vec3,
8+
polar, pt2, rotate_y, turns, vec2, Angle, Lerp, Parametric, Vary, Vec3,
109
};
1110

1211
/// A surface of revolution generated by rotating a 2D curve around the y-axis.
@@ -113,8 +112,8 @@ impl<P: Parametric<Vertex2<Normal2, ()>>> Lathe<P> {
113112
.vary_to(1.0, verts_per_sec as u32)
114113
.map(|t| self.points.eval(t))
115114
{
116-
let mut pos = start.apply_pt(&pt3(pos.x(), pos.y(), 0.0));
117-
let mut norm = start.apply(&vec3(n.x(), n.y(), 0.0));
115+
let mut pos = start.apply_pt(&pos.to_pt3());
116+
let mut norm = start.apply(&n.to_vec3());
118117

119118
for _ in 0..=secs {
120119
b.push_vert(pos, norm);

0 commit comments

Comments
 (0)