summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpommicket <pommicket@gmail.com>2022-12-14 12:45:48 -0500
committerpommicket <pommicket@gmail.com>2022-12-14 12:45:48 -0500
commit3265cb676c5c87fd624f59aaf3ca89d947df8cda (patch)
tree0bc94cbdddb9cecfc6205f834428e6efafcdb69a
parent3f839f2ec08ffbb75bee391202e7e0d7432d97e0 (diff)
GenRandom
-rw-r--r--.gitignore2
-rw-r--r--Cargo.lock88
-rw-r--r--Cargo.toml8
-rw-r--r--gen_random/Cargo.lock120
-rw-r--r--gen_random/Cargo.toml9
-rw-r--r--gen_random/src/lib.rs28
-rw-r--r--gen_random_proc_macro/Cargo.lock47
-rw-r--r--gen_random_proc_macro/Cargo.toml12
-rw-r--r--gen_random_proc_macro/src/lib.rs120
-rw-r--r--gen_random_test/Cargo.lock128
-rw-r--r--gen_random_test/Cargo.toml11
-rw-r--r--gen_random_test/src/lib.rs63
-rw-r--r--src/main.rs9
-rw-r--r--src/sdf.rs8
14 files changed, 648 insertions, 5 deletions
diff --git a/.gitignore b/.gitignore
index d2459d5..ba07c0f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,4 @@
-/target
+target
tags
*.swp
*.out
diff --git a/Cargo.lock b/Cargo.lock
index ad36445..3f05fe1 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -21,8 +21,12 @@ checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
name = "autosdf"
version = "0.1.0"
dependencies = [
+ "gen_random",
+ "gen_random_proc_macro",
+ "gen_random_test",
"gl",
"nalgebra",
+ "rand",
]
[[package]]
@@ -38,6 +42,42 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
+name = "gen_random"
+version = "0.1.0"
+dependencies = [
+ "rand",
+]
+
+[[package]]
+name = "gen_random_proc_macro"
+version = "0.1.0"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "gen_random_test"
+version = "0.1.0"
+dependencies = [
+ "gen_random",
+ "gen_random_proc_macro",
+ "rand",
+]
+
+[[package]]
+name = "getrandom"
+version = "0.2.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31"
+dependencies = [
+ "cfg-if",
+ "libc",
+ "wasi",
+]
+
+[[package]]
name = "gl"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -64,6 +104,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2db585e1d738fc771bf08a151420d3ed193d9d895a36df7f6f8a9456b911ddc"
[[package]]
+name = "libc"
+version = "0.2.138"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "db6d7e329c562c5dfab7a46a2afabc8b987ab9a4834c9d1ca04dc54c1546cef8"
+
+[[package]]
name = "log"
version = "0.4.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -154,6 +200,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1de2e551fb905ac83f73f7aedf2f0cb4a0da7e35efa24a202a936269f1f18e1"
[[package]]
+name = "ppv-lite86"
+version = "0.2.17"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
+
+[[package]]
name = "proc-macro2"
version = "1.0.47"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -172,6 +224,36 @@ dependencies = [
]
[[package]]
+name = "rand"
+version = "0.8.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
+dependencies = [
+ "libc",
+ "rand_chacha",
+ "rand_core",
+]
+
+[[package]]
+name = "rand_chacha"
+version = "0.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
+dependencies = [
+ "ppv-lite86",
+ "rand_core",
+]
+
+[[package]]
+name = "rand_core"
+version = "0.6.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
+dependencies = [
+ "getrandom",
+]
+
+[[package]]
name = "rawpointer"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -223,6 +305,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3"
[[package]]
+name = "wasi"
+version = "0.11.0+wasi-snapshot-preview1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
+
+[[package]]
name = "wide"
version = "0.7.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
index afac22e..6ea046f 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -6,5 +6,9 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
-gl = "0.14.0"
-nalgebra = "0.31.4"
+gl = "0.14"
+nalgebra = "0.31"
+rand = "0.8"
+gen_random_proc_macro = { path = "./gen_random_proc_macro" }
+gen_random = { path = "./gen_random" }
+gen_random_test = { path = "./gen_random_test" }
diff --git a/gen_random/Cargo.lock b/gen_random/Cargo.lock
new file mode 100644
index 0000000..b7e5b20
--- /dev/null
+++ b/gen_random/Cargo.lock
@@ -0,0 +1,120 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 3
+
+[[package]]
+name = "cfg-if"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
+
+[[package]]
+name = "gen_random"
+version = "0.1.0"
+dependencies = [
+ "gen_random_proc_macro",
+ "rand",
+]
+
+[[package]]
+name = "gen_random_proc_macro"
+version = "0.1.0"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "getrandom"
+version = "0.2.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31"
+dependencies = [
+ "cfg-if",
+ "libc",
+ "wasi",
+]
+
+[[package]]
+name = "libc"
+version = "0.2.138"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "db6d7e329c562c5dfab7a46a2afabc8b987ab9a4834c9d1ca04dc54c1546cef8"
+
+[[package]]
+name = "ppv-lite86"
+version = "0.2.17"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
+
+[[package]]
+name = "proc-macro2"
+version = "1.0.47"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5ea3d908b0e36316caf9e9e2c4625cdde190a7e6f440d794667ed17a1855e725"
+dependencies = [
+ "unicode-ident",
+]
+
+[[package]]
+name = "quote"
+version = "1.0.21"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179"
+dependencies = [
+ "proc-macro2",
+]
+
+[[package]]
+name = "rand"
+version = "0.8.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
+dependencies = [
+ "libc",
+ "rand_chacha",
+ "rand_core",
+]
+
+[[package]]
+name = "rand_chacha"
+version = "0.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
+dependencies = [
+ "ppv-lite86",
+ "rand_core",
+]
+
+[[package]]
+name = "rand_core"
+version = "0.6.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
+dependencies = [
+ "getrandom",
+]
+
+[[package]]
+name = "syn"
+version = "1.0.105"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "60b9b43d45702de4c839cb9b51d9f529c5dd26a4aff255b42b1ebc03e88ee908"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "unicode-ident",
+]
+
+[[package]]
+name = "unicode-ident"
+version = "1.0.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3"
+
+[[package]]
+name = "wasi"
+version = "0.11.0+wasi-snapshot-preview1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
diff --git a/gen_random/Cargo.toml b/gen_random/Cargo.toml
new file mode 100644
index 0000000..1d14093
--- /dev/null
+++ b/gen_random/Cargo.toml
@@ -0,0 +1,9 @@
+[package]
+name = "gen_random"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+rand = "0.8"
diff --git a/gen_random/src/lib.rs b/gen_random/src/lib.rs
new file mode 100644
index 0000000..f091ec3
--- /dev/null
+++ b/gen_random/src/lib.rs
@@ -0,0 +1,28 @@
+extern crate rand;
+use rand::Rng;
+
+pub trait GenRandom: Sized {
+ fn gen_random(rng: &mut impl Rng) -> Self;
+ fn gen_thread_random() -> Self {
+ let mut thread_rng = rand::thread_rng();
+ Self::gen_random(&mut thread_rng)
+ }
+}
+
+impl GenRandom for f32 {
+ fn gen_random(rng: &mut impl Rng) -> Self {
+ rng.gen_range(0.0..1.0)
+ }
+}
+
+impl GenRandom for f64 {
+ fn gen_random(rng: &mut impl Rng) -> Self {
+ rng.gen_range(0.0..1.0)
+ }
+}
+
+impl<T: GenRandom> GenRandom for Box<T> {
+ fn gen_random(rng: &mut impl Rng) -> Self {
+ Box::new(T::gen_random(rng))
+ }
+}
diff --git a/gen_random_proc_macro/Cargo.lock b/gen_random_proc_macro/Cargo.lock
new file mode 100644
index 0000000..412ff2e
--- /dev/null
+++ b/gen_random_proc_macro/Cargo.lock
@@ -0,0 +1,47 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 3
+
+[[package]]
+name = "gen_random_proc_macro"
+version = "0.1.0"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "proc-macro2"
+version = "1.0.47"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5ea3d908b0e36316caf9e9e2c4625cdde190a7e6f440d794667ed17a1855e725"
+dependencies = [
+ "unicode-ident",
+]
+
+[[package]]
+name = "quote"
+version = "1.0.21"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179"
+dependencies = [
+ "proc-macro2",
+]
+
+[[package]]
+name = "syn"
+version = "1.0.105"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "60b9b43d45702de4c839cb9b51d9f529c5dd26a4aff255b42b1ebc03e88ee908"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "unicode-ident",
+]
+
+[[package]]
+name = "unicode-ident"
+version = "1.0.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3"
diff --git a/gen_random_proc_macro/Cargo.toml b/gen_random_proc_macro/Cargo.toml
new file mode 100644
index 0000000..bcf05ce
--- /dev/null
+++ b/gen_random_proc_macro/Cargo.toml
@@ -0,0 +1,12 @@
+[package]
+name = "gen_random_proc_macro"
+version = "0.1.0"
+edition = "2021"
+
+[lib]
+proc-macro = true
+
+[dependencies]
+proc-macro2 = "1.0"
+quote = "1.0"
+syn = "1.0"
diff --git a/gen_random_proc_macro/src/lib.rs b/gen_random_proc_macro/src/lib.rs
new file mode 100644
index 0000000..e621a76
--- /dev/null
+++ b/gen_random_proc_macro/src/lib.rs
@@ -0,0 +1,120 @@
+extern crate quote;
+extern crate proc_macro2;
+extern crate syn;
+
+use proc_macro::{TokenStream, TokenTree::{self, Punct, Literal}};
+use quote::quote;
+
+#[proc_macro_derive(GenRandom, attributes(prob))]
+pub fn gen_random_derive(input: TokenStream) -> TokenStream {
+ // Construct a representation of Rust code as a syntax tree
+ // that we can manipulate
+ let ast = syn::parse(input).unwrap();
+ // Build the trait implementation
+ impl_gen_random(&ast)
+}
+
+
+fn impl_gen_random(ast: &syn::DeriveInput) -> TokenStream {
+ let name = &ast.ident;
+ match &ast.data {
+ syn::Data::Enum(enumeration) => {
+ let variants = &enumeration.variants;
+ let epsilon: f64 = 1e-9;
+ let one_minus_epsilon = 1.0 - epsilon;
+ let mut function_body = quote! {
+ let mut variant: f64 = rng.gen_range(0.0..#one_minus_epsilon);
+ };
+
+ let mut test_variant = one_minus_epsilon;
+
+ // parse enum fields
+ for variant in variants.iter() {
+ let probability: f64 = {
+ let attr = variant.attrs.iter().find(|a| {
+ let path = &a.path;
+ if let Some(ident) = path.get_ident() {
+ ident == "prob"
+ } else {
+ false
+ }
+ });
+ let Some(attr) = attr else {
+ panic!("Variant {} has no probability", variant.ident)
+ };
+ let tokens: TokenStream = attr.tokens.clone().into();
+ let tokens: Vec<TokenTree> = tokens.into_iter().collect();
+ if tokens.len() != 2 {
+ panic!("Expected prob = <floating-point number>");
+ }
+ match &tokens[0] {
+ Punct(equals) if equals.as_char() == '=' => {}
+ _ => panic!("Expected = after prob attribute"),
+ };
+
+ let Literal(literal) = &tokens[1] else {
+ panic!("Bad number for prob attribute.");
+ };
+ literal.to_string().parse().expect("Bad number for prob attribute")
+ };
+
+ let name = &variant.ident;
+
+
+ let mut variant_arguments = quote! {};
+ for field in variant.fields.iter() {
+ if let Some(name) = &field.ident {
+ variant_arguments.extend(quote! {#name: });
+ }
+ let ty = &field.ty;
+ variant_arguments.extend(quote! { <#ty as GenRandom>::gen_random(rng), });
+ }
+
+ // surround the arguments with either () or {} brackets
+ let constructor_group = match variant.fields {
+ syn::Fields::Named(_) => {
+ Some(proc_macro2::Group::new(
+ proc_macro2::Delimiter::Brace,
+ variant_arguments
+ ))
+ },
+ syn::Fields::Unnamed(_) => {
+ Some(proc_macro2::Group::new(
+ proc_macro2::Delimiter::Parenthesis,
+ variant_arguments
+ ))
+ },
+ syn::Fields::Unit => None,
+ };
+
+ function_body.extend(quote! {
+ variant -= #probability;
+ if variant <= 0.0 { return Self::#name #constructor_group; }
+ });
+ test_variant -= probability;
+
+
+ }
+
+ if test_variant >= 0.0 || test_variant < -2.0 * epsilon {
+ panic!("Probabilities for enum do not add up to 1 (final test_variant = {test_variant}).");
+ }
+
+ function_body.extend(quote! {
+ panic!("RNG returned value outside of range.")
+ });
+
+ let gen = quote! {
+ impl GenRandom for #name {
+ fn gen_random(rng: &mut impl rand::Rng) -> Self {
+ #function_body
+ }
+ }
+ };
+ //println!("{gen}");
+ gen.into()
+ },
+ _ => panic!("derive(GenRandom) can currently only be applied to enums."),
+ }
+}
+
diff --git a/gen_random_test/Cargo.lock b/gen_random_test/Cargo.lock
new file mode 100644
index 0000000..8b5a9ff
--- /dev/null
+++ b/gen_random_test/Cargo.lock
@@ -0,0 +1,128 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 3
+
+[[package]]
+name = "cfg-if"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
+
+[[package]]
+name = "gen_random"
+version = "0.1.0"
+dependencies = [
+ "rand",
+]
+
+[[package]]
+name = "gen_random_proc_macro"
+version = "0.1.0"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "gen_random_test"
+version = "0.1.0"
+dependencies = [
+ "gen_random",
+ "gen_random_proc_macro",
+ "rand",
+]
+
+[[package]]
+name = "getrandom"
+version = "0.2.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31"
+dependencies = [
+ "cfg-if",
+ "libc",
+ "wasi",
+]
+
+[[package]]
+name = "libc"
+version = "0.2.138"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "db6d7e329c562c5dfab7a46a2afabc8b987ab9a4834c9d1ca04dc54c1546cef8"
+
+[[package]]
+name = "ppv-lite86"
+version = "0.2.17"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
+
+[[package]]
+name = "proc-macro2"
+version = "1.0.47"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5ea3d908b0e36316caf9e9e2c4625cdde190a7e6f440d794667ed17a1855e725"
+dependencies = [
+ "unicode-ident",
+]
+
+[[package]]
+name = "quote"
+version = "1.0.21"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179"
+dependencies = [
+ "proc-macro2",
+]
+
+[[package]]
+name = "rand"
+version = "0.8.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
+dependencies = [
+ "libc",
+ "rand_chacha",
+ "rand_core",
+]
+
+[[package]]
+name = "rand_chacha"
+version = "0.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
+dependencies = [
+ "ppv-lite86",
+ "rand_core",
+]
+
+[[package]]
+name = "rand_core"
+version = "0.6.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
+dependencies = [
+ "getrandom",
+]
+
+[[package]]
+name = "syn"
+version = "1.0.105"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "60b9b43d45702de4c839cb9b51d9f529c5dd26a4aff255b42b1ebc03e88ee908"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "unicode-ident",
+]
+
+[[package]]
+name = "unicode-ident"
+version = "1.0.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3"
+
+[[package]]
+name = "wasi"
+version = "0.11.0+wasi-snapshot-preview1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
diff --git a/gen_random_test/Cargo.toml b/gen_random_test/Cargo.toml
new file mode 100644
index 0000000..b712853
--- /dev/null
+++ b/gen_random_test/Cargo.toml
@@ -0,0 +1,11 @@
+[package]
+name = "gen_random_test"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+gen_random_proc_macro = { path = "../gen_random_proc_macro" }
+gen_random = { path = "../gen_random" }
+rand = "0.8"
diff --git a/gen_random_test/src/lib.rs b/gen_random_test/src/lib.rs
new file mode 100644
index 0000000..faca129
--- /dev/null
+++ b/gen_random_test/src/lib.rs
@@ -0,0 +1,63 @@
+extern crate rand;
+extern crate gen_random_proc_macro;
+extern crate gen_random;
+use gen_random::GenRandom;
+use gen_random_proc_macro::GenRandom;
+
+#[derive(GenRandom, Debug)]
+enum Test1 {
+ #[prob = 0.2]
+ A(f32),
+ #[prob = 0.8]
+ B(f32)
+}
+
+#[derive(GenRandom, Debug)]
+#[allow(dead_code)]
+enum Test2 {
+ #[prob = 0.1]
+ Variant1,
+ #[prob = 0.7]
+ Variant2 { x : f32, y: f64, z: Test1 },
+ #[prob = 0.2]
+ Variant3(f32, Box<Test2>)
+}
+
+#[derive(GenRandom, Debug)]
+enum LinkedList {
+ #[prob = 0.1]
+ Empty,
+ #[prob = 0.9]
+ Cons(f32, Box<LinkedList>)
+}
+
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn basic() {
+ let mut rng = rand::thread_rng();
+
+ let tests1: Vec<_> = (0..10).map(|_| {
+ Test1::gen_random(&mut rng)
+ }).collect();
+ println!("{tests1:?}");
+ }
+
+ #[test]
+ fn many_types_of_variants() {
+ let mut rng = rand::thread_rng();
+ let tests2: Vec<_> = (0..10).map(|_| {
+ Test2::gen_random(&mut rng)
+ }).collect();
+ println!("{tests2:?}");
+ }
+
+ #[test]
+ fn linked_list() {
+ let ll = LinkedList::gen_thread_random();
+ println!("{ll:?}");
+ }
+}
diff --git a/src/main.rs b/src/main.rs
index 2594fdd..60e2499 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -7,9 +7,11 @@
- mouse sensitivity
- AA quality
- # iterations, distance cutoff
+- documentation
*/
extern crate nalgebra;
+extern crate gen_random;
pub mod sdf;
mod sdl;
@@ -17,6 +19,8 @@ pub mod win;
use nalgebra::{Matrix3, Matrix4, Rotation3, Vector3};
use std::time::Instant;
+use gen_random::GenRandom;
+
type Vec3 = Vector3<f32>;
type Mat3 = Matrix3<f32>;
@@ -62,6 +66,9 @@ impl View {
fn try_main() -> Result<(), String> {
use sdf::{Constant, R3ToR, R3ToR3, RToR};
+ let _test = Constant::gen_thread_random();
+ println!("{_test:?}");
+
let funciton = R3ToR::compose(
R3ToR3::InfiniteMirrors(Constant::from(2.0)),
R3ToR::sphere_f32(0.2),
@@ -150,7 +157,7 @@ void main() {
}",
);
- println!("{fshader_source}");
+ //println!("{fshader_source}");
let program = window
.create_program(
diff --git a/src/sdf.rs b/src/sdf.rs
index f79dc3a..61b070f 100644
--- a/src/sdf.rs
+++ b/src/sdf.rs
@@ -1,6 +1,10 @@
#![allow(dead_code)] // @TODO @TEMPORARY
+extern crate rand;
+extern crate gen_random_proc_macro;
use std::fmt::{self, Display, Formatter, Write};
+use gen_random_proc_macro::GenRandom;
+use gen_random::GenRandom;
// we're only writing numbers and strings so write! should never fail.
macro_rules! write_str {
@@ -8,10 +12,12 @@ macro_rules! write_str {
}
/// these are constant across 3D space, not across time/user input/etc.
+#[derive(GenRandom, Debug)]
pub enum Constant {
+ #[prob = 0.5]
F32(f32),
+ #[prob = 0.5]
Time(f32, f32),
- // @TODO: sin, sqrt, mod
}
impl From<f32> for Constant {