import java.util.List;
import java.util.ArrayList;
/**
* Created by IntelliJ IDEA.
* User: schalentier
* Date: 21.06.2007
* Time: 13:33:00
* To change this template use File | Settings | File Templates.
*/
public class BoundingTest {
private static final int NO_BOUNDINGS = 10000;
public class BoundingBox{
double x1, y1, z1;
double x2, y2, z2;
public BoundingBox(double x1, double y1, double z1, double x2, double y2, double z2) {
this.x1 = x1;
this.y1 = y1;
this.z1 = z1;
this.x2 = x2;
this.y2 = y2;
this.z2 = z2;
}
public boolean intersect( BoundingBox b ) {
if( b.x1>x2 ) return false;
if( b.y1>y2 ) return false;
if( b.z1>z2 ) return false;
if( b.x2<x1 ) return false;
if( b.y2<y1 ) return false;
if( b.z2<z1 ) return false;
return true;
}
}
public class BoundingSphere {
double x,y,z;
double r, rSquared;
public BoundingSphere(double x, double y, double z, double r) {
this.x = x;
this.y = y;
this.z = z;
this.r = r;
rSquared = r*r;
}
public boolean intersect( BoundingSphere s ) {
double distSquared = (s.x-x)*(s.x-x) + (s.y-y)*(s.y-y) + (s.z-z)*(s.z-z);
if( distSquared< rSquared + s.rSquared ) return true;
return false;
}
}
public long benchmarkBoxes() {
List<BoundingBox> boxes = new ArrayList<BoundingBox>();
for( int i=0;i< NO_BOUNDINGS; i++) {
boxes.add( new BoundingBox(Math.random(),Math.random(),Math.random(),Math.random(),Math.random(),Math.random()));
}
int numberOfIntersects = 0;
long time = System.nanoTime();
for( int i=0;i<NO_BOUNDINGS;i++ ) {
for( int j=0;j<NO_BOUNDINGS;j++ ) {
if( j==i ) continue;
if( boxes.get(i).intersect( boxes.get(j) ) ) numberOfIntersects++;
}
}
time = System.nanoTime() - time;
System.out.println("Intersecting Boxes "+numberOfIntersects);
System.out.println("Time "+time);
return time;
}
public long benchmarkSpheres() {
List<BoundingSphere> spheres = new ArrayList<BoundingSphere>();
for( int i=0;i< NO_BOUNDINGS; i++) {
spheres.add( new BoundingSphere(Math.random(),Math.random(),Math.random(),Math.random()));
}
int numberOfIntersects = 0;
long time = System.nanoTime();
for( int i=0;i<NO_BOUNDINGS;i++ ) {
for( int j=0;j<NO_BOUNDINGS;j++ ) {
if( j==i ) continue;
if( spheres.get(i).intersect( spheres.get(j) ) ) numberOfIntersects++;
}
}
time = System.nanoTime()-time;
System.out.println("Intersecting Spheres "+numberOfIntersects);
System.out.println("Time "+time);
return time;
}
public static void main(String[] args) {
double temp = 1;
// initialize vm
for( int i=0;i<10000000; i++) { temp*=temp; }
System.out.println("Starting benchmark...");
BoundingTest test = new BoundingTest();
long timeBoxes = test.benchmarkBoxes();
long timeSpheres = test.benchmarkSpheres();
System.out.println("Boxes/Spheres "+(double) timeBoxes/(double) timeSpheres);
}
}
-->
Starting benchmark...
Intersecting Boxes 1669276
Time 5829930147
Intersecting Spheres 60922124
Time 6948703558
Boxes/Spheres 0.8389953749412776