Skip to content

Commit

Permalink
Merge pull request spatial-go#24 from tianpf/main
Browse files Browse the repository at this point in the history
code refactoring
  • Loading branch information
tianpf committed Aug 19, 2021
2 parents ad35c8f + 3db0f46 commit 56d9943
Show file tree
Hide file tree
Showing 65 changed files with 1,549 additions and 330 deletions.
7 changes: 6 additions & 1 deletion algorithm/algoerr/algoerr.go → algorithm/algoerr.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package algoerr
// Package algorithm defines Specifies Computational Geometric and algorithm err.
// Specifies and implements various fundamental Computational Geometric algorithms.
package algorithm

import (
"fmt"
)

// ErrQuadrant ...
var ErrQuadrant = fmt.Errorf("Cannot compute the quadrant for point")

// ErrNilSteric ...
var ErrNilSteric = fmt.Errorf("Steric is nil")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package algoerr
package algorithm

import (
"testing"
Expand Down
17 changes: 10 additions & 7 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 @@ -147,7 +149,9 @@ func (eb *ComputerBuffer) addPolygon(p matrix.PolygonMatrix) {
calc.ImExterior)
}
}
func (eb *ComputerBuffer) addRingBothSides(ring matrix.LineMatrix, distance float64) {

// AddRingBothSides ...
func (eb *ComputerBuffer) AddRingBothSides(ring matrix.LineMatrix, distance float64) {
eb.addRingSide(ring, distance,
calc.SideLeft,
calc.ImExterior, calc.ImInterior)
Expand All @@ -160,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 @@ -171,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
7 changes: 3 additions & 4 deletions algorithm/buffer/convex_hull.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,13 @@ func (c *ConvexHullComputer) padArray3(pts []matrix.Matrix) []matrix.Matrix {
}

func (c *ConvexHullComputer) preSort(pts []matrix.Matrix) []matrix.Matrix {
t := matrix.Matrix{}

// find the lowest point in the set. If two or more points have
// the same minimum y matrix choose the one with the minimum x.
// This focal point is put in array location pts[0].
for i := 1; i < len(pts); i++ {
if (pts[i][1] < pts[0][1]) || ((pts[i][1] == pts[0][1]) && (pts[i][0] < pts[0][0])) {
t = pts[0]
t := pts[0]
pts[0] = pts[i]
pts[i] = t
}
Expand All @@ -141,14 +140,14 @@ func (c *ConvexHullComputer) preSort(pts []matrix.Matrix) []matrix.Matrix {

// grahamScan Uses the Graham Scan algorithm to compute the convex hull vertices.
func (c *ConvexHullComputer) grahamScan(ms []matrix.Matrix) *list.List {
p := matrix.Matrix{}

ps := list.New()
ps.PushBack(ms[0])
ps.PushBack(ms[1])
ps.PushBack(ms[2])
for i := 3; i < len(ms); i++ {
el := ps.Back()
p = el.Value.(matrix.Matrix)
p := el.Value.(matrix.Matrix)
ps.Remove(el)
// check for empty stack to guard against robustness problems
for ps.Len() != 0 && OrientationIndex(ps.Back().Value.(matrix.Matrix), p, ms[i]) > 0 {
Expand Down
4 changes: 2 additions & 2 deletions algorithm/buffer/curve_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (c *CurveBuilder) AddCurve(pts matrix.LineMatrix, leftLoc, rightLoc int) {
c.Curves = append(c.Curves, *curve)
}

// isRingCurveInverted Tests whether the offset curve for a ring is fully inverted.
// IsRingCurveInverted Tests whether the offset curve for a ring is fully inverted.
// An inverted ("inside-out") curve occurs in some specific situations
// involving a buffer distance which should result in a fully-eroded (empty) buffer.
// It can happen that the sides of a small, convex polygon
Expand All @@ -94,7 +94,7 @@ func (c *CurveBuilder) AddCurve(pts matrix.LineMatrix, leftLoc, rightLoc int) {
// It must be discarded from the set of offset curves used in the buffer.
// Heuristics are used to reduce the number of cases which area checked,
// for efficiency and correctness.
func (c *CurveBuilder) isRingCurveInverted(pts matrix.LineMatrix, distance float64) bool {
func (c *CurveBuilder) IsRingCurveInverted(pts matrix.LineMatrix, distance float64) bool {
if distance == 0.0 {
return false
}
Expand Down
2 changes: 1 addition & 1 deletion algorithm/buffer/interior.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func (ip *InteriorPointArea) findBestMidpoint(crossings []float64) {
return
}

sort.Sort(sort.Float64Slice(crossings))
sort.Float64sAreSorted(sort.Float64Slice(crossings))

// Entries in crossings list are expected to occur in pairs representing a
// section of the scan line interior to the polygon (which may be zero-length)
Expand Down
13 changes: 6 additions & 7 deletions algorithm/buffer/line_simplifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ const (
// LineSimplifier Simplifies a buffer input line to
// remove concavities with shallow depth.
type LineSimplifier struct {
inputLine matrix.LineMatrix
distanceTol float64
isDeleted []byte
angleOrientation int
leftLoc, rightLoc int
parameters *CurveParameters
inputLine matrix.LineMatrix
distanceTol float64
isDeleted []byte
angleOrientation int
}

// Simplify the input coordinate list.
Expand Down Expand Up @@ -114,7 +112,8 @@ func (l *LineSimplifier) isDeletable(i0, i1, i2 int, distanceTol float64) bool {
return l.isShallowSampled(p0, p1, i0, i2, distanceTol)
}

func (l *LineSimplifier) isShallowConcavity(p0, p1, p2 matrix.Matrix, distanceTol float64) bool {
// IsShallowConcavity ...
func (l *LineSimplifier) IsShallowConcavity(p0, p1, p2 matrix.Matrix, distanceTol float64) bool {
orientation := l.orientationIndex(p0[0], p0[1], p1[0], p1[1], p2[0], p2[1])
isAngleToSimplify := (orientation == l.angleOrientation)
if !isAngleToSimplify {
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
4 changes: 2 additions & 2 deletions algorithm/buffer/simplify/topology_preserving_simplifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ func (t *TopologyPreservingSimplifier) getResultGeometry() matrix.Steric {
(&LineStringMapBuilderFilter{t}).filter(t.InputGeom)

t.lineSimplifier.Simplify(t.linestrings)
var tr Trans
tr = &LineStringTransformer{linestrings: t.linestrings}

tr := &LineStringTransformer{linestrings: t.linestrings}
result, _ := tr.Transform(t.InputGeom)
return result
}
Expand Down
14 changes: 7 additions & 7 deletions algorithm/buffer/simplify/transformer.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package simplify

import (
"github.com/spatial-go/geoos/algorithm/algoerr"
"github.com/spatial-go/geoos/algorithm"
"github.com/spatial-go/geoos/algorithm/buffer"
"github.com/spatial-go/geoos/algorithm/matrix"
)
Expand All @@ -23,8 +23,8 @@ type Trans interface {
// Transformer A framework for processes which transform an input Geometry into
// an output Geometry, possibly changing its structure and type(s).
type Transformer struct {
InputGeom matrix.Steric
pruneEmptyGeometry, preserveGeometryCollectionType, preserveCollections, preserveType bool
InputGeom matrix.Steric
pruneEmptyGeometry, preserveType bool
}

// Transform ...
Expand All @@ -40,7 +40,7 @@ func (t *Transformer) Transform(inputGeom matrix.Steric) (matrix.Steric, error)
case matrix.Collection:
return t.transformCollection(m, nil), nil
default:
return nil, algoerr.ErrUnknownType(m)
return nil, algorithm.ErrUnknownType(m)
}
}

Expand Down Expand Up @@ -136,7 +136,7 @@ func (l *LineStringTransformer) Transform(inputGeom matrix.Steric) (matrix.Steri
case matrix.Collection:
return l.transformCollection(m, inputGeom), nil
default:
return nil, algoerr.ErrUnknownType(m)
return nil, algorithm.ErrUnknownType(m)
}
}

Expand All @@ -155,7 +155,7 @@ func (l *LineStringTransformer) findLineString(parent matrix.LineMatrix) (*Tagge
return v, nil
}
}
return nil, algoerr.ErrNotInSlice
return nil, algorithm.ErrNotInSlice
}

func (l *LineStringTransformer) transformCoordinates(pts []matrix.Matrix, parent matrix.Steric) []matrix.Matrix {
Expand Down Expand Up @@ -193,7 +193,7 @@ func (d *DPTransformer) Transform(inputGeom matrix.Steric) (matrix.Steric, error
case matrix.Collection:
return d.transformCollection(m, nil), nil
default:
return nil, algoerr.ErrUnknownType(m)
return nil, algorithm.ErrUnknownType(m)
}
}

Expand Down
5 changes: 3 additions & 2 deletions algorithm/calc/angle/angle.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package angle define angel calculation function.
package angle

import (
Expand Down Expand Up @@ -61,9 +62,9 @@ func IsAcute(p0, p1, p2 matrix.Matrix) bool {
return dotprod > 0
}

// isObtuse Tests whether the angle between p0-p1-p2 is obtuse.
// IsObtuse Tests whether the angle between p0-p1-p2 is obtuse.
// An angle is obtuse if it is greater than 90 degrees.
func isObtuse(p0, p1, p2 matrix.Matrix) bool {
func IsObtuse(p0, p1, p2 matrix.Matrix) bool {
// relies on fact that A dot B is negative if A ang B is obtuse
dx0 := p0[0] - p1[0]
dy0 := p0[1] - p1[1]
Expand Down
49 changes: 49 additions & 0 deletions algorithm/calc/angle/quadrant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package angle

import (
"github.com/spatial-go/geoos/algorithm"
"github.com/spatial-go/geoos/algorithm/matrix"
)

// Quadrant four const
const (
NE = iota + 1
NW
SW
SE
)

// QuadrantFloat Returns the quadrant of a directed line segment (specified as x and y displacements, which cannot both be 0).
func QuadrantFloat(dx, dy float64) (int, error) {
if dx == 0.0 && dy == 0.0 {
return 0, algorithm.ErrQuadrant
}
if dx >= 0.0 {
if dy >= 0.0 {
return NE, nil
}

return SE, nil
}
if dy >= 0.0 {
return NW, nil
}
return SW, nil
}

// Quadrant Returns the quadrant of a directed line segment from p0 to p1.
func Quadrant(p0, p1 matrix.Matrix) (int, error) {
if p1[0] == p0[0] && p1[1] == p0[1] {
return 0, algorithm.ErrQuadrant
}
if p1[0] >= p0[0] {
if p1[1] >= p0[1] {
return NE, nil
}
return SE, nil
}
if p1[1] >= p0[1] {
return NW, nil
}
return SW, nil
}
1 change: 1 addition & 0 deletions algorithm/filter/filter.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package filter Define matrix data filter function.
package filter

import (
Expand Down
8 changes: 4 additions & 4 deletions algorithm/matrix/intersection_matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package matrix
import (
"bytes"

"github.com/spatial-go/geoos/algorithm/algoerr"
"github.com/spatial-go/geoos/algorithm"
"github.com/spatial-go/geoos/algorithm/calc"
)

Expand Down Expand Up @@ -178,7 +178,7 @@ func (im *IntersectionMatrix) IsOverlaps(dimensionOfGeometryA, dimensionOfGeomet
// Matches Tests whether this matrix matches the given matrix pattern.
func (im *IntersectionMatrix) Matches(pattern string) (bool, error) {
if len(pattern) != 9 {
return false, algoerr.ErrorShouldBeLength9(pattern)
return false, algorithm.ErrorShouldBeLength9(pattern)
}
for ai := 0; ai < 3; ai++ {
for bi := 0; bi < 3; bi++ {
Expand Down Expand Up @@ -270,7 +270,7 @@ func toDimensionSymbol(dimensionValue int) (byte, error) {
case calc.ImA:
return calc.ImSymA, nil
default:
return byte('_'), algoerr.ErrorUnknownDimension(dimensionValue)
return byte('_'), algorithm.ErrorUnknownDimension(dimensionValue)
}
}

Expand All @@ -290,7 +290,7 @@ func toDimensionValue(dimensionSymbol byte) (int, error) {
case calc.ImSymA:
return calc.ImA, nil
default:
return -1, algoerr.ErrorUnknownDimension(dimensionSymbol)
return -1, algorithm.ErrorUnknownDimension(dimensionSymbol)
}
}

Expand Down
4 changes: 2 additions & 2 deletions algorithm/matrix/line_segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package matrix
import (
"math"

"github.com/spatial-go/geoos/algorithm/algoerr"
"github.com/spatial-go/geoos/algorithm"
)

// LineSegment is line.
Expand Down Expand Up @@ -45,7 +45,7 @@ func (l *LineSegment) PointAlongOffset(segmentLengthFraction, offsetDistance flo
uy := 0.0
if offsetDistance != 0.0 {
if lenXY <= 0.0 {
return nil, algoerr.ErrComputeOffsetZero
return nil, algorithm.ErrComputeOffsetZero
}

// u is the vector that is the length of the offset, in the direction of the segment
Expand Down
Loading

0 comments on commit 56d9943

Please sign in to comment.