use of cbit.image.VCImageUncompressed in project vcell by virtualcell.
the class RayCaster method resampleGeometry.
public static Geometry resampleGeometry(GeometryThumbnailImageFactory geometryThumbnailImageFactory, Geometry origGeometry, ISize sampleSize) throws ImageException, PropertyVetoException, GeometryException, ExpressionException {
if (origGeometry.getDimension() < 3) {
throw new GeometryException("Presently, the Raycaster resampling works only for 3d geometries.");
}
GeometrySpec origGeometrySpec = origGeometry.getGeometrySpec();
VCImage origSubvolumeImage = origGeometrySpec.getSampledImage().getCurrentValue();
if (origSubvolumeImage == null) {
throw new GeometryException("original geometry does not have a sampled image");
}
VCImage resampledSubvolumeImage = RayCaster.sampleGeometry(origGeometry, sampleSize, false);
//
// Check if resampling failed:
// if not the same number of pixelClasses (between original geometry and resampled)
// if the subvolume handles are the same
//
boolean bSameSubvolumes = true;
if (origSubvolumeImage.getNumPixelClasses() != resampledSubvolumeImage.getNumPixelClasses()) {
bSameSubvolumes = false;
}
//
for (VCPixelClass origPixelClass : origSubvolumeImage.getPixelClasses()) {
VCPixelClass resampledPixelClass = resampledSubvolumeImage.getPixelClassFromPixelValue(origPixelClass.getPixel());
if (resampledPixelClass == null) {
bSameSubvolumes = false;
break;
}
}
//
if (!bSameSubvolumes) {
StringBuffer message = new StringBuffer();
message.append("\n\nexisting geometry:\n");
for (SubVolume oldSubvolume : origGeometrySpec.getSubVolumes()) {
long count = origSubvolumeImage.countPixelsByValue((byte) oldSubvolume.getHandle());
message.append("subvolume('" + oldSubvolume.getName() + "',handle=" + oldSubvolume.getHandle() + ",numPixels=" + count + " of " + origSubvolumeImage.getNumXYZ() + "\n");
}
message.append("\n\nnew resampled handle VCImage:\n");
for (VCPixelClass newPixelClass : resampledSubvolumeImage.getPixelClasses()) {
long count = resampledSubvolumeImage.countPixelsByValue((byte) newPixelClass.getPixel());
message.append("pixelClass('" + newPixelClass.getPixelClassName() + "',pixelValue=" + newPixelClass.getPixel() + ",numPixels=" + count + " of " + resampledSubvolumeImage.getNumXYZ() + ")\n");
}
throw new GeometryException("original Geometry had " + origSubvolumeImage.getNumPixelClasses() + " subvolumes, resampled Geometry found " + resampledSubvolumeImage.getNumPixelClasses() + " subvolumes " + message.toString());
}
//
// Create new VCImage that will form the basis for a new image-based geometry.
//
VCImage newVCImage = null;
if (origGeometrySpec.getImage() != null) {
//
// was an image-based geometry - try to make new VCImage similar to the original (same pixelClass names and pixel values).
// the goal is to make identical geometries if the sample size is same as the original image size.
//
// create a new VCImage with same image pixel values (not subvolume handles) and pixel class names as original image.
//
byte[] newVCImagePixels = new byte[sampleSize.getXYZ()];
byte[] resampledSubvolumePixels = resampledSubvolumeImage.getPixels();
for (int i = 0; i < sampleSize.getXYZ(); i++) {
int subvolumeHandle = resampledSubvolumePixels[i];
ImageSubVolume imageSubvolume = (ImageSubVolume) origGeometrySpec.getSubVolume(subvolumeHandle);
newVCImagePixels[i] = (byte) imageSubvolume.getPixelValue();
}
newVCImage = new VCImageUncompressed(null, newVCImagePixels, origGeometry.getExtent(), sampleSize.getX(), sampleSize.getY(), sampleSize.getZ());
newVCImage.setName(origGeometrySpec.getImage().getName());
ArrayList<VCPixelClass> newPixelClasses = new ArrayList<VCPixelClass>();
for (VCPixelClass origPixelClass : origGeometrySpec.getImage().getPixelClasses()) {
SubVolume origSubvolume = origGeometrySpec.getImageSubVolumeFromPixelValue(origPixelClass.getPixel());
newPixelClasses.add(new VCPixelClass(null, origSubvolume.getName(), origPixelClass.getPixel()));
}
newVCImage.setPixelClasses(newPixelClasses.toArray(new VCPixelClass[newPixelClasses.size()]));
} else {
//
// was an analytic geometry - create a new image-based geometry
//
// create a new VCImage with image pixel values and pixelClass names equal to corresponding subvolumes from original geometry
// make new subvolume names equal to old subvolume names.
//
byte[] newVCImageSubvolumePixels = resampledSubvolumeImage.getPixels().clone();
newVCImage = new VCImageUncompressed(null, newVCImageSubvolumePixels, origGeometry.getExtent(), sampleSize.getX(), sampleSize.getY(), sampleSize.getZ());
ArrayList<VCPixelClass> newPixelClasses = new ArrayList<VCPixelClass>();
for (SubVolume origSubvolume : origGeometrySpec.getSubVolumes()) {
// note: newVCImage already has subvolume handle pixels.
newPixelClasses.add(new VCPixelClass(null, origSubvolume.getName(), origSubvolume.getHandle()));
}
newVCImage.setPixelClasses(newPixelClasses.toArray(new VCPixelClass[newPixelClasses.size()]));
}
//
// construct the new geometry with the sampled VCImage.
//
Geometry newGeometry = new Geometry(origGeometry.getName(), newVCImage);
newGeometry.getGeometrySpec().setExtent(origGeometry.getExtent());
newGeometry.getGeometrySpec().setOrigin(origGeometry.getOrigin());
newGeometry.setDescription(origGeometry.getDescription());
newGeometry.getGeometrySurfaceDescription().setFilterCutoffFrequency(origGeometry.getGeometrySurfaceDescription().getFilterCutoffFrequency());
newGeometry.precomputeAll(geometryThumbnailImageFactory, true, true);
return newGeometry;
}
use of cbit.image.VCImageUncompressed in project vcell by virtualcell.
the class RayCaster method createGeometry.
public static Geometry createGeometry(GeometryThumbnailImageFactory geometryThumbnailImageFactory, SurfaceCollection surfaceCollection, Origin origin, Extent extent, ISize sampleSize) throws ImageException, PropertyVetoException, GeometryException, ExpressionException {
int numX = sampleSize.getX();
int numY = sampleSize.getY();
int numZ = sampleSize.getZ();
VolumeSamples volumeSamples = volumeSampleSurface(surfaceCollection, sampleSize, origin, extent, false);
// for each mask bit, find union of masks which contain that bit ... iterate until no change.
HashSet<Long> uniqueMasks = volumeSamples.getUniqueMasks();
ArrayList<Long> consensusMaskArray = new ArrayList<Long>(uniqueMasks);
boolean bChanged = true;
while (bChanged) {
bChanged = false;
for (int i = 0; i < consensusMaskArray.size(); i++) {
for (int j = i + 1; j < consensusMaskArray.size(); j++) {
if ((((long) consensusMaskArray.get(i)) & ((long) consensusMaskArray.get(j))) != 0L && (((long) consensusMaskArray.get(i)) != ((long) consensusMaskArray.get(j)))) {
long merged = consensusMaskArray.get(i) | consensusMaskArray.get(j);
if ((((merged << 1) & merged) == 0L) && (((merged >> 1) & merged) == 0L)) {
System.out.println("++++++++++++++++++++++++++++++++ merged " + Long.toBinaryString(consensusMaskArray.get(i)) + " with " + Long.toBinaryString(consensusMaskArray.get(j)) + " to get " + Long.toBinaryString(merged));
consensusMaskArray.set(i, merged);
consensusMaskArray.set(j, merged);
bChanged = true;
}
}
}
}
}
HashSet<Long> consensusSet = new HashSet<Long>(consensusMaskArray);
int[] pixels = new int[numX * numY * numZ];
int pixelValue = 1;
for (long mask : consensusSet) {
System.out.println("++++++++++++++++++ +++++++++++++++ ++++++++++++++++++ consensus mask: " + Long.toBinaryString(mask));
// use connectsAcrossSurface() to avoid short-circuiting a membrane.
for (int i = 0; i < volumeSamples.getNumXYZ(); i++) {
if ((volumeSamples.getMask(i) & mask) != 0) {
// if (incidentSurfaceMasks[i] == mask){
pixels[i] = pixelValue;
}
}
pixelValue++;
}
VCImageUncompressed vcImage = new VCImageUncompressed(null, pixels, extent, numX, numY, numZ);
Geometry geometry = new Geometry("newGeometry", vcImage);
geometry.getGeometrySpec().setExtent(extent);
geometry.getGeometrySpec().setOrigin(origin);
geometry.precomputeAll(geometryThumbnailImageFactory, true, true);
return geometry;
}
use of cbit.image.VCImageUncompressed in project vcell by virtualcell.
the class GeometrySpec method createSampledImage.
/**
* This method was created in VisualAge.
* @return cbit.image.VCImage
*/
public VCImage createSampledImage(ISize sampleSize) throws GeometryException, ImageException, ExpressionException {
VCellThreadChecker.checkCpuIntensiveInvocation();
byte[] handles = new byte[sampleSize.getX() * sampleSize.getY() * sampleSize.getZ()];
for (int i = 0; i < handles.length; i++) {
handles[i] = -1;
}
//
if (dimension == 0) {
handles[0] = 0;
}
//
if (vcImage != null) {
//
// make lookup table of pixel values ----> handles
//
byte[] handleLookupTable = new byte[256];
for (int i = 0; i < fieldSubVolumes.length; i++) {
SubVolume sv = fieldSubVolumes[i];
if (sv instanceof ImageSubVolume) {
ImageSubVolume isv = (ImageSubVolume) sv;
handleLookupTable[isv.getPixelValue()] = (byte) isv.getHandle();
}
}
byte[] vciPixels = vcImage.getPixels();
if (vcImage.getNumX() == sampleSize.getX() && vcImage.getNumY() == sampleSize.getY() && vcImage.getNumZ() == sampleSize.getZ()) {
//
for (int j = 0; j < vciPixels.length; j++) {
handles[j] = handleLookupTable[0xff & (int) vciPixels[j]];
}
} else {
//
// image and sample same size, just translate pixels to handles
//
double deltaX = (sampleSize.getX() > 1) ? (getExtent().getX() / (sampleSize.getX() - 1)) : 0;
double deltaY = (sampleSize.getY() > 1) ? (getExtent().getY() / (sampleSize.getY() - 1)) : 0;
double deltaZ = (sampleSize.getZ() > 1) ? (getExtent().getZ() / (sampleSize.getZ() - 1)) : 0;
double ox = getOrigin().getX();
double oy = getOrigin().getY();
double oz = getOrigin().getZ();
int handleIndex = 0;
for (int k = 0; k < sampleSize.getZ(); k++) {
double coordZ = oz + deltaZ * k;
for (int j = 0; j < sampleSize.getY(); j++) {
double coordY = oy + deltaY * j;
for (int i = 0; i < sampleSize.getX(); i++) {
double coordX = ox + deltaX * i;
int imageIndex = getImageIndex(coordX, coordY, coordZ);
handles[handleIndex++] = handleLookupTable[0xff & (int) vciPixels[imageIndex]];
}
}
}
}
}
//
// go through AnalyticSubVolumes to overlay over ImageSubVolumes
//
int displayIndex = 0;
// double deltaX = (sampleSize.getX()>1)?(getExtent().getX()/(sampleSize.getX()-1)):0;
// double deltaY = (sampleSize.getY()>1)?(getExtent().getY()/(sampleSize.getY()-1)):0;
// double deltaZ = (sampleSize.getZ()>1)?(getExtent().getZ()/(sampleSize.getZ()-1)):0;
double ox = getOrigin().getX();
double oy = getOrigin().getY();
double oz = getOrigin().getZ();
int numX = sampleSize.getX();
int numY = sampleSize.getY();
int numZ = sampleSize.getZ();
//
// rebind x,y,z,t and set Index values
//
Enumeration<SubVolume> enumASV = getAnalyticOrCSGSubVolumes();
while (enumASV.hasMoreElements()) {
SubVolume sv = enumASV.nextElement();
if (sv instanceof AnalyticSubVolume) {
((AnalyticSubVolume) sv).rebind();
}
}
if (getNumAnalyticOrCSGSubVolumes() > 0) {
for (int k = 0; k < sampleSize.getZ(); k++) {
double unit_z = (numZ > 1) ? ((double) k) / (numZ - 1) : 0.5;
double coordZ = (numZ > 1) ? oz + extent.getZ() * unit_z : 0;
for (int j = 0; j < sampleSize.getY(); j++) {
double unit_y = (numY > 1) ? ((double) j) / (numY - 1) : 0.5;
double coordY = (numY > 1) ? oy + extent.getY() * unit_y : 0;
for (int i = 0; i < sampleSize.getX(); i++) {
double unit_x = (numX > 1) ? ((double) i) / (numX - 1) : 0.5;
double coordX = ox + extent.getX() * unit_x;
SubVolume subVolume = getAnalyticOrCSGSubVolume(coordX, coordY, coordZ);
if (subVolume != null) {
handles[displayIndex] = (byte) subVolume.getHandle();
} else {
AnalyticSubVolume backgroundSubVol = new AnalyticSubVolume(getFreeSubVolumeName("background"), new Expression(1.0));
try {
this.addAnalyticSubVolumeOrCSGObject(backgroundSubVol, false);
} catch (PropertyVetoException e) {
e.printStackTrace(System.out);
throw new GeometryException("Issue adding a background subvolume to geometry : " + e.getMessage());
}
handles[displayIndex] = (byte) backgroundSubVol.getHandle();
}
displayIndex++;
}
}
}
}
return new VCImageUncompressed(null, handles, getExtent(), sampleSize.getX(), sampleSize.getY(), sampleSize.getZ());
}
use of cbit.image.VCImageUncompressed in project vcell by virtualcell.
the class FunctionRangeGenerator method main.
public static void main(String[] args) {
try {
Expression exp = new Expression("a+log(b)+c");
VarStatistics[] varStats = new VarStatistics[3];
varStats[0] = new VarStatistics("a", new double[] { 1.0, 2.0, 3.0 }, new double[] { 1.0, 2.0, 3.0 });
varStats[1] = new VarStatistics("b", new double[] { 1.0, 2.0, 3.0 }, new double[] { 1.0, 2.0, 3.0 });
varStats[2] = new VarStatistics("c", new double[] { 1.0, 2.0, 3.0 }, new double[] { 1.0, 2.0, 3.0 });
double[] times = new double[] { 0.0, 1.0, 2.0 };
Extent extent = new Extent(5, 5, 5);
Origin origin = new Origin(0, 0, 0);
// int numSamplesPerDim = 10;
byte[] pixels = new byte[3 * 3 * 3];
BitSet indomain = new BitSet(pixels.length);
for (int i = 0; i < pixels.length; i++) {
indomain.set(i);
}
VCImageUncompressed vcImage = new VCImageUncompressed(null, pixels, extent, 3, 3, 3);
RegionImage regionImage = new RegionImage(vcImage, 3, extent, origin, RegionImage.NO_SMOOTHING);
CartesianMesh mesh = CartesianMesh.createSimpleCartesianMesh(origin, extent, new ISize(regionImage.getNumX(), regionImage.getNumY(), regionImage.getNumZ()), regionImage, true);
FunctionStatistics results = FunctionRangeGenerator.getFunctionStatistics(exp, varStats, times, mesh, indomain, VariableType.VOLUME);
System.out.println(results.getDefaultDatasetRange().toString());
} catch (Exception e) {
e.printStackTrace();
}
}
use of cbit.image.VCImageUncompressed in project vcell by virtualcell.
the class FRAPStudy method getPSFFieldData.
public static FieldDataIdentifierSpec getPSFFieldData(LocalWorkspace localWorkspace) {
// create ROI image
short[] psfFieldData = null;
psfFieldData = new short[9];
psfFieldData[4] = (short) 1;
// create field data
int NumTimePoints = 1;
// 8 rois integrated into 1 image
int NumChannels = 1;
short[][][] pixData = new short[NumTimePoints][NumChannels][1];
pixData[0][0] = psfFieldData;
// get extental data id
ExternalDataIdentifier newPsfExtDataID = FRAPStudy.createNewExternalDataInfo(localWorkspace, FRAPStudy.PSF_DATA_NAME).getExternalDataIdentifier();
CartesianMesh cartesianMesh;
try {
Origin origin = new Origin(0, 0, 0);
Extent ext = new Extent(1, 1, 1);
ISize isize = new ISize(3, 3, 1);
cartesianMesh = CartesianMesh.createSimpleCartesianMesh(origin, ext, isize, new RegionImage(new VCImageUncompressed(null, new byte[isize.getXYZ()], ext, isize.getX(), isize.getY(), isize.getZ()), 0, null, null, RegionImage.NO_SMOOTHING));
FieldDataFileOperationSpec fdos = new FieldDataFileOperationSpec();
fdos.opType = FieldDataFileOperationSpec.FDOS_ADD;
fdos.cartesianMesh = cartesianMesh;
fdos.shortSpecData = pixData;
fdos.specEDI = newPsfExtDataID;
fdos.varNames = new String[] { "psfVar" };
fdos.owner = LocalWorkspace.getDefaultOwner();
fdos.times = new double[] { 0.0 };
fdos.variableTypes = new VariableType[] { VariableType.VOLUME };
fdos.origin = origin;
fdos.extent = ext;
fdos.isize = isize;
localWorkspace.getDataSetControllerImpl().fieldDataFileOperation(fdos);
FieldFunctionArguments psfFieldFunc = new FieldFunctionArguments(PSF_DATA_NAME, "psfVar", new Expression(0.0), VariableType.VOLUME);
FieldDataIdentifierSpec fdis = new FieldDataIdentifierSpec(psfFieldFunc, newPsfExtDataID);
return fdis;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
Aggregations