]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/utils/GridSpacing.java
Two rendering glitch fixes for time series charts
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / utils / GridSpacing.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.g2d.utils;
13
14 import java.io.Serializable;
15
16 import org.simantics.scenegraph.utils.GridUtils;
17
18
19 /**
20  * Grid configuration.
21  * 
22  * @author Tuukka Lehtonen
23  * @author Marko Luukkainen
24  * @author Toni Kalajainen
25  */
26 public final class GridSpacing implements Serializable {
27
28     private static final long serialVersionUID = 8445639004963828463L;
29
30     /** When the view scale gets beneath this value a grid should no longer be shown */
31     public static final double GRID_MIN_USER_SIZE = 1e-6;
32
33         public static final GridSpacing SOME_SPACING = GridSpacing.makeGridSpacing(100, 100, 40);
34     
35     /** The minimum amount of pixel between major grid lines. */
36     public double       minPixels;
37
38     /** Grid segment sizes (canvas coordinates) */
39     public double               segment, segmentExp;
40
41     /** Conversions between canvas (unit) and control (pixel) */
42     public double               pixelsPerUnit;
43     public double       unitsPerPixel;
44
45     public double               pixelsPerSegment;
46     
47     public GridSpacing(double spacing, double spacingExp, double unitsPerPixel, double minPixels)
48     {
49         this.unitsPerPixel = unitsPerPixel;
50         pixelsPerUnit = 1 / unitsPerPixel;
51         this.segment = spacing; 
52         this.segmentExp = spacingExp;
53         pixelsPerSegment = spacing * pixelsPerUnit;
54         this.minPixels = minPixels;
55     }
56
57     @Override
58     public boolean equals(Object obj) {
59         if (obj == null) return false;
60         if (obj == this) return true;
61         if (obj instanceof GridSpacing == false) return false;
62         GridSpacing g2 = (GridSpacing)obj;
63         if(pixelsPerUnit != g2.pixelsPerUnit) return false;
64         if(pixelsPerSegment != g2.pixelsPerSegment) return false;
65         if(unitsPerPixel != g2.unitsPerPixel) return false;
66         if(segment != g2.segment) return false;
67         if(segmentExp != g2.segmentExp) return false;
68         return true;
69     }
70
71     /**
72      * Calculates grid cofiguration
73      * 
74      * @return grid configuration 
75      */
76     public static GridSpacing makeGridSpacing(
77                 double viewboxSize,  
78                 double controlWidth,  
79                 double minPixels)
80     {
81         if (controlWidth==0) controlWidth = 1;
82         // This prevents clients from getting the proper GridSpacing information.
83         //if (Math.abs(viewboxSize) < GRID_MIN_USER_SIZE) viewboxSize = GRID_MIN_USER_SIZE * Math.signum(viewboxSize);
84
85         double unitsPerPixel = viewboxSize / Math.max(controlWidth, minPixels);
86         double [] gridSpacing = getGridSpacing(unitsPerPixel, minPixels);
87         return new GridSpacing(gridSpacing[0], gridSpacing[1], unitsPerPixel, minPixels);
88     }
89
90     public static GridSpacing makeGridSpacingForTime(
91                 double viewboxSize,  
92                 double controlWidth,  
93                 double minPixels)
94     {
95         if (controlWidth==0) controlWidth = 1;
96         if (viewboxSize < GRID_MIN_USER_SIZE) viewboxSize = 1;
97
98         double unitsPerPixel = viewboxSize / Math.max(controlWidth, minPixels);
99         double [] gridSpacing = getGridSpacing(unitsPerPixel, minPixels);
100         return new GridSpacing(gridSpacing[0], gridSpacing[1], unitsPerPixel, minPixels);
101     }
102     
103     private static double[] getGridSpacing(double unitsPerPixel, double minPixels) {
104         double minGridSize = minPixels * unitsPerPixel;
105         double[] gridSpacing = GridUtils.getEvenGridSpacingWithExp(minGridSize, new double[2]);
106         return gridSpacing;
107     }
108
109     /**
110      * Snap point to the grid. The snapping is based on grid last paint operation.
111      * 
112      * @param pos
113      * @return
114      */
115     public double snapToGrid(double pos) {
116         return GridUtils.snapToGrid(pos, segment);
117     }
118
119     @Override
120     public String toString() {
121         return String.format("%s [minPixels=%.15f, segment=%.15f, segmentExp=%.15f, pixelsPerUnit=%.15f, unitsPerPixel=%.15f, pixelsPerSegment=%.15f]",
122                 getClass().getName(),
123                 minPixels,
124                 segment,
125                 segmentExp,
126                 pixelsPerUnit,
127                 unitsPerPixel,
128                 pixelsPerSegment);
129     }
130
131 }