(no commit message)
[utils] / gps / src / main / java / org / wamblee / gps / geometry / Plane.java
1 /*
2  * Copyright 2006 the original author or authors.
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
17 package org.wamblee.gps.geometry;
18
19 import org.wamblee.general.Pair;
20
21 /**
22  * Represents a plane. Usually used to represent a tangent plane to the earth surface to
23  * locally approximate the earth as flat.  
24  *
25  * @author Erik Brakkee
26  */
27 public class Plane {
28     
29     private static final double EPS = 1e-4;
30     
31     private Coordinates _point; 
32     private Coordinates _normal; 
33     private Coordinates _north;
34     private Coordinates _east;
35
36     /**
37      * Constructs a plane. 
38      * @param aPoint Point on the plane. 
39      * @param aNormal Normal, not necessarily normalized. 
40      */
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);
47       
48         if (  _normal.innerProduct(_north) > EPS ) {
49             throw new IllegalArgumentException("North access is not within the plane");
50         }
51     }
52     
53     /**
54      * Projects a point onto the plane.  
55      * @param aPoint Point to project. 
56      * @return Projected point. 
57      */
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));
63     }
64     
65     /**
66      * Returns normalized coordinates within the plane of the projection of a point. 
67      */
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);
74     }
75 }