Added a Rust port of Leonhard Grünschloß's Sobol sampler.

The Halton sampler appears to be better, but it was fun to add
this anyway!
This commit is contained in:
Nathan Vegdahl 2017-05-11 23:56:12 -07:00
parent b698a52f6c
commit 04e8a6ca73
7 changed files with 53360 additions and 3 deletions

5
Cargo.lock generated
View File

@ -151,6 +151,7 @@ dependencies = [
"rustc-serialize 0.3.23 (registry+https://github.com/rust-lang/crates.io-index)",
"scoped_threadpool 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
"simd 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"sobol 0.1.0",
"spectra_xyz 0.1.0",
"time 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -180,6 +181,10 @@ name = "simd"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "sobol"
version = "0.1.0"
[[package]]
name = "strsim"
version = "0.6.0"

View File

@ -1,5 +1,10 @@
[workspace]
members = ["sub_crates/mem_arena", "sub_crates/halton", "sub_crates/spectra_xyz"]
members = [
"sub_crates/mem_arena",
"sub_crates/halton",
"sub_crates/sobol",
"sub_crates/spectra_xyz"
]
[package]
name = "psychopath"
@ -35,5 +40,8 @@ path = "sub_crates/halton"
[dependencies.mem_arena]
path = "sub_crates/mem_arena"
[dependencies.sobol]
path = "sub_crates/sobol"
[dependencies.spectra_xyz]
path = "sub_crates/spectra_xyz"

View File

@ -159,7 +159,7 @@ impl<'a> Renderer<'a> {
halton::sample(1, offset + si as u32)),
halton::sample(2, offset + si as u32),
map_0_1_to_wavelength(halton::sample(3,
offset +
si as
u32)),

View File

@ -29,7 +29,7 @@ use std::path::Path;
/// How many components to generate.
const NUM_DIMENSIONS: usize = 128;
const NUM_DIMENSIONS: usize = 256;
fn main() {
let out_dir = env::var("OUT_DIR").unwrap();

View File

@ -0,0 +1,9 @@
[package]
name = "sobol"
version = "0.1.0"
authors = ["Nathan Vegdahl <cessen@cessen.com>"]
license = "MIT"
[lib]
name = "sobol"
path = "src/lib.rs"

View File

@ -0,0 +1,54 @@
// Copyright (c) 2012 Leonhard Gruenschloss (leonhard@gruenschloss.org)
//
// 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.
// Adapted to Rust by Nathan Vegdahl (2017)
mod matrices;
pub use matrices::NUM_DIMENSIONS;
use matrices::{SIZE, MATRICES};
/// Compute one component of the Sobol'-sequence, where the component
/// corresponds to the dimension parameter, and the index specifies
/// the point inside the sequence. The scramble parameter can be used
/// to permute elementary intervals, and might be chosen randomly to
/// generate a randomized QMC sequence.
#[inline]
pub fn sample_with_scramble(dimension: u32, mut index: u32, scramble: u32) -> f32 {
assert!((dimension as usize) < NUM_DIMENSIONS);
let mut result = scramble;
let mut i = (dimension as usize) * SIZE;
while index != 0 {
if (index & 1) != 0 {
result ^= MATRICES[i];
}
index >>= 1;
i += 1;
}
return result as f32 * (1.0 / (1u64 << 32) as f32);
}
#[inline]
pub fn sample(dimension: u32, index: u32) -> f32 {
sample_with_scramble(dimension, index, 0)
}

File diff suppressed because it is too large Load Diff