[Bprocessor-commit] /gl/src/net/sourceforge/bprocessor/gl/tool OffsetTool.java, 1.17, 1.18
Status: Pre-Alpha
Brought to you by:
henryml
From: rimestad <rim...@us...> - 2007-09-27 10:50:17
|
Update of /cvsroot/bprocessor//gl/src/net/sourceforge/bprocessor/gl/tool In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv15123/src/net/sourceforge/bprocessor/gl/tool Modified Files: OffsetTool.java Log Message: Made som visual feedback and a changed structur for extrusion Index: OffsetTool.java =================================================================== RCS file: /cvsroot/bprocessor//gl/src/net/sourceforge/bprocessor/gl/tool/OffsetTool.java,v retrieving revision 1.17 retrieving revision 1.18 diff -C2 -d -r1.17 -r1.18 *** OffsetTool.java 20 Sep 2007 07:45:06 -0000 1.17 --- OffsetTool.java 27 Sep 2007 10:50:16 -0000 1.18 *************** *** 12,17 **** import java.awt.event.MouseEvent; import java.util.Collection; ! import java.util.Iterator; import java.util.List; import java.util.LinkedList; --- 12,18 ---- import java.awt.event.MouseEvent; + import java.util.ArrayList; import java.util.Collection; ! import java.util.HashSet; import java.util.List; import java.util.LinkedList; *************** *** 21,26 **** --- 22,29 ---- import net.sourceforge.bprocessor.gl.GLView; import net.sourceforge.bprocessor.gl.model.Intersection; + import net.sourceforge.bprocessor.model.CoordinateSystem; import net.sourceforge.bprocessor.model.Direction; import net.sourceforge.bprocessor.model.Edge; + import net.sourceforge.bprocessor.model.Geometric; import net.sourceforge.bprocessor.model.Geometry; import net.sourceforge.bprocessor.model.Point; *************** *** 39,52 **** /** The current contour */ ! private List contour; /** A map of wich direction each vertex should be moved */ ! private Map directionMap; ! ! /** The last point the offset was moved to */ ! private Vertex lastOffsetPoint; ! ! /** Tells if a surface is found to do the offset from */ ! private boolean surfaceFound; /** A vector perpendicular to both the edge clicked to start the offset, --- 42,49 ---- /** The current contour */ ! private List<Edge> contour; /** A map of wich direction each vertex should be moved */ ! private Map<Vertex, Direction> directionMap; /** A vector perpendicular to both the edge clicked to start the offset, *************** *** 61,64 **** --- 58,69 ---- private Edge edge; + private CoordinateSystem offsetCoord; + + private Surface offsetSurface; + + private LinkedList<Edge> finalEdgeList; + + private Edge guide; + /** * Constructor fo OffsetTool *************** *** 68,74 **** public OffsetTool(GLView glv, Cursor cursor) { super(glv, cursor); ! contour = new LinkedList(); directionMap = new HashMap(); - surfaceFound = false; dragging = false; outNormal = null; --- 73,81 ---- public OffsetTool(GLView glv, Cursor cursor) { super(glv, cursor); ! guide = new Edge(new Vertex(0, 0, 0), new Vertex(0, 0, 0)); ! guide.setStrippled(true); ! contour = new LinkedList<Edge>(); ! finalEdgeList = new LinkedList<Edge>(); directionMap = new HashMap(); dragging = false; outNormal = null; *************** *** 78,138 **** /** - * * Update feedback */ ! public void updateFeedback() { ! if (start != null && lastOffsetPoint != null) { ! if (!surfaceFound) { ! Edge startEdge = (Edge)start.object(); ! if (current != null && ! current.object() instanceof Surface && ! ((Surface)current.object()).contains(startEdge)) { ! surfaceFound = true; ! Surface surface = (Surface)current.object(); ! List<Edge> edges = new LinkedList<Edge>(); ! edges.addAll(surface.getEdges()); ! Collection selection = Selection.primary(); ! if (selection.size() > 0 && edges.containsAll(selection)) { ! log.info("Did contain all"); ! edges.clear(); ! edges.addAll(selection); ! } ! directionMap = new HashMap(); ! contour.addAll(Edge.offset(edges, surface, ! -(current.vertex().minus(start.vertex()).length()), directionMap)); ! feedback(contour); ! outNormal = startEdge.getDirection().cross(surface.normal()); ! outNormal.scale(1 / outNormal.length()); ! if (current.vertex().minus(start.vertex()).dot(outNormal) > 0) { ! outNormal.scale(1 / outNormal.length()); ! } else { ! outNormal.scale(-1 / outNormal.length()); ! } ! } ! lastOffsetPoint = current.vertex().copy(); } else { ! Vertex projection = outNormal.copy(); ! projection.scale((lastOffsetPoint.minus(current.vertex())).dot(projection)); ! double length; ! if (projection.dot(outNormal) > 0) { ! length = projection.length(); ! } else { ! length = -projection.length(); } - moveContour(length); - lastOffsetPoint = current.vertex().copy(); - } - } - if (start == null && current.object() instanceof Surface) { - Surface s = (Surface)current.object(); - if (Selection.primary().isEmpty()) { - edge = s.findClosestEdge(current.vertex(), s.getEdges()); - } else { - edge = s.findClosestEdge(current.vertex(), Selection.primary()); } if (edge != null) { glv.setCursor(this.getCursor()); edgePoint = edge.intersection(current.vertex()); ! LinkedList feedback = new LinkedList(); feedback.add(new Point(edgePoint)); feedback(feedback); --- 85,125 ---- /** * Update feedback */ ! private void updateFeedback() { ! if (start == null) { ! if (!finalEdgeList.isEmpty()) { ! //find closest edge in selection ! edge = findClosestEdge(current.vertex(), finalEdgeList); } else { ! //look at the current object ! if (target instanceof Surface) { ! offsetSurface = (Surface)target; ! lockingPlane = offsetSurface.plane(); ! lock = true; ! } ! if (offsetSurface != null) { ! offsetCoord = offsetSurface.coordinateSystem(); ! edge = findClosestEdge(current.vertex(), offsetSurface.getEdges()); } } if (edge != null) { glv.setCursor(this.getCursor()); edgePoint = edge.intersection(current.vertex()); ! Vertex dir = edge.getDirection(); ! outNormal = dir.cross(offsetCoord.getN()); ! outNormal.normalize(); ! if (offsetSurface != null && current != null) { ! if (offsetSurface.surrounds(current.vertex())) { ! if (outNormal.dot(current.vertex().minus(edgePoint)) > 0) { ! outNormal.scale(-1); ! } ! } else { ! if (outNormal.dot(current.vertex().minus(edgePoint)) < 0) { ! outNormal.scale(-1); ! } ! } ! } ! LinkedList<Geometric> feedback = new LinkedList<Geometric>(); feedback.add(new Point(edgePoint)); feedback(feedback); *************** *** 143,146 **** --- 130,163 ---- current = null; } + } else { + Vertex delta = current.vertex().minus(start.vertex()); + double length = delta.length(); + double dot = delta.dot(outNormal); + if (dot < 0) { + length *= -1; + } + if (contour.isEmpty()) { + if (finalEdgeList.isEmpty()) { + contour = Edge.offset(offsetSurface.getEdges(), offsetCoord, length, directionMap); + } else { + Collection<Edge> edges = finalEdgeList; + contour = Edge.offset(edges, offsetCoord, length, directionMap); + } + Collection<Edge> feedback = new ArrayList<Edge>(contour.size() + 1); + feedback.addAll(contour); + feedback.add(guide); + feedback(feedback); + } else { + // Just use the keys to offset it + for (Vertex origin : directionMap.keySet()) { + Direction d = directionMap.get(origin); + Vertex moveDelta = d.getDirection().copy(); + moveDelta.scale(length); + Vertex to = origin.add(moveDelta); + d.getVertex().set(to); + } + } + guide.setFrom(start.vertex()); + guide.setTo(current.vertex()); } updateLength(); *************** *** 149,152 **** --- 166,186 ---- } + private Edge findClosestEdge(Vertex vertex, Collection<Edge> edges) { + // TODO Auto-generated method stub + double minLength = Double.MAX_VALUE; + Edge closestEdge = null; + for (Geometric g : edges) { + Edge e = (Edge)g; + Vertex intersection = e.intersection(vertex); + Vertex v = intersection.minus(vertex); + double dist = v.length(); + if (e.coincides(intersection) && dist < minLength) { + closestEdge = e; + minLength = dist; + } + } + return closestEdge; + } + /** * Tip when the selection is wrong *************** *** 157,175 **** } - /** - * Moves the offset contour by the sepcified length - * @param length the length to move the offset - */ - private void moveContour(double length) { - Iterator it = directionMap.values().iterator(); - while (it.hasNext()) { - Direction d = (Direction)it.next(); - Vertex dir; - dir = d.getDirection().copy(); - dir.scale(length); - d.getVertex().move(dir.getX(), dir.getY(), dir.getZ()); - } - } - /** * Invoked when the mouse cursor has been moved --- 191,194 ---- *************** *** 178,185 **** @Override protected void moved(MouseEvent e) { ! current = findIntersection(e); ! if (current != null) { ! updateFeedback(); } } --- 197,208 ---- @Override protected void moved(MouseEvent e) { ! current = findIntersection(e); ! findTarget(e); ! if (outNormal != null && start != null) { ! Vertex v = ! (new Edge(start.vertex(), start.vertex().add(outNormal)).intersection(current.vertex())); ! current = new Intersection(v, Intersection.PLANE_INTERSECTION, null); } + updateFeedback(); } *************** *** 189,202 **** */ protected void pressed(MouseEvent e) { ! if (start == null) { if (current != null) { ! if (current.object() instanceof Edge) { ! start = current; ! lastOffsetPoint = start.vertex().copy(); ! } else if (current.object() instanceof Surface) { ! start = new Intersection(edgePoint, Intersection.EDGE, edge); ! lastOffsetPoint = current.vertex().copy(); ! } setTip(secondClickTip()); } } else { --- 212,220 ---- */ protected void pressed(MouseEvent e) { ! if (start == null && edge != null) { if (current != null) { ! start = new Intersection(edgePoint, Intersection.EDGE, edge); setTip(secondClickTip()); + updateFeedback(); } } else { *************** *** 209,214 **** */ public void onVertex() { ! if (start != null && current != null && lastOffsetPoint != null) { ! Vertex lengthVector = start.vertex().minus(current.vertex()); Vertex projection = outNormal.copy(); projection.scale(start.vertex().minus(lastOffsetPoint).dot(projection)); --- 227,234 ---- */ public void onVertex() { ! if (start != null && current != null && offsetCoord != null) { ! updateFeedback(); ! endOffset(); ! /* Vertex lengthVector = start.vertex().minus(current.vertex()); Vertex projection = outNormal.copy(); projection.scale(start.vertex().minus(lastOffsetPoint).dot(projection)); *************** *** 220,224 **** moveContour(length - projectionLength); } ! endOffset(); } } --- 240,244 ---- moveContour(length - projectionLength); } ! endOffset();*/ } } *************** *** 236,253 **** * {@inheritDoc} */ - public void prepare() { - super.prepare(); - cleanSelection(); - } - - /** - * {@inheritDoc} - */ @Override public void cleanUp() { directionMap = new HashMap(); contour.clear(); ! lastOffsetPoint = null; ! surfaceFound = false; outNormal = null; edgePoint = null; --- 256,266 ---- * {@inheritDoc} */ @Override public void cleanUp() { directionMap = new HashMap(); + guide.setFrom(new Vertex(0, 0, 0)); + guide.setTo(new Vertex(0, 0, 0)); contour.clear(); ! offsetSurface = null; outNormal = null; edgePoint = null; *************** *** 255,258 **** --- 268,272 ---- feedback(contour); super.cleanUp(); + prepare(); } *************** *** 280,296 **** /** - * Update the length field - */ - protected void updateLength() { - if (start != null && lastOffsetPoint != null && outNormal != null) { - Vertex projection = outNormal.copy(); - projection.scale((lastOffsetPoint.minus(start.vertex())).dot(projection)); - setLength(projection.length()); - } else { - super.updateLength(); - } - } - - /** * Tip on how to set the first click * @return short describtion of what to do --- 294,297 ---- *************** *** 310,312 **** --- 311,422 ---- " Press Escape to cancel offset"; } + + /** + * {@inheritDoc} + */ + @Override + public void prepare() { + super.prepare(); + finalEdgeList.clear(); + Selection selection = getSelection(); + if (!selection.isEmpty()) { + if (selection.size() == 1) { + selection.clear(); + return; + } + Collection<Edge> edges = new HashSet<Edge>(selection.size()); + Map<Vertex, Collection<Edge>> vertex2edges = new HashMap<Vertex, Collection<Edge>>(); + Edge first = null; + for (Geometric g : selection) { + if (g instanceof Edge) { + Edge e = (Edge)g; + edges.add(e); + Collection<Edge> fromlist = vertex2edges.get(e.getFrom()); + if (fromlist != null) { + fromlist.add(e); + } else { + fromlist = new ArrayList<Edge>(); + fromlist.add(e); + vertex2edges.put(e.getFrom(), fromlist); + } + Collection<Edge> tolist = vertex2edges.get(e.getTo()); + if (tolist != null) { + tolist.add(e); + } else { + tolist = new ArrayList<Edge>(); + tolist.add(e); + vertex2edges.put(e.getTo(), tolist); + } + } else { + edges.clear(); + selection.clear(); + } + } + //Sort the edges so that they are connected + Vertex firstVertex = null; + int firstCount = 0; + for (Vertex v : vertex2edges.keySet()) { + Collection<Edge> fromConnected = vertex2edges.get(v); + if (fromConnected.size() > 2) { + // The edges have to be in one connected not several + finalEdgeList.clear(); + selection.clear(); + break; + } + if (fromConnected.size() == 1) { + //if the vertex only have one connected edge add it is the first + if (firstVertex == null) { + first = fromConnected.iterator().next(); + finalEdgeList.add(first); + firstVertex = v; + } + firstCount++; + } + } + if (firstCount > 2) { + //Can only have two ends + finalEdgeList.clear(); + selection.clear(); + } + if (!finalEdgeList.isEmpty()) { + Edge workEdge = finalEdgeList.get(0); + Vertex workVertex = firstVertex; + while (true) { + workVertex = workEdge.otherVertex(workVertex); + Collection<Edge> connected = vertex2edges.get(workVertex); + Edge prevWorkEdge = workEdge; + for (Edge e : connected) { + if (e != workEdge) { + workEdge = e; + finalEdgeList.add(workEdge); + break; + } + } + if (prevWorkEdge == workEdge) { + break; + } + } + Vertex n = null; + if (finalEdgeList.size() == 2) { + Edge e1 = finalEdgeList.get(0); + Edge e2 = finalEdgeList.get(1); + double angle = e1.getDirection().angle(e2.getDirection()); + if (angle > 0 && angle < Math.PI) { + n = e1.getDirection().cross(e2.getDirection()); + } else { + finalEdgeList.clear(); + selection.clear(); + } + } else { + n = Surface.normal0(finalEdgeList); + } + if (n != null) { + Vertex i = first.getDirection(); + i.normalize(); + Vertex j = n.cross(i); + offsetCoord = new CoordinateSystem(i, j, n, first.getFrom()); + } + } + } + } } |