From 45c8ce0af817411ca80913f668a44856d3fe4cfa Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Wed, 13 Jan 2021 10:54:25 -0500 Subject: [PATCH] added color struct --- Cargo.lock | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 1 + src/color.rs | 44 ++++++++++++++++++++++++++++++++ src/main.rs | 9 ++++++- src/point.rs | 37 ++++++++++++++++++++++++--- 5 files changed, 157 insertions(+), 6 deletions(-) create mode 100644 src/color.rs diff --git a/Cargo.lock b/Cargo.lock index 21383e3..71012e1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -24,6 +24,12 @@ version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + [[package]] name = "cmake" version = "0.1.45" @@ -39,6 +45,17 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d9435d864e017c3c6afeac1654189b06cdb491cf2ff73dbf0d73b0f292f42ff8" +[[package]] +name = "getrandom" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4060f4657be78b8e766215b02b18a2e862d83745545de804638e2b545e81aee6" +dependencies = [ + "cfg-if 1.0.0", + "libc", + "wasi", +] + [[package]] name = "libc" version = "0.2.82" @@ -51,7 +68,7 @@ version = "0.4.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fcf3805d4480bb5b86070dcfeb9e2cb2ebc148adb753c5cca5f884d1d65a42b2" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", ] [[package]] @@ -76,15 +93,68 @@ name = "point-generator" version = "0.0.1" dependencies = [ "bytes", + "rand", "zmq", ] +[[package]] +name = "ppv-lite86" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" + +[[package]] +name = "rand" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18519b42a40024d661e1714153e9ad0c3de27cd495760ceb09710920f1098b1e" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", + "rand_hc", +] + +[[package]] +name = "rand_chacha" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c026d7df8b298d90ccbbc5190bd04d85e159eaf5576caeacf8741da93ccbd2e5" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_hc" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" +dependencies = [ + "rand_core", +] + [[package]] name = "toml" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "736b60249cb25337bc196faa43ee12c705e426f3d55c214d73a4e7be06f92cb4" +[[package]] +name = "wasi" +version = "0.10.1+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93c6c3420963c5c64bca373b25e77acb562081b9bb4dd5bb864187742186cea9" + [[package]] name = "zeromq-src" version = "0.1.10+4.3.2" diff --git a/Cargo.toml b/Cargo.toml index da8193b..d9d6cc6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,5 +14,6 @@ path = "src/main.rs" [dependencies] bytes = "1.0.1" +rand = "0.8.2" zmq = { version = "0.9", features = ['vendored'] } #zeromq = "0.2.0" # Doesn't compile. Complains about Rust issue #54727 diff --git a/src/color.rs b/src/color.rs new file mode 100644 index 0000000..0d0c158 --- /dev/null +++ b/src/color.rs @@ -0,0 +1,44 @@ +#[derive(Copy, Clone, Debug)] +pub enum Color { + Black, + White, + Red, + Green, + Blue, + Custom { r: f32, g: f32, b: f32 }, +} + +impl Color { + pub fn get_f32_r(&self) -> f32 { + match &self { + Color::Black => 0.0f32, + Color::White => 1.0f32, + Color::Red => 1.0f32, + Color::Green => 0.0f32, + Color::Blue => 0.0f32, + Color::Custom {r, g, b} => *r, + } + } + + pub fn get_f32_g(&self) -> f32 { + match &self { + Color::Black => 0.0f32, + Color::White => 1.0f32, + Color::Red => 0.0f32, + Color::Green => 1.0f32, + Color::Blue => 0.0f32, + Color::Custom {r, g, b} => *g, + } + } + + pub fn get_f32_b(&self) -> f32 { + match &self { + Color::Black => 0.0f32, + Color::White => 1.0f32, + Color::Red => 1.0f32, + Color::Green => 0.0f32, + Color::Blue => 0.0f32, + Color::Custom {r, g, b} => *b, + } + } +} diff --git a/src/main.rs b/src/main.rs index 88a06bb..51326bd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,13 @@ +//! Generate random points to send over ZeroMQ to a consumer + mod point; +mod color; use point::Point; use std::thread; use zmq::Error; use std::time::Duration; +use crate::color::Color; fn main() { let ctx = zmq::Context::new(); @@ -14,7 +18,10 @@ fn main() { //socket.send("hello world!", 0).unwrap(); loop { - let point = Point::white_at(0.0f32, 0.0f32, 0.0f32); + let point = Point::at_random_range(-1000.0f32, 1000.0f32, Color::White); + + println!("Location: {}", point.location_string()); + let bytes = point.to_bytes(); //let result = socket.send("hello world!", 0); diff --git a/src/point.rs b/src/point.rs index d036659..ef8ebb7 100644 --- a/src/point.rs +++ b/src/point.rs @@ -1,4 +1,8 @@ use bytes::{BytesMut, BufMut}; +use rand::distributions::Uniform; +use rand::Rng; +use crate::color::Color; + pub struct Point { pub x: f32, @@ -13,19 +17,40 @@ pub struct Point { impl Point { - pub fn white_at(x: f32, y: f32, z: f32) -> Self { + pub fn at(x: f32, y: f32, z: f32, color: Color) -> Self { Point { x, y, z, _unused: 0.0f32, - r: 1.0f32, - g: 1.0f32, - b: 1.0f32, + r: color.get_f32_r(), + g: color.get_f32_g(), + b: color.get_f32_b(), a: 1.0f32, } } + pub fn at_random_range(min: f32, max: f32, color: Color) -> Self { + let mut rng = rand::thread_rng(); + let range = Uniform::new_inclusive(min, max); + + let x = rng.sample(&range); + let y = rng.sample(&range); + let z = rng.sample(&range); + + Point { + x, + y, + z, + _unused: 0.0f32, + r: color.get_f32_r(), + g: color.get_f32_g(), + b: color.get_f32_b(), + a: 1.0f32, + } + } + + pub fn to_bytes(&self) -> Vec { let mut buf = BytesMut::with_capacity(32); buf.put_f32(self.x); @@ -38,4 +63,8 @@ impl Point { buf.put_f32(self.a); buf.to_vec() } + + pub fn location_string(&self) -> String { + format!("{}, {}, {}", self.x, self.y, self.z) + } } \ No newline at end of file