|
| 1 | +/* |
| 2 | + * Copyright (C) 2011 United States Government as represented by the Administrator of the |
| 3 | + * National Aeronautics and Space Administration. |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +package gov.nasa.worldwind.geom; |
| 7 | + |
| 8 | +import java.util.*; |
| 9 | + |
| 10 | +/** |
| 11 | + * @author Tom Gaskins |
| 12 | + * @version $Id$ |
| 13 | + */ |
| 14 | +public final class Intersection // Instances are immutable |
| 15 | +{ |
| 16 | + protected Vec4 intersectionPoint; |
| 17 | + protected Position intersectionPosition; |
| 18 | + protected boolean isTangent; |
| 19 | + protected Object object; |
| 20 | + |
| 21 | + /** |
| 22 | + * Constructs an Intersection from an intersection point and tangency indicator. |
| 23 | + * |
| 24 | + * @param intersectionPoint the intersection point. |
| 25 | + * @param isTangent true if the intersection is tangent to the object intersected, otherwise false. |
| 26 | + * |
| 27 | + * @throws IllegalArgumentException if <code>intersectionPoint</code> is null |
| 28 | + */ |
| 29 | + public Intersection(Vec4 intersectionPoint, boolean isTangent) |
| 30 | + { |
| 31 | + if (intersectionPoint == null) |
| 32 | + { |
| 33 | + throw new IllegalArgumentException("Intersection Point Is Null"); |
| 34 | + } |
| 35 | + this.intersectionPoint = intersectionPoint; |
| 36 | + this.isTangent = isTangent; |
| 37 | + } |
| 38 | + |
| 39 | + public Intersection(Vec4 intersectionPoint, Position intersectionPosition, boolean isTangent, Object object) |
| 40 | + { |
| 41 | + if (intersectionPoint == null) |
| 42 | + { |
| 43 | + throw new IllegalArgumentException("Intersection Point Is Null"); |
| 44 | + } |
| 45 | + |
| 46 | + this.intersectionPoint = intersectionPoint; |
| 47 | + this.intersectionPosition = intersectionPosition; |
| 48 | + this.isTangent = isTangent; |
| 49 | + this.object = object; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Returns the intersection position if one has been set. |
| 54 | + * |
| 55 | + * @return the intersection position, or null if the position has not been set. |
| 56 | + */ |
| 57 | + public Position getIntersectionPosition() |
| 58 | + { |
| 59 | + return intersectionPosition; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Specifies the intersection position, which should be a position computed from the intersection point. |
| 64 | + * |
| 65 | + * @param intersectionPosition the intersection position. May be null. |
| 66 | + */ |
| 67 | + public void setIntersectionPosition(Position intersectionPosition) |
| 68 | + { |
| 69 | + this.intersectionPosition = intersectionPosition; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Returns the object associated with the intersection. |
| 74 | + * |
| 75 | + * @return the object associated with the intersection, or null if no object is associated. |
| 76 | + */ |
| 77 | + public Object getObject() |
| 78 | + { |
| 79 | + return object; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Specifies the object to associate with the intersection. |
| 84 | + * |
| 85 | + * @param object the object to associate with the intersection. May be null. |
| 86 | + */ |
| 87 | + public void setObject(Object object) |
| 88 | + { |
| 89 | + this.object = object; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Returns the intersection point. |
| 94 | + * |
| 95 | + * @return the intersection point. |
| 96 | + */ |
| 97 | + public Vec4 getIntersectionPoint() |
| 98 | + { |
| 99 | + return intersectionPoint; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Specifies the intersection point. |
| 104 | + * |
| 105 | + * @param intersectionPoint the intersection point. May be null, but typically should not be. |
| 106 | + */ |
| 107 | + public void setIntersectionPoint(Vec4 intersectionPoint) |
| 108 | + { |
| 109 | + this.intersectionPoint = intersectionPoint; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Indicates whether the intersection is tangent to the object intersected. |
| 114 | + * |
| 115 | + * @return true if the intersection is tangent, otherwise false. |
| 116 | + */ |
| 117 | + public boolean isTangent() |
| 118 | + { |
| 119 | + return isTangent; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Specifies whether the intersection is tangent to the object intersected. |
| 124 | + * |
| 125 | + * @param tangent true if the intersection is tangent, otherwise false. |
| 126 | + */ |
| 127 | + public void setTangent(boolean tangent) |
| 128 | + { |
| 129 | + isTangent = tangent; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Merges two lists of intersections into a single list sorted by intersection distance from a specified reference |
| 134 | + * point. |
| 135 | + * |
| 136 | + * @param refPoint the reference point. |
| 137 | + * @param listA the first list of intersections. |
| 138 | + * @param listB the second list of intersections. |
| 139 | + * |
| 140 | + * @return the merged list of intersections, sorted by increasing distance from the reference point. |
| 141 | + */ |
| 142 | + public static Queue<Intersection> sort(final Vec4 refPoint, List<Intersection> listA, List<Intersection> listB) |
| 143 | + { |
| 144 | + PriorityQueue<Intersection> sorted = new PriorityQueue<Intersection>(10, new Comparator<Intersection>() |
| 145 | + { |
| 146 | + public int compare(Intersection losiA, Intersection losiB) |
| 147 | + { |
| 148 | + if (losiA.intersectionPoint == null || losiB.intersectionPoint == null) |
| 149 | + return 0; |
| 150 | + |
| 151 | + double dA = refPoint.distanceTo3(losiA.intersectionPoint); |
| 152 | + double dB = refPoint.distanceTo3(losiB.intersectionPoint); |
| 153 | + |
| 154 | + return dA < dB ? -1 : dA == dB ? 0 : 1; |
| 155 | + } |
| 156 | + }); |
| 157 | + |
| 158 | + if (listA != null) |
| 159 | + { |
| 160 | + for (Intersection intersection : listA) |
| 161 | + { |
| 162 | + sorted.add(intersection); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + if (listB != null) |
| 167 | + { |
| 168 | + for (Intersection intersection : listB) |
| 169 | + { |
| 170 | + sorted.add(intersection); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + return sorted; |
| 175 | + } |
| 176 | + |
| 177 | + @Override |
| 178 | + public boolean equals(Object o) |
| 179 | + { |
| 180 | + if (this == o) |
| 181 | + return true; |
| 182 | + if (o == null || getClass() != o.getClass()) |
| 183 | + return false; |
| 184 | + |
| 185 | + final gov.nasa.worldwind.geom.Intersection that = (gov.nasa.worldwind.geom.Intersection) o; |
| 186 | + |
| 187 | + if (isTangent != that.isTangent) |
| 188 | + return false; |
| 189 | + //noinspection RedundantIfStatement |
| 190 | + if (!intersectionPoint.equals(that.intersectionPoint)) |
| 191 | + return false; |
| 192 | + |
| 193 | + return true; |
| 194 | + } |
| 195 | + |
| 196 | + @Override |
| 197 | + public int hashCode() |
| 198 | + { |
| 199 | + int result; |
| 200 | + result = intersectionPoint.hashCode(); |
| 201 | + result = 29 * result + (isTangent ? 1 : 0); |
| 202 | + return result; |
| 203 | + } |
| 204 | + |
| 205 | + @Override |
| 206 | + public String toString() |
| 207 | + { |
| 208 | + String pt = "Intersection Point: " + this.intersectionPoint; |
| 209 | + String tang = this.isTangent ? " is a tangent." : " not a tangent"; |
| 210 | + return pt + tang; |
| 211 | + } |
| 212 | +} |
0 commit comments