diff options
author | xengineering <mail2xengineering@protonmail.com> | 2021-05-25 12:51:50 +0200 |
---|---|---|
committer | xengineering <mail2xengineering@protonmail.com> | 2021-05-25 12:51:50 +0200 |
commit | 4ab710eba1708966f8ae9e637deafa5be060b6d5 (patch) | |
tree | 88c2a8b51a5d0963c1571563fc396500541fee0f /geometry.go | |
parent | e03455a979abf4444d4362dacbbf55c6915d3b69 (diff) | |
download | stlscope-4ab710eba1708966f8ae9e637deafa5be060b6d5.tar stlscope-4ab710eba1708966f8ae9e637deafa5be060b6d5.tar.zst stlscope-4ab710eba1708966f8ae9e637deafa5be060b6d5.zip |
Implement Home View
Diffstat (limited to 'geometry.go')
-rw-r--r-- | geometry.go | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/geometry.go b/geometry.go index 04896dc..1fc516a 100644 --- a/geometry.go +++ b/geometry.go @@ -2,6 +2,10 @@ package main +import ( + "github.com/go-gl/mathgl/mgl32" +) + // representation of a three-dimensional point in space type Point struct { scalars [3]float32 // x = scalars[0], y = scalars[1], z = scalars[2] @@ -17,3 +21,46 @@ type Surface struct { triangles []*Triangle } +// return a transformation to view the whole centered surface +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 +} + |