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
use nalgebra::{Isometry3, Point3, RealField, Unit, UnitQuaternion, Vector3};
use std::ops::Neg;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Frame<N: RealField> {
pos: Point3<N>,
rot: UnitQuaternion<N>,
zat: N,
}
impl<N: RealField> Frame<N> {
pub fn look_at(eye: &Point3<N>, at: &Point3<N>, up: &Vector3<N>) -> Self {
let dir = at - eye;
Self {
pos: at.clone(),
rot: UnitQuaternion::face_towards(&-dir, up),
zat: dir.norm(),
}
}
pub fn eye(&self) -> Point3<N> {
self.pos + self.rot * Vector3::z_axis().into_inner() * self.zat
}
pub fn set_eye(&mut self, eye: &Point3<N>, up: &Vector3<N>) {
*self = Self::look_at(eye, &self.pos, up);
}
pub fn target(&self) -> &Point3<N> {
&self.pos
}
pub fn set_target(&mut self, at: &Point3<N>) {
let eye = self.eye();
self.pos = at.clone();
self.zat = (self.pos - eye).norm();
}
pub fn distance(&self) -> N {
self.zat
}
pub fn set_distance(&mut self, zat: N) {
self.zat = zat;
}
pub fn scale(&mut self, rat: N) {
self.zat *= rat;
}
pub fn local_scale_at(&mut self, rat: N, pos: &Point3<N>) {
self.scale(rat);
self.local_slide(&(pos - pos * rat));
}
pub fn scale_at(&mut self, rat: N, pos: &Point3<N>) {
self.scale(rat);
self.slide(&(pos - pos * rat));
}
pub fn local_slide(&mut self, vec: &Vector3<N>) {
self.pos += self.rot * vec;
}
pub fn slide(&mut self, vec: &Vector3<N>) {
self.pos += vec;
}
pub fn local_orbit(&mut self, rot: &UnitQuaternion<N>) {
self.rot *= rot;
}
pub fn local_orbit_at(&mut self, rot: &UnitQuaternion<N>, pos: &Point3<N>) {
self.local_orbit(rot);
self.local_slide(&(pos - rot * pos));
}
pub fn orbit(&mut self, rot: &UnitQuaternion<N>) {
self.rot = rot * self.rot;
}
pub fn orbit_at(&mut self, rot: &UnitQuaternion<N>, pos: &Point3<N>) {
self.orbit(rot);
self.slide(&(pos - rot * pos));
}
pub fn local_pitch_axis(&self) -> Unit<Vector3<N>> {
Vector3::x_axis()
}
pub fn local_yaw_axis(&self) -> Unit<Vector3<N>> {
Vector3::y_axis()
}
pub fn local_roll_axis(&self) -> Unit<Vector3<N>> {
Vector3::z_axis()
}
pub fn pitch_axis(&self) -> Unit<Vector3<N>> {
self.rot * self.local_pitch_axis()
}
pub fn yaw_axis(&self) -> Unit<Vector3<N>> {
self.rot * self.local_yaw_axis()
}
pub fn roll_axis(&self) -> Unit<Vector3<N>> {
self.rot * self.local_roll_axis()
}
pub fn angles(&self) -> (N, N, N) {
self.rot.euler_angles()
}
pub fn set_angles(&mut self, pitch: N, yaw: N, roll: N) {
self.rot = UnitQuaternion::from_euler_angles(pitch, yaw, roll);
}
pub fn view(&self) -> Isometry3<N> {
let rot = self.rot.inverse();
let eye = rot * self.pos + Vector3::z_axis().into_inner() * self.zat;
Isometry3::from_parts(eye.coords.neg().into(), rot)
}
}