use of org.vcell.vis.vcell.MembraneElement in project vcell by virtualcell.
the class CartesianMeshFileReader method readCartesianMesh.
private CartesianMesh readCartesianMesh(CommentStringTokenizer tokens, final MembraneMeshMetrics membraneMeshMetrics, final SubdomainInfo subdomainInfo) throws MathException {
//
// clear previous contents
//
MembraneElement[] membraneElements = null;
String version = null;
MeshRegionInfo meshRegionInfo = null;
ISize size = null;
Vect3D extent = null;
Vect3D origin = null;
ContourElement[] contourElements = null;
//
// read new stuff
//
String token = null;
token = tokens.nextToken();
if (token.equalsIgnoreCase(VCML.Version)) {
//
// read version number
//
token = tokens.nextToken();
version = token;
token = tokens.nextToken();
}
if (token.equalsIgnoreCase(VCML.CartesianMesh)) {
token = tokens.nextToken();
} else {
throw new MathFormatException("unexpected token " + token + " expecting " + VCML.CartesianMesh);
}
//
// only Version 1.1 and later supports membrane connectivity (as of 8/30/2000)
//
boolean bConnectivity = false;
if (version.equals(VERSION_1_1) || version.equals(VERSION_1_2)) {
bConnectivity = true;
}
//
// only Version 1.2 and later supports Regions
//
boolean bRegions = false;
if (version.equals(VERSION_1_2)) {
bRegions = true;
meshRegionInfo = new MeshRegionInfo();
}
if (!token.equalsIgnoreCase(VCML.BeginBlock)) {
throw new MathFormatException("unexpected token " + token + " expecting " + VCML.BeginBlock);
}
while (tokens.hasMoreTokens()) {
token = tokens.nextToken();
if (token.equalsIgnoreCase(VCML.EndBlock)) {
break;
}
if (token.equalsIgnoreCase(VCML.Size)) {
int sx, sy, sz;
try {
token = tokens.nextToken();
sx = Integer.valueOf(token).intValue();
token = tokens.nextToken();
sy = Integer.valueOf(token).intValue();
token = tokens.nextToken();
sz = Integer.valueOf(token).intValue();
} catch (NumberFormatException e) {
throw new MathFormatException("expected: " + VCML.Size + " # # #");
}
size = new ISize(sx, sy, sz);
continue;
}
if (token.equalsIgnoreCase(VCML.Extent)) {
double ex, ey, ez;
try {
token = tokens.nextToken();
ex = Double.valueOf(token).doubleValue();
token = tokens.nextToken();
ey = Double.valueOf(token).doubleValue();
token = tokens.nextToken();
ez = Double.valueOf(token).doubleValue();
} catch (NumberFormatException e) {
throw new MathFormatException("expected: " + VCML.Extent + " # # #");
}
extent = new Vect3D(ex, ey, ez);
continue;
}
if (token.equalsIgnoreCase(VCML.Origin)) {
double ox, oy, oz;
try {
token = tokens.nextToken();
ox = Double.valueOf(token).doubleValue();
token = tokens.nextToken();
oy = Double.valueOf(token).doubleValue();
token = tokens.nextToken();
oz = Double.valueOf(token).doubleValue();
} catch (NumberFormatException e) {
throw new MathFormatException("expected: " + VCML.Origin + " # # #");
}
origin = new Vect3D(ox, oy, oz);
continue;
}
//
if (token.equalsIgnoreCase(VCML.VolumeRegionsMapSubvolume)) {
token = tokens.nextToken();
if (!token.equalsIgnoreCase(VCML.BeginBlock)) {
throw new MathFormatException("unexpected token " + token + " expecting " + VCML.BeginBlock);
}
token = tokens.nextToken();
int numVolumeRegions = 0;
try {
numVolumeRegions = Integer.valueOf(token).intValue();
} catch (NumberFormatException e) {
throw new MathFormatException("unexpected token " + token + " expecting the VolumeRegionsMapSubvolume list length");
}
int checkCount = 0;
while (tokens.hasMoreTokens()) {
token = tokens.nextToken();
if (token.equalsIgnoreCase(VCML.EndBlock)) {
break;
}
try {
int volRegionID = Integer.valueOf(token).intValue();
token = tokens.nextToken();
int subvolumeID = Integer.valueOf(token).intValue();
token = tokens.nextToken();
double volume = Double.valueOf(token).doubleValue();
String subdomainName = null;
if (subdomainInfo != null) {
subdomainName = subdomainInfo.getCompartmentSubdomainName(subvolumeID);
}
meshRegionInfo.mapVolumeRegionToSubvolume(volRegionID, subvolumeID, volume, subdomainName);
} catch (NumberFormatException e) {
throw new MathFormatException("expected: # # #");
}
checkCount += 1;
}
if (checkCount != numVolumeRegions) {
throw new MathFormatException("CartesianMesh.read->VolumeRegionsMapSubvolume: read " + checkCount + " VolRegions but was expecting " + numVolumeRegions);
}
continue;
}
if (token.equalsIgnoreCase(VCML.MembraneRegionsMapVolumeRegion)) {
token = tokens.nextToken();
if (!token.equalsIgnoreCase(VCML.BeginBlock)) {
throw new MathFormatException("unexpected token " + token + " expecting " + VCML.BeginBlock);
}
token = tokens.nextToken();
int numMembraneRegions = 0;
try {
numMembraneRegions = Integer.valueOf(token).intValue();
} catch (NumberFormatException e) {
throw new MathFormatException("unexpected token " + token + " expecting the MembraneRegionsMapVolumeRegion list length");
}
int checkCount = 0;
while (tokens.hasMoreTokens()) {
token = tokens.nextToken();
if (token.equalsIgnoreCase(VCML.EndBlock)) {
break;
}
try {
int memRegionID = Integer.valueOf(token).intValue();
token = tokens.nextToken();
int volRegionIn = Integer.valueOf(token).intValue();
token = tokens.nextToken();
int volRegionOut = Integer.valueOf(token).intValue();
token = tokens.nextToken();
double surface = Double.valueOf(token).doubleValue();
meshRegionInfo.mapMembraneRegionToVolumeRegion(memRegionID, volRegionIn, volRegionOut, surface);
} catch (NumberFormatException e) {
throw new MathFormatException("expected: # # #");
}
checkCount += 1;
}
if (checkCount != numMembraneRegions) {
throw new MathFormatException("CartesianMesh.read->MembraneRegionsMapVolumeRegion: read " + checkCount + " MembraneRegions but was expecting " + numMembraneRegions);
}
continue;
}
if (token.equalsIgnoreCase(VCML.VolumeElementsMapVolumeRegion)) {
token = tokens.nextToken();
if (!token.equalsIgnoreCase(VCML.BeginBlock)) {
throw new MathFormatException("unexpected token " + token + " expecting " + VCML.BeginBlock);
}
token = tokens.nextToken();
int numVolumeElements = 0;
try {
numVolumeElements = Integer.valueOf(token).intValue();
} catch (NumberFormatException e) {
throw new MathFormatException("unexpected token " + token + " expecting the VolumeElementsMapVolumeRegion list length");
}
token = tokens.nextToken();
boolean bCompressed = token.equalsIgnoreCase("Compressed");
if (!bCompressed) {
if (!token.equalsIgnoreCase("UnCompressed")) {
throw new MathFormatException("unexpected token " + token + " expecting Compress or UnCompress");
}
}
byte[] volumeElementMap = new byte[numVolumeElements];
int checkCount = 0;
if (bCompressed) {
// Get HEX encoded bytes of the compressed VolumeElements-RegionID Map
StringBuffer hexOfCompressed = new StringBuffer();
while (tokens.hasMoreTokens()) {
token = tokens.nextToken();
if (token.equalsIgnoreCase(VCML.EndBlock)) {
break;
}
hexOfCompressed.append(token);
}
// Un-HEX the compressed data
byte[] compressedData = Hex.toBytes(hexOfCompressed.toString());
try {
meshRegionInfo.setCompressedVolumeElementMapVolumeRegion(compressedData, numVolumeElements);
} catch (IOException e) {
throw new MathFormatException("CartesianMesh.read->VolumeElementsMapVolumeRegion " + e.toString());
}
checkCount = meshRegionInfo.getUncompressedVolumeElementMapVolumeRegionLength();
} else {
while (tokens.hasMoreTokens()) {
token = tokens.nextToken();
if (token.equalsIgnoreCase(VCML.EndBlock)) {
break;
}
try {
int volumeRegionID = Integer.valueOf(token).intValue();
volumeElementMap[checkCount] = (byte) volumeRegionID;
} catch (NumberFormatException e) {
throw new MathFormatException("expected: # # #");
}
checkCount += 1;
}
}
if (checkCount != numVolumeElements && checkCount != 2 * numVolumeElements) {
throw new MathFormatException("CartesianMesh.read->VolumeElementsMapVolumeRegion: read " + checkCount + " VolumeElements but was expecting " + numVolumeElements);
}
continue;
}
//
//
//
HashMap<Integer, Integer> volumeRegionMapSubvolume = getVolumeRegionMapSubvolume(meshRegionInfo);
if (token.equalsIgnoreCase(VCML.MembraneElements)) {
//
// read '{'
//
token = tokens.nextToken();
if (!token.equalsIgnoreCase(VCML.BeginBlock)) {
throw new MathFormatException("unexpected token " + token + " expecting " + VCML.BeginBlock);
}
token = tokens.nextToken();
int numMemElements = 0;
try {
numMemElements = Integer.valueOf(token).intValue();
} catch (NumberFormatException e) {
throw new MathFormatException("unexpected token " + token + " expecting the membraneElement list length");
}
//
// read list of the following format:
//
// memIndex insideVolIndex outsideVolIndex
//
membraneElements = new MembraneElement[numMemElements];
int index = 0;
int[] membraneElementMapMembraneRegion = null;
if (bRegions) {
membraneElementMapMembraneRegion = new int[numMemElements];
meshRegionInfo.mapMembraneElementsToMembraneRegions(membraneElementMapMembraneRegion);
}
//
while (tokens.hasMoreTokens()) {
token = tokens.nextToken();
if (token.equalsIgnoreCase(VCML.EndBlock)) {
break;
}
int memIndex = -1;
int insideIndex = -1;
int outsideIndex = -1;
try {
//
// read first three tokens of a membrane element
//
// membraneIndex insideIndex outsideIndex
//
memIndex = Integer.valueOf(token).intValue();
token = tokens.nextToken();
insideIndex = Integer.valueOf(token).intValue();
token = tokens.nextToken();
outsideIndex = Integer.valueOf(token).intValue();
if (subdomainInfo != null) {
int insideRegionIndex = meshRegionInfo.getVolumeElementMapVolumeRegion(insideIndex);
int outsideRegionIndex = meshRegionInfo.getVolumeElementMapVolumeRegion(outsideIndex);
int insideSubVolumeHandle = volumeRegionMapSubvolume.get(insideRegionIndex);
int outsideSubVolumeHandle = volumeRegionMapSubvolume.get(outsideRegionIndex);
int realInsideSubVolumeHandle = subdomainInfo.getInside(insideSubVolumeHandle, outsideSubVolumeHandle);
if (realInsideSubVolumeHandle != insideSubVolumeHandle) {
int temp = insideIndex;
insideIndex = outsideIndex;
outsideIndex = temp;
}
}
} catch (NumberFormatException e) {
throw new MathFormatException("expected: # # #");
}
MembraneElement me = null;
//
if (bConnectivity) {
try {
token = tokens.nextToken();
int neighbor1 = Integer.valueOf(token).intValue();
token = tokens.nextToken();
int neighbor2 = Integer.valueOf(token).intValue();
token = tokens.nextToken();
int neighbor3 = Integer.valueOf(token).intValue();
token = tokens.nextToken();
int neighbor4 = Integer.valueOf(token).intValue();
//
if (bRegions) {
token = tokens.nextToken();
int regionID = Integer.valueOf(token).intValue();
membraneElementMapMembraneRegion[memIndex] = regionID;
}
if (membraneMeshMetrics == null) {
me = new MembraneElement(memIndex, insideIndex, outsideIndex, neighbor1, neighbor2, neighbor3, neighbor4, MembraneElement.AREA_UNDEFINED, 0, 0, 0, 0, 0, 0);
} else {
me = new MembraneElement(memIndex, insideIndex, outsideIndex, neighbor1, neighbor2, neighbor3, neighbor4, membraneMeshMetrics.areas[memIndex], membraneMeshMetrics.normals[memIndex][0], membraneMeshMetrics.normals[memIndex][1], membraneMeshMetrics.normals[memIndex][2], membraneMeshMetrics.centroids[memIndex][0], membraneMeshMetrics.centroids[memIndex][1], membraneMeshMetrics.centroids[memIndex][2]);
}
} catch (NumberFormatException e) {
throw new MathFormatException("expected: # # # # # # #");
}
} else {
me = new MembraneElement(memIndex, insideIndex, outsideIndex);
}
membraneElements[index] = me;
index++;
}
continue;
}
if (token.equalsIgnoreCase(VCML.ContourElements)) {
//
// read '{'
//
token = tokens.nextToken();
if (!token.equalsIgnoreCase(VCML.BeginBlock)) {
throw new MathFormatException("unexpected token " + token + " expecting " + VCML.BeginBlock);
}
token = tokens.nextToken();
int numContourElements = 0;
try {
numContourElements = Integer.valueOf(token).intValue();
} catch (NumberFormatException e) {
throw new MathFormatException("unexpected token " + token + " expecting the contourElement list length");
}
//
// read list of the following format:
//
// contourIndex volumeIndex beginCoord endCoord prevIndex nextIndex
//
contourElements = new ContourElement[numContourElements];
int index = 0;
//
while (tokens.hasMoreTokens()) {
token = tokens.nextToken();
if (token.equalsIgnoreCase(VCML.EndBlock)) {
break;
}
ContourElement ce = null;
try {
//
// read first two tokens of a contour element
//
// contourIndex volumeIndex
//
int contourIndex = Integer.valueOf(token).intValue();
token = tokens.nextToken();
int volumeIndex = Integer.valueOf(token).intValue();
token = tokens.nextToken();
//
// read beginCoord endCoord
//
double beginX = Double.valueOf(token).doubleValue();
token = tokens.nextToken();
double beginY = Double.valueOf(token).doubleValue();
token = tokens.nextToken();
double beginZ = Double.valueOf(token).doubleValue();
token = tokens.nextToken();
double endX = Double.valueOf(token).doubleValue();
token = tokens.nextToken();
double endY = Double.valueOf(token).doubleValue();
token = tokens.nextToken();
double endZ = Double.valueOf(token).doubleValue();
token = tokens.nextToken();
Coordinate begin = new Coordinate(beginX, beginY, beginZ);
Coordinate end = new Coordinate(endX, endY, endZ);
//
// read last two tokens of a contour element
//
// prevContourIndex nextContourIndex
//
int prevContourIndex = Integer.valueOf(token).intValue();
token = tokens.nextToken();
int nextContourIndex = Integer.valueOf(token).intValue();
ce = new ContourElement(contourIndex, volumeIndex, begin, end, prevContourIndex, nextContourIndex);
} catch (NumberFormatException e) {
throw new MathFormatException("expected: %d %d %f %f %f %f %f %f %d %d");
}
contourElements[index] = ce;
index++;
}
continue;
}
throw new MathFormatException("unexpected identifier " + token);
}
int dimension = getGeometryDimension(size);
switch(dimension) {
case 1:
{
if (extent.y != 1 || extent.z != 1) {
System.out.println("Extent " + extent.toString() + " for a 1-D mesh truncated to 1 for y and z");
extent = new Vect3D(extent.x, 1.0, 1.0);
}
break;
}
case 2:
{
if (extent.z != 1) {
System.out.println("Extent " + extent.toString() + " for a 2-D mesh truncated to 1 for z");
extent = new Vect3D(extent.x, extent.y, 1.0);
}
break;
}
}
CartesianMesh mesh = new CartesianMesh(version, subdomainInfo, membraneElements, contourElements, meshRegionInfo, size, extent, origin, dimension);
return mesh;
}
use of org.vcell.vis.vcell.MembraneElement in project vcell by virtualcell.
the class CartesianMeshMapping method fromMesh3DMembrane.
private VisMesh fromMesh3DMembrane(CartesianMesh cartesianMesh, String domainName) {
ISize size = cartesianMesh.getSize();
int numX = size.getX();
int numY = size.getY();
int dimension = 3;
Vect3D origin = new Vect3D(cartesianMesh.getOrigin().x, cartesianMesh.getOrigin().y, cartesianMesh.getOrigin().z);
Vect3D extent = new Vect3D(cartesianMesh.getExtent().x, cartesianMesh.getExtent().y, cartesianMesh.getExtent().z);
// invoke VisMesh() constructor
VisMesh visMesh = new VisMesh(dimension, origin, extent);
int currPointIndex = 0;
HashMap<String, Integer> pointDict = new HashMap<String, Integer>();
List<MembraneElement> membraneElements = cartesianMesh.getMembraneElements(domainName);
for (MembraneElement membraneElement : membraneElements) {
// inside
int insideVolumeIndex = membraneElement.getInsideVolumeIndex();
int insideI = insideVolumeIndex % numX;
int insideJ = (insideVolumeIndex % (numX * numY)) / numX;
int insideK = insideVolumeIndex / (numX * numY);
Box3D insideBox = cartesianMesh.getVolumeElementBox(insideI, insideJ, insideK);
// outside
int outsideVolumeIndex = membraneElement.getOutsideVolumeIndex();
int outsideI = outsideVolumeIndex % numX;
int outsideJ = (outsideVolumeIndex % (numX * numY)) / numX;
int outsideK = outsideVolumeIndex / (numX * numY);
VisPoint p1Coord;
VisPoint p2Coord;
VisPoint p3Coord;
VisPoint p4Coord;
if (insideI == outsideI + 1) {
// x- z cross y
double x = insideBox.x_lo;
p1Coord = new VisPoint(x, insideBox.y_lo, insideBox.z_lo);
p2Coord = new VisPoint(x, insideBox.y_lo, insideBox.z_hi);
p3Coord = new VisPoint(x, insideBox.y_hi, insideBox.z_hi);
p4Coord = new VisPoint(x, insideBox.y_hi, insideBox.z_lo);
} else if (outsideI == insideI + 1) {
// x+ y cross z
double x = insideBox.x_hi;
p1Coord = new VisPoint(x, insideBox.y_lo, insideBox.z_lo);
p2Coord = new VisPoint(x, insideBox.y_hi, insideBox.z_lo);
p3Coord = new VisPoint(x, insideBox.y_hi, insideBox.z_hi);
p4Coord = new VisPoint(x, insideBox.y_lo, insideBox.z_hi);
} else if (insideJ == outsideJ + 1) {
// y- x cross z
double y = insideBox.y_lo;
p1Coord = new VisPoint(insideBox.x_lo, y, insideBox.z_lo);
p2Coord = new VisPoint(insideBox.x_hi, y, insideBox.z_lo);
p3Coord = new VisPoint(insideBox.x_hi, y, insideBox.z_hi);
p4Coord = new VisPoint(insideBox.x_lo, y, insideBox.z_hi);
} else if (outsideJ == insideJ + 1) {
// y+ z cross x
double y = insideBox.y_hi;
p1Coord = new VisPoint(insideBox.x_lo, y, insideBox.z_lo);
p2Coord = new VisPoint(insideBox.x_lo, y, insideBox.z_hi);
p3Coord = new VisPoint(insideBox.x_hi, y, insideBox.z_hi);
p4Coord = new VisPoint(insideBox.x_hi, y, insideBox.z_lo);
} else if (insideK == outsideK + 1) {
// z- y cross x
double z = insideBox.z_lo;
p1Coord = new VisPoint(insideBox.x_lo, insideBox.y_lo, z);
p2Coord = new VisPoint(insideBox.x_lo, insideBox.y_hi, z);
p3Coord = new VisPoint(insideBox.x_hi, insideBox.y_hi, z);
p4Coord = new VisPoint(insideBox.x_hi, insideBox.y_lo, z);
} else if (outsideK == insideK + 1) {
// z+ x cross y
double z = insideBox.z_hi;
p1Coord = new VisPoint(insideBox.x_lo, insideBox.y_lo, z);
p2Coord = new VisPoint(insideBox.x_hi, insideBox.y_lo, z);
p3Coord = new VisPoint(insideBox.x_hi, insideBox.y_hi, z);
p4Coord = new VisPoint(insideBox.x_lo, insideBox.y_hi, z);
} else {
throw new RuntimeException("inside/outside volume indices not reconciled in membraneElement " + membraneElement.getMembraneIndex() + " in domain " + domainName);
}
//
// make sure vertices are added to model without duplicates and get the assigned identifier.
//
String p1Key = toStringKey(p1Coord);
Integer i1 = pointDict.get(p1Key);
if (i1 == null) {
pointDict.put(p1Key, currPointIndex);
i1 = currPointIndex;
visMesh.addToPoints(p1Coord);
currPointIndex++;
}
String p2Key = toStringKey(p2Coord);
Integer i2 = pointDict.get(p2Key);
if (i2 == null) {
pointDict.put(p2Key, currPointIndex);
i2 = currPointIndex;
visMesh.addToPoints(p2Coord);
currPointIndex++;
}
String p3Key = toStringKey(p3Coord);
Integer i3 = pointDict.get(p3Key);
if (i3 == null) {
pointDict.put(p3Key, currPointIndex);
i3 = currPointIndex;
visMesh.addToPoints(p3Coord);
currPointIndex++;
}
String p4Key = toStringKey(p4Coord);
Integer i4 = pointDict.get(p4Key);
if (i4 == null) {
pointDict.put(p4Key, currPointIndex);
i4 = currPointIndex;
visMesh.addToPoints(p4Coord);
currPointIndex++;
}
VisPolygon quad = new VisPolygon(Arrays.asList(new Integer[] { i1, i2, i3, i4 }));
quad.setFiniteVolumeIndex(new FiniteVolumeIndex(membraneElement.getMembraneIndex(), cartesianMesh.getMembraneRegionIndex(membraneElement.getMembraneIndex())));
// print('adding a cell at level '+str(currLevel.getLevel())+" from "+str(p1Coord)+" to "+str(p3Coord))
visMesh.addToPolygons(quad);
}
return visMesh;
}
Aggregations