use of cbit.vcell.geometry.RegionImage.MembraneElementIdentifier in project vcell by virtualcell.
the class RasterExporter method writeStanfordPolygonTex.
public static PolyTexHelper writeStanfordPolygonTex(RegionImage regionImage, Writer writer, /*BitSet bInDomain,*/
CartesianMesh mesh) throws IOException, MathException {
int totalPolygons = regionImage.getSurfacecollection().getTotalPolygonCount();
int totalVerts = totalPolygons * 4;
final int ALL_NEIGHBOR_QUAD_BOX_SIDE = 3;
// 2D image texture
int imgSideSize = (int) Math.ceil(Math.sqrt((totalPolygons)));
int texelXSize = (imgSideSize * ALL_NEIGHBOR_QUAD_BOX_SIDE);
int[][] texIndexToFaceIndexPtrs = new int[texelXSize * texelXSize][0];
// Arrays.fill(texImage0, Integer.MAX_VALUE);
final int squareSize = 40;
int fontXSize = texelXSize * squareSize;
int fontYSize = texelXSize * squareSize;
BufferedImage image0 = createTextureImage(fontXSize);
// int[] texFontImage = new int[fontXSize*fontYSize];
// Arrays.fill(texFontImage, 0xFF000000);
// int[] bitMasks0 = new int[]{0xFF0000, 0xFF00, 0xFF, 0xFF000000};
// SinglePixelPackedSampleModel sm0 = new SinglePixelPackedSampleModel(
// DataBuffer.TYPE_INT, texelXSize*squareSize, texelXSize*squareSize, bitMasks0);
// DataBufferInt db0 = new DataBufferInt(texFontImage, texFontImage.length);
// WritableRaster wr0 = Raster.createWritableRaster(sm0, db0, new Point());
// BufferedImage image0 = new BufferedImage(ColorModel.getRGBdefault(), wr0, false, null);
Graphics2D g2d = image0.createGraphics();
g2d.setColor(Color.white);
Font bigF = Font.decode("Dialog-plain-10");
g2d.setFont(bigF);
float texelSize = 1.0f / texelXSize;
writer.write("ply\n");
writer.write("format ascii 1.0\n");
// writer.write("comment "+geometrySurfaceDescription.getGeometry().getName()+"\n");
writer.write("comment textureImage size is " + imgSideSize + "x" + imgSideSize + "\n");
writer.write("element vertex " + totalVerts + "\n");
writer.write("property float x\n");
writer.write("property float y\n");
writer.write("property float z\n");
// must be 's' for blender (u texture coord)
writer.write("property float s\n");
// must be 't' for blender (v texture coord)
writer.write("property float t\n");
writer.write("element face " + totalPolygons + "\n");
writer.write("property list uchar int vertex_index\n");
writer.write("end_header\n");
StringBuffer sbVert = new StringBuffer();
StringBuffer sbFace = new StringBuffer();
int faceIndex = 0;
int vertIndex = 0;
for (int currSurf = 0; currSurf < regionImage.getSurfacecollection().getSurfaceCount(); currSurf++) {
for (int currFace = 0; currFace < regionImage.getSurfacecollection().getSurfaces(currSurf).getPolygonCount(); currFace++) {
// if(bInDomain != null && !bInDomain.get(faceIndex)){
// continue;
// }
g2d.setColor(Color.white);
Quadrilateral quad = (Quadrilateral) regionImage.getSurfacecollection().getSurfaces(currSurf).getPolygons(currFace);
NAHelper naHelper0 = calcNAHelper(quad);
Point2D p2dm = Coordinate.get2DProjection(naHelper0.middle, naHelper0.normalAxis);
int x = faceIndex % imgSideSize;
int y = faceIndex / imgSideSize;
int boxX = (x * ALL_NEIGHBOR_QUAD_BOX_SIDE) + 1;
int boxY = (y * ALL_NEIGHBOR_QUAD_BOX_SIDE) + 1;
int fontX = boxX * squareSize;
int fontY = boxY * squareSize;
// das.getColorFromValue(data[faceIndex]);//face color
texIndexToFaceIndexPtrs[boxY * texelXSize + boxX] = new int[] { faceIndex };
g2d.drawString(currSurf + "-" + currFace, fontX, fontY);
float imgXTexelCenter = (boxX + .5f) * texelSize;
float imgYTexelCenter = (boxY + .5f) * texelSize;
ArrayList<RegionImage.MembraneEdgeNeighbor> neighbors = regionImage.getMembraneEdgeNeighbors()[currSurf][currFace];
sbFace.append(quad.getNodeCount() + "");
for (int vrt = 0; vrt < quad.getNodeCount(); vrt++) {
g2d.setColor(Color.white);
RegionImage.MembraneEdgeNeighbor membraneEdgeNeighbor = neighbors.get(vrt);
MembraneElementIdentifier neighb = membraneEdgeNeighbor.getMembraneElementIdentifier();
if (neighb != null) {
Quadrilateral quadNeighbor = (Quadrilateral) regionImage.getSurfacecollection().getSurfaces(neighb.surfaceIndex).getPolygons(neighb.nonMasterPolygonIndex);
NAHelper naHelper1 = calcNAHelper(quadNeighbor);
Point2D p2dmNeighb = Coordinate.get2DProjection(naHelper1.middle, naHelper0.normalAxis);
double xdiff = p2dmNeighb.getX() - p2dm.getX();
double ydiff = p2dmNeighb.getY() - p2dm.getY();
int dxn = (int) Math.signum((Math.abs(xdiff) < 1e-9 ? 0 : xdiff));
int dyn = (int) Math.signum((Math.abs(ydiff) < 1e-9 ? 0 : ydiff));
// das.getColorFromValue(data[membraneEdgeNeighbor.getMasterPolygonIndex()]);
texIndexToFaceIndexPtrs[(boxY + dyn) * texelXSize + (boxX + dxn)] = new int[] { membraneEdgeNeighbor.getMasterPolygonIndex() };
g2d.drawString(neighb.nonMasterPolygonIndex + "", fontX + (dxn * squareSize), fontY + (dyn * squareSize));
RegionImage.MembraneEdgeNeighbor membraneEdgeNeighbor2 = neighbors.get((vrt + 1 == quad.getNodeCount() ? 0 : vrt + 1));
MembraneElementIdentifier neighb2 = membraneEdgeNeighbor2.getMembraneElementIdentifier();
if (neighb2 != null) {
CommonNeighb commonNeighb = calculateCommonNeighbor(mesh, regionImage, mesh.getMembraneElements()[membraneEdgeNeighbor.getMasterPolygonIndex()], mesh.getMembraneElements()[membraneEdgeNeighbor2.getMasterPolygonIndex()], faceIndex);
if (commonNeighb != null) {
if (commonNeighb.neighborneighbos.size() == 0) {
commonNeighb.neighborneighbos.add(membraneEdgeNeighbor.getMasterPolygonIndex());
commonNeighb.neighborneighbos.add(membraneEdgeNeighbor2.getMasterPolygonIndex());
}
// mesh.getMembraneElements()[commonNeighb].getCentroid();
Coordinate commonCoord = new Coordinate(commonNeighb.node.getX(), commonNeighb.node.getY(), commonNeighb.node.getZ());
Point2D p2dmcommon = Coordinate.get2DProjection(commonCoord, naHelper0.normalAxis);
double xdiff1 = p2dmcommon.getX() - p2dm.getX();
double ydiff1 = p2dmcommon.getY() - p2dm.getY();
int dxc = (int) Math.signum((Math.abs(xdiff1) < 1e-9 ? 0 : xdiff1));
int dyc = (int) Math.signum((Math.abs(ydiff1) < 1e-9 ? 0 : ydiff1));
g2d.setColor(Color.green);
int xloc = fontX + (dxc * squareSize);
int yloc = fontY + (dyc * squareSize);
double avgVal = 0;
int texIndex = (boxY + dyc) * texelXSize + boxX + dxc;
texIndexToFaceIndexPtrs[texIndex] = new int[commonNeighb.neighborneighbos.size()];
for (int k = 0; k < commonNeighb.neighborneighbos.size(); k++) {
g2d.drawString(regionImage.getQuadIndexToSurfAndFace().get(commonNeighb.neighborneighbos.get(k)).getFace() + "", xloc, yloc + (k * 11));
// avgVal+= data[commonNeighb.neighborneighbos.get(k)];
// das.getColorFromValue(avgVal/commonNeighb.neighborneighbos.size());
texIndexToFaceIndexPtrs[texIndex][k] = commonNeighb.neighborneighbos.get(k);
}
// if(texImage0[texIndex] != Integer.MAX_VALUE){
// System.out.println("texture pixel used twice");
// }
}
}
} else {
System.out.println("neighbor null");
}
Point2D p2dv = Coordinate.get2DProjection(new Coordinate(quad.getNodes(vrt).getX(), quad.getNodes(vrt).getY(), quad.getNodes(vrt).getZ()), naHelper0.normalAxis);
double xdiff3 = p2dv.getX() - p2dm.getX();
double ydiff3 = p2dv.getY() - p2dm.getY();
int dx = (int) Math.signum((Math.abs(xdiff3) < 1e-9 ? 0 : xdiff3));
int dy = (int) Math.signum((Math.abs(ydiff3) < 1e-9 ? 0 : ydiff3));
float u = (float) imgXTexelCenter + ((float) dx * texelSize / 2.0f);
float v = (float) imgYTexelCenter + ((float) dy * texelSize / 2.0f);
Node vertex = quad.getNodes()[vrt];
sbVert.append((float) vertex.getX() + " " + (float) vertex.getY() + " " + (float) vertex.getZ() + " " + u + " " + v + "\n");
sbFace.append(" ");
sbFace.append(vertIndex + "");
vertIndex++;
}
sbFace.append("\n");
faceIndex++;
}
}
if (faceIndex != totalPolygons || vertIndex != totalVerts) {
throw new IOException("Final faces and verts count don't match");
}
writer.write(sbVert.toString());
writer.write(sbFace.toString());
// ImageIO.write(op0.filter(image0, null), "png", baos);
return new PolyTexHelper(texIndexToFaceIndexPtrs, flipPNG(image0), texelXSize);
}
Aggregations