1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2009-2011 - DIGITEO - Pierre Lando
4  * Copyright (C) 2013 - Scilab Enterprises - Calixte DENIZET
5  *
6  * Copyright (C) 2012 - 2016 - Scilab Enterprises
7  *
8  * This file is hereby licensed under the terms of the GNU GPL v2.0,
9  * pursuant to article 5.3.4 of the CeCILL v.2.1.
10  * This file was originally licensed under the terms of the CeCILL v2.1,
11  * and continues to be available under such terms.
12  * For more information, see the COPYING file which you should have received
13  * along with this program.
14  */
15 
16 package org.scilab.forge.scirenderer.ruler;
17 
18 import org.scilab.forge.scirenderer.ruler.graduations.Graduations;
19 import org.scilab.forge.scirenderer.ruler.graduations.LinearGraduations;
20 import org.scilab.forge.scirenderer.ruler.graduations.LogarithmicGraduations;
21 import org.scilab.forge.scirenderer.shapes.appearance.Color;
22 import org.scilab.forge.scirenderer.tranformations.Vector3d;
23 
24 /**
25  * Default ruler model.
26  *
27  * @author Pierre Lando
28  */
29 public final class DefaultRulerModel implements RulerModel {
30 
31     /**
32      * Value associated with the first point.
33      */
34     private double firstValue = DEFAULT_FIRST_VALUE;
35 
36     /**
37      * Value associated with the second point.
38      */
39     private double secondValue = DEFAULT_SECOND_VALUE;
40 
41     /**
42      * First point position in world coordinate.
43      */
44     private Vector3d firstPoint = DEFAULT_FIRST_POINT;
45 
46     /**
47      * Second point position in world coordinate.
48      */
49     private Vector3d secondPoint = DEFAULT_SECOND_POINT;
50 
51     /**
52      * Ticks direction, in world coordinate.
53      */
54     private Vector3d ticksDirection = DEFAULT_TICKS_DIRECTION;
55 
56     /**
57      * Ticks length, in pixel.
58      */
59     private int ticksLength = DEFAULT_TICK_LENGTH;
60 
61     /**
62      * Sub ticks length, in pixel.
63      */
64     private int subTicksLength = DEFAULT_SUB_TICK_LENGTH;
65 
66     /**
67      * Minimal sub-ticks distance.
68      */
69     private double minimalSubTicksDistance = DEFAULT_MINIMAL_SUB_TICKS_DISTANCE;
70 
71     /**
72      * Sprite distance to segment, in pixel.
73 
74      */
75     private int spriteDistance = DEFAULT_SPRITE_DISTANCE;
76 
77     /**
78      * The graduations.
79      */
80     private Graduations graduations;
81 
82     /**
83      * Current margin.
84      * The margin is the minimal allowed distance between to sprite of the same ruler.
85      */
86     private double margin = DEFAULT_MARGIN;
87 
88     /**
89      * Current line visibility.
90      */
91     private boolean lineVisible = DEFAULT_LINE_VISIBLE;
92 
93     /**
94      * Auto-ticks status.
95      */
96     private boolean isAutoTicks = DEFAULT_AUTO_TICKS_STATUS;
97 
98     /**
99      * Logarithmic status.
100      */
101     private boolean isLogarithmic = DEFAULT_LOGARITHMIC_STATUS;
102 
103     /**
104      * Current color.
105      */
106     private Color color = DEFAULT_COLOR;
107 
108     /**
109      * Used graduations when {@link this#isAutoTicks} is false.
110      */
111     private Graduations userGraduation;
112 
113     /**
114      * Line width
115      */
116     private double lineWidth = DEFAULT_LINE_WIDTH;
117 
118     private int subticksNumber = -1;
119     private String format = "";
120     private double scale;
121     private double translate;
122 
123     /**
124      * Default constructor.
125      */
DefaultRulerModel()126     public DefaultRulerModel() {
127     }
128 
129     @Override
getFirstValue()130     public double getFirstValue() {
131         return firstValue;
132     }
133 
134     /**
135      * First value setter.
136      * @param firstValue the new first value.
137      */
setFirstValue(double firstValue)138     public void setFirstValue(double firstValue) {
139         graduations = null;
140         this.firstValue = (Double.isInfinite(firstValue) ? Double.MAX_VALUE : firstValue);
141     }
142 
143     @Override
getSecondValue()144     public double getSecondValue() {
145         return secondValue;
146     }
147 
148     /**
149      * Second value setter.
150      * @param secondValue the new second value.
151      */
setSecondValue(double secondValue)152     public void setSecondValue(double secondValue) {
153         graduations = null;
154         this.secondValue = (Double.isInfinite(secondValue) ? Double.MAX_VALUE : secondValue);
155     }
156 
157     /**
158      * Set the first and second value in one call.
159      * @param firstValue the first value.
160      * @param secondValue the second value.
161      */
setValues(double firstValue, double secondValue)162     public void setValues(double firstValue, double secondValue) {
163         setFirstValue(firstValue);
164         setSecondValue(secondValue);
165     }
166 
167     @Override
getFirstPoint()168     public Vector3d getFirstPoint() {
169         return firstPoint;
170     }
171 
172     /**
173      * First point setter.
174      * @param firstPoint the new first point.
175      */
setFirstPoint(Vector3d firstPoint)176     public void setFirstPoint(Vector3d firstPoint) {
177         this.firstPoint = firstPoint;
178     }
179 
180     @Override
getSecondPoint()181     public Vector3d getSecondPoint() {
182         return secondPoint;
183     }
184 
185     /**
186      * Second point setter.
187      * @param secondPoint the new second point.
188      */
setSecondPoint(Vector3d secondPoint)189     public void setSecondPoint(Vector3d secondPoint) {
190         this.secondPoint = secondPoint;
191     }
192 
193     /**
194      * Point setter.
195      * @param firstPoint the new first point.
196      * @param secondPoint the new second point.
197      */
setPoints(Vector3d firstPoint, Vector3d secondPoint)198     public void setPoints(Vector3d firstPoint, Vector3d secondPoint) {
199         this.firstPoint = firstPoint;
200         this.secondPoint = secondPoint;
201     }
202 
203     @Override
getTicksDirection()204     public Vector3d getTicksDirection() {
205         return ticksDirection;
206     }
207 
208     /**
209      * Ticks direction setter.
210      * @param ticksDirection the new ticks direction.
211      */
setTicksDirection(Vector3d ticksDirection)212     public void setTicksDirection(Vector3d ticksDirection) {
213         this.ticksDirection = ticksDirection;
214     }
215 
216     @Override
getTicksLength()217     public int getTicksLength() {
218         return ticksLength;
219     }
220 
221     /**
222      * Ticks length setter.
223      * @param ticksLength the new tick length in pixels.
224      */
setTicksLength(int ticksLength)225     public void setTicksLength(int ticksLength) {
226         this.ticksLength = ticksLength;
227     }
228 
229     @Override
getSubTicksLength()230     public int getSubTicksLength() {
231         return subTicksLength;
232     }
233 
234     /**
235      * Sub-ticks length setter.
236      * @param subTicksLength the new sub-tick length in pixels.
237      */
setSubTicksLength(int subTicksLength)238     public void setSubTicksLength(int subTicksLength) {
239         this.subTicksLength = subTicksLength;
240     }
241 
242     @Override
getGraduations()243     public Graduations getGraduations() {
244         if (isAutoTicks) {
245             if (graduations == null) {
246                 if (isLogarithmic) {
247                     graduations = LogarithmicGraduations.create(getFirstValue(), getSecondValue());
248                 } else {
249                     graduations = LinearGraduations.create(getFirstValue(), getSecondValue());
250                 }
251             }
252             return graduations;
253         } else {
254             return userGraduation;
255         }
256     }
257 
258     /**
259      * User graduation setter.
260      * @param graduations the new user-defined graduations.
261      */
setUserGraduation(Graduations graduations)262     public void setUserGraduation(Graduations graduations) {
263         this.userGraduation = graduations;
264     }
265 
266     @Override
getPosition(double value)267     public Vector3d getPosition(double value) {
268         if ((firstPoint != null) && (secondPoint != null)) {
269             if (isLogarithmic()) {
270                 double valueLog = Math.log10(value);
271                 double firstValueLog = Math.log10(firstValue);
272                 double secondValueLog = Math.log10(secondValue);
273                 double s = (secondValueLog - firstValueLog);
274                 double k1 = (valueLog - firstValueLog) / s;
275                 double k2 = (secondValueLog - valueLog) / s;
276                 return firstPoint.times(k2).plus(secondPoint.times(k1));
277             } else {
278                 double s = (secondValue - firstValue);
279                 return firstPoint.times((secondValue - value) / s).plus(secondPoint.times((value - firstValue) / s));
280             }
281         } else {
282             return null;
283         }
284     }
285 
286     @Override
getMargin()287     public double getMargin() {
288         return margin;
289     }
290 
291     /**
292      * Margin setter.
293      * The margin is minimal distance accepted between ticks labels.
294      * @param margin the new margin value.
295      */
setMargin(double margin)296     public void setMargin(double margin) {
297         this.margin = margin;
298     }
299 
300     @Override
isLineVisible()301     public boolean isLineVisible() {
302         return lineVisible;
303     }
304 
305     /**
306      * Line visibility setter.
307      * @param lineVisible the new line visibility status.
308      */
setLineVisible(boolean lineVisible)309     public void setLineVisible(boolean lineVisible) {
310         this.lineVisible = lineVisible;
311     }
312 
313     @Override
isAutoTicks()314     public boolean isAutoTicks() {
315         return isAutoTicks;
316     }
317 
318     /**
319      * Auto-ticks parameter setter.
320      * If it set to {@code false}, the user defined graduation will be used.
321      * @param isAutoTicks the new auto-ticks status.
322      */
setAutoTicks(boolean isAutoTicks)323     public void setAutoTicks(boolean isAutoTicks) {
324         this.isAutoTicks = isAutoTicks;
325     }
326 
327     @Override
isLogarithmic()328     public boolean isLogarithmic() {
329         return isLogarithmic;
330     }
331 
332     /**
333      * Logarithmic or linear setter
334      * @param isLogarithmic the new logarithmic status.
335      */
setLogarithmic(boolean isLogarithmic)336     public void setLogarithmic(boolean isLogarithmic) {
337         if (isLogarithmic != this.isLogarithmic) {
338             this.isLogarithmic = isLogarithmic;
339             graduations = null;
340         }
341     }
342 
343     @Override
getSpriteDistance()344     public int getSpriteDistance() {
345         return spriteDistance;
346     }
347 
348     /**
349      * Sprite distance setter.
350      * @param spriteDistance the new sprite distance to the main ticks in pixel.
351      */
setSpriteDistance(int spriteDistance)352     public void setSpriteDistance(int spriteDistance) {
353         this.spriteDistance = spriteDistance;
354     }
355 
356     @Override
getMinimalSubTicksDistance()357     public double getMinimalSubTicksDistance() {
358         return minimalSubTicksDistance;
359     }
360 
361     /**
362      * Minimal accepted distance between sub-ticks setter.
363      * @param minimalSubTicksDistance the new minimal accepted distance between sub-ticks.
364      */
setMinimalSubTicksDistance(double minimalSubTicksDistance)365     public void setMinimalSubTicksDistance(double minimalSubTicksDistance) {
366         this.minimalSubTicksDistance = minimalSubTicksDistance;
367     }
368 
369     @Override
getColor()370     public Color getColor() {
371         return color;
372     }
373 
374     /**
375      * Ruler color setter.
376      * @param color the new rule color.
377      */
setColor(Color color)378     public void setColor(Color color) {
379         this.color = color;
380     }
381 
382     @Override
getLineWidth()383     public double getLineWidth() {
384         return lineWidth;
385     }
386 
387     /**
388      * Ruler line width setter
389      * @param lineWidth the new line width
390      */
setLineWidth(double lineWidth)391     public void setLineWidth(double lineWidth) {
392         this.lineWidth = lineWidth;
393     }
394 
395     @Override
getSubticksNumber()396     public int getSubticksNumber() {
397         return subticksNumber;
398     }
399 
400     /**
401      * Set number of subticks.
402      * @param N the number of subticks or -1 if the computation is automatic.
403      */
setSubticksNumber(int N)404     public void setSubticksNumber(int N) {
405         this.subticksNumber = N;
406     }
407 
408     @Override
getFormat()409     public String getFormat() {
410         return format;
411     }
412 
413     /**
414      * Set the format.
415      * @param format the format as used in String.format.
416      */
setFormat(String format)417     public void setFormat(String format) {
418         this.format = format;
419     }
420 
421     @Override
getScale()422     public double getScale() {
423         return scale;
424     }
425 
426     @Override
getTranslate()427     public double getTranslate() {
428         return translate;
429     }
430 
431     /**
432      * Set the scale-translate factors.
433      * @param .
434      */
setSTFactors(Double[] factors)435     public void setSTFactors(Double[] factors) {
436         this.scale = factors[0];
437         this.translate = factors[1];
438     }
439 }
440