001package jmri.jmrix.rps;
002
003import javax.vecmath.Point3d;
004import org.slf4j.Logger;
005import org.slf4j.LoggerFactory;
006
007/**
008 * Some helpful implementations and values for Calculators.
009 *
010 * @author Bob Jacobsen Copyright (C) 2006, 2008
011 */
012public abstract class AbstractCalculator implements Calculator {
013
014    // Sensor position objects, fully packed array indexed from zero
015    Point3d sensors[];
016
017    // Values for usual calculators
018    // These are fully packed (start at index 0, no skipped entries)
019    double[] Tr;  // received time
020    double[] Xr;  // receiver X
021    double[] Yr;  // receiver Y
022    double[] Zr;  // receiver Z
023    int nr;
024
025    public void prep(Reading r) {
026
027        if (log.isDebugEnabled()) {
028            log.debug("Reading: {}", r.toString());
029            log.debug("Sensors: {}", sensors.length);
030            if (sensors.length >= 1 && sensors[0] != null) {
031                log.debug("Sensor[0]: {},{},{}", sensors[0].x, sensors[0].y, sensors[0].z);
032            }
033            if (sensors.length >= 2 && sensors[1] != null) {
034                log.debug("Sensor[1]: {},{},{}", sensors[1].x, sensors[1].y, sensors[1].z);
035            }
036        }
037
038        nr = r.getNValues();
039
040        // zero doesn't count in receivers
041        if (nr != sensors.length - 1) {
042            log.error("Mismatch: {} readings, {} receivers", nr, (sensors.length - 1));
043        }
044        nr = Math.min(nr, sensors.length - 1); // accept the shortest
045
046        Tr = new double[nr];
047        Xr = new double[nr];
048        Yr = new double[nr];
049        Zr = new double[nr];
050
051        // generate vector
052        int j = 0;
053        for (int i = 0; i <= nr; i++) {
054            if (sensors[i] != null) {
055                Tr[j] = r.getValue(i);
056                Xr[j] = sensors[i].x;
057                Yr[j] = sensors[i].y;
058                Zr[j] = sensors[i].z;
059                j++;
060            }
061        }
062        nr = j;
063
064        summarize();
065    }
066
067    void summarize() {
068        log.debug("nr is {}", nr);
069        for (int j = 0; j < nr; j++) {
070            log.debug(" t: {} to {},{},{}", Tr[j], Xr[j], Yr[j], Zr[j]);
071        }
072    }
073
074    private final static Logger log = LoggerFactory.getLogger(AbstractCalculator.class);
075
076}