Skip to content

Commit

Permalink
Plane and Sphere normals, tests update
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed May 29, 2017
1 parent b845b6d commit b16b4d1
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ path = "src/lib.rs"

[dependencies]
cgmath = "0.14"
mint = "0.1"
mint = "0.2"
2 changes: 1 addition & 1 deletion src/cube.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl SharedVertex<Vertex> for Cube {

impl IndexedPolygon<Quad<usize>> for Cube {
fn indexed_polygon(&self, idx: usize) -> Quad<usize> {
self.face_indexed(idx).1
Quad::new(idx*4+0, idx*4+1, idx*4+2, idx*4+3)
}

fn indexed_polygon_count(&self) -> usize { 6 }
Expand Down
12 changes: 7 additions & 5 deletions src/cylinder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::cmp;
use std::f32::consts::PI;
use Vertex;
use super::{Quad, Polygon, Triangle};
Expand Down Expand Up @@ -51,6 +50,9 @@ impl Cylinder {
}
}

/// Create a new subdivided cylinder.
/// `u` is the number of points across the radius.
/// `h` is the number of segments across the height.
pub fn subdivide(u: usize, h: usize) -> Self {
assert!(u > 1 && h > 0);
Cylinder {
Expand All @@ -77,7 +79,7 @@ impl Cylinder {
let z = (hc as f32 / self.sub_h as f32) * 2. - 1.;
Vertex {
pos: [n[0], n[1], z],
normal: n,
normal,
}
}
}
Expand All @@ -92,7 +94,7 @@ impl Iterator for Cylinder {

fn next(&mut self) -> Option<Self::Item> {
if self.u == self.sub_u {
if self.h > self.sub_h {
if self.h >= self.sub_h {
return None;
}
self.u = 0;
Expand All @@ -110,7 +112,7 @@ impl Iterator for Cylinder {

Some(if self.h < 0 {
Polygon::PolyTri(Triangle::new(x, BOT, y))
} else if self.h > self.sub_h {
} else if self.h == self.sub_h {
Polygon::PolyTri(Triangle::new(x, y, TOP))
} else {
let z = self.vert(u1, self.h + 1);
Expand Down Expand Up @@ -149,7 +151,7 @@ impl IndexedPolygon<Polygon<usize>> for Cylinder {
if h < 0 {
let start = 0;
Polygon::PolyTri(Triangle::new(base + u, start, base + u1))
} else if h > self.sub_h {
} else if h == self.sub_h {
let end = self.shared_vertex_count() - 1;
Polygon::PolyTri(Triangle::new(base + u, base + u1, end))
} else {
Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,15 @@ pub mod generators {
pub use sphere::SphereUV;
}

/// Common vertex position type.
pub type Position = mint::Vector3<f32>;
/// Common vertex normal type.
pub type Normal = mint::Vector3<f32>;
/// Common vertex type.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Vertex {
/// Vertex position
pos: Position,
pub pos: Position,
/// Vertex normal
normal: Normal,
pub normal: Normal,
}
5 changes: 4 additions & 1 deletion src/plane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ impl Plane {
let sy = self.subdivide_y as f32;
let x = (2. / sx) * x as f32 - 1.;
let y = (2. / sy) * y as f32 - 1.;
[x, y, 0.0]
Vertex {
pos: [x, y, 0.0],
normal: [0., 0., 1.],
}
}
}

Expand Down
10 changes: 7 additions & 3 deletions src/sphere.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,13 @@ impl SphereUV {
let u = (u as f32 / self.sub_u as f32) * PI * 2.;
let v = (v as f32 / self.sub_v as f32) * PI;

[u.cos() * v.sin(),
u.sin() * v.sin(),
v.cos()]
let p = [u.cos() * v.sin(),
u.sin() * v.sin(),
v.cos()];
Vertex {
pos: p,
normal: p,
}
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ fn gen_cube() {
#[test]
fn gen_cylinder() {
test(generators::Cylinder::new(5));
test(generators::Cylinder::subdivide(3, 4));
}

#[test]
Expand Down
32 changes: 17 additions & 15 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use genmesh::{
Vertex,
};

use genmesh::generators::{Cube, Plane};
use genmesh::generators::{Plane};

#[test]
fn quad_vertex() {
Expand Down Expand Up @@ -147,38 +147,40 @@ fn plane() {
let mut plane = Plane::new();
let a = plane.next().unwrap();

assert_eq!(a.x, [-1f32, -1., 0.]);
assert_eq!(a.y, [ 1f32, -1., 0.]);
assert_eq!(a.z, [ 1f32, 1., 0.]);
assert_eq!(a.w, [-1f32, 1., 0.]);
assert_eq!(a.x.pos, [-1f32, -1., 0.]);
assert_eq!(a.y.pos, [ 1f32, -1., 0.]);
assert_eq!(a.z.pos, [ 1f32, 1., 0.]);
assert_eq!(a.w.pos, [-1f32, 1., 0.]);
}

//TODO: LRU tests changed once the normals got introduced to the `Cube`.
// these tests may need to be revised now.
#[test]
fn lru_indexer() {
let mut vectices: Vec<Vertex> = Vec::new();
let indexes: Vec<usize> = {
let mut indexer = LruIndexer::new(8, |_, v| vectices.push(v));

Cube::new().vertex(|v| indexer.index(v))
.vertices()
.collect()
Plane::subdivide(1, 3).vertex(|v| indexer.index(v))
.vertices()
.collect()
};

assert_eq!(8, vectices.len());
assert_eq!(6*4, indexes.len());
assert_eq!(3*4, indexes.len());

let mut vectices: Vec<Vertex> = Vec::new();
let indexes: Vec<usize> = {
let mut indexer = LruIndexer::new(4, |_, v| vectices.push(v));

Cube::new().triangulate()
.vertex(|v| indexer.index(v))
.vertices()
.collect()
Plane::subdivide(1, 3).triangulate()
.vertex(|v| indexer.index(v))
.vertices()
.collect()
};

assert_eq!(20, vectices.len());
assert_eq!(3*6*2, indexes.len());
assert_eq!(8, vectices.len());
assert_eq!(3*3*2, indexes.len());
}

#[test]
Expand Down
12 changes: 8 additions & 4 deletions tests/winding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ use genmesh::{
struct Edge {
dir: cgmath::Vector3<f32>,
mid: cgmath::Vector3<f32>,
nor: cgmath::Vector3<f32>,
}

impl Edge {
fn new(Line{ x, y }: Line<Vertex>) -> Self {
fn new(line: Line<Vertex>) -> Self {
let Line{ x: Vertex{ pos: x, normal: nx }, y: Vertex{ pos: y, normal: ny } } = line;
Edge {
dir: cgmath::vec3(y[0] - x[0], y[1] - x[1], y[2] - x[2]),
mid: cgmath::vec3(y[0] + x[0], y[1] + x[1], y[2] + x[2]) * 0.5,
nor: cgmath::vec3(nx[0] + ny[0], nx[1] + ny[1], nx[2] + ny[2]),
}
}

Expand All @@ -45,7 +48,7 @@ impl Edge {
fn check_to(&self, e: &Edge) {
let normal = self.dir.cross(e.dir);
let mid = (self.mid + e.mid) * 0.5;
assert!(normal.dot(mid) > 0.0);
assert!(normal.dot(mid) > 0.0 && e.nor.dot(mid) > 0.0);
}
}

Expand Down Expand Up @@ -73,9 +76,9 @@ fn test<P, I>(poly_iter: I) where
#[test]
fn wind_plane() {
test(generators::Plane::new()
.map(|p| p.map_vertex(|v| [v[0], v[1], 1f32])));
.map(|p| p.map_vertex(|mut v| {v.pos[2] = 1.; v})));
test(generators::Plane::subdivide(3, 4)
.map(|p| p.map_vertex(|v| [v[0], v[1], 1f32])));
.map(|p| p.map_vertex(|mut v| {v.pos[2] = 1.; v})));
}

#[test]
Expand All @@ -86,6 +89,7 @@ fn gen_cube() {
#[test]
fn gen_cylinder() {
test(generators::Cylinder::new(5));
test(generators::Cylinder::subdivide(3, 4));
}

#[test]
Expand Down

0 comments on commit b16b4d1

Please sign in to comment.