00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
00109 if (!element.getTagName().equals("result")) {
00110 throw new IllegalArgumentException("Invalid tag name: " + element.getTagName());
00111 }
00112
00113
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
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
00134 if ((type == Type.NOISE || type == Type.HOLDER) && rotation != 0) {
00135 throw new IllegalArgumentException("type = " + type + ", rotation = " + rotation);
00136 }
00137
00138
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
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
00180 if (rotation % 90 == 0) {
00181
00182 switch (rotation) {
00183 case 0:
00184
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
00201 Matrix3d rotate = new Matrix3d();
00202 rotate.rotZ(Math.toRadians(-rotation));
00203 rotate.transform(sampleVector);
00204 }
00205
00206 if (step != null) {
00207
00208 Vector3d noise = step.getNoise();
00209 sampleVector.sub(noise);
00210
00211
00212 if (step.getProject() != null && step.getProject().getOrientation() == Project.Orientation.MINUS_Z) {
00213 sampleVector.set(sampleVector.x, -sampleVector.y, -sampleVector.z);
00214 }
00215
00216
00217 Vector3d holder = step.getHolder();
00218 sampleVector.sub(holder);
00219 }
00220
00221
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 }