View Javadoc

1   /*
2    * @(#) BoundsUtil.java Mar 3, 2004 Copyright (c) 2002-2005 Delft University of Technology Jaffalaan 5, 2628
3    * BX Delft, the Netherlands. All rights reserved. This software is proprietary information of Delft
4    * University of Technology The code is published under the Lesser General Public License
5    */
6   
7   package nl.tudelft.simulation.language.d3;
8   
9   import java.awt.geom.Rectangle2D;
10  
11  import javax.media.j3d.Bounds;
12  import javax.media.j3d.Transform3D;
13  import javax.vecmath.Point3d;
14  import javax.vecmath.Vector3d;
15  
16  /***
17   * A BoundsUtil.
18   * <p>
19   * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft University of Technology </a>, the
20   * Netherlands.
21   * <p>
22   * See for project information <a
23   * href="http://www.simulation.tudelft.nl/dsol/language">www.simulation.tudelft.nl/language </a> <br>
24   * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser General Public License (LGPL)
25   * </a>, no warranty
26   * 
27   * @version $Revision: 1.8 $ $Date: 2005/07/04 12:21:22 $
28   * @author <a href="http://www.peter-jacobs.com">Peter Jacobs </a>
29   */
30  public final class BoundsUtil
31  {
32      /***
33       * constructs a new BoundsUtil.
34       */
35      private BoundsUtil()
36      {
37          super();
38          // unreachable code
39      }
40  
41      /***
42       * computes the intersect of bounds with the zValue.
43       * 
44       * @param bounds
45       *            the bounds
46       * @param center
47       *            the
48       * @param zValue
49       *            the zValue
50       * @return Rectangle2D the result
51       */
52      public static Rectangle2D getIntersect(final DirectedPoint center, final Bounds bounds,
53              final double zValue)
54      {
55          BoundingBox box = new BoundingBox((Bounds) bounds.clone());
56          Transform3D transform = new Transform3D();
57          transform.rotZ(center.getRotZ());
58          transform.rotY(center.getRotY());
59          transform.rotX(center.getRotX());
60          transform.setTranslation(new Vector3d(new Point3d(center.x, center.y, center.z)));
61          box.transform(transform);
62  
63          Point3d lower = new Point3d();
64          box.getLower(lower);
65          lower.set(lower.x, lower.y, zValue);
66          if (!box.intersect(lower))
67          {
68              return null;
69          }
70          Point3d upper = new Point3d();
71          box.getUpper(upper);
72          return new Rectangle2D.Double(lower.x, lower.y, (upper.x - lower.x), (upper.y - lower.y));
73      }
74  
75      /***
76       * rotates and translates to a directed point.
77       * 
78       * @param point
79       *            the point
80       * @param bounds
81       *            the bounds
82       * @return the bounds
83       */
84      public static Bounds transform(final Bounds bounds, final DirectedPoint point)
85      {
86          Bounds result = (Bounds) bounds.clone();
87  
88          // First we rotate around 0,0,0
89          Transform3D transform = new Transform3D();
90          transform.rotX(point.getRotX());
91          transform.rotY(point.getRotY());
92          transform.rotZ(point.getRotZ());
93          transform.setTranslation(new Vector3d(point));
94          result.transform(transform);
95          return result;
96      }
97  
98      /***
99       * @param center
100      *            the center of the bounds
101      * @param bounds
102      *            the bounds
103      * @param point
104      *            the point
105      * @return whether or not the point is in the bounds
106      */
107     public static boolean contains(final DirectedPoint center, final Bounds bounds, final Point3d point)
108     {
109         BoundingBox box = new BoundingBox((Bounds) bounds.clone());
110         Transform3D transform = new Transform3D();
111         transform.rotZ(center.getRotZ());
112         transform.rotY(center.getRotY());
113         transform.rotX(center.getRotX());
114         transform.setTranslation(new Vector3d(center));
115         box.transform(transform);
116         Point3d lower = new Point3d();
117         box.getLower(lower);
118         Point3d upper = new Point3d();
119         box.getUpper(upper);
120         return (point.x >= lower.x && point.x <= upper.x && point.y >= lower.y && point.y <= upper.y
121                 && point.z >= lower.z && point.z <= upper.z);
122     }
123 }