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

added check for existing verticies to be reused

parent 817789c4
Loading
Loading
Loading
Loading
+36 −14
Original line number Diff line number Diff line
package icosahedron

import "math"
import (
	"math"
)

/*

@@ -123,6 +125,7 @@ func Subdivide(icoIn *Icosahedron) (*Icosahedron, error) {
	cv := len(vso)

	// iterate through all the faces
	//matches := 0
	for _, f := range icoIn.fs {
		ia, ib, ic := f[0], f[1], f[2]
		va, vb, vc := vso[ia : ia+1][0], vso[ib : ib+1][0], vso[ic : ic+1][0]
@@ -131,28 +134,47 @@ func Subdivide(icoIn *Icosahedron) (*Icosahedron, error) {
		v1, _ := calculateMidPoint(vb, vc, 0.5)
		v2, _ := calculateMidPoint(vc, va, 0.5)

		// TBD: new verticies do not have the correct distance to the center!!!

		// add new vertices
		vso = append(vso, v0)
		vso = append(vso, v1)
		vso = append(vso, v2)
		in := [3]int{-1, -1, -1}

		// do the new vertices already exist
		for vi, vn := range []Vertex{
			v0,
			v1,
			v2,
		} {
			im := -1
			match := false
			for vj, vo := range vso {
				if (vn.x == vo.x) && (vn.y == vo.y) && (vn.z == vo.z) {
					// found a match
					im = vj
					match = true
					//matches++
					break
				}
			}
			if match == true {
				in[vi] = im
			} else {
				in[vi] = cv + vi
				vso = append(vso, vn)
			}
		}

		i0 := cv + 0
		i1 := cv + 1
		i2 := cv + 2
		// TBD: new verticies do not have the correct distance to the center!!!

		// add new faces
		f0 := Face{ia, i0, i2}
		f1 := Face{i0, ib, i1}
		f2 := Face{i1, ic, i2}
		f3 := Face{i0, i1, i2}
		f0 := Face{ia, in[0], in[2]}
		f1 := Face{in[0], ib, in[1]}
		f2 := Face{in[1], ic, in[2]}
		f3 := Face{in[0], in[1], in[2]}

		fso = append(fso, f0)
		fso = append(fso, f1)
		fso = append(fso, f2)
		fso = append(fso, f3)
	}
	//fmt.Fprintf(os.Stderr, "* found matches: %d\n", matches)

	icoOut := Icosahedron{level: icoIn.level + 1, vs: vso, fs: fso}