java.lang.Object
de.grogra.vecmath.Math2
This class contains a set of mathematical functions and constants,
including random and noise generators, linear algebra, and
table-based trigonometric functions.
- Author:
- Ole Kniemeyer
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final float
The very small number epsilon. 1E-05static final float
The very small number epsilon. 1E-09static float
The constant 1 / (2 π) asfloat
.static float
The constant 1 / π asfloat
.static float
The constant 2 π asfloat
.static float
The constant π asfloat
.static float
The constant π / 2 asfloat
.static int
Numbers of skysegments for the turtleskystatic final Vector3d[]
Define the turtlesky -
Method Summary
Modifier and TypeMethodDescriptionstatic float
static float
static float
Computated the distance between two vectorsstatic float
Calculates the distance between the point pp and the piece of straight line delimited by sa and se.static int
binomial
(int n, int k) static float
ccos
(char c) Returns the cosine of the argument.static double
clamp
(double value, double min, double max) static float
clamp
(float value, float min, float max) static int
clamp
(int value, int min, int max) static long
clamp
(long value, long min, long max) static double
closestConnection
(Tuple3d p, Vector3d v, Tuple3d q) static float
csin
(char c) Returns the sine of the argument.static void
cutCone
(Vector3d ss, float alpha, Vector3d pp, Vector3d qq, CutConeParameter ccp) static void
cutRay2
(float a1, float a2, float r1, float r2, float u1, float u2, float v1, float v2, CutRay2Parameter crp) static void
decomposeQR
(Matrix3d r, Matrix3d q) Computes a QR decomposition ofr
.static float
determinate3
(Vector3d a1, Vector3d a2, Vector3d a3) static double
static void
This method represents the derivative of a Perlin-style 3D noise function.static double
static double
Computes the scalar product(p - q) v
static float
static float
static void
dTurbulence
(Tuple3f out, float x, float y, float z, int octaves, float lambda, float omega) static double
estimateScaleSquared
(Matrix4d transformation) static int
factorial
(int n) static int
floor
(double x) static float
fresnel
(Vector3f normal, Vector3f in, float iorRatio, Vector3f reflectedOut, Vector3f transmittedOut) Computes reflected and transmitted directions according to Fresnel's formulas.static float
gamma
(float x) static void
getBeginAndEndOfShoot
(Matrix4d m, double length, Vector3d beginOfShoot, Vector3d endOfShoot) static void
getBeginOfShoot
(Matrix4d m, Vector3d beginOfShoot) static void
getEndOfShoot
(Matrix4d m, double length, Vector3d endOfShoot) static void
getOrthogonal
(Tuple3d in, Tuple3d out) Computes an orthogonal vector toin
.static void
getOrthogonal
(Tuple3f in, Tuple3f out) Computes an orthogonal vector toin
.static void
getOrthogonalBasis
(Tuple3d in, Matrix3d out, boolean orthonormal) Computes an orthogonal basis.static void
getOrthogonalBasis
(Tuple3f in, Matrix3f out, boolean orthonormal) Computes an orthogonal basis.static double
intersectLineWithFrustum
(double px, double py, double pz, double dx, double dy, double dz, double ax, double ay, double az, double top, double base, double tan) Determines the fraction of a line (p,d) which intersects a frustum.static double
intersectLineWithFrustum
(Point3d point, Vector3d direction, Point3d origin, Vector3d axis, double top, double base, double tan) static void
invertAffine
(Matrix34d in, Matrix4d out) static void
invertAffine
(Matrix3d in, Matrix3d out) static void
invertAffine
(Matrix3f in, Matrix3f out) static void
invertAffine
(Matrix4d in, Matrix4d out) static void
invertAffine
(Matrix4f in, Matrix4f out) static void
static void
static void
invTransformPoint
(Matrix3d t, Tuple2d p) static void
invTransformPoint
(Matrix3f t, Tuple2f p) static void
invTransformPoint
(Matrix4d t, Tuple3d p) static void
invTransformPointAndVector
(Matrix4d t, Tuple3d p, Tuple3d v) static void
static void
static void
static void
static boolean
isInsideConeT
(double px, double py, double pz, double ax, double ay, double az, double tipDistance, double capDistance, double tan) Determines if a point p lies inside a (part of a) cone.static boolean
isInsideConeT
(double px, double py, double pz, double ox, double oy, double oz, double ax, double ay, double az, double tipDistance, double capDistance, double tan) static boolean
Test of all parts of the vector are nullstatic boolean
static boolean
lessThanOrEqual
(Tuple3d a, Tuple3d b) static void
static void
static void
static void
static void
static void
static void
static void
static void
static void
static void
static void
static void
static void
static void
static void
static void
static long
nextRandom
(long seed) Returns the next 48-bit pseudo-random number based on the previous pseudo-random numberseed
.static float
noise
(float x, float y, float z) This method represents a Perlin-style 3D noise function.static void
Normalizes this vector in place.static void
Normalizes this vector in place.static void
static boolean
parallelVector
(Vector3d a1, Vector3d a2) static long
pow
(int a, int b) static boolean
quadricIntersection
(double a00, double a01, double a02, double a11, double a12, double a22, double b0, double b1, double b2, double c, Point3d p, Vector3d d, double[] lambda) static char
random
(char index) This method represents a random permutation ofchar
values.static int
roundUpNextPowerOfTwo
(int v) Calculate the next power of two greater or equal than v.static void
static void
setAffineTransform
(AffineTransform out, Matrix3d in) static void
setMatrix3d
(Matrix3d out, AffineTransform in) static double
shortestConnection
(Tuple3d p1, Vector3d v1, Tuple3d p2, Vector3d v2, double[] lambda) Computes the shortest straight connection between two lines.static double
shortestConnection
(Tuple3d p1, Vector3d v1, Tuple3f p2, Vector3f v2, double[] lambda) Computes the shortest straight connection between two lines.static float
shortestConnection
(Tuple3f p1, Vector3f v1, Tuple3f p2, Vector3f v2, float[] lambda) Computes the shortest straight connection between two lines.static float
sin
(float x) Returns the sine of the argument.static float
sin01
(float x) Returns the sine of the argument.static int
skySegment
(Vector3d rv) static float[]
static float[]
static float[]
static void
transformPoint
(Matrix3d t, Tuple2d p) static void
transformPoint
(Matrix4d t, Tuple3d p) static void
transformPoint
(Matrix4d t, Tuple3d p, Tuple3d out) static void
transformPoint
(Matrix4f t, Tuple3f p) static void
static void
transformVector
(Matrix3d t, Tuple2d v) static void
transformVector
(Matrix4d t, Tuple3d v) static void
transformVector
(Matrix4f t, Tuple3f v) static float
turbulence
(float x, float y, float z, int octaves, float lambda, float omega)
-
Field Details
-
M_PI
public static float M_PIThe constant π asfloat
. -
M_PI_2
public static float M_PI_2The constant π / 2 asfloat
. -
M_2PI
public static float M_2PIThe constant 2 π asfloat
. -
M_1_PI
public static float M_1_PIThe constant 1 / π asfloat
. -
M_1_2PI
public static float M_1_2PIThe constant 1 / (2 π) asfloat
. -
EPSILON
public static final float EPSILONThe very small number epsilon. 1E-09- See Also:
-
EPS
public static final float EPSThe very small number epsilon. 1E-05- See Also:
-
nbSkySegments
public static int nbSkySegmentsNumbers of skysegments for the turtlesky -
turtsky
Define the turtlesky
-
-
Method Details
-
random
public static char random(char index) This method represents a random permutation ofchar
values. I.e., it is a bijective map onchar
s, the mapping being defined by a pseudo-random number generator. The mapping remains the same between different invocations of the Java Virtual Machine.- Parameters:
index
- an index into a permutation table- Returns:
- the permuted value
-
noise
public static float noise(float x, float y, float z) This method represents a Perlin-style 3D noise function. It is implemented based on POV-Ray's Noise function.- Parameters:
x
- x-coordinatey
- x-coordinatez
- x-coordinate- Returns:
- noise value at (x, y, z)
-
dNoise
This method represents the derivative of a Perlin-style 3D noise function. It is implemented based on POV-Ray's DNoise function.- Parameters:
out
- the computed derivate at (x, y, z) is placed in herex
- x-coordinatey
- x-coordinatez
- x-coordinate
-
dTurbulence
public static void dTurbulence(Tuple3f out, float x, float y, float z, int octaves, float lambda, float omega) -
turbulence
public static float turbulence(float x, float y, float z, int octaves, float lambda, float omega) -
csin
public static float csin(char c) Returns the sine of the argument. The range ofchar
values is mapped to [0, 2 π), i.e., the angle in radians is 2 πc
/ 0x10000. This method uses a pre-computed table.- Parameters:
c
- an angle- Returns:
- the sine of the angle
-
ccos
public static float ccos(char c) Returns the cosine of the argument. The range ofchar
values is mapped to [0, 2 π), i.e., the angle in radians is 2 πc
/ 0x10000. This method uses a pre-computed table.- Parameters:
c
- an angle- Returns:
- the cosine of the angle
-
sin
public static float sin(float x) Returns the sine of the argument. This method uses a pre-computed table.- Parameters:
x
- an angle, in radians- Returns:
- the sine of the angle
-
sin01
public static float sin01(float x) Returns the sine of the argument. The range of [0, 1] forx
is mapped to [0, 2 π], i.e., the angle in radians is 2 πx
. This method uses a pre-computed table.- Parameters:
x
- an angle- Returns:
- the sine of the angle
-
nextRandom
public static long nextRandom(long seed) Returns the next 48-bit pseudo-random number based on the previous pseudo-random numberseed
. The sequence of numbers is the same as the sequence computed byjava.util.Random
:nextRandom = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
However, this method should be slightly faster because it does not have to deal with multi-threading issues.Pseudo-random
int
s ofn
bits should be obtained from the returned value by the formula(int) (nextRandom >>> (48 - n))
.- Parameters:
seed
- previous number of sequence- Returns:
- next number of sequence
-
floor
public static int floor(double x) -
gamma
public static float gamma(float x) -
quadricIntersection
-
distance
-
closestConnection
-
shortestConnection
public static double shortestConnection(Tuple3d p1, Vector3d v1, Tuple3d p2, Vector3d v2, double[] lambda) Computes the shortest straight connection between two lines. This method computes two scalarslambda[0]
andlambda[1]
such that the line fromp1 + lambda[0] v1
top2 + lambda[1] v2
is the shortest connection line between these lines.- Parameters:
p1
- a point on the first linev1
- the direction of the first linep2
- a point on the second linev2
- the direction of the second linelambda
- the computed scalars are written into this array- Returns:
- the length of the shortest connection
-
shortestConnection
public static double shortestConnection(Tuple3d p1, Vector3d v1, Tuple3f p2, Vector3f v2, double[] lambda) Computes the shortest straight connection between two lines. This method computes two scalarslambda[0]
andlambda[1]
such that the line fromp1 + lambda[0] v1
top2 + lambda[1] v2
is the shortest connection line between these lines.- Parameters:
p1
- a point on the first linev1
- the direction of the first linep2
- a point on the second linev2
- the direction of the second linelambda
- the computed scalars are written into this array- Returns:
- the length of the shortest connection
-
shortestConnection
public static float shortestConnection(Tuple3f p1, Vector3f v1, Tuple3f p2, Vector3f v2, float[] lambda) Computes the shortest straight connection between two lines. This method computes two scalarslambda[0]
andlambda[1]
such that the line fromp1 + lambda[0] v1
top2 + lambda[1] v2
is the shortest connection line between these lines.- Parameters:
p1
- a point on the first linev1
- the direction of the first linep2
- a point on the second linev2
- the direction of the second linelambda
- the computed scalars are written into this array- Returns:
- the length of the shortest connection
-
mulAffine
-
invertAffine
-
invertAffine
-
invTransformVector
-
invTransformPoint
-
invTransformVector
-
invTransformPoint
-
transformVector
-
transformPoint
-
setAffineTransform
-
setMatrix3d
-
invertAffine
-
invertAffine
-
invertAffine
-
makeAffine
-
makeAffine
-
makeAffine
-
makeAffine
-
mulAffine
-
setAffine
-
invMul
-
invTransformVector
-
invTransformVector
-
invTransformPoint
-
invTransformPoint
-
invTransformPointAndVector
-
transformVector
-
transformPoint
-
transformPoint
-
transformTranspose
-
transformVector
-
transformPoint
-
lmul
-
getOrthogonal
Computes an orthogonal vector toin
. The computed vector is written toout
, which may be the same reference asin
.- Parameters:
in
- an input vectorout
- the computed orthogonal vector is placed in here
-
getOrthogonal
Computes an orthogonal vector toin
. The computed vector is written toout
, which may be the same reference asin
.- Parameters:
in
- an input vectorout
- the computed orthogonal vector is placed in here
-
getOrthogonalBasis
Computes an orthogonal basis. The first two columns of the computed basisout
are unit vectors, the third column equalsin
iforthonormal
isfalse
, otherwise it equals the unit vector in the direction ofin
. Thus,out
represents a coordinate transformation which transforms the local z-axis into the direction ofin
.- Parameters:
in
- the direction of the local z-axisout
- the computed matrixorthonormal
- compute an orthonormal matrix?
-
getOrthogonalBasis
Computes an orthogonal basis. The first two columns of the computed basisout
are unit vectors, the third column equalsin
iforthonormal
isfalse
, otherwise it equals the unit vector in the direction ofin
. Thus,out
represents a coordinate transformation which transforms the local z-axis into the direction ofin
.- Parameters:
in
- the direction of the local z-axisout
- the computed matrixorthonormal
- compute an orthonormal matrix?
-
decomposeQR
Computes a QR decomposition ofr
. The non-singular matrixa
to be composed is the parameterr
. This method computesr
andq
such thata = q * r
,q
is an orthogonal matrix, andr
an upper triangular matrix. The signs ofr.m00
andr.m11
are positive. The sign ofr.m22
is the sign ofdet(a)
. As a consequence,det(q) == 1
, soq
is a pure rotation.- Parameters:
r
- the input for the matrix to compose and the output for rq
- the output for q
-
isInsideConeT
public static boolean isInsideConeT(double px, double py, double pz, double ox, double oy, double oz, double ax, double ay, double az, double tipDistance, double capDistance, double tan) -
isInsideConeT
public static boolean isInsideConeT(double px, double py, double pz, double ax, double ay, double az, double tipDistance, double capDistance, double tan) Determines if a point p lies inside a (part of a) cone. The cone's tip is located at the origin, its axis is given by a and points to the center of the cone's cap which is located at axis * capDistance. The small cone from the tip to axis * tipDistance is removed, i.e., this tests actually checks againts a frustum. The half-angle of the cone is specified by its tangent.- Parameters:
px
- x coordinate of point ppy
- y coordinate of point ppz
- z coordinate of point pax
- x coordinate of cone axis aay
- y coordinate of cone axis aaz
- z coordinate of cone axis atipDistance
- distance to the tip (in units of axis)capDistance
- distance of the cap (in units of axis)tan
- tangent of cone's half angle- Returns:
- true iff p lies within or on the cone
-
intersectLineWithFrustum
-
intersectLineWithFrustum
public static double intersectLineWithFrustum(double px, double py, double pz, double dx, double dy, double dz, double ax, double ay, double az, double top, double base, double tan) Determines the fraction of a line (p,d) which intersects a frustum.The frustum is specified by the cone from which it is obtained by cutting away the top: The cone's tip is located at the origin, its axis is given by a and points to the center of the cone's cap which is located at base * a. The half-angle of the cone is specified by its tangent. The cutting plane for obtaining the frustum is located at the position top * a.
The line is specified by its origin p and its vector d from the origin to the end.
- Parameters:
px
- x coordinate of line origin ppy
- y coordinate of line origin ppz
- z coordinate of line origin pdx
- x coordinate of line vector ddy
- y coordinate of line vector ddz
- z coordinate of line vector dax
- x coordinate of cone axis aay
- y coordinate of cone axis aaz
- z coordinate of cone axis atop
- relative position of frustum's top cap along axisbase
- relative position of frustum's base cap along axistan
- tangent of cone's half angle- Returns:
- fraction of line which intersects the frustum
-
pow
public static long pow(int a, int b) -
factorial
public static int factorial(int n) -
binomial
public static int binomial(int n, int k) -
min
-
max
-
min
-
max
-
min
-
max
-
min
-
max
-
dot
Computes the scalar product(p - q) v
- Parameters:
p
- a pointq
- point which is subtracted fromp
v
- a vector- Returns:
- scalar product
(p - q) v
-
estimateScaleSquared
-
fresnel
public static float fresnel(Vector3f normal, Vector3f in, float iorRatio, Vector3f reflectedOut, Vector3f transmittedOut) Computes reflected and transmitted directions according to Fresnel's formulas.- Parameters:
normal
- the normal unit vector of the surfacein
- the negated direction unit vector of the incoming rayiorRatio
- the index of refraction of the surface side where the normal vector points into, divided by the index of refraction of the opposite sidereflectedOut
- the computed unit vector of the reflection directiontransmittedOut
- the computed unit vector of the transmission direction- Returns:
- the reflection coefficient
-
dot
-
dot
-
dot
-
mul
-
mul
-
lessThan
-
lessThanOrEqual
-
roundUpNextPowerOfTwo
public static int roundUpNextPowerOfTwo(int v) Calculate the next power of two greater or equal than v. (see "Bit Twiddling Hacks")- Parameters:
v
-- Returns:
-
toFloatArray
-
toFloatArray
-
toFloatArray
-
normalize
Normalizes this vector in place. If the length is 0, then the complett vector will be (0, 0, 0).- Parameters:
aVector
- The normalized vector
-
normalize
Normalizes this vector in place. If the length is 0, then the complett vector will be (0, 0, 0).- Parameters:
aVector
- The normalized vector
-
abstpp
Computated the distance between two vectors- Parameters:
av
- first vectorev
- second vector- Returns:
- distance between the two vectors
-
isNullVector
Test of all parts of the vector are null- Parameters:
a
- the vector to test- Returns:
- true, if all parts of the vector are null
-
abstps
Calculates the distance between the point pp and the piece of straight line delimited by sa and se. If sa == se, the distance to this point is calculated.- Parameters:
pp
- Pointsa
- Begin of the straight linese
- End of the straight line- Returns:
- Distance between the point pp and the piece of straight line
-
cutCone
public static void cutCone(Vector3d ss, float alpha, Vector3d pp, Vector3d qq, CutConeParameter ccp) -
cutRay2
public static void cutRay2(float a1, float a2, float r1, float r2, float u1, float u2, float v1, float v2, CutRay2Parameter crp) -
skySegment
-
absthgs
-
determinate3
-
parallelVector
-
abstphg
-
onbco
-
getBeginAndEndOfShoot
-
getBeginOfShoot
-
getEndOfShoot
-
clamp
public static int clamp(int value, int min, int max) -
clamp
public static long clamp(long value, long min, long max) -
clamp
public static float clamp(float value, float min, float max) -
clamp
public static double clamp(double value, double min, double max)
-