Debian Codegraph @

src/dr/geo/Polygon.java

raw symbols SCIP
1 /*
2 * Polygon.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.geo;
27
28 import org.jdom.Element;
29
30 import java.util.StringTokenizer;
31 import java.util.List;
32
33 import dr.xml.XMLParseException;
34
35 /**
36 * Created by IntelliJ IDEA.
37 * User: phil
38 * Date: Feb 19, 2009
39 * Time: 5:15:25 PM
40 * To change this template use File | Settings | File Templates.
41 */
42 public class Polygon {
43
44 public static final String POLYGON = "polygon";
45 public static final String CLOSED = "closed";
46 public static final String COORDINATES = "coordinates";
47 // includes a density value
48 //public static final String DENSITY = "density";
49
50 public Polygon(Element e) {
51
52 boolean closed = Boolean.valueOf(e.getAttributeValue(CLOSED));
53
54 List children = e.getChildren();
55 for (int a = 0; a < children.size(); a++) {
56 Element childElement = (Element)children.get(a);
57 if (childElement.getName().equals(COORDINATES)) {
58
59 String value = childElement.getTextTrim();
60 StringTokenizer st1 = new StringTokenizer(value, "\n");
61 int count = st1.countTokens(); //System.out.println(count);
62
63 x = new double[count];
64 y = new double[count];
65 z = new double[count];
66 // assumes closed!
67 length = x.length - 1;
68
69 for (int i = 0; i < count; i++) {
70 String line = st1.nextToken();
71 StringTokenizer st2 = new StringTokenizer(line, ",");
72 if (st2.countTokens() != 3)
73 throw new IllegalArgumentException("All KML coordinates must contain (X,Y,Z) values. Three dimensions not found in element '" + line + "'");
74 x[i] = Double.valueOf(st2.nextToken());
75 y[i] = Double.valueOf(st2.nextToken());
76 z[i] = Double.valueOf(st2.nextToken());
77 }
78 //gets the density value
79 //String densityValue = e.getChild(DENSITY).getTextTrim();
80 //d = Double.valueOf(densityValue);
81 }
82 }
83 planarInZ = isPlanarInZ();
84 }
85
86 protected double[] x;
87 protected double[] y;
88 protected double[] z;
89 protected double[] t;
90 // declares dernsity double
91 //protected double d;
92
93 protected int length;
94 private boolean planarInZ;
95
96 //returns density value
97 //public double getDensity(){
98 //return d;
99 //}
100
101 private boolean isPlanarInZ() {
102 double z0 = z[0];
103 boolean planar = true;
104 for (int i = 0; i < length; i++) {
105 if (z0 != z[i]) {
106 planar = false;
107 break;
108 }
109 }
110 return planar;
111 }
112
113 public boolean contains2DPoint(double inX, double inY) {
114
115 // System.err.print("contains (" + inX + "," + inY + ") = ");
116
117 if (!planarInZ)
118 throw new RuntimeException("Only 2D polygons are currently implemented");
119 boolean contains = false;
120
121 // Take a horizontal ray from (inX,inY) to the right.
122 // If ray across the polygon edges an odd # of times, the point is inside.
123 for (int i = 0, j = length - 1; i < length; j = i++) {
124
125 if ((((y[i] <= inY) && (inY < y[j])) ||
126 ((y[j] <= inY) && (inY < y[i]))) &&
127 (inX < (x[j] - x[i]) * (inY - y[i]) / (y[j] - y[i]) + x[i]))
128 contains = !contains;
129 }
130
131 // System.err.println(contains);
132 return contains;
133 }
134
135 }