Skip to content

Commit

Permalink
Simpler nodeSet implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
peterstace committed Nov 22, 2023
1 parent 0163a19 commit 29c694d
Showing 1 changed file with 12 additions and 29 deletions.
41 changes: 12 additions & 29 deletions geom/dcel_node_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,6 @@ import (
"math"
)

func newNodeSet(maxULPSize float64, sizeHint int) nodeSet {
// The appropriate multiplication factor to use to calculate bucket size is
// a bit of a guess.
bucketSize := maxULPSize * 0x200
return nodeSet{
bucketSize,
make(map[nodeBucket]XY, sizeHint),
}
}

// nodeSet is a set of XY values (nodes). If an XY value is inserted, but it is
// "close" to an existing XY in the set, then the original XY is returned (and
// the new XY _not_ inserted). The two XYs essentially merge together.
type nodeSet struct {
bucketWidth float64
nodes map[nodeBucket]XY
Expand All @@ -26,25 +13,21 @@ type nodeBucket struct {
x, y int
}

func newNodeSet(ulp float64, sizeHint int) nodeSet {
return nodeSet{
ulp * 0x1000,
make(map[nodeBucket]XY, sizeHint),
}
}

func (s nodeSet) insertOrGet(xy XY) XY {
half := 0.5 * s.bucketWidth
b := nodeBucket{
int(math.Floor(xy.X / s.bucketWidth)),
int(math.Floor(xy.Y / s.bucketWidth)),
x: int(math.Floor((xy.X + half) / s.bucketWidth)),
y: int(math.Floor((xy.Y + half) / s.bucketWidth)),
}
for _, offset := range [...]nodeBucket{
b,
{b.x - 1, b.y - 1},
{b.x - 1, b.y},
{b.x - 1, b.y + 1},
{b.x, b.y - 1},
{b.x, b.y + 1},
{b.x + 1, b.y - 1},
{b.x + 1, b.y},
{b.x + 1, b.y + 1},
} {
if node, ok := s.nodes[offset]; ok {
return node
}
if node, ok := s.nodes[b]; ok {
return node
}
s.nodes[b] = xy
return xy
Expand Down

0 comments on commit 29c694d

Please sign in to comment.