Package org.graphstream.ui.view.camera
Class DefaultCamera2D
java.lang.Object
org.graphstream.ui.view.camera.DefaultCamera2D
- All Implemented Interfaces:
Camera
public class DefaultCamera2D extends Object implements Camera
Define a view of the graph or a part of the graph.
The camera can be seen as an element in charge of projecting the graph
elements in graph units (GU) into rendering space units, often in pixels. It
defines the transformation, an affine matrix, to pass from the first to the
second (in fact its the back-end that does it).
It also contains the graph metrics. This is 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 any rendering units). The two mains methods for this operation
are [[Camera.pushView(GraphicGraph)]] and [[Camera.popView()]].
The user of the camera must set both the view port and the graph bounds in
order for the camera to correctly project the graph view (the Renderer does
that before using the Camera, at each frame). The camera model is as follows:
the camera defines a center at which it always points. It can zoom on the
graph (as if the camera angle of view was changing), pan in any direction by
moving its center of view and rotate along the axe going from the center to
the camera position (camera can rotate around two axes in 3D, but this is a
2D camera).
There are two modes: - an "auto-fit" mode where the camera always show the
whole graph even if it changes in size, by automatically changing the center
and zoom values, - and a "user" mode where the camera center (looked-at
point), zoom and panning are specified and will not be modified in the bounds
of the graph change.
The camera is also able to answer questions like: "what element is visible
actually?", or "on what element is the mouse cursor actually?".
The camera is also able to compute sprite positions according to their
attachment, as well as maintaining a list of all elements out of the view, so
that it is not needed to render them.
-
Constructor Summary
Constructors Constructor Description DefaultCamera2D(GraphicGraph graph) -
Method Summary
Modifier and Type Method Description Collection<GraphicElement>allGraphicElementsIn(GraphicGraph graph, EnumSet<InteractiveElement> types, double x1, double y1, double x2, double y2)voidautoFitView()Compute a transformation matrix that pass from graph units (user space) to pixel units (device space) so that the whole graph is visible.voidcheckVisibility(GraphicGraph graph)booleanedgeContains(GraphicElement elt, double x, double y)GraphicElementfindGraphicElementAt(GraphicGraph graph, EnumSet<InteractiveElement> types, double x, double y)Search for the first GraphicElement among those specified.doublegetGraphDimension()A number in GU that gives the approximate graph size (often the diagonal of the graph).GraphMetricsgetMetrics()Get theorg.graphstream.ui.swingViewer.util.GraphMetricsobject linked to this Camera.Point3getNodeOrSpritePositionGU(GraphicElement elt, Point3 pos)ValuesgetNodeOrSpriteSize(GraphicElement elt)Point3getSpritePosition(GraphicSprite sprite, Point3 pos, StyleConstants.Units units)Compute the real position of a sprite according to its eventual attachment in graph units.Point3getSpritePositionEdge(GraphicSprite sprite, Point3 position, StyleConstants.Units units)Point3getSpritePositionFree(GraphicSprite sprite, Point3 position, StyleConstants.Units units)Point3getSpritePositionNode(GraphicSprite sprite, Point3 position, StyleConstants.Units units)Compute the position of a sprite if attached to a node.Point3getViewCenter()The view centre (a point in graph units).doublegetViewPercent()The portion of the graph visible.doublegetViewRotation()The current rotation angle.double[]graphViewport()booleanisEdgeIn(GraphicEdge edge, double X1, double Y1, double X2, double Y2)booleanisEdgeVisible(GraphicEdge edge)Check if an edge is visible in the current view port.booleanisNodeIn(GraphicNode node, double X1, double Y1, double X2, double Y2)Is the given node visible in the given area.booleanisSpriteIn(GraphicSprite sprite, double X1, double Y1, double X2, double Y2)Is the given sprite visible in the given area.booleanisSpriteVisible(GraphicSprite sprite)Check if a sprite is visible in the current view port.booleanisTextVisible(GraphicElement element)booleanisVisible(GraphicElement element)True if the element should be visible on screen.booleannodeContains(GraphicElement elt, double x, double y)doublepaddingXgu()doublepaddingXpx()doublepaddingYgu()doublepaddingYpx()voidpopView()Restore the transform that was used before#pushView()is used.voidpushView(GraphicGraph graph)Set the camera view in the given graphics and backup the previous transform of the graphics.voidremoveGraphViewport()Remove the specified graph view port.voidresetView()Reset the view to the automatic mode.voidsetAutoFitView(boolean on)Enable or disable automatic adjustment of the view to see the entire graph.voidsetBackend(Backend backend)voidsetBounds(double minx, double miny, double minz, double maxx, double maxy, double maxz)Set the graphic graph bounds (the lowest and highest points).voidsetBounds(GraphicGraph graph)Set the graphic graph bounds from the graphic graph.voidsetGraphViewport(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).voidsetPadding(GraphicGraph graph)Set the graph padding.voidsetViewCenter(double x, double y, double z)Change the view centre.voidsetViewCenter(Point3 p)voidsetViewPercent(double percent)Zoom the view.voidsetViewport(double x, double y, double viewportWidth, double viewportHeight)Set the output view port size in pixels.voidsetViewRotation(double theta)Rotate the view around its centre point by a given theta angles (in degrees).voidsetZoom(double z)Set the zoom (or percent of the graph visible), 1 means the graph is fully visible.booleanspriteContains(GraphicElement elt, double x, double y)Point3spritePositionPx(GraphicSprite sprite)StringtoString()Point3transformGuToPx(double x, double y, double z)Transform a point in graph units into pixels.Point3transformPxToGu(double x, double y)Return the given point in pixels converted in graph units (GU) using the inverse transformation of the current projection matrix.voiduserView()Compute a transformation that pass from graph units (user space) to a pixel units (device space) so that the view (zoom and center) requested by the user is produced.
-
Constructor Details
-
Method Details
-
getViewCenter
Description copied from interface:CameraThe view centre (a point in graph units).- Specified by:
getViewCenterin interfaceCamera- Returns:
- The view centre.
-
setViewCenter
public void setViewCenter(double x, double y, double z)Description copied from interface:CameraChange the view centre.- Specified by:
setViewCenterin interfaceCamera- Parameters:
x- The new abscissa.y- The new ordinate.z- The new depth.
-
setViewCenter
-
getViewPercent
public double getViewPercent()Description copied from interface:CameraThe portion of the graph visible.- Specified by:
getViewPercentin interfaceCamera- 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:CameraZoom the view.- Specified by:
setViewPercentin interfaceCamera- Parameters:
percent- Percent of the graph visible.
-
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.
-
getViewRotation
public double getViewRotation()Description copied from interface:CameraThe current rotation angle.- Specified by:
getViewRotationin interfaceCamera- Returns:
- The rotation angle in degrees.
-
setViewRotation
public void setViewRotation(double theta)Description copied from interface:CameraRotate the view around its centre point by a given theta angles (in degrees).- Specified by:
setViewRotationin interfaceCamera- Parameters:
theta- The rotation angle in degrees.
-
setViewport
public void setViewport(double x, double y, 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.
-
getGraphDimension
public double getGraphDimension()Description copied from interface:CameraA 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:
getGraphDimensionin interfaceCamera- Returns:
- The graph estimated size in graph units.
-
spriteContains
-
spritePositionPx
-
getSpritePosition
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.
-
getSpritePositionFree
public Point3 getSpritePositionFree(GraphicSprite sprite, Point3 position, StyleConstants.Units units) -
getSpritePositionEdge
public Point3 getSpritePositionEdge(GraphicSprite sprite, Point3 position, StyleConstants.Units units) -
getSpritePositionNode
public Point3 getSpritePositionNode(GraphicSprite sprite, Point3 position, StyleConstants.Units units)Compute the position of a sprite if attached to a node.- Parameters:
sprite- The sprite.position- Where to stored the computed position, if null, the position is created.units- The units the computed position must be given into.- Returns:
- The same instance as pos, or a new one if pos was null.
-
nodeContains
-
edgeContains
-
getNodeOrSpriteSize
-
getNodeOrSpritePositionGU
-
removeGraphViewport
public void removeGraphViewport()Description copied from interface:CameraRemove the specified graph view port.- Specified by:
removeGraphViewportin interfaceCamera- See Also:
Camera.setGraphViewport(double, double, double, double)
-
setGraphViewport
public void setGraphViewport(double minx, double miny, double maxx, double maxy)Description copied from interface:CameraSpecify exactly the minimum and maximum points in GU that are visible (more points may be visible due to aspect-ratio constraints).- Specified by:
setGraphViewportin interfaceCamera- 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()
-
resetView
public void resetView()Description copied from interface:CameraReset the view to the automatic mode. -
pushView
Set the camera view in the given graphics and backup the previous transform of the graphics. CallpopView()to restore the saved transform. You can only push one time the view.- Parameters:
graph- The graphic graph (used to check element visibility).
-
popView
public void popView()Restore the transform that was used before#pushView()is used. -
checkVisibility
-
isNodeIn
Is the given node visible in the given area.- Parameters:
node- The node to check.X1- The min abscissa of the area.Y1- The min ordinate of the area.X2- The max abscissa of the area.Y2- The max ordinate of the area.- Returns:
- True if the node lies in the given area.
-
isEdgeIn
-
setPadding
Set the graph padding. Called in pushView.- Parameters:
graph- The graphic graph.
-
autoFitView
public void autoFitView()Compute a transformation matrix that pass from graph units (user space) to pixel units (device space) so that the whole graph is visible. -
userView
public void userView()Compute a transformation that pass from graph units (user space) to a pixel units (device space) so that the view (zoom and center) requested by the user is produced. -
paddingXgu
public double paddingXgu() -
paddingYgu
public double paddingYgu() -
paddingXpx
public double paddingXpx() -
paddingYpx
public double paddingYpx() -
setBounds
public void setBounds(double minx, double miny, double minz, double maxx, double maxy, double maxz)Set the graphic graph bounds (the lowest and highest points). -
setBounds
Set the graphic graph bounds from the graphic graph. -
getMetrics
Description copied from interface:CameraGet theorg.graphstream.ui.swingViewer.util.GraphMetricsobject linked to this Camera. It can be used to convert pixels to graphic units and vice versa.- Specified by:
getMetricsin interfaceCamera- Returns:
- a GraphMetrics instance
-
setAutoFitView
public void setAutoFitView(boolean on)Enable or disable automatic adjustment of the view to see the entire graph.- Specified by:
setAutoFitViewin interfaceCamera- Parameters:
on- If true, automatic adjustment is enabled.
-
setBackend
-
transformGuToPx
Transform a point in graph units into pixels.- Specified by:
transformGuToPxin interfaceCamera- Returns:
- The transformed point.
-
transformPxToGu
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:
transformPxToGuin interfaceCamera- Parameters:
x- The source point abscissa in pixels.y- The source point ordinate in pixels.- Returns:
- The resulting points in graph units.
-
isTextVisible
-
isVisible
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. -
isSpriteVisible
Check if a sprite is visible in the current view port.- Parameters:
sprite- The sprite to check.- Returns:
- True if visible.
-
isSpriteIn
Is the given sprite visible in the given area.- Parameters:
sprite- The sprite to check.X1- The min abscissa of the area.Y1- The min ordinate of the area.X2- The max abscissa of the area.Y2- The max ordinate of the area.- Returns:
- True if the node lies in the given area.
-
isEdgeVisible
Check if an edge is visible in the current view port.- Parameters:
edge- The edge to check.- Returns:
- True if visible.
-
findGraphicElementAt
public GraphicElement findGraphicElementAt(GraphicGraph graph, EnumSet<InteractiveElement> types, double x, double y)Search for the first GraphicElement among those specified. Multiple elements are resolved by priority-InteractiveElement.NODE>InteractiveElement.EDGE>InteractiveElement.SPRITE, (in that order) that contains the point at coordinates (x, y).- Specified by:
findGraphicElementAtin interfaceCamera- 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.
-
graphViewport
public double[] graphViewport() -
allGraphicElementsIn
public Collection<GraphicElement> allGraphicElementsIn(GraphicGraph graph, EnumSet<InteractiveElement> types, double x1, double y1, double x2, double y2)- Specified by:
allGraphicElementsInin interfaceCamera
-
toString
-