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.participant;
15 import static org.simantics.g2d.canvas.Hints.KEY_CANVAS_TRANSFORM;
17 import java.awt.Graphics2D;
18 import java.awt.geom.AffineTransform;
19 import java.awt.geom.NoninvertibleTransformException;
20 import java.awt.geom.Path2D;
21 import java.awt.geom.Point2D;
22 import java.awt.geom.Rectangle2D;
24 import org.simantics.g2d.canvas.Hints;
25 import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;
26 import org.simantics.g2d.canvas.impl.HintReflection.HintListener;
27 import org.simantics.g2d.scenegraph.SceneGraphConstants;
28 import org.simantics.g2d.utils.GeometryUtils;
29 import org.simantics.scenegraph.g2d.IG2DNode;
30 import org.simantics.scenegraph.utils.NodeUtil;
31 import org.simantics.utils.datastructures.hints.IHintContext.Key;
32 import org.simantics.utils.datastructures.hints.IHintObservable;
33 import org.simantics.utils.page.MarginUtils.Margins;
36 * This class contains utilities for coordinate conversions between control
39 * @author Toni Kalajainen
41 * @author Tuukka Lehtonen
43 public class TransformUtil extends AbstractCanvasParticipant {
45 /** Set dirty if transform changes */
46 @HintListener(Class=Hints.class, Field="KEY_CANVAS_TRANSFORM")
47 public void transformChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) {
48 if (oldValue != null && oldValue.equals(newValue)) return;
49 //System.out.println("TransformUtil: CANVAS TRANSFORM CHANGED TO " + newValue);
54 * Get viewport rectangle.
55 * Transforms controlRect to area on canvas.
57 * @param controlRect rectangle on control
58 * @return area on canvas
60 public Path2D toCanvasArea(Rectangle2D controlRect)
62 Point2D p[] = toCanvasCorners(controlRect);
64 Path2D path = new Path2D.Double();
65 path.moveTo(p[0].getX(), p[0].getY());
66 path.lineTo(p[1].getX(), p[1].getY());
67 path.lineTo(p[2].getX(), p[2].getY());
68 path.lineTo(p[3].getX(), p[3].getY());
75 * Returns 4 canvas corders in order: top-left, top-right, bottom-right, bottom-left
76 * @param controlRect rectangle on control
77 * @return area on canvas
79 public Point2D[] toCanvasCorners(Rectangle2D controlRect)
81 // at·control point = canvas point
82 AffineTransform at = getHint(KEY_CANVAS_TRANSFORM);
83 if (at==null) at = new AffineTransform();
86 at = at.createInverse();
87 } catch (NoninvertibleTransformException e) {
91 Point2D p1 = new Point2D.Double(controlRect.getMinX(), controlRect.getMinY());
92 Point2D p2 = new Point2D.Double(controlRect.getMaxX(), controlRect.getMinY());
93 Point2D p3 = new Point2D.Double(controlRect.getMaxX(), controlRect.getMaxY());
94 Point2D p4 = new Point2D.Double(controlRect.getMinX(), controlRect.getMaxY());
100 at.transform(p3, p3);
101 at.transform(p4, p4);
103 return new Point2D[] {p1, p2, p3, p4};
107 * Converts control rectangle to a rectangle on canvas that contains control rect.
108 * @param controlRect rectangle on control
109 * @return rectangle on canvas that contains control rect
111 public Rectangle2D toCanvasRect(Rectangle2D controlRect)
113 AffineTransform at = getHint(KEY_CANVAS_TRANSFORM);
115 return (Rectangle2D) controlRect.clone();
117 Path2D path = toCanvasArea(controlRect);
118 return path.getBounds2D();
122 * Transforms from canvas coordinate system to canvas coordinate system (also diagram)
125 public void controlToCanvas(Graphics2D g2d)
127 AffineTransform at = getHint(KEY_CANVAS_TRANSFORM);
128 if (at==null) return;
133 * Transforms coordinate system of g2d from canvas to control
136 public void canvasToControl(Graphics2D g2d)
138 AffineTransform at = getHint(KEY_CANVAS_TRANSFORM);
139 if (at==null) return;
141 g2d.transform(at.createInverse());
142 } catch (NoninvertibleTransformException e) {
147 * Convert control point to canvas point
150 public Point2D controlToCanvas(Point2D control, Point2D canvas)
152 AffineTransform at = getInverseTransform();
153 if (canvas==null) canvas = new Point2D.Double();
155 canvas.setLocation(control);
158 at.transform(control, canvas);
163 * Convert canvas point to control
166 public Point2D canvasToControl(Point2D canvas, Point2D control)
168 AffineTransform at = getHint(KEY_CANVAS_TRANSFORM);
169 if (control==null) control = new Point2D.Double();
171 control.setLocation(canvas);
174 at.transform(canvas, control);
179 * Convert canvas vector to control vector
182 public Point2D canvasVectorToControlVector(Point2D canvas, Point2D control)
184 AffineTransform at = getHint(KEY_CANVAS_TRANSFORM);
185 if (control==null) control = new Point2D.Double();
187 control.setLocation(canvas);
190 double x = canvas.getX();
191 double y = canvas.getY();
193 double m00 = at.getScaleX();
194 double m11 = at.getScaleY();
195 double m01 = at.getShearX();
196 double m10 = at.getShearY();
206 * Convert control vector to canvas vector
209 public Point2D controlVectorToCanvasVector(Point2D control, Point2D canvas)
211 AffineTransform at = getInverseTransform();
212 if (canvas==null) canvas = new Point2D.Double();
214 canvas.setLocation(control);
217 double x = control.getX();
218 double y = control.getY();
220 double m00 = at.getScaleX();
221 double m11 = at.getScaleY();
222 double m01 = at.getShearX();
223 double m10 = at.getShearY();
232 * Get a transform that converts
233 * diagram coordinates to control coordinates.
235 * @return transform a clone
237 public AffineTransform getTransform()
239 AffineTransform at = getHint(KEY_CANVAS_TRANSFORM);
240 if (at==null) return new AffineTransform();
241 return (AffineTransform) at.clone();
245 * Get a transform that converts
246 * diagram vector to control vector.
248 * @return transform a clone
250 public AffineTransform getVectorTransform()
252 AffineTransform at = getHint(KEY_CANVAS_TRANSFORM);
253 if (at==null) return new AffineTransform();
254 double m00 = at.getScaleX();
255 double m11 = at.getScaleY();
256 double m01 = at.getShearX();
257 double m10 = at.getShearY();
258 return new AffineTransform(m00, m10, m01, m11, 0, 0);
263 * Transforms control coordinates to diagram coordinates.
265 * @return inverted transform or <code>null</code> if inversion failed
267 public AffineTransform getInverseTransform()
269 AffineTransform at = getTransform();
270 if (at==null) return null;
272 at = at.createInverse();
274 } catch (NoninvertibleTransformException e) {
280 * Tries to invert the specified {@link AffineTransform}.
282 * @parameter at the transform to invert
283 * @return inverted transform or <code>null</code> if inversion failed
285 public AffineTransform getInverseTransform(AffineTransform at)
290 return at.createInverse();
291 } catch (NoninvertibleTransformException e) {
297 * Transforms control vector to diagram vector
300 public AffineTransform getInverseVectorTransform()
302 AffineTransform at = getTransform();
303 if (at==null) return null;
305 at = at.createInverse();
306 double m00 = at.getScaleX();
307 double m11 = at.getScaleY();
308 double m01 = at.getShearX();
309 double m10 = at.getShearY();
310 return new AffineTransform(m00, m10, m01, m11, 0, 0);
311 } catch (NoninvertibleTransformException e) {
317 public void setTransform(AffineTransform at)
319 assert(at.getShearX()!=Double.POSITIVE_INFINITY && at.getShearX()!=Double.NEGATIVE_INFINITY);
320 assert(at.getShearY()!=Double.POSITIVE_INFINITY && at.getShearY()!=Double.NEGATIVE_INFINITY);
321 AffineTransform old = getHint(KEY_CANVAS_TRANSFORM);
322 if(at.equals(old) == false) { // Send event only if the transform has really changed
323 setHint(KEY_CANVAS_TRANSFORM, at);
325 IG2DNode node = NodeUtil.findNodeById(getContext().getSceneGraph(), SceneGraphConstants.NAVIGATION_NODE_PATH);
327 // This is not in hintListener, because we don't want to update
328 // transform if it already came for scenegraph.
329 node.setTransform(at);
333 public void zoom(double scaleFactor)
335 if (scaleFactor==1.0) return;
336 AffineTransform diagramToControl = getTransform();
337 diagramToControl.scale(scaleFactor, scaleFactor);
338 setTransform(diagramToControl);
341 public void rotate(Point2D centerPointCanvas, double angle)
343 if (angle==0.0) return; // and modulos too
344 AffineTransform diagramToControl = getTransform();
345 diagramToControl.rotate(-angle, centerPointCanvas.getX(), centerPointCanvas.getY());
346 setTransform(diagramToControl);
349 public void restoreOrientation(Point2D centerPointCanvas)
351 AffineTransform res = new AffineTransform();
352 AffineTransform at = getTransform();
353 double m01 = at.getShearX();
354 double m11 = at.getScaleY();
355 double theta = Math.atan2(m01, m11);
356 at.rotate(-theta, centerPointCanvas.getX(), centerPointCanvas.getY());
357 res.translate(at.getTranslateX(), at.getTranslateY());
358 res.scale(at.getScaleX(), at.getScaleY());
363 * Get rotate in radians.
366 public double getRotate() {
367 AffineTransform at = getTransform();
368 double m01 = at.getShearX();
369 double m11 = at.getScaleY();
370 return Math.atan2(m01, m11);
375 * @param scaleFactor amount to zoom
376 * @param aroundPoint The center point of zoom in diagram coordinates
378 public void zoomAroundDiagramPoint(double scaleFactor, Point2D aroundPoint)
380 AffineTransform diagramToControl = getTransform();
381 // convert to control point
382 aroundPoint = diagramToControl.transform(aroundPoint, new Point2D.Double());
383 zoomAroundControlPoint(scaleFactor, aroundPoint);
388 * @param scaleFactor amount to zoom
389 * @param centerPoint the control point to zoom around
391 public void zoomAroundControlPoint(double scaleFactor, Point2D centerPoint)
393 if (scaleFactor==1.0) return;
394 AffineTransform diagramToControl = getTransform();
396 Point2D pointBeforeScale = diagramToControl.inverseTransform(centerPoint, new Point2D.Double());
397 diagramToControl.scale(scaleFactor, scaleFactor);
398 Point2D pointAfterScale = diagramToControl.inverseTransform(centerPoint, new Point2D.Double());
399 double _dx = pointAfterScale.getX() - pointBeforeScale.getX();
400 double _dy = pointAfterScale.getY() - pointBeforeScale.getY();
401 diagramToControl.translate(_dx, _dy);
402 } catch (NoninvertibleTransformException e1) {
404 setTransform(diagramToControl);
408 * Transform the viewport. The transform values are in diagram coordinates
412 public void translateWithCanvasCoordinates(double dx, double dy)
414 AffineTransform controlToDiagram = getTransform();
415 controlToDiagram.translate(dx, dy);
416 setTransform(controlToDiagram);
420 * Transform the viewport. The transform values are in control coordinates
424 public void translateWithControlCoordinates(Point2D offset)
426 if (offset.getX()==0 && offset.getY()==0) return;
427 AffineTransform at = getInverseTransform();
428 offset = at.transform(offset, new Point2D.Double());
429 translateWithCanvasCoordinates(offset.getX()-at.getTranslateX(), offset.getY()-at.getTranslateY());
433 * Get scale ratio between control and canvas
437 public Point2D getScale(Point2D pt)
439 AffineTransform at = getTransform();
440 return GeometryUtils.getScaleXY(at, pt);
444 * Modifies transform to make canvas area fully visible.
447 * @param margins margins
449 public void fitArea(Rectangle2D controlArea, Rectangle2D diagramArea, Margins margins)
451 AffineTransform fitTx = org.simantics.scenegraph.utils.GeometryUtils.fitArea(controlArea, diagramArea, margins);
452 //System.out.println("fitArea(" + controlArea + ", " + diagramArea + ", " + margins + "): " + fitTx);