Thread: [Bprocessor-commit] gl/src/net/sourceforge/bprocessor/gl/tool MoveTool.java,1.23,1.24
Status: Pre-Alpha
Brought to you by:
henryml
From: Nordholt <nor...@us...> - 2006-01-13 15:56:44
|
Update of /cvsroot/bprocessor/gl/src/net/sourceforge/bprocessor/gl/tool In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv28428/tool Modified Files: MoveTool.java Log Message: made new free movemode, movemodes can be changed using the M key. (still needs adjustments) Index: MoveTool.java =================================================================== RCS file: /cvsroot/bprocessor/gl/src/net/sourceforge/bprocessor/gl/tool/MoveTool.java,v retrieving revision 1.23 retrieving revision 1.24 diff -C2 -d -r1.23 -r1.24 *** MoveTool.java 11 Jan 2006 13:55:28 -0000 1.23 --- MoveTool.java 13 Jan 2006 15:56:34 -0000 1.24 *************** *** 10,14 **** import net.sourceforge.bprocessor.gl.model.ClippingPlane; - import net.sourceforge.bprocessor.model.Constraints; import net.sourceforge.bprocessor.model.Edge; --- 10,13 ---- *************** *** 19,22 **** --- 18,24 ---- import net.sourceforge.bprocessor.model.Camera; + import net.sourceforge.bprocessor.gl.view.Transformation; + import net.sourceforge.bprocessor.gl.view.View; + import java.awt.Cursor; import java.awt.event.MouseEvent; *************** *** 28,31 **** --- 30,34 ---- import java.util.Set; import java.util.List; + import java.util.LinkedList; import org.apache.log4j.Logger; *************** *** 38,65 **** private static Logger log = Logger.getLogger(MoveTool.class); ! /** XY move mode for 3D view */ ! private static final int XY = 0; ! ! /** XZ move mode for 3D view */ ! private static final int XZ = 1; ! ! /** The the moving mode in 3D view */ ! private int moveMode = XY; /** The initial movepoint */ private Vertex initial; - /** The parent location (total drag) */ - private Vertex parentPos; - /** The location to move from */ private Vertex from; - /** The previous X coordinate */ - private double previousX = 0; - - /** The previous Y cordinate */ - private double previousY = 0; - /** The plane to drag according to */ private Plane dragPlane; --- 41,56 ---- private static Logger log = Logger.getLogger(MoveTool.class); ! /** The axis restricted movement mode */ ! private static final int AXIS_RESTRICTED = 0; + /** The free snap movement mode */ + private static final int FREE_SNAP = 1; + /** The initial movepoint */ private Vertex initial; /** The location to move from */ private Vertex from; /** The plane to drag according to */ private Plane dragPlane; *************** *** 80,83 **** --- 71,77 ---- private String number; + /** The mode of moving */ + private int moveMode; + /** * The Constructor *************** *** 89,92 **** --- 83,87 ---- moveConstructors = null; restrictionVector = null; + moveMode = FREE_SNAP; } *************** *** 143,162 **** protected void pressed(MouseEvent e) { super.pressed(e); ! previousX = e.getX(); ! previousY = e.getY(); ! clearConstructors(moveConstructors); if (target != null) { ! dragPlane = findMovePlane(target); ! initial = glv.getView().toPlaneCoords(new double[] {previousX, previousY}, dragPlane); ! parentPos = new Vertex(initial.getX(), initial.getY(), initial.getZ()); ! from = new Vertex(initial.getX(), initial.getY(), initial.getZ()); ! ! moveConstructors = new HashSet(); ! if (target instanceof Vertex) { ! moveConstructors.addAll(makeEdgeConstructors((Vertex)target)); ! } else { ! moveConstructors.addAll(makeXYZConstructors(initial)); ! } displayConstructors(moveConstructors); --- 138,148 ---- protected void pressed(MouseEvent e) { super.pressed(e); ! clearConstructors(moveConstructors); if (target != null) { ! initial = findInitial((Entity)target, e); ! from = initial.copy(); ! ! setupConstructors(); displayConstructors(moveConstructors); *************** *** 173,214 **** } } ! /** ! * Finds the plan to move the element on ! * @param o The object to find a plane for ! * @return The movement plane */ ! protected Plane findMovePlane(Object o) { ! Plane temp = null; ! if (o instanceof Vertex) { ! temp = new Plane(0, 0, 1, -((Vertex)o).getZ()); ! } else if (o instanceof Edge) { ! temp = new Plane(0, 0, 1, -((Edge)o).getTo().getZ()); ! } else if (o instanceof Surface) { ! Surface sel = (Surface)o; ! temp = sel.plane(); ! } else if (o instanceof ClippingPlane) { ! ClippingPlane clippingplane = (ClippingPlane) o; ! temp = clippingplane.getPlane(); ! } ! return temp; } ! /** ! * Finds an appropriate dragPlane that contains the ! * restrictionvector. */ ! protected void findRestrictionPlane() { ! Camera camera = glv.getView().getCamera(); ! double[] centerPoint = camera.getCenter(); ! double[] cameraPoint = camera.getCamera(); ! Vertex eyeVector = new Vertex(cameraPoint[0] - centerPoint[0], ! cameraPoint[1] - centerPoint[1], ! cameraPoint[2] - centerPoint[2]); ! Vertex planeNormal = (restrictionVector.cross(eyeVector)).cross(restrictionVector); ! dragPlane = new Plane(planeNormal.getX(), planeNormal.getY(), planeNormal.getZ(), ! -planeNormal.dot(initial)); } ! /** * Invoked when a mouse button has been released on a component. --- 159,208 ---- } } ! /** ! * Sets the move constructors appropriate for the ! * movement mode. */ ! private void setupConstructors() { ! if (moveMode == AXIS_RESTRICTED) { ! if (target instanceof Vertex && !((Vertex)target).getEdges().isEmpty()) { ! moveConstructors = makeEdgeConstructors((Vertex)target); ! } else { ! moveConstructors = makeXYZConstructors(initial); ! } ! } else if (moveMode == FREE_SNAP) { ! moveConstructors = makeXYZConerConstructors(initial); ! } } ! /** ! * Finds the initial point clicked for movement. ! * @param ent the entity initialy clicked ! * @param e the mouse event associated with the click. ! * @return the initial point of movement. */ ! protected Vertex findInitial(Entity ent, MouseEvent e) { ! double x = e.getX(); ! double y = View.getHeight() - e.getY(); ! View v = glv.getView(); ! Transformation transformation = v.transformation(); ! Vertex near = new Vertex(x, y, 0.0); ! Vertex far = new Vertex(x, y, 1.0); ! Edge ray = new Edge(near, far); ! ray = transformation.unProject(ray); ! if (ent instanceof Vertex) { ! return ((Vertex)ent).copy(); ! } else if (ent instanceof Surface) { ! Plane surfacePlane = ((Surface)ent).plane(); ! return surfacePlane.intersection(ray); ! } else if (ent instanceof Edge) { ! Edge edge = (Edge) ent; ! Edge intersection = edge.intersection(ray); ! return intersection.getFrom(); ! } ! log.warn("The clicked object is not an Entity"); ! return null; } ! /** * Invoked when a mouse button has been released on a component. *************** *** 235,277 **** return; } ! ! if (restrictionVector == null) { ! boolean found = findRestriction(e); ! if (found) { ! findRestrictionPlane(); ! } ! } if (dragPlane != null && restrictionVector != null) { int x = e.getX(); int y = e.getY(); ! ! parentPos = glv.getView().toPlaneCoords(new double[] {x, y}, dragPlane); Vertex delta = parentPos.minus(from); ! ! //Restricting movement align with restriction vector ! Vertex restrictCopy = restrictionVector.copy(); ! restrictCopy.scale(1 / restrictCopy.length()); ! restrictCopy.scale(delta.dot(restrictCopy)); ! delta = restrictCopy; ! parentPos = from.add(delta); ! if (log.isDebugEnabled()) { log.debug("from " + from.getX() + ", " + from.getY() + ", " + from.getZ()); log.debug("delta " + delta.getX() + ", " + delta.getY() + ", " + delta.getZ()); - } - - //snapping to initial position - Vertex v = initial.minus(parentPos); - Vertex appr = initial.minus(from); - if (Math.abs(v.getX()) < .25) { - delta.setX(appr.getX()); - } - if (Math.abs(v.getY()) < .25) { - delta.setY(appr.getY()); - } - if (Math.abs(v.getZ()) < .25) { - delta.setZ(appr.getZ()); } move(vertices, delta); from.move(delta.getX(), delta.getY(), delta.getZ()); --- 229,247 ---- return; } ! findRestrictions(e); if (dragPlane != null && restrictionVector != null) { int x = e.getX(); int y = e.getY(); ! Vertex parentPos = glv.getView().toPlaneCoords(new double[] {x, y}, dragPlane); Vertex delta = parentPos.minus(from); ! //Restricting movement to fit movement mode. ! delta = restrict(delta); ! if (log.isDebugEnabled()) { log.debug("from " + from.getX() + ", " + from.getY() + ", " + from.getZ()); log.debug("delta " + delta.getX() + ", " + delta.getY() + ", " + delta.getZ()); } + delta = snapToInitial(delta); move(vertices, delta); from.move(delta.getX(), delta.getY(), delta.getZ()); *************** *** 279,289 **** update(); } - - previousX = e.getX(); - previousY = e.getY(); } /** * Finds the current restriction vector. * There must be some number of moveconstructors find a --- 249,370 ---- update(); } } + /** + * Corrects a movement vector to make the + * movement snap to the initial position of + * the moved object(s). + * @param delta the movement vector. + * @return the corrected movement vector. + */ + private Vertex snapToInitial(Vertex delta) { + Vertex parentPos = from.add(delta); + Vertex v = initial.minus(parentPos); + Vertex appr = initial.minus(from); + if (Math.abs(v.getX()) < .25) { + delta.setX(appr.getX()); + } + if (Math.abs(v.getY()) < .25) { + delta.setY(appr.getY()); + } + if (Math.abs(v.getZ()) < .25) { + delta.setZ(appr.getZ()); + } + return delta; + } + + /** + * Restricts the movement to fit the movement mode, + * given a desired movement vector. + * @param delta the movement vector. + * @return the restricted movement vector. + */ + private Vertex restrict(Vertex delta) { + if (moveMode == AXIS_RESTRICTED) { + Vertex restrictCopy = restrictionVector.copy(); + restrictCopy.scale(1 / restrictCopy.length()); + restrictCopy.scale(delta.dot(restrictCopy)); + delta = restrictCopy; + } + return delta; + } /** + * Set up the restriction depending on the + * movement mode. + * @param e a mouse event. + */ + private void findRestrictions(MouseEvent e) { + if (moveMode == AXIS_RESTRICTED) { + if (restrictionVector == null) { + boolean found = findRestrictionVector(e); + if (found) { + findRestrictionPlane(); + } + } + } else if (moveMode == FREE_SNAP) { + if (dragPlane == null) { + Vertex upper = new Vertex(0, 0, 50); + upper = upper.add(initial); + Vertex upperX = new Vertex(50, 0, 50); + upperX = upperX.add(initial); + Vertex upperY = new Vertex(0, 50, 50); + upperY = upperY.add(initial); + Vertex lowerX = new Vertex(50, 0, 0); + lowerX = lowerX.add(initial); + Vertex lowerY = new Vertex(0, 50, 0); + lowerY = lowerY.add(initial); + Vertex lowerXY = new Vertex(50, 50, 0); + lowerXY = lowerXY.add(initial); + + List xzEdges = new LinkedList(); + xzEdges.add(new Edge(initial, upper)); + xzEdges.add(new Edge(upper, upperX)); + xzEdges.add(new Edge(upperX, lowerX)); + xzEdges.add(new Edge(lowerX, initial)); + + List yzEdges = new LinkedList(); + yzEdges.add(new Edge(initial, upper)); + yzEdges.add(new Edge(upper, upperY)); + yzEdges.add(new Edge(upperY, lowerY)); + yzEdges.add(new Edge(lowerY, initial)); + + List xyEdges = new LinkedList(); + xyEdges.add(new Edge(initial, lowerX)); + xyEdges.add(new Edge(lowerX, lowerXY)); + xyEdges.add(new Edge(lowerXY, lowerY)); + xyEdges.add(new Edge(lowerY, initial)); + + Surface xzSurface = new Surface(xzEdges); + Surface yzSurface = new Surface(yzEdges); + Surface xySurface = new Surface(xyEdges); + glv.getView().addTempSurface(xzSurface); + glv.getView().addTempSurface(yzSurface); + glv.getView().addTempSurface(xySurface); + + List objects = glv.getView().getObjectAtPoint(e.getX(), e.getY()); + + Iterator it = objects.iterator(); + while (it.hasNext()) { + Object ob = it.next(); + if (ob instanceof Surface) { + Surface surface = (Surface)ob; + if (surface.equals(xzSurface) || + surface.equals(yzSurface) || + surface.equals(xySurface)) { + dragPlane = surface.plane(); + restrictionVector = new Vertex(0, 0, 0); + } + } + } + glv.getView().removeTempSurface(xzSurface); + glv.getView().removeTempSurface(xySurface); + glv.getView().removeTempSurface(yzSurface); + clearConstructors(moveConstructors); + } + } + } + + /** * Finds the current restriction vector. * There must be some number of moveconstructors find a *************** *** 292,296 **** * @return wherther or not a vector was found. */ ! private boolean findRestriction(MouseEvent e) { if (moveConstructors != null) { List hitEdges = glv.getView().getEdgeAtPoint(e.getX(), e.getY()); --- 373,377 ---- * @return wherther or not a vector was found. */ ! private boolean findRestrictionVector(MouseEvent e) { if (moveConstructors != null) { List hitEdges = glv.getView().getEdgeAtPoint(e.getX(), e.getY()); *************** *** 310,314 **** restrictionVector = axis.getFrom().minus(axis.getTo()); lastRestriction = restrictionVector.copy(); - //when a moveconstructor is selected it is the //only one shown. --- 391,394 ---- *************** *** 322,325 **** --- 402,430 ---- return false; } + + /** + * Sets an appropriate dragPlane that aligned with the + * restrictionvector and containing a specified point to + * make reasonable movements. + */ + protected void findRestrictionPlane() { + Camera camera = glv.getView().getCamera(); + double[] centerPoint = camera.getCenter(); + double[] cameraPoint = camera.getCamera(); + + Vertex eyeVector = new Vertex(cameraPoint[0] - centerPoint[0], + cameraPoint[1] - centerPoint[1], + cameraPoint[2] - centerPoint[2]); + Vertex cross = restrictionVector.cross(eyeVector); + Vertex planeNormal; + if (cross.isZero()) { + double[] rollVector = camera.getRoll(); + planeNormal = new Vertex(rollVector[0], rollVector[1], rollVector[2]); + } else { + planeNormal = (restrictionVector.cross(eyeVector)).cross(restrictionVector); + } + dragPlane = new Plane(planeNormal.getX(), planeNormal.getY(), planeNormal.getZ(), + -planeNormal.dot(initial)); + } /** *************** *** 344,348 **** /** * Invoked when a key has been pressed. Lets user control the mode of movement. ! * ESCAPE - cancel movement * @param e The KeyEvent */ --- 449,457 ---- /** * Invoked when a key has been pressed. Lets user control the mode of movement. ! * After movement in one direction a length can be typed in to adjust the movement ! * to a specific length. ! * ESCAPE - cancel movement. ! * 0-9 - type length in length field. ! * ENTER - perform move of the specified length. * @param e The KeyEvent */ *************** *** 353,360 **** move(vertices, initial.minus(from)); } } else if (lastRestriction != null && initial != null && from != null && ! vertices != null) { if (e.getKeyCode() == KeyEvent.VK_1) { number += "1"; --- 462,478 ---- move(vertices, initial.minus(from)); } + } else if (e.getKeyCode() == KeyEvent.VK_M) { + if (dragPlane == null) { + if (moveMode == AXIS_RESTRICTED) { + moveMode = FREE_SNAP; + } else { + moveMode = AXIS_RESTRICTED; + } + } } else if (lastRestriction != null && initial != null && from != null && ! vertices != null && ! number != null) { if (e.getKeyCode() == KeyEvent.VK_1) { number += "1"; |