Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

MeasurementResult.java

Go to the documentation of this file.
00001 /*
00002  * MeasurementResult.java
00003  *
00004  * Copyright (C) 2005 Project SQUID, http://www.cs.helsinki.fi/group/squid/
00005  *
00006  * This file is part of Ikayaki.
00007  *
00008  * Ikayaki is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * Ikayaki is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with Ikayaki; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00021  */
00022 
00023 package ikayaki;
00024 
00025 import org.w3c.dom.Document;
00026 import org.w3c.dom.Element;
00027 
00028 import javax.vecmath.Matrix3d;
00029 import javax.vecmath.Vector3d;
00030 
00039 public class MeasurementResult {
00040 
00044     private final Type type;
00045 
00049     private final int rotation;
00050 
00054     private final Vector3d rawVector = new Vector3d();
00055 
00059     private final Vector3d sampleVector = new Vector3d();
00060 
00065     private final Vector3d geographicVector = new Vector3d();
00066 
00080     public MeasurementResult(Type type, int rotation, double x, double y, double z) {
00081         if (type == null) {
00082             throw new NullPointerException();
00083         }
00084         if ((type == Type.NOISE || type == Type.HOLDER) && rotation != 0) {
00085             throw new IllegalArgumentException("type = " + type + ", rotation = " + rotation);
00086         }
00087         this.type = type;
00088         this.rotation = rotation % 360;
00089         rawVector.set(x, y, z);
00090         applyFixes(null);
00091         setTransform(null);
00092     }
00093 
00103     public MeasurementResult(Element element) {
00104         if (element == null) {
00105             throw new NullPointerException();
00106         }
00107 
00108         // verify tag name
00109         if (!element.getTagName().equals("result")) {
00110             throw new IllegalArgumentException("Invalid tag name: " + element.getTagName());
00111         }
00112 
00113         // get type
00114         String s = element.getAttribute("type");
00115         try {
00116             this.type = Type.valueOf(s);
00117         } catch (IllegalArgumentException e) {
00118             throw new IllegalArgumentException("Invalid type: " + s, e);
00119         }
00120 
00121         // get rotation
00122         s = element.getAttribute("rotation");
00123         try {
00124             int rotation = Integer.parseInt(s) % 360;
00125             while (rotation < 0) {
00126                 rotation += 360;
00127             }
00128             this.rotation = rotation;
00129         } catch (NumberFormatException e) {
00130             throw new IllegalArgumentException("Invalid rotation: " + s, e);
00131         }
00132 
00133         // verify type and rotation
00134         if ((type == Type.NOISE || type == Type.HOLDER) && rotation != 0) {
00135             throw new IllegalArgumentException("type = " + type + ", rotation = " + rotation);
00136         }
00137 
00138         // get x, y, z
00139         try {
00140             rawVector.set(Double.parseDouble(element.getAttribute("x")),
00141                     Double.parseDouble(element.getAttribute("y")),
00142                     Double.parseDouble(element.getAttribute("z")));
00143         } catch (NumberFormatException e) {
00144             throw new IllegalArgumentException("Invalid x, y or z: " + e.getMessage(), e);
00145         }
00146 
00147         // initialize sampleVector and geographicVector
00148         applyFixes(null);
00149         setTransform(null);
00150     }
00151 
00157     public Element getElement(Document document) {
00158         Element element = document.createElement("result");
00159 
00160         element.setAttribute("type", type.name());
00161         element.setAttribute("rotation", Integer.toString(rotation));
00162         element.setAttribute("x", Double.toString(rawVector.x));
00163         element.setAttribute("y", Double.toString(rawVector.y));
00164         element.setAttribute("z", Double.toString(rawVector.z));
00165 
00166         return element;
00167     }
00168 
00176     protected void applyFixes(MeasurementStep step) {
00177         sampleVector.set(rawVector);
00178 
00179         // apply rotation fix
00180         if (rotation % 90 == 0) {
00181             // accurate and fast algorithm for trivial angles
00182             switch (rotation) {
00183             case 0:
00184                 // NO NEED TO ROTATE
00185                 break;
00186             case 90:
00187                 sampleVector.set(sampleVector.y, -sampleVector.x, sampleVector.z);
00188                 break;
00189             case 180:
00190                 sampleVector.set(-sampleVector.x, -sampleVector.y, sampleVector.z);
00191                 break;
00192             case 270:
00193                 sampleVector.set(-sampleVector.y, sampleVector.x, sampleVector.z);
00194                 break;
00195             default:
00196                 assert false;
00197                 throw new IllegalStateException("rotation = " + rotation);
00198             }
00199         } else {
00200             // rotate all non-trivial angles by using a matrix
00201             Matrix3d rotate = new Matrix3d();
00202             rotate.rotZ(Math.toRadians(-rotation));
00203             rotate.transform(sampleVector);
00204         }
00205 
00206         if (step != null) {
00207             // apply the noise fix
00208             Vector3d noise = step.getNoise();
00209             sampleVector.sub(noise);
00210 
00211             // apply the +/-Z fix
00212             if (step.getProject() != null && step.getProject().getOrientation() == Project.Orientation.MINUS_Z) {
00213                 sampleVector.set(sampleVector.x, -sampleVector.y, -sampleVector.z);
00214             }
00215 
00216             // apply the sample holder fix
00217             Vector3d holder = step.getHolder();     // will be zero, if this project is the holder calibration project
00218             sampleVector.sub(holder);
00219         }
00220 
00221         // reset geographic vector
00222         setTransform(null);
00223     }
00224 
00231     protected void setTransform(Matrix3d transform) {
00232         if (transform != null) {
00233             transform.transform(sampleVector, geographicVector);
00234         } else {
00235             geographicVector.set(sampleVector);
00236         }
00237     }
00238 
00242     public Type getType() {
00243         return type;
00244     }
00245 
00249     public int getRotation() {
00250         return rotation;
00251     }
00252 
00260     public double getGeographicX() {
00261         if (type != Type.SAMPLE) {
00262             throw new IllegalStateException();
00263         }
00264         return geographicVector.x;
00265     }
00266 
00274     public double getGeographicY() {
00275         if (type != Type.SAMPLE) {
00276             throw new IllegalStateException();
00277         }
00278         return geographicVector.y;
00279     }
00280 
00288     public double getGeographicZ() {
00289         if (type != Type.SAMPLE) {
00290             throw new IllegalStateException();
00291         }
00292         return geographicVector.z;
00293     }
00294 
00299     protected Vector3d getGeographicVector() {
00300         return geographicVector;
00301     }
00302 
00306     public double getSampleX() {
00307         return sampleVector.x;
00308     }
00309 
00313     public double getSampleY() {
00314         return sampleVector.y;
00315     }
00316 
00320     public double getSampleZ() {
00321         return sampleVector.z;
00322     }
00323 
00328     protected Vector3d getSampleVector() {
00329         return sampleVector;
00330     }
00331 
00335     public double getRawX() {
00336         return rawVector.x;
00337     }
00338 
00342     public double getRawY() {
00343         return rawVector.y;
00344     }
00345 
00349     public double getRawZ() {
00350         return rawVector.z;
00351     }
00352 
00357     protected Vector3d getRawVector() {
00358         return rawVector;
00359     }
00360 
00361     public enum Type {
00362         SAMPLE, HOLDER, NOISE
00363     }
00364 }

Generated on Fri May 6 16:00:32 2005 for Squid by  doxygen 1.4.1