Java源码示例:javax.vecmath.Matrix3d
示例1
/**
* Convert a matrix to Euler rotations. There are many valid solutions.
* See also https://www.learnopencv.com/rotation-matrix-to-euler-angles/
* @param mat the Matrix3d to convert.
* @return a Vector3d resulting radian rotations. One possible solution.
*/
public static Vector3d matrixToEuler(Matrix3d mat) {
assert(isRotationMatrix(mat));
double sy = Math.sqrt(mat.m00*mat.m00 + mat.m10*mat.m10);
boolean singular = sy < 1e-6;
double x,y,z;
if(!singular) {
x = Math.atan2( mat.m21,mat.m22);
y = Math.atan2(-mat.m20,sy);
z = Math.atan2( mat.m10,mat.m00);
} else {
x = Math.atan2(-mat.m12, mat.m11);
y = Math.atan2(-mat.m20, sy);
z = 0;
}
return new Vector3d(x,y,z);
}
示例2
/**
* Convert Euler rotations to a matrix.
* See also https://www.learnopencv.com/rotation-matrix-to-euler-angles/
* @param v radian rotation values
* @return Matrix3d resulting matrix
*/
public static Matrix3d eulerToMatrix(Vector3d v) {
double c0 = Math.cos(v.x); double s0 = Math.sin(v.x);
double c1 = Math.cos(v.y); double s1 = Math.sin(v.y);
double c2 = Math.cos(v.z); double s2 = Math.sin(v.z);
Matrix3d rX=new Matrix3d( 1, 0, 0,
0,c0,-s0,
0,s0, c0);
Matrix3d rY=new Matrix3d(c1, 0,s1,
0, 1, 0,
-s1, 0,c1);
Matrix3d rZ=new Matrix3d(c2,-s2, 0,
s2, c2, 0,
0, 0, 1);
Matrix3d result = new Matrix3d();
Matrix3d interim = new Matrix3d();
interim.mul(rY,rX);
result.mul(rZ,interim);
return result;
}
示例3
/**
* Rotate all the vertexes by a given amount
* @param arg0 amount in degrees to rotate around X,Y, and then Z.
*/
public void adjustRotation(Vector3d arg0) {
// generate the pose matrix
Matrix3d pose = new Matrix3d();
Matrix3d rotX = new Matrix3d();
Matrix3d rotY = new Matrix3d();
Matrix3d rotZ = new Matrix3d();
rotX.rotX((float)Math.toRadians(arg0.x));
rotY.rotY((float)Math.toRadians(arg0.y));
rotZ.rotZ((float)Math.toRadians(arg0.z));
pose.set(rotX);
pose.mul(rotY);
pose.mul(rotZ);
adjust.set(pose);
isDirty=true;
}
示例4
public DHTool_GoProCamera() {
super();
setName("GoPro Camera");
flags = LinkAdjust.R;
refreshPoseMatrix();
setModelFilename("/Sixi2/gopro/gopro.stl");
setModelScale(0.1f);
setModelOrigin(0, 0, 0.5);
setModelRotation(90, 90, 0);
// adjust the model's position and rotation.
this.setPosition(new Vector3d(50,0,50));
Matrix3d m = new Matrix3d();
m.setIdentity();
m.rotX(Math.toRadians(90));
Matrix3d m2 = new Matrix3d();
m2.setIdentity();
m2.rotZ(Math.toRadians(90));
m.mul(m2);
this.setRotation(m);
}
示例5
protected Matrix3d buildPanTiltMatrix(double panDeg,double tiltDeg) {
Matrix3d a = new Matrix3d();
Matrix3d b = new Matrix3d();
Matrix3d c = new Matrix3d();
a.rotZ(Math.toRadians(panDeg));
b.rotX(Math.toRadians(-tiltDeg));
c.mul(b,a);
right.x=c.m00;
right.y=c.m01;
right.z=c.m02;
up.x=c.m10;
up.y=c.m11;
up.z=c.m12;
forward.x=c.m20;
forward.y=c.m21;
forward.z=c.m22;
c.transpose();
return c;
}
示例6
@Test
public void testEulerMatrix() {
Vector3d v1 = new Vector3d();
for(int i=0;i<1000;++i) {
v1.x = Math.random() * Math.PI*2.0;
v1.y = Math.random() * Math.PI*2.0;
v1.z = Math.random() * Math.PI*2.0;
Matrix3d a = MatrixHelper.eulerToMatrix(v1);
Vector3d v2 = MatrixHelper.matrixToEuler(a);
Matrix3d b = MatrixHelper.eulerToMatrix(v2);
boolean test = b.epsilonEquals(a, 1e-6);
assert(test);
if(test==false) {
System.out.println(i+"a="+a);
System.out.println(i+"b="+b);
b.sub(a);
System.out.println(i+"d="+b);
}
org.junit.Assert.assertTrue(test);
}
System.out.println("testEulerMatrix() OK");
}
示例7
/**
* Produce a Detector properties object populated with sensible default values given image shape.
* It produces a detector normal to the beam and centred on the beam with square pixels of 0.1024mm and set 200mm
* from the sample.
*
* @param shape image shape
*/
public static DetectorProperties getDefaultDetectorProperties(int... shape) {
int heightInPixels = shape[0];
int widthInPixels = shape[1];
// Set a few default values
double pixelSizeX = 0.1024;
double pixelSizeY = 0.1024;
double distance = 200.00;
// Create identity orientation
Matrix3d identityMatrix = new Matrix3d();
identityMatrix.setIdentity();
// Create the detector origin vector based on the above
Vector3d dOrigin = new Vector3d((widthInPixels - widthInPixels/2d) * pixelSizeX, (heightInPixels - heightInPixels/2d) * pixelSizeY, distance);
return new DetectorProperties(dOrigin, heightInPixels, widthInPixels, pixelSizeX, pixelSizeY, identityMatrix);
}
示例8
/**
* Reset entries that are less than or equal to 1 unit of least precision of
* the matrix's scale
* @param m
*/
private static void santise(Matrix3d m) {
double scale = m.getScale();
double min = Math.ulp(scale);
for (int i = 0; i < 3; i++) {
double t;
t = Math.abs(m.getElement(i, 0));
if (t > 0 && t <= min) {
m.setElement(i, 0, 0);
}
t = Math.abs(m.getElement(i, 1));
if (t > 0 && t <= min) {
m.setElement(i, 1, 0);
}
t = Math.abs(m.getElement(i, 2));
if (t > 0 && t <= min) {
m.setElement(i, 2, 0);
}
}
}
示例9
@Override
public Matrix3d getViewMatrix(int index) {
Matrix3d m = new Matrix3d();
switch (index) {
case 0:
m.setIdentity(); // front vertex-centered
break;
case 1:
m.rotX(-0.6523581397843639); // back face-centered -0.5535743588970415 m.rotX(Math.toRadians(-26));
break;
case 2:
m.rotZ(Math.PI/2);
Matrix3d m1 = new Matrix3d();
m1.rotX(-1.0172219678978445);
m.mul(m1);
break;
default:
throw new IllegalArgumentException("getViewMatrix: index out of range:" + index);
}
return m;
}
示例10
/**
* Returns the vertices of an n-fold polygon of given radius and center
* @param n
* @param radius
* @param center
* @return
*/
@Override
public Point3d[] getVertices() {
double x = getSideLengthFromCircumscribedRadius(circumscribedRadius)/2;
double z = x/Math.sqrt(2);
Point3d[] tetrahedron = new Point3d[4];
tetrahedron[0] = new Point3d(-x, 0, -z);
tetrahedron[1] = new Point3d( x, 0, -z);
tetrahedron[2] = new Point3d( 0, -x, z);
tetrahedron[3] = new Point3d( 0, x, z);
Point3d centroid = CalcPoint.centroid(tetrahedron);
// rotate tetrahedron to align one vertex with the +z axis
Matrix3d m = new Matrix3d();
m.rotX(0.5 * TETRAHEDRAL_ANGLE);
for (Point3d p: tetrahedron) {
p.sub(centroid);
m.transform(p);
}
return tetrahedron;
}
示例11
/**
* Returns the vertices of an n-fold polygon of given radius and center
* @return
*/
@Override
public Point3d[] getVertices() {
Point3d[] polygon = new Point3d[2*n];
Matrix3d m = new Matrix3d();
Point3d center = new Point3d(0, 0, height/2);
for (int i = 0; i < n; i++) {
polygon[i] = new Point3d(0, circumscribedRadius, 0);
m.rotZ(i*2*Math.PI/n);
m.transform(polygon[i]);
polygon[n+i] = new Point3d(polygon[i]);
polygon[i].sub(center);
polygon[n+i].add(center);
}
return polygon;
}
示例12
@Override
public Matrix3d getViewMatrix(int index) {
Matrix3d m = new Matrix3d();
switch (index) {
case 0:
m.setIdentity(); // front
break;
case 1:
m.rotX(Math.PI/2); // side edge-centered
break;
case 2:
m.rotY(Math.PI/n); // side face-centered
Matrix3d m1 = new Matrix3d();
m1.rotX(Math.PI/2);
m.mul(m1);
break;
case 3:
m.set(flipX()); // back
break;
default:
throw new IllegalArgumentException("getViewMatrix: index out of range:" + index);
}
return m;
}
示例13
@Override
public Matrix3d getViewMatrix(int index) {
Matrix3d m = new Matrix3d();
switch (index) {
case 0:
m.setIdentity(); // C4 vertex-centered
break;
case 1:
m.rotX(-0.5 * TETRAHEDRAL_ANGLE); // C3 face-centered 2.0*Math.PI/3
Matrix3d m1 = new Matrix3d();
m1.rotZ(Math.PI/4);
m.mul(m1);
break;
case 2:
m.rotY(Math.PI/4); // side face-centered
break;
default:
throw new IllegalArgumentException("getViewMatrix: index out of range:" + index);
}
return m;
}
示例14
@Override
public Matrix3d getViewMatrix(int index) {
Matrix3d m = new Matrix3d();
switch (index) {
case 0: m.setIdentity(); // front
break;
case 1: m.rotY(Math.PI/2); // left
break;
case 2: m.rotY(Math.PI); // back
break;
case 3: m.rotY(-Math.PI/2); // right
break;
case 4: m.rotX(Math.PI/2); // top
break;
case 5: m.rotX(-Math.PI/2); // bottom
break;
default: throw new IllegalArgumentException("getViewMatrix: index out of range:" + index);
}
return m;
}
示例15
public static Matrix4d getMatrixFromAlgebraic(String transfAlgebraic) {
String[] parts = transfAlgebraic.toUpperCase().split(",");
double[] xCoef = convertAlgebraicStrToCoefficients(parts[0].trim());
double[] yCoef = convertAlgebraicStrToCoefficients(parts[1].trim());
double[] zCoef = convertAlgebraicStrToCoefficients(parts[2].trim());
Matrix4d mat = new Matrix4d();
mat.setIdentity();
mat.setRotation(new Matrix3d(xCoef[0],xCoef[1],xCoef[2],yCoef[0],yCoef[1],yCoef[2],zCoef[0],zCoef[1],zCoef[2]));
mat.setTranslation(new Vector3d(xCoef[3],yCoef[3],zCoef[3]));
return mat;
//return new Matrix4d(xCoef[0],xCoef[1],xCoef[2],xCoef[3],
// yCoef[0],yCoef[1],yCoef[2],yCoef[3],
// zCoef[0],zCoef[1],zCoef[2],zCoef[3],
// 0,0,0,1);
}
示例16
/**
* Generic quadcopter constructor.
*
* @param world world where to place the vehicle
* @param modelName filename of model to load, in .obj format
* @param orientation "x" or "+"
* @param armLength length of arm from center
* @param rotorThrust full thrust of one rotor
* @param rotorTorque torque at full thrust of one rotor
* @param rotorTimeConst spin-up time of rotor
* @param rotorsOffset rotors positions offset from gravity center
* @throws FileNotFoundException if .obj model file not found
*/
public Quadcopter(World world, String modelName, String orientation, double armLength, double rotorThrust,
double rotorTorque, double rotorTimeConst, Vector3d rotorsOffset) throws FileNotFoundException {
super(world, modelName);
rotorPositions[0] = new Vector3d(0.0, armLength, 0.0);
rotorPositions[1] = new Vector3d(0.0, -armLength, 0.0);
rotorPositions[2] = new Vector3d(armLength, 0.0, 0.0);
rotorPositions[3] = new Vector3d(-armLength, 0.0, 0.0);
if (orientation.equals("x") || orientation.equals("X")) {
Matrix3d r = new Matrix3d();
r.rotZ(-Math.PI / 4);
for (int i = 0; i < rotorsNum; i++) {
r.transform(rotorPositions[i]);
}
} else if (orientation.equals("+")) {
} else {
throw new RuntimeException("Unknown orientation: " + orientation);
}
for (int i = 0; i < rotors.length; i++) {
rotorPositions[i].add(rotorsOffset);
Rotor rotor = rotors[i];
rotor.setFullThrust(rotorThrust);
rotor.setFullTorque(i < 2 ? -rotorTorque : rotorTorque);
rotor.setTimeConstant(rotorTimeConst);
}
}
示例17
@Override
public void update(long t) {
this.position = baseObject.position;
this.velocity = baseObject.velocity;
this.acceleration = baseObject.acceleration;
double yaw = Math.atan2(baseObject.rotation.getElement(1, 0), baseObject.rotation.getElement(0, 0));
this.rotation.rotZ(yaw);
if (pitchChannel >= 0 && baseObject instanceof AbstractVehicle) {
// Control camera pitch
List<Double> control = ((AbstractVehicle) baseObject).getControl();
if (control.size() > pitchChannel) {
Matrix3d r = new Matrix3d();
r.rotY(control.get(4) * pitchScale);
this.rotation.mul(r);
}
}
}
示例18
/**
* Creates a standard 3x3 rotation matrix for this transform and the given transform type.
*
* @param transformType
* @return
*/
public Matrix3d createRotationMatrix(TransformType transformType) {
double[] internalRotationMatrix = getInternalMatrix(transformType);
return new Matrix3d(internalRotationMatrix[0], internalRotationMatrix[1],
internalRotationMatrix[2], internalRotationMatrix[4], internalRotationMatrix[5],
internalRotationMatrix[6], internalRotationMatrix[8], internalRotationMatrix[9],
internalRotationMatrix[10]);
}
示例19
/**
* Generates the rotated moment of inertia tensor with the body; uses the following formula: I'
* = R * I * R-transpose; where I' is the rotated inertia, I is un-rotated interim, and R is the
* rotation matrix.
*/
private void calculateFramedMOITensor() {
double[] framedMOI = RotationMatrices.getZeroMatrix(3);
// Copy the rotation matrix, ignore the translation and scaling parts.
Matrix3d rotationMatrix = getParent().getShipTransformationManager()
.getCurrentPhysicsTransform().createRotationMatrix(TransformType.SUBSPACE_TO_GLOBAL);
Matrix3d inertiaBodyFrame = new Matrix3d(gameMoITensor);
// The product of the overall rotation matrix with the inertia tensor.
inertiaBodyFrame.mul(rotationMatrix);
rotationMatrix.transpose();
// The product of the inertia tensor multiplied with the transpose of the
// rotation transpose.
inertiaBodyFrame.mul(rotationMatrix);
framedMOI[0] = inertiaBodyFrame.m00;
framedMOI[1] = inertiaBodyFrame.m01;
framedMOI[2] = inertiaBodyFrame.m02;
framedMOI[3] = inertiaBodyFrame.m10;
framedMOI[4] = inertiaBodyFrame.m11;
framedMOI[5] = inertiaBodyFrame.m12;
framedMOI[6] = inertiaBodyFrame.m20;
framedMOI[7] = inertiaBodyFrame.m21;
framedMOI[8] = inertiaBodyFrame.m22;
physMOITensor = framedMOI;
setPhysInvMOITensor(RotationMatrices.inverse3by3(framedMOI));
}
示例20
/**
* Calculate the angles and call them back to the app
* @param inFunction function to call for export
*/
private void callbackRender(Export3dFunction inFunction)
{
Transform3D trans3d = new Transform3D();
_orbit.getViewingPlatform().getViewPlatformTransform().getTransform(trans3d);
Matrix3d matrix = new Matrix3d();
trans3d.get(matrix);
Point3d point = new Point3d(0.0, 0.0, 1.0);
matrix.transform(point);
// Set up initial rotations
Transform3D firstTran = new Transform3D();
firstTran.rotY(Math.toRadians(-INITIAL_Y_ROTATION));
Transform3D secondTran = new Transform3D();
secondTran.rotX(Math.toRadians(-INITIAL_X_ROTATION));
// Apply inverse rotations in reverse order to the test point
Point3d result = new Point3d();
secondTran.transform(point, result);
firstTran.transform(result);
// Give the settings to the rendering function
inFunction.setCameraCoordinates(result.x, result.y, result.z);
inFunction.setAltitudeExaggeration(_altFactor);
inFunction.setTerrainDefinition(_terrainDefinition);
inFunction.setImageDefinition(_imageDefinition);
inFunction.begin();
}
示例21
/**
* Confirms that this matrix is a rotation matrix. Matrix A * transpose(A) should be the Identity.
* See also https://www.learnopencv.com/rotation-matrix-to-euler-angles/
* @param mat
* @return
*/
public static boolean isRotationMatrix(Matrix3d mat) {
Matrix3d m1 = new Matrix3d(mat);
Matrix3d m2 = new Matrix3d();
m2.transpose(m1);
m1.mul(m2);
m2.setIdentity();
return m1.epsilonEquals(m2, 1e-6);
}
示例22
/**
* normalize the 3x3 component of the mTarget matrix. Do not affect position.
* @param mTarget the matrix that will be normalized.
*/
public static void normalize3(Matrix4d mTarget) {
Matrix3d m3 = new Matrix3d();
Vector3d v3 = new Vector3d();
mTarget.get(v3);
mTarget.get(m3);
m3.normalize();
mTarget.set(m3);
mTarget.setTranslation(v3);
}
示例23
public void setRotation(Matrix3d m) {
setChanged();
t.setRotation(m);
notifyObservers();
Vector3d r = MatrixHelper.matrixToEuler(t);
rot.set(Math.toDegrees(r.x),
Math.toDegrees(r.y),
Math.toDegrees(r.z));
}
示例24
@Override
public void update(Observable o, Object arg) {
Matrix4d m4 = new Matrix4d();
Vector3d rDeg = rot.get();
Vector3d rRad = new Vector3d(
Math.toRadians(rDeg.x),
Math.toRadians(rDeg.y),
Math.toRadians(rDeg.z));
Matrix3d m3 = MatrixHelper.eulerToMatrix(rRad);
m4.set(m3);
m4.setTranslation(pos.get());
this.set(m4);
super.update(o, arg);
}
示例25
/**
*
* @param arg0 Vector3d of radian rotation values
*/
public void setRotation(Vector3d arg0) {
Matrix4d m4 = new Matrix4d();
Matrix3d m3 = MatrixHelper.eulerToMatrix(arg0);
m4.set(m3);
m4.setTranslation(getPosition());
setPose(m4);
}
示例26
/**
*
* @param original
*/
public void restore(DetectorProperties original) {
fire = false;
setOrigin(new Vector3d(original.getOrigin()));
setBeamVector(new Vector3d(original.getBeamVector()));
setPx(original.getPx());
setPy(original.getPy());
setStartX(original.getStartX());
setStartY(original.getStartY());
setVPxSize(original.getVPxSize());
setHPxSize(original.getHPxSize());
fire = true;
setOrientation(new Matrix3d(original.getOrientation()));
}
示例27
/**
* Rotates this coordinate about the input vector through the input angle (radians - because we
* usually use this internally)
*
* @param rotAxis The axis of rotation
* @param angle The angle of rotation (in radians)
*/
public Vector3d rotate(final Vector3d rotAxis, final double angle) {
final Vector3d thisVec = new Vector3d(ecfVector);
final Vector3d axis = new Vector3d(rotAxis);
axis.normalize();
final Matrix3d trans = new Matrix3d();
trans.set(new AxisAngle4d(axis, angle));
trans.transform(thisVec);
return thisVec;
}
示例28
private Matrix3d getRotationMatrix() {
getRmsd();
if (!transformationCalculated) {
calcRotationMatrix();
transformationCalculated = true;
}
return rotmat;
}
示例29
@Override
public Point3d[] getVertices() {
Point3d[] icosahedron = new Point3d[12];
// see http://answers.yahoo.com/question/index?qid=20080108041441AAJCjEu
double c = circumscribedRadius * 1 / Math.sqrt(5);
double s = 2 * c; // golden ratio
double c1 = Math.sqrt((3-Math.sqrt(5))/8); // cos(2Pi/5)
double s1 = Math.sqrt((5+Math.sqrt(5))/8); // sin(2Pi/5)
double c2 = Math.sqrt((3+Math.sqrt(5))/8); // cos(Pi/5)
double s2 = Math.sqrt((5-Math.sqrt(5))/8); // sin(Pi/5)
icosahedron[0] = new Point3d(0, 0, circumscribedRadius);
icosahedron[1] = new Point3d(s, 0, c);
icosahedron[2] = new Point3d(s*c1, s*s1, c);
icosahedron[3] = new Point3d(-s*c2, s*s2, c);
icosahedron[4] = new Point3d(-s*c2, -s*s2, c);
icosahedron[5] = new Point3d(s*c1, -s*s1, c);
for (int i = 0; i < 6; i++) {
icosahedron[i+6] = new Point3d(icosahedron[i]);
icosahedron[i+6].negate();
}
Matrix3d m = new Matrix3d();
m.rotZ(Math.PI/10);
for (Point3d p: icosahedron) {
m.transform(p);
}
return icosahedron;
}
示例30
@Override
public Matrix3d getViewMatrix(int index) {
Matrix3d m = new Matrix3d();
switch (index) {
case 0: m.setIdentity(); // front vertex-centered
break;
case 1: m.rotX(Math.PI); // back face-centered
break;
case 2: double angle = Math.PI - 0.5 * TETRAHEDRAL_ANGLE; // Side edge-centered
m.rotX(angle);
break;
default: throw new IllegalArgumentException("getViewMatrix: index out of range:" + index);
}
return m;
}