Made the track serializable.
[utils] / gps / src / org / wamblee / gps / geometry / Coordinates.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 java.io.Serializable;
20
21 /**
22  * Coordinates in some 3-dimensional coordinate system.  
23  */
24 public class Coordinates implements Serializable {
25
26     private double _x1; 
27     private double _x2; 
28     private double _x3; 
29     
30     /**
31      * Constructs the coordinates. 
32      * @param aX1 First coordinate. 
33      * @param aX2 Second coordinate. 
34      * @param aX3 Third coordinate. 
35      */
36     public Coordinates(double aX1, double aX2, double aX3) { 
37         _x1 = aX1; 
38         _x2 = aX2;
39         _x3 = aX3; 
40     }
41     
42     public double getX1() { 
43         return _x1; 
44     }
45     
46     public double getX2() { 
47         return _x2; 
48     }
49     
50     public double getX3() { 
51         return _x3; 
52     }
53     
54     public double getX(int i) { 
55         switch (i) { 
56         case 1: return _x1; 
57         case 2: return _x2;
58         case 3: return _x3;
59         }
60         throw new IllegalArgumentException("coordinate out of range " + i);
61     }
62     
63     /* (non-Javadoc)
64      * @see java.lang.Object#toString()
65      */
66     @Override
67     public String toString() {
68         return "(" + getX1() + ", " + getX2() + ", " + getX3() + ")";
69     }
70     
71     public Coordinates add(Coordinates aC) { 
72         return new Coordinates(_x1 + aC._x1, _x2 + aC._x2, _x3 + aC._x3);
73     }
74     
75     public Coordinates subtract(Coordinates aC) { 
76         return new Coordinates(_x1 - aC._x1, _x2 - aC._x2, _x3 - aC._x3);
77     }
78   
79     public double innerProduct(Coordinates aC) { 
80         return _x1 * aC._x1 + _x2 * aC._x2 +  _x3 * aC._x3;
81     }
82     
83     public Coordinates outerProduct(Coordinates aC) { 
84         return new Coordinates(
85            _x2*aC._x3 - _x3*aC._x2,
86            -_x1*aC._x3 + _x3*aC._x1,
87            _x1*aC._x2 - _x2*aC._x1
88         );
89     }
90     
91     public double norm() { 
92         return Math.sqrt(innerProduct(this));
93     }
94     
95     public Coordinates scale(double aMultiplier) { 
96         return new Coordinates(_x1*aMultiplier, _x2*aMultiplier, _x3*aMultiplier);
97     }
98     
99     public Coordinates normalize() {
100         return scale(1.0/norm());
101     }
102 }