[Bprocessor-commit] model/src/net/sourceforge/bprocessor/model Space.java,1.28,1.29
Status: Pre-Alpha
Brought to you by:
henryml
From: Nordholt <nor...@us...> - 2006-05-03 10:12:44
|
Update of /cvsroot/bprocessor/model/src/net/sourceforge/bprocessor/model In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26938 Modified Files: Space.java Log Message: added method for copying a space Index: Space.java =================================================================== RCS file: /cvsroot/bprocessor/model/src/net/sourceforge/bprocessor/model/Space.java,v retrieving revision 1.28 retrieving revision 1.29 diff -C2 -d -r1.28 -r1.29 *** Space.java 1 May 2006 09:55:15 -0000 1.28 --- Space.java 3 May 2006 10:12:34 -0000 1.29 *************** *** 16,19 **** --- 16,20 ---- import java.util.List; import java.util.Set; + import java.util.Map; import org.apache.log4j.Logger; *************** *** 911,913 **** --- 912,1015 ---- Project.getInstance().delete(this); } + + /** + * Copy this space and return the copy + * @return the copy of this space + */ + public Space copy() { + Map copiedVertices = new HashMap(); + Map copiedEdges = new HashMap(); + Map copiedSurfaces = new HashMap(); + Map copiedSpaces = new HashMap(); + + + Space copy = new Space("*copy of " + this.getName() + "*", + this.getType(), + this.isContainer()); + + /* Copying the envelope of this space */ + + /* vertices */ + Iterator it = this.collect().iterator(); + while (it.hasNext()) { + Vertex v = (Vertex)it.next(); + Vertex newVertex = v.copy(); + if (vertices.values().contains(v)) { + copy.add(newVertex); + } else { + Project.getInstance().add(newVertex); + } + copiedVertices.put(v, newVertex); + } + + /* surfaces and edges */ + it = this.envelope.iterator(); + while (it.hasNext()) { + Surface s = (Surface)it.next(); + Iterator edgeIt = s.getEdges().iterator(); + List newEdges = new LinkedList(); + while (edgeIt.hasNext()) { + Edge e = (Edge)edgeIt.next(); + if (copiedEdges.keySet().contains(e)) { + newEdges.add(copiedEdges.get(e)); + } else { + Edge newEdge = new Edge((Vertex)copiedVertices.get(e.getTo()), + (Vertex)copiedVertices.get(e.getFrom())); + Project.getInstance().add(newEdge); + copiedEdges.put(e, newEdge); + newEdges.add(newEdge); + } + } + Surface newSurface = new Surface(newEdges); + Space worldEmpty = Project.getInstance().world().getEmpty(); + if (s.getBackDomain() == this) { + newSurface.setBackDomain(copy); + } else { + newSurface.setBackDomain(worldEmpty); + } + + if (s.getFrontDomain() == this) { + newSurface.setFrontDomain(copy); + } else { + newSurface.setFrontDomain(worldEmpty); + } + copiedSurfaces.put(s, newSurface); + Project.getInstance().add(newSurface); + } + + Set newSurfaces = new HashSet(copiedSurfaces.values()); + copy.setEnvelope(newSurfaces); + Project.getInstance().add(copy); + + /* Copying elements of this space */ + + /* edges */ + it = edges.values().iterator(); + while (it.hasNext()) { + Edge e = (Edge)it.next(); + Edge elementEdge = new Edge((Vertex)copiedVertices.get(e.getTo()), + (Vertex)copiedVertices.get(e.getFrom())); + copiedEdges.put(e, elementEdge); + copy.add(elementEdge); + } + + /* surfaces */ + it = surfaces.values().iterator(); + while (it.hasNext()) { + Surface s = (Surface)it.next(); + List edgeCopies = new LinkedList(); + Iterator edgeIt = s.getEdges().iterator(); + while (edgeIt.hasNext()) { + Edge e = (Edge)edgeIt.next(); + edgeCopies.add(copiedEdges.get(e)); + } + Surface elementSurface = new Surface(edgeCopies); + elementSurface.setBackDomain(s.getBackDomain()); + elementSurface.setFrontDomain(s.getFrontDomain()); + copiedSurfaces.put(s, elementSurface); + copy.add(elementSurface); + } + + return copy; + } } |