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 Details

  • Method Details

    • 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​(Point3 p)
    • 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.
    • 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: Camera
      The current rotation angle.
      Specified by:
      getViewRotation in interface Camera
      Returns:
      The rotation angle in degrees.
    • setViewRotation

      public void setViewRotation​(double theta)
      Description copied from interface: Camera
      Rotate the view around its centre point by a given theta angles (in degrees).
      Specified by:
      setViewRotation in interface Camera
      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: 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.
    • spriteContains

      public boolean spriteContains​(GraphicElement elt, double x, double y)
    • spritePositionPx

      public Point3 spritePositionPx​(GraphicSprite sprite)
    • getSpritePosition

      public Point3 getSpritePosition​(GraphicSprite sprite, Point3 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.
    • 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

      public boolean nodeContains​(GraphicElement elt, double x, double y)
    • edgeContains

      public boolean edgeContains​(GraphicElement elt, double x, double y)
    • getNodeOrSpriteSize

      public Values getNodeOrSpriteSize​(GraphicElement elt)
    • getNodeOrSpritePositionGU

      public Point3 getNodeOrSpritePositionGU​(GraphicElement elt, Point3 pos)
    • 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)
    • 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()
    • resetView

      public void resetView()
      Description copied from interface: Camera
      Reset the view to the automatic mode.
      Specified by:
      resetView in interface Camera
    • pushView

      public void pushView​(GraphicGraph graph)
      Set the camera view in the given graphics and backup the previous transform of the graphics. Call popView() 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

      public void checkVisibility​(GraphicGraph graph)
    • isNodeIn

      public boolean isNodeIn​(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

      public boolean isEdgeIn​(GraphicEdge edge, double X1, double Y1, double X2, double Y2)
    • setPadding

      public void setPadding​(GraphicGraph graph)
      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).
      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.
    • setBounds

      public void setBounds​(GraphicGraph graph)
      Set the graphic graph bounds from the graphic graph.
    • getMetrics

      public GraphMetrics getMetrics()
      Description copied from interface: Camera
      Get the org.graphstream.ui.swingViewer.util.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
    • 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.
    • setBackend

      public void setBackend​(Backend backend)
    • transformGuToPx

      public Point3 transformGuToPx​(double x, double y, double z)
      Transform a point in graph units into pixels.
      Specified by:
      transformGuToPx in interface Camera
      Returns:
      The transformed point.
    • transformPxToGu

      public 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. 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.
    • isTextVisible

      public boolean isTextVisible​(GraphicElement element)
    • 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.
    • isSpriteVisible

      public boolean isSpriteVisible​(GraphicSprite sprite)
      Check if a sprite is visible in the current view port.
      Parameters:
      sprite - The sprite to check.
      Returns:
      True if visible.
    • isSpriteIn

      public boolean isSpriteIn​(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.
    • isEdgeVisible

      public boolean isEdgeVisible​(GraphicEdge edge)
      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:
      findGraphicElementAt in interface 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.
    • graphViewport

      public double[] graphViewport()
    • allGraphicElementsIn

      public Collection<GraphicElement> allGraphicElementsIn​(GraphicGraph graph, EnumSet<InteractiveElement> types, double x1, double y1, double x2, double y2)
      Specified by:
      allGraphicElementsIn in interface Camera
    • toString

      public String toString()
      Overrides:
      toString in class Object