2 * Copyright 2006 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.wamblee.gps.geometry;
19 import org.wamblee.general.Pair;
22 * Represents a plane. Usually used to represent a tangent plane to the earth surface to
23 * locally approximate the earth as flat.
25 * @author Erik Brakkee
29 private static final double EPS = 1e-4;
31 private Coordinates _point;
32 private Coordinates _normal;
33 private Coordinates _north;
34 private Coordinates _east;
38 * @param aPoint Point on the plane.
39 * @param aNormal Normal, not necessarily normalized.
41 public Plane(Point aPoint, Point aNormal) {
42 _point = aPoint.getReferenceCoordinates();
43 _normal = aNormal.getReferenceCoordinates().normalize();
44 Coordinates north = new Coordinates(0.0, 0.0, 1.0);
45 _north = north.subtract(_normal.scale(north.innerProduct(_normal))).normalize();
46 _east = _north.outerProduct(_normal);
48 if ( _normal.innerProduct(_north) > EPS ) {
49 throw new IllegalArgumentException("North access is not within the plane");
54 * Projects a point onto the plane.
55 * @param aPoint Point to project.
56 * @return Projected point.
58 private Coordinates project(Point aPoint) {
59 Coordinates ref = aPoint.getReferenceCoordinates();
60 double lambda = _normal.innerProduct(
61 _point.subtract(ref));
62 return ref.add(_normal.scale(lambda));
66 * Returns normalized coordinates within the plane of the projection of a point.
68 public Pair<Double,Double> normalizedProjection(Point aPoint) {
69 Coordinates projection = project(aPoint);
70 Coordinates delta = projection.subtract(_point);
71 double x1 = delta.innerProduct(_north);
72 double x2 = delta.innerProduct(_east);
73 return new Pair<Double,Double>(x1, x2);