Skip to content

Commit

Permalink
Fix buffer bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
tianpf committed Aug 13, 2021
1 parent 8d15638 commit 44936d3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
13 changes: 7 additions & 6 deletions algorithm/buffer/buffer.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package buffer define geomtry matrix conversion.
package buffer

import (
Expand Down Expand Up @@ -107,6 +108,7 @@ func (eb *ComputerBuffer) isLineOffsetEmpty(distance float64) bool {
}
func (eb *ComputerBuffer) addPolygon(p matrix.PolygonMatrix) {
offsetDistance := eb.distance

offsetSide := calc.SideLeft
if eb.distance < 0.0 {
offsetDistance = -eb.distance
Expand Down Expand Up @@ -162,8 +164,7 @@ func (eb *ComputerBuffer) AddRingBothSides(ring matrix.LineMatrix, distance floa
// addRingSide Adds an offset curve for one side of a ring.
// The side and left and right topological location arguments
// are provided as if the ring is oriented CW.
// (If the ring is in the opposite orientation,
// this is detected and
// (If the ring is in the opposite orientation,this is detected and
// the left and right locations are interchanged and the side is flipped.)
func (eb *ComputerBuffer) addRingSide(ring matrix.LineMatrix, offsetDistance float64, side, cwLeftLoc, cwRightLoc int) {
// don't bother adding ring if it is "flat" and will disappear in the output
Expand All @@ -173,17 +174,17 @@ func (eb *ComputerBuffer) addRingSide(ring matrix.LineMatrix, offsetDistance flo

leftLoc := cwLeftLoc
rightLoc := cwRightLoc

// add test ccw
isCCW := measure.IsCCW(matrix.LineMatrix(ring))
if len(ring) >= calc.MinRingSize && isCCW {
if len(ring) >= calc.MinRingSize && !isCCW {
leftLoc = cwRightLoc
rightLoc = cwLeftLoc
if side == calc.SideLeft {
side = calc.SideRight
}
if side == calc.SideRight {
} else if side == calc.SideRight {
side = calc.SideLeft
}

}
eb.RingCurve(matrix.LineMatrix(ring), offsetDistance, side, leftLoc, rightLoc)

Expand Down
13 changes: 12 additions & 1 deletion algorithm/buffer/buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,21 @@ func TestBuffer(t *testing.T) {
{122.99325784324384, 41.118723147333654}},
},
},
{name: "poly buffer", args: args{
geom: matrix.PolygonMatrix{{{100, 100}, {200, 100}, {200, 200}, {100, 200}, {100, 100}}},
distance: 50,
quadsegs: 4,
}, want: matrix.PolygonMatrix{
{{50, 100}, {53.80602337443563, 80.86582838174559}, {64.64466094067251, 64.64466094067274}, {80.86582838174527, 53.80602337443576}, {100, 50}, {200, 50},
{219.1341716182545, 53.80602337443566}, {235.35533905932738, 64.64466094067262}, {246.19397662556435, 80.86582838174552}, {250, 100}, {250, 200},
{246.19397662556435, 219.1341716182545}, {235.35533905932738, 235.35533905932738}, {219.1341716182545, 246.19397662556435}, {200, 250}, {100, 250},
{80.86582838174552, 246.19397662556435}, {64.64466094067262, 235.35533905932738}, {53.80602337443566, 219.1341716182545}, {50, 200}, {50, 100}},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Buffer(tt.args.geom, tt.args.distance, tt.args.quadsegs); !got.EqualsExact(tt.want, 0.000001) {
if got := Buffer(tt.args.geom, tt.args.distance, tt.args.quadsegs); got == nil || !got.EqualsExact(tt.want, 0.000001) {
t.Errorf("Buffer() = %v,\n want %v", got, tt.want)
}
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package simplify define Douglas Peucker and Topology Preserving simplify.
package simplify

import (
Expand Down

0 comments on commit 44936d3

Please sign in to comment.