Skip to content

Commit

Permalink
Remove final traces of ConstructorOption
Browse files Browse the repository at this point in the history
  • Loading branch information
peterstace committed Sep 6, 2023
1 parent 86faacd commit 5f6b8ab
Show file tree
Hide file tree
Showing 15 changed files with 45 additions and 152 deletions.
39 changes: 0 additions & 39 deletions geom/ctor_options.go

This file was deleted.

5 changes: 1 addition & 4 deletions geom/dcel.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package geom

func newDCELFromGeometries(a, b Geometry) (*doublyConnectedEdgeList, error) {
ghosts := createGhosts(a, b)
a, b, ghosts, err := reNodeGeometries(a, b, ghosts)
if err != nil {
return nil, wrap(err, "re-noding")
}
a, b, ghosts = reNodeGeometries(a, b, ghosts)

interactions := findInteractionPoints([]Geometry{a, b, ghosts.AsGeometry()})

Expand Down
95 changes: 27 additions & 68 deletions geom/dcel_re_noding.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func ulpSizeForLine(ln line) float64 {
// such that when the two geometries are overlaid the only interactions
// (including self-interactions) between geometries are at nodes. Nodes that
// are close to each other are also snapped together.
func reNodeGeometries(g1, g2 Geometry, mls MultiLineString) (Geometry, Geometry, MultiLineString, error) {
func reNodeGeometries(g1, g2 Geometry, mls MultiLineString) (Geometry, Geometry, MultiLineString) {
// Calculate the maximum ULP size over all control points in the input
// geometries. This size is a good indication of the precision that we
// should use when node merging.
Expand All @@ -83,44 +83,29 @@ func reNodeGeometries(g1, g2 Geometry, mls MultiLineString) (Geometry, Geometry,
mls = mls.TransformXYWithoutValidation(nodes.insertOrGet)

// Create additional nodes for crossings.
var err error
cut := newCutSet(all)
g1, err = reNodeGeometry(g1, cut, nodes)
if err != nil {
return Geometry{}, Geometry{}, MultiLineString{}, err
}
g2, err = reNodeGeometry(g2, cut, nodes)
if err != nil {
return Geometry{}, Geometry{}, MultiLineString{}, err
}
mls, err = reNodeMultiLineString(mls, cut, nodes)
if err != nil {
return Geometry{}, Geometry{}, MultiLineString{}, err
}
return g1, g2, mls, nil
g1 = reNodeGeometry(g1, cut, nodes)
g2 = reNodeGeometry(g2, cut, nodes)
mls = reNodeMultiLineString(mls, cut, nodes)
return g1, g2, mls
}

// reNodeGeometry re-nodes a single geometry, using a common cut set and node
// map. The cut set is already noded.
func reNodeGeometry(g Geometry, cut cutSet, nodes nodeSet) (Geometry, error) {
func reNodeGeometry(g Geometry, cut cutSet, nodes nodeSet) Geometry {
switch g.Type() {
case TypeGeometryCollection:
gc, err := reNodeGeometryCollection(g.MustAsGeometryCollection(), cut, nodes)
return gc.AsGeometry(), err
return reNodeGeometryCollection(g.MustAsGeometryCollection(), cut, nodes).AsGeometry()
case TypeLineString:
ls, err := reNodeLineString(g.MustAsLineString(), cut, nodes)
return ls.AsGeometry(), err
return reNodeLineString(g.MustAsLineString(), cut, nodes).AsGeometry()
case TypePolygon:
poly, err := reNodePolygon(g.MustAsPolygon(), cut, nodes)
return poly.AsGeometry(), err
return reNodePolygon(g.MustAsPolygon(), cut, nodes).AsGeometry()
case TypeMultiLineString:
mls, err := reNodeMultiLineString(g.MustAsMultiLineString(), cut, nodes)
return mls.AsGeometry(), err
return reNodeMultiLineString(g.MustAsMultiLineString(), cut, nodes).AsGeometry()
case TypeMultiPolygon:
mp, err := reNodeMultiPolygonString(g.MustAsMultiPolygon(), cut, nodes)
return mp.AsGeometry(), err
return reNodeMultiPolygonString(g.MustAsMultiPolygon(), cut, nodes).AsGeometry()
case TypePoint, TypeMultiPoint:
return g, nil
return g
default:
panic(fmt.Sprintf("unknown geometry type %v", g.Type()))
}
Expand Down Expand Up @@ -197,7 +182,7 @@ func appendPoints(points []XY, g Geometry) []XY {
return points
}

func reNodeLineString(ls LineString, cut cutSet, nodes nodeSet) (LineString, error) {
func reNodeLineString(ls LineString, cut cutSet, nodes nodeSet) LineString {
var newCoords []float64
seq := ls.Coordinates()
n := seq.Length()
Expand Down Expand Up @@ -245,69 +230,43 @@ func reNodeLineString(ls LineString, cut cutSet, nodes nodeSet) (LineString, err
newCoords = append(newCoords, last.X, last.Y)
}

newLS, err := NewLineString(NewSequence(newCoords, DimXY), DisableAllValidations)
if err != nil {
return LineString{}, err
}
return newLS, nil
return NewLineStringWithoutValidation(NewSequence(newCoords, DimXY))
}

func reNodeMultiLineString(mls MultiLineString, cut cutSet, nodes nodeSet) (MultiLineString, error) {
func reNodeMultiLineString(mls MultiLineString, cut cutSet, nodes nodeSet) MultiLineString {
n := mls.NumLineStrings()
lss := make([]LineString, n)
for i := 0; i < n; i++ {
var err error
lss[i], err = reNodeLineString(mls.LineStringN(i), cut, nodes)
if err != nil {
return MultiLineString{}, err
}
lss[i] = reNodeLineString(mls.LineStringN(i), cut, nodes)
}
return NewMultiLineString(lss, DisableAllValidations), nil
return NewMultiLineString(lss)
}

func reNodePolygon(poly Polygon, cut cutSet, nodes nodeSet) (Polygon, error) {
reNodedBoundary, err := reNodeMultiLineString(poly.Boundary(), cut, nodes)
if err != nil {
return Polygon{}, err
}
func reNodePolygon(poly Polygon, cut cutSet, nodes nodeSet) Polygon {
reNodedBoundary := reNodeMultiLineString(poly.Boundary(), cut, nodes)
n := reNodedBoundary.NumLineStrings()
rings := make([]LineString, n)
for i := 0; i < n; i++ {
rings[i] = reNodedBoundary.LineStringN(i)
}
reNodedPoly, err := NewPolygon(rings, DisableAllValidations)
if err != nil {
return Polygon{}, err
}
return reNodedPoly, nil
reNodedPoly := NewPolygonWithoutValidation(rings)
return reNodedPoly
}

func reNodeMultiPolygonString(mp MultiPolygon, cut cutSet, nodes nodeSet) (MultiPolygon, error) {
func reNodeMultiPolygonString(mp MultiPolygon, cut cutSet, nodes nodeSet) MultiPolygon {
n := mp.NumPolygons()
polys := make([]Polygon, n)
for i := 0; i < n; i++ {
var err error
polys[i], err = reNodePolygon(mp.PolygonN(i), cut, nodes)
if err != nil {
return MultiPolygon{}, err
}
polys[i] = reNodePolygon(mp.PolygonN(i), cut, nodes)
}
reNodedMP, err := NewMultiPolygon(polys, DisableAllValidations)
if err != nil {
return MultiPolygon{}, err
}
return reNodedMP, nil
return NewMultiPolygonWithoutValidation(polys)
}

func reNodeGeometryCollection(gc GeometryCollection, cut cutSet, nodes nodeSet) (GeometryCollection, error) {
func reNodeGeometryCollection(gc GeometryCollection, cut cutSet, nodes nodeSet) GeometryCollection {
n := gc.NumGeometries()
geoms := make([]Geometry, n)
for i := 0; i < n; i++ {
var err error
geoms[i], err = reNodeGeometry(gc.GeometryN(i), cut, nodes)
if err != nil {
return GeometryCollection{}, err
}
geoms[i] = reNodeGeometry(gc.GeometryN(i), cut, nodes)
}
return NewGeometryCollection(geoms, DisableAllValidations), nil
return NewGeometryCollection(geoms)
}
5 changes: 1 addition & 4 deletions geom/dcel_re_noding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,7 @@ func TestReNode(t *testing.T) {
if err != nil {
t.Fatal(err)
}
gotA, gotB, _, err := reNodeGeometries(inA, inB, MultiLineString{})
if err != nil {
t.Fatal(err)
}
gotA, gotB, _ := reNodeGeometries(inA, inB, MultiLineString{})
if !ExactEquals(gotA, wantA) || !ExactEquals(gotB, wantB) {
t.Logf("INPUT A: %v\n", inA.AsText())
t.Logf("INPUT B: %v\n", inB.AsText())
Expand Down
4 changes: 2 additions & 2 deletions geom/perf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ func regularPolygon(center XY, radius float64, sides int) Polygon {
}
coords[2*sides+0] = coords[0]
coords[2*sides+1] = coords[1]
ring, err := NewLineString(NewSequence(coords, DimXY), geom.DisableAllValidations)
ring, err := NewLineString(NewSequence(coords, DimXY))
if err != nil {
panic(err)
}
poly, err := NewPolygon([]LineString{ring}, geom.DisableAllValidations)
poly, err := NewPolygon([]LineString{ring})
if err != nil {
panic(err)
}
Expand Down
6 changes: 1 addition & 5 deletions geom/type_envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,7 @@ func (e Envelope) BoundingDiagonal() Geometry {
// when constructing the diagonal.
coords := []float64{e.minX(), e.minY, e.maxX, e.maxY}
seq := NewSequence(coords, DimXY)
ls, err := NewLineString(seq, DisableAllValidations)
if err != nil {
panic("programming error: validations disabled by LineString validation failed")
}
return ls.AsGeometry()
return NewLineStringWithoutValidation(seq).AsGeometry()
}

// String implements the fmt.Stringer interface by printing the envelope in a
Expand Down
2 changes: 1 addition & 1 deletion geom/type_geometry_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type GeometryCollection struct {
//
// Note that this constructor doesn't check the validity of its Polygon
// arguments.
func NewGeometryCollection(geoms []Geometry, opts ...ConstructorOption) GeometryCollection {
func NewGeometryCollection(geoms []Geometry) GeometryCollection {
if len(geoms) == 0 {
return GeometryCollection{}
}
Expand Down
5 changes: 1 addition & 4 deletions geom/type_line_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@ type LineString struct {
// NewLineString creates a new LineString from a Sequence of points. An error
// is returned if the LineString would be invalid (see the Validate method for
// details).
func NewLineString(seq Sequence, opts ...ConstructorOption) (LineString, error) {
func NewLineString(seq Sequence) (LineString, error) {
ls := NewLineStringWithoutValidation(seq)
if newOptionSet(opts).skipValidations {
return ls, nil
}
if err := ls.Validate(); err != nil {
return LineString{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion geom/type_multi_line_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type MultiLineString struct {
//
// Note that this constructor doesn't check the validity of its LineString
// arguments.
func NewMultiLineString(lines []LineString, opts ...ConstructorOption) MultiLineString {
func NewMultiLineString(lines []LineString) MultiLineString {
if len(lines) == 0 {
return MultiLineString{}
}
Expand Down
2 changes: 1 addition & 1 deletion geom/type_multi_point.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type MultiPoint struct {
//
// Note that this constructor doesn't check the validity of its Point
// arguments.
func NewMultiPoint(pts []Point, opts ...ConstructorOption) MultiPoint {
func NewMultiPoint(pts []Point) MultiPoint {
if len(pts) == 0 {
return MultiPoint{}
}
Expand Down
5 changes: 1 addition & 4 deletions geom/type_multi_polygon.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,8 @@ type MultiPolygon struct {
//
// Note that this constructor doesn't check the validity of its Polygon
// arguments.
func NewMultiPolygon(polys []Polygon, opts ...ConstructorOption) (MultiPolygon, error) {
func NewMultiPolygon(polys []Polygon) (MultiPolygon, error) {
mp := NewMultiPolygonWithoutValidation(polys)
if newOptionSet(opts).skipValidations {
return mp, nil
}
if err := mp.checkMultiPolygonConstraints(); err != nil {
return MultiPolygon{}, err
}
Expand Down
6 changes: 1 addition & 5 deletions geom/type_point.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,8 @@ type Point struct {

// NewPoint creates a new point given its Coordinates. An error is returned for
// invalid points (see the Validate method for details).
func NewPoint(c Coordinates, opts ...ConstructorOption) (Point, error) {
func NewPoint(c Coordinates) (Point, error) {
pt := NewPointWithoutValidation(c)
os := newOptionSet(opts)
if os.skipValidations {
return pt, nil
}
if err := pt.Validate(); err != nil {
return Point{}, err
}
Expand Down
5 changes: 1 addition & 4 deletions geom/type_polygon.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,8 @@ type Polygon struct {
//
// An error is returned if any of the Polygon constraints are not met (see the
// Validate method for details).
func NewPolygon(rings []LineString, opts ...ConstructorOption) (Polygon, error) {
func NewPolygon(rings []LineString) (Polygon, error) {
poly := NewPolygonWithoutValidation(rings)
if newOptionSet(opts).skipValidations {
return poly, nil
}
if err := poly.Validate(); err != nil {
return Polygon{}, err
}
Expand Down
12 changes: 4 additions & 8 deletions geom/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ func TestPointValidation(t *testing.T) {
}
})
t.Run("Validate", func(t *testing.T) {
pt, err := NewPoint(tc.input, DisableAllValidations)
expectNoErr(t, err)
pt := NewPointWithoutValidation(tc.input)
expectBoolEq(t, tc.wantValid, pt.Validate() == nil)
})
})
Expand Down Expand Up @@ -75,8 +74,7 @@ func TestLineStringValidation(t *testing.T) {
expectBoolEq(t, tc.wantValid, err == nil)
})
t.Run("Validate", func(t *testing.T) {
ls, err := NewLineString(seq, DisableAllValidations)
expectNoErr(t, err)
ls := NewLineStringWithoutValidation(seq)
expectBoolEq(t, tc.wantValid, ls.Validate() == nil)
})
})
Expand Down Expand Up @@ -183,8 +181,7 @@ func TestMultiPointValidation(t *testing.T) {
t.Run(strconv.Itoa(i), func(t *testing.T) {
var pts []Point
for _, c := range tc.coords {
pt, err := NewPoint(c, DisableAllValidations)
expectNoErr(t, err)
pt := NewPointWithoutValidation(c)
pts = append(pts, pt)
}
mp := NewMultiPoint(pts)
Expand All @@ -208,8 +205,7 @@ func TestMultiLineStringValidation(t *testing.T) {
var lss []LineString
for _, coords := range tc.coords {
seq := NewSequence(coords, DimXY)
ls, err := NewLineString(seq, DisableAllValidations)
expectNoErr(t, err)
ls := NewLineStringWithoutValidation(seq)
lss = append(lss, ls)
}
mls := NewMultiLineString(lss)
Expand Down
4 changes: 2 additions & 2 deletions internal/perf/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ func regularPolygon(center geom.XY, radius float64, sides int) geom.Polygon {
}
coords[2*sides+0] = coords[0]
coords[2*sides+1] = coords[1]
ring, err := geom.NewLineString(geom.NewSequence(coords, geom.DimXY), geom.DisableAllValidations)
ring, err := geom.NewLineString(geom.NewSequence(coords, geom.DimXY))
if err != nil {
panic(err)
}
poly, err := geom.NewPolygon([]geom.LineString{ring}, geom.DisableAllValidations)
poly, err := geom.NewPolygon([]geom.LineString{ring})
if err != nil {
panic(err)
}
Expand Down

0 comments on commit 5f6b8ab

Please sign in to comment.