001package jmri.jmrix.rps;
002
003import javax.vecmath.Point3d;
004import javax.vecmath.Vector3d;
005
006/**
007 * Encodes a single measurement point for RPS.
008 * <p>
009 * Immutable
010 *
011 * @author Bob Jacobsen Copyright (C) 2006
012 */
013public class Measurement {
014
015    public Measurement(Reading r) {
016        this.r = r;
017    }
018
019    public Measurement(Reading r, double x, double y, double z, double vsound, int code, String source) {
020        this(r);
021        this.x = x;
022        this.y = y;
023        this.z = z;
024        this.vsound = vsound;
025        this.code = code;
026        this.source = source;
027    }
028
029    /**
030     * Return the Reading this measurement made from.
031     * <p>
032     * By definition, Reading objects are immutable
033     *
034     * @return the reading.
035     */
036    public Reading getReading() {
037        return r;
038    }
039
040    /**
041     * Return the ID int of the transmitter this measurement describes.
042     * @return transmitter ID.
043     */
044    public String getId() {
045        if (r == null) {
046            return "<none>";
047        }
048        return r.getId();
049    }
050
051    public double getX() {
052        return x;
053    }
054
055    public double getY() {
056        return y;
057    }
058
059    public double getZ() {
060        return z;
061    }
062
063    public double getVSound() {
064        return vsound;
065    }
066
067    boolean valid = true;
068
069    public boolean isValidPosition() {
070        if (!valid) {
071            return false;
072        }
073        return !(Math.abs(x) > 1.E10 || Math.abs(y) > 1.E10 || Math.abs(z) > 1.E10);
074    }
075
076    public void setValidPosition(boolean val) {
077        valid = val;
078    }
079
080    /**
081     * Error code, defined specifically by generator.
082     * @return error code.
083     */
084    public int getCode() {
085        return code;
086    }
087
088    /**
089     * Should this be considered a valid measurement?
090     * @return if getCode greater 0.
091     */
092    public boolean isOkPoint() {
093        if (getCode() > 0) {
094            return true;
095        }
096        return false;
097    }
098
099    /**
100     * Get the error code as a human-readable string.
101     * @return readable error code.
102     */
103    public String textCode() {
104        return "" + getCode();
105    }
106
107    public Point3d getPoint() {
108        return new Point3d(x, y, z);
109    }
110
111    public Vector3d getVector() {
112        return new Vector3d(x, y, z);
113    }
114
115    /**
116     * Get name of the source.
117     * @return source name.
118     */
119    public String getSource() {
120        return source;
121    }
122
123    double x, y, z, vsound;
124    protected int code;
125    String source;
126
127    Reading r;  // a Reading object is by definition immutable
128
129    @Override
130    public String toString() {
131        if (!isValidPosition()) {
132            // out-of-range
133            return "Measurement id=" + getId() + " invalid position";
134        }
135        return "Measurement id=" + getId() + " position= "
136                + truncate(x) + ", " + truncate(y) + ", " + truncate(z);
137    }
138
139    // provide a quick decimal truncation for formatting
140    double truncate(double x) {
141        return (int) Math.round(x * 10) / 10.;
142    }
143
144}