-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspherical.go
60 lines (50 loc) · 1.26 KB
/
spherical.go
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
package math
type Spherical struct {
radius float32
phi float32 // polar angle
theta float32 // azimuthal angle
}
func NewDefaultSpherical() *Spherical {
return NewSpherical(1, 0, 0)
}
func NewSpherical(radius float32, phi float32, theta float32) *Spherical {
return &Spherical{
radius: radius,
phi: phi,
theta: theta,
}
}
func (sphere *Spherical) Set(radius float32, phi float32, theta float32) {
sphere.radius = radius
sphere.phi = phi
sphere.theta = theta
}
func (sphere *Spherical) Clone() *Spherical {
return &Spherical{
radius: sphere.radius,
phi: sphere.phi,
theta: sphere.theta,
}
}
func (sphere *Spherical) Copy(source *Spherical) {
sphere.radius = source.radius
sphere.phi = source.phi
sphere.theta = source.theta
}
func (sphere *Spherical) MakeSafe() {
const EPS = float32(0.000001)
sphere.phi = Max(EPS, Min(Pi-EPS, sphere.phi))
}
func (sphere *Spherical) SetFromVector3(vec *Vector3) {
sphere.SetFromCartesianCoordinates(vec.X, vec.Y, vec.Z)
}
func (sphere *Spherical) SetFromCartesianCoordinates(x float32, y float32, z float32) {
sphere.radius = Sqrt(x*x + y*y + z*z)
if sphere.radius == 0 {
sphere.theta = 0
sphere.phi = 0
} else {
sphere.theta = Atan2(x, z)
sphere.phi = Acos(Clamp(y/sphere.radius, - 1, 1))
}
}