From 83b76d3719e401463f3c80df01d4bd7ee1b0b309 Mon Sep 17 00:00:00 2001 From: tiye Date: Sun, 12 Jan 2025 00:35:08 +0800 Subject: [PATCH] reduce vertex generating cost of cubic array --- README.md | 2 + src/main/axis.mbt | 18 +++---- src/main/container.mbt | 8 ++-- src/main/{cubic-grid.mbt => cubic-array.mbt} | 49 +++++++++++++------- src/main/sphere-tessellation.mbt | 14 +++++- 5 files changed, 59 insertions(+), 32 deletions(-) rename src/main/{cubic-grid.mbt => cubic-array.mbt} (58%) diff --git a/README.md b/README.md index c85ae03..8e38d92 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,8 @@ URL params: - `quat-tree` - `quat-product` - `prime-walk` + - `sphere-tess` + - `cubic-array` - `read` to read from storage, options(defaults to `true`): diff --git a/src/main/axis.mbt b/src/main/axis.mbt index 0aaf532..ff406be 100644 --- a/src/main/axis.mbt +++ b/src/main/axis.mbt @@ -2,21 +2,23 @@ fn comp_axis( width? : Float ) -> @caterfoil.CaterfoilRenderObject!@caterfoil.BuilderError { + let short = 20.0.to_float() + let long = 400.0.to_float() let axis_x : Array[@caterfoil.Vertex] = [ - { position: Quaternion::new(x=-20.0), color: Color::yellow(a=0.3) }, - { position: Quaternion::new(x=200.0), color: Color::yellow() }, + { position: Quaternion::new(x=-short), color: Color::yellow(a=0.3) }, + { position: Quaternion::new(x=long), color: Color::yellow() }, ] let axis_y : Array[@caterfoil.Vertex] = [ - { position: Quaternion::new(y=-20.0), color: Color::orange(a=0.3) }, - { position: Quaternion::new(y=200.0), color: Color::orange() }, + { position: Quaternion::new(y=-short), color: Color::orange(a=0.3) }, + { position: Quaternion::new(y=long), color: Color::orange() }, ] let axis_z : Array[@caterfoil.Vertex] = [ - { position: Quaternion::new(z=-20.0), color: Color::gray(a=0.3) }, - { position: Quaternion::new(z=200.0), color: Color::gray() }, + { position: Quaternion::new(z=-short), color: Color::gray(a=0.3) }, + { position: Quaternion::new(z=long), color: Color::gray() }, ] let axis_w : Array[@caterfoil.Vertex] = [ - { position: Quaternion::new(w=-20.0), color: Color::blue_brigher(a=0.3) }, - { position: Quaternion::new(w=200.0), color: Color::blue_brigher() }, + { position: Quaternion::new(w=-short), color: Color::blue_brigher(a=0.3) }, + { position: Quaternion::new(w=long), color: Color::blue_brigher() }, ] let triangles_list = @caterfoil.create_vertex_data!(axis_x, width=width.or(1)) + @caterfoil.create_vertex_data!(axis_y, width=width.or(1)) + diff --git a/src/main/container.mbt b/src/main/container.mbt index 2ab8e90..9562548 100644 --- a/src/main/container.mbt +++ b/src/main/container.mbt @@ -9,12 +9,12 @@ enum Tab { QuatProduct PrimeWalk SphereTessellation - CubeGrid + CubicArray } ///| fn Tab::default() -> Tab { - CubeGrid + CubicArray } ///| @@ -29,7 +29,7 @@ fn Tab::from_string(s : String) -> Tab { "quat-product" => QuatProduct "prime-walk" => PrimeWalk "sphere-tess" => SphereTessellation - "cubic-grid" => CubeGrid + "cubic-grid" => CubicArray _ => { println("Unknown tab: " + s) Tab::default() @@ -64,7 +64,7 @@ fn comp_container( ) PrimeWalk => comp_prime_walk!() SphereTessellation => comp_sphere_tessellation() - CubeGrid => comp_cubic_gird!(width=2) + CubicArray => comp_cubic_array!(width=2) }, if params.show_axis { comp_axis!(width=4) diff --git a/src/main/cubic-grid.mbt b/src/main/cubic-array.mbt similarity index 58% rename from src/main/cubic-grid.mbt rename to src/main/cubic-array.mbt index 44253e0..1963318 100644 --- a/src/main/cubic-grid.mbt +++ b/src/main/cubic-array.mbt @@ -1,53 +1,66 @@ ///| -fn comp_cubic_gird( +fn comp_cubic_array( width? : Float ) -> @caterfoil.CaterfoilRenderObject!@caterfoil.BuilderError { // create lines of grid in 4 dimension, 4x4x4x4 cells, thus 5x5x5x5 lines let triangles_list : Array[@caterfoil.PolylineVertex] = [] - let unit : Float = 10.0 - let size = 16 + let unit : Float = 60.0 + let size = 20 + let size_f = size.to_float() for ix = 0; ix <= size; ix = ix + 1 { for iy = 0; iy <= size; iy = iy + 1 { for iz = 0; iz <= size; iz = iz + 1 { for iw = 0; iw <= size; iw = iw + 1 { - let x = ix.to_float() * unit - unit * size.to_float() * 0.5 - let y = iy.to_float() * unit - unit * size.to_float() * 0.5 - let z = iz.to_float() * unit - unit * size.to_float() * 0.5 - let w = iw.to_float() * unit - unit * size.to_float() * 0.5 + let x = ix.to_float() * unit - unit * size_f * 0.5 + let y = iy.to_float() * unit - unit * size_f * 0.5 + let z = iz.to_float() * unit - unit * size_f * 0.5 + let w = iw.to_float() * unit - unit * size_f * 0.5 let p = Quaternion::new(x~, y~, z~, w~) // draw 4 lines of unit length in 4 directions - if ix < size { + if ix == 0 { let line : Array[@caterfoil.Vertex] = [ - { position: p, color: Color::yellow() }, - { position: p + Quaternion::new(x=unit), color: Color::yellow() }, + { position: p, color: Color::orange() }, + { + position: p + Quaternion::new(x=unit * size_f), + color: Color::orange(), + }, ] triangles_list.push_iter( @caterfoil.create_vertex_data!(line, width=width.or(1)).iter(), ) } - if iy < size { + if iy == 0 { let line : Array[@caterfoil.Vertex] = [ - { position: p, color: Color::orange() }, - { position: p + Quaternion::new(y=unit), color: Color::orange() }, + { position: p, color: Color::green() }, + { + position: p + Quaternion::new(y=unit * size_f), + color: Color::green(), + }, ] triangles_list.push_iter( @caterfoil.create_vertex_data!(line, width=width.or(1)).iter(), ) } - if iz < size { + if iz == 0 { let line : Array[@caterfoil.Vertex] = [ { position: p, color: Color::gray() }, - { position: p + Quaternion::new(z=unit), color: Color::gray() }, + { + position: p + Quaternion::new(z=unit * size_f), + color: Color::gray(), + }, ] triangles_list.push_iter( @caterfoil.create_vertex_data!(line, width=width.or(1)).iter(), ) } - if iw < size { + if iw == 0 { let line : Array[@caterfoil.Vertex] = [ - { position: p, color: Color::blue() }, - { position: p + Quaternion::new(w=unit), color: Color::blue() }, + { position: p, color: Color::blue_brigher() }, + { + position: p + Quaternion::new(w=unit * size_f), + color: Color::blue_brigher(), + }, ] triangles_list.push_iter( @caterfoil.create_vertex_data!(line, width=width.or(1)).iter(), diff --git a/src/main/sphere-tessellation.mbt b/src/main/sphere-tessellation.mbt index 378c886..e2aa74b 100644 --- a/src/main/sphere-tessellation.mbt +++ b/src/main/sphere-tessellation.mbt @@ -89,10 +89,20 @@ pub fn comp_sphere_tessellation( triangles = next_triangles } let data = [] - for triangle in triangles { + for idx, triangle in triangles.iter2() { + let at = idx.to_float() / triangles.length().to_float() + let color = if at < 0.25 { + Color::orange() + } else if at < 0.5 { + Color::green() + } else if at < 0.75 { + Color::gray() + } else { + Color::blue_brigher() + } let lines = triangle.lines_points() for point in lines { - let v = Vertex::{ position: point.scale(80.0), color: Color::new(r=1.0) } + let v = Vertex::{ position: point.scale(80.0), color } data.push(v) } }