2 Copyright (C) 2001, 2006 United States Government
3 as represented by the Administrator of the
4 National Aeronautics and Space Administration.
7 package gov
.nasa
.worldwind
.geom
;
9 import gov
.nasa
.worldwind
.util
.Logging
;
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
;
23 * @throws IllegalArgumentException if <code>origin</code> is null, or <code>direction</code> is null or has zero
26 public Line(Vec4 origin
, Vec4 direction
)
28 String message
= 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";
37 message
= Logging
.getMessage(message
);
38 Logging
.logger().severe(message
);
39 throw new IllegalArgumentException(message
);
43 this.direction
= direction
;
46 public final Vec4
getDirection()
51 public final Vec4
getOrigin()
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
);
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
71 * @param o the object to be compared against.
72 * @return true if these two objects are equal, false otherwise
75 public final boolean equals(Object o
)
79 if (o
== null || getClass() != o
.getClass())
82 final gov
.nasa
.worldwind
.geom
.Line line
= (gov
.nasa
.worldwind
.geom
.Line
) o
;
84 if (!direction
.equals(line
.direction
))
86 //noinspection RedundantIfStatement
87 if (!line
.origin
.equals(origin
))
94 public final int hashCode()
97 result
= origin
.hashCode();
98 result
= 29 * result
+ direction
.hashCode();
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
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
)
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
);