20package denoptim.utils;
22import java.math.BigDecimal;
23import java.math.RoundingMode;
24import java.util.ArrayList;
25import java.util.Collection;
28import javax.vecmath.Point3d;
29import javax.vecmath.Vector3d;
31import denoptim.constants.DENOPTIMConstants;
56 double angle = Math.acos(abdot/(alen*blen));
57 return Math.toDegrees(
angle);
67 public static void norm(
double[] d,
double[] ret)
86 public static Vector3d
normDist(Point3d p1, Point3d p2)
88 Vector3d v =
new Vector3d(p1.x - p2.x,
102 public static double[]
scale(
double[] d,
double a)
104 double[] scaled =
new double[3];
105 scaled[0] = d[0] * a;
106 scaled[1] = d[1] * a;
107 scaled[2] = d[2] * a;
119 public static void scalar(
double[] d,
double a,
double[] ret)
136 return Math.sqrt (v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
150 return v0[0] * v1[0] + v0[1] * v1[1] + v0[2] * v1[2];
164 double[] crossProduct =
new double[3];
165 crossProduct[0] = v0[1] * v1[2] - v0[2] * v1[1];
166 crossProduct[1] = v0[2] * v1[0] - v0[0] * v1[2];
167 crossProduct[2] = v0[0] * v1[1] - v0[1] * v1[0];
180 public static double[]
subtract (
double[] v0,
double[] v1)
182 double[] res =
new double[3];
183 res[0] = v0[0] - v1[0];
184 res[1] = v0[1] - v1[1];
185 res[2] = v0[2] - v1[2];
198 public static double[]
add (
double[] v0,
double[] v1)
200 double[] res =
new double[3];
201 res[0] = v0[0] + v1[0];
202 res[1] = v0[1] + v1[1];
203 res[2] = v0[2] + v1[2];
219 Point3d p2, Point3d p3)
221 double[] v0 =
new double[] {p0.x, p0.y, p0.z};
222 double[] v1 =
new double[] {p1.x, p1.y, p1.z};
223 double[] v2 =
new double[] {p2.x, p2.y, p2.z};
224 double[] v3 =
new double[] {p3.x, p3.y, p3.z};
242 double[] v2,
double[] v3)
255 return Math.toDegrees(Math.atan2(psin, pcos));
260 public static double log2(
double x)
262 return Math.log(x)/Math.log(2);
268 public static double roundValue(
double val,
int decimalPlaces)
270 BigDecimal bd =
new BigDecimal(val);
271 bd = bd.setScale(decimalPlaces, RoundingMode.UP);
272 return bd.doubleValue();
284 public static double angle(Point3d a, Point3d b, Point3d c)
286 double xba = a.x - b.x;
287 double xbc = c.x - b.x;
288 double yba = a.y - b.y;
289 double ybc = c.y - b.y;
290 double zba = a.z - b.z;
291 double zbc = c.z - b.z;
293 double ba = Math.sqrt(xba*xba + yba*yba + zba*zba);
294 double bc = Math.sqrt(xbc*xbc + ybc*ybc + zbc*zbc);
296 double dot = xba*xbc + yba*ybc + zba*zbc;
307 angle = Math.toDegrees(Math.acos(dot));
324 public static double getAngle(
double[] A,
double[] B,
double[] C)
326 double[] vBA = {A[0]-B[0], A[1]-B[1], A[2]-B[2]};
327 double[] vBC = {C[0]-B[0], C[1]-B[1], C[2]-B[2]};
342 public static double distance(Point3d a, Point3d b)
344 double dx = a.x - b.x, dy = a.y - b.y, dz = a.z - b.z;
345 return Math.sqrt(dx*dx + dy*dy + dz*dz);
350 private static final double[]
tp1 =
new double[3];
351 private static final double[]
tp2 =
new double[3];
352 private static final double[]
tp3 =
new double[3];
353 private static final double[]
tp4 =
new double[3];
363 public static double torsion(Point3d p1, Point3d p2, Point3d p3, Point3d p4)
380 public static double sum(
double[] a)
396 public static double mean(
double[] a)
401 return sum / a.length;
406 public static double mean(List<Double> vals)
408 double[] arr =
new double[vals.size()];
409 for (
int i=0; i<vals.size(); i++)
411 arr[i] = vals.get(i);
428 Vector3d normalDir =
new Vector3d();
429 ArrayList<Vector3d> candidates =
new ArrayList<>();
430 candidates.add(
new Vector3d(1.0, 0.0, 0.0));
431 candidates.add(
new Vector3d(0.0, 1.0, 0.0));
432 candidates.add(
new Vector3d(0.0, 0.0, 1.0));
433 candidates.add(
new Vector3d(1.0/Math.sqrt(2.0),
436 candidates.add(
new Vector3d(1.0/Math.sqrt(2.0),
438 1.0/Math.sqrt(2.0)));
439 candidates.add(
new Vector3d(0.0,
441 1.0/Math.sqrt(2.0)));
443 for (Vector3d candidate : candidates) {
444 double res = vecA.dot(candidate);
445 if (Math.abs(res) > 0.1 && Math.abs(res) < 0.9) {
446 normalDir.cross(vecA, candidate);
450 normalDir.normalize();
457 public static double[]
centroidOf(Collection<
double[]> points,
int dimension)
459 double[] centroid =
new double[dimension];
460 for (
double[] point : points)
462 for (
int i = 0; i < centroid.length; i++)
464 centroid[i] += point[i];
467 for (
int i = 0; i < centroid.length; i++)
469 centroid[i] /= points.size();
General set of constants used in DENOPTIM.
static final double FLOATCOMPARISONTOLERANCE
Smallest difference for comparison of double and float numbers.
Some useful math operations.
static Vector3d normDist(Point3d p1, Point3d p2)
Get normalized distance between two point.
static double sum(double[] a)
Return sum of all values in array.
static double computeDihedralAngle(double[] v0, double[] v1, double[] v2, double[] v3)
Compute the dihedral angle.
static double computeDotProduct(double[] v0, double[] v1)
Compute the dot product (a scalar) between two vectors.
static double angle(Point3d a, Point3d b, Point3d c)
Calculate the angle between the 3 points.
static final double[] tp3
static double log2(double x)
static double length(double[] v)
Return the norm of a vector.
static double mean(List< Double > vals)
static double[] add(double[] v0, double[] v1)
Perform vector addition.
static final double[] tp4
static double roundValue(double val, int decimalPlaces)
static void scalar(double[] d, double a, double[] ret)
Scales a vector.
static double getAngle(double[] A, double[] B, double[] C)
Calculates angle (in degrees) between vectors BA and BC.
static final double[] tp1
static double[] subtract(double[] v0, double[] v1)
Perform vector subtraction.
static final double[] tp2
static double calculateAngle(double[] a, double[] b)
compute the angle between two vectors a & b of same length
static double computeDihedralAngle(Point3d p0, Point3d p1, Point3d p2, Point3d p3)
Compute the dihedral angle.
static double[] scale(double[] d, double a)
Scales a vector.
static void norm(double[] d, double[] ret)
Normalizes a vector.
static double torsion(Point3d p1, Point3d p2, Point3d p3, Point3d p4)
Calculate the torsion angle between the 4 points.
static double distance(Point3d a, Point3d b)
Calculates distance between point a and point b.
static double[] centroidOf(Collection< double[]> points, int dimension)
static double[] computeCrossProduct(double[] v0, double[] v1)
Compute the cross product of two vectors.
static Vector3d getNormalDirection(Vector3d vecA)
Generate a vector that is perpendicular to the given one.
static double mean(double[] a)
Calculate mean value.