diff options
author | Charlotte Pabst <charlotte.pabst@stud.tu-darmstadt.de> | 2024-03-24 16:27:11 +0100 |
---|---|---|
committer | Charlotte Pabst <charlotte.pabst@stud.tu-darmstadt.de> | 2024-03-24 17:20:07 +0100 |
commit | f552c45b5f08c4cf7af9dda1f4251d130b38ab15 (patch) | |
tree | 35952d917346a99f9403e6913e5cda720933f0a9 | |
parent | 4b7532ca0d6ff21d5531febb749b43112d0451e8 (diff) | |
download | dcel-f552c45b5f08c4cf7af9dda1f4251d130b38ab15.tar.xz |
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | LICENSE | 7 | ||||
-rwxr-xr-x | conv.sh | 8 | ||||
-rw-r--r-- | examples/animation.rs | 122 | ||||
-rw-r--r-- | examples/cairo.rs | 54 | ||||
-rw-r--r-- | examples/subdivision.rs | 4 | ||||
-rw-r--r-- | src/lib.rs | 96 | ||||
-rw-r--r-- | src/obj_export.rs | 12 |
8 files changed, 74 insertions, 230 deletions
@@ -2,3 +2,4 @@ *.png *.dot *.gif +perf* @@ -0,0 +1,7 @@ +Copyright 2024 Charlotte Pabst + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/conv.sh b/conv.sh deleted file mode 100755 index 84c1e23..0000000 --- a/conv.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/bash -set -e -cargo run -for x in *.dot; do - dot -Kneato -T png $x -o $x.png - ../resize/target/release/resize $x.png 830 827 -done -gifski --fast -r 1 -o dcel.gif *.dot.png diff --git a/examples/animation.rs b/examples/animation.rs deleted file mode 100644 index f6aca39..0000000 --- a/examples/animation.rs +++ /dev/null @@ -1,122 +0,0 @@ -use cairo::{Context, SvgSurface}; -use dcel::Dcel; -use enumset::EnumSet; -use std::borrow::Cow; - -fn main() { - let show = |name, dcel: &Dcel<(&'static str, [i64; 2])>| { - let width = 300.0; - let height = width; - - let surf = - //cairo::ImageSurface::create(cairo::Format::ARgb32, width as _, height as _).unwrap(); - SvgSurface::new(width, height, Some(name)).unwrap(); - let ctx = Context::new(surf).unwrap(); - ctx.set_line_width(1.0); - - dcel::write_img( - &dcel, - &ctx, - EnumSet::all(), - |v| { - [ - v.1[0] as f64 * width / 3.0 + width / 2.0, - v.1[1] as f64 * height / 3.0 + height / 2.0, - ] - }, - |v| Cow::Borrowed(v.0), - 12.0, - ) - .unwrap(); - - /*ctx.target() - .write_to_png(&mut std::fs::File::create(name).unwrap()) - .unwrap(); - */ - /*write!( - &mut std::fs::File::create(name).unwrap(), - "{}", - DisplayFn(|f: &mut fmt::Formatter<'_>| dcel_write_dot( - dcel, - |v| v.1.map(|x| x as _), - |v, f| write!(f, "{}", v.0), - f, - DcelDotOptions { - prev: false, - next: true, - twin: true, - } - )) - ) - .unwrap();*/ - }; - - Dcel::new(|mut dcel| { - let body = dcel.new_body(); - // Mevvlfs(a, [w, n], l, f, s) - - //let op = dcel.mevvlfs(*body, [("W", [-4, 0]), ("N", [0, 4])]); - let op = dcel - .mevvlfs(*body, [("W", [-1, 0]), ("N", [0, 1])]) - .unwrap(); - let op2 = dcel.mev(*op.loop_, *op.vertices[1], ("E", [1, 0])).unwrap(); - let op3 = dcel.mev(*op.loop_, *op2.vertex, ("S", [0, -1])).unwrap(); - - dcel.melf([*op3.vertex, *op.vertices[0]], *op.loop_) - .unwrap(); - dcel.melf([*op.vertices[0], *op2.vertex], *op.loop_) - .unwrap(); - - show("cairo.svg", &dcel); - }); - - /* - dbg!(ctx.clip_extents().unwrap()); - - let text = "meow meow meow mew mew mrrrp"; - let x = 250.0; - let y = 250.0; - - let ext = ctx.text_extents(text).unwrap(); - - ctx.move_to( - x - ext.width() / 2.0, - y - ext.y_bearing() - ext.height() / 2.0, - ); - ctx.show_text(text).unwrap(); - - ctx.set_line_width(1.0); - { - let mat = ctx.matrix(); - - ctx.translate(x, y); - ctx.scale( - (ext.width() + ctx.line_width()) / 2.0f64.sqrt(), - (ext.height() + ctx.line_width()) / 2.0f64.sqrt(), - ); - ctx.translate(-x, -y); - ctx.new_path(); - ctx.arc(x, y, 1.0, 0.0, 2.0 * std::f64::consts::PI); - ctx.set_matrix(mat); - } - - ctx.stroke().unwrap();*/ - - /*ctx.rectangle( - x - (ext.width() + ctx.line_width()) / 2.0, - y - (ext.height() + ctx.line_width()) / 2.0, - ext.width() + ctx.line_width(), - ext.height() + ctx.line_width(), - ); - ctx.stroke().unwrap();*/ - - /*ctx.rectangle( - x - ext.width() / 2.0, - y - ext.height() / 2.0, - ext.width(), - ext.height(), - );*/ - - //cairo_move_to (cr, i + 0.5 - te.x_bearing - te.width / 2, - // 0.5 - te.y_bearing - te.height / 2); -} diff --git a/examples/cairo.rs b/examples/cairo.rs new file mode 100644 index 0000000..3114136 --- /dev/null +++ b/examples/cairo.rs @@ -0,0 +1,54 @@ +use cairo::{Context, SvgSurface}; +use dcel::Dcel; +use enumset::EnumSet; +use std::borrow::Cow; + +fn main() { + let show = |name, dcel: &Dcel<(&'static str, [i64; 2])>| { + let width = 300.0; + let height = width; + + let surf = + //cairo::ImageSurface::create(cairo::Format::ARgb32, width as _, height as _).unwrap(); + SvgSurface::new(width, height, Some(name)).unwrap(); + let ctx = Context::new(surf).unwrap(); + ctx.set_line_width(1.0); + + dcel::write_img( + &dcel, + &ctx, + EnumSet::all(), + |v| { + [ + v.1[0] as f64 * width / 3.0 + width / 2.0, + v.1[1] as f64 * height / 3.0 + height / 2.0, + ] + }, + |v| Cow::Borrowed(v.0), + 12.0, + ) + .unwrap(); + + /*ctx.target() + .write_to_png(&mut std::fs::File::create(name).unwrap()) + .unwrap(); + */ + }; + + Dcel::new(|mut dcel| { + let body = dcel.new_body(); + + let op = dcel + .mevvlfs(*body, [("W", [-1, 0]), ("N", [0, 1])]) + .unwrap(); + let op2 = dcel.mev(*op.loop_, *op.vertices[1], ("E", [1, 0])).unwrap(); + let op3 = dcel.mev(*op.loop_, *op2.vertex, ("S", [0, -1])).unwrap(); + + dcel.melf([*op3.vertex, *op.vertices[0]], *op.loop_) + .unwrap(); + dcel.melf([*op.vertices[0], *op2.vertex], *op.loop_) + .unwrap(); + + show("cairo.svg", &dcel); + }); +} diff --git a/examples/subdivision.rs b/examples/subdivision.rs index ba80e49..883b121 100644 --- a/examples/subdivision.rs +++ b/examples/subdivision.rs @@ -2,7 +2,7 @@ use dcel::{ptr_t, Dcel, ObjExport, ObjImport, Ptr, Shell}; use std::array::from_fn; use std::collections::HashMap; -fn avg(mut iter: impl Iterator<Item = [f32; 3]>) -> Option<[f32; 3]> { +fn avg(iter: impl Iterator<Item = [f32; 3]>) -> Option<[f32; 3]> { let mut count = 0usize; let mut accum = [0.0; 3]; @@ -113,7 +113,7 @@ fn main() { let shell = body.iter_shells(&dcel).next().unwrap().item; for i in 0..5 { - println!("{i}..."); + println!("subdivision level {i}..."); catmull_clark_subdivision(&mut dcel, shell); } @@ -167,6 +167,7 @@ fn short_debug_fn<'tok, 'brand, 'arena, T: Entity<'brand, 'arena>>(x: lens_t!(T) DisplayFn(move |f| short_debug_(T::type_name(), id, f)) } +/* fn short_debug_list<'tok, 'brand, 'arena, T, I>(iter: I, f: &mut Formatter) -> fmt::Result where 'brand: 'tok + 'arena, @@ -175,6 +176,7 @@ where { f.debug_list().entries(iter.map(short_debug_fn)).finish() } +*/ fn or_err<T>(cond: bool, err: T) -> Result<(), T> { if cond { @@ -948,97 +950,3 @@ impl<'brand, 'arena, V> Dcel<'brand, 'arena, V> { Kpmh::new(new_shell, old_face, new_face).apply(self) } } - -use std::io::Write; - -fn _main() { - let show = |name, dcel: &Dcel<(&'static str, [i64; 2])>| { - write!( - &mut std::fs::File::create(name).unwrap(), - "{}", - DisplayFn(|f: &mut fmt::Formatter<'_>| dcel_write_dot( - dcel, - |v| v.1.map(|x| x as _), - |v, f| write!(f, "{}", v.0), - f, - DcelDotOptions { - prev: false, - next: true, - twin: true, - } - )) - ) - .unwrap(); - }; - - GhostToken::new(|token| { - let arena = DcelArena::default(); - let mut dcel = Dcel::from_token(token, &arena); - - let body = dcel.new_body(); - // Mevvlfs(a, [w, n], l, f, s) - - //let op = dcel.mevvlfs(*body, [("W", [-4, 0]), ("N", [0, 4])]); - let op = dcel - .mevvlfs(*body, [("W", [-4, 0]), ("N", [0, 4])]) - .unwrap(); - let op2 = dcel.mev(*op.loop_, *op.vertices[1], ("E", [4, 0])).unwrap(); - let op3 = dcel.mev(*op.loop_, *op2.vertex, ("S", [0, -4])).unwrap(); - - dcel.melf([*op3.vertex, *op.vertices[0]], *op.loop_) - .unwrap(); - dcel.melf([*op.vertices[0], *op2.vertex], *op.loop_) - .unwrap(); - - show("cool_stuff.dot", &dcel); - - /*println!("{:?}", op.edge.lens(&dcel)); - println!("{:?}", op.vertices[0].lens(&dcel)); - println!("{:?}", op.vertices[1].lens(&dcel)); - println!("{:?}", op.loop_.lens(&dcel)); - println!("{:?}", op.face.lens(&dcel)); - println!("{:?}", op.shell.lens(&dcel));*/ - - //dbg!(body.lens(&dcel)); - - // dcel.undo(op); - - /* - - let (a, [w, n], _) = dcel.mevvls([("W", [-4, 0]), ("N", [0, 4])]); - show("1.dot", &dcel); - - let b = dcel.mve(a, ("E", [4, 0])).unwrap().0; - show("2.dot", &dcel); - - dcel.mve(a, ("S", [0, -4])).unwrap(); - show("3.dot", &dcel); - - //dcel.mel(w, n); - show("4.dot", &dcel);*/ - - /* - eprintln!( - "{} {}", - a.borrow(&dcel.token).id.unwrap(), - b.borrow(&dcel.token).id.unwrap() - );*/ - - /* - print!( - "{}", - "{}", - layFn(|f: &mut fmt::Formatter<'_>| { - v in dcel.vertices.elements.iter() { - } - - - h in dcel.half_edges.elements.iter() { - } - - Ok(()) - }) - }) - );*/ - }); -} diff --git a/src/obj_export.rs b/src/obj_export.rs index 7998112..d7d1c41 100644 --- a/src/obj_export.rs +++ b/src/obj_export.rs @@ -41,7 +41,7 @@ pub struct ObjExport<'tok, 'brand, 'arena, V, W, VPos, VTex, VNorm> { writer: &'tok mut W, dcel: &'tok Dcel<'brand, 'arena, V>, vertex_pos: VPos, - pos_ids: HashMap<usize, usize>, + pos_ids: Vec<Option<usize>>, textures: VertAttr<lens!(Face), VTex, (f64, Option<(f64, Option<f64>)>), V>, normals: VertAttr<lens!(Face), VNorm, (f64, f64, f64), V>, } @@ -65,7 +65,7 @@ where writer, dcel, vertex_pos, - pos_ids: HashMap::new(), + pos_ids: Vec::new(), textures: VertAttr::new(vertex_texture), normals: VertAttr::new(vertex_normal), } @@ -76,7 +76,11 @@ where let mut next_id = 1; for shell in self.dcel.iter_bodies().flat_map(Lens::iter_shells) { for vertex in shell.iter_vertices() { - self.pos_ids.insert(vertex.id(), next_id); + let v_id = vertex.id(); + if v_id <= self.pos_ids.len() { + self.pos_ids.resize(v_id + 1, None); + } + self.pos_ids.insert(v_id, Some(next_id)); next_id += 1; let (x, y, z, w) = (self.vertex_pos)(vertex.data()); @@ -132,7 +136,7 @@ where half_edge: lens!(HalfEdge), ) -> std::io::Result<()> { let vert = half_edge.origin(); - write!(self.writer, " {}", self.pos_ids[&vert.id()])?; + write!(self.writer, " {}", self.pos_ids[vert.id()].unwrap())?; let t = self.textures.add(face, vert.id(), vert.data()); let n = self.normals.add(face, vert.id(), vert.data()); |