Thread: [Bprocessor-commit] gl/src/net/sourceforge/bprocessor/gl/tool VectorMoveTool.java, NONE, 1.1 Contro
Status: Pre-Alpha
Brought to you by:
henryml
From: Michael L. <he...@us...> - 2006-08-04 11:48:15
|
Update of /cvsroot/bprocessor/gl/src/net/sourceforge/bprocessor/gl/tool In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv6715/src/net/sourceforge/bprocessor/gl/tool Modified Files: Tool.java ToolFactory.java ControlledMoveStrategy.java AbstractPencil.java Added Files: VectorMoveTool.java ControlledMoveTool.java AlternateMoveTool.java Log Message: Refactoring of Move Tool Index: Tool.java =================================================================== RCS file: /cvsroot/bprocessor/gl/src/net/sourceforge/bprocessor/gl/tool/Tool.java,v retrieving revision 1.25 retrieving revision 1.26 diff -C2 -d -r1.25 -r1.26 *** Tool.java 10 Jul 2006 15:26:38 -0000 1.25 --- Tool.java 4 Aug 2006 11:48:11 -0000 1.26 *************** *** 56,59 **** --- 56,61 ---- /** The Alternative Rect tool */ public static final int ALT_RECT_TOOL = 17; + /** The controlled move tool */ + public static final int CONTROLLED_MOVE_TOOL = 18; /** --- NEW FILE: ControlledMoveTool.java --- //--------------------------------------------------------------------------------- // $Id: ControlledMoveTool.java,v 1.1 2006/08/04 11:48:11 henryml Exp $ // // Copyright (c) 2005 The BProcessor Team (http://bprocessor.sourceforge.net) // Released under the Lesser GNU Public License v2.1 //--------------------------------------------------------------------------------- package net.sourceforge.bprocessor.gl.tool; import java.awt.Cursor; import java.awt.event.MouseEvent; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import net.sourceforge.bprocessor.gl.GLView; import net.sourceforge.bprocessor.model.Edge; import net.sourceforge.bprocessor.model.Geometric; import net.sourceforge.bprocessor.model.Project; import net.sourceforge.bprocessor.model.Surface; import net.sourceforge.bprocessor.model.Vertex; /** * ControlledMoveTool */ public class ControlledMoveTool extends AlternateMoveTool { /** Slidemap */ private Map slideMap; /** Direction */ private Vertex direction; /** * ControlledMoveTool * @param glv GLView * @param cursor Cursor */ public ControlledMoveTool(GLView glv, Cursor cursor) { super(glv, cursor); } /** * Trys to create the slideMap, returns true if it is succesfull, * false otherwise. * @return werther the creation of the map is succesfull. */ private Map createSlideMap() { Map slideMap = new HashMap(); Iterator it = entities.iterator(); while (it.hasNext()) { Geometric geo = (Geometric)it.next(); Map map = null; if (geo instanceof Edge) { map = createSlideMap((Edge)geo); } else if (geo instanceof Surface) { map = createSlideMap((Surface)geo); } else if (geo instanceof Vertex) { map = createSlideMap((Vertex)geo); } if (map != null) { slideMap.putAll(map); } else { return null; } } return slideMap; } /** * Finds the slidemap for an edge. * @param edge the edge * @return a map for edge, null if none can be found */ private Map createSlideMap(Edge edge) { if (current.object() instanceof Surface) { Surface surface = (Surface)current.object(); if (edge.getSurfaces().contains(surface)) { List edges = surface.getEdges(); int index = edges.indexOf(edge); Edge leftEdge; Edge rightEdge; if (index == 0) { leftEdge = (Edge)edges.get(edges.size() - 1); rightEdge = (Edge)edges.get(index + 1); } else if (index == (edges.size() - 1)) { leftEdge = (Edge)edges.get(index - 1); rightEdge = (Edge)edges.get(0); } else { leftEdge = (Edge)edges.get(index - 1); rightEdge = (Edge)edges.get(index + 1); } Edge toSlideEdge = null; Edge fromSlideEdge = null; Vertex to = edge.getTo(); Vertex from = edge.getFrom(); if (to.getEdges().contains(leftEdge) && from.getEdges().contains(rightEdge)) { toSlideEdge = leftEdge; fromSlideEdge = rightEdge; } else if (from.getEdges().contains(leftEdge) && to.getEdges().contains(rightEdge)) { fromSlideEdge = leftEdge; toSlideEdge = rightEdge; } Map map = new HashMap(); map.put(edge.getTo(), toSlideEdge.getDirection()); map.put(edge.getFrom(), fromSlideEdge.getDirection()); return map; } } return null; } /** * Finds the slide map for a vertex. * @param vertex the vertex * @return a map for the vertex, or null if none could be found. */ private Map createSlideMap(Vertex vertex) { if (current.object() instanceof Edge) { Edge edge = (Edge)current.object(); if (vertex.getEdges().contains(edge)) { Map map = new HashMap(); map.put(vertex, edge.getDirection()); return map; } } return null; } /** * Finds the slide map for a surface. * @param surface the surface * @return the map, or null if none could be found. */ private Map createSlideMap(Surface surface) { Collection verts = surface.getVertices(); Iterator it = verts.iterator(); boolean bound = false; Map map = new HashMap(); while (it.hasNext() && !bound) { Vertex vertex = (Vertex)it.next(); map.put(vertex, surface.normal()); Collection edges = vertex.getEdges(); Iterator edgeIt = edges.iterator(); int otherEdges = 0; while (edgeIt.hasNext() && !bound) { Edge edge = (Edge)edgeIt.next(); if (!surface.getEdges().contains(edge)) { otherEdges++; map.put(vertex, edge.getDirection()); } bound = otherEdges > 1; } bound = otherEdges > 1; } Collection holes = surface.getHoles(); it = holes.iterator(); while (it.hasNext() && !bound) { Map holeMap = createSlideMap((Surface)it.next()); if (holeMap != null) { map.putAll(holeMap); } else { bound = true; } } if (!bound) { return map; } else { return null; } } /** * @param e MouseEvent * @return Success */ protected boolean prepare(MouseEvent e) { current = findIntersection(e); if (current != null) { slideMap = createSlideMap(); if (slideMap != null) { Iterator it = entities.iterator(); boolean found = false; while (it.hasNext() && !found) { direction = direction((Geometric)it.next()); found = direction != null; } } } return slideMap != null; } /** * Finds the direction of the controlled movement. * @param geo a geometric object. * @return the direction. */ private Vertex direction(Geometric geo) { if (geo instanceof Surface) { Surface surface = (Surface)geo; return surface.normal(); } else if (geo instanceof Edge) { Edge edge = (Edge)geo; if (current.object() instanceof Surface) { Surface surface = (Surface)current.object(); if (edge.getSurfaces().contains(surface)) { Vertex edgeDir = edge.getDirection(); Vertex normal = surface.normal(); Vertex dir = edgeDir.cross(normal); dir.scale(1 / dir.length()); return dir; } } } else if (geo instanceof Vertex) { return (Vertex)slideMap.get(geo); } return null; } /** * Move the vertices * @param objects Collection of geometric objects * @param delta Vertex (vector) */ public void move(Collection objects, Vertex delta) { if (slideMap != null && slideMap.keySet().containsAll(vertices)) { Vertex directionCopy = direction.copy(); directionCopy.scale(1 / directionCopy.length()); directionCopy.scale(delta.dot(directionCopy)); delta = directionCopy; if (delta.length() != 0) { Vertex deltaUnit = delta.copy(); deltaUnit.scale(1 / delta.length()); Iterator it = vertices.iterator(); while (it.hasNext()) { Vertex vertex = (Vertex)it.next(); Vertex slideDir = ((Vertex)slideMap.get(vertex)).copy(); slideDir.scale(1 / slideDir.length()); double slideScale; if (delta == null) { Project.info("delta null"); } if (slideDir == null) { Project.info("slideDir null"); } if (deltaUnit == null) { Project.info("deltaUnit null"); } if (delta.getX() != 0) { slideScale = delta.getX() / (slideDir.dot(deltaUnit) * deltaUnit.getX()); } else if (delta.getY() != 0) { slideScale = delta.getY() / (slideDir.dot(deltaUnit) * deltaUnit.getY()); } else { slideScale = delta.getZ() / (slideDir.dot(deltaUnit) * deltaUnit.getZ()); } slideDir.scale(slideScale); vertex.move(slideDir.getX(), slideDir.getY(), slideDir.getZ()); } } } } /** * @see net.sourceforge.bprocessor.gl.tool.AbstractPencil#cleanUp() */ public void cleanUp() { slideMap = null; direction = null; super.cleanUp(); } } Index: ToolFactory.java =================================================================== RCS file: /cvsroot/bprocessor/gl/src/net/sourceforge/bprocessor/gl/tool/ToolFactory.java,v retrieving revision 1.56 retrieving revision 1.57 diff -C2 -d -r1.56 -r1.57 *** ToolFactory.java 31 Jul 2006 14:06:56 -0000 1.56 --- ToolFactory.java 4 Aug 2006 11:48:11 -0000 1.57 *************** *** 60,64 **** /** select tool */ private SpaceTool select; ! /** move tool */ private MoveTool move; --- 60,70 ---- /** select tool */ private SpaceTool select; ! ! /** Alternate Move Tool */ ! private AbstractTool alternate; ! ! /** Alternate Move Tool */ ! private AbstractTool controlled; ! /** move tool */ private MoveTool move; *************** *** 196,199 **** --- 202,207 ---- eraser = new EraserTool(glv, pencilcursor); arc = new ArcTool(glv, pencilcursor); + alternate = new VectorMoveTool(glv, pencilcursor); + controlled = new ControlledMoveTool(glv, pencilcursor); controlledStrategy = new ControlledMoveStrategy(glv, pencilcursor); vectorStrategy = new VectorMoveStrategy(glv, pencilcursor); *************** *** 233,239 **** "Biconextrudetool2.gif", "Controlled Extrude"); mulextrudeBut.setMnemonic(KeyEvent.VK_B); ! moveBut = this.registerTool(Tool.MOVE_TOOL, move, "Biconmovetool.gif", "Move"); moveBut.setMnemonic(KeyEvent.VK_M); ! moveBut.addMouseListener(new MoveButtonMouseListener(moveBut)); rotBut = this.registerTool(Tool.ROTATION_TOOL, rotation, "Biconrotobj.gif", "Rotation"); rotBut.setMnemonic(KeyEvent.VK_R); --- 241,248 ---- "Biconextrudetool2.gif", "Controlled Extrude"); mulextrudeBut.setMnemonic(KeyEvent.VK_B); ! moveBut = this.registerTool(Tool.MOVE_TOOL, alternate, "Biconmovetool.gif", "Move"); moveBut.setMnemonic(KeyEvent.VK_M); ! registerTool(Tool.CONTROLLED_MOVE_TOOL, controlled, "Biconmovetool.gif", "Controlled Move"); ! //moveBut.addMouseListener(new MoveButtonMouseListener(moveBut)); rotBut = this.registerTool(Tool.ROTATION_TOOL, rotation, "Biconrotobj.gif", "Rotation"); rotBut.setMnemonic(KeyEvent.VK_R); Index: AbstractPencil.java =================================================================== RCS file: /cvsroot/bprocessor/gl/src/net/sourceforge/bprocessor/gl/tool/AbstractPencil.java,v retrieving revision 1.52 retrieving revision 1.53 diff -C2 -d -r1.52 -r1.53 *** AbstractPencil.java 3 Aug 2006 14:29:33 -0000 1.52 --- AbstractPencil.java 4 Aug 2006 11:48:11 -0000 1.53 *************** *** 484,488 **** } if (incident != null) { ! constructors.add(new CoordinateSystem(incident.vertex())); } --- 484,490 ---- } if (incident != null) { ! if (start == null || (!start.equalEps(incident))) { ! constructors.add(new CoordinateSystem(incident.vertex())); ! } } *************** *** 672,676 **** timer.restart(); if (intersection != null) { ! Object target = null; float[] targetColor = View.TARGET_COLOR; --- 674,678 ---- timer.restart(); if (intersection != null) { ! target = null; float[] targetColor = View.TARGET_COLOR; *************** *** 881,895 **** protected Set getAffected(Collection moveElements) { HashSet affected = new HashSet(); ! HashSet affectedEdges = new HashSet(); ! HashSet affectedSurfaces = new HashSet(); ! affected.addAll(collect(moveElements)); ! Iterator it = affected.iterator(); ! while (it.hasNext()) { ! Vertex v = (Vertex)it.next(); ! affectedEdges.addAll(v.getEdges()); ! affectedSurfaces.addAll(v.getSurfaces()); ! } ! affected.addAll(affectedEdges); ! affected.addAll(affectedSurfaces); { Iterator iter = moveElements.iterator(); --- 883,895 ---- protected Set getAffected(Collection moveElements) { HashSet affected = new HashSet(); ! ! Set vertices = collect(moveElements); ! Set edges = Vertex.edges(vertices); ! Set surfaces = Edge.surfaces(edges); ! ! affected.addAll(vertices); ! affected.addAll(edges); ! affected.addAll(surfaces); ! { Iterator iter = moveElements.iterator(); --- NEW FILE: VectorMoveTool.java --- //--------------------------------------------------------------------------------- // $Id: VectorMoveTool.java,v 1.1 2006/08/04 11:48:11 henryml Exp $ // // Copyright (c) 2005 The BProcessor Team (http://bprocessor.sourceforge.net) // Released under the Lesser GNU Public License v2.1 //--------------------------------------------------------------------------------- package net.sourceforge.bprocessor.gl.tool; import java.awt.Cursor; import java.util.Collection; import java.util.Iterator; import net.sourceforge.bprocessor.gl.GLView; import net.sourceforge.bprocessor.model.Edge; import net.sourceforge.bprocessor.model.Surface; import net.sourceforge.bprocessor.model.Vertex; /** * VectorMoveTool */ public class VectorMoveTool extends AlternateMoveTool { /** * Constructor for VectorMoveTool * @param glv GLView * @param cursor Cursor */ public VectorMoveTool(GLView glv, Cursor cursor) { super(glv, cursor); } /** * Move the vertices * @param objects Collection of geometric objects * @param delta Vertex (vector) */ public void move(Collection objects, Vertex delta) { Iterator it = objects.iterator(); while (it.hasNext()) { Object elm = it.next(); if (elm instanceof Vertex) { ((Vertex)elm).move(delta.getX(), delta.getY(), delta.getZ()); } else if (elm instanceof Edge) { ((Edge)elm).move(delta.getX(), delta.getY(), delta.getZ()); } else if (elm instanceof Surface) { ((Surface) elm).move(delta.getX(), delta.getY(), delta.getZ()); } } } } Index: ControlledMoveStrategy.java =================================================================== RCS file: /cvsroot/bprocessor/gl/src/net/sourceforge/bprocessor/gl/tool/ControlledMoveStrategy.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** ControlledMoveStrategy.java 21 Jul 2006 10:42:06 -0000 1.6 --- ControlledMoveStrategy.java 4 Aug 2006 11:48:11 -0000 1.7 *************** *** 12,16 **** import net.sourceforge.bprocessor.model.Surface; import net.sourceforge.bprocessor.model.Vertex; - import net.sourceforge.bprocessor.model.Plane; import net.sourceforge.bprocessor.model.Geometric; --- 12,15 ---- *************** *** 38,47 **** private HashMap slideMap; - /** The geometric to be moved controlled */ - private Geometric controlled; - - /** The plane to drag according to */ - private Plane dragPlane; - /** Direction of the controlled movement */ private Vertex direction; --- 37,40 ---- *************** *** 171,176 **** Surface surface = (Surface)current.object(); if (edge.getSurfaces().contains(surface)) { - dragPlane = surface.plane(); - List edges = surface.getEdges(); int index = edges.indexOf(edge); --- 164,167 ---- --- NEW FILE: AlternateMoveTool.java --- //--------------------------------------------------------------------------------- // $Id: AlternateMoveTool.java,v 1.1 2006/08/04 11:48:11 henryml Exp $ // // Copyright (c) 2005 The BProcessor Team (http://bprocessor.sourceforge.net) // Released under the Lesser GNU Public License v2.1 //--------------------------------------------------------------------------------- package net.sourceforge.bprocessor.gl.tool; import java.awt.Cursor; import java.awt.event.MouseEvent; import java.util.Collection; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.Set; import net.sourceforge.bprocessor.gl.GLView; import net.sourceforge.bprocessor.model.Edge; import net.sourceforge.bprocessor.model.Project; import net.sourceforge.bprocessor.model.Surface; import net.sourceforge.bprocessor.model.Vertex; /** * Move Tool */ public class AlternateMoveTool extends AbstractPencil { /** Entities */ protected Set entities; /** Vertices */ protected Collection vertices; /** Affected */ private Set affected; /** From */ private Vertex from; /** Last */ private Vertex last; /** To */ private Vertex to; /** active */ private boolean active; /** * Constructor for MoveTool * @param glv The GLView * @param cursor The Cursor */ public AlternateMoveTool(GLView glv, Cursor cursor) { super(glv, cursor); } /** * Move the vertices * @param objects Collection of geometric objects * @param delta Vertex (vector) */ public void move(Collection objects, Vertex delta) { } /** * Update feedback */ protected void updateFeedback() { if (start == null) { if (!selection.isEmpty()) { if (current != null) { makeTarget(current); updateConstructors(); } } } else { if (current != null) { Edge edge = new Edge(from, to); edge.setStrippled(true); Collection feedback = new LinkedList(); feedback.add(edge); feedback(feedback); makeTarget(current); updateConstructors(); } } } /** * @param e MouseEvent * @return Success */ protected boolean prepare(MouseEvent e) { return true; } /** * @see net.sourceforge.bprocessor.gl.tool.AbstractTool#moved() */ protected void moved(MouseEvent e) { if (start == null) { if (selection.isEmpty()) { findTarget(e); glv.getView().makeTarget(target); } else { current = findIntersection(e); if (current != null) { current = current.copy(); updateFeedback(); } } } else { if (!active) { active = prepare(e); if (active) { excluded(affected); } } if (active) { current = findIntersection(e); if (current != null) { current = current.copy(); to = current.vertex(); move(vertices, to.minus(last)); last = to; updateFeedback(); } } } } /** * @see net.sourceforge.bprocessor.gl.tool.AbstractTool#pressed() */ protected void pressed(MouseEvent e) { if (start == null) { if (selection.isEmpty()) { if (target != null) { entities = new HashSet(); entities.add(target); } } else { entities = new HashSet(); entities.addAll(selection); } if (!entities.isEmpty()) { affected = getAffected(entities); vertices = collect(entities); current = findIntersection(e).copy(); if (current != null) { from = current.vertex(); to = from; last = from; start = current; updateFeedback(); } } } else { Collection surfaces = Edge.surfaces(Vertex.edges(vertices)); Iterator iter = surfaces.iterator(); while (iter.hasNext()) { Surface current = (Surface) iter.next(); holeAnalysis(current); } Project.getInstance().checkpoint(); cleanUp(); moved(e); } } /** * @see net.sourceforge.bprocessor.gl.tool.AbstractTool#dragged() */ protected void dragged(MouseEvent e) { } /** * @see net.sourceforge.bprocessor.gl.tool.AbstractTool#released() */ protected void released(MouseEvent e) { } /** * @see net.sourceforge.bprocessor.gl.tool.AbstractPencil#cleanUp() */ public void cleanUp() { entities = null; affected = null; from = null; to = null; last = null; active = false; excluded(new LinkedList()); super.cleanUp(); } } |