From 72cb64077649094a2c632c5fdd822712e80e8217 Mon Sep 17 00:00:00 2001 From: raffitz Date: Sun, 4 Apr 2021 10:30:48 +0100 Subject: [PATCH] Add an alternative implementation for a tetrahedron --- README.md | 2 ++ examples/htetra3.solid | 60 +++++++++++++++++++++++++++++++++++++ examples/tetrahedron3.solid | 52 ++++++++++++++++++++++++++++++++ src/astree.rs | 6 ++++ src/main.rs | 3 ++ 5 files changed, 123 insertions(+) create mode 100644 examples/htetra3.solid create mode 100644 examples/tetrahedron3.solid diff --git a/README.md b/README.md index 34b8883..3bb2841 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,8 @@ The output consists of a series of images, each depicting a layer of the solid, | `htetra1.solid` | Hollow Tetrahedron | A hollow tetrahedron stood on its edge | | `tetrahedron2.solid` | Tetrahedron | A tetrahedron stood on its base/vertex | | `htetra2.solid` | Hollow Tetrahedron | A hollow tetrahedron stood on its base/vertex | +| `tetrahedron3.solid` | Tetrahedron | A tetrahedron stood on its base/vertex | +| `htetra3.solid` | Hollow Tetrahedron | A hollow tetrahedron stood on its base/vertex | | `octahedron1.solid` | Octahedron | An octaheron stood on its vertex | | `hoctahedron1.solid` | Hollow Octahedron | A hollow octahedron stood on its vertex | | `sphere.solid` | Sphere | A sphere of radius s | diff --git a/examples/htetra3.solid b/examples/htetra3.solid new file mode 100644 index 0000000..5355bd1 --- /dev/null +++ b/examples/htetra3.solid @@ -0,0 +1,60 @@ +# Tetrahedron + +# Tetrahedron bounding box size: + +# Figure 1: Side View +# +# *_ - +# / \ ^ +# / | \_ +# / | \ depth +# / | \_ v +# *----0-------* - +# +# Figure 2: Top View +# +# *_B +# | \_ +# |\ \_ +# | \ \_ +# | \ \_ +# | \A \__ +# | *--__*D +# | / _/ +# | / _/ +# | / _/ +# |/ _/ +# |_/ +# *C + +# From these diagrams, you can tell that the width is the height of an +# equilateral triangle. If we assume the edge of the tetrahedron is s, then: + +define @ns s - 4 +define @half_height s / 2 + +# For the depth, a few calculations are necessary: +# +# a + b = s * sin(⅓π) +# a² + (s/2)² = b² +define @a (s/2)*tan(⅙π) +define @na (@ns/2)*tan(⅙π) +define @b s * (sin(⅓π) - tan(⅙π)/2) +# a² + d² = (s*sin(⅓π))² +define @depth s * sqrt((sin(⅓π))^2 - ((1/2)*tan(⅙π))^2) +define @ndepth @ns * sqrt((sin(⅓π))^2 - ((1/2)*tan(⅙π))^2) + +# Considering that the origin is in the center of the base, the min x is -a, +# the min y is -s/2, and the min z is 0 +-@a < x < @b +-@half_height < y < @half_height +0 < z < @depth + +{{ρ ≤ @a * sec(φ - ⅓π) * (1 - z/@depth) ∧ φ ≥ 0 ∧ φ ≤ 2 * ⅓π} ∨ \ +{ρ ≤ @a * sec(φ - π) * (1 - z/@depth) ∧ {φ ≥ 2 * ⅓π ∨ φ ≤ -2 * ⅓π}} ∨ \ +{ρ ≤ @a * sec(φ + ⅓π) * (1 - z/@depth) ∧ φ ≤ 0 ∧ φ ≥ -2 * ⅓π} ∨ ρ = 0} \ +⊻ \ +{z ≥ 1 ∧ z ≤ @ndepth ∧ \ +{{ρ ≤ @na * sec(φ - ⅓π) * (1 - z/@ndepth) ∧ φ ≥ 0 ∧ φ ≤ 2 * ⅓π} ∨ \ +{ρ ≤ @na * sec(φ - π) * (1 - z/@ndepth) ∧ {φ ≥ 2 * ⅓π ∨ φ ≤ -2 * ⅓π}} ∨ \ +{ρ ≤ @na * sec(φ + ⅓π) * (1 - z/@ndepth) ∧ φ ≤ 0 ∧ φ ≥ -2 * ⅓π} ∨ ρ = 0}} diff --git a/examples/tetrahedron3.solid b/examples/tetrahedron3.solid new file mode 100644 index 0000000..deaf3ef --- /dev/null +++ b/examples/tetrahedron3.solid @@ -0,0 +1,52 @@ +# Tetrahedron + +# Tetrahedron bounding box size: + +# Figure 1: Side View +# +# *_ - +# / \ ^ +# / | \_ +# / | \ depth +# / | \_ v +# *----0-------* - +# +# Figure 2: Top View +# +# *_B +# | \_ +# |\ \_ +# | \ \_ +# | \ \_ +# | \A \__ +# | *--__*D +# | / _/ +# | / _/ +# | / _/ +# |/ _/ +# |_/ +# *C + +# From these diagrams, you can tell that the width is the height of an +# equilateral triangle. If we assume the edge of the tetrahedron is s, then: + +define @half_height s / 2 + +# For the depth, a few calculations are necessary: +# +# a + b = s * sin(⅓π) +# a² + (s/2)² = b² +define @a (s/2)*tan(⅙π) +define @b s * (sin(⅓π) - tan(⅙π)/2) +# a² + d² = (s*sin(⅓π))² +define @depth s * sqrt((sin(⅓π))^2 - ((1/2)*tan(⅙π))^2) + +# Considering that the origin is in the center of the base, the min x is -a, +# the min y is -s/2, and the min z is 0 +-@a < x < @b +-@half_height < y < @half_height +0 < z < @depth + +{ρ ≤ @a * sec(φ - ⅓π) * (1 - z/@depth) ∧ φ ≥ 0 ∧ φ ≤ 2 * ⅓π} ∨ \ +{ρ ≤ @a * sec(φ - π) * (1 - z/@depth) ∧ {φ ≥ 2 * ⅓π ∨ φ ≤ -2 * ⅓π}} ∨ \ +{ρ ≤ @a * sec(φ + ⅓π) * (1 - z/@depth) ∧ φ ≤ 0 ∧ φ ≥ -2 * ⅓π} ∨ ρ = 0 diff --git a/src/astree.rs b/src/astree.rs index 7c481c4..2b849b9 100644 --- a/src/astree.rs +++ b/src/astree.rs @@ -6,6 +6,9 @@ pub enum FunctionType { Sin, Cos, Tan, + Sec, + Csc, + Cot, Asin, Acos, Atan, @@ -41,6 +44,9 @@ impl FunctionData { FunctionType::Sin => Ok(value.sin()), FunctionType::Cos => Ok(value.cos()), FunctionType::Tan => Ok(value.tan()), + FunctionType::Sec => Ok(value.cos().recip()), + FunctionType::Csc => Ok(value.sin().recip()), + FunctionType::Cot => Ok(value.tan().recip()), FunctionType::Asin => Ok(value.asin()), FunctionType::Acos => Ok(value.acos()), FunctionType::Atan => Ok(value.atan()), diff --git a/src/main.rs b/src/main.rs index c06065e..48a0f4e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -144,6 +144,9 @@ pomelo! { %type #[token("sin", |_| FunctionType::Sin)] #[token("cos", |_| FunctionType::Cos)] #[token("tan", |_| FunctionType::Tan)] + #[token("sec", |_| FunctionType::Sec)] + #[token("csc", |_| FunctionType::Csc)] + #[token("cot", |_| FunctionType::Cot)] #[token("asin", |_| FunctionType::Asin)] #[token("acos", |_| FunctionType::Acos)] #[token("atan", |_| FunctionType::Atan)]