public class DefaultCamera extends Object implements Camera
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 ?".
Modifier and Type | Field and Description |
---|---|
protected boolean |
autoFit
Automatic centring of the view.
|
protected Point3 |
center
The camera centre of view.
|
protected GraphicGraph |
graph
The graph.
|
protected double[] |
gviewport
The graph view port, if any.
|
protected double |
gviewportDiagonal |
protected GraphMetrics |
metrics
Information on the graph overall dimension and position.
|
protected HashSet<String> |
nodeInvisible
Which node is visible.
|
protected AffineTransform |
oldTx
The previous affine transform.
|
protected Values |
padding
Padding around the graph.
|
protected double |
rotation
The rotation angle.
|
protected AffineTransform |
Tx
The graph-space -> pixel-space transformation.
|
protected AffineTransform |
xT
The inverse transform of Tx.
|
protected double |
zoom
The camera zoom.
|
Constructor and Description |
---|
DefaultCamera(GraphicGraph graph)
New camera.
|
Modifier and Type | Method and Description |
---|---|
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).
|
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.
|
void |
checkVisibility(GraphicGraph graph)
Process each node to check if it is in the actual view port, and mark
invisible nodes.
|
protected boolean |
edgeContains(GraphicElement elt,
double x,
double y) |
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. |
protected double |
getPaddingXgu() |
protected double |
getPaddingXpx() |
protected double |
getPaddingYgu() |
protected double |
getPaddingYpx() |
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.
|
protected Point2D.Double |
getSpritePositionEdge(GraphicSprite sprite,
Point2D.Double pos,
StyleConstants.Units units)
Compute the position of a sprite if attached to an edge.
|
protected Point2D.Double |
getSpritePositionFree(GraphicSprite sprite,
Point2D.Double pos,
StyleConstants.Units units)
Compute the position of a sprite if it is not attached.
|
protected Point2D.Double |
getSpritePositionNode(GraphicSprite sprite,
Point2D.Double pos,
StyleConstants.Units units)
Compute the position of a sprite if attached to a node.
|
Point3 |
getViewCenter()
The view centre (a point in graph units).
|
double |
getViewPercent()
The portion of the graph visible.
|
double |
getViewRotation()
The current rotation angle.
|
protected boolean |
isEdgeVisible(GraphicEdge edge)
Check if an edge is visible in the current view port.
|
protected boolean |
isNodeIn(GraphicNode node,
double X1,
double Y1,
double X2,
double Y2)
Is the given node visible in the given area.
|
protected boolean |
isSpriteIn(GraphicSprite sprite,
double X1,
double Y1,
double X2,
double Y2)
Is the given sprite visible in the given area.
|
protected boolean |
isSpriteVisible(GraphicSprite sprite)
Check if a sprite is visible in the current view port.
|
boolean |
isVisible(GraphicElement element)
True if the element should be visible on screen.
|
protected boolean |
nodeContains(GraphicElement elt,
double x,
double y)
Check if a node contains the given point (x,y).
|
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.
|
protected boolean |
spriteContains(GraphicElement elt,
double x,
double y)
Check if a sprite contains the given point (x,y).
|
protected Point2D.Double |
spritePositionPx(GraphicSprite sprite) |
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.
|
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.
|
protected GraphicGraph graph
protected GraphMetrics metrics
protected boolean autoFit
protected double zoom
protected AffineTransform Tx
protected AffineTransform xT
protected AffineTransform oldTx
protected double rotation
protected HashSet<String> nodeInvisible
protected double[] gviewport
protected double gviewportDiagonal
public DefaultCamera(GraphicGraph graph)
public Point3 getViewCenter()
Camera
getViewCenter
in interface Camera
public void setViewCenter(double x, double y, double z)
Camera
setViewCenter
in interface Camera
x
- The new abscissa.y
- The new ordinate.z
- The new depth.public void setViewCenter(double x, double y)
public double getViewPercent()
Camera
getViewPercent
in interface Camera
public void setViewPercent(double percent)
Camera
setViewPercent
in interface Camera
percent
- Percent of the graph visible.public double getViewRotation()
Camera
getViewRotation
in interface Camera
public GraphMetrics getMetrics()
Camera
GraphMetrics
object linked to this Camera. It can be used
to convert pixels to graphic units and vice versa.getMetrics
in interface Camera
public void resetView()
Camera
public void setBounds(double minx, double miny, double minz, double maxx, double maxy, double maxz)
Camera
public double getGraphDimension()
Camera
getGraphDimension
in interface Camera
public boolean isVisible(GraphicElement element)
public Point3 transformPxToGu(double x, double y)
Camera
transformPxToGu
in interface Camera
x
- The source point abscissa in pixels.y
- The source point ordinate in pixels.public Point3 transformGuToPx(double x, double y, double z)
Camera
transformGuToPx
in interface Camera
public void checkVisibility(GraphicGraph graph)
public GraphicElement findNodeOrSpriteAt(GraphicGraph graph, double x, double y)
graph
- The graph to search for.x
- The point abscissa.y
- The point ordinate.public ArrayList<GraphicElement> allNodesOrSpritesIn(GraphicGraph graph, double x1, double y1, double x2, double y2)
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.public Point2D.Double getSpritePosition(GraphicSprite sprite, Point2D.Double pos, StyleConstants.Units units)
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).public double[] getGraphViewport()
public void setGraphViewport(double minx, double miny, double maxx, double maxy)
Camera
setGraphViewport
in interface Camera
minx
- The minimum abscissa visible.miny
- The minimum ordinate visible.maxx
- The maximum abscissa visible.maxy
- The maximum abscissa visible.Camera.removeGraphViewport()
public void removeGraphViewport()
Camera
removeGraphViewport
in interface Camera
Camera.setGraphViewport(double, double, double, double)
public void pushView(GraphicGraph graph, Graphics2D g2)
popView(Graphics2D)
to restore
the saved transform. You can only push one time the view.g2
- The Swing graphics to change.public void popView(Graphics2D g2)
pushView(GraphicGraph, Graphics2D)
is used.g2
- The Swing graphics to restore.protected void autoFitView(Graphics2D g2)
g2
- The Swing graphics.protected void userView(Graphics2D g2)
g2
- The Swing graphics.public void setAutoFitView(boolean on)
setAutoFitView
in interface Camera
on
- If true, automatic adjustment is enabled.public void setZoom(double z)
z
- The zoom.public void setViewRotation(double theta)
setViewRotation
in interface Camera
theta
- The rotation angle in degrees.public void setViewport(double viewportX, double viewportY, double viewportWidth, double viewportHeight)
viewportWidth
- The width in pixels of the view port.viewportHeight
- The width in pixels of the view port.public void setPadding(GraphicGraph graph)
graph
- The graphic graph.protected double getPaddingXgu()
protected double getPaddingYgu()
protected double getPaddingXpx()
protected double getPaddingYpx()
protected boolean isSpriteVisible(GraphicSprite sprite)
sprite
- The sprite to check.protected boolean isEdgeVisible(GraphicEdge edge)
edge
- The edge to check.protected boolean isNodeIn(GraphicNode node, double X1, double Y1, double X2, double Y2)
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.protected boolean isSpriteIn(GraphicSprite sprite, double X1, double Y1, double X2, double Y2)
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.protected Point2D.Double spritePositionPx(GraphicSprite sprite)
protected boolean nodeContains(GraphicElement elt, double x, double y)
elt
- The node.x
- The point abscissa.y
- The point ordinate.protected boolean edgeContains(GraphicElement elt, double x, double y)
protected boolean spriteContains(GraphicElement elt, double x, double y)
elt
- The sprite.x
- The point abscissa.y
- The point ordinate.protected Point2D.Double getSpritePositionFree(GraphicSprite sprite, Point2D.Double pos, StyleConstants.Units units)
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.protected Point2D.Double getSpritePositionNode(GraphicSprite sprite, Point2D.Double pos, StyleConstants.Units units)
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.protected Point2D.Double getSpritePositionEdge(GraphicSprite sprite, Point2D.Double pos, StyleConstants.Units units)
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.WebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses