]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/utils/GridSpacing.java
Fixed all line endings of the repository
[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         if (Math.abs(viewboxSize) < GRID_MIN_USER_SIZE) viewboxSize = GRID_MIN_USER_SIZE * Math.signum(viewboxSize);
83
84         double unitsPerPixel = viewboxSize / Math.max(controlWidth, minPixels);
85         double [] gridSpacing = getGridSpacing(unitsPerPixel, minPixels);
86         return new GridSpacing(gridSpacing[0], gridSpacing[1], unitsPerPixel, minPixels);
87     }
88
89     public static GridSpacing makeGridSpacingForTime(
90                 double viewboxSize,  
91                 double controlWidth,  
92                 double minPixels)
93     {
94         if (controlWidth==0) controlWidth = 1;
95         if (viewboxSize < GRID_MIN_USER_SIZE) viewboxSize = 1;
96
97         double unitsPerPixel = viewboxSize / Math.max(controlWidth, minPixels);
98         double [] gridSpacing = getGridSpacing(unitsPerPixel, minPixels);
99         return new GridSpacing(gridSpacing[0], gridSpacing[1], unitsPerPixel, minPixels);
100     }
101     
102     private static double[] getGridSpacing(double unitsPerPixel, double minPixels) {
103         double minGridSize = minPixels * unitsPerPixel;
104         double[] gridSpacing = GridUtils.getEvenGridSpacingWithExp(minGridSize, new double[2]);
105         return gridSpacing;
106     }
107
108     /**
109      * Snap point to the grid. The snapping is based on grid last paint operation.
110      * 
111      * @param pos
112      * @return
113      */
114     public double snapToGrid(double pos) {
115         return GridUtils.snapToGrid(pos, segment);
116     }
117
118 }