View Javadoc
1   package org.opentrafficsim.road.gtu.lane.object;
2   
3   import nl.tudelft.simulation.language.d3.DirectedPoint;
4   
5   import org.djunits.value.vdouble.scalar.Length;
6   import org.opentrafficsim.core.geometry.OTSGeometryException;
7   import org.opentrafficsim.core.geometry.OTSLine3D;
8   import org.opentrafficsim.core.geometry.OTSPoint3D;
9   import org.opentrafficsim.core.object.StaticObject;
10  import org.opentrafficsim.road.network.lane.CrossSectionElement;
11  
12  /**
13   * <p>
14   * Copyright (c) 2013-2015 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
15   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
16   * </p>
17   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
18   * initial version Nov 26, 2015 <br>
19   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
20   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
21   */
22  public abstract class AbstractCSEObject extends StaticObject
23  {
24      /**
25       * @param geometry the geometry of the object
26       * @param height the height of the object
27       */
28      public AbstractCSEObject(final OTSLine3D geometry, final Length.Rel height)
29      {
30          super(geometry, height);
31      }
32  
33      /**
34       * Create an object around a position on the center of a cross section element (e.g. lane).
35       * @param cse the cross section element, e.g. lane, where the block is located
36       * @param position the relative position on the design line of the link for this block
37       * @param length the length of the object, parallel to the center line of the cse
38       * @param width the width of the object, perpendicular to the center line of the cse
39       * @param height the height of the object, above to the center line of the cse
40       * @return a new Geometry on the right position on the cse
41       * @throws OTSGeometryException in case of degenerate line or position beyond the center line
42       */
43      protected static OTSLine3D createRectangleOnCSE(final CrossSectionElement cse, final Length.Rel position,
44          final Length.Rel length, final Length.Rel width, final Length.Rel height) throws OTSGeometryException
45      {
46          // TODO: REPAIR
47          double fraction = position.si / cse.getParentLink().getLength().si;
48          double w2 = width.si / 2.0;
49          double l2 = length.si / 2.0;
50          DirectedPoint cp = cse.getCenterLine().getLocationFraction(fraction);
51          double a = cp.getRotZ();
52          double l2ca = l2 * Math.cos(a);
53          double l2sa = l2 * Math.sin(a);
54          double w2ca = w2 * Math.cos(a + Math.PI / 2.0);
55          double w2sa = w2 * Math.sin(a + Math.PI / 2.0);
56          OTSPoint3D p1 = new OTSPoint3D(cp.x - l2ca - w2ca, cp.y - l2sa - w2sa, cp.z + height.si);
57          OTSPoint3D p3 = new OTSPoint3D(cp.x + l2ca + w2ca, cp.y + l2sa + w2sa, cp.z + height.si);
58          OTSPoint3D p2 = new OTSPoint3D(cp.x + l2ca - w2ca, cp.y + l2sa - w2sa, cp.z + height.si);
59          OTSPoint3D p4 = new OTSPoint3D(cp.x - l2ca + w2ca, cp.y - l2sa + w2sa, cp.z + height.si);
60          return new OTSLine3D(p1, p2, p3, p4, p1);
61      }
62  
63      /**
64       * Create an object around a position on the center of a cross section element (e.g. lane).
65       * @param cse the cross section element, e.g. lane, where the block is located
66       * @param position the relative position on the design line of the link for this block
67       * @param length the length of the object, parallel to the center line of the cse
68       * @param width the width of the object, perpendicular to the center line of the cse
69       * @param height the height of the object, above to the center line of the cse
70       * @param distance the lateral distance of the object to the lane's center line; note: plus is left, minus is right
71       * @return a new Geometry on the right position on the cse
72       * @throws OTSGeometryException in case of degenerate line or position beyond the center line
73       */
74      protected static OTSLine3D createRectangleNextToCSE(final CrossSectionElement cse, final Length.Rel position,
75          final Length.Rel length, final Length.Rel width, final Length.Rel height, final Length.Rel distance)
76          throws OTSGeometryException
77      {
78          double fraction = position.si / cse.getParentLink().getLength().si;
79          double w2 = width.si / 2.0;
80          double l2 = length.si / 2.0;
81          double d = distance.si;
82          DirectedPoint cp = cse.getCenterLine().getLocationFraction(fraction);
83          double a = cp.getRotZ();
84          cp =
85              new DirectedPoint(cp.x + d * Math.cos(a + Math.PI / 2.0), cp.y + d * Math.sin(a + Math.PI / 2.0), cp.z,
86                  cp.getRotX(), cp.getRotY(), cp.getRotZ());
87          double l2ca = l2 * Math.cos(a);
88          double l2sa = l2 * Math.sin(a);
89          double w2ca = w2 * Math.cos(a + Math.PI / 2.0);
90          double w2sa = w2 * Math.sin(a + Math.PI / 2.0);
91          OTSPoint3D p1 = new OTSPoint3D(cp.x - l2ca - w2ca, cp.y - l2sa - w2sa, cp.z + height.si);
92          OTSPoint3D p3 = new OTSPoint3D(cp.x + l2ca + w2ca, cp.y + l2sa + w2sa, cp.z + height.si);
93          OTSPoint3D p2 = new OTSPoint3D(cp.x + l2ca - w2ca, cp.y + l2sa - w2sa, cp.z + height.si);
94          OTSPoint3D p4 = new OTSPoint3D(cp.x - l2ca + w2ca, cp.y - l2sa + w2sa, cp.z + height.si);
95          return new OTSLine3D(p1, p2, p3, p4, p1);
96      }
97  
98  }