1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.g2d.utils;
14 import java.io.Serializable;
16 import org.simantics.scenegraph.utils.GridUtils;
22 * @author Tuukka Lehtonen
23 * @author Marko Luukkainen
24 * @author Toni Kalajainen
26 public final class GridSpacing implements Serializable {
28 private static final long serialVersionUID = 8445639004963828463L;
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;
33 public static final GridSpacing SOME_SPACING = GridSpacing.makeGridSpacing(100, 100, 40);
35 /** The minimum amount of pixel between major grid lines. */
36 public double minPixels;
38 /** Grid segment sizes (canvas coordinates) */
39 public double segment, segmentExp;
41 /** Conversions between canvas (unit) and control (pixel) */
42 public double pixelsPerUnit;
43 public double unitsPerPixel;
45 public double pixelsPerSegment;
47 public GridSpacing(double spacing, double spacingExp, double unitsPerPixel, double minPixels)
49 this.unitsPerPixel = unitsPerPixel;
50 pixelsPerUnit = 1 / unitsPerPixel;
51 this.segment = spacing;
52 this.segmentExp = spacingExp;
53 pixelsPerSegment = spacing * pixelsPerUnit;
54 this.minPixels = minPixels;
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;
72 * Calculates grid cofiguration
74 * @return grid configuration
76 public static GridSpacing makeGridSpacing(
81 if (controlWidth==0) controlWidth = 1;
82 if (Math.abs(viewboxSize) < GRID_MIN_USER_SIZE) viewboxSize = GRID_MIN_USER_SIZE * Math.signum(viewboxSize);
84 double unitsPerPixel = viewboxSize / Math.max(controlWidth, minPixels);
85 double [] gridSpacing = getGridSpacing(unitsPerPixel, minPixels);
86 return new GridSpacing(gridSpacing[0], gridSpacing[1], unitsPerPixel, minPixels);
89 public static GridSpacing makeGridSpacingForTime(
94 if (controlWidth==0) controlWidth = 1;
95 if (viewboxSize < GRID_MIN_USER_SIZE) viewboxSize = 1;
97 double unitsPerPixel = viewboxSize / Math.max(controlWidth, minPixels);
98 double [] gridSpacing = getGridSpacing(unitsPerPixel, minPixels);
99 return new GridSpacing(gridSpacing[0], gridSpacing[1], unitsPerPixel, minPixels);
102 private static double[] getGridSpacing(double unitsPerPixel, double minPixels) {
103 double minGridSize = minPixels * unitsPerPixel;
104 double[] gridSpacing = GridUtils.getEvenGridSpacingWithExp(minGridSize, new double[2]);
109 * Snap point to the grid. The snapping is based on grid last paint operation.
114 public double snapToGrid(double pos) {
115 return GridUtils.snapToGrid(pos, segment);