diva.canvas.connector
Class ManhattanRouter

java.lang.Object
  extended by diva.canvas.connector.ManhattanRouter

public class ManhattanRouter
extends Object

An object that is used to route a single Manhattan connector. This code is abstracted out into a separate class so that the routing algorithm can be used in other applications. A route consists of a series of segments, which can be created based on the positions and direction of the start and endpoints, and then manipulated to make a more complex route. For example, a segment can be split, adding two new segments to the route. Once the route is complete, a Polyline2D can be extracted from it for the purpose of drawing a connector.

Each segment has a "direction." This is specified as a constant from SwingUtilities, such as SwingUtilities.SOUTH, and indicates the direction of the segment when proceeding from the start to the end of the route.

Each segment in a route can be "pinned." This means that the position of the segment (perpendicular to its direction) will not be changed by any of the routing methods. Typically, if an interactor moves a segment, it will then pin it so that it appears to "stay put." The result is a reasonable compromise between automatic routing and manual control.

Version:
$Revision: 1.7 $
Author:
Michael Shilman, John Reekie

Nested Class Summary
static class ManhattanRouter.Segment
          Inner class representing a segment of the route.
static class ManhattanRouter.SegmentList
          Inner class for a list of segments.
 
Constructor Summary
ManhattanRouter()
          Create a new empty router.
 
Method Summary
 void addSegment(int direction)
          Add a new segment to the route.
 Polyline2D createShape()
          Create a Polyline2D that represents the shape of the current route.
 void deleteSegment(int segment)
          Delete a segment.
 Rectangle2D getRegion(int first, int last)
          Get the region covered by some number of segments.
 ManhattanRouter.Segment getSegment(int segment)
          Get the segment with the given index
 int getSegmentCount()
          Get the number of segments
 Point2D getStart()
          Get the starting point
static void main(String[] argv)
          Test this thing...
 void route(Point2D start, int startDir, Point2D end, int endDir)
          Create a route based on the given start and end points, and the direction leaving and arriving at those points.
 void setSegments(ManhattanRouter.SegmentList segments)
          Set all segments of the route.
 void splitSegment(int segment)
          Split a segment.
 String toString()
          Print the route
 void translateEnd(double dx, double dy)
          Reroute the end section of the route.
 int translateSegment(int segment, double x, double y)
          Translate a segment by the given distance.
 void translateStart(double dx, double dy)
          Translate and reroute the start section of the route.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ManhattanRouter

public ManhattanRouter()
Create a new empty router.

Method Detail

addSegment

public void addSegment(int direction)
Add a new segment to the route. The initial length of the segment is zero.


createShape

public Polyline2D createShape()
Create a Polyline2D that represents the shape of the current route.


deleteSegment

public void deleteSegment(int segment)
Delete a segment. The segment *and* it's following segment will be deleted. In addition, the lengths of the segments before and after the two deleted segments will be adjusted accordingly. This method cannot be called on the first, last, or next to last segment. It can also only be called if the preceding and following segments have the same direction.


getRegion

public Rectangle2D getRegion(int first,
                             int last)
Get the region covered by some number of segments. The returned rectangle covers all segments between and including the given first and last segments. It doesn't include the pixels used to actually draw any graphical objects, so clients will want to enlarge the returned rectangle if using it for damage regions.


getSegment

public ManhattanRouter.Segment getSegment(int segment)
Get the segment with the given index


getSegmentCount

public int getSegmentCount()
Get the number of segments


getStart

public Point2D getStart()
Get the starting point


translateEnd

public void translateEnd(double dx,
                         double dy)
Reroute the end section of the route. Specifically, segments from the last pinned segment up to the end point may be rerouted. New segments may be created if necessary.


translateStart

public void translateStart(double dx,
                           double dy)
Translate and reroute the start section of the route. Specifically, segments from the start up to the first pinned segment may be rerouted. New segments may be created if necessary.


route

public void route(Point2D start,
                  int startDir,
                  Point2D end,
                  int endDir)
Create a route based on the given start and end points, and the direction leaving and arriving at those points.


setSegments

public void setSegments(ManhattanRouter.SegmentList segments)
Set all segments of the route. This method is intended for use by clients that are reconstructing a route from a saved representation.


splitSegment

public void splitSegment(int segment)
Split a segment. The referenced segment will be split in half, thereby inserting two segments into the route: one perpendicular to it of zero length, and another in the same direction of half the length. A client will typically translate one of the two half-segments immediately after this call.


toString

public String toString()
Print the route

Overrides:
toString in class Object

translateSegment

public int translateSegment(int segment,
                            double x,
                            double y)
Translate a segment by the given distance. Segments may be created on either side if needed; in addition, segments on either side may be deleted if this segment "lines up" with a free segment. This method cannot be called on the first or last segments. One of the x or y parameters will be ignored, depending on the direction of the segment. Note that, as a result of calling this method, the index of the segment being moved will change; not only that, the Segment object will be replaced as well. So this method returns the index of the "same" segment after it's been translated.

Often, an interactor that calls this method will also pin the segment so that further calls to translateStart() or translateEnd() do not overwrite the new segment location.


main

public static void main(String[] argv)
Test this thing...



Copyright © 2015 Central Laboratory of the Research Councils. All Rights Reserved.