Thread: [Bprocessor-commit] gl/src/net/sourceforge/bprocessor/gl/tool RotationTool.java, 1.21, 1.22
Status: Pre-Alpha
Brought to you by:
henryml
|
From: rimestad <rim...@us...> - 2006-12-13 17:42:53
|
Update of /cvsroot/bprocessor/gl/src/net/sourceforge/bprocessor/gl/tool In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv16686/src/net/sourceforge/bprocessor/gl/tool Modified Files: RotationTool.java Log Message: made some major changes to the rotation tool Index: RotationTool.java =================================================================== RCS file: /cvsroot/bprocessor/gl/src/net/sourceforge/bprocessor/gl/tool/RotationTool.java,v retrieving revision 1.21 retrieving revision 1.22 diff -C2 -d -r1.21 -r1.22 *** RotationTool.java 5 Dec 2006 09:07:25 -0000 1.21 --- RotationTool.java 13 Dec 2006 17:42:45 -0000 1.22 *************** *** 7,17 **** --- 7,26 ---- package net.sourceforge.bprocessor.gl.tool; + import net.java.games.jogl.GL; + import net.java.games.jogl.GLDrawable; + import net.java.games.jogl.GLU; import net.sourceforge.bprocessor.gl.GLView; + import net.sourceforge.bprocessor.gl.model.GlObject; import net.sourceforge.bprocessor.gl.model.Intersection; + import net.sourceforge.bprocessor.gui.GUI; + import net.sourceforge.bprocessor.model.Camera; import net.sourceforge.bprocessor.model.Edge; import net.sourceforge.bprocessor.model.Geometry; import net.sourceforge.bprocessor.model.Line; + import net.sourceforge.bprocessor.model.Plane; + import net.sourceforge.bprocessor.model.Project; import net.sourceforge.bprocessor.model.Selection; + import net.sourceforge.bprocessor.model.Surface; import net.sourceforge.bprocessor.model.Vertex; *************** *** 36,45 **** /** The vertexes to rotate */ private Set vertices = new HashSet(); ! /***/ ! private Intersection p1; ! /***/ ! private Intersection p2; ! /***/ ! private Line l; /** * The Constructor --- 45,65 ---- /** The vertexes to rotate */ private Set vertices = new HashSet(); ! /** The first pressed point */ ! private Vertex p1; ! /** The secondly pressed point*/ ! private Vertex p2; ! /** The starting rotation point*/ ! private Vertex from; ! /** the previous vertex when moving the mouse */ ! private Vertex previous; ! /** The plane whose normal is rotated aboud */ ! private Plane onto; ! /** The gl object drawing the rotation circle */ ! private GlProtractor pro; ! /** The total rotation angle */ ! private double totalAngle; ! /** The set of object not to hit */ ! private Set nothits = new HashSet(); ! /** * The Constructor *************** *** 58,61 **** --- 78,124 ---- process(e); } + + /** + * @param e MouseEvent + */ + protected void moved(MouseEvent e) { + current = findIntersection(e); + if (current != null) { + updateFeedback(); + if (previous != null) { + //find the angle to rotate + Vertex to = onto.projection(current.vertex()); + double angle = (previous.minus(p1).angle(to.minus(p1))); + if ((previous.minus(p1).cross(to.minus(p1))).angle(p2.minus(p1)) > 0) { + angle *= -1; + } + if (angle != Double.NaN) { + totalAngle = (totalAngle + angle) % (2 * Math.PI); + rotate(angle); + } + previous = onto.projection(current.vertex()); + } + } + } + + /** + * Invoked when a mouse button has been released on a component. + * @param e The MouseEvent + */ + protected void released(MouseEvent e) { + + } + + /** + * Update the length field + */ + protected void updateLength() { + double angle = 0; + if (p1 != null && p2 != null && from != null && current != null) { + angle = Math.toDegrees(totalAngle); + setLength(angle / 1000); + pro.setAngle((int)angle); + } + } /** *************** *** 71,110 **** */ private void process(MouseEvent e) { ! if (p1 == null) { ! p1 = current; ! start = p1; ! active = true; ! } else if (p2 == null) { ! p2 = current; ! start = p2; ! active = true; } else { ! if (start == p2) { ! //finish tool ! current = findIntersection(e); ! start = current; active = true; ! } else { current = findIntersection(e); ! Vertex v1 = new Edge(p1.vertex(), start.vertex()).getDirection(); ! Vertex v2 = new Edge(p1.vertex(), current.vertex()).getDirection(); ! double angle = v1.angle(v2); ! vertices = Selection.primary().collect(); ! Iterator iter = vertices.iterator(); ! while (iter.hasNext()) { ! Vertex v = (Vertex) iter.next(); ! Geometry.rotate(angle, l.getDirection().getX(), ! l.getDirection().getY(), l.getDirection().getZ(), v, l.getOrigin()); ! } ! clear(); active = false; } } ! updateFeedback(); } /** ! * bla ! * */ private void updateFeedback() { --- 134,220 ---- */ private void process(MouseEvent e) { ! if (Selection.primary().isEmpty()) { ! GUI.getInstance().alert("There is no selection to rotate"); } else { ! vertices = Selection.primary().collect(); ! if (p1 == null) { ! if (current.type() == Intersection.EDGE) { ! p1 = current.vertex(); ! p2 = p1.add(((Edge)current.object()).getDirection()); ! initDisk(); ! } else if (current.type() == Intersection.SURFACE) { ! p1 = current.vertex(); ! p2 = p1.add(((Surface)(current.object())).normal()); ! initDisk(); ! } else { ! p1 = current.vertex(); ! start = current; ! active = true; ! totalAngle = 0; ! } ! } else if (p2 == null) { ! p2 = current.vertex(); ! start = null; ! initDisk(); active = true; ! } else if (from == null) { current = findIntersection(e); ! from = onto.projection(current.vertex()); ! previous = from; ! pro.setStart(from); ! active = false; ! nothits = getAffected(vertices); ! } else { ! finish(); active = false; } + updateFeedback(); } ! } ! ! /** ! * Initialize the gldisk ! */ ! private void initDisk() { ! Vertex v = p2.minus(p1); ! v.normalize(); ! onto = new Plane(v.getX(), v.getY(), v.getZ(), p1); ! pro = new GlProtractor(onto, p1); ! glv.getView().addGlObjects3D(pro); ! } ! ! /** ! * Finish off the tool ! */ ! private void finish() { ! Project.getInstance().checkpoint(); ! Project.getInstance().changed(vertices); ! active = false; ! clear(); } /** ! * Escape ! */ ! public void escape() { ! rotate(-totalAngle); ! cleanUp(); ! } ! ! /** ! * Apply current tool with specified length value ! * @param length The lengthfield value ! */ ! public void apply(double length) { ! int sign = 1; ! if (totalAngle < 0) { ! sign = -1; ! } ! rotate(sign * (Math.toRadians(length * 1000) - Math.abs(totalAngle))); ! finish(); ! } ! ! /** ! * Update the feedback */ private void updateFeedback() { *************** *** 112,131 **** List edges = new LinkedList(); if (p1 != null) { ! points.add(p1.vertex()); if (p2 != null) { ! if (l == null) { ! l = new Line(p1.vertex(), p2.vertex().minus(p1.vertex())); ! l.setActive(true); ! } edges.add(l); ! Edge e = new Edge(p1.vertex(), start.vertex()); ! e.setStrippled(true); ! edges.add(e); ! e = new Edge(p1.vertex(), current.vertex()); ! e.setStrippled(true); ! edges.add(e); } } ! excluded(edges); points(points); feedback(edges); --- 222,238 ---- List edges = new LinkedList(); if (p1 != null) { ! points.add(p1); if (p2 != null) { ! Line l = new Line(p1, p2.minus(p1)); ! l.setActive(true); edges.add(l); ! edges.add(onto); } } ! List temp = new LinkedList(); ! temp.addAll(nothits); ! temp.addAll(edges); ! temp.addAll(points); ! excluded(temp); points(points); feedback(edges); *************** *** 133,145 **** updateConstructors(); } ! ! /** ! * Invoked when a mouse button has been released on a component. ! * @param e The MouseEvent ! */ ! protected void released(MouseEvent e) { ! ! } ! /** * Remove tool specific things at tool change --- 240,244 ---- updateConstructors(); } ! /** * Remove tool specific things at tool change *************** *** 156,159 **** --- 255,259 ---- private void clear() { vertices.clear(); + totalAngle = 0; p1 = null; p2 = null; *************** *** 161,198 **** current = null; incident = null; ! l = null; } ! /** ! * @param e MouseEvent */ ! protected void moved(MouseEvent e) { ! current = findIntersection(e); ! if (current != null) { ! updateFeedback(); } } /** ! * Update the length field */ ! protected void updateLength() { ! Vertex v1 = null; ! Vertex v2 = null; ! if (p1 != null && p2 != null) { ! if (current != null) { ! if (start != null) { ! v1 = new Edge(p1.vertex(), start.vertex()).getDirection(); ! v2 = new Edge(p1.vertex(), current.vertex()).getDirection(); ! } else if (incident != null) { ! v1 = new Edge(p1.vertex(), incident.vertex()).getDirection(); ! v2 = new Edge(p1.vertex(), current.vertex()).getDirection(); ! } } } ! if (v1 != null && v2 != null) { ! setLength(v1.angle(v2)); ! } else { ! glv.setLengthValue(""); } } --- 261,426 ---- current = null; incident = null; ! from = null; ! onto = null; ! glv.getView().removeGlObjects3D(pro); ! pro = null; ! previous = null; ! nothits.clear(); } ! /** ! * Do the rotation of the vertices ! * @param angle the angle to rotate in degrees */ ! private void rotate(double angle) { ! if (angle != Double.NaN) { ! Iterator iter = vertices.iterator(); ! while (iter.hasNext()) { ! Vertex v = (Vertex)iter.next(); ! Geometry.rotate(angle, onto.getDoublev()[Plane.X], ! onto.getDoublev()[Plane.Y], onto.getDoublev()[Plane.Z], v, p1); ! } ! } else { ! log.error("Angle were " + angle); } } /** ! * A GlObject that draw a protactor on the gl */ ! class GlProtractor implements GlObject { ! /** The location for the protractor */ ! private Plane plane; ! /** The input rotation angle */ ! private int angle; ! /** The center of the protractor */ ! private Vertex origin; ! /** The vertex the rotation starts from */ ! private Vertex start; ! /** The preset angles to intersect with */ ! private LinkedList edges; ! /** The magnification factor */ ! private double factor; ! ! /** ! * The construtor for the protractor ! * @param p The location for the protractor ! * @param origin The center for the protractor ! */ ! public GlProtractor(Plane p, Vertex origin) { ! angle = 0; ! plane = p; ! this.origin = origin; ! edges = new LinkedList(); ! } ! ! /** ! * Setter of the angle of rotation ! * @param angle The rotation angle to visualize ! */ ! public void setAngle(int angle) { ! this.angle = angle; ! } ! ! /** ! * Setter for the start vertex ! * @param start the first vertex in the rotation ! */ ! public void setStart(Vertex start) { ! this.start = start; ! double i = 0; ! double increment = Math.PI / 12; ! while (i < Math.PI) { ! Vertex to = start.copy(); ! Vertex n = plane.normal(); ! Geometry.rotate(i, n.getX(), n.getY(), n.getZ(), to, origin); ! Edge e = new Edge(origin, to); ! edges.add(new Line(origin, e.getDirection())); ! i += increment; } } ! ! /** ! * @inheritDoc ! */ ! public void draw(GLDrawable gld) { ! Camera cam = Project.getInstance().getCurrentCamera(); ! Vertex camorig = new Vertex(cam.getCamera()); ! double dist = camorig.distance(plane.origin()); ! factor = dist / 20; ! GL gl = gld.getGL(); ! GLU glu = gld.getGLU(); ! // translate ! Vertex z = plane.normal(); ! z.normalize(); ! Vertex y = new Vertex(0, 1, 0); ! if (y.minus(z).length() < 0.00000001) { ! y = new Vertex(1, 0, 0); ! } ! ! /*gl.glColor3d(0, 0, 0); ! gl.glLineWidth(2); ! Iterator iter = edges.iterator(); ! while (iter.hasNext()) { ! Line l = (Line) iter.next(); ! Vertex from = l.getPointAt(-5); ! Vertex to = l.getPointAt(5); ! gl.glBegin(GL.GL_LINE_STRIP); ! gl.glVertex3d(from.getX(), from.getY(), from.getZ()); ! gl.glVertex3d(to.getX(), to.getY(), to.getZ()); ! gl.glEnd(); ! } ! */ ! // place the axis right ! if (start != null) { ! y = start.minus(origin); ! y.normalize(); ! } ! Vertex x = y.cross(z); ! y = z.cross(x); ! double[] m = new double[]{x.getX(), x.getY(), x.getZ(), 0, ! y.getX(), y.getY(), y.getZ(), 0, ! z.getX(), z.getY(), z.getZ(), 0, ! origin.getX(), origin.getY(), origin.getZ(), 1}; ! gl.glPushMatrix(); ! gl.glMultMatrixd(m); ! //draw ! ! ! ! gl.glColor4fv(new float[] {0.5f, 0.5f, 0.5f, 0.6f}); ! int sign = 1; ! if (angle < 0) { ! sign = -1; ! } ! glu.gluPartialDisk( ! glu.gluNewQuadric(), 0, 2 * factor, 360, 1, 0, sign * (360 - Math.abs(angle))); ! gl.glColor4fv(new float[] {0.0f, 0.0f, 1.0f, 1.0f}); ! glu.gluPartialDisk(glu.gluNewQuadric(), 0, 2.2 * factor, 360, 1, 0, -angle); ! //restore matrix ! gl.glPopMatrix(); ! } ! ! /** ! * @inheritDoc ! */ ! public Vertex intersection(Edge e) { ! Vertex i = plane.intersection(e); ! Iterator iter = edges.iterator(); ! double p1 = -2.5 * factor; ! double p2 = 2.5 * factor; ! while (iter.hasNext()) { ! Line l = (Line) iter.next(); ! Edge tmp = new Edge(l.getPointAt(p1), l.getPointAt(p2)); ! Edge con = e.intersection(tmp); ! if (con.getLength() < 0.05) { ! if (tmp.coincides(con.getFrom())) { ! i = con.getFrom(); ! } else if (tmp.coincides(con.getTo())) { ! i = con.getTo(); ! } ! } ! } ! return i; } } |