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
extern crate nalgebra as na;
use na::{Point3, Vector3};
pub trait Plane {
fn plane(&mut self) -> Vector3<f32>;
fn on_plane(&self, input_point: &Point3<f32>) -> Result<bool, &'static str>;
}
pub trait Transform{
fn to_global(&self, input_point: Point3<f32>) -> na::Point3<f32>;
fn to_local(&self, input_point: Point3<f32>) -> na::Point3<f32>;
fn contains_from_global(&mut self, input_point: Point3<f32>) -> Result<bool, &'static str>{
let local_point = Self::to_local(&self, input_point);
Self::contains_from_local(&self,&local_point)
}
fn contains_from_local(&self, input: &Point3<f32>) ->Result<bool, &'static str>;
}
pub fn quadralateral_contains(points: &[Point3<f32>;4], check_point: &Point3<f32>)->bool{
let am_vec = vector3_from_points(&points[0] , &check_point);
let ab_vec = vector3_from_points(&points[0], &points[1]);
let ad_vec = vector3_from_points(&points[0] , &points[3]);
let am_dot = am_vec.dot(&ab_vec);
if 0.0 < am_vec.dot(&ab_vec){
println!{"1"}
if am_dot < ab_vec.dot(&ab_vec){
println!{"2"}
let am_dot = am_vec.dot(&ad_vec);
if 0.0 < am_vec.dot(&ad_vec){
println!{"3"}
if am_dot < ad_vec.dot(&ad_vec){
println!{"4"}
return true
}
}
}
}
return false
}
pub fn vector3_from_points(p1: &Point3<f32>, p2: &Point3<f32>) -> Vector3<f32> {
let pv = p1 - p2;
Vector3::new(pv.x, pv.y, pv.z)
}
pub fn plane_normal_vector(p1: &Point3<f32>, p2: &Point3<f32> ,p3: &Point3<f32>) -> Vector3<f32> {
let v1 = vector3_from_points(&p1, &p2);
let v2 = vector3_from_points(&p1, &p3);
v1.cross(&v2)
}
fn distance(p1: &Point3<f32>, p2:&Point3<f32>)->f32{
let pv = p1 - p2;
return (pv.x.powf(2.0) + pv.y.powf(2.0) + pv.z.powf(2.0)).powf(0.5)
}
pub fn organize_points<'a>(input_points: &'a mut [Point3<f32>;4]) -> &'a [Point3<f32>;4]{
let distances : Vec<_> = input_points[1..4].iter()
.zip([input_points[0];4].iter())
.map(|(x,y)| distance(x,y))
.collect();
println!{"the distances are : {:?}", distances}
let mut max_ = distances[0];
let mut max_index = 1;
for i in 1..3{
let current_value = distances[i];
println!{"current max: {} current value: {} index: {}", max_, current_value, max_index}
if current_value > max_{
max_ = current_value;
max_index = i+1;
}
}
println!{"ended with max {} and index {}", max_, max_index}
if max_index != 3{
println!{"changing indexes aroujnd {}", max_index}
let copy = input_points[3];
input_points[3] = input_points[max_index];
input_points[max_index] = copy;
}
else{
println!{"array already oriented correctly"}
}
return input_points
}