<commit_before>// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package f32

import "fmt"

// A Mat4 is a 4x4 matrix of float32 values.
// Elements are indexed first by row then column, i.e. m[row][column].
type Mat4 [4]Vec4

func (m *Mat4) String() string {
	return fmt.Sprintf(`Mat4[% 0.3f, % 0.3f, % 0.3f, % 0.3f,
     % 0.3f, % 0.3f, % 0.3f, % 0.3f,
     % 0.3f, % 0.3f, % 0.3f, % 0.3f,
     % 0.3f, % 0.3f, % 0.3f, % 0.3f]`,
		m[0][0], m[0][1], m[0][2], m[0][3],
		m[1][0], m[1][1], m[1][2], m[1][3],
		m[2][0], m[2][1], m[2][2], m[2][3],
		m[3][0], m[3][1], m[3][2], m[3][3])
}

func (m *Mat4) Identity() {
	*m = Mat4{
		{1, 0, 0, 0},
		{0, 1, 0, 0},
		{0, 0, 1, 0},
		{0, 0, 0, 1},
	}
}

func (m *Mat4) Eq(n *Mat4, epsilon float32) bool {
	for i := range m {
		for j := range m[i] {
			diff := m[i][j] - n[i][j]
			if diff < -epsilon || +epsilon < diff {
				return false
			}
		}
	}
	return true
}

func (m *Mat4) Mul(m0, m1 *Mat4) {
	// If you intend to make this faster, skip the usual loop unrolling
	// games and go straight to halide/renderscript/etc.
	for i := 0; i < 4; i++ {
		for j := 0; j < 4; j++ {
			m[i][j] = m1[i][0]*m0[0][j] +
				m1[i][1]*m0[1][j] +
				m1[i][2]*m0[2][j] +
				m1[i][3]*m0[3][j]
		}
	}
}

func (m *Mat4) Perspective(fov Radian, aspect, near, far float32) {
	t := Tan(float32(fov) / 2)

	m[0][0] = 1 / (aspect * t)
	m[1][1] = 1 / t
	m[2][2] = -(far + near) / (far - near)
	m[2][3] = -1
	m[3][2] = -2 * far * near / (far - near)
}

func (m *Mat4) Scale(src *Mat4, scale *Vec3) {
	for i, s := range scale {
		m[i][0] = src[i][0] * s
		m[i][1] = src[i][1] * s
		m[i][2] = src[i][2] * s
		m[i][3] = src[i][3] * s
	}
	m[3] = src[3]
}

func (m *Mat4) Translate(src Mat4, v *Vec3) {
	*m = src

	m[3][0] = src[0][0]*v[0] + src[1][0]*v[1] + src[2][0]*v[2] + src[3][0]<commit_msg>go.mobile/f32: make Mat4.Translate take a pointer, to be consistent with the other methods.<commit_after>// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package f32

import "fmt"

// A Mat4 is a 4x4 matrix of float32 values.
// Elements are indexed first by row then column, i.e. m[row][column].
type Mat4 [4]Vec4

func (m *Mat4) String() string {
	return fmt.Sprintf(`Mat4[% 0.3f, % 0.3f, % 0.3f, % 0.3f,
     % 0.3f, % 0.3f, % 0.3f, % 0.3f,
     % 0.3f, % 0.3f, % 0.3f, % 0.3f,
     % 0.3f, % 0.3f, % 0.3f, % 0.3f]`,
		m[0][0], m[0][1], m[0][2], m[0][3],
		m[1][0], m[1][1], m[1][2], m[1][3],
		m[2][0], m[2][1], m[2][2], m[2][3],
		m[3][0], m[3][1], m[3][2], m[3][3])
}

func (m *Mat4) Identity() {
	*m = Mat4{
		{1, 0, 0, 0},
		{0, 1, 0, 0},
		{0, 0, 1, 0},
		{0, 0, 0, 1},
	}
}

func (m *Mat4) Eq(n *Mat4, epsilon float32) bool {
	for i := range m {
		for j := range m[i] {
			diff := m[i][j] - n[i][j]
			if diff < -epsilon || +epsilon < diff {
				return false
			}
		}
	}
	return true
}

func (m *Mat4) Mul(m0, m1 *Mat4) {
	// If you intend to make this faster, skip the usual loop unrolling
	// games and go straight to halide/renderscript/etc.
	for i := 0; i < 4; i++ {
		for j := 0; j < 4; j++ {
			m[i][j] = m1[i][0]*m0[0][j] +
				m1[i][1]*m0[1][j] +
				m1[i][2]*m0[2][j] +
				m1[i][3]*m0[3][j]
		}
	}
}

func (m *Mat4) Perspective(fov Radian, aspect, near, far float32) {
	t := Tan(float32(fov) / 2)

	m[0][0] = 1 / (aspect * t)
	m[1][1] = 1 / t
	m[2][2] = -(far + near) / (far - near)
	m[2][3] = -1
	m[3][2] = -2 * far * near / (far - near)
}

func (m *Mat4) Scale(src *Mat4, scale *Vec3) {
	for i, s := range scale {
		m[i][0] = src[i][0] * s
		m[i][1] = src[i][1] * s
		m[i][2] = src[i][2] * s
		m[i][3] = src[i][3] * s
	}
	m[3] = src[3]
}

func (m *Mat4) Translate(src *Mat4, v *Vec3) {
	*m = *src

	m[3][0] = src[0][0]*v[0] + src[1][0]*v[1] + src[2][0]*v[2] + src[3][0]
{'fraction_non_alphanumeric': 0.2512742099898063, 'fraction_numerical': 0.0899592252803262, 'mean_word_length': 2.770413064361191, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2581246', 'n_tokens_mistral': 2152, 'n_tokens_neox': 1976, 'n_words': 523}