I'm trying to write a threaded Decasteljau algorithm for control polygons with any set of points in golang but can't get the goroutines to work right because their work randomly and i can't manage to get all the goroutines to work .
here's my code for the decasteljau.go
file :
package main
import (
"fmt"
)
type ControlPolygon struct {
Vertices []Vertex
}
type Spline struct {
Vertices map[int]Vertex
}
type splinePoint struct {
index int
vertex Vertex
}
func (controlPolygon ControlPolygon) Decasteljau(levelOfDetail int) {
//
// LevelOfDetail is the number of points in the spline
//
spline := Spline{make(map[int]Vertex)}
splinePointsChannel := make(chan splinePoint)
for index := 1; index < levelOfDetail; index++ {
splinePoint := splinePoint{}
splinePoint.index = index
pointPosition := float64(index) / float64(levelOfDetail)
go func() {
fmt.Println("goroutine number:", index)
splinePoint.findSplinePoint(controlPolygon.Vertices, pointPosition)
splinePointsChannel <- splinePoint
}()
}
point := <-splinePointsChannel
spline.Vertices[point.index] = point.vertex
fmt.Println(spline)
}
func (point *splinePoint) findSplinePoint(vertices []Vertex, pointPosition float64) {
var interpolationPoints []Vertex
if len(vertices) == 1 {
fmt.Println("vertices : ", vertices)
point.vertex = vertices[0]
}
if len(vertices) > 1 {
for i := 0; i < len(vertices)-1; i++ {
interpolationPoint := vertices[i].GetInterpolationPoint(vertices[i+1], pointPosition)
interpolationPoints = append(interpolationPoints, interpolationPoint)
}
point.findSplinePoint(interpolationPoints, pointPosition)
fmt.Println()
} else {
fmt.Println("Done Detailing ..")
return
}
}
func main() {
v1 := Vertex{0, 0, 0}
v2 := Vertex{0, 0, 1}
v3 := Vertex{0, 1, 1}
v4 := Vertex{0, 1, 0}
vectices := []Vertex{v1, v2, v3, v4}
controlPolygon := ControlPolygon{vectices}
controlPolygon.Decasteljau(10)
}
I'm also new to go concurrency and after a lot of research i'm still wondering if i need to use buffered or unbuffered channels for my case . I also found that goroutines are mostly used for managing networks rather than optimizing 3D so i would love to know if i'm using a good stack for writing concurrent 3D algorithms