Updated to worldwind release 20070817
[worldwind-tracker.git] / gov / nasa / worldwind / geom / Line.java
blobadec263de73ccbf638f699639fcbff6dd8e7fd71
1 /*
2 Copyright (C) 2001, 2006 United States Government
3 as represented by the Administrator of the
4 National Aeronautics and Space Administration.
5 All Rights Reserved.
6 */
7 package gov.nasa.worldwind.geom;
9 import gov.nasa.worldwind.util.Logging;
11 /**
12 * @author Tom Gaskins
13 * @version $Id: Line.java 2471 2007-07-31 21:50:57Z tgaskins $
15 public final class Line// Instances are immutable
17 private final Vec4 origin;
18 private final Vec4 direction;
20 /**
21 * @param origin
22 * @param direction
23 * @throws IllegalArgumentException if <code>origin</code> is null, or <code>direction</code> is null or has zero
24 * length
26 public Line(Vec4 origin, Vec4 direction)
28 String message = null;
29 if (origin == null)
30 message = "nullValue.OriginIsNull";
31 else if (direction == null)
32 message = "nullValue.DirectionIsNull";
33 else if (direction.getLength3() <= 0)
34 message = "Geom.Line.DirectionIsZeroVector";
35 if (message != null)
37 message = Logging.getMessage(message);
38 Logging.logger().severe(message);
39 throw new IllegalArgumentException(message);
42 this.origin = origin;
43 this.direction = direction;
46 public final Vec4 getDirection()
48 return direction;
51 public final Vec4 getOrigin()
53 return origin;
56 public final Vec4 getPointAt(double t)
58 return Vec4.fromLine3(this.origin, t, this.direction);
61 public final double selfDot()
63 return this.origin.dot3(this.direction);
66 /**
67 * Performs a comparison to test whether this Object is internally identical to the other Object <code>o</code>.
68 * This method takes into account both direction and origin, so two lines which may be equivalent may not be
69 * considered equal.
71 * @param o the object to be compared against.
72 * @return true if these two objects are equal, false otherwise
74 @Override
75 public final boolean equals(Object o)
77 if (this == o)
78 return true;
79 if (o == null || getClass() != o.getClass())
80 return false;
82 final gov.nasa.worldwind.geom.Line line = (gov.nasa.worldwind.geom.Line) o;
84 if (!direction.equals(line.direction))
85 return false;
86 //noinspection RedundantIfStatement
87 if (!line.origin.equals(origin))
88 return false;
90 return true;
93 @Override
94 public final int hashCode()
96 int result;
97 result = origin.hashCode();
98 result = 29 * result + direction.hashCode();
99 return result;
102 public String toString()
104 return "Origin: " + this.origin + ", Direction: " + this.direction;
108 * Calculate the shortests distance between this line and a specified <code>Vec4</code>. This method returns a
109 * positive distance.
111 * @param p the <code>Vec4</code> whose distance from this <code>Line</code> will be calculated
112 * @return the distance between this <code>Line</code> and the specified <code>Vec4</code>
113 * @throws IllegalArgumentException if <code>p</code> is null
115 public final double distanceTo(Vec4 p)
117 if (p == null)
119 String message = Logging.getMessage("nullValue.PointIsNull");
120 Logging.logger().severe(message);
121 throw new IllegalArgumentException(message);
124 Vec4 origin = this.origin;
125 Vec4 sideB = origin.subtract3(p); // really a vector
127 double distanceToOrigin = sideB.dot3(this.direction);
128 double divisor = distanceToOrigin / this.direction.getLengthSquared3();
130 Vec4 sideA = this.direction.multiply3(divisor);
132 double aSquared = sideA.getLengthSquared3();
133 double bSquared = sideB.getLengthSquared3();
135 return Math.sqrt(bSquared - aSquared);