* The world system is defined as follows: * *
* w2 o * | * | * | * w0 o-------o w1 *** Each point is defined with x,y,z so this system may in effect be * arbitrary oriented in space, and may include sharing. *
* The device system is defined as follows: * *
* width * x0,y0 o-------o * | * height | * | * o *** The matrix maps w2 to (x0,y0), w0 to the lower left corner of the * device rectangle, and w1 to the lower right corner of the device * rectangle. * * @param w0 x,y,z coordinate of first world position. * @param w1 x,y,z coordinate of second world position. * @param w2 x,y,z coordinate of third world position. * @param x0 X coordinate of upper left corner of device. * @param y0 Y coordinate of upper left corner of device. * @param width Width of device * @param height Height of device. */ public void setWorld2DeviceTransform (double[] w0, double[] w1, double[] w2, int x0, int y0, int width, int height) { setIdentity(); double[] x = new double[4]; double[] y = new double[4]; double[] z = new double[4]; // Make direction vectors for new system x[0] = w2[0]; y[0] = w2[1]; z[0] = w2[2]; x[1] = w1[0] - w0[0]; y[1] = w1[1] - w0[1]; z[1] = w1[2] - w0[2]; x[2] = w0[0] - w2[0]; y[2] = w0[1] - w2[1]; z[2] = w0[2] - w2[2]; x[3] = y[1]*z[2] - z[1]*y[2]; y[3] = z[1]*x[2] - x[1]*z[2]; z[3] = x[1]*y[2] - y[1]*x[2]; // Normalize new z-vector, in case someone needs // new z-value in addition to device coordinates */ double length = Math.sqrt (x[3]*x[3] + y[3]*y[3] + z[3]*z[3]); x[3] /= length; y[3] /= length; z[3] /= length; // Translate back to new origin translate (-x[0], -y[0], -z[0]); // Multiply with inverse of definition of new coordinate system double a = y[2]*z[3] - z[2]*y[3]; double b = z[1]*y[3] - y[1]*z[3]; double c = y[1]*z[2] - z[1]*y[2]; double det = x[1]*a + x[2]*b + x[3]*c; double[] m = new double[16]; m[0] = a / det; m[1] = b / det; m[2] = c / det; m[3] = 0.0; m[4] = (x[3]*z[2] - x[2]*z[3]) / det; m[5] = (x[1]*z[3] - x[3]*z[1]) / det; m[6] = (z[1]*x[2] - x[1]*z[2]) / det; m[7] = 0.0; m[8] = (x[2]*y[3] - x[3]*y[2]) / det; m[9] = (y[1]*x[3] - x[1]*y[3]) / det; m[10] = (x[1]*y[2] - y[1]*x[2]) / det; m[11] = 0.0; m[12] = 0.0; m[13] = 0.0; m[14] = 0.0; m[15] = 1.0; Matrix4x4 matrix = new Matrix4x4 (m); multiply (matrix); // Scale according to height and width of viewport matrix.setIdentity(); matrix.setElement (0, 0, width); matrix.setElement (1, 1, height); multiply (matrix); // Translate according to origin of viewport matrix.setIdentity(); matrix.setElement (3, 0, x0); matrix.setElement (3, 1, y0); multiply (matrix); } /** * Create a string representation of this matrix. * * @return String representing this matrix. */ public String toString() { String string = new String(); for (int i=0; i<4; i++) { for (int j=0; j<4; j++) string += getElement(i,j) + " "; string += '\n'; } return string; } } /* * This code is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This code is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, write to the Free * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, * MA 02111-1307, USA. */ //package no.geosoft.cc.geometry; /** * Implementation of a 4-element vector suited for use with * Matrix4x4 * * @author Jacob Dreyer */ //public class Vector4 { private double[] v_; private void initialize() { v_ = new double[4]; for (int i = 0; i < 4; i++) v_[i] = 0.0; } /** * Create a default 4-element vector (all elements set to 0.0). */ public Vector4() { initialize(); } /** * Create a 4-element vector with the specified values. * * @param v1 1st element. * @param v2 2nd element. * @param v3 3rd element. * @param v4 4th element */ public Vector4 (double v1, double v2, double v3, double v4) { initialize(); set (v1, v2, v3, v4); } /** * Construct a 4-element vector as a copy of the specified vector. * * @param vector4 */ public Vector4 (Vector4 vector4) { initialize(); set (vector4); } /** * Set the elements of this vector. * * @param v1 1st element. * @param v2 2nd element. * @param v3 3rd element. * @param v4 4th element */ public void set (double v1, double v2, double v3, double v4) { v_[0] = v1; v_[1] = v2; v_[2] = v3; v_[3] = v4; } /** * Set the elements of this vector according to the specified vector. * * @param vector Vector to copy. */ public void set (Vector4 vector) { for (int i = 0; i < 4; i++) v_[0] = vector.v_[i]; } /** * Check if this 4-element vector equals the specified object. * * @return TRue if the two equals, false otherwise. */ public boolean equals (Object object) { Vector4 vector = (Vector4) object; return v_[0] == vector.v_[0] && v_[1] == vector.v_[1] && v_[2] == vector.v_[2] && v_[3] == vector.v_[3]; } /** * Return the i'th element of this vector. * * @param i Index of element to get (first is 0). * @return i'th element of this vector. */ public double getElement (int i) { return v_[i]; } /** * Set the i'th element of this vector. * * @param i Index of element to set (first is 0). * @param Value to set. */ public void setElement (int i, double value) { v_[i] = value; } /** * Create a string representation of this vector. * * @return String representing this vector. */ public String toString() { return ("Vector4: [" + v_[0] + "," + v_[1] + "," + v_[2] + "," + v_[3] + "]"); } }