Print

Print


Commit in GeomConverter/src/org/lcsim/geometry/segmentation on MAIN
CartesianGridXY.java+102-101.2 -> 1.3
implement findCellContainingXYZ and a boundsCheck method

GeomConverter/src/org/lcsim/geometry/segmentation
CartesianGridXY.java 1.2 -> 1.3
diff -u -r1.2 -r1.3
--- CartesianGridXY.java	15 Sep 2009 19:55:31 -0000	1.2
+++ CartesianGridXY.java	21 Sep 2009 21:13:19 -0000	1.3
@@ -9,11 +9,15 @@
 import org.jdom.DataConversionException;
 import org.jdom.Element;
 import org.lcsim.detector.IDetectorElement;
+import org.lcsim.detector.IDetectorElementContainer;
 import org.lcsim.detector.identifier.ExpandedIdentifier;
 import org.lcsim.detector.identifier.IExpandedIdentifier;
 import org.lcsim.detector.identifier.IIdentifier;
 import org.lcsim.detector.identifier.IIdentifierHelper;
 import org.lcsim.detector.identifier.Identifier;
+import org.lcsim.detector.solids.Box;
+import org.lcsim.detector.solids.Inside;
+import org.lcsim.detector.solids.Trd;
 import org.lcsim.geometry.util.IDDescriptor;
 
 /**
@@ -87,10 +91,7 @@
 		
 		// Create an ExpandedIdentifier for the current id.
 		IExpandedIdentifier thisId = helper.unpack(currId);
-		
-		// Compute maximum number of neighbors.
-		//int nMax = (2*layerRange + 1)*(2*xRange + 1)*(2*yRange + 1) - 1;
-		
+				
 		// Create return array.
 		List<Long> neighbors = new ArrayList<Long>();
 		
@@ -139,13 +140,97 @@
 		}
 		return result;
 	}
-	
-	// TODO: Implement this method.  Need proper bounds checking first. 
+	 
 	public long findCellContainingXYZ(double x, double y, double z) 
 	{
-		if (true)
-			throw new RuntimeException("Not implemented");
-		return 0;
+		Hep3Vector pos = new BasicHep3Vector(x,y,z);
+		IDetectorElement de = 
+			getSubdetector().getDetectorElement().findDetectorElement(pos);
+		if (!de.getGeometry().getPhysicalVolume().isSensitive())
+		{
+			throw new RuntimeException("The volume " + de.getName() + " is not sensitive.");
+		}
+		Hep3Vector localPosition = de.getGeometry().transformGlobalToLocal(pos);
+		ExpandedIdentifier geomId = new ExpandedIdentifier(de.getExpandedIdentifier());
+		geomId.setValue(xIndex, getXBin(localPosition.x()));
+		geomId.setValue(yIndex, getYBin(localPosition.y()));
+		return getSubdetector().getDetectorElement().getIdentifierHelper().pack(geomId).getValue();
+	}
+	
+	// FIXME: Copied from GridXYZ.
+	public int getBin(double u, double gridSizeU)
+	{
+		double u0 = gridSizeU / 2;
+		int iu = (int) Math.floor((u - u0) / gridSizeU + 0.5);
+		return iu;
+	}
+
+	// FIXME: Copied from GridXYZ.
+	public int getXBin(double x)
+	{
+		return getBin(x, gridSizeX);
+	}
+
+	// FIXME: Copied from GridXYZ.
+	public int getYBin(double y)
+	{
+		return getBin(y, gridSizeY);
+	}
+	
+	public double computePosition(int binValue, double gridSize)
+	{
+		return (((double) binValue) + 0.5) * gridSize;
+	}
+		
+	public boolean boundsCheck(long rawId)
+	{
+		IIdentifier geomId = makeGeometryIdentifier(rawId);
+		IIdentifierHelper helper = getSubdetector().getDetectorElement().getIdentifierHelper();
+		IIdentifier id = new Identifier(rawId);
+		int xVal = helper.getValue(id, xIndex);
+		int yVal = helper.getValue(id, yIndex);
+		IDetectorElementContainer deSrch = getSubdetector().getDetectorElement().findDetectorElement(geomId);
+		if (deSrch == null || deSrch.size() == 0)
+		{
+			return false;
+		}
+		IDetectorElement de = deSrch.get(0);
+		double xPos = computePosition(xVal, gridSizeX);
+		double yPos = computePosition(yVal, gridSizeY);
+		if (de.getGeometry().getLogicalVolume().getSolid() instanceof Box)
+		{			
+			Box sensorBox = (Box)de.getGeometry().getLogicalVolume().getSolid();
+			// Check coordinate values against box bounds.
+			if (sensorBox.inside(new BasicHep3Vector(xPos, yPos, 0)) == Inside.INSIDE)
+			{
+				return true;
+			}
+			// TODO: Handle edge case with partial cells.  (How???)
+			else
+			{
+				return false;
+			}
+		}	
+		// TODO: Implement Trapezoid bounds check here.
+		else if (de.getGeometry().getLogicalVolume().getSolid() instanceof Trd)
+		{
+			Trd sensorTrd = (Trd)de.getGeometry().getLogicalVolume().getSolid();
+			// Check coordinate values against trd bounds.
+			// FIXME: Y goes to Z?  Need to double-check where this is being converted.
+			if (sensorTrd.inside(new BasicHep3Vector(xPos, 0, yPos)) == Inside.INSIDE)
+			{
+				return true;
+			}
+			// TODO: Handle edge case with partial cells.  (How???)
+			else
+			{
+				return false;
+			}
+		}
+		else
+		{
+			throw new RuntimeException("Don't know how to bounds check solid " + de.getGeometry().getLogicalVolume().getSolid().getName() + ".");
+		}
 	}
 	
 	public double[] getPosition()
@@ -254,5 +339,12 @@
 		localPosition[0] = localPosition[1] = localPosition[2] = 0.0;
 		computeLocalX();
 		computeLocalY();
-	}			
+	}
+	
+	private IIdentifier makeGeometryIdentifier(long id)
+	{
+		IExpandedIdentifier geomIdExp = detector.getDetectorElement().getIdentifierHelper().unpack(new Identifier(id), geomFields);
+		return detector.getDetectorElement().getIdentifierHelper().pack(geomIdExp);
+	}
+	
 }
\ No newline at end of file
CVSspam 0.2.8