cz.cuni.jagrlib
Class Geometry

java.lang.Object
  extended by cz.cuni.jagrlib.Geometry

public class Geometry
extends java.lang.Object

Simple geometric routines.

Since:
0.12
See Also:
Geometry.java

Field Summary
static double EPSILON
          The smallest absolute value that can be inverted.
static float EPSILON_F
          The smallest absolute value that can be inverted.
static double SCENE_EPSILON
          Upper bound for 3D rounding errors (two 3D points closer than SCENE_EPSILON are assumed to be equal).
 
Constructor Summary
Geometry()
           
 
Method Summary
static double dotProduct3D(double[] a, double[] b)
          Computes dot product of two 3D vectors.
static void getAxes(double[] p, double[] p1, double[] p2)
          Finds two other axes to the given vector, their vector product will give the original vector, all three vectors will be perpendicular to each other.
static void getPerpendiculars(double[] p, double[] p1, double[] p2)
          Finds two vectors perpendicular to the given vector (so that their vector product will give the original vector).
static boolean isZero(double x)
          Compares the given number with zero (using EPSILON tolerance).
static boolean isZero(float x)
          Compares the given number with zero (using EPSILON_F tolerance).
static double normalize3D(double[] v)
          Normalizes the given 3D vector (Cartesian or homogeneous) in place.
static double[] normalize3D(double[] from, double[] to)
          Normalizes the given 3D vector (Cartesian or homogeneous) and assigns it.
static double pointBox2D(double[] p, double[] ul, double[] size)
          Computes 2D distance between point p and the rectangular box ul, ul+size.
static double pointBox3D(double[] p, double[] ul, double[] size)
          Computes 3D distance between point p and the rectangular box ul, ul+size.
static double[] pointInTriangle2D(double[] p, double[] a, double[] b, double[] c, double[] uv)
          Tests whether the given point lies in the given triangle.
static double pointPoint3D(double[] a, double[] b)
          Computes Euclidean distance of two 3D points.
static double pointSegment2D(double[] p, double[] a, double[] b)
          Computes 2D distance between point p and line segment ab.
static double pointSegment2D(double px, double py, double ax, double ay, double bx, double by)
          Computes 2D distance between point [ px, py ] and line segment [ ax, ay ] [ bx, by ].
static double pointSegment3D(double[] p, double[] a, double[] b)
          Computes 3D distance between point p and the line segment ab.
static boolean rayBox(double[] origin, double[] direction, double[] ul, double[] size, double[] result)
          Computes intersection of the ray with the given AABB (works in either dimension).
static double raySegment2D(double[] origin, double[] direction, double[] a, double[] b)
          Computes intersection of the given ray with the given line segment.
static double raySegment2D(double ox, double oy, double dx, double dy, double ax, double ay, double bx, double by)
          Computes intersection of the given ray with the given line segment.
static double rayTriangle3D(double[] origin, double[] direction, double[] a, double[] b, double[] c, double[] uv)
          Ray-triangle intersection test in 3D.
static double[] specularReflection(double[] normal, double[] in, double[] out)
          Computes direction vector of perfect specular-reflected ray.
static double[] specularRefraction(double[] normal, double n, double[] in, double[] out)
          Computes direction vector of perfect specular-refracted ray.
static double[] toCartesian(double sinTheta, double cosTheta, double phi, double[] result)
          Converts spherical coordinate vector to Cartesian 3D coordinates.
static double[] toCartesian(double x, double y, double z, double w, double[] to)
          Converts the given coordinate vector to Cartesian 3D coordinates.
static double[] toCartesian3D(double[] from, double[] to)
          Converts the given coordinate vector to Cartesian 3D coordinates.
static double triangleArea(double[][] v)
          Computes area of the given triangle.
static double[] triangleNormal(double[][] v, double[] normal)
          Computes normal vector of the given triangle.
static double[] vector(double[] a, double[] b, double[] result)
          Computes a vector from its endpoints.
static double[] vectorProduct(double[] a, double[] b, double[] result)
          Computes vector product of two (either cartesian or homogeneous) 3D vectors.
static java.lang.String vectorToString(double[] v)
          Converts double[] vector to readable form.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EPSILON

public static final double EPSILON
The smallest absolute value that can be inverted.


EPSILON_F

public static final float EPSILON_F
The smallest absolute value that can be inverted.


SCENE_EPSILON

public static final double SCENE_EPSILON
Upper bound for 3D rounding errors (two 3D points closer than SCENE_EPSILON are assumed to be equal).

See Also:
Constant Field Values
Constructor Detail

Geometry

public Geometry()
Method Detail

isZero

public static final boolean isZero(double x)
Compares the given number with zero (using EPSILON tolerance).


isZero

public static final boolean isZero(float x)
Compares the given number with zero (using EPSILON_F tolerance).


pointPoint3D

public static final double pointPoint3D(double[] a,
                                        double[] b)
Computes Euclidean distance of two 3D points.

Parameters:
a - The 1st point.
b - The 2nd point.
Returns:
Euclidean distance of the two points (or Double.POSITIVE_INFINITY in case of error).

pointSegment2D

public static final double pointSegment2D(double[] p,
                                          double[] a,
                                          double[] b)
Computes 2D distance between point p and line segment ab.

Parameters:
p - Point coordinates.
a - Segment's starting point.
b - Segment's end point.
Returns:
Distance or Double.POSITIVE_INFINITY in case of error.

pointSegment2D

public static final double pointSegment2D(double px,
                                          double py,
                                          double ax,
                                          double ay,
                                          double bx,
                                          double by)
Computes 2D distance between point [ px, py ] and line segment [ ax, ay ] [ bx, by ].


pointSegment3D

public static final double pointSegment3D(double[] p,
                                          double[] a,
                                          double[] b)
Computes 3D distance between point p and the line segment ab.

Parameters:
p - Point coordinates.
a - Segment's starting point.
b - Segment's end point.
Returns:
Distance or Double.POSITIVE_INFINITY in case of error.

pointBox2D

public static final double pointBox2D(double[] p,
                                      double[] ul,
                                      double[] size)
Computes 2D distance between point p and the rectangular box ul, ul+size.

Parameters:
p - Point coordinates.
ul - Upper left rectangle corner.
size - Box size.
Returns:
Distance or -1.0 if the point lies inside the rectangle.

pointBox3D

public static final double pointBox3D(double[] p,
                                      double[] ul,
                                      double[] size)
Computes 3D distance between point p and the rectangular box ul, ul+size.

Parameters:
p - Point coordinates.
ul - Upper left box corner.
size - Box size.
Returns:
Distance or -1.0 if the point lies inside the box.

raySegment2D

public static final double raySegment2D(double[] origin,
                                        double[] direction,
                                        double[] a,
                                        double[] b)
Computes intersection of the given ray with the given line segment. Even negative intersections are computed (ray-parameter t < 0.0). Returns Parametric coordinate on the ray if succeeded, Double.NEGATIVE_INFINITY otherwise.


raySegment2D

public static final double raySegment2D(double ox,
                                        double oy,
                                        double dx,
                                        double dy,
                                        double ax,
                                        double ay,
                                        double bx,
                                        double by)
Computes intersection of the given ray with the given line segment. Even negative intersections are computed (ray-parameter t < 0.0). Returns Parametric coordinate on the ray if succeeded, Double.NEGATIVE_INFINITY otherwise.


rayBox

public static final boolean rayBox(double[] origin,
                                   double[] direction,
                                   double[] ul,
                                   double[] size,
                                   double[] result)
Computes intersection of the ray with the given AABB (works in either dimension). Returns true if intersection exists (in such case the pre-allocated result array will be filled with [ tmin. tmax ]).


rayTriangle3D

public static final double rayTriangle3D(double[] origin,
                                         double[] direction,
                                         double[] a,
                                         double[] b,
                                         double[] c,
                                         double[] uv)
Ray-triangle intersection test in 3D. According to Tomas Moller, JGT.
origin + t * direction = (1 - u - v) * a + u * b + v * c.

Parameters:
uv - Pre-allocated array to hold barycentric coordinates.
Returns:
Parametric coordinate on the ray if succeeded, Double.NEGATIVE_INFINITY otherwise.

pointInTriangle2D

public static final double[] pointInTriangle2D(double[] p,
                                               double[] a,
                                               double[] b,
                                               double[] c,
                                               double[] uv)
Tests whether the given point lies in the given triangle. In positive case: computes barycentric coordinates of the point:

p = (1 - u - v) * a + u * b + v * c.

Parameters:
p - 2D point.
a - Vertex A of the triangle.
b - Vertex B of the triangle.
c - Vertex C of the triangle.
uv - Pre-allocated array to hold barycentric coordinates.
Returns:
Barycentric coordinate-array in positive case, null otherwise.

vector

public static final double[] vector(double[] a,
                                    double[] b,
                                    double[] result)
Computes a vector from its endpoints.

Parameters:
a - Vector's starting point.
b - Vector's end point.
result - Optional pre-allocated result array (double[3] or double[4]).
Returns:
Vector coordinates or null for bad parameters, double[3] for result == null.

triangleArea

public static final double triangleArea(double[][] v)
Computes area of the given triangle. Triangle is defined by its vertices (cartesian .. [x,y,z]).


triangleNormal

public static final double[] triangleNormal(double[][] v,
                                            double[] normal)
Computes normal vector of the given triangle. Triangle is defined by its vertices (cartesian .. [x,y,z]).


dotProduct3D

public static final double dotProduct3D(double[] a,
                                        double[] b)
Computes dot product of two 3D vectors.

Parameters:
a - The first argument.
b - The second argument.
Returns:
Dot product.

vectorProduct

public static final double[] vectorProduct(double[] a,
                                           double[] b,
                                           double[] result)
Computes vector product of two (either cartesian or homogeneous) 3D vectors.

Parameters:
a - The first argument.
b - The second argument.
result - Optional pre-allocated result array (double[3] or double[4]).
Returns:
Vector product: null for bad parameters, double[3] for result == null.

getPerpendiculars

public static final void getPerpendiculars(double[] p,
                                           double[] p1,
                                           double[] p2)
Finds two vectors perpendicular to the given vector (so that their vector product will give the original vector).

Parameters:
p - Inout vector (double[3] or double[4]).
p1 - The 1st output vector (pre-allocated array of size 3 or 4).
p2 - The 2nd output vector (pre-allocated array of size 3 or 4).
See Also:
vectorProduct(double[], double[], double[]), getAxes(double[], double[], double[])

getAxes

public static final void getAxes(double[] p,
                                 double[] p1,
                                 double[] p2)
Finds two other axes to the given vector, their vector product will give the original vector, all three vectors will be perpendicular to each other.

Parameters:
p - Inout vector (double[3] or double[4]).
p1 - The 1st output vector (pre-allocated array of size 3 or 4).
p2 - The 2nd output vector (pre-allocated array of size 3 or 4).
See Also:
vectorProduct(double[], double[], double[]), getPerpendiculars(double[], double[], double[])

specularReflection

public static final double[] specularReflection(double[] normal,
                                                double[] in,
                                                double[] out)
Computes direction vector of perfect specular-reflected ray.

Parameters:
normal - Normal vector (points to the outside of the solid). double[3] or double[4].
in - Input vector (points from the intersection point to the light source). double[3] or double[4].
out - Pre-allocated array to hold result - points from the intersection point. double[3] or double[4], can be null.
Returns:
Direction vector which points from the intersection point.
See Also:
specularRefraction(double[], double, double[], double[])

specularRefraction

public static final double[] specularRefraction(double[] normal,
                                                double n,
                                                double[] in,
                                                double[] out)
Computes direction vector of perfect specular-refracted ray.

Parameters:
normal - Normal vector (double[3] or double[4]).
n - Relative index of refraction (v(normal) / v(-normal)).
in - Input vector (points from the intersection point to the light source). double[3] or double[4].
out - Pre-allocated array to hold result - points from the intersection point. double[3] or double[4], can be null.
Returns:
Direction vector which points from the intersection point or null in case of total reflection.
See Also:
specularReflection(double[], double[], double[])

normalize3D

public static final double normalize3D(double[] v)
Normalizes the given 3D vector (Cartesian or homogeneous) in place.

Parameters:
v - Vector to be normalized (double[3] or double[4]). v[3] will be ignored (not changed).
Returns:
Size (Euclidean norm) of the original vector.

normalize3D

public static final double[] normalize3D(double[] from,
                                         double[] to)
Normalizes the given 3D vector (Cartesian or homogeneous) and assigns it.

Parameters:
from - Source coordinate vector (v[3] /if present/ will be ignored).
to - Pre-allocated destination coordinate vector (double[3] or double[4]).
Returns:
Normalized vector (double[3] for from == null) or null in case of error.

toCartesian3D

public static final double[] toCartesian3D(double[] from,
                                           double[] to)
Converts the given coordinate vector to Cartesian 3D coordinates.

Parameters:
from - Source coordinate vector (double[3] or double[4]).
to - Pre-allocated destination coordinate vector (double[3] or null).
Returns:
Cartesian 3D coordinate vector or null in case of error.
See Also:
toCartesian(double, double, double, double, double[])

toCartesian

public static final double[] toCartesian(double x,
                                         double y,
                                         double z,
                                         double w,
                                         double[] to)
Converts the given coordinate vector to Cartesian 3D coordinates.

Parameters:
x - X coordinate.
y - Y coordinate.
z - Z coordinate.
w - W coordinate.
to - Pre-allocated destination coordinate vector (double[3] or null).
Returns:
Cartesian 3D coordinate vector.
See Also:
toCartesian3D(double[], double[])

toCartesian

public static final double[] toCartesian(double sinTheta,
                                         double cosTheta,
                                         double phi,
                                         double[] result)
Converts spherical coordinate vector to Cartesian 3D coordinates.

Parameters:
sinTheta - Sinus of angle theta.
cosTheta - Cosinus of angle theta.
phi - Angle phi.
result - Pre-allocated destination coordinate vector (double[3] or null).
Returns:
Cartesian 3D coordinate vector.
See Also:
toCartesian3D(double[], double[])

vectorToString

public static final java.lang.String vectorToString(double[] v)
Converts double[] vector to readable form.