...

Source file src/github.com/golang/geo/s2/shapeutil_edge_iterator.go

Documentation: github.com/golang/geo/s2

     1  // Copyright 2020 Google Inc. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package s2
    16  
    17  // EdgeIterator is an iterator that advances through all edges in an ShapeIndex.
    18  // This is different to the ShapeIndexIterator, which advances through the cells in the
    19  // ShapeIndex.
    20  type EdgeIterator struct {
    21  	index    *ShapeIndex
    22  	shapeID  int32
    23  	numEdges int32
    24  	edgeID   int32
    25  }
    26  
    27  // NewEdgeIterator creates a new edge iterator for the given index.
    28  func NewEdgeIterator(index *ShapeIndex) *EdgeIterator {
    29  	e := &EdgeIterator{
    30  		index:   index,
    31  		shapeID: -1,
    32  		edgeID:  -1,
    33  	}
    34  
    35  	e.Next()
    36  	return e
    37  }
    38  
    39  // ShapeID returns the current shape ID.
    40  func (e *EdgeIterator) ShapeID() int32 { return e.shapeID }
    41  
    42  // EdgeID returns the current edge ID.
    43  func (e *EdgeIterator) EdgeID() int32 { return e.edgeID }
    44  
    45  // ShapeEdgeID returns the current (shapeID, edgeID).
    46  func (e *EdgeIterator) ShapeEdgeID() ShapeEdgeID { return ShapeEdgeID{e.shapeID, e.edgeID} }
    47  
    48  // Edge returns the current edge.
    49  func (e *EdgeIterator) Edge() Edge {
    50  	return e.index.Shape(e.shapeID).Edge(int(e.edgeID))
    51  }
    52  
    53  // Done reports if the iterator is positioned at or after the last index edge.
    54  func (e *EdgeIterator) Done() bool { return e.shapeID >= int32(len(e.index.shapes)) }
    55  
    56  // Next positions the iterator at the next index edge.
    57  func (e *EdgeIterator) Next() {
    58  	e.edgeID++
    59  	for ; e.edgeID >= e.numEdges; e.edgeID++ {
    60  		e.shapeID++
    61  		if e.shapeID >= int32(len(e.index.shapes)) {
    62  			break
    63  		}
    64  		shape := e.index.Shape(e.shapeID)
    65  		if shape == nil {
    66  			e.numEdges = 0
    67  		} else {
    68  			e.numEdges = int32(shape.NumEdges())
    69  		}
    70  		e.edgeID = -1
    71  	}
    72  }
    73  

View as plain text