Simplify Matrix math, use 6-value affine matrixes.

It turns out that affine matrices are much simpler than the 3x3 matrices
they imply, and we can use this to dramatically streamline some code.
For a test program, this was about a 50% gain in frame rate just from
the cost of the applyMatrixAndMask calls in imdraw, which were calling
matrix.Project() many times. Simplifying matrix.Project, alone, got a
nearly 50% frame rate boost!

Also modify pixelgl's SetMatrix to copy the six values of a 3x2
Affine into the corresponding locations of a 3x3 matrix.
This commit is contained in:
Seebs 2017-06-05 18:54:53 -05:00
parent 1586e600a0
commit c321515d3c
2 changed files with 51 additions and 35 deletions

View File

@ -3,8 +3,6 @@ package pixel
import ( import (
"fmt" "fmt"
"math" "math"
"github.com/go-gl/mathgl/mgl64"
) )
// Vec is a 2D vector type with X and Y coordinates. // Vec is a 2D vector type with X and Y coordinates.
@ -251,7 +249,7 @@ func (r Rect) Union(s Rect) Rect {
) )
} }
// Matrix is a 3x3 transformation matrix that can be used for all kinds of spacial transforms, such // Matrix is a 3x2 affine matrix that can be used for all kinds of spatial transforms, such
// as movement, scaling and rotations. // as movement, scaling and rotations.
// //
// Matrix has a handful of useful methods, each of which adds a transformation to the matrix. For // Matrix has a handful of useful methods, each of which adds a transformation to the matrix. For
@ -261,38 +259,41 @@ func (r Rect) Union(s Rect) Rect {
// //
// This code creates a Matrix that first moves everything by 100 units horizontally and 200 units // This code creates a Matrix that first moves everything by 100 units horizontally and 200 units
// vertically and then rotates everything by 90 degrees around the origin. // vertically and then rotates everything by 90 degrees around the origin.
type Matrix [9]float64 //
// Layout is:
// [0] [2] [4]
// [1] [3] [5]
// 0 0 1 [implicit row]
type Matrix [6]float64
// IM stands for identity matrix. Does nothing, no transformation. // IM stands for identity matrix. Does nothing, no transformation.
var IM = Matrix(mgl64.Ident3()) var IM = Matrix{1, 0, 0, 1, 0, 0}
// String returns a string representation of the Matrix. // String returns a string representation of the Matrix.
// //
// m := pixel.IM // m := pixel.IM
// fmt.Println(m) // Matrix(1 0 0 | 0 1 0 | 0 0 1) // fmt.Println(m) // Matrix(1 0 0 | 0 1 0)
func (m Matrix) String() string { func (m Matrix) String() string {
return fmt.Sprintf( return fmt.Sprintf(
"Matrix(%v %v %v | %v %v %v | %v %v %v)", "Matrix(%v %v %v | %v %v %v)",
m[0], m[3], m[6], m[0], m[2], m[4],
m[1], m[4], m[7], m[1], m[3], m[5],
m[2], m[5], m[8],
) )
} }
// Moved moves everything by the delta vector. // Moved moves everything by the delta vector.
func (m Matrix) Moved(delta Vec) Matrix { func (m Matrix) Moved(delta Vec) Matrix {
m3 := mgl64.Mat3(m) m[4], m[5] = m[4]+delta.X, m[5]+delta.Y
m3 = mgl64.Translate2D(delta.XY()).Mul3(m3) return m
return Matrix(m3)
} }
// ScaledXY scales everything around a given point by the scale factor in each axis respectively. // ScaledXY scales everything around a given point by the scale factor in each axis respectively.
func (m Matrix) ScaledXY(around Vec, scale Vec) Matrix { func (m Matrix) ScaledXY(around Vec, scale Vec) Matrix {
m3 := mgl64.Mat3(m) m[4], m[5] = m[4]-around.X, m[5]-around.Y
m3 = mgl64.Translate2D(around.Scaled(-1).XY()).Mul3(m3) m[0], m[2], m[4] = m[0]*scale.X, m[2]*scale.X, m[4]*scale.X
m3 = mgl64.Scale2D(scale.XY()).Mul3(m3) m[1], m[3], m[5] = m[1]*scale.Y, m[3]*scale.Y, m[5]*scale.Y
m3 = mgl64.Translate2D(around.XY()).Mul3(m3) m[4], m[5] = m[4]+around.X, m[5]+around.Y
return Matrix(m3) return m
} }
// Scaled scales everything around a given point by the scale factor. // Scaled scales everything around a given point by the scale factor.
@ -302,36 +303,44 @@ func (m Matrix) Scaled(around Vec, scale float64) Matrix {
// Rotated rotates everything around a given point by the given angle in radians. // Rotated rotates everything around a given point by the given angle in radians.
func (m Matrix) Rotated(around Vec, angle float64) Matrix { func (m Matrix) Rotated(around Vec, angle float64) Matrix {
m3 := mgl64.Mat3(m) sint, cost := math.Sincos(angle)
m3 = mgl64.Translate2D(around.Scaled(-1).XY()).Mul3(m3) m[4], m[5] = m[4]-around.X, m[5]-around.Y
m3 = mgl64.Rotate3DZ(angle).Mul3(m3) m = m.Chained(Matrix{cost, sint, -sint, cost, 0, 0})
m3 = mgl64.Translate2D(around.XY()).Mul3(m3) m[4], m[5] = m[4]+around.X, m[5]+around.Y
return Matrix(m3) return m
} }
// Chained adds another Matrix to this one. All tranformations by the next Matrix will be applied // Chained adds another Matrix to this one. All tranformations by the next Matrix will be applied
// after the transformations of this Matrix. // after the transformations of this Matrix.
func (m Matrix) Chained(next Matrix) Matrix { func (m Matrix) Chained(next Matrix) Matrix {
m3 := mgl64.Mat3(m) return Matrix{
m3 = mgl64.Mat3(next).Mul3(m3) m[0]*next[0] + m[2]*next[1],
return Matrix(m3) m[1]*next[0] + m[3]*next[1],
m[0]*next[2] + m[2]*next[3],
m[1]*next[2] + m[3]*next[3],
m[0]*next[4] + m[2]*next[5] + m[4],
m[1]*next[4] + m[3]*next[5] + m[5],
}
} }
// Project applies all transformations added to the Matrix to a vector u and returns the result. // Project applies all transformations added to the Matrix to a vector u and returns the result.
// //
// Time complexity is O(1). // Time complexity is O(1).
func (m Matrix) Project(u Vec) Vec { func (m Matrix) Project(u Vec) Vec {
m3 := mgl64.Mat3(m) return Vec{X: m[0]*u.X + m[2]*u.Y + m[4], Y: m[1]*u.X + m[3]*u.Y + m[5]}
proj := m3.Mul3x1(mgl64.Vec3{u.X, u.Y, 1})
return V(proj.X(), proj.Y())
} }
// Unproject does the inverse operation to Project. // Unproject does the inverse operation to Project.
// //
// It turns out that multiplying a vector by the inverse matrix of m
// can be nearly-accomplished by subtracting the translate part of the
// matrix and multplying by the inverse of the top-left 2x2 matrix,
// and the inverse of a 2x2 matrix is simple enough to just be
// inlined in the computation.
//
// Time complexity is O(1). // Time complexity is O(1).
func (m Matrix) Unproject(u Vec) Vec { func (m Matrix) Unproject(u Vec) Vec {
m3 := mgl64.Mat3(m) d := (m[0] * m[3]) - (m[1] * m[2])
inv := m3.Inv() u.X, u.Y = (u.X-m[4])/d, (u.Y-m[5])/d
unproj := inv.Mul3x1(mgl64.Vec3{u.X, u.Y, 1}) return Vec{u.X*m[3] - u.Y*m[1], u.Y*m[0] - u.X*m[2]}
return V(unproj.X(), unproj.Y())
} }

View File

@ -90,9 +90,16 @@ func (c *Canvas) MakePicture(p pixel.Picture) pixel.TargetPicture {
} }
// SetMatrix sets a Matrix that every point will be projected by. // SetMatrix sets a Matrix that every point will be projected by.
// pixel.Matrix is 3x2 with an implicit 0, 0, 1 row after it. So
// [0] [2] [4] [0] [3] [6]
// [1] [3] [5] => [1] [4] [7]
// 0 0 1 0 0 1
// since all matrix ops are affine, the last row never changes,
// and we don't need to copy it
//
func (c *Canvas) SetMatrix(m pixel.Matrix) { func (c *Canvas) SetMatrix(m pixel.Matrix) {
for i := range m { for i, j := range [6]int{ 0, 1, 3, 4, 6, 7} {
c.mat[i] = float32(m[i]) c.mat[j] = float32(m[i])
} }
} }