[Bprocessor-commit] model/src/net/sourceforge/bprocessor/model Camera.java, 1.47, 1.48
Status: Pre-Alpha
Brought to you by:
henryml
|
From: Michael L. <he...@us...> - 2007-12-11 13:24:47
|
Update of /cvsroot/bprocessor/model/src/net/sourceforge/bprocessor/model In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv24571/src/net/sourceforge/bprocessor/model Modified Files: Camera.java Log Message: faster but not very good zoomOn function Index: Camera.java =================================================================== RCS file: /cvsroot/bprocessor/model/src/net/sourceforge/bprocessor/model/Camera.java,v retrieving revision 1.47 retrieving revision 1.48 diff -C2 -d -r1.47 -r1.48 *** Camera.java 13 Nov 2007 12:19:29 -0000 1.47 --- Camera.java 11 Dec 2007 13:24:44 -0000 1.48 *************** *** 477,510 **** /** ! * Moves the camera to a position where the entire model is visible. ! * @param aspect the acspect ratio of the view. ! * @param c the Collection of object to zoom on to. */ ! public void zoomOn(Collection<? extends Geometric> c, double aspect) { ! Set<Vertex> allVertices = new HashSet<Vertex>(); ! for (Geometric g : c) { ! if (g instanceof Space) { ! Space s = (Space)g; ! if (s.getEnvelope().isEmpty()) { ! if (s.getVertices().isEmpty()) { ! g.collect(); ! } else { ! allVertices.addAll(s.getVertices()); ! } ! } else { ! allVertices.addAll(Geometry.collect(s.getEnvelope())); } - } else { - allVertices.addAll(g.collect()); } } ! ! if (!allVertices.isEmpty()) { ! //Computing center and radius of the minimal bounding sphere ! Set<Vertex> notTested = new HashSet<Vertex>(allVertices); double radius = 0; Vertex p1 = null; Vertex p2 = null; ! Iterator it = allVertices.iterator(); while (it.hasNext()) { Vertex v = (Vertex) it.next(); --- 477,621 ---- /** ! * Represents a bounding box ! * */ ! 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); } ! } ! ! /** ! * Represents a bounding sphere ! * ! */ ! private static class BoundingSphere { ! private Vertex center; ! private double radius; ! ! /** ! * Constructs a bounding sphere ! * @param center center ! * @param radius radius ! */ ! public BoundingSphere(Vertex center, double radius) { ! this.center = center; ! this.radius = radius; ! } ! ! /** ! * Returns center ! * @return center ! */ ! public Vertex center() { ! return center; ! } ! ! /** ! * Returns radius ! * @return radius ! */ ! public double radius() { ! return radius; ! } ! ! /** ! * Computes a crude approximation of boundinsphere in linear time ! * @param vertices collection of vertices ! * @return bounding sphere ! */ ! public static BoundingSphere from(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 a better 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(); *************** *** 521,534 **** } if (p1 != null && p2 != null) { ! Vertex modelCenter = p1.minus(p2); modelCenter.scaleInPlace(1.0 / 2.0); modelCenter = modelCenter.add(p2); ! //expand sphere if needs ! it = allVertices.iterator(); while (it.hasNext()) { Vertex v = (Vertex)it.next(); Vertex dV = v.minus(modelCenter); double dist = dV.length(); - //if v is outside the sphere expand it to contain v. if (dist > radius) { radius = (dist + radius) / 2.0; --- 632,643 ---- } if (p1 != null && p2 != null) { ! modelCenter = p1.minus(p2); modelCenter.scaleInPlace(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; *************** *** 537,559 **** } } - - //Setting up camera - double halfFocal = this.getFocalwidth() / 2.0; - double halfFocalVerTan = Math.tan(Math.toRadians(halfFocal)); - double halfFocalHorTan = halfFocalVerTan * aspect; - double width = Math.min(halfFocalHorTan, halfFocalVerTan); - double cameraDistance = radius / width; - Vertex cameraPos = new Vertex(camera[0] - center[0], - camera[1] - center[1], - camera[2] - center[2]); - cameraPos.scaleInPlace(cameraDistance / cameraPos.length()); - cameraPos = cameraPos.add(modelCenter); - setCenter(new double[] {modelCenter.getX(), - modelCenter.getY(), - modelCenter.getZ()}); - setCamera(new double[] {cameraPos.getX(), - cameraPos.getY(), - cameraPos.getZ()}); } } } --- 646,697 ---- } } } + return new BoundingSphere(modelCenter, radius); + } + } + + /** + * Moves the camera to a position where the entire model is visible. + * @param aspect the acspect ratio of the view. + * @param c the Collection of object to zoom on to. + */ + public void zoomOn(Collection<? extends Geometric> c, double aspect) { + Set<Vertex> allVertices = new HashSet<Vertex>(); + for (Geometric g : c) { + if (g instanceof Space) { + Space s = (Space)g; + if (s.getEnvelope().isEmpty()) { + allVertices.addAll(s.collectInterior()); + } else { + allVertices.addAll(Geometry.collect(s.getEnvelope())); + } + } else { + allVertices.addAll(g.collect()); + } + } + + if (!allVertices.isEmpty()) { + + BoundingSphere sphere = BoundingSphere.from(allVertices); + double radius = sphere.radius; + Vertex modelCenter = sphere.center; + + //Setting up camera + double halfFocal = this.getFocalwidth() / 2.0; + double halfFocalVerTan = Math.tan(Math.toRadians(halfFocal)); + double halfFocalHorTan = halfFocalVerTan * aspect; + double width = Math.min(halfFocalHorTan, halfFocalVerTan); + double cameraDistance = radius / width; + Vertex cameraPos = new Vertex(camera[0] - center[0], + camera[1] - center[1], + camera[2] - center[2]); + cameraPos.scaleInPlace(cameraDistance / cameraPos.length()); + cameraPos = cameraPos.add(modelCenter); + setCenter(new double[] {modelCenter.getX(), + modelCenter.getY(), + modelCenter.getZ()}); + setCamera(new double[] {cameraPos.getX(), + cameraPos.getY(), + cameraPos.getZ()}); } } |