(no commit message)
[utils] / gps / src / 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 public class Plane {
26     
27     private static final double EPS = 1e-4;
28     
29     private Coordinates _point; 
30     private Coordinates _normal; 
31     private Coordinates _north;
32     private Coordinates _east;
33
34     /**
35      * Constructs a plane. 
36      * @param aPoint Point on the plane. 
37      * @param aNormal Normal, not necessarily normalized. 
38      */
39     public Plane(Point aPoint, Point aNormal) {
40         _point = aPoint.getReferenceCoordinates(); 
41         _normal = aNormal.getReferenceCoordinates().normalize();
42         Coordinates north = new Coordinates(0.0, 0.0, 1.0);
43         _north = north.subtract(_normal.scale(north.innerProduct(_normal))).normalize();
44         _east = _north.outerProduct(_normal);
45       
46         if (  _normal.innerProduct(_north) > EPS ) {
47             throw new IllegalArgumentException("North access is not within the plane");
48         }
49     }
50     
51     /**
52      * Projects a point onto the plane.  
53      * @param aPoint Point to project. 
54      * @return Projected point. 
55      */
56     private Coordinates project(Point aPoint) { 
57         Coordinates ref = aPoint.getReferenceCoordinates();
58         double lambda = _normal.innerProduct(
59                 _point.subtract(ref));
60         return ref.add(_normal.scale(lambda));
61     }
62     
63     /**
64      * Returns normalized coordinates within the plane of the projection of a point. 
65      */
66     public Pair<Double,Double> normalizedProjection(Point aPoint) { 
67         Coordinates projection = project(aPoint);
68         Coordinates delta = projection.subtract(_point);
69         double x1 = delta.innerProduct(_north);
70         double x2 = delta.innerProduct(_east);
71         return new Pair<Double,Double>(x1, x2);
72     }
73 }