Beispiel #1
0
// Normalize normalizes the vector to unit length.
func (vec *T) Normalize() *T {
	sl := vec.LengthSqr()
	if sl == 0 || sl == 1 {
		return vec
	}
	return vec.Scale(1 / math.Sqrt(sl))
}
Beispiel #2
0
// Vec3Diff returns the rotation quaternion between two vectors.
func Vec3Diff(a, b *vec3.T) T {
	cr := vec3.Cross(a, b)
	sr := math.Sqrt(2 * (1 + vec3.Dot(a, b)))
	oosr := 1 / sr

	q := T{cr[0] * oosr, cr[1] * oosr, cr[2] * oosr, sr * 0.5}
	return q.Normalized()
}
Beispiel #3
0
// Normalize normalizes to a unit quaternation.
func (quat *T) Normalize() *T {
	norm := quat.Norm()
	if norm != 1 && norm != 0 {
		ool := 1 / math.Sqrt(norm)
		quat[0] *= ool
		quat[1] *= ool
		quat[2] *= ool
		quat[3] *= ool
	}
	return quat
}
Beispiel #4
0
// Normalized returns a copy normalized to a unit quaternation.
func (quat *T) Normalized() T {
	norm := quat.Norm()
	if norm != 1 && norm != 0 {
		ool := 1 / math.Sqrt(norm)
		return T{
			quat[0] * ool,
			quat[1] * ool,
			quat[2] * ool,
			quat[3] * ool,
		}
	} else {
		return *quat
	}
}
Beispiel #5
0
// Quaternion extracts a quaternion from the rotation part of the matrix.
func (mat *T) Quaternion() quaternion.T {
	tr := mat.Trace()

	s := math.Sqrt(tr + 1)
	w := s * 0.5
	s = 0.5 / s

	q := quaternion.T{
		(mat[1][2] - mat[2][1]) * s,
		(mat[2][0] - mat[0][2]) * s,
		(mat[0][1] - mat[1][0]) * s,
		w,
	}
	return q.Normalized()
}
Beispiel #6
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
}
Beispiel #7
0
// Length returns the length of the vector.
// See also LengthSqr and Normalize.
func (vec *T) Length() float32 {
	return float32(math.Sqrt(vec.LengthSqr()))
}
Beispiel #8
0
func HSLMap(x, y, z float32) color.NRGBA {
	s := fmath.Sqrt(x*x + y*y + z*z)
	l := 0.5*z + 0.5
	h := float32(math.Atan2(float64(y), float64(x)))
	return HSL(h, s, l)
}