From bae65abe2cdc9ef28157e4d057bdbe7e468c1ca0 Mon Sep 17 00:00:00 2001 From: Hagen Schendel Date: Sat, 26 Aug 2017 16:16:05 +0200 Subject: [PATCH] go fmt --- float.go | 4 ++-- readwrite_test.go | 38 ++++++++++++++++++-------------------- solid_test.go | 4 ++-- triangle.go | 6 +++--- vec3.go | 34 +++++++++++++++++----------------- vec3_test.go | 42 +++++++++++++++++++++--------------------- 6 files changed, 63 insertions(+), 65 deletions(-) diff --git a/float.go b/float.go index 5fdc62d..45865a7 100644 --- a/float.go +++ b/float.go @@ -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 { diff --git a/readwrite_test.go b/readwrite_test.go index a395515..402102f 100644 --- a/readwrite_test.go +++ b/readwrite_test.go @@ -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) @@ -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) @@ -237,10 +237,9 @@ 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) @@ -248,12 +247,12 @@ func BenchmarkWriteMediumFile_Binary(b *testing.B) { 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 @@ -265,4 +264,3 @@ func BenchmarkWriteMediumFile_Binary(b *testing.B) { } } } - diff --git a/solid_test.go b/solid_test.go index 072c8f9..ea2a5fa 100644 --- a/solid_test.go +++ b/solid_test.go @@ -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") } diff --git a/triangle.go b/triangle.go index 0946575..883032c 100644 --- a/triangle.go +++ b/triangle.go @@ -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])). @@ -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 } diff --git a/vec3.go b/vec3.go index 924c522..b2d6556 100644 --- a/vec3.go +++ b/vec3.go @@ -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) -} \ No newline at end of file + 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) +} diff --git a/vec3_test.go b/vec3_test.go index 5e10c8d..104a72c 100644 --- a/vec3_test.go +++ b/vec3_test.go @@ -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) + } + } }