-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrans.go
89 lines (71 loc) · 1.94 KB
/
trans.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
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
package proj
import (
"math"
dmat4 "github.com/flywave/go3d/float64/mat4"
dvec3 "github.com/flywave/go3d/float64/vec3"
)
const (
Deg2Rad = math.Pi / 180
Rad2Deg = 180 / math.Pi
)
var (
wgs84Proj *Proj
ecefProj *Proj
mercProj *Proj
)
func init() {
wgs84Proj, _ = NewProj("+proj=longlat +datum=WGS84 +no_defs")
ecefProj, _ = NewProj("+proj=geocent +datum=WGS84 +units=m +no_defs")
mercProj, _ = NewProj("+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs +over")
}
func Lonlat2Ecef(lon, lat, z float64) (float64, float64, float64, error) {
return Transform3(wgs84Proj, ecefProj, lon*Deg2Rad, lat*Deg2Rad, z)
}
func Ecef2Lonlat(x, y, z float64) (float64, float64, float64, error) {
lon, lat, z, err := Transform3(ecefProj, wgs84Proj, x, y, z)
return lon * Rad2Deg, lat * Rad2Deg, z, err
}
func Lonlat2Merc(lon, lat float64) (float64, float64, error) {
wgs84Proj, _ := NewProj("+proj=longlat +datum=WGS84 +units=m +no_defs")
return Transform2(wgs84Proj, mercProj, lon*Deg2Rad, lat*Deg2Rad)
}
func Merc2Lonlat(x, y float64) (float64, float64, error) {
lon, lat, err := Transform2(mercProj, wgs84Proj, x, y)
return lon * Rad2Deg, lat * Rad2Deg, err
}
func GetUpRotation(x, y, z float64) *dmat4.T {
eye := dvec3.T{x, y, z}
target := dvec3.T{0, 0, 0}
up := dvec3.T{0, 0, 1}
_z := dvec3.Sub(&eye, &target)
if _z.LengthSqr() == 0 {
// eye and target are in the same position
_z[2] = 1
}
_z.Normalize()
_x := dvec3.Cross(&up, &_z)
if _x.LengthSqr() == 0 {
// up and z are parallel
if math.Abs(float64(up[2])) == 1 {
_z[0] += 0.0001
} else {
_z[2] += 0.0001
}
_z.Normalize()
_x = dvec3.Cross(&up, &_z)
}
_x.Normalize()
_y := dvec3.Cross(&_z, &_x)
te := dmat4.Ident
te[0][0] = _x[0]
te[0][1] = _y[0]
te[0][2] = _z[0]
te[1][0] = _x[1]
te[1][1] = _y[1]
te[1][2] = _z[1]
te[2][0] = _x[2]
te[2][1] = _y[2]
te[2][2] = _z[2]
te.Transpose()
return &te
}