Skip to content

Commit 5493f1c

Browse files
committedAug 17, 2022
make Vec3 math methods public
1 parent c252098 commit 5493f1c

8 files changed

+42
-41
lines changed
 

‎float.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const HalfPi = math.Pi * 0.5
1818
// QuarterPi is math.Pi * 0.25
1919
const QuarterPi = math.Pi * 0.25
2020

21-
// almostEqual returns true, if a and b are equal allowing for numerical error tol.
21+
// AlmostEqual returns true, if a and b are equal allowing for numerical error tol.
2222
func almostEqual32(a, b, tol float32) bool {
2323
return math.Abs(float64(a-b)) <= float64(tol)
2424
}

‎solid.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func (s *Solid) Measure() SolidMeasure {
9191
}
9292
}
9393

94-
measure.Len = measure.Max.diff(measure.Min)
94+
measure.Len = measure.Max.Diff(measure.Min)
9595

9696
return measure
9797
}
@@ -226,10 +226,11 @@ func (s *Solid) scaleDim(factor float64, dim int) {
226226
}
227227
}
228228

229-
// Rotate the solid by angle radians around a rotation axis defined
229+
// Rotate the solid by Angle radians around a rotation axis defined
230230
// by a point pos on the axis and a direction vector dir. This
231231
// example would rotate the solid by 90 degree around the z-axis:
232-
// stl.Rotate(stl.Vec3{0,0,0}, stl.Vec3{0,0,1}, stl.HalfPi)
232+
//
233+
// stl.Rotate(stl.Vec3{0,0,0}, stl.Vec3{0,0,1}, stl.HalfPi)
233234
func (s *Solid) Rotate(pos, dir Vec3, angle float64) {
234235
var rotationMatrix Mat4
235236
RotationMatrix(pos, dir, angle, &rotationMatrix)

‎solid_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func TestRotate(t *testing.T) {
131131
s := makeTestSolid()
132132
s.Rotate(Vec3{0, 0, 0}, Vec3{0, 0, 1}, 0)
133133
if !sOrig.sameOrderAlmostEqual(s) {
134-
t.Error("Not equal after rotation around z-axis with 0 angle")
134+
t.Error("Not equal after rotation around z-axis with 0 Angle")
135135
t.Log("Expected:\n", sOrig)
136136
t.Log("Found:\n", s)
137137
}

‎test_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ func (s *Solid) sameOrderAlmostEqual(o *Solid) bool {
8989
}
9090

9191
func (t *Triangle) sameOrderAlmostEqual(o *Triangle, tol float32) bool {
92-
return t.Normal.almostEqual(o.Normal, tol) &&
93-
t.Vertices[0].almostEqual(o.Vertices[0], tol) &&
94-
t.Vertices[1].almostEqual(o.Vertices[1], tol) &&
95-
t.Vertices[2].almostEqual(o.Vertices[2], tol) &&
92+
return t.Normal.AlmostEqual(o.Normal, tol) &&
93+
t.Vertices[0].AlmostEqual(o.Vertices[0], tol) &&
94+
t.Vertices[1].AlmostEqual(o.Vertices[1], tol) &&
95+
t.Vertices[2].AlmostEqual(o.Vertices[2], tol) &&
9696
t.Attributes == o.Attributes
9797
}

‎transform.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import (
66
"math"
77
)
88

9-
// RotationMatrix calculates a 4x4 rotation matrix for a rotation of angle in radians
9+
// RotationMatrix calculates a 4x4 rotation matrix for a rotation of Angle in radians
1010
// around a rotation axis defined by a point on it (pos) and its direction (dir).
1111
// The result is written into *rotationMatrix.
1212
func RotationMatrix(pos Vec3, dir Vec3, angle float64, rotationMatrix *Mat4) {
13-
dirN := dir.unitVec()
13+
dirN := dir.UnitVec3()
1414

1515
s := math.Sin(angle)
1616
c := math.Cos(angle)

‎triangle.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ type Triangle struct {
2626
func (t *Triangle) calculateNormal() Vec3 {
2727
// The normal is calculated by normalizing the result of
2828
// (V0-V2) x (V1-V2)
29-
return t.Vertices[0].diff(t.Vertices[2]).
30-
cross(t.Vertices[1].diff(t.Vertices[2])).
31-
unitVec()
29+
return t.Vertices[0].Diff(t.Vertices[2]).
30+
Cross(t.Vertices[1].Diff(t.Vertices[2])).
31+
UnitVec3()
3232
}
3333

3434
// Recalculate the redundant normal vector using the right hand rule
@@ -52,16 +52,16 @@ func (t *Triangle) transformNR(transformationMatrix *Mat4) {
5252
}
5353

5454
// Returns true if at least two vertices are exactly equal, meaning
55-
// this is a line, or even a dot.
55+
// this is a line, or even a Dot.
5656
func (t *Triangle) hasEqualVertices() bool {
5757
return t.Vertices[0] == t.Vertices[1] ||
5858
t.Vertices[0] == t.Vertices[2] ||
5959
t.Vertices[1] == t.Vertices[2]
6060
}
6161

6262
// Checks if normal matches vertices using right hand rule, with
63-
// numerical tolerance for angle between them given by tol in radians.
63+
// numerical tolerance for Angle between them given by tol in radians.
6464
func (t *Triangle) checkNormal(tol float64) bool {
6565
calculatedNormal := t.calculateNormal()
66-
return t.Normal.angle(calculatedNormal) < tol
66+
return t.Normal.Angle(calculatedNormal) < tol
6767
}

‎vec3.go

+17-17
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ func (vec Vec3) len() float64 {
1717
return math.Sqrt(float64(vec[0]*vec[0] + vec[1]*vec[1] + vec[2]*vec[2]))
1818
}
1919

20-
// unitVec returns vec multiplied by 1/vec.Len(), so its new length is 1. If the vector is empty, it is returned as such.
21-
func (vec Vec3) unitVec() Vec3 {
20+
// UnitVec3 returns vec multiplied by 1/vec.Len(), so its new length is 1. If the vector is empty, it is returned as such.
21+
func (vec Vec3) UnitVec3() Vec3 {
2222
l := vec.len()
2323
if l == 0 {
2424
return vec
@@ -27,58 +27,58 @@ func (vec Vec3) unitVec() Vec3 {
2727
return Vec3{float32(float64(vec[0]) / l), float32(float64(vec[1]) / l), float32(float64(vec[2]) / l)}
2828
}
2929

30-
// multScalar multiplies vec by scalar.
31-
func (vec Vec3) multScalar(scalar float64) Vec3 {
30+
// MultScalar multiplies vec by scalar.
31+
func (vec Vec3) MultScalar(scalar float64) Vec3 {
3232
return Vec3{float32(float64(vec[0]) * scalar), float32(float64(vec[1]) * scalar), float32(float64(vec[2]) * scalar)}
3333
}
3434

35-
// almostEqual returns true if vec and o are equal allowing for numerical error tol.
36-
func (vec Vec3) almostEqual(o Vec3, tol float32) bool {
35+
// AlmostEqual returns true if vec and o are equal allowing for numerical error tol.
36+
func (vec Vec3) AlmostEqual(o Vec3, tol float32) bool {
3737
return almostEqual32(vec[0], o[0], tol) && almostEqual32(vec[1], o[1], tol) && almostEqual32(vec[2], o[2], tol)
3838
}
3939

40-
// add returns the sum of vectors vec and o.
41-
func (vec Vec3) add(o Vec3) Vec3 {
40+
// Add returns the sum of vectors vec and o.
41+
func (vec Vec3) Add(o Vec3) Vec3 {
4242
return Vec3{
4343
vec[0] + o[0],
4444
vec[1] + o[1],
4545
vec[2] + o[2],
4646
}
4747
}
4848

49-
// diff returns the difference vec - o.
50-
func (vec Vec3) diff(o Vec3) Vec3 {
49+
// Diff returns the difference vec - o.
50+
func (vec Vec3) Diff(o Vec3) Vec3 {
5151
return Vec3{
5252
vec[0] - o[0],
5353
vec[1] - o[1],
5454
vec[2] - o[2],
5555
}
5656
}
5757

58-
// cross returns the vector cross product vec x o.
59-
func (vec Vec3) cross(o Vec3) Vec3 {
58+
// Cross returns the vector Cross product vec x o.
59+
func (vec Vec3) Cross(o Vec3) Vec3 {
6060
return Vec3{
6161
vec[1]*o[2] - vec[2]*o[1],
6262
vec[2]*o[0] - vec[0]*o[2],
6363
vec[0]*o[1] - vec[1]*o[0],
6464
}
6565
}
6666

67-
// dot returns the dot product between vec and o.
68-
func (vec Vec3) dot(o Vec3) float64 {
67+
// Dot returns the Dot product between vec and o.
68+
func (vec Vec3) Dot(o Vec3) float64 {
6969
return float64(vec[0])*float64(o[0]) +
7070
float64(vec[1])*float64(o[1]) +
7171
float64(vec[2])*float64(o[2])
7272
}
7373

74-
// angle between vec and o in radians, without sign, between 0 and Pi.
74+
// Angle between vec and o in radians, without sign, between 0 and Pi.
7575
// If vec or o is the origin, this returns 0.
76-
func (vec Vec3) angle(o Vec3) float64 {
76+
func (vec Vec3) Angle(o Vec3) float64 {
7777
lenProd := vec.len() * o.len()
7878
if lenProd == 0 {
7979
return 0
8080
}
81-
cosAngle := vec.dot(o) / lenProd
81+
cosAngle := vec.Dot(o) / lenProd
8282
// Numerical correction
8383
if cosAngle < -1 {
8484
cosAngle = -1

‎vec3_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ func TestVec3Angle(t *testing.T) {
1010
v := Vec3{1, 0, 0}
1111
tol := 0.00005
1212
testV := []Vec3{
13-
Vec3{0, 1, 0},
14-
Vec3{0, -1, 0},
15-
Vec3{-1, 0, 0},
16-
Vec3{-1, 1, 0},
17-
Vec3{-1, -1, 0}}
13+
{0, 1, 0},
14+
{0, -1, 0},
15+
{-1, 0, 0},
16+
{-1, 1, 0},
17+
{-1, -1, 0}}
1818
expected := []float64{
1919
HalfPi,
2020
HalfPi,
2121
Pi,
2222
HalfPi + QuarterPi,
2323
HalfPi + QuarterPi}
2424
for i, tv := range testV {
25-
r := v.angle(tv)
25+
r := v.Angle(tv)
2626
if !almostEqual64(expected[i], r, tol) {
27-
t.Errorf("angle(%v, %v) = %g Pi, expected %g Pi", v, tv, r/Pi, expected[i]/Pi)
27+
t.Errorf("Angle(%v, %v) = %g Pi, expected %g Pi", v, tv, r/Pi, expected[i]/Pi)
2828
}
2929
}
3030
}

0 commit comments

Comments
 (0)
Please sign in to comment.