Example #1
0
func TestDeterminant(t *testing.T) {
	detId := Ident.Determinant()
	if detId != 1 {
		t.Errorf("Wrong determinant for identity matrix: %f", detId)
	}

	detTwo := Ident
	detTwo[0][0] = 2
	if det := detTwo.Determinant(); det != 2 {
		t.Errorf("Wrong determinant: %f", det)
	}

	scale2 := Ident.Scale(2)
	if det := scale2.Determinant(); det != 2*2*2*1 {
		t.Errorf("Wrong determinant: %f", det)
	}

	row1changed, _ := Parse("3 0 0 0 2 2 0 0 1 0 2 0 2 0 0 1")
	if det := row1changed.Determinant(); det != 12 {
		t.Errorf("Wrong determinant: %f", det)
	}

	row12changed, _ := Parse("3 1 0 0 2 5 0 0 1 6 2 0 2 100 0 1")
	if det := row12changed.Determinant(); det != 26 {
		t.Errorf("Wrong determinant: %f", det)
	}

	row123changed := ROW_123_CHANGED
	if det := row123changed.Determinant3x3(); det != 60.500 {
		t.Errorf("Wrong determinant for 3x3 matrix: %f", det)
	}
	if det := row123changed.Determinant(); det != 60.500 {
		t.Errorf("Wrong determinant: %f", det)
	}
	randomMatrix, err := Parse("0.43685 0.81673 0.63721 0.23421 0.16600 0.40608 0.53479 0.43210 0.37328 0.36436 0.56356 0.66830 0.32475 0.14294 0.42137 0.98046")
	randomMatrix.Transpose() //transpose for easy comparability with octave output
	if err != nil {
		t.Errorf("Could not parse random matrix: %v", err)
	}
	if det := randomMatrix.Determinant3x3(); math.Abs(det-0.043437) > EPSILON {
		t.Errorf("Wrong determinant for random sub 3x3 matrix: %f", det)
	}

	if det := randomMatrix.Determinant(); math.Abs(det-0.012208) > EPSILON {
		t.Errorf("Wrong determinant for random matrix: %f", det)
	}
}
Example #2
0
File: mat4.go Project: ungerik/go3d
// ExtractEulerAngles extracts the rotation part of the matrix as Euler angle rotation values.
func (mat *T) ExtractEulerAngles() (yHead, xPitch, zRoll float32) {
	xPitch = math.Asin(mat[1][2])
	f12 := math.Abs(mat[1][2])
	if f12 > (1.0-0.0001) && f12 < (1.0+0.0001) { // f12 == 1.0
		yHead = 0.0
		zRoll = math.Atan2(mat[0][1], mat[0][0])
	} else {
		yHead = math.Atan2(-mat[0][2], mat[2][2])
		zRoll = math.Atan2(-mat[1][0], mat[1][1])
	}
	return yHead, xPitch, zRoll
}
Example #3
0
// AxisAngle extracts the rotation in form of an axis and a rotation angle.
func (quat *T) AxisAngle() (axis vec3.T, angle float32) {
	cos := quat[3]
	sin := math.Sqrt(1 - cos*cos)
	angle = math.Acos(cos)

	var ooSin float32
	if math.Abs(sin) < 0.0005 {
		ooSin = 1
	} else {
		ooSin = 1 / sin
	}
	axis[0] = quat[0] * ooSin
	axis[1] = quat[1] * ooSin
	axis[2] = quat[2] * ooSin

	return axis, angle
}
Example #4
0
// Absed returns a copy of the vector containing the absolute values.
func (vec *T) Absed() T {
	return T{math.Abs(vec[0]), math.Abs(vec[1]), math.Abs(vec[2])}
}
Example #5
0
// Abs sets every component of the vector to its absolute value.
func (vec *T) Abs() *T {
	vec[0] = math.Abs(vec[0])
	vec[1] = math.Abs(vec[1])
	vec[2] = math.Abs(vec[2])
	return vec
}