jfig.canvas
Class FigTrafo2D

java.lang.Object
  extended byjfig.canvas.FigTrafo2D
All Implemented Interfaces:
java.lang.Cloneable
Direct Known Subclasses:
Trafo2D, ViewerTrafo

public class FigTrafo2D
extends java.lang.Object
implements java.lang.Cloneable

simple 2D transformation with translation and rotation.

Class FigTrafo2D collects the coordinate transformation methods needed for the xfig/jfig object model.

The internal representation in jfig files (.fig) uses integer coordinates with 2400dpi resolution, called world-coordinates (WC). This allows easy and accurate scaling to typical bitmapped output devices, although the usual output is expected to be to a 75dpi screen or a (resolution independent) Postscript file. As is standard with screen-based coordinates, the origin (0,0) is in the top-left corner of the screen/page.

This program does not use SI-units (that is, meters). A simple hack is provided to scale the internal WC-values back to mm-values. The resolution of 2400 dpi provides an equivalent of approximately 96 dots/mm.

The grid and slider scales may be set and displayed in inch or mm.

Coordinates of objects (as multiples of 2400dpi) may be positive or negative in the range -MAXINT .. MAXINT. The output filters take care to center or align a drawing on a given piece of paper.

The standard transformations between world-coordinates and the computer screen do not allow for rotations. A zoom factor, however, is supported. jfig assumes a fixed screen resolution of 75dpi.

I hope to include compound and hierarchical objects in the editor in the future. The transformations used for these objects will allow rotations.


Field Summary
protected  java.awt.Point anchor
           
static int COARSE_GRID
           
static int COARSE_SNAP
           
protected  double d_zoom_factor
           
static boolean debug
           
static int DOTS_PER_INCH
           
static int DOTS_PER_MM
           
static int DOTS_PER_XMM
           
static int dpi
           
static int DPI
           
(package private) static double FACTOR
           
(package private) static double[] factors
           
static int FINE_GRID
           
static int FINE_SNAP
           
 int gridMode
           
static double MAX_ZOOM_FACTOR
           
static int MEDIUM_GRID
           
static int MEDIUM_SNAP
           
static double MIN_ZOOM_FACTOR
           
static int NO_GRID
           
static int NO_SNAP
           
(package private) static double[] offsets
           
protected  java.awt.Point p
           
protected  double snapDelta
           
 int snapMode
           
static int TEN_GRID
           
static int TEN_SNAP
           
 long timestamp
           
 int units
           
static int UNITS_INCHES
           
static int UNITS_MM
           
static java.lang.String[] UNITS_STRING
           
static int UNITS_XMM
           
protected  double zoom_factor
           
protected  int zoom_factor75
           
 
Constructor Summary
FigTrafo2D()
          default constructor.
 
Method Summary
 void doChangeUnits()
          toggle the current units (UNITS_INCHES -> UNITS_MM -> UNITS_XMM)
 java.lang.String get_units_string()
          get string representation of current units, one of "in", "mm", "xmm".
 int get_units()
          get current units (only UNITS_MM and UNITS_INCHES are currently defined)
 java.awt.Point getAnchor()
          get the current viewport anchor
 FigTrafo2D getClone()
          build a clone of the current transformation.
 int getGridMode()
          get the current grid mode (none, coarse, medium, fine)
 double getGridSpacingValue()
          return the spacing (in world coordinates) of two grid points.
 double getMaxZoomFactor()
           
 double getMinZoomFactor()
           
 java.awt.Point getScreenCoords(java.awt.Point WP)
           
 double getSnap()
          get the current snapDelta value, that is, the current world coordinate distance of two snap points.
 int getSnapRelative()
          return a 'relative' snap distance.
 long getTimestamp()
          get the timestamp of this FigTrafo2D.
 int getUnits()
           
 double getValueInUnits(int v)
          convert the value of world coordinate v into the current units
 java.awt.Point getWorldCoords(int sx, int sy)
          construct a new world coordinate Point from screen coords (sx,sy).
 java.awt.Point getWorldCoords(java.awt.Point SP)
          construct a new world coordinate Point from screen coordinate Point SP.
 java.awt.Point getWorldCoordsSnapped(int sx, int sy)
          construct a new world-coord Point from grid-snapped screen coords (sx,sy).
 java.awt.Point getWorldCoordsSnapped(java.awt.Point SP)
          construct a new world-coord Point from grid-snapped screen-coord Point SP.
 double getZoom()
           
 double getZoomFactor_MultipleOfTwo()
          get the current zoom factor, rounded to the next multiple of 2
 double getZoomFactor()
          get the current zoom factor.
 boolean insideSnap(int x1, int x2)
          check whether two world coordinates differ by less than the current snap distance (of the equivalent of five screen pixels at the current zoom factor).
 void message(java.lang.String msg)
          print a String message via jfig.utils.ExceptionTracer
 java.awt.Point screen_to_wc_snapped(int sx, int sy, java.awt.Point p)
          set Point p world coordinates from grid-snapped screen coords (sx,sy).
 java.awt.Point screen_to_wc_snapped(java.awt.Point sc_p, java.awt.Point p)
          transform a point from screen-coordinates to grid-snapped world-coordinates.
 int screen_to_wc_x(int sx)
           
 int screen_to_wc_y(int sy)
           
 int screen_to_wc(int dsx)
          get the world coordinate length dwx of a length dsx in screen coords.
 java.awt.Point screen_to_wc(java.awt.Point sc_p, java.awt.Point p)
          transform a point from screen-coordinates to world-coordinates
 void set_zoom_region_ooooolllllldddd(int wx, int wy, int wx2, int wy2, int width, int height)
          set_zoom_region(): set the viewport to the given region
 void set_zoom_region(int wx, int wy, int wx2, int wy2, int width, int height)
          set_zoom_region(): set the viewport to the given region.
 void set_zoom(double zoom)
          change the zoom factor.
 void setAnchor(java.awt.Point anchor)
          set the viewport anchor (left bottom corner of the part of the drawing currently visible on the screen).
 void setGridMode(int gridMode)
          set a new grid mode.
 void setSnapAbsolute(double snapDelta)
          set the snapDelta absolutely.
 void setSnapRelative(int mode)
          set the snapDelta relative to the current zoom factor and units.
 void setUnits(int _units)
           
 int wc_to_screen_x(int wc_x)
          transform a world-coordinate-x to screen-coordinate-x
 int wc_to_screen_y(int wc_y)
          transform a world-coordinate-y to screen-coordinate-y
 double wc_to_screen(double dwx)
           
 java.awt.Point wc_to_screen(double wx, double wy, java.awt.Point sp)
          transform a point from world-coordinates to a point sp in screen-coordinates.
 int wc_to_screen(int dwx)
          get the screen coordinate length dsx of a length dwx in world coords.
 java.awt.Point wc_to_screen(java.awt.Point wp, java.awt.Point sp)
          transform a point wp from world-coordinates to a point sp in screen-coordinates.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

dpi

public static final int dpi
See Also:
Constant Field Values

DPI

public static final int DPI
See Also:
Constant Field Values

DOTS_PER_MM

public static final int DOTS_PER_MM
See Also:
Constant Field Values

DOTS_PER_INCH

public static final int DOTS_PER_INCH
See Also:
Constant Field Values

DOTS_PER_XMM

public static final int DOTS_PER_XMM
See Also:
Constant Field Values

UNITS_MM

public static final int UNITS_MM
See Also:
Constant Field Values

UNITS_INCHES

public static final int UNITS_INCHES
See Also:
Constant Field Values

UNITS_XMM

public static final int UNITS_XMM
See Also:
Constant Field Values

UNITS_STRING

public static java.lang.String[] UNITS_STRING

NO_GRID

public static final int NO_GRID
See Also:
Constant Field Values

FINE_GRID

public static final int FINE_GRID
See Also:
Constant Field Values

MEDIUM_GRID

public static final int MEDIUM_GRID
See Also:
Constant Field Values

COARSE_GRID

public static final int COARSE_GRID
See Also:
Constant Field Values

TEN_GRID

public static final int TEN_GRID
See Also:
Constant Field Values

NO_SNAP

public static final int NO_SNAP
See Also:
Constant Field Values

COARSE_SNAP

public static final int COARSE_SNAP
See Also:
Constant Field Values

MEDIUM_SNAP

public static final int MEDIUM_SNAP
See Also:
Constant Field Values

FINE_SNAP

public static final int FINE_SNAP
See Also:
Constant Field Values

TEN_SNAP

public static final int TEN_SNAP
See Also:
Constant Field Values

MIN_ZOOM_FACTOR

public static final double MIN_ZOOM_FACTOR
See Also:
Constant Field Values

MAX_ZOOM_FACTOR

public static final double MAX_ZOOM_FACTOR
See Also:
Constant Field Values

debug

public static final boolean debug
See Also:
Constant Field Values

units

public int units

zoom_factor

protected double zoom_factor

zoom_factor75

protected int zoom_factor75

d_zoom_factor

protected double d_zoom_factor

snapDelta

protected double snapDelta

snapMode

public int snapMode

gridMode

public int gridMode

timestamp

public long timestamp

anchor

protected java.awt.Point anchor

p

protected java.awt.Point p

offsets

static final double[] offsets

factors

static final double[] factors

FACTOR

static final double FACTOR
See Also:
Constant Field Values
Constructor Detail

FigTrafo2D

public FigTrafo2D()
default constructor. Allocates the anchor and the temporary point p, set zoom factor 1.0, coarse grid, inches units.

Method Detail

setAnchor

public void setAnchor(java.awt.Point anchor)
set the viewport anchor (left bottom corner of the part of the drawing currently visible on the screen). This will be necessary after panning or a change to the zoom factor.

The size of the viewport in world-coordinates depends both on the size of the drawing canvas on the screen and the current zoom-factor.

Parameters:
anchor - The new value (Point) for the viewport anchor.

getAnchor

public java.awt.Point getAnchor()
get the current viewport anchor


set_zoom

public void set_zoom(double zoom)
change the zoom factor.

Parameters:
zoom - The new zoom factor. Currently only positive integers are allowed.

set_zoom_region

public void set_zoom_region(int wx,
                            int wy,
                            int wx2,
                            int wy2,
                            int width,
                            int height)
set_zoom_region(): set the viewport to the given region.

Actually, this method tries to fit the specified region as far as possible into the current window size, while limiting the zoom-factors to multiples of sqrt(2).

anchor'(

Parameters:
wx - the top left corner (x)
wy - the top left corner (y)
wx2 - the bottom right corner (x)
wy2 - (y)
width - the current window size (width)
height - (height)

set_zoom_region_ooooolllllldddd

public void set_zoom_region_ooooolllllldddd(int wx,
                                            int wy,
                                            int wx2,
                                            int wy2,
                                            int width,
                                            int height)
set_zoom_region(): set the viewport to the given region

Parameters:
wx - the top left corner (x)
wy - the top left corner (y)
wx2 - the bottom right corner (x)
wy2 - (y)
width - the current window size (width)
height - (height)

getZoomFactor

public double getZoomFactor()
get the current zoom factor.


getZoom

public double getZoom()

getMinZoomFactor

public double getMinZoomFactor()

getMaxZoomFactor

public double getMaxZoomFactor()

getZoomFactor_MultipleOfTwo

public double getZoomFactor_MultipleOfTwo()
get the current zoom factor, rounded to the next multiple of 2


get_units

public int get_units()
get current units (only UNITS_MM and UNITS_INCHES are currently defined)


getUnits

public int getUnits()

get_units_string

public java.lang.String get_units_string()
get string representation of current units, one of "in", "mm", "xmm".


getValueInUnits

public double getValueInUnits(int v)
convert the value of world coordinate v into the current units


setSnapAbsolute

public void setSnapAbsolute(double snapDelta)
set the snapDelta absolutely. Using the function screen_to_wc_snapped() this allows to snap all world-coordinates to multiples of the snapDelta only. For example, use setSnap(24) to force all coordinates to be multiples of 1/100 inch.

Parameters:
snapDelta - The new value for the snapDelta variable

setSnapRelative

public void setSnapRelative(int mode)
set the snapDelta relative to the current zoom factor and units. Depending on the value of snapMode (NO_SNAP, ... FINE_SNAP), the absolute value of snapDelta at 2400 dpi is calculated.


getSnap

public double getSnap()
get the current snapDelta value, that is, the current world coordinate distance of two snap points.


getSnapRelative

public int getSnapRelative()
return a 'relative' snap distance.

This method either returns the current world coordinate distance of two snap points like method getSnap() (if snapping is active), or the world coordinate distance of five pixels in screen coords at the current zoom factor.

Use this method to find out whether a given mouse position is 'near' a given coordinate, dependent on current snap mode and zoom factor.


insideSnap

public boolean insideSnap(int x1,
                          int x2)
check whether two world coordinates differ by less than the current snap distance (of the equivalent of five screen pixels at the current zoom factor).


getGridSpacingValue

public double getGridSpacingValue()
return the spacing (in world coordinates) of two grid points.


screen_to_wc

public int screen_to_wc(int dsx)
get the world coordinate length dwx of a length dsx in screen coords.


screen_to_wc_x

public int screen_to_wc_x(int sx)

screen_to_wc_y

public int screen_to_wc_y(int sy)

screen_to_wc

public java.awt.Point screen_to_wc(java.awt.Point sc_p,
                                   java.awt.Point p)
transform a point from screen-coordinates to world-coordinates

Parameters:
sc_p - The point in screen-coordinates to be transformed
p - The point in world-coordinates to be transformed,

wc_to_screen

public int wc_to_screen(int dwx)
get the screen coordinate length dsx of a length dwx in world coords. We provide this function with both integer and double argument and return types.


wc_to_screen

public double wc_to_screen(double dwx)

wc_to_screen_x

public int wc_to_screen_x(int wc_x)
transform a world-coordinate-x to screen-coordinate-x

Parameters:
wc_x - The world-coordinate x-value to be transformed

wc_to_screen_y

public int wc_to_screen_y(int wc_y)
transform a world-coordinate-y to screen-coordinate-y

Parameters:
wc_y - The world-coordinate y-value to be transformed

wc_to_screen

public java.awt.Point wc_to_screen(java.awt.Point wp,
                                   java.awt.Point sp)
transform a point wp from world-coordinates to a point sp in screen-coordinates. Return a pointer to sp.


wc_to_screen

public java.awt.Point wc_to_screen(double wx,
                                   double wy,
                                   java.awt.Point sp)
transform a point from world-coordinates to a point sp in screen-coordinates. Return a pointer to sp.


screen_to_wc_snapped

public java.awt.Point screen_to_wc_snapped(java.awt.Point sc_p,
                                           java.awt.Point p)
transform a point from screen-coordinates to grid-snapped world-coordinates. That is, every coordinate returned by this function is a multiple of the snapDelta variable


screen_to_wc_snapped

public java.awt.Point screen_to_wc_snapped(int sx,
                                           int sy,
                                           java.awt.Point p)
set Point p world coordinates from grid-snapped screen coords (sx,sy).


getWorldCoords

public java.awt.Point getWorldCoords(int sx,
                                     int sy)
construct a new world coordinate Point from screen coords (sx,sy).


getWorldCoords

public java.awt.Point getWorldCoords(java.awt.Point SP)
construct a new world coordinate Point from screen coordinate Point SP.


getWorldCoordsSnapped

public java.awt.Point getWorldCoordsSnapped(int sx,
                                            int sy)
construct a new world-coord Point from grid-snapped screen coords (sx,sy).


getWorldCoordsSnapped

public java.awt.Point getWorldCoordsSnapped(java.awt.Point SP)
construct a new world-coord Point from grid-snapped screen-coord Point SP.


getScreenCoords

public java.awt.Point getScreenCoords(java.awt.Point WP)

setGridMode

public void setGridMode(int gridMode)
set a new grid mode.

Parameters:
gridMode - the integer value of the new grid spacing, one of NO_GRID, COARSE_GRID, MEDIUM_GRID, FINE_GRID.

getGridMode

public int getGridMode()
get the current grid mode (none, coarse, medium, fine)


getTimestamp

public long getTimestamp()
get the timestamp of this FigTrafo2D.

The timestamp is updated whenever the transformation anchor or zoom factor is changed.

Compare the transformation timestamp with object timestamps to decide whether the screen representation of those objects need to be updated.


doChangeUnits

public void doChangeUnits()
toggle the current units (UNITS_INCHES -> UNITS_MM -> UNITS_XMM)


setUnits

public void setUnits(int _units)

getClone

public FigTrafo2D getClone()
build a clone of the current transformation. We use the standard Java clone() method, and afterwards correct some references to get a 'deep copy' semantic.


message

public void message(java.lang.String msg)
print a String message via jfig.utils.ExceptionTracer