1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#![warn(missing_docs)]
extern crate alga;
#[cfg(test)]
#[macro_use]
extern crate approx;
extern crate bbox;
extern crate nalgebra as na;
extern crate num_traits;
extern crate stl_io;
use alga::general::Real;
pub use bbox::BoundingBox;
use num_traits::Float;
use std::fmt::Debug;
mod transformer;
pub use self::transformer::AffineTransformer;
mod twister;
pub use self::twister::Twister;
mod bender;
pub use self::bender::Bender;
mod boolean;
pub use self::boolean::{Intersection, Union};
mod sphere;
pub use self::sphere::Sphere;
mod cylinder;
pub use self::cylinder::{Cone, Cylinder};
mod slab;
pub use self::slab::{SlabX, SlabY, SlabZ};
mod mesh;
pub use self::mesh::Mesh;
pub struct PrimitiveParameters<S> {
pub fade_range: S,
pub r_multiplier: S,
}
const ALWAYS_PRECISE: f32 = 1.;
const EPSILON: f32 = 1e-10;
fn normal_from_object<S: Debug + Real + Float + From<f32>>(
f: &Object<S>,
p: na::Point3<S>,
) -> na::Vector3<S> {
let null: S = From::from(0.0);
let e: S = From::from(EPSILON);
let a: S = From::from(ALWAYS_PRECISE);
let epsilon_x = na::Vector3::<S>::new(e, null, null);
let epsilon_y = na::Vector3::<S>::new(null, e, null);
let epsilon_z = na::Vector3::<S>::new(null, null, e);
let center = f.approx_value(p, a);
let dx = f.approx_value(&p + epsilon_x, a) - center;
let dy = f.approx_value(&p + epsilon_y, a) - center;
let dz = f.approx_value(&p + epsilon_z, a) - center;
na::Vector3::<S>::new(dx, dy, dz).normalize()
}
pub trait Object<S: Real + Float + From<f32>>
: ObjectClone<S> + Debug + Sync + Send {
fn bbox(&self) -> &BoundingBox<S>;
fn set_bbox(&mut self, _: BoundingBox<S>) {
unimplemented!();
}
fn set_parameters(&mut self, _: &PrimitiveParameters<S>) {}
fn approx_value(&self, _: na::Point3<S>, _: S) -> S {
unimplemented!();
}
fn normal(&self, _: na::Point3<S>) -> na::Vector3<S> {
unimplemented!();
}
fn translate(&self, v: na::Vector3<S>) -> Box<Object<S>> {
AffineTransformer::new_translate(self.clone_box(), v)
}
fn rotate(&self, r: na::Vector3<S>) -> Box<Object<S>> {
AffineTransformer::new_rotate(self.clone_box(), r)
}
fn scale(&self, s: na::Vector3<S>) -> Box<Object<S>> {
AffineTransformer::new_scale(self.clone_box(), s)
}
}
pub trait ObjectClone<S> {
fn clone_box(&self) -> Box<Object<S>>;
}
impl<S: Real + Float + From<f32>, T> ObjectClone<S> for T
where
T: 'static + Object<S> + Clone,
{
fn clone_box(&self) -> Box<Object<S>> {
Box::new(self.clone())
}
}
impl<S> Clone for Box<Object<S>> {
fn clone(&self) -> Box<Object<S>> {
self.clone_box()
}
}
impl<S> PartialEq for Box<Object<S>> {
fn eq(&self, _: &Box<Object<S>>) -> bool {
false
}
}
impl<S> PartialOrd for Box<Object<S>> {
fn partial_cmp(&self, _: &Box<Object<S>>) -> Option<::std::cmp::Ordering> {
None
}
}