Debian Codegraph @

src/dr/geo/NewPolygon2D.java

raw symbols SCIP
1 /*
2 * NewPolygon2D.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 dr.xml.*;
29 import org.jdom.Element;
30
31 import java.awt.geom.*;
32 import java.util.List;
33 import java.util.StringTokenizer;
34
35 /**
36 * @author Marc A. Suchard
37 * @author Philippe Lemey
38 */
39
40 public class NewPolygon2D {
41
42 public static final String POLYGON = "polygon";
43 public static final String CLOSED = "closed";
44 public static final String FILL_VALUE = "fillValue";
45
46 public NewPolygon2D(GeneralPath path) {
47 this.path = path;
48 }
49
50 public NewPolygon2D(Element e) {
51
52 List<Element> children = e.getChildren();
53 // for (int a = 0; a < children.size(); a++) {
54 for (Element childElement : children) {
55 // Element childElement = (Element) children.get(a);
56 if (childElement.getName().equals(KMLCoordinates.COORDINATES)) {
57
58 String value = childElement.getTextTrim();
59 StringTokenizer st1 = new StringTokenizer(value, "\n");
60 int count = st1.countTokens(); //System.out.println(count);
61
62 // point2Ds = new LinkedList<Point2D>();
63 this.path = new GeneralPath();
64
65 for (int i = 0; i < count; i++) {
66 String line = st1.nextToken();
67 StringTokenizer st2 = new StringTokenizer(line, ",");
68 if (st2.countTokens() != 3)
69 throw new IllegalArgumentException("All KML coordinates must contain (X,Y,Z) values. Three dimensions not found in element '" + line + "'");
70 final float x = Float.valueOf(st2.nextToken());
71 final float y = Float.valueOf(st2.nextToken());
72 if (i == 0) {
73 path.moveTo(x, y);
74 } else {
75 path.lineTo(x, y);
76 }
77 // point2Ds.add(new Point2D.Double(x, y));
78 }
79 // length = point2Ds.size() - 1;
80 break;
81
82 }
83 }
84 }
85
86 public NewPolygon2D() {
87 path = new GeneralPath();
88 }
89
90 public void moveTo(Point2D pt) {
91 path.moveTo((float) pt.getX(), (float) pt.getY());
92 }
93
94 public void lineTo(Point2D pt) {
95 path.lineTo((float) pt.getX(), (float) pt.getY());
96 }
97
98 public boolean contains(Point2D pt) {
99 return path.contains(pt);
100 }
101
102 public void closePath() {
103 path.closePath();
104 }
105
106 public void setFillValue(double value) {
107 fillValue = value;
108 }
109
110 public double getFillValue() {
111 return fillValue;
112 }
113
114 public NewPolygon2D clip(Rectangle2D boundingBox) {
115 Area thisArea = new Area(path);
116 thisArea.intersect(new Area(boundingBox));
117 PathIterator iterator = thisArea.getPathIterator(null);
118 double[] v = new double[2];
119 while (!iterator.isDone()) {
120 int type = iterator.currentSegment(v);
121 System.err.println(":" + v[0] + v[1] + "\n");
122 iterator.next();
123 }
124 System.exit(-1);
125
126 GeneralPath path = new GeneralPath(thisArea);
127 path.closePath();
128 NewPolygon2D newPolygon = new NewPolygon2D(path);
129 newPolygon.setFillValue(this.getFillValue());
130 return newPolygon;
131 }
132
133 public String toString() {
134 StringBuffer sb = new StringBuffer();
135 sb.append("polygon(");
136 sb.append(fillValue);
137 sb.append(")[\n");
138 PathIterator iterator = path.getPathIterator(null);
139 float[] values = new float[2];
140 while (!iterator.isDone()) {
141 int type = iterator.currentSegment(values);
142 Point2D pt = new Point2D.Double(values[0], values[1]);
143 sb.append("\t");
144 sb.append(pt);
145 sb.append("\n");
146 iterator.next();
147 }
148 // sb.append(iterator);
149 // for(Point2D pt : point2Ds) {
150 // sb.append("\t");
151 // sb.append(pt);
152 // sb.append("\n");
153 // }
154 // sb.append(path.toString());
155 sb.append("]");
156 return sb.toString();
157 }
158
159 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
160
161 public String getParserName() {
162 return POLYGON;
163 }
164
165 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
166
167 KMLCoordinates coordinates = (KMLCoordinates) xo.getChild(KMLCoordinates.class);
168 boolean closed = xo.getAttribute(CLOSED, false);
169
170 if ((!closed && coordinates.length < 3) ||
171 (closed && coordinates.length < 4))
172 throw new XMLParseException("Insufficient points in polygon '" + xo.getId() + "' to define a polygon in 2D");
173
174 NewPolygon2D polygon = new NewPolygon2D();
175 polygon.moveTo(new Point2D.Double(coordinates.x[0], coordinates.y[0]));
176 int length = coordinates.length;
177 if (closed)
178 length--;
179 for (int i = 1; i < length; i++)
180 polygon.lineTo(new Point2D.Double(coordinates.x[i], coordinates.y[i]));
181 polygon.lineTo(new Point2D.Double(coordinates.x[0], coordinates.y[0]));
182 // polygon.closePath();
183
184 polygon.setFillValue(xo.getAttribute(FILL_VALUE, 0.0));
185
186 return polygon;
187 }
188
189 //************************************************************************
190 // AbstractXMLObjectParser implementation
191 //************************************************************************
192
193 public String getParserDescription() {
194 return "This element represents a polygon.";
195 }
196
197 public Class getReturnType() {
198 return Polygon2D.class;
199 }
200
201 public XMLSyntaxRule[] getSyntaxRules() {
202 return rules;
203 }
204
205 private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{
206 new ElementRule(KMLCoordinates.class),
207 AttributeRule.newBooleanRule(CLOSED, true),
208 AttributeRule.newDoubleRule(FILL_VALUE, true),
209 };
210 };
211
212 public static void main(String[] args) {
213 NewPolygon2D polygon = new NewPolygon2D();
214 polygon.moveTo(new Point2D.Double(-10, -10));
215 polygon.lineTo(new Point2D.Double(-10, 50));
216 polygon.lineTo(new Point2D.Double(10, 50));
217 polygon.lineTo(new Point2D.Double(10, -10));
218 polygon.lineTo(new Point2D.Double(-10, -10));
219 // polygon.closePath();
220 System.out.println(polygon);
221 System.out.println("");
222 // System.exit(-1);
223
224 Point2D pt = new Point2D.Double(0, 0);
225 System.out.println("polygon contains " + pt + ": " + polygon.contains(pt));
226 pt = new Point2D.Double(100, 100);
227 System.out.println("polygon contains " + pt + ": " + polygon.contains(pt));
228 System.out.println("");
229
230 Rectangle2D boundingBox = new Rectangle2D.Double(0, 0, 100, 100); // defines lower-left corner and width/height
231 System.out.println(boundingBox);
232 NewPolygon2D myClip = polygon.clip(boundingBox);
233 System.out.println(myClip);
234
235 }
236
237 private double fillValue;
238 private GeneralPath path;
239 }