View Javadoc
1   package org.opentrafficsim.core.network;
2   
3   import java.io.Serializable;
4   import java.rmi.RemoteException;
5   import java.util.HashSet;
6   import java.util.Set;
7   
8   import javax.media.j3d.BoundingSphere;
9   import javax.media.j3d.Bounds;
10  import javax.vecmath.Point3d;
11  
12  import nl.tudelft.simulation.dsol.animation.LocatableInterface;
13  import nl.tudelft.simulation.language.d3.DirectedPoint;
14  
15  import org.opentrafficsim.core.unit.AnglePlaneUnit;
16  import org.opentrafficsim.core.unit.AngleSlopeUnit;
17  import org.opentrafficsim.core.value.vdouble.scalar.DoubleScalar;
18  
19  /**
20   * The Node is a point with an id. It is used in the network to connect Links.
21   * <p>
22   * Copyright (c) 2013-2014 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
23   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
24   * <p>
25   * @version Aug 19, 2014 <br>
26   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
27   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
28   * @author <a href="http://www.citg.tudelft.nl">Guus Tamminga</a>
29   * @param <ID> the ID type.
30   * @param <P> the point type, e.g., com.vividsolutions.jts.geom.Point, DirectedPoint.
31   */
32  public abstract class AbstractNode<ID, P> implements Node<ID, P>, LocatableInterface, Serializable
33  {
34      /** */
35      private static final long serialVersionUID = 20140920L;
36  
37      /** the node id. */
38      private final ID id;
39  
40      /** the point. */
41      private final P point;
42  
43      /** the 3D direction. "East" is 0 degrees. "North" is 90 degrees (1/2 pi radians). */
44      private final DoubleScalar.Abs<AnglePlaneUnit> direction;
45  
46      /** the slope as an angle. */
47      private final DoubleScalar.Abs<AngleSlopeUnit> slope;
48  
49      /** the incoming links. */
50      private final Set<Link<?, ? extends Node<ID, P>>> linksIn = new HashSet<Link<?, ? extends Node<ID, P>>>();
51  
52      /** the outgoing links. */
53      private final Set<Link<?, ? extends Node<ID, P>>> linksOut = new HashSet<Link<?, ? extends Node<ID, P>>>();
54  
55      /**
56       * Construction of a Node.
57       * @param id the id of the Node.
58       * @param point the point with usually an x and y setting.
59       * @param direction the 3D direction. "East" is 0 degrees. "North" is 90 degrees (1/2 pi radians).
60       * @param slope the slope as an angle.
61       */
62      public AbstractNode(final ID id, final P point, final DoubleScalar.Abs<AnglePlaneUnit> direction,
63          final DoubleScalar.Abs<AngleSlopeUnit> slope)
64      {
65          this.id = id;
66          this.point = point;
67          this.direction = direction;
68          this.slope = slope;
69      }
70  
71      /**
72       * Construction of a Node.
73       * @param id the id of the Node.
74       * @param point the point with usually an x and y setting.
75       */
76      public AbstractNode(final ID id, final P point)
77      {
78          this(id, point, new DoubleScalar.Abs<AnglePlaneUnit>(0.0, AnglePlaneUnit.SI), new DoubleScalar.Abs<AngleSlopeUnit>(
79              0.0, AngleSlopeUnit.SI));
80      }
81  
82      /**
83       * @return node id.
84       */
85      public final ID getId()
86      {
87          return this.id;
88      }
89  
90      /**
91       * @return point.
92       */
93      public final P getPoint()
94      {
95          return this.point;
96      }
97  
98      /**
99       * @return the x-value of the point.
100      */
101     public abstract double getX();
102 
103     /**
104      * @return the y-value of the point.
105      */
106     public abstract double getY();
107 
108     /**
109      * @return the z-value of the point.
110      */
111     public abstract double getZ();
112 
113     /** {@inheritDoc} */
114     @Override
115     public final void addLinkIn(final Link<?, ? extends Node<ID, P>> linkIn)
116     {
117         this.linksIn.add(linkIn);
118     }
119 
120     /** {@inheritDoc} */
121     @Override
122     public final void addLinkOut(final Link<?, ? extends Node<ID, P>> linkOut)
123     {
124         this.linksOut.add(linkOut);
125     }
126 
127     /** {@inheritDoc} */
128     @Override
129     public final Set<Link<?, ? extends Node<ID, P>>> getLinksIn()
130     {
131         // XXX: should return a copy?
132         return this.linksIn;
133     }
134 
135     /** {@inheritDoc} */
136     @Override
137     public final Set<Link<?, ? extends Node<ID, P>>> getLinksOut()
138     {
139         // XXX: should return a copy?
140         return this.linksOut;
141     }
142 
143     /**
144      * @return direction.
145      */
146     public final DoubleScalar.Abs<AnglePlaneUnit> getDirection()
147     {
148         return this.direction;
149     }
150 
151     /**
152      * @return slope.
153      */
154     public final DoubleScalar.Abs<AngleSlopeUnit> getSlope()
155     {
156         return this.slope;
157     }
158 
159     /** {@inheritDoc} */
160     @Override
161     public final DirectedPoint getLocation() throws RemoteException
162     {
163         return new DirectedPoint(new double[] {getX(), getY(), getZ()});
164     }
165 
166     /** {@inheritDoc} */
167     @Override
168     public final Bounds getBounds() throws RemoteException
169     {
170         return new BoundingSphere(new Point3d(0.0d, 0.0d, 0.0d), 10.0d);
171     }
172 
173     /** {@inheritDoc} */
174     public final String toString()
175     {
176         return "Node " + this.id;
177     }
178 
179     /** {@inheritDoc} */
180     @Override
181     @SuppressWarnings("checkstyle:designforextension")
182     public int hashCode()
183     {
184         final int prime = 31;
185         int result = 1;
186         result = prime * result + ((this.id == null) ? 0 : this.id.hashCode());
187         return result;
188     }
189 
190     /** {@inheritDoc} */
191     @Override
192     @SuppressWarnings({"checkstyle:designforextension", "checkstyle:needbraces"})
193     public boolean equals(final Object obj)
194     {
195         if (this == obj)
196             return true;
197         if (obj == null)
198             return false;
199         if (getClass() != obj.getClass())
200             return false;
201         @SuppressWarnings("unchecked")
202         AbstractNode<ID, P> other = (AbstractNode<ID, P>) obj;
203         if (this.id == null)
204         {
205             if (other.id != null)
206                 return false;
207         }
208         else if (!this.id.equals(other.id))
209             return false;
210         return true;
211     }
212 
213 }