Commit 7b6a0f1d authored by Boris Mühmer's avatar Boris Mühmer
Browse files

added ScaleTo() to alter vertices postion located on sphere

parent 522e650c
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -174,3 +174,13 @@ func Subdivide(icoIn *Icosahedron) (*Icosahedron, error) {

	return &icoOut, nil
}

// ScaleTo aligns the vertices on a sphere with radius r.
func (i *Icosahedron) ScaleTo(r float32) error {
	for ni, vi := range i.Vertices() {
		d := vi.Len()
		s := r / d
		i.vs[ni] = vi.Mul(s)
	}
	return nil
}
+54 −0
Original line number Diff line number Diff line
@@ -34,3 +34,57 @@ func TestIcosahedronSubdivide(t *testing.T) {
		t.Logf("Icosahedron: subdivision %d, %d vertices, %d faces", ico.Level(), cv, cf)
	}
}

func abs(x float32) float32 {
	if x < 0 {
		return -x
	}
	return x
}

func TestIcosahedronVarious(t *testing.T) {
	ico, err := Create()
	if err != nil {
		t.Errorf("Create() failed with error: %v", err)
	}
	var ds [numberOfVertices]float32
	for i, v := range ico.Vertices() {
		ds[i] = v.Len()
	}
	for i, d := range ds {
		t.Logf("v[%d].Len() = %f", i, d)
	}
	log := false
	for _, r0 := range []float32{
		1.0,
		2.0,
		3.0,
		50.0,
		123.0,
		123.4,
		1234567.0,
		1234567.8,
		1234567.89,
		1234567.891,
		1234567.89123456789,
		//12345678.0,
		//12345678.9,
		//123456789,
	} {
		if err := ico.ScaleTo(r0); err != nil {
			t.Errorf("ScaleTo(%f) failed with error: %v", r0, err)
		}
		for i, v := range ico.Vertices() {
			ds[i] = v.Len()
			dx := abs(ds[i] - r0)
			if dx > 0.000001 {
				t.Errorf("ds[%d] = %f, expected %f (diff: %f)", i, ds[i], r0, dx)
			}
		}
		if log {
			for i, d := range ds {
				t.Logf("%f: v[%d].Len() = %f", r0, i, d)
			}
		}
	}
}