...

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

Documentation: github.com/golang/geo/s2

     1  // Copyright 2017 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  func laxLoopFromPoints(vertices []Point) *laxLoop {
    34  	l := &laxLoop{
    35  		numVertices: len(vertices),
    36  		vertices:    make([]Point, len(vertices)),
    37  	}
    38  	copy(l.vertices, vertices)
    39  	return l
    40  }
    41  
    42  func laxLoopFromLoop(loop *Loop) *laxLoop {
    43  	if loop.IsFull() {
    44  		panic("FullLoops are not yet supported")
    45  	}
    46  	if loop.IsEmpty() {
    47  		return &laxLoop{}
    48  	}
    49  
    50  	l := &laxLoop{
    51  		numVertices: len(loop.vertices),
    52  		vertices:    make([]Point, len(loop.vertices)),
    53  	}
    54  	copy(l.vertices, loop.vertices)
    55  	return l
    56  }
    57  
    58  func (l *laxLoop) vertex(i int) Point { return l.vertices[i] }
    59  func (l *laxLoop) NumEdges() int      { return l.numVertices }
    60  func (l *laxLoop) Edge(e int) Edge {
    61  	e1 := e + 1
    62  	if e1 == l.numVertices {
    63  		e1 = 0
    64  	}
    65  	return Edge{l.vertices[e], l.vertices[e1]}
    66  
    67  }
    68  func (l *laxLoop) Dimension() int                 { return 2 }
    69  func (l *laxLoop) ReferencePoint() ReferencePoint { return referencePointForShape(l) }
    70  func (l *laxLoop) NumChains() int                 { return minInt(1, l.numVertices) }
    71  func (l *laxLoop) Chain(i int) Chain              { return Chain{0, l.numVertices} }
    72  func (l *laxLoop) ChainEdge(i, j int) Edge {
    73  	var k int
    74  	if j+1 == l.numVertices {
    75  		k = j + 1
    76  	}
    77  	return Edge{l.vertices[j], l.vertices[k]}
    78  }
    79  func (l *laxLoop) ChainPosition(e int) ChainPosition { return ChainPosition{0, e} }
    80  func (l *laxLoop) IsEmpty() bool                     { return defaultShapeIsEmpty(l) }
    81  func (l *laxLoop) IsFull() bool                      { return defaultShapeIsFull(l) }
    82  func (l *laxLoop) typeTag() typeTag                  { return typeTagNone }
    83  func (l *laxLoop) privateInterface()                 {}
    84  

View as plain text