[Bprocessor-commit] model/src/net/sourceforge/bprocessor/model Geometry.java, 1.38, 1.39
Status: Pre-Alpha
Brought to you by:
henryml
|
From: Michael L. <he...@us...> - 2006-12-12 09:50:42
|
Update of /cvsroot/bprocessor/model/src/net/sourceforge/bprocessor/model In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv16435/src/net/sourceforge/bprocessor/model Modified Files: Geometry.java Log Message: Removed old pencil code Index: Geometry.java =================================================================== RCS file: /cvsroot/bprocessor/model/src/net/sourceforge/bprocessor/model/Geometry.java,v retrieving revision 1.38 retrieving revision 1.39 diff -C2 -d -r1.38 -r1.39 *** Geometry.java 12 Dec 2006 09:43:26 -0000 1.38 --- Geometry.java 12 Dec 2006 09:50:39 -0000 1.39 *************** *** 9,13 **** import java.util.ArrayList; - import java.util.Arrays; import java.util.Collection; import java.util.HashSet; --- 9,12 ---- *************** *** 236,680 **** /** - * Find surfaces formed by a sequence of newly inserted edges - * @param edges The list of edges - * @return a list of the surfaces created - */ - public static List surfaceAnalysis(List edges) { - List result = new LinkedList(); - Edge first = (Edge) edges.get(0); - Vertex from = first.getFrom(); - Edge last = (Edge) edges.get(edges.size() - 1); - Vertex to = last.getTo(); - Surface exterior = null; - //Finding out if the edges are all contained in one surface with - //endpoints of the chain of edges as vertices of the surface. - //If such a surface exists it is set to extorior - { - Set fromSurfaces = from.getSurfaces(); - Set toSurfaces = to.getSurfaces(); - Set shared = new HashSet(); - Iterator it = fromSurfaces.iterator(); - while (it.hasNext()) { - Surface current = (Surface) it.next(); - if (toSurfaces.contains(current)) { - shared.add(current); - } - } - if (shared.size() == 1) { - exterior = (Surface) shared.iterator().next(); - } else { - Iterator iter = shared.iterator(); - while (edges.size() > 1 && iter.hasNext() && (exterior == null)) { - Surface currentSurface = (Surface) iter.next(); - Vertex currentVertex = first.otherVertex(from); - int currentIndex = 1; - Edge currentEdge = (Edge) edges.get(currentIndex); - boolean outside = false; - while ((currentVertex != to) && !outside) { - outside = !currentSurface.surrounds(currentVertex); - currentVertex = currentEdge.otherVertex(currentVertex); - currentIndex++; - if (currentIndex < edges.size()) { - currentEdge = (Edge) edges.get(currentIndex); - } else { - currentEdge = null; - } - } - if (!outside) { - exterior = currentSurface; - } - } - } - } - //Find the surfaces - Set siblings = to.getEdges(); - siblings.remove(first); - siblings.remove(last); - while (!siblings.isEmpty()) { - Edge current = (Edge) siblings.iterator().next(); - CoordinateSystem system = CoordinateSystem.create(last, current); - siblings.remove(current); - if (system != null) { - VertexNode node = new VertexNode(to, system); - EdgeNode lastEdgeNode = node.insert(last); - if (lastEdgeNode != null) { - node.insert(current); - siblings = node.insert(siblings); - node.sort(); - result.addAll(findSurfaces(node, lastEdgeNode, edges, exterior)); - } - } - } - return result; - } - - /** - * Find surfaces that the new edges forms in the plane the coordinate system - * of the VertexNode - * @param vertex The Vertex - * @param last The first EdgeNode - * @param edges The edges - * @param exterior The exterior - * @return a list of the surfaces found - */ - private static List findSurfaces(VertexNode vertex, EdgeNode last, - List edges, Surface exterior) { - int count = 0; - List surfaces = new LinkedList(); - Space space = ((Edge) edges.iterator().next()).getOwner(); - { - Surface clockwise = clockwiseSurface(vertex, last, edges); - if (clockwise != null) { - if (clockwise.angle(vertex.system()) < 0) { - space.add(clockwise); - holeAnalysis(clockwise); - surfaces.add(clockwise); - count++; - } - } - } - { - Surface counterclockwise = counterclockwiseSurface(vertex, last, edges); - if (counterclockwise != null) { - if (counterclockwise.angle(vertex.system()) > 0) { - space.add(counterclockwise); - holeAnalysis(counterclockwise); - surfaces.add(counterclockwise); - count++; - } - } - } - if (exterior != null) { - if (count == 2) { - Iterator iter = surfaces.iterator(); - Vertex normal = exterior.normal(); - while (iter.hasNext()) { - Surface current = (Surface) iter.next(); - Vertex n = current.normal(); - Vertex o = n.add(normal); - if (o.isZero()) { - current.flip(); - } - current.setBackDomain(exterior.getBackDomain()); - current.setFrontDomain(exterior.getFrontDomain()); - } - space.remove(exterior); - } - } - return surfaces; - } - - /** - * Find the clockwise surface that edges forms in the coordinate system of - * the VertexNode - * @param vertex The Vertex - * @param last The first EdgeNode - * @param edges The edges - * @return The Surface - */ - private static Surface clockwiseSurface(VertexNode vertex, EdgeNode last, List edges) { - if (last == null) { - throw new Error("last null"); - } - ArrayList result = new ArrayList(); - result.addAll(edges); - - Vertex from = ((Edge) edges.get(0)).getFrom(); - - EdgeNode currentEdge = last; - VertexNode current = vertex; - - while (current.vertex() != from) { - currentEdge = current.clockwise(currentEdge.edge()); - if (result.contains(currentEdge.edge())) { - return null; - } - result.add(currentEdge.edge()); - current = currentEdge.other(current); - } - return new Surface(result); - } - - /** - * Find the counterclockwise surface that edges forms in the coordinate system of - * the VertexNode - * @param vertex The Vertex - * @param last The first EdgeNode - * @param edges The edges - * @return The Surface - */ - private static Surface counterclockwiseSurface(VertexNode vertex, EdgeNode last, List edges) { - ArrayList result = new ArrayList(); - result.addAll(edges); - - Vertex from = ((Edge) edges.get(0)).getFrom(); - - EdgeNode currentEdge = last; - VertexNode current = vertex; - - while (current.vertex() != from) { - currentEdge = current.counterclockwise(currentEdge.edge()); - if (result.contains(currentEdge.edge())) { - return null; - } - result.add(currentEdge.edge()); - current = currentEdge.other(current); - } - return new Surface(result); - } - - - /** - * The VertexNode class is used for internal surface calculations - */ - private static class VertexNode { - /** The vertex */ - private Vertex vertex; - /** The edgenodes */ - private List edgenodes; - /** The coordinate system */ - private CoordinateSystem system; - - /** - * Constructor for VertexNode - * @param vertex The vertex - * @param system The coordinate systen - */ - public VertexNode(Vertex vertex, CoordinateSystem system) { - this.vertex = vertex; - this.system = system; - edgenodes = new LinkedList(); - } - /** - * Insert a list of edges into this VertexNode by - * finding all edges in that lies in the coordinate - * system, and sorting the edges clockwise - * @param edges List of edges - * @return The edges that are not inserted - */ - public Set insert(Set edges) { - Set result = new HashSet(); - Iterator iter = edges.iterator(); - while (iter.hasNext()) { - Edge current = (Edge) iter.next(); - if (insert(current) == null) { - result.add(current); - } - } - return result; - } - /** - * Test if this edges is in the coordinate system and - * insert if yes. - * @param edge The edge - * @return The EdgeNode if inserted - */ - public EdgeNode insert(Edge edge) { - Vertex v = edge.otherVertex(vertex).minus(vertex); - double dz = v.dot(system.getN()); - if (Math.abs(dz) < 0.00000001) { - double dx = v.dot(system.getI()); - double dy = v.dot(system.getJ()); - EdgeNode node = new EdgeNode(edge, dx, dy); - insert(node); - return node; - } else { - return null; - } - } - - /** - * Insert an EdgeNode - * @param node The EdgeNode - */ - public void insert(EdgeNode node) { - edgenodes.add(node); - } - - /** - * Get the vertex - * @return The vertex - */ - public Vertex vertex() { - return vertex; - } - - /** - * Get the system - * @return The system - */ - public CoordinateSystem system() { - return system; - } - - /** - * Sort the EdgeNodes clockwise around the vertex - */ - public void sort() { - LinkedList right = new LinkedList(); - LinkedList left = new LinkedList(); - Iterator iter = edgenodes.iterator(); - while (iter.hasNext()) { - EdgeNode current = (EdgeNode) iter.next(); - if (current.dx == 0) { - if (current.dy < 0) { - right.addLast(current); - } - if (current.dy > 0) { - left.addLast(current); - } - } - if (current.dx > 0) { - right.addLast(current); - } - if (current.dx < 0) { - left.addLast(current); - } - } - Object[] rights = right.toArray(); - Arrays.sort(rights); - Object[] lefts = left.toArray(); - Arrays.sort(lefts); - LinkedList result = new LinkedList(); - for (int i = 0; i < rights.length; i++) { - result.addLast(((EdgeNode) rights[i])); - } - for (int i = 0; i < lefts.length; i++) { - result.addLast(((EdgeNode) lefts[i])); - } - edgenodes = result; - } - - /** - * Return the edgenode counterclockwise around the vertex from the edge - * @param edge The edgenode - * @return The counterclockwise edgenode - */ - public EdgeNode counterclockwise(Edge edge) { - Object[] sorted = edgenodes.toArray(); - int index = 0; - for (int i = 0; i < sorted.length; i++) { - if (((EdgeNode) sorted[i]).edge() == edge) { - index = i; - } - } - index++; - if (index == sorted.length) { - return (EdgeNode) sorted[0]; - } else { - return (EdgeNode) sorted[index]; - } - } - - /** - * Return the edgenode clockwise around the vertex from the edge - * @param edge The edgen - * @return The clockwise edgenode - */ - public EdgeNode clockwise(Edge edge) { - EdgeNode result = null; - Object[] sorted = edgenodes.toArray(); - int index = 0; - for (int i = 0; i < sorted.length; i++) { - if (((EdgeNode) sorted[i]).edge() == edge) { - index = i; - } - } - if (index == 0) { - result = (EdgeNode) sorted[sorted.length - 1]; - } else { - result = (EdgeNode) sorted[index - 1]; - } - if (result == null) { - throw new Error("null"); - } - return result; - } - - /** - * Return a String describing this VertexNode - * @return String describing this VertexNode - */ - public String toString() { - StringBuffer buffer = new StringBuffer(); - buffer.append("{" + vertex + "\n"); - Iterator iter = edgenodes.iterator(); - while (iter.hasNext()) { - EdgeNode current = (EdgeNode) iter.next(); - buffer.append(" " + current.edge() + "\n"); - } - buffer.append("}\n"); - return buffer.toString(); - } - } - - /** - * The EdgeNode class is used for surface calculations - */ - private static class EdgeNode implements Comparable { - /** The edge */ - private Edge edge; - /** The dx */ - private double dx; - /** The dy */ - private double dy; - - /** - * Constructor for EdgeNode - * @param edge The edge - * @param dx The dx - * @param dy The dy - */ - public EdgeNode(Edge edge, double dx, double dy) { - this.edge = edge; - this.dx = dx; - this.dy = dy; - } - - /** - * Return the Edge - * @return The Edge - */ - public Edge edge() { - return edge; - } - - /** - * Return the other end - * @param node The node - * @return The other end - */ - public VertexNode other(VertexNode node) { - Vertex other = edge.otherVertex(node.vertex()); - CoordinateSystem system = node.system(); - VertexNode result = new VertexNode(other, system); - Set edges = other.getEdges(); - result.insert(edges); - result.sort(); - return result; - } - - /** - * Compare one edgenode to a nother - * @param other The other - * @return The result of comparison - */ - public int compareTo(Object other) { - EdgeNode entry = (EdgeNode) other; - if (this.dx == 0) { - return -1; - } - if (entry.dx == 0) { - return 1; - } - if ((this.dy / this.dx) < (entry.dy / entry.dx)) { - return -1; - } else { - return 1; - } - } - } - - /** * Test if exterior contains interior * @param exterior The exterior surface --- 235,238 ---- |