org.graphstream.ui.swingViewer.util
Class DefaultCamera

java.lang.Object
  extended by org.graphstream.ui.swingViewer.util.DefaultCamera
All Implemented Interfaces:
Camera

public class DefaultCamera
extends Object
implements Camera

Define how the graph is viewed.

The camera is in charge of projecting the graph spaces in graph units (GU) into user spaces (often in pixels). It defines the transformation (an affine matrix) to passe from the first to the second. It also contains the graph metrics, a set of values that give the overall dimensions of the graph in graph units, as well as the view port, the area on the screen (or any rendering surface) that will receive the results in pixels (or rendering units).

The camera defines a centre at which it always points. It can zoom on the graph, pan in any direction and rotate along two axes.

Knowing the transformation also allows to provide services like "what element is not invisible ?" (not in the camera view) or "on what element is the mouse cursor actually ?".


Constructor Summary
DefaultCamera(GraphicGraph graph)
          New camera.
 
Method Summary
 ArrayList<GraphicElement> allNodesOrSpritesIn(GraphicGraph graph, double x1, double y1, double x2, double y2)
          Search for all the nodes and sprites contained inside the rectangle (x1,y1)-(x2,y2).
 void checkVisibility(GraphicGraph graph)
          Process each node to check if it is in the actual view port, and mark invisible nodes.
 GraphicElement findNodeOrSpriteAt(GraphicGraph graph, double x, double y)
          Search for the first node or sprite (in that order) that contains the point at coordinates (x, y).
 double getGraphDimension()
          A number in GU that gives the approximate graph size (often the diagonal of the graph).
 double[] getGraphViewport()
           
 GraphMetrics getMetrics()
          Get the GraphMetrics object linked to this Camera.
 Point2D.Double getSpritePosition(GraphicSprite sprite, Point2D.Double pos, StyleConstants.Units units)
          Compute the real position of a sprite according to its eventual attachment in graph units.
 Point3 getViewCenter()
          The view centre (a point in graph units).
 double getViewPercent()
          The portion of the graph visible.
 double getViewRotation()
          The current rotation angle.
 boolean isVisible(GraphicElement element)
          True if the element should be visible on screen.
 void popView(Graphics2D g2)
          Restore the transform that was used before pushView(GraphicGraph, Graphics2D) is used.
 void pushView(GraphicGraph graph, Graphics2D g2)
          Set the camera view in the given graphics and backup the previous transform of the graphics.
 void removeGraphViewport()
          Remove the specified graph view port.
 void resetView()
          Reset the view to the automatic mode.
 void setAutoFitView(boolean on)
          Enable or disable automatic adjustment of the view to see the entire graph.
 void setBounds(double minx, double miny, double minz, double maxx, double maxy, double maxz)
          Set the bounds of the graphic graph in GU.
 void setGraphViewport(double minx, double miny, double maxx, double maxy)
          Specify exactly the minimum and maximum points in GU that are visible (more points may be visible due to aspect-ratio constraints).
 void setPadding(GraphicGraph graph)
          Set the graph padding.
 void setViewCenter(double x, double y)
           
 void setViewCenter(double x, double y, double z)
          Change the view centre.
 void setViewPercent(double percent)
          Zoom the view.
 void setViewport(double viewportX, double viewportY, double viewportWidth, double viewportHeight)
          Set the output view port size in pixels.
 void setViewRotation(double theta)
          Set the rotation angle around the centre.
 void setZoom(double z)
          Set the zoom (or percent of the graph visible), 1 means the graph is fully visible.
 String toString()
           
 Point3 transformGuToPx(double x, double y, double z)
          Transform a point in graph units into pixels.
 Point3 transformPxToGu(double x, double y)
          Return the given point in pixels converted in graph units (GU) using the inverse transformation of the current projection matrix.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DefaultCamera

public DefaultCamera(GraphicGraph graph)
New camera.

Method Detail

getViewCenter

public Point3 getViewCenter()
Description copied from interface: Camera
The view centre (a point in graph units).

Specified by:
getViewCenter in interface Camera
Returns:
The view centre.

setViewCenter

public void setViewCenter(double x,
                          double y,
                          double z)
Description copied from interface: Camera
Change the view centre.

Specified by:
setViewCenter in interface Camera
Parameters:
x - The new abscissa.
y - The new ordinate.
z - The new depth.

setViewCenter

public void setViewCenter(double x,
                          double y)

getViewPercent

public double getViewPercent()
Description copied from interface: Camera
The portion of the graph visible.

Specified by:
getViewPercent in interface Camera
Returns:
A real for which value 1 means the graph is fully visible and uses the whole view port.

setViewPercent

public void setViewPercent(double percent)
Description copied from interface: Camera
Zoom the view.

Specified by:
setViewPercent in interface Camera
Parameters:
percent - Percent of the graph visible.

getViewRotation

public double getViewRotation()
Description copied from interface: Camera
The current rotation angle.

Specified by:
getViewRotation in interface Camera
Returns:
The rotation angle in degrees.

getMetrics

public GraphMetrics getMetrics()
Description copied from interface: Camera
Get the GraphMetrics object linked to this Camera. It can be used to convert pixels to graphic units and vice versa.

Specified by:
getMetrics in interface Camera
Returns:
a GraphMetrics instance

toString

public String toString()
Overrides:
toString in class Object

resetView

public void resetView()
Description copied from interface: Camera
Reset the view to the automatic mode.

Specified by:
resetView in interface Camera

setBounds

public void setBounds(double minx,
                      double miny,
                      double minz,
                      double maxx,
                      double maxy,
                      double maxz)
Description copied from interface: Camera
Set the bounds of the graphic graph in GU. Called by the Viewer.

Specified by:
setBounds in interface Camera
Parameters:
minx - Lowest abscissa.
miny - Lowest ordinate.
minz - Lowest depth.
maxx - Highest abscissa.
maxy - Highest ordinate.
maxz - Highest depth.

getGraphDimension

public double getGraphDimension()
Description copied from interface: Camera
A number in GU that gives the approximate graph size (often the diagonal of the graph). This allows to compute displacements in the graph as percent of its overall size. For example this can be used to move the view centre.

Specified by:
getGraphDimension in interface Camera
Returns:
The graph estimated size in graph units.

isVisible

public boolean isVisible(GraphicElement element)
True if the element should be visible on screen. The method used is to transform the center of the element (which is always in graph units) using the camera actual transformation to put it in pixel units. Then to look in the style sheet the size of the element and to test if its enclosing rectangle intersects the view port. For edges, its two nodes are used. As a speed-up by default if the camera is in automatic fitting mode, all element should be visible, and the test always returns true.

Specified by:
isVisible in interface Camera
Parameters:
element - The element to test.
Returns:
True if the element is visible and therefore must be rendered.

transformPxToGu

public Point3 transformPxToGu(double x,
                              double y)
Description copied from interface: Camera
Return the given point in pixels converted in graph units (GU) using the inverse transformation of the current projection matrix. The inverse matrix is computed only once each time a new projection matrix is created.

Specified by:
transformPxToGu in interface Camera
Parameters:
x - The source point abscissa in pixels.
y - The source point ordinate in pixels.
Returns:
The resulting points in graph units.

transformGuToPx

public Point3 transformGuToPx(double x,
                              double y,
                              double z)
Description copied from interface: Camera
Transform a point in graph units into pixels.

Specified by:
transformGuToPx in interface Camera
Returns:
The transformed point.

checkVisibility

public void checkVisibility(GraphicGraph graph)
Process each node to check if it is in the actual view port, and mark invisible nodes. This method allows for fast node, sprite and edge visibility checking when drawing. This must be called before each rendering (if the view port changed).


findNodeOrSpriteAt

public GraphicElement findNodeOrSpriteAt(GraphicGraph graph,
                                         double x,
                                         double y)
Search for the first node or sprite (in that order) that contains the point at coordinates (x, y).

Parameters:
graph - The graph to search for.
x - The point abscissa.
y - The point ordinate.
Returns:
The first node or sprite at the given coordinates or null if nothing found.

allNodesOrSpritesIn

public ArrayList<GraphicElement> allNodesOrSpritesIn(GraphicGraph graph,
                                                     double x1,
                                                     double y1,
                                                     double x2,
                                                     double y2)
Search for all the nodes and sprites contained inside the rectangle (x1,y1)-(x2,y2).

Parameters:
graph - The graph to search for.
x1 - The rectangle lowest point abscissa.
y1 - The rectangle lowest point ordinate.
x2 - The rectangle highest point abscissa.
y2 - The rectangle highest point ordinate.
Returns:
The set of sprites and nodes in the given rectangle.

getSpritePosition

public Point2D.Double getSpritePosition(GraphicSprite sprite,
                                        Point2D.Double pos,
                                        StyleConstants.Units units)
Compute the real position of a sprite according to its eventual attachment in graph units.

Parameters:
sprite - The sprite.
pos - Receiver for the sprite 2D position, can be null.
units - The units in which the position must be computed (the sprite already contains units).
Returns:
The same instance as the one given by parameter pos or a new one if pos was null, containing the computed position in the given units.

getGraphViewport

public double[] getGraphViewport()

setGraphViewport

public void setGraphViewport(double minx,
                             double miny,
                             double maxx,
                             double maxy)
Description copied from interface: Camera
Specify exactly the minimum and maximum points in GU that are visible (more points may be visible due to aspect-ratio constraints).

Specified by:
setGraphViewport in interface Camera
Parameters:
minx - The minimum abscissa visible.
miny - The minimum ordinate visible.
maxx - The maximum abscissa visible.
maxy - The maximum abscissa visible.
See Also:
Camera.removeGraphViewport()

removeGraphViewport

public void removeGraphViewport()
Description copied from interface: Camera
Remove the specified graph view port.

Specified by:
removeGraphViewport in interface Camera
See Also:
Camera.setGraphViewport(double, double, double, double)

pushView

public void pushView(GraphicGraph graph,
                     Graphics2D g2)
Set the camera view in the given graphics and backup the previous transform of the graphics. Call popView(Graphics2D) to restore the saved transform. You can only push one time the view.

Parameters:
g2 - The Swing graphics to change.

popView

public void popView(Graphics2D g2)
Restore the transform that was used before pushView(GraphicGraph, Graphics2D) is used.

Parameters:
g2 - The Swing graphics to restore.

setAutoFitView

public void setAutoFitView(boolean on)
Enable or disable automatic adjustment of the view to see the entire graph.

Specified by:
setAutoFitView in interface Camera
Parameters:
on - If true, automatic adjustment is enabled.

setZoom

public void setZoom(double z)
Set the zoom (or percent of the graph visible), 1 means the graph is fully visible.

Parameters:
z - The zoom.

setViewRotation

public void setViewRotation(double theta)
Set the rotation angle around the centre.

Specified by:
setViewRotation in interface Camera
Parameters:
theta - The rotation angle in degrees.

setViewport

public void setViewport(double viewportX,
                        double viewportY,
                        double viewportWidth,
                        double viewportHeight)
Set the output view port size in pixels.

Parameters:
viewportWidth - The width in pixels of the view port.
viewportHeight - The width in pixels of the view port.

setPadding

public void setPadding(GraphicGraph graph)
Set the graph padding.

Parameters:
graph - The graphic graph.


Copyright © 2012. All Rights Reserved.