[Bprocessor-commit] model/src/net/sourceforge/bprocessor/model SurfaceAnalysis.java, 1.16, 1.17
Status: Pre-Alpha
Brought to you by:
henryml
From: Michael L. <he...@us...> - 2008-09-30 13:21:24
|
Update of /cvsroot/bprocessor/model/src/net/sourceforge/bprocessor/model In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv9407/src/net/sourceforge/bprocessor/model Modified Files: SurfaceAnalysis.java Log Message: Refactored surfaceanalysis Index: SurfaceAnalysis.java =================================================================== RCS file: /cvsroot/bprocessor/model/src/net/sourceforge/bprocessor/model/SurfaceAnalysis.java,v retrieving revision 1.16 retrieving revision 1.17 diff -C2 -d -r1.16 -r1.17 *** SurfaceAnalysis.java 11 Jan 2008 09:22:08 -0000 1.16 --- SurfaceAnalysis.java 30 Sep 2008 13:20:46 -0000 1.17 *************** *** 22,26 **** public class SurfaceAnalysis { private static final boolean VERBOSE = false; ! /** * --- 22,26 ---- public class SurfaceAnalysis { private static final boolean VERBOSE = false; ! /** * *************** *** 37,41 **** return false; } ! /** * --- 37,41 ---- return false; } ! /** * *************** *** 58,62 **** return false; } ! /** * Remove all coordinatesystems from space --- 58,62 ---- return false; } ! /** * Remove all coordinatesystems from space *************** *** 69,74 **** } } ! ! /** * --- 69,74 ---- } } ! ! /** * *************** *** 79,83 **** return planeAnalysis(space, space.getEdges()); } ! /** * @param space Space --- 79,83 ---- return planeAnalysis(space, space.getEdges()); } ! /** * @param space Space *************** *** 93,99 **** { Collection<Vertex> vertices = space.getVertices(); ! Map<Vertex, List<Edge>> edgemap = space.getEdgeMap(vertices); ! for (Edge current : edges) { List<Edge> incident = new LinkedList(); --- 93,99 ---- { Collection<Vertex> vertices = space.getVertices(); ! Map<Vertex, List<Edge>> edgemap = space.getEdgeMap(vertices); ! for (Edge current : edges) { List<Edge> incident = new LinkedList(); *************** *** 144,148 **** return systems; } ! /** * Clear surfaces in specified space --- 144,148 ---- return systems; } ! /** * Clear surfaces in specified space *************** *** 155,159 **** } } ! /** * Find surfaces in the space starting with the --- 155,159 ---- } } ! /** * Find surfaces in the space starting with the *************** *** 174,178 **** return added; } ! /** * Find surfaces in specified space --- 174,178 ---- return added; } ! /** * Find surfaces in specified space *************** *** 185,435 **** } } ! /** - * Find surfaces in specified space that lies in the specified - * coordinatesystem * @param space Space ! * @param system CoordinateSystem ! * @param start collection of edges ! * @return collection of Surfaces */ public Collection<Surface> surfaceAnalysis(Container space, CoordinateSystem system, Collection<Edge> start) { ! ! if (VERBOSE) { ! System.out.println(" plane " + system.getN()); ! } ! ! Plane plane = system.plane(); ! Map<Vertex, VertexNode> vmap = new LinkedHashMap(); ! Map<Edge, EdgeNode> emap = new LinkedHashMap(); ! { ! List<Vertex> vertices = new LinkedList(space.getVertices()); ! Entity.sort(vertices); ! for (Vertex current : vertices) { ! if (plane.contains(current)) { ! vmap.put(current, new VertexNode(current)); ! } ! } ! } ! { ! List<Edge> edges = new LinkedList(space.getEdges()); ! Entity.sort(edges); ! for (Edge current : edges) { ! VertexNode from = vmap.get(current.getFrom()); ! VertexNode to = vmap.get(current.getTo()); ! if (from != null && to != null) { ! EdgeNode node = new EdgeNode(current, from, to); ! emap.put(current, node); ! from.add(node); ! to.add(node); ! } ! } ! } ! for (VertexNode current : vmap.values()) { ! current.sort(system); ! } ! { ! Set<Edge> keys = emap.keySet(); ! Collection<Surface> surfaces = space.getSurfaces(); ! for (Surface current : surfaces) { ! if (keys.containsAll(current.getEdges())) { ! int direction = Surface.direction(current.getEdges(), system); ! if (direction == Surface.RIGHT) { ! VertexNode from = vmap.get(current.getFirstVertex()); ! for (Edge edge : current.getEdges()) { ! EdgeNode node = emap.get(edge); ! VertexNode to = node.other(from); ! node.addRight(to, current); ! from = to; ! } ! } ! if (direction == Surface.LEFT) { ! VertexNode from = (VertexNode) vmap.get(current.getFirstVertex()); ! for (Edge edge : current.getEdges()) { ! EdgeNode node = (EdgeNode) emap.get(edge); ! VertexNode to = node.other(from); ! node.addLeft(to, current); ! from = to; ! } ! } ! } ! } ! } ! Set<Surface> added = new HashSet(); ! Set<Surface> removed = new HashSet(); ! for (Edge current : start) { ! if (emap.containsKey(current)) { ! EdgeNode node = emap.get(current); ! surfaceAnalysis(node, system, added, removed); ! } ! } ! for (Surface current : removed) { ! current.delete(); ! } ! for (Surface current : added) { ! space.add(current); ! } ! return added; ! } ! ! private List<Edge> rightContour(EdgeNode start) { ! List<Edge> edges = new LinkedList(); ! VertexNode to = start.to; ! EdgeNode current = start; ! EdgeNode next = null; ! while (next != start) { ! edges.add(current.edge); ! next = current.succ(to); ! if (next.left == next.right) { ! next = next.succ(to); ! } ! to = next.other(to); ! current = next; ! } ! return edges; ! } ! ! private List<Edge> leftContour(EdgeNode start) { ! List<Edge> edges = new LinkedList(); ! VertexNode to = start.to; ! EdgeNode current = start; ! EdgeNode next = null; ! while (next != start) { ! edges.add(current.edge); ! next = current.pred(to); ! if (next.left == next.right) { ! next = next.pred(to); ! } ! to = next.other(to); ! current = next; ! } ! return edges; ! } ! ! private boolean isVoid(Space space) { ! if (space != null) { ! return space.isVoid(); ! } else { ! return true; ! } ! } ! ! private void assign(Surface surface, Collection<Surface> surfaces) { ! if (VERBOSE) { ! System.out.println(" assign " + surface); ! for (Surface current : surfaces) { ! String name = current.getName(); ! Space front = current.getFrontDomain(); ! Space back = current.getBackDomain(); ! String desc = "[" + name + " " + front + " | " + back + "]"; ! System.out.println(" " + desc); ! } ! } ! ! Container back = null; ! Material backmaterial = null; ! Container front = null; ! Material frontmaterial = null; ! Vertex n1 = surface.normal(); ! for (Surface other : surfaces) { ! Vertex n2 = other.normal(); ! if (n1.dot(n2) > 0) { ! if (isVoid(back)) { ! back = other.getBackDomain(); ! backmaterial = other.getBackMaterial(); ! } ! if (isVoid(front)) { ! front = other.getFrontDomain(); ! frontmaterial = other.getFrontMaterial(); ! } ! } else { ! if (isVoid(back)) { ! back = other.getFrontDomain(); ! backmaterial = other.getFrontMaterial(); ! } ! if (isVoid(front)) { ! front = other.getBackDomain(); ! frontmaterial = other.getBackMaterial(); ! } ! } ! } ! if (VERBOSE) { ! System.out.println(" " + front + " | " + back); ! } ! surface.setBackDomain(back); ! surface.setBackMaterial(backmaterial); ! surface.setFrontDomain(front); ! surface.setFrontMaterial(frontmaterial); ! } ! ! private boolean marked(SurfaceNode node) { ! if (node == null) { ! return false; ! } else { ! return node.mark; ! } ! } ! ! private void surfaceAnalysis (EdgeNode start, CoordinateSystem system, ! Set<Surface> added, Set<Surface> removed) { ! if (!marked(start.left)) { ! if (VERBOSE) { ! System.out.println(" left-side " + start.edge); ! } ! ! SurfaceNode s = new SurfaceNode(); ! s.mark = true; ! VertexNode to = start.to; ! EdgeNode current = start; ! while (current.getLeft(to) != s) { ! s.consume(current.getLeft(to)); ! current.setLeft(to, s); ! current = current.pred(to); ! to = current.other(to); ! } ! if (start.left != start.right) { ! List<Edge> edges = leftContour(start); ! if (edges.size() > 2) { ! int turn = Surface.direction(edges, system); ! if (turn == Surface.LEFT) { ! Surface surface = new Surface(edges); ! assign(surface, s.surfaces); ! added.add(surface); ! removed.addAll(s.surfaces); ! } ! } ! } ! } ! if (!marked(start.right)) { ! if (VERBOSE) { ! System.out.println(" right-side " + start.edge); ! } ! ! SurfaceNode s = new SurfaceNode(); ! s.mark = true; ! VertexNode to = start.to; ! EdgeNode current = start; ! while (current.getRight(to) != s) { ! s.consume(current.getRight(to)); ! current.setRight(to, s); ! current = current.succ(to); ! to = current.other(to); ! } ! if (start.right != start.left) { ! List<Edge> edges = rightContour(start); ! if (edges.size() > 2) { ! int turn = Surface.direction(edges, system); ! if (turn == Surface.RIGHT) { ! Surface surface = new Surface(edges); ! assign(surface, s.surfaces); ! added.add(surface); ! removed.addAll(s.surfaces); ! } ! } ! } ! } } ! /** * VertexNode --- 185,202 ---- } } ! ! /** * @param space Space ! * @param system CoordinteSystem ! * @param start Start list of edges ! * @return surfaces */ public Collection<Surface> surfaceAnalysis(Container space, CoordinateSystem system, Collection<Edge> start) { ! EdgeStructure structure = new EdgeStructure(space, system, start); ! return structure.surfaceAnalysis(start); } ! /** * VertexNode *************** *** 440,444 **** /** edges */ private List<EdgeNode> edges; ! /** * Constructor --- 207,211 ---- /** edges */ private List<EdgeNode> edges; ! /** * Constructor *************** *** 449,453 **** edges = new LinkedList(); } ! /** * Return vertex --- 216,220 ---- edges = new LinkedList(); } ! /** * Return vertex *************** *** 457,461 **** return vertex; } ! /** * --- 224,228 ---- return vertex; } ! /** * *************** *** 465,469 **** edges.add(edge); } ! /** * Sort edges --- 232,236 ---- edges.add(edge); } ! /** * Sort edges *************** *** 495,499 **** } } ! /** * @return String --- 262,266 ---- } } ! /** * @return String *************** *** 503,507 **** } } ! /** * EdgeNode --- 270,531 ---- } } ! ! /** ! * ! */ ! public class EdgeStructure { ! private Map<Vertex, VertexNode> vmap; ! private Map<Edge, EdgeNode> emap; ! private Container space; ! private CoordinateSystem system; ! ! private List<Edge> rightContour(EdgeNode start) { ! List<Edge> edges = new LinkedList(); ! VertexNode to = start.to; ! EdgeNode current = start; ! EdgeNode next = null; ! while (next != start) { ! edges.add(current.edge); ! next = current.succ(to); ! if (next.left == next.right) { ! next = next.succ(to); ! } ! to = next.other(to); ! current = next; ! } ! return edges; ! } ! ! private List<Edge> leftContour(EdgeNode start) { ! List<Edge> edges = new LinkedList(); ! VertexNode to = start.to; ! EdgeNode current = start; ! EdgeNode next = null; ! while (next != start) { ! edges.add(current.edge); ! next = current.pred(to); ! if (next.left == next.right) { ! next = next.pred(to); ! } ! to = next.other(to); ! current = next; ! } ! return edges; ! } ! ! private boolean isVoid(Space space) { ! if (space != null) { ! return space.isVoid(); ! } else { ! return true; ! } ! } ! ! private void assign(Surface surface, Collection<Surface> surfaces) { ! if (VERBOSE) { ! System.out.println(" assign " + surface); ! for (Surface current : surfaces) { ! String name = current.getName(); ! Space front = current.getFrontDomain(); ! Space back = current.getBackDomain(); ! String desc = "[" + name + " " + front + " | " + back + "]"; ! System.out.println(" " + desc); ! } ! } ! ! Container back = null; ! Material backmaterial = null; ! Container front = null; ! Material frontmaterial = null; ! Vertex n1 = surface.normal(); ! for (Surface other : surfaces) { ! Vertex n2 = other.normal(); ! if (n1.dot(n2) > 0) { ! if (isVoid(back)) { ! back = other.getBackDomain(); ! backmaterial = other.getBackMaterial(); ! } ! if (isVoid(front)) { ! front = other.getFrontDomain(); ! frontmaterial = other.getFrontMaterial(); ! } ! } else { ! if (isVoid(back)) { ! back = other.getFrontDomain(); ! backmaterial = other.getFrontMaterial(); ! } ! if (isVoid(front)) { ! front = other.getBackDomain(); ! frontmaterial = other.getBackMaterial(); ! } ! } ! } ! if (VERBOSE) { ! System.out.println(" " + front + " | " + back); ! } ! surface.setBackDomain(back); ! surface.setBackMaterial(backmaterial); ! surface.setFrontDomain(front); ! surface.setFrontMaterial(frontmaterial); ! } ! ! private boolean marked(SurfaceNode node) { ! if (node == null) { ! return false; ! } else { ! return node.mark; ! } ! } ! ! ! private void surfaceAnalysis (EdgeNode start, CoordinateSystem system, ! Set<Surface> added, Set<Surface> removed) { ! if (!marked(start.left)) { ! if (VERBOSE) { ! System.out.println(" left-side " + start.edge); ! } ! ! SurfaceNode s = new SurfaceNode(); ! s.mark = true; ! VertexNode to = start.to; ! EdgeNode current = start; ! while (current.getLeft(to) != s) { ! s.consume(current.getLeft(to)); ! current.setLeft(to, s); ! current = current.pred(to); ! to = current.other(to); ! } ! if (start.left != start.right) { ! List<Edge> edges = leftContour(start); ! if (edges.size() > 2) { ! int turn = Surface.direction(edges, system); ! if (turn == Surface.LEFT) { ! Surface surface = new Surface(edges); ! assign(surface, s.surfaces); ! added.add(surface); ! removed.addAll(s.surfaces); ! } ! } ! } ! } ! if (!marked(start.right)) { ! if (VERBOSE) { ! System.out.println(" right-side " + start.edge); ! } ! ! SurfaceNode s = new SurfaceNode(); ! s.mark = true; ! VertexNode to = start.to; ! EdgeNode current = start; ! while (current.getRight(to) != s) { ! s.consume(current.getRight(to)); ! current.setRight(to, s); ! current = current.succ(to); ! to = current.other(to); ! } ! if (start.right != start.left) { ! List<Edge> edges = rightContour(start); ! if (edges.size() > 2) { ! int turn = Surface.direction(edges, system); ! if (turn == Surface.RIGHT) { ! Surface surface = new Surface(edges); ! assign(surface, s.surfaces); ! added.add(surface); ! removed.addAll(s.surfaces); ! } ! } ! } ! } ! } ! ! /** ! * ! * @param space Space ! * @param system CoordinateSystem ! * @param start List of edges ! */ ! public EdgeStructure(Container space, CoordinateSystem system, ! Collection<Edge> start) { ! this.space = space; ! this.system = system; ! Plane plane = system.plane(); ! vmap = new LinkedHashMap(); ! emap = new LinkedHashMap(); ! { ! List<Vertex> vertices = new LinkedList(space.getVertices()); ! Entity.sort(vertices); ! for (Vertex current : vertices) { ! if (plane.contains(current)) { ! vmap.put(current, new VertexNode(current)); ! } ! } ! } ! { ! List<Edge> edges = new LinkedList(space.getEdges()); ! Entity.sort(edges); ! for (Edge current : edges) { ! VertexNode from = vmap.get(current.getFrom()); ! VertexNode to = vmap.get(current.getTo()); ! if (from != null && to != null) { ! EdgeNode node = new EdgeNode(current, from, to); ! emap.put(current, node); ! from.add(node); ! to.add(node); ! } ! } ! } ! for (VertexNode current : vmap.values()) { ! current.sort(system); ! } ! { ! Set<Edge> keys = emap.keySet(); ! Collection<Surface> surfaces = space.getSurfaces(); ! for (Surface current : surfaces) { ! if (keys.containsAll(current.getEdges())) { ! int direction = Surface.direction(current.getEdges(), system); ! if (direction == Surface.RIGHT) { ! VertexNode from = vmap.get(current.getFirstVertex()); ! for (Edge edge : current.getEdges()) { ! EdgeNode node = emap.get(edge); ! VertexNode to = node.other(from); ! node.addRight(to, current); ! from = to; ! } ! } ! if (direction == Surface.LEFT) { ! VertexNode from = (VertexNode) vmap.get(current.getFirstVertex()); ! for (Edge edge : current.getEdges()) { ! EdgeNode node = (EdgeNode) emap.get(edge); ! VertexNode to = node.other(from); ! node.addLeft(to, current); ! from = to; ! } ! } ! } ! } ! } ! } ! ! private Collection<Surface> surfaceAnalysis(Collection<Edge> start) { ! Set<Surface> added = new HashSet(); ! Set<Surface> removed = new HashSet(); ! for (Edge current : start) { ! if (emap.containsKey(current)) { ! EdgeNode node = emap.get(current); ! surfaceAnalysis(node, system, added, removed); ! } ! } ! for (Surface current : removed) { ! current.delete(); ! } ! for (Surface current : added) { ! space.add(current); ! } ! return added; ! ! ! } ! } ! /** * EdgeNode *************** *** 526,530 **** /** right surface */ private SurfaceNode right; ! /** * --- 550,554 ---- /** right surface */ private SurfaceNode right; ! /** * *************** *** 538,542 **** this.to = to; } ! /** * --- 562,566 ---- this.to = to; } ! /** * *************** *** 553,557 **** return null; } ! /** * --- 577,581 ---- return null; } ! /** * *************** *** 568,572 **** return null; } ! /** * --- 592,596 ---- return null; } ! /** * *************** *** 583,587 **** return null; } ! /** * --- 607,611 ---- return null; } ! /** * *************** *** 598,602 **** return null; } ! /** * @param end VertexNode --- 622,626 ---- return null; } ! /** * @param end VertexNode *************** *** 611,615 **** } } ! /** * --- 635,639 ---- } } ! /** * *************** *** 626,630 **** return null; } ! /** * --- 650,654 ---- return null; } ! /** * *************** *** 640,644 **** } } ! /** * --- 664,668 ---- } } ! /** * *************** *** 655,659 **** } } ! /** * --- 679,683 ---- } } ! /** * *************** *** 670,674 **** } } ! /** * @return String --- 694,698 ---- } } ! /** * @return String *************** *** 676,687 **** public String toString() { return "{" + edge.getName() ! + " fp:" + ((fromPred == null) ? "nil" : fromPred.edge.getName()) ! + " fs:" + ((fromSucc == null) ? "nil" : fromSucc.edge.getName()) ! + " tp:" + ((toPred == null) ? "nil" : toPred.edge.getName()) ! + " ts:" + ((toSucc == null) ? "nil" : toSucc.edge.getName()) + "}"; } } ! /** * SurfaceNode --- 700,711 ---- public String toString() { return "{" + edge.getName() ! + " fp:" + ((fromPred == null) ? "nil" : fromPred.edge.getName()) ! + " fs:" + ((fromSucc == null) ? "nil" : fromSucc.edge.getName()) ! + " tp:" + ((toPred == null) ? "nil" : toPred.edge.getName()) ! + " ts:" + ((toSucc == null) ? "nil" : toSucc.edge.getName()) + "}"; } } ! /** * SurfaceNode *************** *** 699,708 **** */ private Set<Surface> surfaces; ! /** * Mark used while visiting all sides of adjacent edges. */ private boolean mark; ! /** * Constructs this SurfaceNode with an empty set of surfaces --- 723,732 ---- */ private Set<Surface> surfaces; ! /** * Mark used while visiting all sides of adjacent edges. */ private boolean mark; ! /** * Constructs this SurfaceNode with an empty set of surfaces *************** *** 724,728 **** mark = false; } ! /** * Adds the surfaces from another SurfaceNode to the set of --- 748,752 ---- mark = false; } ! /** * Adds the surfaces from another SurfaceNode to the set of *************** *** 736,740 **** } } ! /** * Adds the surface to the set of surfaces of this SurfaceNode. --- 760,764 ---- } } ! /** * Adds the surface to the set of surfaces of this SurfaceNode. *************** *** 744,748 **** surfaces.add(surface); } ! /** * {@inheritDoc} --- 768,772 ---- surfaces.add(surface); } ! /** * {@inheritDoc} |