Skip to content

Commit 2a55321

Browse files
committed
Point cloud extraction and deferred rasterisation example
Also Factor some common code out of individual examples.
1 parent 745d6c7 commit 2a55321

File tree

10 files changed

+545
-46
lines changed

10 files changed

+545
-46
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "isosurface"
3-
version = "0.0.1"
3+
version = "0.0.2"
44
authors = ["Tristam MacDonald <swiftcoder@gmail.com>"]
55
license = "Apache-2.0"
66
description = "Isosurface extraction algorithms"

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Isosurface
2-
Isosurface extraction algorithms for Rust. Currently only Marching Cubes is implemented, fancier algorithms to be added at a later date.
2+
Isosurface extraction algorithms for Rust. Currently only a few techniques are implemented, fancier algorithms to be added at a later date.
33

44
This crate has no dependencies, although the example relies on the glium, cgmath, num crates.
55

@@ -10,7 +10,10 @@ The implementation has been optimised for performance, with memory use kept as a
1010

1111
Indices are 32-bit becuase for chunks of 32x32 and larger you'll typically end up with mor than 65k vertices. If you are targetting a mobile platform that supports only 16-bit indices, you'll need to use smaller chunk sizes, and truncate on the output side.
1212

13+
# Point Clouds and Deferred Rasterisation
14+
Point cloud extraction is typically not all that useful, given that point clouds don't contain any data about the actual surface. However, Gavan Woolery (gavanw@) posted an interesting image of reconstructing surface data in image space on the GPU, so I've added a simple example of that.
15+
1316
# Why are optimisations enabled in debug builds?
1417
Without optimisations enabled, debug builds are 70x slower (1 minute to extract a 256^3 volume, versus ~800 milliseconds).
1518

16-
This implementation relies on a lot of nested for loops over integer ranges, and the range iterators themselves entirely dominate the CPU profiles in unoptimised builds. While this could likely be worked around by converting the `for 0..8` style of loop to a while loop with manual counter, that seems ugly and distinctly not in the spirit of rust. I'd rather leave optimisations enabled, and wait for the compiler to become better at handling iterators.
19+
The marching cubes implementation relies on a lot of nested for loops over integer ranges, and the range iterators themselves entirely dominate the CPU profiles in unoptimised builds. While this could likely be worked around by converting the `for 0..8` style of loop to a while loop with manual counter, that seems ugly and distinctly not in the spirit of rust. I'd rather leave optimisations enabled, and wait for the compiler to become better at handling iterators.

examples/common/mod.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2017 Tristam MacDonald
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
pub mod sources;
16+
17+
use std::slice;
18+
19+
/// This is used to reinterpret slices of floats as slices of repr(C) structs, without any
20+
/// copying. It is optimal, but it is also punching holes in the type system. I hope that Rust
21+
/// provides safe functionality to handle this in the future. In the meantime, reproduce
22+
/// this workaround at your own risk.
23+
pub fn reinterpret_cast_slice<S, T>(input : &[S], length : usize) -> &[T] {
24+
unsafe {
25+
slice::from_raw_parts(input.as_ptr() as *const T, length)
26+
}
27+
}

examples/common/sources.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2017 Tristam MacDonald
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//! Isosurface definitions for use in multiple examples
16+
17+
use isosurface;
18+
19+
/// The distance-field equation for a torus
20+
fn torus(x : f32, y : f32, z : f32) -> f32 {
21+
const R1 : f32 = 1.0 / 4.0;
22+
const R2 : f32 = 1.0 / 10.0;
23+
let q_x = ((x*x + y*y).sqrt()).abs() - R1;
24+
let len = (q_x*q_x + z*z).sqrt();
25+
len - R2
26+
}
27+
28+
pub struct Torus {}
29+
30+
impl isosurface::source::Source for Torus {
31+
fn sample(&self, x : f32, y : f32, z : f32) -> f32 {
32+
torus(x - 0.5, y - 0.5, z - 0.5)
33+
}
34+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy