Class DefaultCamera

java.lang.Object
org.graphstream.ui.swing_viewer.util.DefaultCamera
All Implemented Interfaces:
org.graphstream.ui.view.camera.Camera

public class DefaultCamera extends Object implements org.graphstream.ui.view.camera.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 ?".

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected boolean
    Automatic centring of the view.
    protected org.graphstream.ui.geom.Point3
    The camera centre of view.
    protected org.graphstream.ui.graphicGraph.GraphicGraph
    The graph.
    protected double[]
    The graph view port, if any.
    protected double
     
    protected org.graphstream.ui.view.util.GraphMetrics
    Information on the graph overall dimension and position.
    protected HashSet<String>
    Which node is visible.
    protected AffineTransform
    The previous affine transform.
    protected org.graphstream.ui.graphicGraph.stylesheet.Values
    Padding around the graph.
    protected double
    The rotation angle.
    protected AffineTransform
    The graph-space -> pixel-space transformation.
    protected AffineTransform
    The inverse transform of Tx.
    protected double
    The camera zoom.
  • Constructor Summary

    Constructors
    Constructor
    Description
    DefaultCamera(org.graphstream.ui.graphicGraph.GraphicGraph graph)
    New camera.
  • Method Summary

    Modifier and Type
    Method
    Description
    Collection<org.graphstream.ui.graphicGraph.GraphicElement>
    allGraphicElementsIn(org.graphstream.ui.graphicGraph.GraphicGraph graph, EnumSet<org.graphstream.ui.view.util.InteractiveElement> types, double x1, double y1, double x2, double y2)
     
    protected void
    Compute a transformation matrix that pass from graph units (user space) to pixel units (device space) so that the whole graph is visible.
    void
    checkVisibility(org.graphstream.ui.graphicGraph.GraphicGraph graph)
    Process each node to check if it is in the actual view port, and mark invisible nodes.
    protected boolean
    edgeContains(org.graphstream.ui.graphicGraph.GraphicElement elt, double x, double y)
    Check if an edge contains the given point (x,y).
    org.graphstream.ui.graphicGraph.GraphicElement
    findGraphicElementAt(org.graphstream.ui.graphicGraph.GraphicGraph graph, EnumSet<org.graphstream.ui.view.util.InteractiveElement> types, double x, double y)
    Search for the first GraphicElement among those specified.
    double
     
    double[]
     
    org.graphstream.ui.view.util.GraphMetrics
     
    protected double
     
    protected double
     
    protected double
     
    protected double
     
    getSpritePosition(org.graphstream.ui.graphicGraph.GraphicSprite sprite, Point2D.Double pos, org.graphstream.ui.graphicGraph.stylesheet.StyleConstants.Units units)
    Compute the real position of a sprite according to its eventual attachment in graph units.
    protected Point2D.Double
    getSpritePositionEdge(org.graphstream.ui.graphicGraph.GraphicSprite sprite, Point2D.Double pos, org.graphstream.ui.graphicGraph.stylesheet.StyleConstants.Units units)
    Compute the position of a sprite if attached to an edge.
    protected Point2D.Double
    getSpritePositionFree(org.graphstream.ui.graphicGraph.GraphicSprite sprite, Point2D.Double pos, org.graphstream.ui.graphicGraph.stylesheet.StyleConstants.Units units)
    Compute the position of a sprite if it is not attached.
    protected Point2D.Double
    getSpritePositionNode(org.graphstream.ui.graphicGraph.GraphicSprite sprite, Point2D.Double pos, org.graphstream.ui.graphicGraph.stylesheet.StyleConstants.Units units)
    Compute the position of a sprite if attached to a node.
    org.graphstream.ui.geom.Point3
     
    double
     
    double
     
    protected boolean
    isEdgeIn(org.graphstream.ui.graphicGraph.GraphicEdge edge, double X1, double Y1, double X2, double Y2)
    Is the given sprite visible in the given area.
    protected boolean
    isEdgeVisible(org.graphstream.ui.graphicGraph.GraphicEdge edge)
    Check if an edge is visible in the current view port.
    protected boolean
    isNodeIn(org.graphstream.ui.graphicGraph.GraphicNode node, double X1, double Y1, double X2, double Y2)
    Is the given node visible in the given area.
    protected boolean
    isSpriteIn(org.graphstream.ui.graphicGraph.GraphicSprite sprite, double X1, double Y1, double X2, double Y2)
    Is the given sprite visible in the given area.
    protected boolean
    isSpriteVisible(org.graphstream.ui.graphicGraph.GraphicSprite sprite)
    Check if a sprite is visible in the current view port.
    boolean
    isVisible(org.graphstream.ui.graphicGraph.GraphicElement element)
    True if the element should be visible on screen.
    protected boolean
    nodeContains(org.graphstream.ui.graphicGraph.GraphicElement elt, double x, double y)
    Check if a node contains the given point (x,y).
    void
    Restore the transform that was used before pushView(GraphicGraph, Graphics2D) is used.
    void
    pushView(org.graphstream.ui.graphicGraph.GraphicGraph graph, Graphics2D g2)
    Set the camera view in the given graphics and backup the previous transform of the graphics.
    void
     
    void
     
    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)
     
    void
    setGraphViewport(double minx, double miny, double maxx, double maxy)
     
    void
    setPadding(org.graphstream.ui.graphicGraph.GraphicGraph graph)
    Set the graph padding.
    void
    setViewCenter(double x, double y)
     
    void
    setViewCenter(double x, double y, double z)
     
    void
    setViewPercent(double percent)
     
    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.
    protected boolean
    spriteContains(org.graphstream.ui.graphicGraph.GraphicElement elt, double x, double y)
    Check if a sprite contains the given point (x,y).
    protected Point2D.Double
    spritePositionPx(org.graphstream.ui.graphicGraph.GraphicSprite sprite)
     
     
    org.graphstream.ui.geom.Point3
    transformGuToPx(double x, double y, double z)
     
    org.graphstream.ui.geom.Point3
    transformPxToGu(double x, double y)
     
    protected void
    Compute a transformation that pass from graph units (user space) to a pixel units (device space) so that the view (zoom and centre) requested by the user is produced.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • graph

      protected org.graphstream.ui.graphicGraph.GraphicGraph graph
      The graph.
    • metrics

      protected org.graphstream.ui.view.util.GraphMetrics metrics
      Information on the graph overall dimension and position.
    • autoFit

      protected boolean autoFit
      Automatic centring of the view.
    • center

      protected org.graphstream.ui.geom.Point3 center
      The camera centre of view.
    • zoom

      protected double zoom
      The camera zoom.
    • Tx

      protected AffineTransform Tx
      The graph-space -> pixel-space transformation.
    • xT

      protected AffineTransform xT
      The inverse transform of Tx.
    • oldTx

      protected AffineTransform oldTx
      The previous affine transform.
    • rotation

      protected double rotation
      The rotation angle.
    • padding

      protected org.graphstream.ui.graphicGraph.stylesheet.Values padding
      Padding around the graph.
    • nodeInvisible

      protected HashSet<String> nodeInvisible
      Which node is visible. This allows to mark invisible nodes to fasten visibility tests for nodes, attached sprites and edges.
    • gviewport

      protected double[] gviewport
      The graph view port, if any. The graph view port is a view inside the graph space. It allows to compute the view according to a specified area of the graph space instead of the graph dimensions.
    • gviewportDiagonal

      protected double gviewportDiagonal
  • Constructor Details

    • DefaultCamera

      public DefaultCamera(org.graphstream.ui.graphicGraph.GraphicGraph graph)
      New camera.
  • Method Details

    • getViewCenter

      public org.graphstream.ui.geom.Point3 getViewCenter()
      Specified by:
      getViewCenter in interface org.graphstream.ui.view.camera.Camera
    • setViewCenter

      public void setViewCenter(double x, double y, double z)
      Specified by:
      setViewCenter in interface org.graphstream.ui.view.camera.Camera
    • setViewCenter

      public void setViewCenter(double x, double y)
    • getViewPercent

      public double getViewPercent()
      Specified by:
      getViewPercent in interface org.graphstream.ui.view.camera.Camera
    • setViewPercent

      public void setViewPercent(double percent)
      Specified by:
      setViewPercent in interface org.graphstream.ui.view.camera.Camera
    • getViewRotation

      public double getViewRotation()
      Specified by:
      getViewRotation in interface org.graphstream.ui.view.camera.Camera
    • getMetrics

      public org.graphstream.ui.view.util.GraphMetrics getMetrics()
      Specified by:
      getMetrics in interface org.graphstream.ui.view.camera.Camera
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • resetView

      public void resetView()
      Specified by:
      resetView in interface org.graphstream.ui.view.camera.Camera
    • setBounds

      public void setBounds(double minx, double miny, double minz, double maxx, double maxy, double maxz)
      Specified by:
      setBounds in interface org.graphstream.ui.view.camera.Camera
    • getGraphDimension

      public double getGraphDimension()
      Specified by:
      getGraphDimension in interface org.graphstream.ui.view.camera.Camera
    • isVisible

      public boolean isVisible(org.graphstream.ui.graphicGraph.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 org.graphstream.ui.view.camera.Camera
      Parameters:
      element - The element to test.
      Returns:
      True if the element is visible and therefore must be rendered.
    • transformPxToGu

      public org.graphstream.ui.geom.Point3 transformPxToGu(double x, double y)
      Specified by:
      transformPxToGu in interface org.graphstream.ui.view.camera.Camera
    • transformGuToPx

      public org.graphstream.ui.geom.Point3 transformGuToPx(double x, double y, double z)
      Specified by:
      transformGuToPx in interface org.graphstream.ui.view.camera.Camera
    • checkVisibility

      public void checkVisibility(org.graphstream.ui.graphicGraph.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).
    • findGraphicElementAt

      public org.graphstream.ui.graphicGraph.GraphicElement findGraphicElementAt(org.graphstream.ui.graphicGraph.GraphicGraph graph, EnumSet<org.graphstream.ui.view.util.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:
      findGraphicElementAt in interface org.graphstream.ui.view.camera.Camera
      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.
    • allGraphicElementsIn

      public Collection<org.graphstream.ui.graphicGraph.GraphicElement> allGraphicElementsIn(org.graphstream.ui.graphicGraph.GraphicGraph graph, EnumSet<org.graphstream.ui.view.util.InteractiveElement> types, double x1, double y1, double x2, double y2)
      Specified by:
      allGraphicElementsIn in interface org.graphstream.ui.view.camera.Camera
    • getSpritePosition

      public Point2D.Double getSpritePosition(org.graphstream.ui.graphicGraph.GraphicSprite sprite, Point2D.Double pos, org.graphstream.ui.graphicGraph.stylesheet.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)
      Specified by:
      setGraphViewport in interface org.graphstream.ui.view.camera.Camera
    • removeGraphViewport

      public void removeGraphViewport()
      Specified by:
      removeGraphViewport in interface org.graphstream.ui.view.camera.Camera
    • pushView

      public void pushView(org.graphstream.ui.graphicGraph.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.
    • autoFitView

      protected void autoFitView(Graphics2D g2)
      Compute a transformation matrix that pass from graph units (user space) to pixel units (device space) so that the whole graph is visible.
      Parameters:
      g2 - The Swing graphics.
    • userView

      protected void userView(Graphics2D g2)
      Compute a transformation that pass from graph units (user space) to a pixel units (device space) so that the view (zoom and centre) requested by the user is produced.
      Parameters:
      g2 - The Swing graphics.
    • setAutoFitView

      public void setAutoFitView(boolean on)
      Enable or disable automatic adjustment of the view to see the entire graph.
      Specified by:
      setAutoFitView in interface org.graphstream.ui.view.camera.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 org.graphstream.ui.view.camera.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(org.graphstream.ui.graphicGraph.GraphicGraph graph)
      Set the graph padding.
      Parameters:
      graph - The graphic graph.
    • getPaddingXgu

      protected double getPaddingXgu()
    • getPaddingYgu

      protected double getPaddingYgu()
    • getPaddingXpx

      protected double getPaddingXpx()
    • getPaddingYpx

      protected double getPaddingYpx()
    • isSpriteVisible

      protected boolean isSpriteVisible(org.graphstream.ui.graphicGraph.GraphicSprite sprite)
      Check if a sprite is visible in the current view port.
      Parameters:
      sprite - The sprite to check.
      Returns:
      True if visible.
    • isEdgeVisible

      protected boolean isEdgeVisible(org.graphstream.ui.graphicGraph.GraphicEdge edge)
      Check if an edge is visible in the current view port.
      Parameters:
      edge - The edge to check.
      Returns:
      True if visible.
    • isNodeIn

      protected boolean isNodeIn(org.graphstream.ui.graphicGraph.GraphicNode node, double X1, double Y1, double X2, double Y2)
      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

      protected boolean isEdgeIn(org.graphstream.ui.graphicGraph.GraphicEdge edge, double X1, double Y1, double X2, double Y2)
      Is the given sprite visible in the given area.
      Parameters:
      edge - The edge 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 edge lies in the given area.
    • isSpriteIn

      protected boolean isSpriteIn(org.graphstream.ui.graphicGraph.GraphicSprite sprite, double X1, double Y1, double X2, double Y2)
      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.
    • spritePositionPx

      protected Point2D.Double spritePositionPx(org.graphstream.ui.graphicGraph.GraphicSprite sprite)
    • nodeContains

      protected boolean nodeContains(org.graphstream.ui.graphicGraph.GraphicElement elt, double x, double y)
      Check if a node contains the given point (x,y).
      Parameters:
      elt - The node.
      x - The point abscissa.
      y - The point ordinate.
      Returns:
      True if (x,y) is in the given element.
    • edgeContains

      protected boolean edgeContains(org.graphstream.ui.graphicGraph.GraphicElement elt, double x, double y)
      Check if an edge contains the given point (x,y).
      Parameters:
      elt - The edge.
      x - The point abscissa.
      y - The point ordinate.
      Returns:
      True if (x,y) is in the given element.
    • spriteContains

      protected boolean spriteContains(org.graphstream.ui.graphicGraph.GraphicElement elt, double x, double y)
      Check if a sprite contains the given point (x,y).
      Parameters:
      elt - The sprite.
      x - The point abscissa.
      y - The point ordinate.
      Returns:
      True if (x,y) is in the given element.
    • getSpritePositionFree

      protected Point2D.Double getSpritePositionFree(org.graphstream.ui.graphicGraph.GraphicSprite sprite, Point2D.Double pos, org.graphstream.ui.graphicGraph.stylesheet.StyleConstants.Units units)
      Compute the position of a sprite if it is not attached.
      Parameters:
      sprite - The sprite.
      pos - 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.
    • getSpritePositionNode

      protected Point2D.Double getSpritePositionNode(org.graphstream.ui.graphicGraph.GraphicSprite sprite, Point2D.Double pos, org.graphstream.ui.graphicGraph.stylesheet.StyleConstants.Units units)
      Compute the position of a sprite if attached to a node.
      Parameters:
      sprite - The sprite.
      pos - 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.
    • getSpritePositionEdge

      protected Point2D.Double getSpritePositionEdge(org.graphstream.ui.graphicGraph.GraphicSprite sprite, Point2D.Double pos, org.graphstream.ui.graphicGraph.stylesheet.StyleConstants.Units units)
      Compute the position of a sprite if attached to an edge.
      Parameters:
      sprite - The sprite.
      pos - 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.