$darkmode
DENOPTIM
CartesianSpaceUtils.java
Go to the documentation of this file.
1package denoptim.utils;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import javax.vecmath.AxisAngle4d;
7import javax.vecmath.Matrix3d;
8import javax.vecmath.Point3d;
9import javax.vecmath.Vector3d;
10
16{
17
18//------------------------------------------------------------------------------
19
25 public static void translateOrigin(Vector3d v, Point3d newOrigin)
26 {
27 v.x = v.x + newOrigin.x;
28 v.y = v.y + newOrigin.y;
29 v.z = v.z + newOrigin.z;
30 }
31
32//------------------------------------------------------------------------------
33
41 public static Vector3d getVectorFromTo(Point3d a, Point3d b)
42 {
43 double x = b.x - a.x;
44 double y = b.y - a.y;
45 double z = b.z - a.z;
46
47 return new Vector3d(x, y, z);
48 }
49
50//------------------------------------------------------------------------------
51
58 public static Vector3d getSumOfVector(Vector3d A, Vector3d B)
59 {
60 return new Vector3d((A.x + B.x), (A.y + B.y), (A.z + B.z));
61 }
62
63//------------------------------------------------------------------------------
64
71 public static Vector3d getDiffOfVector(Vector3d A, Vector3d B)
72 {
73 return new Vector3d((A.x - B.x), (A.y - B.y), (A.z - B.z));
74 }
75
76//------------------------------------------------------------------------------
77
84 public static Vector3d getNormalDirection(Vector3d dir)
85 {
86 Vector3d normalDir = new Vector3d();
87
88 Vector3d dirX = new Vector3d(1.0, 0.0, 0.0);
89 Vector3d dirY = new Vector3d(0.0, 1.0, 0.0);
90 Vector3d dirZ = new Vector3d(0.0, 0.0, 1.0);
91 List<Vector3d> candidates = new ArrayList<Vector3d>();
92 candidates.add(dirX);
93 candidates.add(dirY);
94 candidates.add(dirZ);
95
96 // Check for the lucky case... one of the candidates IS the solution
97 List<Double> dotProds = new ArrayList<Double>();
98 boolean found = false;
99 double max = 0.0;
100 for (int i=0; i<candidates.size(); i++)
101 {
102 double res = dir.dot(candidates.get(i));
103 double absRes = Math.abs(res);
104 if (absRes > max)
105 max = absRes;
106
107 if (res == 0.0)
108 {
109 normalDir = candidates.get(i);
110 found = true;
111 break;
112 } else {
113 dotProds.add(absRes);
114 }
115 }
116
117 // So, since you are not that lucky use the cross-product to get a
118 // normal direction using the most divergent of the previous candidates
119 if (!found)
120 {
121 int mostDivergent = dotProds.indexOf(max);
122 normalDir.cross(dir,candidates.get(mostDivergent));
123 normalDir.normalize();
124 }
125
126 return normalDir;
127 }
128
129//------------------------------------------------------------------------------
130
137 public static void rotatedVectorWAxisAngle(Vector3d v, Vector3d axis,
138 double ang)
139 {
140 axis.normalize();
141 double rad = Math.toRadians(ang);
142 AxisAngle4d aa = new AxisAngle4d(axis.x,axis.y,axis.z,rad);
143 Matrix3d rotMatrix = new Matrix3d();
144 rotMatrix.set(aa);
145 rotMatrix.transform(v);
146 }
147
148//------------------------------------------------------------------------------
149
150}
151
Utilities for working in the Cartesian space.
static Vector3d getSumOfVector(Vector3d A, Vector3d B)
Get sum of vector A and B.
static Vector3d getDiffOfVector(Vector3d A, Vector3d B)
Calculates the vector difference of vectors A and B.
static void translateOrigin(Vector3d v, Point3d newOrigin)
Changes the origin of a vector.
static Vector3d getNormalDirection(Vector3d dir)
Generate a vector that is perpendicular to the given one.
static Vector3d getVectorFromTo(Point3d a, Point3d b)
Creates an object Vector3d that originates from point a and goes to point b.
static void rotatedVectorWAxisAngle(Vector3d v, Vector3d axis, double ang)
Rotate a vector according to a given rotation axis and angle.