...

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

Documentation: github.com/golang/geo/s2

     1  // Copyright 2023 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  // Shape interface enforcement
    18  var _ Shape = (*LaxLoop)(nil)
    19  
    20  // LaxLoop represents a closed loop of edges surrounding an interior
    21  // region. It is similar to Loop except that this class allows
    22  // duplicate vertices and edges. Loops may have any number of vertices,
    23  // including 0, 1, or 2. (A one-vertex loop defines a degenerate edge
    24  // consisting of a single point.)
    25  //
    26  // Note that LaxLoop is faster to initialize and more compact than
    27  // Loop, but does not support the same operations as Loop.
    28  type LaxLoop struct {
    29  	numVertices int
    30  	vertices    []Point
    31  }
    32  
    33  // LaxLoopFromPoints creates a LaxLoop from the given points.
    34  func LaxLoopFromPoints(vertices []Point) *LaxLoop {
    35  	l := &LaxLoop{
    36  		numVertices: len(vertices),
    37  		vertices:    make([]Point, len(vertices)),
    38  	}
    39  	copy(l.vertices, vertices)
    40  	return l
    41  }
    42  
    43  // LaxLoopFromLoop creates a LaxLoop from the given Loop, copying its points.
    44  func LaxLoopFromLoop(loop *Loop) *LaxLoop {
    45  	if loop.IsFull() {
    46  		panic("FullLoops are not yet supported")
    47  	}
    48  	if loop.IsEmpty() {
    49  		return &LaxLoop{}
    50  	}
    51  
    52  	l := &LaxLoop{
    53  		numVertices: len(loop.vertices),
    54  		vertices:    make([]Point, len(loop.vertices)),
    55  	}
    56  	copy(l.vertices, loop.vertices)
    57  	return l
    58  }
    59  
    60  func (l *LaxLoop) vertex(i int) Point { return l.vertices[i] }
    61  func (l *LaxLoop) NumEdges() int      { return l.numVertices }
    62  func (l *LaxLoop) Edge(e int) Edge {
    63  	e1 := e + 1
    64  	if e1 == l.numVertices {
    65  		e1 = 0
    66  	}
    67  	return Edge{l.vertices[e], l.vertices[e1]}
    68  
    69  }
    70  func (l *LaxLoop) Dimension() int                 { return 2 }
    71  func (l *LaxLoop) ReferencePoint() ReferencePoint { return referencePointForShape(l) }
    72  func (l *LaxLoop) NumChains() int                 { return minInt(1, l.numVertices) }
    73  func (l *LaxLoop) Chain(i int) Chain              { return Chain{0, l.numVertices} }
    74  func (l *LaxLoop) ChainEdge(i, j int) Edge {
    75  	var k int
    76  	if j+1 == l.numVertices {
    77  		k = j + 1
    78  	}
    79  	return Edge{l.vertices[j], l.vertices[k]}
    80  }
    81  func (l *LaxLoop) ChainPosition(e int) ChainPosition { return ChainPosition{0, e} }
    82  func (l *LaxLoop) IsEmpty() bool                     { return defaultShapeIsEmpty(l) }
    83  func (l *LaxLoop) IsFull() bool                      { return defaultShapeIsFull(l) }
    84  func (l *LaxLoop) typeTag() typeTag                  { return typeTagNone }
    85  func (l *LaxLoop) privateInterface()                 {}
    86  
    87  // TODO(roberts): Remaining to be ported from C++:
    88  // LaxClosedPolyline
    89  // VertexIDLaxLoop
    90  

View as plain text