Transformations for 3D View Elements
Transformations are defined in the org.opensourcephysics.numerics.Transformation interface
as objects which transform a point in 3D space to another point. Most transformations
are invertible (such as rigid motions, for instance),
but they don't need to be.
The class org.opensourcephysics.numerics.Matrix3DTransformation provides
utilities to create the most frequently used transformations,
as well as to multiply them to obtain more sophisticated ones. Some of the
factory (static)
methods
of this class are:
- Matrix3DTransformation rotationX(double theta). Creates
a rotation around the X axis with the given angle in radians.
- Matrix3DTransformation rotationY(double theta). Creates
a rotation around the Y axis with the given angle in radians.
- Matrix3DTransformation rotationZ(double theta). Creates
a rotation around the Z axis with the given angle in radians.
- Matrix3DTransformation rotation(double theta, double[] axis). Creates
a rotation around the given axis with the given angle in radians.
- Matrix3DTransformation createAlignmentTransformation(double[] v1, double v2[]). Creates
a rotation that aligns the first vector with the second vector.
- Matrix3DTransformation Quaternion(double q0, double q1, double q2, double q3). Creates
a rotation about the origin by the given quaternion components.
Objects of the Matrix3DTransformation class support the following methods:
- void setOrigin(double ox, double oy, double oz).
Matrix3DTransformation objects consider, by default, their origin to be the (0,0,0) point.
This method sets a new origin for the transformation.
- void multiply(Matrix3DTransformation transf). Multiplies
(concatenates) this transformation with the given one.
To use any of these methods, we recommend that you first import the org.opensourcephysics.numerics.Matrix3DTransformation class
(through the Import Statements of the Custom panel
of Ejs) . Then, you can write code such as:
Matrix3DTransformation firstTransf = Matrix3DTransformation.rotationX(Math.PI/4.0);
Matrix3DTransformation secondTransf = Matrix3DTransformation.rotationY(Math.PI/2.0);
firstTransf.multiply(secondTransf); // i.e. firstTransf followed by secondTransf
_view.myElement3D.setTransformation(firstTransf);
where myElement3D is a 3D element.
A simple static transformation (i.e. one that will not change during the
execution of the simulation) can be specified in the Transform property
of a 3D view element using one of the following arrays of double constants:
- {theta, x,y,z}. Creates a rotation around the (x,y,z) axis
of angle theta. Example: {1.57, 1.0,0.0,0.0} is a rotation
of
PI/2 around the X axis.
- {x1,y1,z1, x2,y2,z2}. Creates a rotation that aligns
the (x1,y1,z1) vector with the vector (x2,y2,z2).
Example: {1.0,0.0,0.0, 0.0,1.0,0.0} aligns the X axis
with the Y axis.