summaryrefslogtreecommitdiff
path: root/src/de/lmu/ifi/dbs/elki/data/spatial/Polygon.java
blob: 6166e0ce6e7170db86b96707eecb5fc5d1c12855 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package de.lmu.ifi.dbs.elki.data.spatial;

/*
 This file is part of ELKI:
 Environment for Developing KDD-Applications Supported by Index-Structures

 Copyright (C) 2014
 Ludwig-Maximilians-Universität München
 Lehr- und Forschungseinheit für Datenbanksysteme
 ELKI Development Team

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Iterator;
import java.util.List;

import de.lmu.ifi.dbs.elki.math.linearalgebra.Vector;
import de.lmu.ifi.dbs.elki.utilities.datastructures.iterator.ArrayListIter;

/**
 * Class representing a simple polygon. While you can obviously store non-simple
 * polygons in this, note that many of the supplied methods will assume that the
 * polygons are simple.
 * 
 * @author Erich Schubert
 * 
 * @apiviz.composedOf Vector
 */
public class Polygon implements SpatialComparable {
  /**
   * The actual points
   */
  private List<Vector> points;

  /**
   * Minimum values
   */
  private double[] min = null;

  /**
   * Maximum values
   */
  private double[] max = null;

  /**
   * Constructor.
   * 
   * @param points Polygon points
   */
  public Polygon(List<Vector> points) {
    super();
    this.points = points;
    // Compute the bounds.
    if (points.size() > 0) {
      final Iterator<Vector> iter = points.iterator();
      final Vector first = iter.next();
      final int dim = first.getDimensionality();
      min = first.getArrayCopy();
      max = first.getArrayCopy();
      while (iter.hasNext()) {
        Vector next = iter.next();
        for (int i = 0; i < dim; i++) {
          final double cur = next.get(i);
          min[i] = Math.min(min[i], cur);
          max[i] = Math.max(max[i], cur);
        }
      }
    }
  }

  public Polygon(List<Vector> points, double minx, double maxx, double miny, double maxy) {
    super();
    this.points = points;
    this.min = new double[] { minx, miny };
    this.max = new double[] { maxx, maxy };
  }

  /**
   * Get an iterator to the vector contents.
   * 
   * @return Iterator
   */
  public ArrayListIter<Vector> iter() {
    return new ArrayListIter<>(points);
  }

  /**
   * Append the polygon to the buffer.
   * 
   * @param buf Buffer to append to
   */
  public void appendToBuffer(StringBuilder buf) {
    Iterator<Vector> iter = points.iterator();
    while (iter.hasNext()) {
      double[] data = iter.next().getArrayRef();
      for (int i = 0; i < data.length; i++) {
        if (i > 0) {
          buf.append(",");
        }
        buf.append(data[i]);
      }
      if (iter.hasNext()) {
        buf.append(" ");
      }
    }
  }

  @Override
  public String toString() {
    StringBuilder buf = new StringBuilder();
    appendToBuffer(buf);
    return buf.toString();
  }

  /**
   * Get the polygon length.
   * 
   * @return Polygon length
   */
  public int size() {
    return points.size();
  }

  /**
   * Get a vector by index.
   * 
   * @param idx Index to get
   * @return Vector
   */
  public Vector get(int idx) {
    return points.get(idx);
  }

  @Override
  public int getDimensionality() {
    return min.length;
  }

  @Override
  public double getMin(int dimension) {
    return min[dimension];
  }

  @Override
  public double getMax(int dimension) {
    return max[dimension];
  }

  /**
   * Test polygon orientation.
   * 
   * @return -1, 0, 1 for counterclockwise, undefined and clockwise.
   */
  public int testClockwise() {
    if (points.size() < 3) {
      return 0;
    }
    final int size = points.size();
    // Count the number of positive and negative oriented angles
    int c = 0;

    // TODO: faster when using an iterator?
    for (int i = 0; i < size; i++) {
      // Three consecutive points
      final int j = (i + 1) % size;
      final int k = (i + 2) % size;
      final double dxji = points.get(j).get(0) - points.get(i).get(0);
      final double dykj = points.get(k).get(1) - points.get(j).get(1);
      final double dyji = points.get(j).get(1) - points.get(i).get(1);
      final double dxkj = points.get(k).get(0) - points.get(j).get(0);
      final double z = (dxji * dykj) - (dyji * dxkj);
      if (z < 0) {
        c--;
      } else if (z > 0) {
        c++;
      }
    }
    if (c > 0) {
      return -1;
    } else if (c < 0) {
      return +1;
    } else {
      return 0;
    }
  }

  /**
   * Simple polygon intersection test.
   * 
   * <p>
   * FIXME: while this is found on some web pages as "solution" and satisfies or
   * needs, it clearly is not correct; not even for convex polygons: Consider a
   * cross where the two bars are made out of four vertices each. No vertex is
   * inside the other polygon, yet they intersect.
   * 
   * I knew this before writing this code, but this O(n) code was the simplest
   * thing to come up with, and it would work for our current data sets. A way
   * to fix this is to augment it with the obvious O(n*n) segment intersection
   * test. (Note that you will still need to test for point containment, since
   * the whole polygon could be contained in the other!)
   * </p>
   * 
   * @param other Other polygon
   * @return True when the polygons intersect
   */
  public boolean intersects2DIncomplete(Polygon other) {
    assert (this.getDimensionality() == 2);
    assert (other.getDimensionality() == 2);
    for (Vector v : this.points) {
      if (other.containsPoint2D(v)) {
        return true;
      }
    }
    for (Vector v : other.points) {
      if (this.containsPoint2D(v)) {
        return true;
      }
    }
    return false;
  }

  /**
   * Point in polygon test, based on
   * 
   * http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
   * 
   * by W. Randolph Franklin
   * 
   * @param v Point to test
   * @return True when contained.
   */
  public boolean containsPoint2D(Vector v) {
    assert (v.getDimensionality() == 2);
    final double testx = v.get(0);
    final double testy = v.get(1);
    boolean c = false;

    Iterator<Vector> it = points.iterator();
    Vector pre = points.get(points.size() - 1);
    while (it.hasNext()) {
      final Vector cur = it.next();
      final double curx = cur.get(0);
      final double cury = cur.get(1);
      final double prex = pre.get(0);
      final double prey = pre.get(1);
      if (((cury > testy) != (prey > testy))) {
        if ((testx < (prex - curx) * (testy - cury) / (prey - cury) + curx)) {
          c = !c;
        }
      }
      pre = cur;
    }
    return c;
  }
}