Made git CLI mistake last time, adding two missing files now
This commit is contained in:
parent
9c2f58b114
commit
01edb49cb8
|
@ -0,0 +1,124 @@
|
|||
use termtree;
|
||||
use glam::{Vec3};
|
||||
use std::fmt;
|
||||
|
||||
/*
|
||||
Data: [Sphere_0, Sphere_1, Sphere_2]
|
||||
.
|
||||
├── Not
|
||||
├ └── Xor
|
||||
├ ├── Ref(0)
|
||||
├ └── Ref(1)
|
||||
├── Or
|
||||
├ ├── Ref(2),
|
||||
├ └── Ref(1)
|
||||
└── And
|
||||
├── Nor
|
||||
├ ├── Ref(0),
|
||||
├ └── Ref(2)
|
||||
└── Ref(1)
|
||||
*/
|
||||
|
||||
pub trait Field: fmt::Debug + Clone {
|
||||
fn add(&self, other: Self) -> Self;
|
||||
fn sub(&self, other: Self) -> Self;
|
||||
fn mul(&self, other: Self) -> Self;
|
||||
fn div(&self, other: Self) -> Self;
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum FieldOp {
|
||||
Add,
|
||||
Sub,
|
||||
Mul,
|
||||
Div,
|
||||
}
|
||||
|
||||
impl fmt::Display for FieldOp {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let mut name = String::new();
|
||||
match *self {
|
||||
Add => { name = "+".to_string() },
|
||||
Sub => { name = "-".to_string() },
|
||||
Mul => { name = "*".to_string() },
|
||||
Div => { name = "/".to_string() },
|
||||
_ => { name = "?".to_string() }
|
||||
}
|
||||
write!(f, "{}", name)
|
||||
}
|
||||
}
|
||||
|
||||
use FieldOp::*;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct FieldNode<T: Field> {
|
||||
pub field: T,
|
||||
pub op: FieldOp,
|
||||
pub children: Vec<FieldNode<T>>,
|
||||
}
|
||||
|
||||
impl<T: Field> FieldNode<T> {
|
||||
pub fn sample(&self, pos: Vec3) -> T {
|
||||
let mut result = self.field.clone();
|
||||
|
||||
for child in self.children.iter() {
|
||||
let child_result = child.sample(pos);
|
||||
|
||||
result = match child.op {
|
||||
Add => { result.add(child_result) },
|
||||
Sub => { result.sub(child_result) },
|
||||
Mul => { result.mul(child_result) },
|
||||
Div => { result.div(child_result) }
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
pub fn to_termtree(&self) -> termtree::Tree<String> {
|
||||
// Turn the node structure into a tree
|
||||
let mut node_label = String::new();
|
||||
if self.children.is_empty() {
|
||||
node_label = format!("{} {:?}", self.op, self.field);
|
||||
} else {
|
||||
node_label = format!("{} {:?} = {:?}", self.op, self.field, self.sample(Vec3::ZERO));
|
||||
}
|
||||
let mut tree: termtree::Tree<String> = termtree::Tree::new(node_label);
|
||||
for child in self.children.iter() {
|
||||
tree.push(child.to_termtree());
|
||||
}
|
||||
tree
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Field> fmt::Display for FieldNode<T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", self.to_termtree())
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
#[derive(Debug)]
|
||||
enum CSGShape {
|
||||
None,
|
||||
Sphere,
|
||||
Rect,
|
||||
Plane,
|
||||
}
|
||||
|
||||
impl Field for CSGShape {
|
||||
}
|
||||
|
||||
impl fmt::Display for CSGShape {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let mut name = String::new();
|
||||
match *self {
|
||||
None => { name = "".to_string() },
|
||||
_ => { name = format!("{:?}", self) }
|
||||
}
|
||||
write!(f, "{}", name)
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
//use CSGShape::*;
|
|
@ -0,0 +1,49 @@
|
|||
use termtree;
|
||||
use glam::{Vec3};
|
||||
use std::fmt;
|
||||
|
||||
use replicant::fields::{Field, FieldOp, FieldNode};
|
||||
use FieldOp::*;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct Scalar(f32);
|
||||
|
||||
impl Field for Scalar {
|
||||
fn add(&self, other: Self) -> Self {
|
||||
Scalar(self.0 + other.0)
|
||||
}
|
||||
fn sub(&self, other: Self) -> Self {
|
||||
Scalar(self.0 - other.0)
|
||||
}
|
||||
fn mul(&self, other: Self) -> Self {
|
||||
Scalar(self.0 * other.0)
|
||||
}
|
||||
fn div(&self, other: Self) -> Self {
|
||||
Scalar(self.0 / other.0)
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut float_nodes: FieldNode<Scalar> = FieldNode { field: Scalar(10.0), op: Add, children: vec![
|
||||
FieldNode { field: Scalar(8.0), op: Mul, children: Vec::new() },
|
||||
FieldNode { field: Scalar(2.0), op: Sub, children: vec![
|
||||
FieldNode { field: Scalar(0.0), op: Add, children: vec![
|
||||
FieldNode { field: Scalar(0.7), op: Sub, children: Vec::new() },
|
||||
FieldNode { field: Scalar(0.2), op: Add, children: Vec::new() },
|
||||
] },
|
||||
FieldNode { field: Scalar(3.0), op: Div, children: Vec::new() },
|
||||
] },
|
||||
FieldNode { field: Scalar(7.0), op: Mul, children: vec![
|
||||
FieldNode { field: Scalar(0.75), op: Add, children: Vec::new() },
|
||||
FieldNode { field: Scalar(1.0), op: Sub, children: vec![
|
||||
FieldNode { field: Scalar(0.23), op: Sub, children: Vec::new() },
|
||||
] },
|
||||
FieldNode { field: Scalar(2.0), op: Div, children: Vec::new() },
|
||||
] },
|
||||
FieldNode { field: Scalar(0.1), op: Mul, children: Vec::new() },
|
||||
] };
|
||||
|
||||
println!("Node tree:\n{}", float_nodes);
|
||||
println!("{:?}", float_nodes.sample(Vec3::ZERO));
|
||||
}
|
||||
|
Loading…
Reference in New Issue