?? translategizmo.java
字號(hào):
//===========================================================================//=-------------------------------------------------------------------------=//= Module history: =//= - March 16 2006 - Oscar Chavarro: Original base version =//===========================================================================package vsdk.toolkit.gui;// Java basic classesimport java.util.ArrayList;// Awt classesimport java.awt.event.MouseEvent;import java.awt.event.MouseWheelEvent;import java.awt.event.KeyEvent;//import java.awt.Robot;import java.awt.Point;// VSDK classesimport vsdk.toolkit.common.VSDK;import vsdk.toolkit.common.Matrix4x4;import vsdk.toolkit.common.Vector3D;import vsdk.toolkit.common.ColorRgb;import vsdk.toolkit.common.Ray;import vsdk.toolkit.environment.Camera;import vsdk.toolkit.environment.Material;import vsdk.toolkit.environment.geometry.Arrow;import vsdk.toolkit.environment.geometry.Box;import vsdk.toolkit.environment.geometry.Cone;import vsdk.toolkit.environment.geometry.Geometry;import vsdk.toolkit.environment.geometry.InfinitePlane;import vsdk.toolkit.environment.geometry.ParametricCurve;import vsdk.toolkit.environment.scene.SimpleBody;import vsdk.toolkit.processing.GeometricModeler;public class TranslateGizmo extends Gizmo { /// Internal transformation state private Matrix4x4 T; private Camera camera; /// Geometric model based in primitive instancing: primitive concretions private Arrow arrowModel; private Cone cylinderModel; private Box boxModel; private Cone coneModel; /// Geometric model based in primitive instancing: primitive instances /// This list is always of size 12, and its elements follow the order /// indicated in the values of the *_ELEMENT constants of this class. ArrayList<SimpleBody> elementInstances; ArrayList<SimpleBody> elementInstances3dsmax; /// Internal element selection state public static final int X_AXIS_ELEMENT = 1; public static final int Y_AXIS_ELEMENT = 2; public static final int Z_AXIS_ELEMENT = 3; public static final int XYY_SEGMENT_ELEMENT = 4; public static final int XYX_SEGMENT_ELEMENT = 5; public static final int YZZ_SEGMENT_ELEMENT = 6; public static final int YZY_SEGMENT_ELEMENT = 7; public static final int XZZ_SEGMENT_ELEMENT = 8; public static final int XZX_SEGMENT_ELEMENT = 9; public static final int XY_BOX_ELEMENT = 10; public static final int YZ_BOX_ELEMENT = 11; public static final int XZ_BOX_ELEMENT = 12; public static final int NULL_GROUP = 0; public static final int X_AXIS_GROUP = 1; public static final int Y_AXIS_GROUP = 2; public static final int Z_AXIS_GROUP = 3; public static final int XY_PLANE_GROUP = 4; public static final int YZ_PLANE_GROUP = 5; public static final int XZ_PLANE_GROUP = 6; private static final double SEGMENT_LENGHT = 0.32; private static final double SEGMENT_WIDTH = 0.02; private static final double BOX_SIDE = 0.3; private static final double BOX_HEIGHT = 0.01; private static final double ARROW_LENGHT = 1.0; private int aparentSizeInPixels; /// Interaction state private int persistentSelection; private int volatileSelection; //private Robot awtRobot; //private boolean skipRobot; private int oldmousex; private int oldmousey; private Vector3D lastDeltaPosition; private boolean selectedResizing; private double currentScale; private boolean active; public TranslateGizmo(Camera cam) { aparentSizeInPixels = 100; persistentSelection = X_AXIS_GROUP; volatileSelection = NULL_GROUP; // Total arrow lenght = 0.2 empty + 0.5 base + 0.3 head arrowModel = new Arrow(0.5*ARROW_LENGHT, 0.3*ARROW_LENGHT, 0.025, 0.05); cylinderModel = new Cone(SEGMENT_WIDTH, SEGMENT_WIDTH, SEGMENT_LENGHT); boxModel = new Box(BOX_SIDE, BOX_SIDE, BOX_HEIGHT); coneModel = new Cone(0.05, 0, 0.3*ARROW_LENGHT); elementInstances = new ArrayList<SimpleBody>(); for ( int i = 0; i < 12; i++ ) { SimpleBody r = new SimpleBody(); elementInstances.add(r); } elementInstances3dsmax = new ArrayList<SimpleBody>(); for ( int i = 0; i < 15; i++ ) { SimpleBody r = new SimpleBody(); elementInstances3dsmax.add(r); } setCamera(cam); //awtRobot = null; //skipRobot = false; oldmousex = 0; oldmousey = 0; selectedResizing = true; currentScale = 1.0; active = false; lastDeltaPosition = new Vector3D(); } public int getAparentSizeInPixels() { return aparentSizeInPixels; } public void setAparentSizeInPixels(int du) { aparentSizeInPixels = du; } public void setCamera(Camera cam) { camera = cam; } public Camera getCamera() { return camera; } public ArrayList<SimpleBody> getElements() { return elementInstances; } public ArrayList<SimpleBody> getElements3dsmax() { int i; SimpleBody r, o, r2; Geometry g; Material red = createMaterial(0.78, 0, 0); Material green = createMaterial(0, 0.61, 0); Material blue = createMaterial(0, 0, 0.76); Matrix4x4 R = new Matrix4x4(T); R.M[3][0] = 0.0; R.M[3][1] = 0.0; R.M[3][2] = 0.0; R.M[0][3] = 0.0; R.M[1][3] = 0.0; R.M[2][3] = 0.0; R.M[3][3] = 1.0; Matrix4x4 subR = new Matrix4x4(); Matrix4x4 eleR, eleRi; Vector3D subP; Vector3D eleP; coneModel.setBaseRadius(currentScale*0.05); coneModel.setHeight(currentScale*0.3*ARROW_LENGHT); boxModel.setSize(currentScale*(BOX_SIDE+0.025), currentScale*(BOX_SIDE+0.025), currentScale*BOX_HEIGHT); //----------------------------------------------------------------- ParametricCurve lineModel; ParametricCurve segmentModel; lineModel = GeometricModeler.createLine(0, 0, 0, 0, 0, currentScale*0.7); segmentModel = GeometricModeler.createLine(0, 0, 0, 0, 0, currentScale*SEGMENT_LENGHT); //----------------------------------------------------------------- for ( i = 0; i < elementInstances.size(); i++ ) { r = elementInstances3dsmax.get(i); o = elementInstances.get(i); r.setMaterial(o.getMaterial()); r.setRotation(o.getRotation()); r.setRotationInverse(o.getRotationInverse()); g = o.getGeometry(); if ( g != null && g instanceof Arrow ) { r.setGeometry(coneModel); switch ( i ) { case 0: // Rotation subR.axisRotation(Math.toRadians(90.0), 0, 1, 0); eleR = R.multiply(subR); r.setRotation(eleR); eleRi = new Matrix4x4(eleR); eleRi.invert(); r.setRotationInverse(eleRi); // Translation subP = new Vector3D(0, 0, currentScale*0.7*ARROW_LENGHT); eleP = eleR.multiply(subP).add(getPosition()); r.setPosition(eleP); r.setMaterial(red); break; case 1: // Rotation subR.axisRotation(Math.toRadians(90.0), -1, 0, 0); eleR = R.multiply(subR); r.setRotation(eleR); eleRi = new Matrix4x4(eleR); eleRi.invert(); r.setRotationInverse(eleRi); // Translation subP = new Vector3D(0, 0, currentScale*0.7*ARROW_LENGHT); eleP = eleR.multiply(subP).add(getPosition()); r.setPosition(eleP); r.setMaterial(green); break; case 2: // Rotation subR = new Matrix4x4(); eleR = R.multiply(subR); r.setRotation(eleR); eleRi = new Matrix4x4(eleR); eleRi.invert(); r.setRotationInverse(eleRi); // Translation subP = new Vector3D(0, 0, currentScale*0.7*ARROW_LENGHT); eleP = eleR.multiply(subP).add(getPosition()); r.setPosition(eleP); r.setMaterial(blue); break; } r2 = elementInstances3dsmax.get(i+12); r2.setMaterial(o.getMaterial()); r2.setRotation(o.getRotation()); r2.setRotationInverse(o.getRotationInverse()); r2.setPosition(o.getPosition()); r2.setGeometry(lineModel); } else if ( g != null && g instanceof Cone ) { r.setPosition(o.getPosition()); r.setGeometry(segmentModel); } else { r.setPosition(o.getPosition()); r.setGeometry(g); } } return elementInstances3dsmax; } private Material createMaterial(double r, double g, double b) { Material m = new Material(); m.setAmbient(new ColorRgb(0.2, 0.2, 0.2)); m.setDiffuse(new ColorRgb(r, g, b)); m.setSpecular(new ColorRgb(1, 1, 1)); return m; } /** This method updates the data structure contained in the `elementInstances` array starting from the given parameters. - translation is the position of the center of the gizmo - rotation is the rotation matrix containing the orientation of the gizmo - if autosize is false, initialdu, initialdv and camera parameters are not used, and the gizmo doesn't change its current size. If autosize is true, the gizmo size is changed such as from the current camera, the gizmo projection fit a 2D area of initialdu * initialdv pixels. - modelType must be one of the following values: MODEL_FOR_GRAVITY or MODEL_FOR_DISPLAY. Depending on this value the size of current geometric elements could change. */ public void calculateGeometryState(Vector3D translation, Matrix4x4 rotation, boolean autosize, int initialdu, Camera camera) { //----------------------------------------------------------------- int i; Material red = createMaterial(0.78, 0, 0); Material green = createMaterial(0, 0.61, 0); Material blue = createMaterial(0, 0, 0.76); Material yellow = createMaterial(1, 1, 0); Material yellowTransparent = createMaterial(1, 1, 0); yellowTransparent.setOpacity(0.2); int currentSelection; if ( volatileSelection == NULL_GROUP ) { currentSelection = persistentSelection; } else { currentSelection = volatileSelection; } Matrix4x4 R = new Matrix4x4(T); R.M[3][0] = 0.0; R.M[3][1] = 0.0; R.M[3][2] = 0.0; R.M[0][3] = 0.0; R.M[1][3] = 0.0; R.M[2][3] = 0.0; R.M[3][3] = 1.0; Matrix4x4 subR = new Matrix4x4(); Matrix4x4 eleR, eleRi; Vector3D subP; Vector3D eleP; camera.updateVectors(); if ( selectedResizing ) { Vector3D a = new Vector3D(); Vector3D b = new Vector3D(); Vector3D p = getPosition(); Vector3D right = camera.getLeft().multiply(-1); right.normalize(); camera.projectPoint(p, a); camera.projectPoint(p.add(right), b); double factor = VSDK.vectorDistance(a, b); currentScale = ((double)initialdu)/factor; } double scale = currentScale; arrowModel.setBaseLength(scale*0.5*ARROW_LENGHT); arrowModel.setHeadLength(scale*0.3*ARROW_LENGHT); arrowModel.setBaseRadius(scale*0.025); arrowModel.setHeadRadius(scale*0.05); cylinderModel.setBaseRadius(scale*SEGMENT_WIDTH); cylinderModel.setTopRadius(scale*SEGMENT_WIDTH); cylinderModel.setHeight(scale*SEGMENT_LENGHT); boxModel.setSize(scale*BOX_SIDE, scale*BOX_SIDE, scale*BOX_HEIGHT); //----------------------------------------------------------------- Vector3D front = camera.getFront(); Vector3D axisI = new Vector3D(1, 0, 0); Vector3D axisJ = new Vector3D(0, 1, 0); Vector3D axisK = new Vector3D(0, 0, 1); boolean orthogonalCamera; boolean iPar; boolean jPar; boolean kPar; orthogonalCamera = (camera.getProjectionMode() == camera.PROJECTION_MODE_ORTHOGONAL); iPar = (Math.abs(front.dotProduct(axisI)) > 1.0-VSDK.EPSILON); jPar = (Math.abs(front.dotProduct(axisJ)) > 1.0-VSDK.EPSILON); kPar = (Math.abs(front.dotProduct(axisK)) > 1.0-VSDK.EPSILON); int index; for ( i = 0, index = 1; index <= 12 && i < elementInstances.size(); index++, i++ ) { SimpleBody r = elementInstances.get(i); r.setGeometry(null); switch ( index ) { case X_AXIS_ELEMENT: if ( !(orthogonalCamera && iPar) ) { // Basic model r.setGeometry(arrowModel); if ( currentSelection == X_AXIS_GROUP || currentSelection == XY_PLANE_GROUP || currentSelection == XZ_PLANE_GROUP ) { r.setMaterial(yellow); } else { r.setMaterial(red); } // Rotation subR.axisRotation(Math.toRadians(90.0), 0, 1, 0); eleR = R.multiply(subR); r.setRotation(eleR); eleRi = new Matrix4x4(eleR); eleRi.invert(); r.setRotationInverse(eleRi); // Translation subP = new Vector3D(0, 0, scale*0.2*ARROW_LENGHT); eleP = eleR.multiply(subP).add(getPosition());
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -