From 0e80eb500d944f8ad1f3a9e2d296d9a4cbcd7e25 Mon Sep 17 00:00:00 2001 From: adam Date: Sun, 29 Jun 2008 16:46:53 -0700 Subject: [PATCH 1/1] checkpoint darcs-hash:20080629234653-5007d-e0780858ef69096c44796930607cb3634af16fd0.gz --- src/edu/berkeley/qfat/Mesh.java | 2 + src/edu/berkeley/qfat/MeshViewer.java | 52 ++++++++++++++++++++----- src/edu/berkeley/qfat/geom/Matrix.java | 65 +++++++++++++++++++++++++++++-- src/edu/berkeley/qfat/geom/Point.java | 14 ------- src/edu/berkeley/qfat/geom/Segment.java | 4 +- src/edu/berkeley/qfat/geom/Vec.java | 2 + 6 files changed, 112 insertions(+), 27 deletions(-) diff --git a/src/edu/berkeley/qfat/Mesh.java b/src/edu/berkeley/qfat/Mesh.java index ecee9e5..fd73795 100644 --- a/src/edu/berkeley/qfat/Mesh.java +++ b/src/edu/berkeley/qfat/Mesh.java @@ -110,6 +110,7 @@ public class Mesh implements Iterable { error += olderror; } + /* public Vertex hack(GL gl, Point mouse) { double dist = Double.MAX_VALUE; Vertex cur = null; @@ -123,6 +124,7 @@ public class Mesh implements Iterable { } return cur; } + */ public float averageTriangleArea() { int count = 0; diff --git a/src/edu/berkeley/qfat/MeshViewer.java b/src/edu/berkeley/qfat/MeshViewer.java index c943565..b4d958a 100644 --- a/src/edu/berkeley/qfat/MeshViewer.java +++ b/src/edu/berkeley/qfat/MeshViewer.java @@ -96,8 +96,17 @@ public class MeshViewer implements GLEventListener, MouseListener, MouseMotionLi public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } - public void mousePressed(MouseEvent e) { } - public void mouseReleased(MouseEvent e) { } + public void mousePressed(MouseEvent e) { + clickPoint = getMouse(); + clickClosest = closest == null ? null : closest.getPoint(); + } + public void mouseReleased(MouseEvent e) { + clickPoint = null; + clickClosest = null; + } + + Point clickPoint = null; + Point clickClosest = null; int mousex; int mousey; @@ -113,8 +122,19 @@ public class MeshViewer implements GLEventListener, MouseListener, MouseMotionLi float angley = 0; public void mouseDragged(MouseEvent e) { if (shift) { + /* tx += (mousex - e.getX())/(float)20; ty += (mousey - e.getY())/(float)20; + */ + if (closest != null && lastGL != null && projection != null && clickClosest != null) { + synchronized(safeTriangles) { + Vec d1 = projection.inverse().times(getMouse()).minus(projection.inverse().times(clickPoint)); + Vec delta = d1.plus(clickClosest).minus(closest.getPoint()); + //System.out.println(delta + " " + closest.getPoint()); + System.out.println(getMouse().minus(clickPoint)); + closest.move(Matrix.translate(delta), true); + } + } } else { anglex -= mousex - e.getX(); angley += mousey - e.getY(); @@ -165,6 +185,8 @@ public class MeshViewer implements GLEventListener, MouseListener, MouseMotionLi display(gld); } + private GL lastGL = null; + public int temps; public int accepts; public int vertss; @@ -189,6 +211,7 @@ public class MeshViewer implements GLEventListener, MouseListener, MouseMotionLi g.fillRect(140, 65+top, vertss, 10); GL gl = drawable.getGL(); + lastGL = gl; GLU glu = new GLU(); gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); gl.glPointSize(5.0f); @@ -271,11 +294,13 @@ public class MeshViewer implements GLEventListener, MouseListener, MouseMotionLi gl.glBegin(gl.GL_POINTS); closest.getPoint().glVertex(gl); gl.glEnd(); + /* Mesh.Vertex v2 = closest.hack(gl, getMouse()); gl.glBegin(GL.GL_LINES); closest.getPoint().glVertex(gl); if (v2 != null) v2.getPoint().glVertex(gl); gl.glEnd(); + */ } gl.glFlush(); @@ -290,7 +315,8 @@ public class MeshViewer implements GLEventListener, MouseListener, MouseMotionLi gl.glColor3f(0,0,0); gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); double dist = Double.MAX_VALUE; - closest = null; + if (clickPoint==null) closest = null; + projection = Matrix.getProjectionMatrix(gl); synchronized(safeTriangles) { for(Mesh.T t : safeTriangles) t.glTriangle(gl, null); @@ -302,7 +328,7 @@ public class MeshViewer implements GLEventListener, MouseListener, MouseMotionLi gl.glEnd(); gl.glFlush(); - Point projected = p.glProject(gl); + Point projected = projection.times(p); int x = (int)projected.x; int y = (int)projected.y; gl.glReadPixels(x-1, y-1, 3, 3, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, buf); @@ -316,9 +342,11 @@ public class MeshViewer implements GLEventListener, MouseListener, MouseMotionLi p.glVertex(gl); gl.glEnd(); y = glcanvas.getHeight()-y; - if (closest==null || (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey) < dist) { - dist = (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey); - closest = v; + if (clickPoint==null) { + if (closest==null || (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey) < dist) { + dist = (x-mousex)*(x-mousex)+(y-mousey)*(y-mousey); + closest = v; + } } } } @@ -346,6 +374,7 @@ public class MeshViewer implements GLEventListener, MouseListener, MouseMotionLi */ } Mesh.Vertex closest = null; + Matrix projection = null; // I copied this method without changes from the mentioned base class. // It extracts the data in the selection buffer and writes it on the console. @@ -417,7 +446,8 @@ public class MeshViewer implements GLEventListener, MouseListener, MouseMotionLi green -= .12f; blue -= .15f; - if (triangles) switch(t.color/*class*/) { + /* + if (triangles) switch(t.color) { case 0: gl.glColor4f((float)0.25, (float)0.25, (float)0.75, (float)0.3); break; case 1: gl.glColor4f((float)0.25, (float)0.75, (float)0.25, (float)0.3); break; case 2: gl.glColor4f((float)0.75, (float)0.25, (float)0.25, (float)0.3); break; @@ -426,8 +456,12 @@ public class MeshViewer implements GLEventListener, MouseListener, MouseMotionLi case 5: gl.glColor4f((float)0.25, (float)0.75, (float)0.75, (float)0.3); break; case 6: gl.glColor4f((float)0.75, (float)0.25, (float)0.75, (float)0.3); break; } + */ - gl.glColor4f((float)0.25, (float)0.25, (float)0.75, (float)0.3); + gl.glColor4f((float)(0.25+(0.05*t.color)), + (float)(0.25+(0.05*t.color)), + (float)(0.75+(0.05*t.color)), + (float)0.3); //if (t.v1().visible && t.v2().visible && t.v3().visible) continue; /* diff --git a/src/edu/berkeley/qfat/geom/Matrix.java b/src/edu/berkeley/qfat/geom/Matrix.java index 31fe6e0..e52b895 100644 --- a/src/edu/berkeley/qfat/geom/Matrix.java +++ b/src/edu/berkeley/qfat/geom/Matrix.java @@ -1,4 +1,5 @@ package edu.berkeley.qfat.geom; +import javax.media.opengl.*; // FEATURE: precompute/cache determinant? @@ -24,6 +25,14 @@ public class Matrix { /** the identity matrix */ public static final Matrix NEGATIVE_ONE = new Matrix(-1,0,0,0,0,-1,0,0,0,0,-1,0,0,0,0,1); + public Matrix(double a, double b, double c, double d, double e, double f, double g, + double h, double i, double j, double k, double l, double m, double n, double o, double p) { + this((float)a, (float)b, (float)c, (float)d, + (float)e, (float)f, (float)g, (float)h, + (float)i, (float)j, (float)k, (float)l, + (float)m, (float)n, (float)o, (float)p); + } + public Matrix(float a, float b, float c, float d, float e, float f, float g, float h, float i, float j, float k, float l, float m, float n, float o, float p) { this.a = a; this.b = b; this.c = c; this.d = d; this.e = e; this.f = f; this.g = g; this.h = h; this.i = i; @@ -110,9 +119,11 @@ public class Matrix { /** discards bottom row */ public Point times(Point p) { // discards bottom row - return new Point(a*p.x + b*p.y + c*p.z + d, - e*p.x + f*p.y + g*p.z + h, - i*p.x + j*p.y + k*p.z + l); + double x = a*p.x + b*p.y + c*p.z + d; + double y = e*p.x + f*p.y + g*p.z + h; + double z = i*p.x + j*p.y + k*p.z + l; + double q = m*p.x + n*p.y + o*p.z + this.p; + return new Point(x/q, y/q, z/q); } /** discards bottom row */ @@ -301,4 +312,52 @@ public class Matrix { Float.floatToIntBits(o) ^ Float.floatToIntBits(p); } + + public static Matrix getProjectionMatrix(GL gl) { + int view[] = new int[4]; + double mvmatrix[] = new double[16]; + double projmatrix[] = new double[16]; + gl.glGetIntegerv(GL.GL_VIEWPORT, view, 0); + gl.glGetDoublev(GL.GL_MODELVIEW_MATRIX, mvmatrix, 0); + gl.glGetDoublev(GL.GL_PROJECTION_MATRIX, projmatrix, 0); + Matrix m = new Matrix(mvmatrix[0], + mvmatrix[4], + mvmatrix[8], + mvmatrix[12], + mvmatrix[1], + mvmatrix[5], + mvmatrix[9], + mvmatrix[13], + mvmatrix[2], + mvmatrix[6], + mvmatrix[10], + mvmatrix[14], + mvmatrix[3], + mvmatrix[7], + mvmatrix[11], + mvmatrix[15]); + Matrix p = new Matrix(projmatrix[0], + projmatrix[4], + projmatrix[8], + projmatrix[12], + projmatrix[1], + projmatrix[5], + projmatrix[9], + projmatrix[13], + projmatrix[2], + projmatrix[6], + projmatrix[10], + projmatrix[14], + projmatrix[3], + projmatrix[7], + projmatrix[11], + projmatrix[15]); + Matrix z = + new Matrix(0.5*view[2], 0, 0, view[0]+view[2]*0.5, + 0, 0.5*view[3], 0, view[1]+view[3]*0.5, + 0, 0, 0.5, 0.5, + 0, 0, 0, 1); + return z.times(p).times(m); + } + } diff --git a/src/edu/berkeley/qfat/geom/Point.java b/src/edu/berkeley/qfat/geom/Point.java index 2de47a6..f46cc7e 100644 --- a/src/edu/berkeley/qfat/geom/Point.java +++ b/src/edu/berkeley/qfat/geom/Point.java @@ -28,19 +28,5 @@ public final class Point implements HasBoundingBox { public float getMinY() { return y; } public float getMaxZ() { return z; } public float getMinZ() { return z; } - - public Point glProject(GL gl) { - Point p = this; - int viewport[] = new int[4]; - double mvmatrix[] = new double[16]; - double projmatrix[] = new double[16]; - double wcoord[] = new double[4]; - gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0); - gl.glGetDoublev(GL.GL_MODELVIEW_MATRIX, mvmatrix, 0); - gl.glGetDoublev(GL.GL_PROJECTION_MATRIX, projmatrix, 0); - GLU glu = new GLU(); - glu.gluProject(p.x, p.y, p.z, mvmatrix, 0, projmatrix, 0, viewport, 0, wcoord, 0); - return new Point(wcoord[0], wcoord[1], 0); - } } diff --git a/src/edu/berkeley/qfat/geom/Segment.java b/src/edu/berkeley/qfat/geom/Segment.java index 7e6ebd6..5f46051 100644 --- a/src/edu/berkeley/qfat/geom/Segment.java +++ b/src/edu/berkeley/qfat/geom/Segment.java @@ -25,6 +25,8 @@ public class Segment implements HasBoundingBox { public float getMaxZ() { return Math.max(p1.z, p2.z); } public float getMinZ() { return Math.min(p1.z, p2.z); } - public Segment glProject(GL gl) { return new Segment(p1.glProject(gl), p2.glProject(gl)); } + public double distance(Point p) { + throw new RuntimeException("not yet implemented"); + } } diff --git a/src/edu/berkeley/qfat/geom/Vec.java b/src/edu/berkeley/qfat/geom/Vec.java index c601b15..bc10a34 100644 --- a/src/edu/berkeley/qfat/geom/Vec.java +++ b/src/edu/berkeley/qfat/geom/Vec.java @@ -1,5 +1,6 @@ package edu.berkeley.qfat.geom; import javax.media.opengl.*; +import javax.media.opengl.glu.*; /** vector in 3-space; immutable */ public final class Vec { @@ -9,6 +10,7 @@ public final class Vec { public Vec(Point p1, Point p2) { this(p2.x-p1.x, p2.y-p1.y, p2.z-p1.z); } public Vec cross(Vec v) { return new Vec(y*v.z-z*v.y, z*v.x-x*v.z, x*v.y-y*v.x); } public Vec plus(Vec v) { return new Vec(x+v.x, y+v.y, z+v.z); } + public Point plus(Point p) { return p.plus(this); } public Vec minus(Vec v) { return new Vec(x-v.x, y-v.y, z-v.z); } public Vec norm() { return mag()==0 ? this : div(mag()); } public float mag() { return (float)Math.sqrt(x*x+y*y+z*z); } -- 1.7.10.4