com.sun.electric.database.geometry
Class GenMath

java.lang.Object
  extended by com.sun.electric.database.geometry.GenMath
Direct Known Subclasses:
DBMath

public class GenMath
extends java.lang.Object

General Math Functions. If you are working in Database Units, you should be using DBMath instead.


Nested Class Summary
static class GenMath.MutableBoolean
          Class to define a Boolean object that can be modified.
static class GenMath.MutableDouble
          Class to define an Double-like object that can be modified.
static class GenMath.MutableInteger
          Class to define an Integer-like object that can be modified.
static class GenMath.MutableLong
          Class to define a Long-like object that can be modified.
 
Field Summary
static java.awt.geom.AffineTransform MATID
          A transformation matrix that does nothing (identity).
static int MAX_SMALL_COORD
          Maximal ineger that is considered "small int".
static int MIN_SMALL_COORD
          Minimal integer that is considered "small int".
 
Constructor Summary
GenMath()
           
 
Method Summary
static java.awt.geom.Point2D addPoints(java.awt.geom.Point2D p, double dx, double dy)
          Method to return the sum of two points.
static
<T> void
addToBag(java.util.Map<T,GenMath.MutableInteger> bag, java.util.Map<T,GenMath.MutableInteger> otherBag)
          Adds to bag another bag.
static
<T> void
addToBag(java.util.Map<T,GenMath.MutableInteger> bag, T key)
          Increments count to object in a bag.
static
<T> void
addToBag(java.util.Map<T,GenMath.MutableInteger> bag, T key, int c)
          Adds to count of object in a bag.
static java.awt.geom.Rectangle2D arcBBox(java.awt.geom.Point2D s, java.awt.geom.Point2D e, java.awt.geom.Point2D c)
          Method to compute the bounding box of the arc that runs clockwise from "s" to "e" and is centered at "c".
static java.awt.geom.Point2D[] arcconnects(int ang, java.awt.geom.Rectangle2D bounds1, java.awt.geom.Rectangle2D bounds2)
          Method to determine whether an arc at angle "ang" can connect the two ports whose bounding boxes are "lx1<=X<=hx1" and "ly1<=Y<=hy1" for port 1 and "lx2<=X<=hx2" and "ly2<=Y<=hy2" for port 2.
static int ceilInt(double x)
           
static long ceilLong(double x)
           
static boolean clipLine(java.awt.geom.Point2D from, java.awt.geom.Point2D to, double lX, double hX, double lY, double hY)
          Method to clip a line against a rectangle (in double-precision).
static boolean clipLine(java.awt.Point from, java.awt.Point to, int lx, int hx, int ly, int hy)
          Method to clip a line against a rectangle (in integer).
static java.awt.Point[] clipPoly(java.awt.Point[] points, int lx, int hx, int ly, int hy)
          Method to clip a polygon against a rectangular region.
static java.awt.geom.Point2D closestPointToLine(java.awt.geom.Point2D p1, java.awt.geom.Point2D p2, java.awt.geom.Point2D pt)
          Method to find the point on a line that is closest to a given point.
static java.awt.geom.Point2D closestPointToSegment(java.awt.geom.Point2D p1, java.awt.geom.Point2D p2, java.awt.geom.Point2D pt)
          Method to find the point on a line segment that is closest to a given point.
static java.awt.geom.Point2D computeArcCenter(java.awt.geom.Point2D c, java.awt.geom.Point2D p1, java.awt.geom.Point2D p2)
          Method used by "ioedifo.c" and "routmaze.c".
static double cos(int angle)
          Method to compute the cosine of an integer angle (in tenth-degrees).
static double cosSmall(int angle)
          Method to compute the cosine of a small integer angle (in tenth-degrees).
static
<T> int
countInBag(java.util.Map<T,GenMath.MutableInteger> bag, T key)
          Method to return the a value at a location in a collection.
static double distBetweenPoints(java.awt.geom.Point2D p1, java.awt.geom.Point2D p2)
          Method to calcute Euclidean distance between two points.
static double distToLine(java.awt.geom.Point2D l1, java.awt.geom.Point2D l2, java.awt.geom.Point2D pt)
          Method to compute the distance between point (x,y) and the line that runs from (x1,y1) to (x2,y2).
static boolean doublesClose(double a, double b)
          Method to compare two double-precision numbers within an approximate epsilon.
static boolean doublesEqual(double a, double b)
          Method to compare two double-precision numbers within an acceptable epsilon.
static boolean doublesEqual(double a, double b, double myEpsilon)
          Method to compare two double-precision numbers within a given epsilon
static boolean doublesLessThan(double a, double b)
          Method to compare two numbers and see if one is less than the other within an acceptable epsilon.
static int figureAngle(double x, double y)
          Method to return the angle of a vector.
static int figureAngle(java.awt.geom.Point2D end1, java.awt.geom.Point2D end2)
          Method to return the angle between two points.
static double figureAngleRadians(java.awt.geom.Point2D end1, java.awt.geom.Point2D end2)
          Method to return the angle between two points.
static java.awt.geom.Point2D[] findCenters(double r, double x01, double y01, double x02, double y02, double d)
          Method to find the two possible centers for a circle whose radius is "r" and has two points (x01,y01) and (x02,y02) on the edge.
static java.awt.geom.Point2D[] findCenters(double r, java.awt.geom.Point2D p1, java.awt.geom.Point2D p2)
          Method to find the two possible centers for a circle given a radius and two edge points.
static int floorInt(double x)
           
static long floorLong(double x)
           
static double getArea(java.awt.geom.Rectangle2D rect)
          Method to compute the area of a given rectangle.
static double getAreaOfPoints(java.awt.geom.Point2D[] points)
          Method to compute the area of a polygon defined by an array of points.
static java.awt.geom.Rectangle2D getQTreeBox(double x, double y, double w, double h, double centerX, double centerY, int loc)
          Calculates the bounding box of a child depending on the location.
static int getQuadrants(double centerX, double centerY, java.awt.geom.Rectangle2D box)
          General method to obtain quadrant for a given box in a qTree based on the qTree center
static int getX(long xy)
          Returns x coordinate packed in a long value by packXY.
static int getY(long xy)
          Returns y coordinate packed in a long value by packXY.
static java.awt.geom.Point2D intersect(java.awt.geom.Point2D p1, int ang1, java.awt.geom.Point2D p2, int ang2)
          Method to determine the intersection of two lines and return that point.
static java.awt.geom.Point2D intersectRadians(java.awt.geom.Point2D p1, double ang1, java.awt.geom.Point2D p2, double ang2)
          Method to determine the intersection of two lines and return that point.
static boolean isNinetyDegreeRotation(int rotation)
          Method to detect if rotation represents a 90 degree rotation in Electric
static boolean isOnLine(java.awt.geom.Point2D end1, java.awt.geom.Point2D end2, java.awt.geom.Point2D pt)
          Method to tell whether a point is on a given line segment.
static boolean isSmallInt(int v)
          Returns true if specified int value is considered "small int".
static boolean isSmallInt(long v)
          Returns true if specified long value is considered "small int".
static boolean objectsReallyEqual(java.lang.Object first, java.lang.Object second)
          Method to compare two objects for equality.
static long packXY(int x, int y)
          Pack a pair of integer coordinates in a long value.
static long polarToXY(int len, int angle)
          Returns Cartesian coordinates of a vector specified in polar coordinates.
static int primeSince(int x)
          Method to return a prime number greater or equal than a give threshold.
static double rint(double x)
           
static int roundInt(double x)
           
static long roundLong(double x)
          Returns the closest long to the argument.
static double sin(int angle)
          Method to compute the sine of an integer angle (in tenth-degrees).
static double sinSmall(int angle)
          Method to compute the sine of a small integer angle (in tenth-degrees).
static double toNearest(double a, double nearest)
          Method to round a value to the nearest increment.
static java.lang.String transformArrayIntoString(double[] s)
          Method to transform an array of doubles into a string that can be stored in a preference.
static double[] transformStringIntoArray(java.lang.String vector)
          Method to extract an array of doubles from a string.
static long unsignedIntValue(int n)
          Method to return a long that represents the unsigned value of an integer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MIN_SMALL_COORD

public static final int MIN_SMALL_COORD
Minimal integer that is considered "small int". Sum and difference of two "small ints" is always in [Integer.MIN_VALUE..Integer.MAX_VALUE] range.

See Also:
Constant Field Values

MAX_SMALL_COORD

public static final int MAX_SMALL_COORD
Maximal ineger that is considered "small int". Sum and difference of two "small ints" is always in [Integer.MIN_VALUE..Integer.MAX_VALUE] range.

See Also:
Constant Field Values

MATID

public static final java.awt.geom.AffineTransform MATID
A transformation matrix that does nothing (identity).

Constructor Detail

GenMath

public GenMath()
Method Detail

getQuadrants

public static int getQuadrants(double centerX,
                               double centerY,
                               java.awt.geom.Rectangle2D box)
General method to obtain quadrant for a given box in a qTree based on the qTree center

Parameters:
centerX - the X center of the qTree.
centerY - the Y center of the qTree.
box - the given box.
Returns:
the quadrant number.

getQTreeBox

public static java.awt.geom.Rectangle2D getQTreeBox(double x,
                                                    double y,
                                                    double w,
                                                    double h,
                                                    double centerX,
                                                    double centerY,
                                                    int loc)
Calculates the bounding box of a child depending on the location. Parameters are passed to avoid extra calculation

Parameters:
x - Parent x value
y - Parent y value
w - Child width (1/4 of parent if qtree)
h - Child height (1/2 of parent if qtree)
centerX - Parent center x value
centerY - Parent center y value
loc - Location in qtree

transformArrayIntoString

public static java.lang.String transformArrayIntoString(double[] s)
Method to transform an array of doubles into a string that can be stored in a preference. The format of the string is "(v1 v2 v3 ...)"

Parameters:
s - the values.
Returns:
string representing the array.

transformStringIntoArray

public static double[] transformStringIntoArray(java.lang.String vector)
Method to extract an array of doubles from a string. The format of the string is "(v1 v2 v3 ...)"

Parameters:
vector - the input vector in string form.
Returns:
the array of values.

addToBag

public static <T> void addToBag(java.util.Map<T,GenMath.MutableInteger> bag,
                                T key)
Increments count to object in a bag. If object was not in a bag, it will be added.

Parameters:
bag - Map implementing Bag.
key - object to add to bag.

addToBag

public static <T> void addToBag(java.util.Map<T,GenMath.MutableInteger> bag,
                                java.util.Map<T,GenMath.MutableInteger> otherBag)
Adds to bag another bag.

Parameters:
bag - bag to update.
otherBag - bag used for update.

addToBag

public static <T> void addToBag(java.util.Map<T,GenMath.MutableInteger> bag,
                                T key,
                                int c)
Adds to count of object in a bag. If object was not in a bag, it will be added.

Parameters:
bag - Map implementing Bag.
key - object in a bag.
c - count to add to bag.

countInBag

public static <T> int countInBag(java.util.Map<T,GenMath.MutableInteger> bag,
                                 T key)
Method to return the a value at a location in a collection.

Parameters:
bag - the collection (a Map).
key - a key to an entry in the collection.
Returns:
the value at that key.

objectsReallyEqual

public static boolean objectsReallyEqual(java.lang.Object first,
                                         java.lang.Object second)
Method to compare two objects for equality. This does more than a simple "equal" because they may have the same value but have diffent type (one Float, the other Double).

Parameters:
first - the first object to compare.
second - the second object to compare.
Returns:
true if they are equal.

isNinetyDegreeRotation

public static boolean isNinetyDegreeRotation(int rotation)
Method to detect if rotation represents a 90 degree rotation in Electric

Parameters:
rotation - the rotation amount.
Returns:
true if it is a 90-degree rotation.

figureAngle

public static int figureAngle(java.awt.geom.Point2D end1,
                              java.awt.geom.Point2D end2)
Method to return the angle between two points.

Parameters:
end1 - the first point.
end2 - the second point.
Returns:
the angle between the points (in tenth-degrees).

figureAngle

public static int figureAngle(double x,
                              double y)
Method to return the angle of a vector. Return 0 if vector is zero or has NaN components.

Parameters:
x - x-coordinate of a vector.
y - y-coordinate of a vector.
Returns:
the angle of a vector.

figureAngleRadians

public static double figureAngleRadians(java.awt.geom.Point2D end1,
                                        java.awt.geom.Point2D end2)
Method to return the angle between two points.

Parameters:
end1 - the first point.
end2 - the second point.
Returns:
the angle between the points (in radians).

getArea

public static double getArea(java.awt.geom.Rectangle2D rect)
Method to compute the area of a given rectangle.

Parameters:
rect - the rectangle
Returns:
the area of the rectangle

getAreaOfPoints

public static double getAreaOfPoints(java.awt.geom.Point2D[] points)
Method to compute the area of a polygon defined by an array of points. Returns always positive numbers

Parameters:
points - the array of points.
Returns:
the area of the polygon defined by these points.

addPoints

public static java.awt.geom.Point2D addPoints(java.awt.geom.Point2D p,
                                              double dx,
                                              double dy)
Method to return the sum of two points.

Parameters:
p - the first point.
dx - the X component of the second point
dy - the T component of the second point
Returns:
the sum of two points.

isOnLine

public static boolean isOnLine(java.awt.geom.Point2D end1,
                               java.awt.geom.Point2D end2,
                               java.awt.geom.Point2D pt)
Method to tell whether a point is on a given line segment.

NOTE: If you are comparing Electric database units, DO NOT use this method. Use the corresponding method from DBMath.

Parameters:
end1 - the first end of the line segment.
end2 - the second end of the line segment.
pt - the point in question.
Returns:
true if the point is on the line segment.

closestPointToSegment

public static java.awt.geom.Point2D closestPointToSegment(java.awt.geom.Point2D p1,
                                                          java.awt.geom.Point2D p2,
                                                          java.awt.geom.Point2D pt)
Method to find the point on a line segment that is closest to a given point.

Parameters:
p1 - one end of the line segment.
p2 - the other end of the line segment.
pt - the point near the line segment.
Returns:
a point on the line segment that is closest to "pt". The point is guaranteed to be between the two points that define the segment.

closestPointToLine

public static java.awt.geom.Point2D closestPointToLine(java.awt.geom.Point2D p1,
                                                       java.awt.geom.Point2D p2,
                                                       java.awt.geom.Point2D pt)
Method to find the point on a line that is closest to a given point.

Parameters:
p1 - one end of the line.
p2 - the other end of the line.
pt - the point near the line.
Returns:
a point on the line that is closest to "pt". The point is not guaranteed to be between the two points that define the line.

arcconnects

public static java.awt.geom.Point2D[] arcconnects(int ang,
                                                  java.awt.geom.Rectangle2D bounds1,
                                                  java.awt.geom.Rectangle2D bounds2)
Method to determine whether an arc at angle "ang" can connect the two ports whose bounding boxes are "lx1<=X<=hx1" and "ly1<=Y<=hy1" for port 1 and "lx2<=X<=hx2" and "ly2<=Y<=hy2" for port 2. Returns true if a line can be drawn at that angle between the two ports and returns connection points in (x1,y1) and (x2,y2)


distBetweenPoints

public static double distBetweenPoints(java.awt.geom.Point2D p1,
                                       java.awt.geom.Point2D p2)
Method to calcute Euclidean distance between two points.

Parameters:
p1 - the first point.
p2 - the second point.
Returns:
the distance between the points.

distToLine

public static double distToLine(java.awt.geom.Point2D l1,
                                java.awt.geom.Point2D l2,
                                java.awt.geom.Point2D pt)
Method to compute the distance between point (x,y) and the line that runs from (x1,y1) to (x2,y2).


computeArcCenter

public static java.awt.geom.Point2D computeArcCenter(java.awt.geom.Point2D c,
                                                     java.awt.geom.Point2D p1,
                                                     java.awt.geom.Point2D p2)
Method used by "ioedifo.c" and "routmaze.c".


findCenters

public static java.awt.geom.Point2D[] findCenters(double r,
                                                  java.awt.geom.Point2D p1,
                                                  java.awt.geom.Point2D p2)
Method to find the two possible centers for a circle given a radius and two edge points.

Parameters:
r - the radius of the circle.
p1 - one point on the edge of the circle.
p2 - the other point on the edge of the circle.
Returns:
an array of two Point2Ds, either of which could be the center. Returns null if there are no possible centers. This code was written by John Mohammed of Schlumberger.

intersect

public static java.awt.geom.Point2D intersect(java.awt.geom.Point2D p1,
                                              int ang1,
                                              java.awt.geom.Point2D p2,
                                              int ang2)
Method to determine the intersection of two lines and return that point.

Parameters:
p1 - a point on the first line.
ang1 - the angle of the first line (in tenth degrees).
p2 - a point on the second line.
ang2 - the angle of the second line (in tenth degrees).
Returns:
a point that is the intersection of the lines. Returns null if there is no intersection point.

intersectRadians

public static java.awt.geom.Point2D intersectRadians(java.awt.geom.Point2D p1,
                                                     double ang1,
                                                     java.awt.geom.Point2D p2,
                                                     double ang2)
Method to determine the intersection of two lines and return that point.

Parameters:
p1 - a point on the first line.
ang1 - the angle of the first line (in radians).
p2 - a point on the second line.
ang2 - the angle of the second line (in radians).
Returns:
a point that is the intersection of the lines. Returns null if there is no intersection point.

arcBBox

public static java.awt.geom.Rectangle2D arcBBox(java.awt.geom.Point2D s,
                                                java.awt.geom.Point2D e,
                                                java.awt.geom.Point2D c)
Method to compute the bounding box of the arc that runs clockwise from "s" to "e" and is centered at "c". The bounding box is returned.


findCenters

public static java.awt.geom.Point2D[] findCenters(double r,
                                                  double x01,
                                                  double y01,
                                                  double x02,
                                                  double y02,
                                                  double d)
Method to find the two possible centers for a circle whose radius is "r" and has two points (x01,y01) and (x02,y02) on the edge. The two center points are returned in (x1,y1) and (x2,y2). The distance between the points (x01,y01) and (x02,y02) is in "d". The routine returns false if successful, true if there is no intersection. This code was written by John Mohammed of Schlumberger.


toNearest

public static double toNearest(double a,
                               double nearest)
Method to round a value to the nearest increment.

Parameters:
a - the value to round.
nearest - the increment to which it should be rounded.
Returns:
the value, rounded to the nearest increment. For example:
toNearest(10.3, 1.0) = 10.0
toNearest(10.3, 0.1) = 10.3
toNearest(10.3, 0.5) = 10.5

doublesEqual

public static boolean doublesEqual(double a,
                                   double b)
Method to compare two double-precision numbers within an acceptable epsilon.

Parameters:
a - the first number.
b - the second number.
Returns:
true if the numbers are equal to 16 decimal places.

doublesEqual

public static boolean doublesEqual(double a,
                                   double b,
                                   double myEpsilon)
Method to compare two double-precision numbers within a given epsilon

Parameters:
a - the first number.
b - the second number.
myEpsilon - the given epsilon
Returns:
true if the values are close.

doublesLessThan

public static boolean doublesLessThan(double a,
                                      double b)
Method to compare two numbers and see if one is less than the other within an acceptable epsilon.

Parameters:
a - the first number.
b - the second number.
Returns:
true if "a" is less than "b" to 16 decimal places.

doublesClose

public static boolean doublesClose(double a,
                                   double b)
Method to compare two double-precision numbers within an approximate epsilon.

NOTE: If you are comparing Electric database units, DO NOT use this method. Use the corresponding method from DBMath.

Parameters:
a - the first number.
b - the second number.
Returns:
true if the numbers are approximately equal (to a few decimal places).

clipLine

public static boolean clipLine(java.awt.geom.Point2D from,
                               java.awt.geom.Point2D to,
                               double lX,
                               double hX,
                               double lY,
                               double hY)
Method to clip a line against a rectangle (in double-precision).

Parameters:
from - one end of the line.
to - the other end of the line.
lX - the low X bound of the clip.
hX - the high X bound of the clip.
lY - the low Y bound of the clip.
hY - the high Y bound of the clip. The points are modified to fit inside of the clip area.
Returns:
true if the line is not visible.

clipLine

public static boolean clipLine(java.awt.Point from,
                               java.awt.Point to,
                               int lx,
                               int hx,
                               int ly,
                               int hy)
Method to clip a line against a rectangle (in integer).

Parameters:
from - one end of the line.
to - the other end of the line.
lx - the low X bound of the clip.
hx - the high X bound of the clip.
ly - the low Y bound of the clip.
hy - the high Y bound of the clip. The points are modified to fit inside of the clip area.
Returns:
true if the line is not visible.

clipPoly

public static java.awt.Point[] clipPoly(java.awt.Point[] points,
                                        int lx,
                                        int hx,
                                        int ly,
                                        int hy)
Method to clip a polygon against a rectangular region.

Parameters:
points - an array of points that define the polygon.
lx - the low X bound of the clipping region.
hx - the high X bound of the clipping region.
ly - the low Y bound of the clipping region.
hy - the high Y bound of the clipping region.
Returns:
an array of Points that are clipped to the region.

roundLong

public static long roundLong(double x)
Returns the closest long to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type long. In other words, the result is equal to the value of the expression:

(long)Math.floor(a + 0.5d)

Special cases:

Parameters:
x - a floating-point value to be rounded to a long.
Returns:
the value of the argument rounded to the nearest long value.
See Also:
Long.MAX_VALUE, Long.MIN_VALUE

rint

public static double rint(double x)

roundInt

public static int roundInt(double x)

floorLong

public static long floorLong(double x)

ceilLong

public static long ceilLong(double x)

floorInt

public static int floorInt(double x)

ceilInt

public static int ceilInt(double x)

sin

public static double sin(int angle)
Method to compute the sine of an integer angle (in tenth-degrees).

Parameters:
angle - the angle in tenth-degrees.
Returns:
the sine of that angle.

cos

public static double cos(int angle)
Method to compute the cosine of an integer angle (in tenth-degrees).

Parameters:
angle - the angle in tenth-degrees.
Returns:
the cosine of that angle.

sinSmall

public static double sinSmall(int angle)
Method to compute the sine of a small integer angle (in tenth-degrees).

Parameters:
angle - the angle in tenth-degrees in range 0..900.
Returns:
the sine of that angle.

cosSmall

public static double cosSmall(int angle)
Method to compute the cosine of a small integer angle (in tenth-degrees).

Parameters:
angle - the angle in tenth-degrees in range [0..900].
Returns:
the cosine of that angle.

polarToXY

public static long polarToXY(int len,
                             int angle)
Returns Cartesian coordinates of a vector specified in polar coordinates. Result vector has integer components. Euclidean length of result vector is not less than specified length. Components of result vector are packed in a long value: x is in 32 less significant bits, y is in 32 most significant bits.

Parameters:
len - positive vector length
angle - the angle in tenth-degrees in range [0..3600).
Returns:
vector packed in a long value.
See Also:
getX(long), getY(long)

packXY

public static long packXY(int x,
                          int y)
Pack a pair of integer coordinates in a long value. x is packed in a low half, and y is packed in a high half of the long. Use getX and getY to unpack coordiantes.

Parameters:
x - x-coordinate
y - y-coordinate
Returns:
a long value with a packed pair of coordinates.
See Also:
getX(long), getY(long)

getX

public static int getX(long xy)
Returns x coordinate packed in a long value by packXY.

See Also:
packXY(int, int)

getY

public static int getY(long xy)
Returns y coordinate packed in a long value by packXY.

See Also:
packXY(int, int)

unsignedIntValue

public static long unsignedIntValue(int n)
Method to return a long that represents the unsigned value of an integer. I.e., the passed int is a set of bits, and this method returns a number as if those bits were interpreted as an unsigned int.


isSmallInt

public static boolean isSmallInt(long v)
Returns true if specified long value is considered "small int". Sum and difference of two "small ints" is always in [Integer.MIN_VALUE..Integer.MAX_VALUE] range.

Parameters:
v - specified long value.
Returns:
true if specified long value is considered "small int".

isSmallInt

public static boolean isSmallInt(int v)
Returns true if specified int value is considered "small int". Sum and difference of two "small ints" is always in [Integer.MIN_VALUE..Integer.MAX_VALUE] range.

Parameters:
v - specified int value.
Returns:
true if specified int value is considered "small int".

primeSince

public static int primeSince(int x)
Method to return a prime number greater or equal than a give threshold. It is possible for every "int" threshold because Integer.MAX_VALUE is a prime number. Prime number is choosen from a table based on table from Knuth's book.

Parameters:
x - threshold
Returns:
a prime number