]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.g2d.utils.geom;
13
14 import java.awt.geom.Point2D;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.HashSet;
18 import java.util.Set;
19
20 import org.simantics.g2d.utils.GeometryUtils;
21
22 /**
23  * A set of compass [0..360) directions
24  */
25 public class DirectionSet extends HashSet<Double> {
26         
27         public static final DirectionSet NESW = new DirectionSet(0.0, 90.0, 180.0, 270.0);
28         
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);
37
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);
42         
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 = 
46                 new DirectionSet(
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);                         
49
50         private Set<Point2D> unitVectors;
51         
52         public DirectionSet(double ... directions)
53         {
54                 for (double d : directions)
55                         add(d);
56         }
57         
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);
61                 if (e<0) e = 360.0-e;
62                 return super.add(e);
63         }
64         @Override
65         public boolean add(Double e) {
66                 unitVectors = null;
67                 if (e==null) return false;
68                 return add((double)e);
69         }
70         @Override
71         public boolean addAll(Collection<? extends Double> c) {
72                 unitVectors = null;
73                 return super.addAll(c);
74         }
75         @Override
76         public boolean remove(Object o) {
77                 unitVectors = null;
78                 return super.remove(o);
79         }
80         @Override
81         public boolean removeAll(Collection<?> c) {
82                 unitVectors = null;
83                 return super.removeAll(c);
84         }
85         @Override
86         public void clear() {
87                 unitVectors = null;
88                 super.clear();
89         }
90         
91         public Double getClosestDirection(double d2)
92         {
93                 Double closest = null;
94                 for (Double d1 : this)
95                 {
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)
98                                 closest = diff;
99                 }
100                 return closest;
101         }
102         public Double getClosestDirection(double d2, double tolerance)
103         {
104                 Double closest = null;
105                 for (Double d1 : this)
106                 {
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)
110                                 closest = diff;
111                 }
112                 return closest;
113         }       
114         
115         /**
116          * Create direction set of inverse directions
117          * @return
118          */
119         public DirectionSet createInverse() 
120         {
121                 DirectionSet result = new DirectionSet();
122                 for (double dir : this)
123                 {
124                         dir = dir + 180.0;
125                         if (dir>360.0) dir-=360.0;
126                         result.add(dir);
127                 }
128                 return result;
129         }
130         
131         
132         /**
133          * Get directions as unit vectors
134          * @return collection where to add unit vectors
135          */
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()) );
140                 return result;
141         }
142
143         public Set<Point2D> getUnitVectors() {
144                 if (unitVectors==null) {
145                         unitVectors = createUnitVectors();
146                         unitVectors = Collections.unmodifiableSet(unitVectors);
147                 }
148                 return unitVectors;
149         }
150
151         
152         
153 }