View Javadoc

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