Browse Source

Add an alternative implementation for a tetrahedron

main
raffitz 3 years ago
parent
commit
72cb640776
Signed by: raffitz
GPG Key ID: BB3596BD0A31252D
  1. 2
      README.md
  2. 60
      examples/htetra3.solid
  3. 52
      examples/tetrahedron3.solid
  4. 6
      src/astree.rs
  5. 3
      src/main.rs

2
README.md

@ -48,6 +48,8 @@ The output consists of a series of images, each depicting a layer of the solid, @@ -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 |

60
examples/htetra3.solid

@ -0,0 +1,60 @@ @@ -0,0 +1,60 @@
# Tetrahedron
# Tetrahedron bounding box size:
# Figure 1: Side View
#
# *_ -
# / \ ^
# / | \_
# / | \ depth
# / | \_ v
# *----0-------* -
#
# Figure 2: Top View
#
# *_B
# | \_
# |\ \_
# | \ \_
# | \ \_
# | \A \__
# |<a> *-<b>-__*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}}

52
examples/tetrahedron3.solid

@ -0,0 +1,52 @@ @@ -0,0 +1,52 @@
# Tetrahedron
# Tetrahedron bounding box size:
# Figure 1: Side View
#
# *_ -
# / \ ^
# / | \_
# / | \ depth
# / | \_ v
# *----0-------* -
#
# Figure 2: Top View
#
# *_B
# | \_
# |\ \_
# | \ \_
# | \ \_
# | \A \__
# |<a> *-<b>-__*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

6
src/astree.rs

@ -6,6 +6,9 @@ pub enum FunctionType { @@ -6,6 +6,9 @@ pub enum FunctionType {
Sin,
Cos,
Tan,
Sec,
Csc,
Cot,
Asin,
Acos,
Atan,
@ -41,6 +44,9 @@ impl FunctionData { @@ -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()),

3
src/main.rs

@ -144,6 +144,9 @@ pomelo! { @@ -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)]

Loading…
Cancel
Save