Skip to content

Commit

Permalink
go fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
hschendel committed Aug 26, 2017
1 parent e5a0c7f commit bae65ab
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 65 deletions.
4 changes: 2 additions & 2 deletions float.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ const tolerance = float32(0.0005)

// Returns true, if a and b are equal allowing for numerical error tol.
func almostEqual32(a, b, tol float32) bool {
return math.Abs(float64(a-b)) <= float64(tol)
return math.Abs(float64(a-b)) <= float64(tol)
}

// Returns true, if a and b are equal allowing for numerical error tol.
func almostEqual64(a, b, tol float64) bool {
return math.Abs(a-b) <= tol
return math.Abs(a-b) <= tol
}

func min(a, b float32) float32 {
Expand Down
38 changes: 18 additions & 20 deletions readwrite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ const TestFilenameComplexAscii = "testdata/complex_ascii.stl"
const TestFilenameComplexBinary = "testdata/complex_bin.stl"

func TestIsAsciiFile(t *testing.T) {
asciiFile, openAsciiErr := os.Open(TestFilenameSimpleAscii)
if openAsciiErr != nil {
t.Fatal(openAsciiErr)
}
defer asciiFile.Close()
asciiFile, openAsciiErr := os.Open(TestFilenameSimpleAscii)
if openAsciiErr != nil {
t.Fatal(openAsciiErr)
}
defer asciiFile.Close()

isAscii, _, err := isAsciiFile(asciiFile)
if err != nil {
t.Fatal(err)
Expand All @@ -29,12 +29,12 @@ func TestIsAsciiFile(t *testing.T) {
t.Error("ASCII file not detected as ASCII")
}

binaryFile, openBinaryErr := os.Open(TestFilenameSimpleBinary)
if openBinaryErr != nil {
t.Fatal(openBinaryErr)
}
defer binaryFile.Close()
binaryFile, openBinaryErr := os.Open(TestFilenameSimpleBinary)
if openBinaryErr != nil {
t.Fatal(openBinaryErr)
}
defer binaryFile.Close()

isAscii, _, err = isAsciiFile(binaryFile)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -237,23 +237,22 @@ func BenchmarkWriteSmallFile_Binary(b *testing.B) {
}
}


// Does assume ReadFile to work correctly
func BenchmarkWriteMediumFile_Binary(b *testing.B) {
b.StopTimer()
b.StopTimer()
tmpDirName, tmpErr := ioutil.TempDir(os.TempDir(), "stl_test")
if tmpErr != nil {
b.Fatal(tmpErr)
}
defer os.RemoveAll(tmpDirName)

testSolid, readErr := ReadFile(TestFilenameComplexBinary)
if readErr != nil {
b.Fatal(readErr)
}
if readErr != nil {
b.Fatal(readErr)
}

testSolid.IsAscii = false // to be sure ;-)
b.StartTimer()
b.StartTimer()
var tmpFileName string
for i := 0; i < b.N; i++ {
// generate a new file name for every turn, as overwriting the same file again
Expand All @@ -265,4 +264,3 @@ func BenchmarkWriteMediumFile_Binary(b *testing.B) {
}
}
}

4 changes: 2 additions & 2 deletions solid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func TestScale(t *testing.T) {
func TestStretch(t *testing.T) {
sOrig := makeTestSolid()
s := makeTestSolid()
s.Stretch(Vec3{1,2,1})
s.Stretch(Vec3{1,0.5,1})
s.Stretch(Vec3{1, 2, 1})
s.Stretch(Vec3{1, 0.5, 1})
if !sOrig.sameOrderAlmostEqual(s) {
t.Error("Not equal after successive Y scaling * 2 * 0.5")
}
Expand Down
6 changes: 3 additions & 3 deletions triangle.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Triangle struct {

// Calculate the normal vector using the right hand rule
func (t *Triangle) calculateNormal() Vec3 {
// The normal is calculated by normalizing the result of
// The normal is calculated by normalizing the result of
// (V0-V2) x (V1-V2)
return t.Vertices[0].diff(t.Vertices[2]).
cross(t.Vertices[1].diff(t.Vertices[2])).
Expand Down Expand Up @@ -62,6 +62,6 @@ func (t *Triangle) hasEqualVertices() bool {
// Checks if normal matches vertices using right hand rule, with
// numerical tolerance for angle between them given by tol in radians.
func (t *Triangle) checkNormal(tol float64) bool {
calculatedNormal := t.calculateNormal()
return t.Normal.angle(calculatedNormal) < tol
calculatedNormal := t.calculateNormal()
return t.Normal.angle(calculatedNormal) < tol
}
34 changes: 17 additions & 17 deletions vec3.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,25 @@ func (vec Vec3) cross(o Vec3) Vec3 {

// Return dot product between vec and o.
func (vec Vec3) dot(o Vec3) float64 {
return float64(vec[0]) * float64(o[0]) +
float64(vec[1]) * float64(o[1]) +
float64(vec[2]) * float64(o[2])
return float64(vec[0])*float64(o[0]) +
float64(vec[1])*float64(o[1]) +
float64(vec[2])*float64(o[2])
}

// Return angle between vec and o in radians, without sign, between 0 and Pi.
// If vec or o is the origin, this returns 0.
func (vec Vec3) angle(o Vec3) float64 {
lenProd := vec.len() * o.len()
if lenProd == 0 {
return 0
}
cosAngle := vec.dot(o) / lenProd
// Numerical correction
if cosAngle < -1 {
cosAngle = -1
} else if cosAngle > 1 {
cosAngle = 1
}
return math.Acos(cosAngle)
}
lenProd := vec.len() * o.len()
if lenProd == 0 {
return 0
}
cosAngle := vec.dot(o) / lenProd
// Numerical correction
if cosAngle < -1 {
cosAngle = -1
} else if cosAngle > 1 {
cosAngle = 1
}

return math.Acos(cosAngle)
}
42 changes: 21 additions & 21 deletions vec3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@ package stl
// This file contains tests for the Vec3 data type

import (
"testing"
"testing"
)

func TestVec3Angle(t *testing.T) {
v := Vec3 { 1, 0, 0 }
tol := 0.00005
testV := []Vec3 {
Vec3{0, 1, 0},
Vec3{0, -1, 0},
Vec3{-1, 0, 0},
Vec3{-1, 1, 0},
Vec3{-1, -1, 0}}
expected := []float64 {
HalfPi,
HalfPi,
Pi,
HalfPi + QuarterPi,
HalfPi + QuarterPi }
for i, tv := range(testV) {
r := v.angle(tv)
if !almostEqual64(expected[i], r, tol) {
t.Errorf("angle(%v, %v) = %g Pi, expected %g Pi", v, tv, r / Pi, expected[i] / Pi)
}
}
v := Vec3{1, 0, 0}
tol := 0.00005
testV := []Vec3{
Vec3{0, 1, 0},
Vec3{0, -1, 0},
Vec3{-1, 0, 0},
Vec3{-1, 1, 0},
Vec3{-1, -1, 0}}
expected := []float64{
HalfPi,
HalfPi,
Pi,
HalfPi + QuarterPi,
HalfPi + QuarterPi}
for i, tv := range testV {
r := v.angle(tv)
if !almostEqual64(expected[i], r, tol) {
t.Errorf("angle(%v, %v) = %g Pi, expected %g Pi", v, tv, r/Pi, expected[i]/Pi)
}
}
}

0 comments on commit bae65ab

Please sign in to comment.