Skip to content

Commit

Permalink
reduce vertex generating cost of cubic array
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Jan 11, 2025
1 parent 5a52c79 commit 83b76d3
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 32 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`):

Expand Down
18 changes: 10 additions & 8 deletions src/main/axis.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -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)) +
Expand Down
8 changes: 4 additions & 4 deletions src/main/container.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ enum Tab {
QuatProduct
PrimeWalk
SphereTessellation
CubeGrid
CubicArray
}

///|
fn Tab::default() -> Tab {
CubeGrid
CubicArray
}

///|
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
49 changes: 31 additions & 18 deletions src/main/cubic-grid.mbt → src/main/cubic-array.mbt
Original file line number Diff line number Diff line change
@@ -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(),
Expand Down
14 changes: 12 additions & 2 deletions src/main/sphere-tessellation.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down

0 comments on commit 83b76d3

Please sign in to comment.