1 package org.opentrafficsim.graphs; 2 3 import org.opentrafficsim.core.value.vdouble.scalar.DoubleScalar; 4 5 /** 6 * Definition of one axis for a ContourPlot. 7 * <p> 8 * Copyright (c) 2013-2014 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br> 9 * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>. 10 * <p> 11 * @version Jul 28, 2014 <br> 12 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a> 13 */ 14 class Axis 15 { 16 /** Lowest value along this axis. */ 17 private final DoubleScalar<?> minimumValue; 18 19 /** Highest value along this axis. */ 20 private DoubleScalar<?> maximumValue; 21 22 /** Aggregation values along this axis (all values must be an integer multiple of the first value). */ 23 private final double[] granularities; 24 25 /** Current aggregation value (must be one of the values in granularities). */ 26 private double currentGranularity; 27 28 /** Name to describe the axis and to name the pop up menu that changes the current granularity. */ 29 private final String name; 30 31 /** Name to identify this axis. */ 32 private final String shortName; 33 34 /** Format for rendering a value along this axis. */ 35 private String format; 36 37 /** 38 * Create a new AxisDefinition. 39 * @param minimumValue DoubleScalar; the minimum value along this axis 40 * @param maximumValue DoubleScalar; the maximum value along this axis 41 * @param granularities double[]; the aggregation values along this axis (all values must be an integer multiple of the 42 * first value) 43 * @param initialGranularity double; initial aggregation value (must be one of the values in granularities) 44 * @param name String; the name to describe the axis and to name the pop up menu that changes the current granularity 45 * @param shortName String; the name identifying this axis for use in a menu 46 * @param format String; format string for rendering a value along this axis 47 */ 48 public Axis(final DoubleScalar<?> minimumValue, final DoubleScalar<?> maximumValue, final double[] granularities, 49 final double initialGranularity, final String name, final String shortName, final String format) 50 { 51 this.minimumValue = minimumValue; 52 this.setMaximumValue(maximumValue); 53 this.granularities = granularities; 54 if (null != granularities) 55 { 56 this.setCurrentGranularity(initialGranularity); 57 } 58 this.name = name; 59 this.shortName = shortName; 60 this.format = format; 61 } 62 63 /** 64 * Compute the floating point bin number for a value. 65 * @param value DoubleScalar; the value 66 * @return double; the bin that belongs to the value 67 */ 68 public double getRelativeBin(final DoubleScalar<?> value) 69 { 70 return (value.getSI() - this.getMinimumValue().getSI()) / this.getGranularities()[0]; 71 } 72 73 /** 74 * Adjust (increase) the range of this AxisDefinition. 75 * @param newMaximum DoubleScalar; the new maximum value of the axis 76 */ 77 public void adjustMaximumValue(final DoubleScalar<?> newMaximum) 78 { 79 // System.out.println("extending axis " + this.name + " from " + this.maximumValue + " to " + newMaximum); 80 this.setMaximumValue(newMaximum); 81 } 82 83 /** 84 * Return the value for an aggregated bin number. 85 * @param aggregatedBin Integer; the number of a bin 86 * @return Double; the value corresponding to the center of aggregateBin 87 */ 88 public double getValue(final int aggregatedBin) 89 { 90 return this.getMinimumValue().getSI() + 1.0 * aggregatedBin * this.getCurrentGranularity(); 91 } 92 93 /** 94 * @return Integer; the number of bins along this axis 95 */ 96 public int getAggregatedBinCount() 97 { 98 return (int) Math.ceil((this.getMaximumValue().getSI() - this.getMinimumValue().getSI()) 99 / this.getCurrentGranularity()); 100 } 101 102 /** 103 * @return Integer; the number of aggregated bins along this axis 104 */ 105 public int getBinCount() 106 { 107 return (int) Math.ceil((this.getMaximumValue().getSI() - this.getMinimumValue().getSI()) 108 / this.getGranularities()[0]); 109 } 110 111 /** 112 * Get the granularity of this axis. 113 * @return double; the granularity of this axis 114 */ 115 public double getCurrentGranularity() 116 { 117 return this.currentGranularity; 118 } 119 120 /** 121 * Change the granularity for this axis. <br/> 122 * The new value must be present in the granularities. 123 * @param newGranularity double; the new value for the granularity of this axis 124 */ 125 public void setCurrentGranularity(final double newGranularity) 126 { 127 for (double g : this.getGranularities()) 128 { 129 if (g == newGranularity) 130 { 131 this.currentGranularity = newGranularity; 132 return; 133 } 134 } 135 throw new Error("Illegal granularity " + newGranularity); 136 } 137 138 /** 139 * Get the maximum value of this axis. 140 * @return DoubleScalar; the current maximum value of this axis 141 */ 142 public DoubleScalar<?> getMaximumValue() 143 { 144 return this.maximumValue; 145 } 146 147 /** 148 * Change the maximum value of this axis. <br> 149 * The maximum value can only be increased. 150 * @param newMaximumValue DoubleScalar; the new maximum value of this axis 151 */ 152 public void setMaximumValue(final DoubleScalar<?> newMaximumValue) 153 { 154 if (null != this.maximumValue && newMaximumValue.getSI() < this.maximumValue.getSI()) 155 { 156 throw new Error("maximum value may not be decreased"); 157 } 158 this.maximumValue = newMaximumValue; 159 } 160 161 /** 162 * Get the minimum value of this axis. 163 * @return DoubleScalar; the minimum value of this axis 164 */ 165 public DoubleScalar<?> getMinimumValue() 166 { 167 return this.minimumValue; 168 } 169 170 /** 171 * Retrieve the possible granularities for this Axis. 172 * @return granularities 173 */ 174 public final double[] getGranularities() 175 { 176 return this.granularities; 177 } 178 179 /** 180 * Retrieve the format for displaying values along this Axis. 181 * @return format 182 */ 183 public String getFormat() 184 { 185 return this.format; 186 } 187 188 /** 189 * Retrieve the short name for this Axis. 190 * @return String; the short name for this Axis 191 */ 192 public String getShortName() 193 { 194 return this.shortName; 195 } 196 197 /** 198 * Retrieve the name of this Axis. 199 * @return String; the name of this Axis 200 */ 201 public String getName() 202 { 203 return this.name; 204 } 205 206 }