[Bprocessor-commit] gl/src/net/sourceforge/bprocessor/gl/tool ExtrusionTool.java,1.28,1.29
Status: Pre-Alpha
Brought to you by:
henryml
From: Nordholt <nor...@us...> - 2005-11-18 12:32:17
|
Update of /cvsroot/bprocessor/gl/src/net/sourceforge/bprocessor/gl/tool In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20981 Modified Files: ExtrusionTool.java Log Message: small changes Index: ExtrusionTool.java =================================================================== RCS file: /cvsroot/bprocessor/gl/src/net/sourceforge/bprocessor/gl/tool/ExtrusionTool.java,v retrieving revision 1.28 retrieving revision 1.29 diff -C2 -d -r1.28 -r1.29 *** ExtrusionTool.java 18 Nov 2005 01:27:56 -0000 1.28 --- ExtrusionTool.java 18 Nov 2005 12:32:05 -0000 1.29 *************** *** 31,36 **** import java.util.Set; import java.util.HashSet; import java.util.LinkedList; ! import org.apache.log4j.Logger; /** --- 31,37 ---- import java.util.Set; import java.util.HashSet; + import java.util.HashMap; import java.util.LinkedList; ! import java.util.Map; import org.apache.log4j.Logger; /** *************** *** 178,185 **** --- 179,385 ---- protected void released(MouseEvent e) { super.released(e); + //holeDetection(); spaceAssignment(); } /** + * Detects if the an extrusion went through other surfaces and thus + * making it nessesary to form new holes. (should be done before) space- + * assignment) + */ + private void holeDetection() { + //this is when nothing is extruded + if (extrudeSurface == null) { + return; + } + Surface top = (Surface)extrudedSurfaces.getFirst(); + Surface bottom = (Surface)extrudedSurfaces.getLast(); + + List topVerticies = top.getVertices(); + List sideEdges = new LinkedList(); + Iterator topVertIt = topVerticies.iterator(); + while (topVertIt.hasNext()) { + Set edges = ((Vertex)(topVertIt.next())).getEdges(); + if (edges != null) { + if (edges.size() > 3) { + log.warn("Algorithm assumtion is wrong"); + } + Iterator edgeIt = edges.iterator(); + while (edgeIt.hasNext()) { + Edge edge = (Edge)edgeIt.next(); + if (!top.contains(edge)) { + sideEdges.add(edge); + } + } + } + } + Map surfaceToInnerPoints = new HashMap(); + Map edgeToSplit = new HashMap(); + Map splitToEdge = new HashMap(); + Iterator sIt = Project.getInstance().getSurfaces().iterator(); + while (sIt.hasNext()) { + Surface surface = (Surface)sIt.next(); + if (!extrudedSurfaces.contains(surface)) { + List cutVerticies = new LinkedList(); + List cutEdges = new LinkedList(); + boolean hole = true; + Iterator sideEdgeIt = sideEdges.iterator(); + while (sideEdgeIt.hasNext() && hole) { + Edge edge = (Edge)sideEdgeIt.next(); + Vertex vertex = surface.intersection(edge); + if (vertex == null) { + hole = false; + } else { + vertex = createVertex(vertex); + cutVerticies.add(vertex); + cutEdges.add(edge); + splitToEdge.put(vertex, edge); + if (edgeToSplit.containsKey(edge)) { + LinkedList splitList = (LinkedList)edgeToSplit.get(edge); + edgeToSplit.put(edge, insertSplit(splitList, vertex, edge)); + } else { + LinkedList splitList = new LinkedList(); + splitList.add(vertex); + edgeToSplit.put(edge, splitList); + } + } + } + if (hole) { + surfaceToInnerPoints.put(surface, cutVerticies); + } + } + } + /* + Debug code + Iterator it = edgeToSplit.keySet().iterator(); + while (it.hasNext()) { + Edge edge = (Edge)it.next(); + log.info("To: " + edge.getTo()); + Iterator it2 = ((LinkedList)edgeToSplit.get(edge)).iterator(); + while (it2.hasNext()) { + log.info((Vertex)it2.next()); + } + } + */ + breakExtension(surfaceToInnerPoints, edgeToSplit, splitToEdge); + } + + /** + * Keeps a sorted list of splitting verticies sorted after insertion + * so that the verticies closest to the "to" node is in front of the list. + * @param splitList the original list. + * @param split the new vertex to insert. + * @param edge the edge corresponding to the splitlist. + * @return the new list. + */ + private LinkedList insertSplit(LinkedList splitList, Vertex split, Edge edge) { + Iterator it = splitList.iterator(); + boolean inserted = false; + while (it.hasNext() && !inserted) { + Vertex current = (Vertex)it.next(); + if (closerThan(split, current, edge)) { + int i = splitList.indexOf(current); + splitList.add(i, split); + inserted = true; + } + } + if (!inserted) { + splitList.addLast(split); + } + return splitList; + } + + /** + * Tells wherther or not one vertex is closer to the "to"-vertex of + * an edge than an other vertex. + * @param v1 the first vertex + * @param v2 the second vertex + * @param edge the edge + * @return true if v1 is closer to the "to"-vertex otherwise false + */ + private boolean closerThan(Vertex v1, Vertex v2, Edge edge) { + Vertex direction = edge.getFrom().minus(edge.getTo()); + double distance1; + double distance2; + if (direction.getX() != 0) { + distance1 = (v1.getX() + edge.getTo().getX()) / direction.getX(); + distance2 = (v2.getX() + edge.getTo().getX()) / direction.getX(); + } else if (direction.getY() != 0) { + distance1 = (v1.getY() + edge.getTo().getX()) / direction.getY(); + distance2 = (v2.getY() + edge.getTo().getY()) / direction.getY(); + } else { + distance1 = (v1.getZ() + edge.getTo().getZ()) / direction.getZ(); + distance2 = (v2.getZ() + edge.getTo().getZ()) / direction.getZ(); + } + return distance1 < distance2; + } + + + /** + * Makes the apropriate hole when an extrusion goes through a surface + * @param surfaceToPoints a map having a surface as key and a list of + * verticies corresponding to an innersurface as value + * @param edgeToPoints a map having edges as keys and a ordered list of + * verticies that split the edge as value + * @param pointToEdge a map having verticies as keys and the edge which it splits + * as value. + */ + private void breakExtension(Map surfaceToPoints, Map edgeToPoints, Map pointToEdge) { + //First we create the holes + Set surfaceKeys = surfaceToPoints.keySet(); + Iterator skIt = surfaceKeys.iterator(); + Map fromToTo = new HashMap(); + Map toToFrom = new HashMap(); + while (skIt.hasNext()) { + Surface surface = (Surface)skIt.next(); + List verticies = (List)surfaceToPoints.get(surface); + Iterator vertIt = verticies.iterator(); + Vertex origin; + if (vertIt.hasNext()) { + List edges = new LinkedList(); + origin = (Vertex)vertIt.next(); + Vertex from = origin; + Vertex to; + while (vertIt.hasNext()) { + to = (Vertex)vertIt.next(); + if (to != null) { + edges.add(createEdge(from, to)); + toToFrom.put(to, from); + fromToTo.put(from, to); + from = to; + if (to.equals(origin)) { + log.warn("Algorithm assumtions wrong"); + } + } + } + edges.add(createEdge(from, origin)); + Surface hole = createSurface(edges); + surface.addHole(hole); + Project.getInstance().update(surface); + } + } + Iterator it = edgeToPoints.keySet().iterator(); + while (it.hasNext()) { + Iterator pointIt = ((List)edgeToPoints.get((Edge)it.next())).iterator(); + while (pointIt.hasNext()) { + Vertex point = (Vertex)pointIt.next(); + Vertex oppositePoint = (Vertex)toToFrom.get(point); + List edges = new LinkedList(); + edges.add(createEdge(point, oppositePoint)); + Edge oppositeEdge = (Edge)pointToEdge.get(oppositePoint); + LinkedList oppositePoints = (LinkedList)edgeToPoints.get(oppositeEdge); + int indexOfLower = oppositePoints.indexOf(oppositePoint) + 1; + if (indexOfLower < oppositePoints.size()) { + Vertex lowerOppositePoint = (Vertex)oppositePoints.get(indexOfLower); + edges.add(createEdge(oppositePoint, lowerOppositePoint)); + Vertex lowerPoint = (Vertex)(fromToTo.get(lowerOppositePoint)); + edges.add(createEdge(lowerOppositePoint, lowerPoint)); + edges.add(createEdge(lowerPoint, point)); + createSurface(edges); + } + } + } + } + /** * Does appropriate space assignments for the current extrusion. Does * Nothing if nothing has been extruded. *************** *** 417,475 **** for extrusion, and if the "number"-variable has been initialised */ if (dragSurface != null && number != null) { ! if (number.equals("") && e.getKeyCode() == KeyEvent.VK_MINUS) { ! number = "-"; ! } else { ! if (e.getKeyCode() == KeyEvent.VK_1) { ! number += "1"; ! } else if (e.getKeyCode() == KeyEvent.VK_2) { ! number += "2"; ! } else if (e.getKeyCode() == KeyEvent.VK_3) { ! number += "3"; ! } else if (e.getKeyCode() == KeyEvent.VK_4) { ! number += "4"; ! } else if (e.getKeyCode() == KeyEvent.VK_5) { ! number += "5"; ! } else if (e.getKeyCode() == KeyEvent.VK_6) { ! number += "6"; ! } else if (e.getKeyCode() == KeyEvent.VK_7) { ! number += "7"; ! } else if (e.getKeyCode() == KeyEvent.VK_8) { ! number += "8"; ! } else if (e.getKeyCode() == KeyEvent.VK_9) { ! number += "9"; ! } else if (e.getKeyCode() == KeyEvent.VK_0) { ! number += "0"; ! } else if (e.getKeyCode() == KeyEvent.VK_ENTER) { ! if (!number.equals("")) { ! double length = glv.getLength(); ! double delta = extrusion.getLength() - length; ! Vertex extrusionDir = extrusion.getFrom().minus(extrusion.getTo()); ! Vertex normal = dragSurface.normal(); ! normal.scale(1 / normal.length()); ! if (normal.dot(extrusionDir) > 0) { ! delta = 0 - delta; ! } ! moveDelta(delta); ! glv.repaint(); ! } ! } else if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) { ! int length = number.length(); ! if (length > 0) { ! number = number.substring(0, length - 1); } } ! } ! ! if (number.equals("-") || number.equals("")) { ! glv.setLength(0); ! } else { ! try { ! double d = Double.parseDouble(number); ! glv.setLength(d / 1000); ! } catch (NumberFormatException exp) { ! log.warn(exp); } } } super.keyPressed(e); --- 617,672 ---- for extrusion, and if the "number"-variable has been initialised */ + super.keyPressed(e); if (dragSurface != null && number != null) { ! if (e.getKeyCode() == KeyEvent.VK_1) { ! number += "1"; ! } else if (e.getKeyCode() == KeyEvent.VK_2) { ! number += "2"; ! } else if (e.getKeyCode() == KeyEvent.VK_3) { ! number += "3"; ! } else if (e.getKeyCode() == KeyEvent.VK_4) { ! number += "4"; ! } else if (e.getKeyCode() == KeyEvent.VK_5) { ! number += "5"; ! } else if (e.getKeyCode() == KeyEvent.VK_6) { ! number += "6"; ! } else if (e.getKeyCode() == KeyEvent.VK_7) { ! number += "7"; ! } else if (e.getKeyCode() == KeyEvent.VK_8) { ! number += "8"; ! } else if (e.getKeyCode() == KeyEvent.VK_9) { ! number += "9"; ! } else if (e.getKeyCode() == KeyEvent.VK_0) { ! number += "0"; ! } else if (e.getKeyCode() == KeyEvent.VK_ENTER) { ! if (!number.equals("")) { ! double length = glv.getLength(); ! double delta = extrusion.getLength() - length; ! Vertex extrusionDir = extrusion.getFrom().minus(extrusion.getTo()); ! Vertex normal = dragSurface.normal(); ! normal.scale(1 / normal.length()); ! if (normal.dot(extrusionDir) > 0) { ! delta = 0 - delta; } + moveDelta(delta); + glv.repaint(); } ! } else if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) { ! int length = number.length(); ! if (length > 0) { ! number = number.substring(0, length - 1); } } + } + + if (number.equals("") || number.equals("-")) { + glv.setLength(0); + } else { + try { + double d = Double.parseDouble(number); + glv.setLength(d / 1000); + } catch (NumberFormatException exp) { + log.warn(exp); + } } super.keyPressed(e); |