Thread: [Bprocessor-commit] gl/src/net/sourceforge/bprocessor/gl GLView.java,1.2,1.3
Status: Pre-Alpha
Brought to you by:
henryml
From: rimestad <rim...@us...> - 2005-07-22 11:27:28
|
Update of /cvsroot/bprocessor/gl/src/net/sourceforge/bprocessor/gl In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20763 Modified Files: GLView.java Log Message: Added methods for checking snap for grid, axis and vertexes along with creation of edges and surfaces Index: GLView.java =================================================================== RCS file: /cvsroot/bprocessor/gl/src/net/sourceforge/bprocessor/gl/GLView.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** GLView.java 18 Jul 2005 10:47:56 -0000 1.2 --- GLView.java 22 Jul 2005 11:25:57 -0000 1.3 *************** *** 7,14 **** package net.sourceforge.bprocessor.gl; import net.sourceforge.bprocessor.gui.GUI; ! import javax.swing.event.MouseInputListener; ! import net.java.games.jogl.GLCanvas; import net.java.games.jogl.GLCapabilities; --- 7,27 ---- package net.sourceforge.bprocessor.gl; + import net.sourceforge.bprocessor.kernel.notification.Notification; + import net.sourceforge.bprocessor.kernel.notification.Notifier; import net.sourceforge.bprocessor.gui.GUI; + import net.sourceforge.bprocessor.model.Edge; + import net.sourceforge.bprocessor.model.EdgeFacade; + import net.sourceforge.bprocessor.model.Surface; + import net.sourceforge.bprocessor.model.SurfaceFacade; + import net.sourceforge.bprocessor.model.Vertex; + import net.sourceforge.bprocessor.model.VertexFacade; ! import java.util.EventListener; ! import java.util.HashSet; ! import java.util.Iterator; ! import java.util.Set; ! import java.awt.event.MouseListener; ! import java.awt.event.MouseMotionListener; ! import java.awt.event.MouseWheelListener; import net.java.games.jogl.GLCanvas; import net.java.games.jogl.GLCapabilities; *************** *** 24,44 **** private static Logger log = Logger.getLogger(GLView.class); ! /** GL canvas */ ! private GLCanvas glc; ! /** current event listener */ ! private AbstractViewListener currentListener; /** The 3D view mode */ ! public static final int MODE_3D = 0; /** The x z plane view */ ! public static final int MODE_XZ = 1; /** The x y plane view */ ! public static final int MODE_XY = 2; /** The y z plane view */ ! public static final int MODE_YZ = 3; /** --- 37,84 ---- private static Logger log = Logger.getLogger(GLView.class); ! /** An vertex counter */ ! private static long vertexNum = 0; ! /** An edge counter */ ! private static long edgeNum = 0; ! ! /** An surface counter */ ! private static long surfaceNum = 0; ! ! /** The snap variable */ ! public static final double EPSILON = 0.4; /** The 3D view mode */ ! protected static final int MODE_3D = 0; /** The x z plane view */ ! protected static final int MODE_XZ = 1; /** The x y plane view */ ! protected static final int MODE_XY = 2; /** The y z plane view */ ! protected static final int MODE_YZ = 3; ! ! /** The 3D listener*/ ! private static AbstractViewListener view3D; ! ! /**The XY listener */ ! private static AbstractViewListener viewXY; ! ! /** The XZ listener */ ! private static AbstractViewListener viewXZ; ! ! /** The */ ! private static AbstractViewListener viewYZ; ! ! /** GL canvas */ ! private GLCanvas glc; ! ! /** current event listener */ ! private AbstractViewListener currentListener; ! ! /** The current Edge list */ ! private Set edges; /** *************** *** 52,60 **** glc.addGLEventListener(currentListener); ! MouseInputListener mil = new GLMouseListener(this); ! glc.addMouseListener(mil); ! glc.addMouseMotionListener(mil); glc.addKeyListener(new GLKeyListener(this)); GUI.getInstance().registerPanel(glc, GUI.SPLIT_MIDDLE); } --- 92,108 ---- glc.addGLEventListener(currentListener); ! view3D = new View3DListener(); ! viewXY = new ViewXYListener(); ! viewXZ = new ViewXZListener(); ! viewYZ = new ViewYZListener(); ! ! EventListener mil = new GLMouseListener(this); ! glc.addMouseListener((MouseListener)mil); ! glc.addMouseMotionListener((MouseMotionListener)mil); ! glc.addMouseWheelListener((MouseWheelListener)mil); glc.addKeyListener(new GLKeyListener(this)); + edges = new HashSet(); + GUI.getInstance().registerPanel(glc, GUI.SPLIT_MIDDLE); } *************** *** 68,78 **** if (mode == MODE_3D) { ! currentListener = new View3DListener(); } else if (mode == MODE_XZ) { ! currentListener = new ViewXZListener(); } else if (mode == MODE_XY) { ! currentListener = new ViewXYListener(); } else if (mode == MODE_YZ) { ! currentListener = new ViewYZListener(); } else { log.error("[changeView] mode non-existing"); --- 116,126 ---- if (mode == MODE_3D) { ! currentListener = view3D; } else if (mode == MODE_XZ) { ! currentListener = viewXZ; } else if (mode == MODE_XY) { ! currentListener = viewXY; } else if (mode == MODE_YZ) { ! currentListener = viewYZ; } else { log.error("[changeView] mode non-existing"); *************** *** 91,94 **** --- 139,347 ---- /** + * Add the last edge to the edgelist => surface + * @param coord The end coordinate + */ + protected void endSurface(double[] coord) { + Edge activeEdge = currentListener.getActiveEdge(); + Vertex to = vertexCollide(coord); + if (to != null) { + // we have to change excisting surface + removeVertex(activeEdge.getTo()); + activeEdge.setTo(to); + } else { + activeEdge.getTo().setX(coord[0]); + activeEdge.getTo().setY(coord[1]); + activeEdge.getTo().setZ(coord[2]); + } + Surface s = surface(); + s.setEdges(edges); + currentListener.setAlignVertex(null); + edges = new HashSet(); + currentListener.setActiveEdge(null); + } + + /** + * Add an edge to the parent edgelist + * @param coord The to point + */ + protected void addEdge(double[] coord) { + if (edges.isEmpty()) { + Edge activeEdge = edge(); + currentListener.setActiveEdge(activeEdge); + Vertex from = vertex(); + from.setX(coord[0]); + from.setY(coord[1]); + from.setZ(coord[2]); + activeEdge.setFrom(from); + Vertex to = vertex(); + to.setX(coord[0]); + to.setY(coord[1]); + to.setZ(coord[2]); + activeEdge.setTo(to); + edges.add(activeEdge); + currentListener.setAlignVertex(null); + } else { + if (vertexCollide(coord) == null) { + // we did not end an edgelist + Edge activeEdge = currentListener.getActiveEdge(); + Vertex from = activeEdge.getTo(); + activeEdge = edge(); + currentListener.setActiveEdge(activeEdge); + activeEdge.setFrom(from); + Vertex to = vertex(); + to.setX(coord[0]); + to.setY(coord[1]); + to.setZ(coord[2]); + activeEdge.setTo(to); + edges.add(activeEdge); + currentListener.setAlignVertex(null); + } else { + // we did end a edgelist + endSurface(coord); + } + } + } + + /** + * Change the to vertex coordinates for the active edge + * @param coord The new coordinate + */ + protected void changeTo(double[] coord) { + Edge activeEdge = currentListener.getActiveEdge(); + if (activeEdge != null && !edges.isEmpty()) { + activeEdge.getTo().setX(coord[0]); + activeEdge.getTo().setY(coord[1]); + activeEdge.getTo().setZ(coord[2]); + } + } + + /** + * Check if the current point is a vertex if so, it registers the vertex as + * snapVertex and alignVertex in the AbstractListener. + * @param coord the cordinate to check for snap (x, y, z) + */ + protected void checkVertexSnap(double[] coord) { + Vertex v = vertexCollide(coord); + if (v != null) { + currentListener.setAlignVertex(v); + currentListener.setSnapVertex(v); + } else { + removeVertexSnap(); + } + } + + /** + * Set the snapVertex variable to null + */ + protected void removeVertexSnap() { + currentListener.setSnapVertex(null); + } + + /** + * Check if this cordinate collide with another vertex + * @param coord The cordinate to check for collision at + * @return The excisting vertex at coord, null if there ain't any + */ + private Vertex vertexCollide(double[] coord) { + Set vertexes = VertexFacade.getInstance().findAll(); + Iterator it = vertexes.iterator(); + while (it.hasNext()) { + Vertex v = (Vertex)it.next(); + if (collide(v.getX(), coord[0]) && + collide(v.getY(), coord[1]) && + collide(v.getZ(), coord[2]) && + currentListener.getActiveEdge() != null && + currentListener.getActiveEdge().getTo() != v && + currentListener.getActiveEdge().getFrom() != v) { + return v; + } + } + return null; + } + + /** + * Check if a and b is as close as EPSILON + * @param a The target + * @param b The value + * @return true if |a - b| < EPSILON false otherwise + */ + private boolean collide(double a, double b) { + return Math.abs(a - b) < EPSILON; + } + + /** + * Take a coordinate and check if it should snap to grid + * @param coords The coordinates to check for snapping + * @return the snapped coordinates + */ + public double[] snapToGrid(double[] coords) { + if (Math.abs(coords[0] - Math.round(coords[0])) < EPSILON) { + coords[0] = Math.round(coords[0]); + } + if (Math.abs(coords[1] - Math.round(coords[1])) < EPSILON) { + coords[1] = Math.round(coords[1]); + } + if (Math.abs(coords[2] - Math.round(coords[2])) < EPSILON) { + coords[2] = Math.round(coords[2]); + } + return coords; + } + + /** + * Make and register a new vertex + * @return The new Vertex + */ + private Vertex vertex() { + Vertex v = new Vertex("V" + vertexNum); + vertexNum++; + VertexFacade.getInstance().add(v); + + Notification n = new Notification(Notification.VERTEX_CREATED, v.getId()); + Notifier.getInstance().sendNotification(n); + + return v; + } + + /** + * Remove a vertex for good + * @param v Vertex to remove + */ + private void removeVertex(Vertex v) { + VertexFacade.getInstance().remove(v); + + Notification n = new Notification(Notification.VERTEX_DELETED, v.getId()); + Notifier.getInstance().sendNotification(n); + } + + /** + * Make and register a new edge + * @return The new Edge + */ + private Edge edge() { + Edge e = new Edge("E" + edgeNum); + edgeNum++; + EdgeFacade.getInstance().add(e); + + Notification n = new Notification(Notification.EDGE_CREATED, e.getId()); + Notifier.getInstance().sendNotification(n); + + return e; + } + + /** + * Make and register a new Surface + * @return The new Surface + */ + private Surface surface() { + Surface s = new Surface("S" + surfaceNum); + surfaceNum++; + SurfaceFacade.getInstance().add(s); + + Notification n = new Notification(Notification.SURFACE_CREATED, s.getId()); + Notifier.getInstance().sendNotification(n); + return s; + } + + /** * Return the current used GLEventListener * @return The current GLEventListener |