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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.g2d.utils;
\r
14 import java.io.Serializable;
\r
16 import org.simantics.scenegraph.utils.GridUtils;
\r
20 * Grid configuration.
\r
22 * @author Tuukka Lehtonen
\r
23 * @author Marko Luukkainen
\r
24 * @author Toni Kalajainen
\r
26 public final class GridSpacing implements Serializable {
\r
28 private static final long serialVersionUID = 8445639004963828463L;
\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
33 public static final GridSpacing SOME_SPACING = GridSpacing.makeGridSpacing(100, 100, 40);
\r
35 /** The minimum amount of pixel between major grid lines. */
\r
36 public double minPixels;
\r
38 /** Grid segment sizes (canvas coordinates) */
\r
39 public double segment, segmentExp;
\r
41 /** Conversions between canvas (unit) and control (pixel) */
\r
42 public double pixelsPerUnit;
\r
43 public double unitsPerPixel;
\r
45 public double pixelsPerSegment;
\r
47 public GridSpacing(double spacing, double spacingExp, double unitsPerPixel, double minPixels)
\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
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
72 * Calculates grid cofiguration
\r
74 * @return grid configuration
\r
76 public static GridSpacing makeGridSpacing(
\r
77 double viewboxSize,
\r
78 double controlWidth,
\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
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
89 public static GridSpacing makeGridSpacingForTime(
\r
90 double viewboxSize,
\r
91 double controlWidth,
\r
94 if (controlWidth==0) controlWidth = 1;
\r
95 if (viewboxSize < GRID_MIN_USER_SIZE) viewboxSize = 1;
\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
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
109 * Snap point to the grid. The snapping is based on grid last paint operation.
\r
114 public double snapToGrid(double pos) {
\r
115 return GridUtils.snapToGrid(pos, segment);
\r