]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.g2d/src/org/simantics/g2d/utils/geom/DirectionSet.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / utils / geom / DirectionSet.java
index e1475c4a269a608605af132238ca6fc84b8f979a..563101ac8cf9f353227e54fc66ddfbedc84a9cf1 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.g2d.utils.geom;\r
-\r
-import java.awt.geom.Point2D;\r
-import java.util.Collection;\r
-import java.util.Collections;\r
-import java.util.HashSet;\r
-import java.util.Set;\r
-\r
-import org.simantics.g2d.utils.GeometryUtils;\r
-\r
-/**\r
- * A set of compass [0..360) directions\r
- */\r
-public class DirectionSet extends HashSet<Double> {\r
-       \r
-       public static final DirectionSet NESW = new DirectionSet(0.0, 90.0, 180.0, 270.0);\r
-       \r
-       public static final DirectionSet N      = new DirectionSet(0.0);\r
-       public static final DirectionSet NE     = new DirectionSet(0.0, 90.0);\r
-       public static final DirectionSet E      = new DirectionSet(90.0);\r
-       public static final DirectionSet SE     = new DirectionSet(90.0,180.0);\r
-       public static final DirectionSet S      = new DirectionSet(180.0);\r
-       public static final DirectionSet SW     = new DirectionSet(180.0, 270.0);\r
-       public static final DirectionSet W      = new DirectionSet(270.0);\r
-       public static final DirectionSet NW     = new DirectionSet(270.0, 0.0);\r
-\r
-       public static final DirectionSet NE2    = new DirectionSet(0.0, 90.0, 45.0);\r
-       public static final DirectionSet SE2    = new DirectionSet(90.0,180.0, 135.0);\r
-       public static final DirectionSet SW2    = new DirectionSet(180.0, 270.0, 225.0);\r
-       public static final DirectionSet NW2    = new DirectionSet(270.0, 0.0, 315.0);\r
-       \r
-       public static final DirectionSet HORIZ = new DirectionSet(90.0, 270.0);\r
-       public static final DirectionSet VERT = new DirectionSet(0.0, 180.0);\r
-       public static final DirectionSet ANY = \r
-               new DirectionSet(\r
-                               0, 15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180,\r
-                               195, 210, 225, 240, 255, 270, 285, 300, 315, 330, 345);                         \r
-\r
-       private Set<Point2D> unitVectors;\r
-       \r
-       public DirectionSet(double ... directions)\r
-       {\r
-               for (double d : directions)\r
-                       add(d);\r
-       }\r
-       \r
-       private static final long serialVersionUID = 1L;\r
-       private boolean add(double e) {\r
-               if (e<0 || e>=360.0) e = Math.IEEEremainder(e, 360.0);\r
-               if (e<0) e = 360.0-e;\r
-               return super.add(e);\r
-       }\r
-       @Override\r
-       public boolean add(Double e) {\r
-               unitVectors = null;\r
-               if (e==null) return false;\r
-               return add((double)e);\r
-       }\r
-       @Override\r
-       public boolean addAll(Collection<? extends Double> c) {\r
-               unitVectors = null;\r
-               return super.addAll(c);\r
-       }\r
-       @Override\r
-       public boolean remove(Object o) {\r
-               unitVectors = null;\r
-               return super.remove(o);\r
-       }\r
-       @Override\r
-       public boolean removeAll(Collection<?> c) {\r
-               unitVectors = null;\r
-               return super.removeAll(c);\r
-       }\r
-       @Override\r
-       public void clear() {\r
-               unitVectors = null;\r
-               super.clear();\r
-       }\r
-       \r
-       public Double getClosestDirection(double d2)\r
-       {\r
-               Double closest = null;\r
-               for (Double d1 : this)\r
-               {\r
-                       double diff = d1>180==d2>180 ? Math.abs(d1-d2) : d1<d2 ? d1-d2+360 : d2-d1+360;\r
-                       if (closest==null || closest<diff)\r
-                               closest = diff;\r
-               }\r
-               return closest;\r
-       }\r
-       public Double getClosestDirection(double d2, double tolerance)\r
-       {\r
-               Double closest = null;\r
-               for (Double d1 : this)\r
-               {\r
-                       double diff = d1>180==d2>180 ? Math.abs(d1-d2) : d1<d2 ? d1-d2+360 : d2-d1+360;\r
-                       if (diff>tolerance) continue;\r
-                       if (closest==null || closest<diff)\r
-                               closest = diff;\r
-               }\r
-               return closest;\r
-       }       \r
-       \r
-       /**\r
-        * Create direction set of inverse directions\r
-        * @return\r
-        */\r
-       public DirectionSet createInverse() \r
-       {\r
-               DirectionSet result = new DirectionSet();\r
-               for (double dir : this)\r
-               {\r
-                       dir = dir + 180.0;\r
-                       if (dir>360.0) dir-=360.0;\r
-                       result.add(dir);\r
-               }\r
-               return result;\r
-       }\r
-       \r
-       \r
-       /**\r
-        * Get directions as unit vectors\r
-        * @return collection where to add unit vectors\r
-        */\r
-       private Set<Point2D> createUnitVectors() {\r
-               HashSet<Point2D> result = new HashSet<Point2D>(size());\r
-               for (double d : this)\r
-                       result.add( GeometryUtils.toUnitVector(d, new Point2D.Double()) );\r
-               return result;\r
-       }\r
-\r
-       public Set<Point2D> getUnitVectors() {\r
-               if (unitVectors==null) {\r
-                       unitVectors = createUnitVectors();\r
-                       unitVectors = Collections.unmodifiableSet(unitVectors);\r
-               }\r
-               return unitVectors;\r
-       }\r
-\r
-       \r
-       \r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.g2d.utils.geom;
+
+import java.awt.geom.Point2D;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.simantics.g2d.utils.GeometryUtils;
+
+/**
+ * A set of compass [0..360) directions
+ */
+public class DirectionSet extends HashSet<Double> {
+       
+       public static final DirectionSet NESW = new DirectionSet(0.0, 90.0, 180.0, 270.0);
+       
+       public static final DirectionSet N      = new DirectionSet(0.0);
+       public static final DirectionSet NE     = new DirectionSet(0.0, 90.0);
+       public static final DirectionSet E      = new DirectionSet(90.0);
+       public static final DirectionSet SE     = new DirectionSet(90.0,180.0);
+       public static final DirectionSet S      = new DirectionSet(180.0);
+       public static final DirectionSet SW     = new DirectionSet(180.0, 270.0);
+       public static final DirectionSet W      = new DirectionSet(270.0);
+       public static final DirectionSet NW     = new DirectionSet(270.0, 0.0);
+
+       public static final DirectionSet NE2    = new DirectionSet(0.0, 90.0, 45.0);
+       public static final DirectionSet SE2    = new DirectionSet(90.0,180.0, 135.0);
+       public static final DirectionSet SW2    = new DirectionSet(180.0, 270.0, 225.0);
+       public static final DirectionSet NW2    = new DirectionSet(270.0, 0.0, 315.0);
+       
+       public static final DirectionSet HORIZ = new DirectionSet(90.0, 270.0);
+       public static final DirectionSet VERT = new DirectionSet(0.0, 180.0);
+       public static final DirectionSet ANY = 
+               new DirectionSet(
+                               0, 15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180,
+                               195, 210, 225, 240, 255, 270, 285, 300, 315, 330, 345);                         
+
+       private Set<Point2D> unitVectors;
+       
+       public DirectionSet(double ... directions)
+       {
+               for (double d : directions)
+                       add(d);
+       }
+       
+       private static final long serialVersionUID = 1L;
+       private boolean add(double e) {
+               if (e<0 || e>=360.0) e = Math.IEEEremainder(e, 360.0);
+               if (e<0) e = 360.0-e;
+               return super.add(e);
+       }
+       @Override
+       public boolean add(Double e) {
+               unitVectors = null;
+               if (e==null) return false;
+               return add((double)e);
+       }
+       @Override
+       public boolean addAll(Collection<? extends Double> c) {
+               unitVectors = null;
+               return super.addAll(c);
+       }
+       @Override
+       public boolean remove(Object o) {
+               unitVectors = null;
+               return super.remove(o);
+       }
+       @Override
+       public boolean removeAll(Collection<?> c) {
+               unitVectors = null;
+               return super.removeAll(c);
+       }
+       @Override
+       public void clear() {
+               unitVectors = null;
+               super.clear();
+       }
+       
+       public Double getClosestDirection(double d2)
+       {
+               Double closest = null;
+               for (Double d1 : this)
+               {
+                       double diff = d1>180==d2>180 ? Math.abs(d1-d2) : d1<d2 ? d1-d2+360 : d2-d1+360;
+                       if (closest==null || closest<diff)
+                               closest = diff;
+               }
+               return closest;
+       }
+       public Double getClosestDirection(double d2, double tolerance)
+       {
+               Double closest = null;
+               for (Double d1 : this)
+               {
+                       double diff = d1>180==d2>180 ? Math.abs(d1-d2) : d1<d2 ? d1-d2+360 : d2-d1+360;
+                       if (diff>tolerance) continue;
+                       if (closest==null || closest<diff)
+                               closest = diff;
+               }
+               return closest;
+       }       
+       
+       /**
+        * Create direction set of inverse directions
+        * @return
+        */
+       public DirectionSet createInverse() 
+       {
+               DirectionSet result = new DirectionSet();
+               for (double dir : this)
+               {
+                       dir = dir + 180.0;
+                       if (dir>360.0) dir-=360.0;
+                       result.add(dir);
+               }
+               return result;
+       }
+       
+       
+       /**
+        * Get directions as unit vectors
+        * @return collection where to add unit vectors
+        */
+       private Set<Point2D> createUnitVectors() {
+               HashSet<Point2D> result = new HashSet<Point2D>(size());
+               for (double d : this)
+                       result.add( GeometryUtils.toUnitVector(d, new Point2D.Double()) );
+               return result;
+       }
+
+       public Set<Point2D> getUnitVectors() {
+               if (unitVectors==null) {
+                       unitVectors = createUnitVectors();
+                       unitVectors = Collections.unmodifiableSet(unitVectors);
+               }
+               return unitVectors;
+       }
+
+       
+       
 }
\ No newline at end of file