|
| 1 | +/* |
| 2 | + * Copyright 2018 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.google.common.geometry; |
| 17 | + |
| 18 | +import com.google.common.annotations.GwtCompatible; |
| 19 | +import com.google.common.base.Objects; |
| 20 | +import java.math.BigDecimal; |
| 21 | + |
| 22 | +/** A point consisting of BigDecimal coordinates. */ |
| 23 | +@GwtCompatible |
| 24 | +final strictfp class BigPoint implements Comparable<BigPoint> { |
| 25 | + final BigDecimal x; |
| 26 | + final BigDecimal y; |
| 27 | + final BigDecimal z; |
| 28 | + |
| 29 | + /** Creates a point of BigDecimal coordinates from a point of double coordinates. */ |
| 30 | + BigPoint(S2Point p) { |
| 31 | + this(Platform.newBigDecimal(p.x), Platform.newBigDecimal(p.y), Platform.newBigDecimal(p.z)); |
| 32 | + } |
| 33 | + |
| 34 | + /** Creates a point from the given BigDecimal coordinates. */ |
| 35 | + BigPoint(BigDecimal x, BigDecimal y, BigDecimal z) { |
| 36 | + this.x = x; |
| 37 | + this.y = y; |
| 38 | + this.z = z; |
| 39 | + } |
| 40 | + |
| 41 | + /** Returns an S2Point by rounding 'this' to double precision. */ |
| 42 | + S2Point toS2Point() { |
| 43 | + return new S2Point(x.doubleValue(), y.doubleValue(), z.doubleValue()); |
| 44 | + } |
| 45 | + |
| 46 | + /** Returns the vector cross product of 'this' with 'that'. */ |
| 47 | + BigPoint crossProd(BigPoint that) { |
| 48 | + return new BigPoint( |
| 49 | + y.multiply(that.z).subtract(z.multiply(that.y)), |
| 50 | + z.multiply(that.x).subtract(x.multiply(that.z)), |
| 51 | + x.multiply(that.y).subtract(y.multiply(that.x))); |
| 52 | + } |
| 53 | + |
| 54 | + /** Returns the vector dot product of 'this' with 'that'. */ |
| 55 | + BigDecimal dotProd(BigPoint that) { |
| 56 | + return x.multiply(that.x).add(y.multiply(that.y)).add(z.multiply(that.z)); |
| 57 | + } |
| 58 | + |
| 59 | + /** Returns the vector dot product of 'this' with 'that'. */ |
| 60 | + BigDecimal dotProd(S2Point that) { |
| 61 | + return dotProd(new BigPoint(that)); |
| 62 | + } |
| 63 | + |
| 64 | + /** Returns true iff this and 'p' are exactly parallel or anti-parallel. */ |
| 65 | + boolean isLinearlyDependent(BigPoint p) { |
| 66 | + BigPoint n = crossProd(p); |
| 67 | + return n.x.signum() == 0 && n.y.signum() == 0 && n.z.signum() == 0; |
| 68 | + } |
| 69 | + |
| 70 | + /** Returns true iff this and 'p' are exactly anti-parallel, antipodal points. */ |
| 71 | + boolean isAntipodal(BigPoint p) { |
| 72 | + return isLinearlyDependent(p) && dotProd(p).signum() < 0; |
| 73 | + } |
| 74 | + |
| 75 | + /** Returns the square of the magnitude of this vector. */ |
| 76 | + BigDecimal norm2() { |
| 77 | + return this.dotProd(this); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public int compareTo(BigPoint p) { |
| 82 | + int result = x.compareTo(p.x); |
| 83 | + if (result != 0) { |
| 84 | + return result; |
| 85 | + } |
| 86 | + result = y.compareTo(p.y); |
| 87 | + if (result != 0) { |
| 88 | + return result; |
| 89 | + } |
| 90 | + return z.compareTo(p.z); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public boolean equals(Object that) { |
| 95 | + if (!(that instanceof BigPoint)) { |
| 96 | + return false; |
| 97 | + } |
| 98 | + BigPoint thatPoint = (BigPoint) that; |
| 99 | + return x.equals(thatPoint.x) && y.equals(thatPoint.y) && z.equals(thatPoint.z); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public int hashCode() { |
| 104 | + return Objects.hashCode(x, y, z); |
| 105 | + } |
| 106 | +} |
0 commit comments