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