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
|
// vim: shiftwidth=4 tabstop=4 noexpandtab
package main
import (
"github.com/go-gl/mathgl/mgl32"
)
// Point represents a three-dimensional point in space.
type Point struct {
scalars [3]float32 // x = scalars[0], y = scalars[1], z = scalars[2]
}
// Triangle is a geometrical triangle defined by three points.
type Triangle struct {
points [3]*Point
}
// Surface is a slice of triangles.
type Surface struct {
triangles []*Triangle
}
// getHomeView returns a transformation which shows the given Surface in the
// center of the window.
func (s Surface) getHomeView() mgl32.Mat4 {
// evaluating the smallest and biggest values for x,y,z of the surface
var min,max [3]float32
for _,triangle := range(s.triangles) {
for _,point := range(triangle.points) {
for index,_ := range(min) {
if point.scalars[index] < min[index] {
min[index] = point.scalars[index]
}
if point.scalars[index] > max[index] {
max[index] = point.scalars[index]
}
}
}
}
// calculate center point
var center Point
for index,_ := range(min) {
center.scalars[index] = (min[index] + max[index]) / 2
}
// calculate zoom
var zoom,zoomX,zoomY float32
const fillFactor float32 = 0.8
zoomX = (2 / (max[0] - min[0])) * fillFactor
zoomY = (2 / (max[1] - min[1])) * fillFactor
if zoomX > zoomY {
zoom = zoomY
} else {
zoom = zoomX
}
// calculate and return transformation
var trafo mgl32.Mat4 = mgl32.Ident4()
trafo = mgl32.Translate3D(
-center.scalars[0],
-center.scalars[1],
-center.scalars[2]).Mul4(trafo)
trafo = mgl32.Scale3D(zoom, zoom, zoom).Mul4(trafo)
return trafo
}
|