GeomConverter/src/org/lcsim/detector/solids
diff -u -r1.2 -r1.3
--- Polycone.java 10 Oct 2007 22:44:11 -0000 1.2
+++ Polycone.java 13 Feb 2008 23:10:57 -0000 1.3
@@ -3,12 +3,15 @@
import hep.physics.vec.Hep3Vector;
import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
import java.util.List;
/**
* A port of Polycone that only maintains data members.
* No solid functionality for now.
*
+ * Modified by Cosmin Deaconu <[log in to unmask]>
* @author Jeremy McCormick <[log in to unmask]>
*/
public class Polycone
@@ -23,6 +26,17 @@
{
this.zplanes.add(zplane);
}
+
+ //keep ZPlanes sorted in Z... otherwise things will get messed up
+ Collections.sort(this.zplanes, new Comparator<ZPlane>(){
+
+ public int compare(ZPlane a, ZPlane b){
+ if (a.z<b.z) return -1;
+ if (a.z>b.z) return 1;
+ return 0;
+ }
+ });
+
}
public List<ZPlane> getZPlanes()
@@ -68,14 +82,76 @@
}
public double getCubicVolume()
- {
- if (true) throw new RuntimeException("not implemented");
- return 0;
+ {
+ if (zplanes.size()<2) throw new RuntimeException("Too few ZPlanes in PolyCone");
+
+ double vol = 0;
+
+ double z1 = zplanes.get(0).z;
+ double a1 = zplanes.get(0).rmin;
+ double b1 = zplanes.get(0).rmax;
+
+ for (int i = 1; i<zplanes.size(); i++) {
+
+ double z2 = zplanes.get(i).z;
+ double a2 = zplanes.get(i).rmin;
+ double b2 = zplanes.get(i).rmax;
+
+ //we force z2 to be bigger than z1 by sorting at the beginning
+ double dv = Math.PI/(3.0)*(z2-z1)*(b2*b2 + b1*b2 + b1*2 - a2*a2 - a2*a1 - a1*a1);
+
+ vol+=dv;
+ z1=z2;
+ a1=a2;
+ b1=b2;
+ }
+
+ return vol;
}
public Inside inside(Hep3Vector position)
{
- if (true) throw new RuntimeException("not implemented");
- return null;
- }
+ if (zplanes.size()<2) throw new RuntimeException("Too few ZPlanes in PolyCone");
+
+ double r = Math.sqrt(position.x()*position.x() + position.y()*position.y());
+ double z = position.z();
+
+ for (int i = 1; i<zplanes.size(); i++) {
+
+ ZPlane p1 = zplanes.get(i-1);
+ ZPlane p2 = zplanes.get(i);
+
+ //see if it's at the left or right edge
+ if ((i==1 && z == p1.z && r<=p1.rmax && r>=p1.rmin) ||
+ (i==zplanes.size()-1 && z == p2.z && r<=p2.rmax && r>=p2.rmin))
+ return Inside.SURFACE;
+
+ //this means we're in the right section...
+ if ((z <= p2.z && z >= p1.z)) {
+
+ double b = f(position.z(),OUTER,p1,p2);
+ double a = f(position.z(),INNER,p1,p2);
+
+ if (r==b || r==a) return Inside.SURFACE;
+ if (r<b && r>a) return Inside.INSIDE;
+ }
+ }
+
+ return Inside.OUTSIDE;
+ }
+
+ private static boolean INNER = true;
+ private static boolean OUTER = false;
+ //Calculates the radius at any z of either the outer or inner part of the segment
+ private double f(double z, boolean whichR, ZPlane p1, ZPlane p2){
+
+ double z1 = p1.z;
+ double z2 = p2.z;
+ double x1 = whichR ? p1.rmin : p1.rmax;
+ double x2 = whichR ? p2.rmin : p2.rmax;
+
+ double m = (x2-x1)/(z2-z1);
+ return x1 + m*(z-z1);
+ }
+
}
\ No newline at end of file
GeomConverter/test/org/lcsim/detector/solids
diff -N PolyconeTest.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ PolyconeTest.java 13 Feb 2008 23:10:57 -0000 1.1
@@ -0,0 +1,142 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+package org.lcsim.detector.solids;
+
+import hep.physics.vec.BasicHep3Vector;
+import hep.physics.vec.Hep3Vector;
+import java.util.ArrayList;
+import java.util.List;
+import junit.framework.TestCase;
+import org.lcsim.detector.solids.Polycone.ZPlane;
+
+/**
+ *
+ * @author cozzy
+ */
+public class PolyconeTest extends TestCase {
+
+ public PolyconeTest(String testName) {
+ super(testName);
+ }
+
+
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+
+
+
+ }
+
+
+ /**
+ * Test of getCubicVolume method, of class Polycone.
+ *
+ */
+ public void testGetCubicVolume() {
+ ZPlane p1 = new ZPlane(1,2,0);
+ ZPlane p2 = new ZPlane(1,2,10);
+ ZPlane p3 = new ZPlane(2,3,15);
+
+ List<ZPlane> l1 = new ArrayList<ZPlane>();
+ List<ZPlane> l2 = new ArrayList<ZPlane>();
+ List<ZPlane> l3 = new ArrayList<ZPlane>();
+
+ l1.add(p1);
+ l1.add(p2);
+
+ l2.addAll(l1);
+ l2.add(p3);
+
+ l3.add(p2);
+ l3.add(p3);
+
+ Polycone pc1 = new Polycone("Simple Tube",l1);
+ Polycone pc2 = new Polycone("Tube + bevel",l2);
+ Polycone pc3 = new Polycone("Bevel",l3);
+
+ Tube t = new Tube("Tube using Tube Class",1,2,5);
+
+ double tv1 = t.getCubicVolume();
+ double v1 = pc1.getCubicVolume();
+ double v2 = pc2.getCubicVolume();
+ double v3 = pc3.getCubicVolume();
+
+ double realv3 = Math.PI*20;
+ assertEquals(tv1,v1);
+ assertEquals(v1+v3,v2);
+
+ double TOLERANCE = 0.000001;
+ double diff = Math.abs(v3 - realv3);
+ assertTrue( diff < TOLERANCE) ;
+
+ }
+
+
+ public void testSorted(){
+
+ ZPlane z1 = new ZPlane(2,3,4);
+ ZPlane z2 = new ZPlane(3,4,5);
+ ZPlane z3 = new ZPlane(2,4,-5);
+
+ List<ZPlane> l1 = new ArrayList<ZPlane>();
+ List<ZPlane> l2 = new ArrayList<ZPlane>();
+
+ l1.add(z1);
+ l1.add(z2);
+ l1.add(z3);
+
+ l2.add(z3);
+ l2.add(z1);
+ l2.add(z2);
+
+ Polycone p1 = new Polycone("unsorted",l1);
+ Polycone p2 = new Polycone("sorted",l2);
+
+ assertEquals(p1.zplanes.get(0).z,-5.0);
+ assertEquals(p2.zplanes.get(0).z,-5.0);
+ assertEquals(p1.zplanes.get(1).z,4.0);
+ assertEquals(p2.zplanes.get(1).z,4.0);
+ assertEquals(p1.zplanes.get(2).z,5.0);
+ assertEquals(p2.zplanes.get(2).z,5.0);
+
+ }
+
+ public void testInside(){
+
+ ZPlane p1 = new ZPlane(1,2,0);
+ ZPlane p2 = new ZPlane(1,2,10);
+ ZPlane p3 = new ZPlane(2,3,15);
+
+ List<ZPlane> l1 = new ArrayList<ZPlane>();
+ l1.add(p1);
+ l1.add(p2);
+ l1.add(p3);
+
+ Polycone pc = new Polycone("test",l1);
+
+ assertEquals(Inside.OUTSIDE,pc.inside(new BasicHep3Vector(3,0,5)));
+ assertEquals(Inside.OUTSIDE,pc.inside(new BasicHep3Vector(0,3,5)));
+ assertEquals(Inside.OUTSIDE,pc.inside(new BasicHep3Vector(1.5,1.5,5)));
+ assertEquals(Inside.OUTSIDE,pc.inside(new BasicHep3Vector(0,0,1)));
+ assertEquals(Inside.OUTSIDE,pc.inside(new BasicHep3Vector(1.5,0,13)));
+
+ assertEquals(Inside.INSIDE,pc.inside(new BasicHep3Vector(0,2.5,14)));
+ assertEquals(Inside.INSIDE,pc.inside(new BasicHep3Vector(1.1,0,5)));
+ assertEquals(Inside.INSIDE,pc.inside(new BasicHep3Vector(1.1,1.1,5)));
+ assertEquals(Inside.INSIDE,pc.inside(new BasicHep3Vector(1.8,0,13)));
+
+ assertEquals(Inside.SURFACE,pc.inside(new BasicHep3Vector(1.1,1.1,0)));
+ assertEquals(Inside.SURFACE,pc.inside(new BasicHep3Vector(2,0,4)));
+ assertEquals(Inside.SURFACE,pc.inside(new BasicHep3Vector(1,0,4)));
+ assertEquals(Inside.SURFACE,pc.inside(new BasicHep3Vector(2.5,0,12.5)));
+ assertEquals(Inside.SURFACE,pc.inside(new BasicHep3Vector(1.5,0,12.5)));
+
+ }
+
+}
+