1 // Copyright 2013 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 
16 // Original Author: ericv@google.com (Eric Veach)
17 // Converted to D:  madric@gmail.com (Vijay Nayar)
18 
19 module s2.s2edge_vector_shape;
20 
21 import s2.s2shape;
22 import s2.s2point;
23 
24 /**
25  * S2EdgeVectorShape is an S2Shape representing an arbitrary set of edges.  It
26  * is mainly used for testing, but it can also be useful if you have, say, a
27  * collection of polylines and don't care about memory efficiency (since this
28  * class would store most of the vertices twice).
29  *
30  * Note that if you already have data stored in an S2Loop, S2Polyline, or
31  * S2Polygon, then you would be better off using the "Shape" class defined
32  * within those classes (e.g., S2Loop::Shape).  Similarly, if the vertex data
33  * is stored in your own data structures, you can easily write your own
34  * subclass of S2Shape that points to the existing vertex data rather than
35  * copying it.
36  */
37 class S2EdgeVectorShape : S2Shape {
38 public:
39   // Constructs an empty edge vector.
40   this() {}
41 
42   // Constructs an S2EdgeVectorShape from a vector of edges.
43   this(S2Point[2][] edges) {
44     _edges = edges;
45   }
46 
47   // Creates an S2EdgeVectorShape containing a single edge.
48   this(in S2Point a, in S2Point b) {
49     _edges ~= [a, b];
50   }
51 
52   // Adds an edge to the vector.
53   //
54   // IMPORTANT: This method should only be called *before* adding the
55   // S2EdgeVectorShape to an S2ShapeIndex.  S2Shapes can only be modified by
56   // removing them from the index, making changes, and adding them back again.
57   void add(in S2Point a, in S2Point b) {
58     _edges ~= [a, b];
59   }
60 
61   // S2Shape interface:
62   override
63   int numEdges() const {
64     return cast(int) _edges.length;
65   }
66 
67   override
68   S2Shape.Edge edge(int e) const {
69     return S2Shape.Edge(_edges[e][0], _edges[e][1]);
70   }
71 
72   override
73   int dimension() const {
74     return 1;
75   }
76 
77   override
78   S2Shape.ReferencePoint getReferencePoint() const {
79     return ReferencePoint(false);
80   }
81 
82   override
83   int numChains() const {
84     return cast(int) _edges.length;
85   }
86 
87   override
88   S2Shape.Chain chain(int i) const {
89     return Chain(i, 1);
90   }
91 
92   override
93   S2Shape.Edge chainEdge(int i, int j) const
94   in {
95     assert(j == 0);
96   } do {
97     return S2Shape.Edge(_edges[i][0], _edges[i][1]);
98   }
99 
100   override
101   S2Shape.ChainPosition chainPosition(int e) const {
102     return S2Shape.ChainPosition(e, 0);
103   }
104 
105 private:
106   S2Point[2][] _edges;
107 }