Thread: [Bprocessor-commit] model/src/net/sourceforge/bprocessor/model Camera.java, 1.50, 1.51
Status: Pre-Alpha
Brought to you by:
henryml
|
From: Michael L. <he...@us...> - 2007-12-12 09:34:14
|
Update of /cvsroot/bprocessor/model/src/net/sourceforge/bprocessor/model In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv3767/src/net/sourceforge/bprocessor/model Modified Files: Camera.java Log Message: clean up of bounding sphere code Index: Camera.java =================================================================== RCS file: /cvsroot/bprocessor/model/src/net/sourceforge/bprocessor/model/Camera.java,v retrieving revision 1.50 retrieving revision 1.51 diff -C2 -d -r1.50 -r1.51 *** Camera.java 11 Dec 2007 14:49:02 -0000 1.50 --- Camera.java 12 Dec 2007 09:34:16 -0000 1.51 *************** *** 481,548 **** */ private static class BoundingBox { ! private Vertex min; ! private Vertex max; ! ! /** ! * Constructs the bounding box ! * @param min minimum ! * @param max maximum ! */ ! public BoundingBox(Vertex min, Vertex max) { ! this.min = min; ! this.max = max; ! } ! ! /** ! * Returns minimum ! * @return minimum ! */ ! public Vertex min() { ! return min; ! } /** ! * Returns maximum ! * @return maximum */ ! public Vertex max() { ! return max; } /** ! * Computes bounding box * @param vertices collection of vertices - * @return bounding box */ ! public static BoundingBox from(Collection<Vertex> vertices) { ! double xmin = Double.MAX_VALUE; ! double xmax = Double.MIN_VALUE; ! double ymin = Double.MAX_VALUE; ! double ymax = Double.MIN_VALUE; ! double zmin = Double.MAX_VALUE; ! double zmax = Double.MIN_VALUE; for (Vertex current : vertices) { ! if (current.getX() < xmin) { ! xmin = current.getX(); } ! if (current.getX() > xmax) { ! xmax = current.getX(); } ! if (current.getY() < ymin) { ! ymin = current.getY(); } ! if (current.getY() > ymax) { ! ymax = current.getY(); } ! if (current.getZ() < zmin) { ! zmin = current.getZ(); } ! if (current.getZ() > zmax) { ! zmax = current.getZ(); } } ! Vertex min = new Vertex(xmin, ymin, zmin); ! Vertex max = new Vertex(xmax, ymax, zmax); ! return new BoundingBox(min, max); } } --- 481,583 ---- */ private static class BoundingBox { ! private Vertex xmin; ! private Vertex xmax; ! private Vertex ymin; ! private Vertex ymax; ! private Vertex zmin; ! private Vertex zmax; /** ! * Constructs bounding box for ! * @param vertices Collection */ ! public BoundingBox(Collection<Vertex> vertices) { ! compute(vertices); } /** ! * Compute boudning box * @param vertices collection of vertices */ ! public void compute(Collection<Vertex> vertices) { ! xmin = new Vertex(Double.POSITIVE_INFINITY, ! Double.POSITIVE_INFINITY, ! Double.POSITIVE_INFINITY); ! xmax = new Vertex(Double.NEGATIVE_INFINITY, ! Double.NEGATIVE_INFINITY, ! Double.NEGATIVE_INFINITY); ! ymin = xmin; ! ymax = xmax; ! zmin = xmin; ! zmax = xmax; ! for (Vertex current : vertices) { ! if (current.x < xmin.x) { ! xmin = current; } ! if (current.x > xmax.x) { ! xmax = current; } ! if (current.y < ymin.y) { ! ymin = current; } ! if (current.y > ymax.y) { ! ymax = current; } ! if (current.z < zmin.z) { ! zmin = current; } ! if (current.z > zmax.z) { ! zmax = current; } } ! } ! ! /** ! * ! * @return vertex ! */ ! public Vertex xmin() { ! return xmin; ! } ! ! /** ! * ! * @return vertex ! */ ! public Vertex xmax() { ! return xmax; ! } ! ! /** ! * ! * @return vertex ! */ ! public Vertex ymin() { ! return ymin; ! } ! ! /** ! * ! * @return vertex ! */ ! public Vertex ymax() { ! return ymax; ! } ! ! /** ! * ! * @return vertex ! */ ! public Vertex zmin() { ! return zmin; ! } ! ! /** ! * ! * @return vertex ! */ ! public Vertex zmax() { ! return zmax; } } *************** *** 557,560 **** --- 592,603 ---- /** + * Constructs a bounding sphere for specified vertices + * @param vertices collection of vertices + */ + public BoundingSphere(Collection<Vertex> vertices) { + compute(vertices); + } + + /** * Constructs a bounding sphere * @param center center *************** *** 583,619 **** /** ! * Computes an approximation of boundinsphere in linear time ! * Algorithm by Jack Ritter in "Graphics Gems" * @param vertices collection of vertices - * @return bounding sphere */ ! public static BoundingSphere from(Collection<Vertex> vertices) { ! Vertex xmin = new Vertex(Double.MAX_VALUE, Double.MAX_VALUE, Double.MAX_VALUE); ! Vertex xmax = new Vertex(-10000, -10000, -10000); ! Vertex ymin = xmin; ! Vertex ymax = xmax; ! Vertex zmin = xmin; ! Vertex zmax = xmax; - for (Vertex current : vertices) { - if (current.x < xmin.x) { - xmin = current; - } - if (current.x > xmax.x) { - xmax = current; - } - if (current.y < ymin.y) { - ymin = current; - } - if (current.y > ymax.y) { - ymax = current; - } - if (current.z < zmin.z) { - zmin = current; - } - if (current.z > zmax.z) { - zmax = current; - } - } Vertex xspan = xmax.minus(xmin); Vertex yspan = ymax.minus(ymin); --- 626,643 ---- /** ! * Compute a bounding sphere for specified vertices ! * using Jack Ritters algorithm from Graphics Gems. * @param vertices collection of vertices */ ! public void compute(Collection<Vertex> vertices) { ! BoundingBox box = new BoundingBox(vertices); ! ! Vertex xmin = box.xmin(); ! Vertex xmax = box.xmax(); ! Vertex ymin = box.ymin(); ! Vertex ymax = box.ymax(); ! Vertex zmin = box.zmin(); ! Vertex zmax = box.zmax(); Vertex xspan = xmax.minus(xmin); Vertex yspan = ymax.minus(ymin); *************** *** 636,641 **** } ! Vertex center = v1.add(v2).scale(0.5); ! double radius = 0.1; for (Vertex current : vertices) { --- 660,665 ---- } ! center = v1.add(v2).scale(0.5); ! radius = dia / 2; for (Vertex current : vertices) { *************** *** 649,723 **** } if (radius < 0.00001) { ! radius = 0.5; ! } ! return new BoundingSphere(center, radius); ! } ! ! /** ! * Computes a crude approximation of boundinsphere in linear time ! * @param vertices collection of vertices ! * @return bounding sphere ! */ ! public static BoundingSphere from1(Collection<Vertex> vertices) { ! BoundingBox box = BoundingBox.from(vertices); ! Vertex v1 = box.min; ! Vertex v2 = box.max; ! Vertex center = v1.add(v2).scale(0.5); ! double radius = 0.0; ! for (Vertex current : vertices) { ! Vertex d = current.minus(center); ! if (d.length() > radius) { ! radius = d.length(); ! } ! } ! if (radius < 0.00001) { ! radius = 0.5; ! } ! return new BoundingSphere(center, radius); ! } ! ! ! /** ! * Computes an approximation of boundinsphere in quadratic time ! * @param vertices collection of vertices ! * @return bounding sphere ! */ ! public static BoundingSphere from0(Collection<Vertex> vertices) { ! Set<Vertex> notTested = new HashSet<Vertex>(vertices); ! double radius = 0; ! Vertex modelCenter = new Vertex(0, 0, 0); ! Vertex p1 = null; ! Vertex p2 = null; ! Iterator it = vertices.iterator(); ! while (it.hasNext()) { ! Vertex v = (Vertex) it.next(); ! notTested.remove(v); ! Iterator testIt = notTested.iterator(); ! while (testIt.hasNext()) { ! Vertex u = (Vertex)testIt.next(); ! if ((v.minus(u).length() / 2.0) > radius) { ! p1 = v; ! p2 = u; ! radius = v.minus(u).length() / 2.0; ! } ! } ! } ! if (p1 != null && p2 != null) { ! modelCenter = p1.minus(p2); ! modelCenter.scaleIt(1.0 / 2.0); ! modelCenter = modelCenter.add(p2); ! it = vertices.iterator(); ! while (it.hasNext()) { ! Vertex v = (Vertex)it.next(); ! Vertex dV = v.minus(modelCenter); ! double dist = dV.length(); ! if (dist > radius) { ! radius = (dist + radius) / 2.0; ! dV.scaleIt((dist - radius) / dist); ! modelCenter = modelCenter.add(dV); ! } ! } } - return new BoundingSphere(modelCenter, radius); } } --- 673,678 ---- } if (radius < 0.00001) { ! radius = 0.1; } } } *************** *** 745,749 **** if (!allVertices.isEmpty()) { ! BoundingSphere sphere = BoundingSphere.from(allVertices); double radius = sphere.radius; Vertex modelCenter = sphere.center; --- 700,704 ---- if (!allVertices.isEmpty()) { ! BoundingSphere sphere = new BoundingSphere(allVertices); double radius = sphere.radius; Vertex modelCenter = sphere.center; |