1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.g2d.utils.geom;
14 import java.awt.geom.Point2D;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.HashSet;
20 import org.simantics.g2d.utils.GeometryUtils;
23 * A set of compass [0..360) directions
25 public class DirectionSet extends HashSet<Double> {
27 public static final DirectionSet NESW = new DirectionSet(0.0, 90.0, 180.0, 270.0);
29 public static final DirectionSet N = new DirectionSet(0.0);
30 public static final DirectionSet NE = new DirectionSet(0.0, 90.0);
31 public static final DirectionSet E = new DirectionSet(90.0);
32 public static final DirectionSet SE = new DirectionSet(90.0,180.0);
33 public static final DirectionSet S = new DirectionSet(180.0);
34 public static final DirectionSet SW = new DirectionSet(180.0, 270.0);
35 public static final DirectionSet W = new DirectionSet(270.0);
36 public static final DirectionSet NW = new DirectionSet(270.0, 0.0);
38 public static final DirectionSet NE2 = new DirectionSet(0.0, 90.0, 45.0);
39 public static final DirectionSet SE2 = new DirectionSet(90.0,180.0, 135.0);
40 public static final DirectionSet SW2 = new DirectionSet(180.0, 270.0, 225.0);
41 public static final DirectionSet NW2 = new DirectionSet(270.0, 0.0, 315.0);
43 public static final DirectionSet HORIZ = new DirectionSet(90.0, 270.0);
44 public static final DirectionSet VERT = new DirectionSet(0.0, 180.0);
45 public static final DirectionSet ANY =
47 0, 15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180,
48 195, 210, 225, 240, 255, 270, 285, 300, 315, 330, 345);
50 private Set<Point2D> unitVectors;
52 public DirectionSet(double ... directions)
54 for (double d : directions)
58 private static final long serialVersionUID = 1L;
59 private boolean add(double e) {
60 if (e<0 || e>=360.0) e = Math.IEEEremainder(e, 360.0);
65 public boolean add(Double e) {
67 if (e==null) return false;
68 return add((double)e);
71 public boolean addAll(Collection<? extends Double> c) {
73 return super.addAll(c);
76 public boolean remove(Object o) {
78 return super.remove(o);
81 public boolean removeAll(Collection<?> c) {
83 return super.removeAll(c);
91 public Double getClosestDirection(double d2)
93 Double closest = null;
94 for (Double d1 : this)
96 double diff = d1>180==d2>180 ? Math.abs(d1-d2) : d1<d2 ? d1-d2+360 : d2-d1+360;
97 if (closest==null || closest<diff)
102 public Double getClosestDirection(double d2, double tolerance)
104 Double closest = null;
105 for (Double d1 : this)
107 double diff = d1>180==d2>180 ? Math.abs(d1-d2) : d1<d2 ? d1-d2+360 : d2-d1+360;
108 if (diff>tolerance) continue;
109 if (closest==null || closest<diff)
116 * Create direction set of inverse directions
119 public DirectionSet createInverse()
121 DirectionSet result = new DirectionSet();
122 for (double dir : this)
125 if (dir>360.0) dir-=360.0;
133 * Get directions as unit vectors
134 * @return collection where to add unit vectors
136 private Set<Point2D> createUnitVectors() {
137 HashSet<Point2D> result = new HashSet<Point2D>(size());
138 for (double d : this)
139 result.add( GeometryUtils.toUnitVector(d, new Point2D.Double()) );
143 public Set<Point2D> getUnitVectors() {
144 if (unitVectors==null) {
145 unitVectors = createUnitVectors();
146 unitVectors = Collections.unmodifiableSet(unitVectors);