View Javadoc

1   package cz.cuni.amis.pogamut.base.utils.math;
2   
3   import java.awt.geom.Line2D;
4   import java.awt.geom.Point2D;
5   
6   import javax.vecmath.Point3d;
7   import javax.vecmath.Vector3d;
8   
9   import cz.cuni.amis.pogamut.base3d.worldview.object.Location;
10  import cz.cuni.amis.pogamut.base3d.worldview.object.Rotation;
11  import cz.cuni.amis.pogamut.base3d.worldview.object.Velocity;
12  
13  import javax.vecmath.Vector2d;
14  
15  /**
16   * A for Algebra...
17   * @author Jimmy
18   *
19   */
20  public class A {
21  	
22  	public static final double DEG_TO_RAD = (Math.PI / 180);
23  	public static final double RAD_TO_DEG = (180 / Math.PI);
24  	
25  	
26  	public static Point2D projection(Location location) {
27  		return projection(location.getPoint3d());
28  	}
29  	
30  	public static Point2D projection(Velocity velocity) {
31  		return projection(velocity.getVector3d());
32  	}
33  	
34  	public static Point2D projection(Vector3d vector) {
35  		return new Point2D.Double(vector.getX(), vector.getY());
36  	}
37  	
38  	public static Point2D projection(Point3d point) {
39  		return new Point2D.Double(point.getX(), point.getY());
40  	}
41  	
42  	public static Point2D plus(Point2D p1, Point2D p2) {
43  		return new Point2D.Double(p1.getX()+p2.getX(), p1.getY()+p2.getY());		
44  	}
45  	
46  	public static Point2D multi(Point2D p, double multi) {
47  		return new Point2D.Double(p.getX() * multi, p.getY() * multi);
48  	}
49  	
50  	public static Point2D rotate(Point2D point, double rad){
51  		  // R(q) = ( cos q   sin q)
52  	      //        (-sin q   cos q)
53  		return new Point2D.Double( Math.cos(rad)*point.getX() - Math.sin(rad)*point.getY(),
54  			  			           Math.sin(rad)*point.getX() + Math.cos(rad)*point.getY());
55  	}
56  	
57  	public static double deg(double rad) {
58  		return rad * RAD_TO_DEG;
59  	}
60  	
61  	public static double rad(double deg) {
62  		return deg * DEG_TO_RAD;
63  	}
64  		
65  	public static double distanceFromRunningVector(Location agentLocation, Velocity runningVector, Location object) {
66  		Point2D location = projection(agentLocation);
67  		Point2D runVector = projection(runningVector.normalize());
68  		Line2D running = new Line2D.Double(location, plus(location, runVector));
69  		return running.ptLineDist(projection(object));
70  	}
71  	
72  
73  	
74  	public static Point2D vectorSum(Point2D[] vectors) {
75  		if (vectors == null || vectors.length == 0) return null;
76  		Point2D result = vectors[0];
77  		for (int i = 1; i < vectors.length; ++i) {
78  			result = plus(result, vectors[i]);
79  		}
80  		return result;
81  	}
82  
83          	/**
84  	 * Returns degrees!
85  	 * @param agentLocation
86  	 * @param agentRotation in degrees
87  	 * @param object
88  	 * @return
89  	 */
90  	public static double lineOfSightAngle(Location agentLocation, double agentRotationRollRad, Location object) {
91                  Vector2d sight = new Vector2d(Math.cos(agentRotationRollRad), Math.sin(agentRotationRollRad));
92  		Vector2d toTarget = new Vector2d(object.x - agentLocation.x, object.y - agentLocation.y);
93  		double lineOfSightAngle = A.deg(sight.angle(toTarget));
94  		return lineOfSightAngle;
95  	}
96  }