use of cbit.vcell.geometry.surface.Polygon in project vcell by virtualcell.
the class SmoldynFileWriter method writeSurfaces.
private void writeSurfaces() throws SolverException, ImageException, PropertyVetoException, GeometryException, ExpressionException {
GeometrySurfaceDescription geometrySurfaceDescription = resampledGeometry.getGeometrySurfaceDescription();
SurfaceClass[] surfaceClasses = geometrySurfaceDescription.getSurfaceClasses();
GeometrySpec geometrySpec = resampledGeometry.getGeometrySpec();
SubVolume[] surfaceGeometrySubVolumes = geometrySpec.getSubVolumes();
GeometricRegion[] AllGeometricRegions = resampledGeometry.getGeometrySurfaceDescription().getGeometricRegions();
ArrayList<SurfaceGeometricRegion> surfaceRegionList = new ArrayList<SurfaceGeometricRegion>();
ArrayList<VolumeGeometricRegion> volumeRegionList = new ArrayList<VolumeGeometricRegion>();
for (GeometricRegion geometricRegion : AllGeometricRegions) {
if (geometricRegion instanceof SurfaceGeometricRegion) {
surfaceRegionList.add((SurfaceGeometricRegion) geometricRegion);
} else if (geometricRegion instanceof VolumeGeometricRegion) {
volumeRegionList.add((VolumeGeometricRegion) geometricRegion);
} else {
throw new SolverException("unsupported geometric region type " + geometricRegion.getClass());
}
}
printWriter.println("# geometry");
printWriter.println(SmoldynVCellMapper.SmoldynKeyword.dim + " " + dimension);
if (bHasNoSurface) {
printWriter.println(SmoldynVCellMapper.SmoldynKeyword.max_compartment + " " + surfaceGeometrySubVolumes.length);
} else {
printWriter.println(SmoldynVCellMapper.SmoldynKeyword.max_compartment + " " + (surfaceGeometrySubVolumes.length + 1));
// plus the surface which are bounding walls
printWriter.println(SmoldynVCellMapper.SmoldynKeyword.max_surface + " " + (surfaceClasses.length + dimension));
}
printWriter.println();
// write boundaries and wall surfaces
writeWallSurfaces();
// for 3D ... smoldyn normal convension is triangle right-hand-rule normal points to the outside compartment subdomain.
if (!bHasNoSurface) {
membraneSubdomainTriangleMap = new HashMap<MembraneSubDomain, ArrayList<TrianglePanel>>();
// write surfaces
printWriter.println("# surfaces");
int triangleGlobalCount = 0;
int membraneIndex = -1;
SurfaceCollection surfaceCollection = geometrySurfaceDescription.getSurfaceCollection();
// pre-allocate collections used repeatedly in following loops; clear before reusing
HashMap<Node, Set<String>> nodeTriMap = new HashMap<>();
ArrayList<TrianglePanel> triList = new ArrayList<TrianglePanel>();
// use a sorted set to ensure neighbors written out is same order for reproducibility
SortedSet<String> neighborsForCurrentNode = new TreeSet<String>();
for (int sci = 0; sci < surfaceClasses.length; sci++) {
nodeTriMap.clear();
triList.clear();
int triLocalCount = 0;
SurfaceClass surfaceClass = surfaceClasses[sci];
GeometricRegion[] geometricRegions = geometrySurfaceDescription.getGeometricRegions(surfaceClass);
for (GeometricRegion gr : geometricRegions) {
SurfaceGeometricRegion sgr = (SurfaceGeometricRegion) gr;
VolumeGeometricRegion volRegion0 = (VolumeGeometricRegion) sgr.getAdjacentGeometricRegions()[0];
VolumeGeometricRegion volRegion1 = (VolumeGeometricRegion) sgr.getAdjacentGeometricRegions()[1];
SubVolume subVolume0 = volRegion0.getSubVolume();
SubVolume subVolume1 = volRegion1.getSubVolume();
CompartmentSubDomain compart0 = mathDesc.getCompartmentSubDomain(subVolume0.getName());
CompartmentSubDomain compart1 = mathDesc.getCompartmentSubDomain(subVolume1.getName());
MembraneSubDomain membraneSubDomain = mathDesc.getMembraneSubDomain(compart0, compart1);
if (membraneSubDomain == null) {
throw new SolverException(VCellErrorMessages.getSmoldynUnexpectedSurface(compart0, compart1));
}
int exteriorRegionID = volRegion0.getRegionID();
int interiorRegionID = volRegion1.getRegionID();
if (membraneSubDomain.getInsideCompartment() == compart0) {
exteriorRegionID = volRegion1.getRegionID();
interiorRegionID = volRegion0.getRegionID();
}
for (int j = 0; j < surfaceCollection.getSurfaceCount(); j++) {
Surface surface = surfaceCollection.getSurfaces(j);
if ((surface.getInteriorRegionIndex() == exteriorRegionID && surface.getExteriorRegionIndex() == interiorRegionID) || (surface.getInteriorRegionIndex() == interiorRegionID && surface.getExteriorRegionIndex() == exteriorRegionID)) {
// Polygon polygon = surface.getPolygons(k);
for (Polygon polygon : surface) {
if (polygonMembaneElementMap != null) {
membraneIndex = polygonMembaneElementMap.get(polygon).getMembraneIndex();
}
Node[] nodes = polygon.getNodes();
if (dimension == 2) {
// ignore z
Vect3d unitNormal = new Vect3d();
polygon.getUnitNormal(unitNormal);
unitNormal.set(unitNormal.getX(), unitNormal.getY(), 0);
int point0 = 0;
Vect3d v0 = new Vect3d(nodes[point0].getX(), nodes[point0].getY(), 0);
int point1 = 1;
Vect3d v1 = null;
for (point1 = 1; point1 < nodes.length; point1++) {
if (v0.getX() != nodes[point1].getX() || v0.getY() != nodes[point1].getY()) {
v1 = new Vect3d(nodes[point1].getX(), nodes[point1].getY(), 0);
break;
}
}
if (v1 == null) {
throw new RuntimeException("failed to generate surface");
}
Vect3d v01 = Vect3d.sub(v1, v0);
Vect3d unit01n = v01.cross(unitNormal);
unit01n.unit();
if (Math.abs(unit01n.getZ() - 1.0) < 1e-6) {
// v0 to v1 opposes vcell surface normal. it's already flipped.
Triangle triangle;
if (surface.getInteriorRegionIndex() == interiorRegionID) {
// we have to flipped it back
triangle = new Triangle(nodes[point1], nodes[point0], null);
} else {
triangle = new Triangle(nodes[point0], nodes[point1], null);
}
triList.add(new TrianglePanel(triLocalCount++, triangleGlobalCount++, membraneIndex, triangle));
} else if (Math.abs(unit01n.getZ() + 1.0) < 1e-6) {
// v0 to v1 is in direction of vcell surface normal.
Triangle triangle;
if (surface.getInteriorRegionIndex() == interiorRegionID) {
triangle = new Triangle(nodes[point0], nodes[point1], null);
} else {
triangle = new Triangle(nodes[point1], nodes[point0], null);
}
triList.add(new TrianglePanel(triLocalCount++, triangleGlobalCount++, membraneIndex, triangle));
} else {
throw new RuntimeException("failed to generate surface");
}
} else if (dimension == 3) {
Triangle triangle1;
Triangle triangle2;
if (surface.getInteriorRegionIndex() == interiorRegionID) {
// interior
triangle1 = new Triangle(nodes[0], nodes[1], nodes[2]);
triangle2 = new Triangle(nodes[0], nodes[2], nodes[3]);
} else {
triangle1 = new Triangle(nodes[2], nodes[1], nodes[0]);
triangle2 = new Triangle(nodes[3], nodes[2], nodes[0]);
}
triList.add(new TrianglePanel(triLocalCount++, triangleGlobalCount++, membraneIndex, triangle1));
triList.add(new TrianglePanel(triLocalCount++, triangleGlobalCount++, membraneIndex, triangle2));
}
}
}
}
}
// add triangles to node hash
for (TrianglePanel triPanel : triList) {
for (Node node : triPanel.triangle.getNodes()) {
if (node == null) {
continue;
}
Set<String> triNameSet = nodeTriMap.get(node);
if (triNameSet == null) {
triNameSet = new HashSet<String>();
nodeTriMap.put(node, triNameSet);
}
triNameSet.add(triPanel.name);
}
}
SubVolume[] adjacentSubvolums = surfaceClass.getAdjacentSubvolumes().toArray(new SubVolume[0]);
CompartmentSubDomain csd0 = simulation.getMathDescription().getCompartmentSubDomain(adjacentSubvolums[0].getName());
CompartmentSubDomain csd1 = simulation.getMathDescription().getCompartmentSubDomain(adjacentSubvolums[1].getName());
MembraneSubDomain membraneSubDomain = simulation.getMathDescription().getMembraneSubDomain(csd0, csd1);
membraneSubdomainTriangleMap.put(membraneSubDomain, triList);
final boolean initialMoleculesOnMembrane = (closestTriangles != null);
if (initialMoleculesOnMembrane) {
findClosestTriangles(membraneSubDomain, triList);
}
printWriter.println(SmoldynVCellMapper.SmoldynKeyword.start_surface + " " + surfaceClass.getName());
printWriter.println(SmoldynVCellMapper.SmoldynKeyword.action + " " + SmoldynVCellMapper.SmoldynKeyword.all + "(" + SmoldynVCellMapper.SmoldynKeyword.all + ") " + SmoldynVCellMapper.SmoldynKeyword.both + " " + SmoldynVCellMapper.SmoldynKeyword.reflect);
// printWriter.println(SmoldynKeyword.action + " " + SmoldynKeyword.all + "(" + SmoldynKeyword.up + ") " + SmoldynKeyword.both + " " + SmoldynKeyword.reflect);
// get color after species
Color c = colors[sci + particleVariableList.size()];
printWriter.println(SmoldynVCellMapper.SmoldynKeyword.color + " " + SmoldynVCellMapper.SmoldynKeyword.both + " " + c.getRed() / 255.0 + " " + c.getGreen() / 255.0 + " " + c.getBlue() / 255.0 + " 0.1");
printWriter.println(SmoldynVCellMapper.SmoldynKeyword.polygon + " " + SmoldynVCellMapper.SmoldynKeyword.front + " " + SmoldynVCellMapper.SmoldynKeyword.edge);
printWriter.println(SmoldynVCellMapper.SmoldynKeyword.polygon + " " + SmoldynVCellMapper.SmoldynKeyword.back + " " + SmoldynVCellMapper.SmoldynKeyword.edge);
printWriter.println(SmoldynVCellMapper.SmoldynKeyword.max_panels + " " + SmoldynVCellMapper.SmoldynKeyword.tri + " " + triList.size());
for (TrianglePanel trianglePanel : triList) {
Triangle triangle = trianglePanel.triangle;
printWriter.print(SmoldynVCellMapper.SmoldynKeyword.panel + " " + SmoldynVCellMapper.SmoldynKeyword.tri);
switch(dimension) {
case 1:
printWriter.print(" " + triangle.getNodes(0).getX());
break;
case 2:
printWriter.print(" " + triangle.getNodes(0).getX() + " " + triangle.getNodes(0).getY());
printWriter.print(" " + triangle.getNodes(1).getX() + " " + triangle.getNodes(1).getY());
break;
case 3:
for (Node node : triangle.getNodes()) {
printWriter.print(" " + node.getX() + " " + node.getY() + " " + node.getZ());
}
break;
}
printWriter.println(" " + trianglePanel.name);
}
for (TrianglePanel triPanel : triList) {
neighborsForCurrentNode.clear();
for (Node node : triPanel.triangle.getNodes()) {
if (node == null) {
continue;
}
neighborsForCurrentNode.addAll(nodeTriMap.get(node));
}
neighborsForCurrentNode.remove(triPanel.name);
// printWriter.print(SmoldynKeyword.neighbors + " " +triPanel.name);
// to allow smoldyn read line length as 256, chop the neighbors to multiple lines
int maxNeighborCount = 4;
//
int count = 0;
for (String neigh : neighborsForCurrentNode) {
if (count % maxNeighborCount == 0) {
printWriter.println();
printWriter.print(SmoldynVCellMapper.SmoldynKeyword.neighbors + " " + triPanel.name);
}
printWriter.print(" " + neigh);
count++;
}
}
printWriter.println();
printWriter.println(SmoldynVCellMapper.SmoldynKeyword.end_surface);
printWriter.println();
}
// write compartment
// printWriter.println("# bounding wall compartment");
// printWriter.println(SmoldynKeyword.start_compartment + " " + VCellSmoldynKeyword.bounding_wall_compartment);
// printWriter.println(SmoldynKeyword.surface + " " + VCellSmoldynKeyword.bounding_wall_surface_X);
// if (dimension > 1) {
// printWriter.println(SmoldynKeyword.surface + " " + VCellSmoldynKeyword.bounding_wall_surface_Y);
// if (dimension > 2) {
// printWriter.println(SmoldynKeyword.surface + " " + VCellSmoldynKeyword.bounding_wall_surface_Z);
// }
// }
// printWriter.println(SmoldynKeyword.end_compartment);
// printWriter.println();
}
}
use of cbit.vcell.geometry.surface.Polygon in project vcell by virtualcell.
the class SmoldynSurfaceTessellator method writeSurfaces.
protected void writeSurfaces() throws SolverException, ImageException, PropertyVetoException, GeometryException, ExpressionException {
GeometrySurfaceDescription geometrySurfaceDescription = resampledGeometry.getGeometrySurfaceDescription();
SurfaceClass[] surfaceClasses = geometrySurfaceDescription.getSurfaceClasses();
GeometrySpec geometrySpec = resampledGeometry.getGeometrySpec();
SubVolume[] surfaceGeometrySubVolumes = geometrySpec.getSubVolumes();
GeometricRegion[] AllGeometricRegions = resampledGeometry.getGeometrySurfaceDescription().getGeometricRegions();
ArrayList<SurfaceGeometricRegion> surfaceRegionList = new ArrayList<SurfaceGeometricRegion>();
ArrayList<VolumeGeometricRegion> volumeRegionList = new ArrayList<VolumeGeometricRegion>();
for (GeometricRegion geometricRegion : AllGeometricRegions) {
if (geometricRegion instanceof SurfaceGeometricRegion) {
surfaceRegionList.add((SurfaceGeometricRegion) geometricRegion);
} else if (geometricRegion instanceof VolumeGeometricRegion) {
volumeRegionList.add((VolumeGeometricRegion) geometricRegion);
} else {
throw new SolverException("unsupported geometric region type " + geometricRegion.getClass());
}
}
printWriter.println("# geometry");
printWriter.println(SmoldynVCellMapper.SmoldynKeyword.dim + " " + dimension);
if (bHasNoSurface) {
printWriter.println(SmoldynVCellMapper.SmoldynKeyword.max_compartment + " " + surfaceGeometrySubVolumes.length);
} else {
printWriter.println(SmoldynVCellMapper.SmoldynKeyword.max_compartment + " " + (surfaceGeometrySubVolumes.length + 1));
// plus the surface which are bounding walls
printWriter.println(SmoldynVCellMapper.SmoldynKeyword.max_surface + " " + (surfaceClasses.length + dimension));
}
printWriter.println();
// write boundaries and wall surfaces
writeWallSurfaces();
// for 3D ... smoldyn normal convension is triangle right-hand-rule normal points to the outside compartment subdomain.
if (!bHasNoSurface) {
membraneSubdomainTriangleMap = new HashMap<MembraneSubDomain, ArrayList<TrianglePanel>>();
// write surfaces
printWriter.println("# surfaces");
int triangleGlobalCount = 0;
int membraneIndex = -1;
SurfaceCollection surfaceCollection = geometrySurfaceDescription.getSurfaceCollection();
// pre-allocate collections used repeatedly in following loops; clear before reusing
HashMap<Node, Set<String>> nodeTriMap = new HashMap<>();
ArrayList<TrianglePanel> triList = new ArrayList<TrianglePanel>();
// use a sorted set to ensure neighbors written out is same order for reproducibility
SortedSet<String> neighborsForCurrentNode = new TreeSet<String>();
for (int sci = 0; sci < surfaceClasses.length; sci++) {
nodeTriMap.clear();
triList.clear();
int triLocalCount = 0;
SurfaceClass surfaceClass = surfaceClasses[sci];
GeometricRegion[] geometricRegions = geometrySurfaceDescription.getGeometricRegions(surfaceClass);
for (GeometricRegion gr : geometricRegions) {
SurfaceGeometricRegion sgr = (SurfaceGeometricRegion) gr;
VolumeGeometricRegion volRegion0 = (VolumeGeometricRegion) sgr.getAdjacentGeometricRegions()[0];
VolumeGeometricRegion volRegion1 = (VolumeGeometricRegion) sgr.getAdjacentGeometricRegions()[1];
SubVolume subVolume0 = volRegion0.getSubVolume();
SubVolume subVolume1 = volRegion1.getSubVolume();
CompartmentSubDomain compart0 = mathDesc.getCompartmentSubDomain(subVolume0.getName());
CompartmentSubDomain compart1 = mathDesc.getCompartmentSubDomain(subVolume1.getName());
MembraneSubDomain membraneSubDomain = mathDesc.getMembraneSubDomain(compart0, compart1);
if (membraneSubDomain == null) {
throw new SolverException(VCellErrorMessages.getSmoldynUnexpectedSurface(compart0, compart1));
}
int exteriorRegionID = volRegion0.getRegionID();
int interiorRegionID = volRegion1.getRegionID();
if (membraneSubDomain.getInsideCompartment() == compart0) {
exteriorRegionID = volRegion1.getRegionID();
interiorRegionID = volRegion0.getRegionID();
}
for (int j = 0; j < surfaceCollection.getSurfaceCount(); j++) {
Surface surface = surfaceCollection.getSurfaces(j);
if ((surface.getInteriorRegionIndex() == exteriorRegionID && surface.getExteriorRegionIndex() == interiorRegionID) || (surface.getInteriorRegionIndex() == interiorRegionID && surface.getExteriorRegionIndex() == exteriorRegionID)) {
// Polygon polygon = surface.getPolygons(k);
for (Polygon polygon : surface) {
if (polygonMembaneElementMap != null) {
membraneIndex = polygonMembaneElementMap.get(polygon).getMembraneIndex();
}
Node[] nodes = polygon.getNodes();
if (dimension == 2) {
// ignore z
Vect3d unitNormal = new Vect3d();
polygon.getUnitNormal(unitNormal);
unitNormal.set(unitNormal.getX(), unitNormal.getY(), 0);
int point0 = 0;
Vect3d v0 = new Vect3d(nodes[point0].getX(), nodes[point0].getY(), 0);
int point1 = 1;
Vect3d v1 = null;
for (point1 = 1; point1 < nodes.length; point1++) {
if (v0.getX() != nodes[point1].getX() || v0.getY() != nodes[point1].getY()) {
v1 = new Vect3d(nodes[point1].getX(), nodes[point1].getY(), 0);
break;
}
}
if (v1 == null) {
throw new RuntimeException("failed to generate surface");
}
Vect3d v01 = Vect3d.sub(v1, v0);
Vect3d unit01n = v01.cross(unitNormal);
unit01n.unit();
if (Math.abs(unit01n.getZ() - 1.0) < 1e-6) {
// v0 to v1 opposes vcell surface normal. it's already flipped.
Triangle triangle;
if (surface.getInteriorRegionIndex() == interiorRegionID) {
// we have to flipped it back
triangle = new Triangle(nodes[point1], nodes[point0], null);
} else {
triangle = new Triangle(nodes[point0], nodes[point1], null);
}
triList.add(new TrianglePanel(triLocalCount++, triangleGlobalCount++, membraneIndex, triangle));
} else if (Math.abs(unit01n.getZ() + 1.0) < 1e-6) {
// v0 to v1 is in direction of vcell surface normal.
Triangle triangle;
if (surface.getInteriorRegionIndex() == interiorRegionID) {
triangle = new Triangle(nodes[point0], nodes[point1], null);
} else {
triangle = new Triangle(nodes[point1], nodes[point0], null);
}
triList.add(new TrianglePanel(triLocalCount++, triangleGlobalCount++, membraneIndex, triangle));
} else {
throw new RuntimeException("failed to generate surface");
}
} else if (dimension == 3) {
Triangle triangle1;
Triangle triangle2;
if (surface.getInteriorRegionIndex() == interiorRegionID) {
// interior
triangle1 = new Triangle(nodes[0], nodes[1], nodes[2]);
triangle2 = new Triangle(nodes[0], nodes[2], nodes[3]);
} else {
triangle1 = new Triangle(nodes[2], nodes[1], nodes[0]);
triangle2 = new Triangle(nodes[3], nodes[2], nodes[0]);
}
triList.add(new TrianglePanel(triLocalCount++, triangleGlobalCount++, membraneIndex, triangle1));
triList.add(new TrianglePanel(triLocalCount++, triangleGlobalCount++, membraneIndex, triangle2));
}
}
}
}
}
// add triangles to node hash
for (TrianglePanel triPanel : triList) {
for (Node node : triPanel.triangle.getNodes()) {
if (node == null) {
continue;
}
Set<String> triNameSet = nodeTriMap.get(node);
if (triNameSet == null) {
triNameSet = new HashSet<String>();
nodeTriMap.put(node, triNameSet);
}
triNameSet.add(triPanel.name);
}
}
SubVolume[] adjacentSubvolums = surfaceClass.getAdjacentSubvolumes().toArray(new SubVolume[0]);
CompartmentSubDomain csd0 = simulation.getMathDescription().getCompartmentSubDomain(adjacentSubvolums[0].getName());
CompartmentSubDomain csd1 = simulation.getMathDescription().getCompartmentSubDomain(adjacentSubvolums[1].getName());
MembraneSubDomain membraneSubDomain = simulation.getMathDescription().getMembraneSubDomain(csd0, csd1);
membraneSubdomainTriangleMap.put(membraneSubDomain, triList);
final boolean initialMoleculesOnMembrane = (closestTriangles != null);
if (initialMoleculesOnMembrane) {
findClosestTriangles(membraneSubDomain, triList);
}
printWriter.println(SmoldynVCellMapper.SmoldynKeyword.start_surface + " " + surfaceClass.getName());
printWriter.println(SmoldynVCellMapper.SmoldynKeyword.action + " " + SmoldynVCellMapper.SmoldynKeyword.all + "(" + SmoldynVCellMapper.SmoldynKeyword.all + ") " + SmoldynVCellMapper.SmoldynKeyword.both + " " + SmoldynVCellMapper.SmoldynKeyword.reflect);
// printWriter.println(SmoldynKeyword.action + " " + SmoldynKeyword.all + "(" + SmoldynKeyword.up + ") " + SmoldynKeyword.both + " " + SmoldynKeyword.reflect);
Color c = colorForSurface(sci);
printWriter.println(SmoldynVCellMapper.SmoldynKeyword.color + " " + SmoldynVCellMapper.SmoldynKeyword.both + " " + c.getRed() / 255.0 + " " + c.getGreen() / 255.0 + " " + c.getBlue() / 255.0 + " 0.1");
printWriter.println(SmoldynVCellMapper.SmoldynKeyword.polygon + " " + SmoldynVCellMapper.SmoldynKeyword.front + " " + SmoldynVCellMapper.SmoldynKeyword.edge);
printWriter.println(SmoldynVCellMapper.SmoldynKeyword.polygon + " " + SmoldynVCellMapper.SmoldynKeyword.back + " " + SmoldynVCellMapper.SmoldynKeyword.edge);
printWriter.println(SmoldynVCellMapper.SmoldynKeyword.max_panels + " " + SmoldynVCellMapper.SmoldynKeyword.tri + " " + triList.size());
for (TrianglePanel trianglePanel : triList) {
Triangle triangle = trianglePanel.triangle;
printWriter.print(SmoldynVCellMapper.SmoldynKeyword.panel + " " + SmoldynVCellMapper.SmoldynKeyword.tri);
switch(dimension) {
case 1:
printWriter.print(" " + triangle.getNodes(0).getX());
break;
case 2:
printWriter.print(" " + triangle.getNodes(0).getX() + " " + triangle.getNodes(0).getY());
printWriter.print(" " + triangle.getNodes(1).getX() + " " + triangle.getNodes(1).getY());
break;
case 3:
for (Node node : triangle.getNodes()) {
printWriter.print(" " + node.getX() + " " + node.getY() + " " + node.getZ());
}
break;
}
printWriter.println(" " + trianglePanel.name);
}
for (TrianglePanel triPanel : triList) {
neighborsForCurrentNode.clear();
for (Node node : triPanel.triangle.getNodes()) {
if (node == null) {
continue;
}
neighborsForCurrentNode.addAll(nodeTriMap.get(node));
}
neighborsForCurrentNode.remove(triPanel.name);
// printWriter.print(SmoldynKeyword.neighbors + " " +triPanel.name);
// to allow smoldyn read line length as 256, chop the neighbors to multiple lines
int maxNeighborCount = 4;
//
int count = 0;
for (String neigh : neighborsForCurrentNode) {
if (count % maxNeighborCount == 0) {
printWriter.println();
printWriter.print(SmoldynVCellMapper.SmoldynKeyword.neighbors + " " + triPanel.name);
}
printWriter.print(" " + neigh);
count++;
}
}
printWriter.println();
printWriter.println(SmoldynVCellMapper.SmoldynKeyword.end_surface);
printWriter.println();
}
}
}
use of cbit.vcell.geometry.surface.Polygon in project vcell by virtualcell.
the class RegionImage method sortSurfaceCollection.
public static void sortSurfaceCollection(SurfaceCollection surfCollection) {
// Sort nodes
Object[] nodeObjArr = new Object[surfCollection.getNodes().length];
for (int i = 0; i < nodeObjArr.length; i++) {
Object[] temp = new Object[2];
temp[0] = new Integer(i);
temp[1] = surfCollection.getNodes()[i];
nodeObjArr[i] = temp;
}
Arrays.sort(nodeObjArr, new Comparator<Object>() {
public int compare(Object obj1, Object obj2) {
cbit.vcell.geometry.surface.Node o1 = (cbit.vcell.geometry.surface.Node) ((Object[]) obj1)[1];
cbit.vcell.geometry.surface.Node o2 = (cbit.vcell.geometry.surface.Node) ((Object[]) obj2)[1];
double xdiff = o1.getX() - o2.getX();
double xmin = Math.min(Math.abs(o1.getX()), Math.abs(o2.getX()));
double xlimit = (1e-12 * (xmin >= 1.0 ? (Math.pow(10, (int) Math.log10(xmin) + 1)) : 1));
double ydiff = o1.getY() - o2.getY();
double ymin = Math.min(Math.abs(o1.getY()), Math.abs(o2.getY()));
double ylimit = (1e-12 * (ymin >= 1.0 ? (Math.pow(10, (int) Math.log10(ymin) + 1)) : 1));
double zdiff = o1.getZ() - o2.getZ();
double zmin = Math.min(Math.abs(o1.getZ()), Math.abs(o2.getZ()));
double zlimit = (1e-12 * (zmin >= 1.0 ? (Math.pow(10, (int) Math.log10(zmin) + 1)) : 1));
if (Math.abs(zdiff) < zlimit) {
if (Math.abs(ydiff) < ylimit) {
return (int) Math.signum((Math.abs(xdiff) < xlimit ? 0 : xdiff));
}
return (int) Math.signum((Math.abs(ydiff) < ylimit ? 0 : ydiff));
}
return (int) Math.signum((Math.abs(zdiff) < zlimit ? 0 : zdiff));
}
});
int[] remap = new int[nodeObjArr.length];
Arrays.fill(remap, -1);
cbit.vcell.geometry.surface.Node[] sortedNodes = new cbit.vcell.geometry.surface.Node[nodeObjArr.length];
for (int i = 0; i < nodeObjArr.length; i++) {
sortedNodes[i] = (cbit.vcell.geometry.surface.Node) ((Object[]) nodeObjArr[i])[1];
if (remap[sortedNodes[i].getGlobalIndex()] == -1) {
remap[sortedNodes[i].getGlobalIndex()] = i;
} else {
throw new RuntimeException("SORT error: duplicate nodes");
}
}
// surfCollection.setNodes(sortedNodes);
System.arraycopy(sortedNodes, 0, surfCollection.getNodes(), 0, sortedNodes.length);
HashSet<cbit.vcell.geometry.surface.Node> remapHashSet = new HashSet<cbit.vcell.geometry.surface.Node>();
for (int i = 0; i < surfCollection.getSurfaceCount(); i++) {
Surface surf = surfCollection.getSurfaces(i);
Polygon[] sortedPolygonArr = new Polygon[surf.getPolygonCount()];
for (int j = 0; j < surf.getPolygonCount(); j++) {
Polygon poly = surf.getPolygons(j);
for (int k = 0; k < poly.getNodes().length; k++) {
cbit.vcell.geometry.surface.Node node = poly.getNodes(k);
if (!remapHashSet.contains(node)) {
node.setGlobalIndex(remap[node.getGlobalIndex()]);
remapHashSet.add(node);
}
}
sortedPolygonArr[j] = poly;
}
Arrays.sort(sortedPolygonArr, new Comparator<Polygon>() {
public int compare(Polygon obj1, Polygon obj2) {
Coordinate o1 = ((Quadrilateral) obj1).calculateCentroid();
Coordinate o2 = ((Quadrilateral) obj2).calculateCentroid();
double xdiff = o1.getX() - o2.getX();
double xmin = Math.min(Math.abs(o1.getX()), Math.abs(o2.getX()));
double xlimit = (1e-12 * (xmin >= 1.0 ? (Math.pow(10, (int) Math.log10(xmin) + 1)) : 1));
double ydiff = o1.getY() - o2.getY();
double ymin = Math.min(Math.abs(o1.getY()), Math.abs(o2.getY()));
double ylimit = (1e-12 * (ymin >= 1.0 ? (Math.pow(10, (int) Math.log10(ymin) + 1)) : 1));
double zdiff = o1.getZ() - o2.getZ();
double zmin = Math.min(Math.abs(o1.getZ()), Math.abs(o2.getZ()));
double zlimit = (1e-12 * (zmin >= 1.0 ? (Math.pow(10, (int) Math.log10(zmin) + 1)) : 1));
if (Math.abs(zdiff) < zlimit) {
if (Math.abs(ydiff) < ylimit) {
return (int) Math.signum((Math.abs(xdiff) < xlimit ? 0 : xdiff));
}
return (int) Math.signum((Math.abs(ydiff) < ylimit ? 0 : ydiff));
}
return (int) Math.signum((Math.abs(zdiff) < zlimit ? 0 : zdiff));
}
});
OrigSurface sortedSurface = new OrigSurface(surf.getInteriorRegionIndex(), surf.getExteriorRegionIndex());
for (int k = 0; k < sortedPolygonArr.length; k++) {
int minGlobalIndex = sortedPolygonArr[k].getNodes(0).getGlobalIndex();
// System.out.print("Surf "+i+" poly "+k+" nodeGI - ");
for (int j = 0; j < sortedPolygonArr[k].getNodeCount(); j++) {
if (sortedPolygonArr[k].getNodes(j).getGlobalIndex() < minGlobalIndex) {
minGlobalIndex = sortedPolygonArr[k].getNodes(j).getGlobalIndex();
}
// System.out.print(sortedPolygonArr[k].getNodes(j).getGlobalIndex()+" ");
}
while (sortedPolygonArr[k].getNodes(0).getGlobalIndex() != minGlobalIndex) {
cbit.vcell.geometry.surface.Node lastNode = sortedPolygonArr[k].getNodes(sortedPolygonArr[k].getNodeCount() - 1);
System.arraycopy(sortedPolygonArr[k].getNodes(), 0, sortedPolygonArr[k].getNodes(), 1, sortedPolygonArr[k].getNodeCount() - 1);
sortedPolygonArr[k].getNodes()[0] = lastNode;
}
// System.out.println();
sortedSurface.addPolygon(sortedPolygonArr[k]);
}
surfCollection.setSurfaces(i, sortedSurface);
}
}
use of cbit.vcell.geometry.surface.Polygon in project vcell by virtualcell.
the class ROIMultiPaintManager method getImportSTLtasks.
// private File lastImportDir;
private ArrayList<AsynchClientTask> getImportSTLtasks(File[] selectedFiles, Vect3d primarySampleSizes, Vect3d subSampleOffset) {
// try {
final RegionImage currRegionImage = regionImage;
final TreeMap<Integer, String> newROIindexes = new TreeMap<>();
final TreeMap<BoundingBox, Object[]> regionBounds = new TreeMap<>(new Comparator<BoundingBox>() {
@Override
public int compare(BoundingBox arg0, BoundingBox arg1) {
if (arg1 == arg0) {
return 0;
}
if (arg0.getLoX() == arg1.getLoX() && arg0.getLoY() == arg1.getLoY() && arg0.getLoZ() == arg1.getLoZ() && arg0.getHiX() == arg1.getHiX() && arg0.getHiY() == arg1.getHiY() && arg0.getHiZ() == arg1.getHiZ()) {
return 0;
}
return (arg0.getLoX() >= arg1.getLoX() && arg0.getLoY() >= arg1.getLoY() && arg0.getLoZ() >= arg1.getLoZ() && arg0.getHiX() <= arg1.getHiX() && arg0.getHiY() <= arg1.getHiY() && arg0.getHiZ() <= arg1.getHiZ() ? -1 : +1);
}
});
AsynchClientTask sampleSTLtask = new AsynchClientTask("importSTLtask", AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {
@Override
public void run(Hashtable<String, Object> hashTable) throws Exception {
StatsHelper statsHelper = calcMinMax(selectedFiles);
Vect3d worldOrigin = new Vect3d(statsHelper.xmin, statsHelper.ymin, statsHelper.zmin);
Vect3d worldCollapsedBoundingBox = new Vect3d(statsHelper.xmax - statsHelper.xmin, statsHelper.ymax - statsHelper.ymin, statsHelper.zmax - statsHelper.zmin);
Vect3d scale = new Vect3d(worldCollapsedBoundingBox.getX() / (2.0 * primarySampleSizes.getX()), worldCollapsedBoundingBox.getY() / (2.0 * primarySampleSizes.getY()), worldCollapsedBoundingBox.getZ() / (2.0 * primarySampleSizes.getZ()));
// }
if (scale.getX() != 0 && scale.getX() <= (scale.getY() == 0 ? scale.getX() : scale.getY()) && scale.getX() <= (scale.getZ() == 0 ? scale.getX() : scale.getZ())) {
scale = new Vect3d(scale.getX(), scale.getX(), scale.getX());
} else if (scale.getY() != 0 && scale.getY() <= (scale.getX() == 0 ? scale.getY() : scale.getX()) && scale.getY() <= (scale.getZ() == 0 ? scale.getY() : scale.getZ())) {
scale = new Vect3d(scale.getY(), scale.getY(), scale.getY());
} else if (scale.getZ() != 0 && scale.getZ() <= (scale.getY() == 0 ? scale.getZ() : scale.getY()) && scale.getZ() <= (scale.getX() == 0 ? scale.getZ() : scale.getX())) {
scale = new Vect3d(scale.getZ(), scale.getZ(), scale.getZ());
}
for (int j = 0; j < selectedFiles.length; j++) {
// ClientRequestManager.createSurfaceCollectionFromSurfaceFile(selectedFile);
SurfaceCollection surfaceCollection = statsHelper.recalSurfs.get(j);
for (int i = 0; i < surfaceCollection.getSurfaceCount(); i++) {
Surface surface = surfaceCollection.getSurfaces(i);
// System.out.println("surface "+i);
// Vect3d xyzLow = new Vect3d(surface.getPolygons(0).getNodes(0).getX(),surface.getPolygons(0).getNodes(1).getX(),surface.getPolygons(0).getNodes(2).getX());
// Vect3d xyzHigh = new Vect3d(xyzLow);
ComboboxROIName[] existingRoiNames = overlayEditorPanelJAI.getAllCompositeROINamesAndColors();
int newIndex = getUnusedROIColorIndex(existingRoiNames, newROIindexes.keySet());
newROIindexes.put(newIndex, selectedFiles[j].getName());
byte value = (byte) newIndex;
// overlayEditorPanelJAI.addROIName(roiName, true, roiName, true, value);
for (int k = 0; k < surface.getPolygonCount(); k++) {
Polygon polygon = surface.getPolygons(k);
// for (Node pnode:polygon.getNodes()) {
// xyzLow.set(Math.min(xyzLow.getX(), pnode.getX()),Math.min(xyzLow.getY(), pnode.getY()),Math.min(xyzLow.getZ(), pnode.getZ()));
// xyzHigh.set(Math.max(xyzHigh.getX(), pnode.getX()),Math.max(xyzHigh.getY(), pnode.getY()),Math.max(xyzHigh.getZ(), pnode.getZ()));
// }
// if(k%10000 == 0){
// System.out.println("progress= file "+(j+1)+" of "+selectedFiles.length+" "+((k*100)/surface.getPolygonCount())+"%");
// }
// System.out.println(" polygon "+j+" "+polygon.getNodes(0)+" "+polygon.getNodes(1)+" "+polygon.getNodes(2));
Vect3d line1 = new Vect3d(polygon.getNodes(0));
Vect3d end1 = new Vect3d(polygon.getNodes(2));
Vect3d incr1 = Vect3d.sub(end1, line1);
incr1.unit();
incr1.set(incr1.getX() * scale.getX(), incr1.getY() * scale.getY(), incr1.getZ() * scale.getZ());
Vect3d line2 = new Vect3d(line1);
Vect3d end2 = new Vect3d(polygon.getNodes(1));
Vect3d incr2 = Vect3d.sub(end2, line2);
incr2.unit();
incr2.set(incr2.getX() * scale.getX(), incr2.getY() * scale.getY(), incr2.getZ() * scale.getZ());
while (true) {
Vect3d line3 = new Vect3d(line1);
Vect3d end3 = new Vect3d(line2);
Vect3d incr3 = Vect3d.sub(end3, line3);
if (incr3.length() != 0) {
incr3.unit();
incr3.set(incr3.getX() * scale.getX(), incr3.getY() * scale.getY(), incr3.getZ() * scale.getZ());
}
while (true) {
calcXYZ(line3, worldOrigin, worldCollapsedBoundingBox, primarySampleSizes, subSampleOffset, value);
line3.add(incr3);
Vect3d check = Vect3d.sub(end3, line3);
if ((check.length() == 0) || Math.signum(check.getX()) != Math.signum(incr3.getX()) || Math.signum(check.getY()) != Math.signum(incr3.getY()) || Math.signum(check.getZ()) != Math.signum(incr3.getZ())) {
calcXYZ(end3, worldOrigin, worldCollapsedBoundingBox, primarySampleSizes, subSampleOffset, value);
break;
}
}
if (line1.equals(end1) && line2.equals(end2)) {
break;
}
line1.add(incr1);
Vect3d check = Vect3d.sub(end1, line1);
if (check.length() == 0 || Math.signum(check.getX()) != Math.signum(incr1.getX()) || Math.signum(check.getY()) != Math.signum(incr1.getY()) || Math.signum(check.getZ()) != Math.signum(incr1.getZ())) {
line1 = new Vect3d(end1);
}
line2.add(incr2);
check = Vect3d.sub(end2, line2);
if (check.length() == 0 || Math.signum(check.getX()) != Math.signum(incr2.getX()) || Math.signum(check.getY()) != Math.signum(incr2.getY()) || Math.signum(check.getZ()) != Math.signum(incr2.getZ())) {
line2 = new Vect3d(end2);
}
}
}
// regionBounds.put(new BoundingBox(xyzLow.getX(), xyzHigh.getX(),xyzLow.getY(), xyzHigh.getY(),xyzLow.getZ(), xyzHigh.getZ()),new Object[] {new int[] {j,i},roiName});
}
}
// for(BoundingBox bb:regionBounds.keySet()){
// System.out.println(((int[])regionBounds.get(bb)[0])[0]+","+((int[])regionBounds.get(bb)[0])[1]+" name="+((String)regionBounds.get(bb)[1])+" bound="+bb.getLoX()+","+bb.getHiX()+" "+bb.getLoY()+","+bb.getHiY()+" "+bb.getLoZ()+","+bb.getHiZ());
// }
updateAuxiliaryInfo(originalISize, getClientTaskStatusSupport());
// }
}
};
final int[] checkHolder = new int[1];
final String[] cleanupHolder = new String[1];
final String YES = "Yes";
final ComboboxROIName[] cbxHolder = new ComboboxROIName[1];
final String CHECK = "check";
AsynchClientTask addROInamesTask = new AsynchClientTask("Add new ROI names...", AsynchClientTask.TASKTYPE_SWING_BLOCKING) {
@Override
public void run(Hashtable<String, Object> hashTable) throws Exception {
int i = 0;
for (Integer newIndex : newROIindexes.keySet()) {
String roiName = FilenameUtils.getBaseName(newROIindexes.get(newIndex));
roiName = TokenMangler.fixTokenStrict(roiName, 6);
roiName += "_" + i++;
overlayEditorPanelJAI.addROIName(roiName, true, roiName, true, newIndex);
}
cleanupHolder[0] = DialogUtils.showWarningDialog(overlayEditorPanelJAI, "Cleanup imported geometry?", new String[] { YES, "No" }, YES);
if (!YES.equals(cleanupHolder[0])) {
return;
}
checkHolder[0] = getUnusedROIColorIndex(overlayEditorPanelJAI.getAllCompositeROINamesAndColors(), null);
overlayEditorPanelJAI.addROIName(CHECK, true, CHECK, true, checkHolder[0]);
ComboboxROIName[] temp = overlayEditorPanelJAI.getAllCompositeROINamesAndColors();
for (ComboboxROIName cbx : temp) {
if (cbx.getROIName().equals(CHECK)) {
cbxHolder[0] = cbx;
break;
}
}
}
};
// AsynchClientTask removeCheckROI = new AsynchClientTask("Remove check...",AsynchClientTask.TASKTYPE_SWING_BLOCKING) {
//
// @Override
// public void run(Hashtable<String, Object> hashTable) throws Exception {
// ComboboxROIName[] regNames = overlayEditorPanelJAI.getAllCompositeROINamesAndColors();
// for(ComboboxROIName cbx:regNames){
// if(cbx.get)
// }
// }
// };
final AsynchClientTask mergeBackgroundRegions = new AsynchClientTask("Merge background regions...", AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {
@Override
public void run(Hashtable<String, Object> hashTable) throws Exception {
if (!YES.equals(cleanupHolder[0])) {
return;
}
// long start = System.currentTimeMillis();
// while(currRegionImage == regionImage){
// int dur = (int)((System.currentTimeMillis()-start)/1000);
// if(dur > 10){
// return;
// }
// System.out.println("trying..."+dur);
// Thread.sleep(1000);
// }
final TreeMap<RegionInfo, Vect3d[]> regTobb = new TreeMap<>(new Comparator<RegionInfo>() {
@Override
public int compare(RegionInfo arg0, RegionInfo arg1) {
return arg0.getRegionIndex() - arg1.getRegionIndex();
}
});
Hashtable<String, Object> newRegionImgHolder = new Hashtable<>();
createRegionImageTask.run(newRegionImgHolder);
RegionImage newRegionImage = (RegionImage) newRegionImgHolder.get(LOCAL_REGION_IMAGE);
// ArrayList<RegionInfo> remainingRegionInfos = new ArrayList<>(Arrays.asList(newRegionImage.getRegionInfos()));
// BoundingBox bb = new BoundingBox(0, newRegionImage.getNumX()-1, 0, newRegionImage.getNumY()-1, 0, newRegionImage.getNumZ()-1);
// int currIndex = 0;
// for (int z = 0; z <= bb.getHiZ(); z++) {
// for (int y = 0; y <= bb.getHiY(); y++) {
// for (int x = 0; x <= bb.getHiX(); x++) {
// ArrayList<RegionInfo> tempRegionInfos = (ArrayList<RegionInfo>)remainingRegionInfos.clone();
// if(x==0 || x == bb.getHiX() || y==0 || y == bb.getHiY() || z==0 || z == bb.getHiZ()){
// for(RegionInfo reg:tempRegionInfos){
// if(reg.getPixelValue() == 0 && reg.isIndexInRegion(currIndex)){
// remainingRegionInfos.remove(reg);
// break;
// }
// }
// }else{
// for(RegionInfo reg:tempRegionInfos){
// if(reg.getPixelValue() == 0 && reg.isIndexInRegion(currIndex)){
// remainingRegionInfos.remove(reg);
// break;
// }
// }
// }
// currIndex++;
// }
// }
// }
// Remove unfilled regions (background) that touch the whole region border
int allPixCnt = 0;
ArrayList<RegionInfo> excluded = new ArrayList<>();
for (int z = 0; z < newRegionImage.getNumZ(); z++) {
for (int y = 0; y < newRegionImage.getNumY(); y++) {
for (int x = 0; x < newRegionImage.getNumX(); x++) {
RegionInfo regInfo = newRegionImage.getRegionInfoFromOffset(allPixCnt);
allPixCnt += 1;
if (excluded.contains(regInfo)) {
continue;
}
if (regInfo.getPixelValue() == 0 && (x == 0 || x == newRegionImage.getNumX() - 1 || y == 0 || y == newRegionImage.getNumY() - 1 || z == 0 || z == newRegionImage.getNumZ() - 1)) {
excluded.add(regInfo);
if (regTobb.containsKey(regInfo)) {
regTobb.remove(regInfo);
}
continue;
}
Vect3d[] bounds = regTobb.get(regInfo);
if (bounds == null) {
bounds = new Vect3d[] { new Vect3d(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY), new Vect3d(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY) };
regTobb.put(regInfo, bounds);
}
bounds[0].set(Math.min(bounds[0].getX(), x), Math.min(bounds[0].getY(), y), Math.min(bounds[0].getZ(), z));
bounds[1].set(Math.max(bounds[1].getX(), x), Math.max(bounds[1].getY(), y), Math.max(bounds[1].getZ(), z));
}
}
}
// ArrayList<Object[]> tree = new ArrayList<>();//arraylist<object[]>,regionInfo
// ArrayList<RegionInfo> bucket = new ArrayList<>(regTobb.keySet());
// tree.add(new Object[] {null,regTobb.firstKey()});
// // ArrayList<Object[]> stack = new ArrayList<>();
// // stack.add(tree.get(0));
// while(bucket.size() > 0){
// // Object[] next = stack.remove(0);
// RegionInfo next = bucket.remove(0);
// Vect3d[] o2v = (Vect3d[])regTobb.get(regTobb.get(next));
// for(Object[] objArr:tree){
// // if(((RegionInfo)next[1]).getRegionIndex() != reg.getRegionIndex()){
// // for(RegionInfo bucketReg:bucket){
// Vect3d[] o1v = (Vect3d[])regTobb.get(objArr[1]);
// if( o1v[0].getX() >= o2v[0].getX() && o1v[0].getX() <= o2v[1].getX() &&
// o1v[0].getY() >= o2v[0].getY() && o1v[0].getY() <= o2v[1].getY() &&
// o1v[0].getZ() >= o2v[0].getZ() && o1v[0].getZ() <= o2v[1].getZ()
// ){
//
// }
// // }
// // }
// }
// }
// Sort filled/unfilled regions by boundingbox
ArrayList<RegionInfo> sortedRegionInfos = new ArrayList<>(Arrays.asList(regTobb.keySet().toArray(new RegionInfo[0])));
Collections.sort(sortedRegionInfos, new Comparator<RegionInfo>() {
@Override
public int compare(RegionInfo o1, RegionInfo o2) {
Vect3d[] o1v = (Vect3d[]) regTobb.get(o1);
Vect3d[] o2v = (Vect3d[]) regTobb.get(o2);
// }
if (o1v[0].getX() == o2v[0].getX() && o1v[1].getX() == o2v[1].getX() && o1v[0].getY() == o2v[0].getY() && o1v[1].getY() == o2v[1].getY() && o1v[0].getZ() >= o2v[0].getZ() && o1v[1].getZ() == o2v[1].getZ()) {
System.out.println(o1.getRegionIndex() + " == " + o2.getRegionIndex());
return 0;
} else if (o1v[0].getX() >= o2v[0].getX() && o1v[0].getX() <= o2v[1].getX() && // o1v[1].getX() >= o2v[0].getX() && o1v[1].getX() <= o2v[1].getX() &&
o1v[0].getY() >= o2v[0].getY() && o1v[0].getY() <= o2v[1].getY() && // o1v[1].getY() >= o2v[0].getY() && o1v[1].getY() <= o2v[1].getY() &&
o1v[0].getZ() >= o2v[0].getZ() && o1v[0].getZ() <= o2v[1].getZ()) // && o1v[1].getZ() >= o2v[0].getZ() && o1v[1].getZ() <= o2v[1].getZ()
{
System.out.println(o1.getRegionIndex() + " > " + o2.getRegionIndex());
return 1;
} else if (o1v[0].getX() <= o2v[0].getX() && o1v[1].getX() >= o2v[1].getX() && o1v[0].getY() <= o2v[0].getY() && o1v[1].getY() >= o2v[1].getY() && o1v[0].getZ() <= o2v[0].getZ() && o1v[1].getZ() >= o2v[1].getZ()) // (o1v[0].getX() < o2v[0].getX() && o1v[1].getX() < o2v[0].getX()) ||
// (o1v[0].getX() > o2v[1].getX() && o1v[1].getX() > o2v[1].getX()) ||
//
// (o1v[0].getY() < o2v[0].getY() && o1v[1].getY() < o2v[0].getY()) ||
// (o1v[0].getY() > o2v[1].getY() && o1v[1].getY() > o2v[1].getY()) ||
//
// (o1v[0].getZ() < o2v[0].getZ() && o1v[1].getZ() < o2v[0].getZ()) ||
// (o1v[0].getZ() > o2v[1].getZ() && o1v[1].getZ() > o2v[1].getZ())
//
// /* (o1v[0].getX() < o2v[0].getX() && o1v[0].getX() > o2v[1].getX() &&
// o1v[1].getX() < o2v[0].getX() && o1v[1].getX() > o2v[1].getX()) ||
// (o1v[0].getY() < o2v[0].getY() && o1v[0].getY() > o2v[1].getY() &&
// o1v[1].getY() < o2v[0].getY() && o1v[1].getY() > o2v[1].getY()) ||
// (o1v[0].getZ() < o2v[0].getZ() && o1v[0].getZ() > o2v[1].getZ() &&
// o1v[1].getZ() < o2v[0].getZ() && o1v[1].getZ() > o2v[1].getZ())*/
{
System.out.println(o1.getRegionIndex() + " <a " + o2.getRegionIndex());
return -1;
} else if ((o1v[0].getX() < o2v[0].getX() && o1v[1].getX() < o2v[0].getX()) || (o1v[0].getX() > o2v[1].getX() && o1v[1].getX() > o2v[1].getX()) || (o1v[0].getY() < o2v[0].getY() && o1v[1].getY() < o2v[0].getY()) || (o1v[0].getY() > o2v[1].getY() && o1v[1].getY() > o2v[1].getY()) || (o1v[0].getZ() < o2v[0].getZ() && o1v[1].getZ() < o2v[0].getZ()) || (o1v[0].getZ() > o2v[1].getZ() && o1v[1].getZ() > o2v[1].getZ())) {
System.out.println(o1.getRegionIndex() + " <b " + o2.getRegionIndex());
return -1;
}
System.out.println("inconclusive " + o1.getRegionIndex() + " == " + o2.getRegionIndex());
return 0;
// System.out.println("inconclusive "+o1.getRegionIndex()+" "+(o1.getNumPixels() - o2.getNumPixels()<0?"<":(o1.getNumPixels() - o2.getNumPixels()>0?">":"=="))+" "+o2.getRegionIndex());
// return o1.getNumPixels() - o2.getNumPixels();
// System.out.println("inconclusive "+o1.getRegionIndex()+" "+(o1.getRegionIndex()-o2.getRegionIndex()<0?"<":(o1.getRegionIndex()-o2.getRegionIndex()>0?">":"=="))+" "+o2.getRegionIndex());
// return o1.getRegionIndex()-o2.getRegionIndex();
// if(o1v[0].getZ() != o2v[0].getZ()){
// return (int)(o1v[0].getZ() - o2v[0].getZ());
// }else if(o1v[0].getY() != o2v[0].getY()){
// return (int)(o1v[0].getY() - o2v[0].getY());
// }else if(o1v[0].getX() != o2v[0].getX()){
// return (int)(o1v[0].getX() - o2v[0].getX());
// }else
// return 0;
}
});
// Convert unfilled regions into parent regions
int currColorIndex = -1;
// for (int i = sortedRegionInfos.size()-1; i >= 0; i--) {
for (int i = 0; i < sortedRegionInfos.size(); i++) {
RegionInfo regInfo = sortedRegionInfos.get(i);
Vect3d[] o1v = (Vect3d[]) regTobb.get(regInfo);
ComboboxROIName cbxroiName = overlayEditorPanelJAI.getComboboxROIName(regInfo);
// System.out.println(cbxroiName+" colorIndex="+regInfo.getPixelValue()+" regIndex="+regInfo.getRegionIndex()+" pixels="+regInfo.getNumPixels()+" "+o1v[0]+" "+o1v[1]);
if (cbxroiName != null) {
currColorIndex = cbxroiName.getContrastColorIndex();
continue;
}
allPixCnt = 0;
for (int z = 0; z < newRegionImage.getNumZ(); z++) {
for (int y = 0; y < newRegionImage.getNumY(); y++) {
for (int x = 0; x < newRegionImage.getNumX(); x++) {
if (regInfo.isIndexInRegion(allPixCnt)) {
BufferedImage plane = roiComposite[z];
byte[] data = ((DataBufferByte) plane.getRaster().getDataBuffer()).getData();
data[x + (plane.getWidth() * y)] = (byte) currColorIndex;
}
allPixCnt++;
}
}
}
}
// for(RegionInfo regInfo:regTobb.keySet()){
// Vect3d[] varr = regTobb.get(regInfo);
// ComboboxROIName cbxroiName = overlayEditorPanelJAI.getComboboxROIName(regInfo);
// regionBounds.put(new BoundingBox(varr[0].getX(), varr[1].getX(),varr[0].getY(), varr[1].getY(),varr[0].getZ(), varr[1].getZ()),new Object[] {cbxroiName});
// }
// for(BoundingBox bb:regionBounds.keySet()){
// ComboboxROIName cbxroiName = ((ComboboxROIName)regionBounds.get(bb)[0]);
// System.out.println(cbxroiName+" bound="+bb.getLoX()+","+bb.getHiX()+" "+bb.getLoY()+","+bb.getHiY()+" "+bb.getLoZ()+","+bb.getHiZ());
//
// }
// Make sure every pixel has only 1 kind of neighbor
// createRegionImageTask.run(newRegionImgHolder);
// newRegionImage = (RegionImage)newRegionImgHolder.get(LOCAL_REGION_IMAGE);
boolean bHasCheck = false;
VCImage checkImage = ROIMultiPaintManager.createVCImageFromBufferedImages(ROIMultiPaintManager.DEFAULT_EXTENT, roiComposite);
newRegionImage = new RegionImage(checkImage, 0, /*0 means generate no surfacecollection*/
checkImage.getExtent(), ROIMultiPaintManager.DEFAULT_ORIGIN, RegionImage.NO_SMOOTHING, null);
TreeSet<Integer> diffNeighbors = new TreeSet<>();
int[][] offsets = new int[][] { { -1, 0, 0 }, { 1, 0, 0 }, { 0, -1, 0 }, { 0, 1, 0 }, { 0, 0, -1 }, { 0, 0, 1 } };
for (int z = 0; z < newRegionImage.getNumZ(); z++) {
BufferedImage plane = roiComposite[z];
byte[] data = ((DataBufferByte) plane.getRaster().getDataBuffer()).getData();
allPixCnt = 0;
for (int y = 0; y < newRegionImage.getNumY(); y++) {
for (int x = 0; x < newRegionImage.getNumX(); x++) {
diffNeighbors.clear();
RegionInfo regInfo = newRegionImage.getRegionInfoFromOffset(allPixCnt);
if (regInfo.getPixelValue() == 0) {
allPixCnt++;
continue;
}
if (x == 19 && y == 15) {
System.out.println("here");
}
for (int i = 0; i < offsets.length; i++) {
int tmpx = offsets[i][0] + x;
if (tmpx < 0 || tmpx >= newRegionImage.getNumX()) {
continue;
}
int tmpy = offsets[i][1] + y;
if (tmpy < 0 || tmpy >= newRegionImage.getNumY()) {
continue;
}
int tmpz = offsets[i][2] + z;
if (tmpz < 0 || tmpz >= newRegionImage.getNumZ()) {
continue;
}
BufferedImage plane0 = roiComposite[tmpz];
byte[] data0 = ((DataBufferByte) plane.getRaster().getDataBuffer()).getData();
byte shiftb = data0[tmpx + (tmpy * newRegionImage.getNumX())];
if (shiftb != regInfo.getPixelValue()) {
diffNeighbors.add(0x000000FF & shiftb);
}
}
// for (int z0 = -1; z0 <= 1; z0+=2) {
// int tmpz = z+z0;
// if(tmpz < 0 || tmpz >= newRegionImage.getNumZ()){
// continue;
// }
// // tmpz = allPixCnt+(z0*newRegionImage.getNumXY());
// BufferedImage plane0 = roiComposite[z+z0];
// byte[] data0 = ((DataBufferByte)plane.getRaster().getDataBuffer()).getData();
// for (int y0 = -1; y0 <= 1; y0+=2) {
// int tmpy = y+y0;
// if(tmpy < 0 || tmpy >= newRegionImage.getNumY()){
// continue;
// }
// // tmpy = tmpz+(y0*newRegionImage.getNumX());
// for (int x0 = -1; x0 <= 1; x0+=2) {
// int tmpx = x+x0;
// if(tmpx < 0 || tmpx >= newRegionImage.getNumX()){
// continue;
// }
// // tmpx = tmpy+(x0);
// byte shiftb = data0[tmpx+(tmpy*newRegionImage.getNumX())];
// if(shiftb != regInfo.getPixelValue()){
// diffNeighbors.add(0x000000FF&shiftb);
// }
// }
// }
// }
allPixCnt++;
if (diffNeighbors.size() > 1) {
bHasCheck = true;
data[x + y * newRegionImage.getNumX()] = (byte) checkHolder[0];
}
}
}
}
if (!bHasCheck && cbxHolder[0] != null) {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
overlayEditorPanelJAI.deleteROIName(cbxHolder[0]);
}
});
}
}
};
// }).start();
return new ArrayList<>(Arrays.asList(new AsynchClientTask[] { sampleSTLtask, addROInamesTask, mergeBackgroundRegions, getUpdateDisplayAfterCropTask() }));
// JFileChooser importJFC = new JFileChooser(ClientRequestManager.getPreferredPath(UserPreferences.getLastUserPreferences()));
// importJFC.setDialogTitle("Choose .stl file to import");
// importJFC.setMultiSelectionEnabled(true);
// int result = importJFC.showOpenDialog(overlayEditorPanelJAI);
// if(result == JFileChooser.APPROVE_OPTION){
// File[] selectedFiles = importJFC.getSelectedFiles();
// if(statsHelper == null){
// statsHelper = calcMinMax(selectedFiles);
// }
// ClientRequestManager.setPreferredPath(UserPreferences.getLastUserPreferences(), selectedFiles[0]);
//
// Vect3d worldOrigin = new Vect3d(statsHelper.xmin, statsHelper.ymin, statsHelper.zmin);
// Vect3d worldCollapsedBoundingBox = new Vect3d(statsHelper.xmax-statsHelper.xmin, statsHelper.ymax-statsHelper.ymin, statsHelper.zmax-statsHelper.zmin);
// Vect3d scale = new Vect3d(worldCollapsedBoundingBox.getX()/(2.0*primarySampleSizes.getX()), worldCollapsedBoundingBox.getY()/(2.0*primarySampleSizes.getY()), worldCollapsedBoundingBox.getZ()/(2.0*primarySampleSizes.getZ()));
// // if(scale.getX() !=0 && scale.getX() >= (scale.getY()==0?scale.getX():scale.getY()) && scale.getX() >= (scale.getZ()==0?scale.getX():scale.getZ())){
// // scale = new Vect3d(scale.getX(), scale.getX(), scale.getX());
// // }else if(scale.getY() !=0 && scale.getY() >= (scale.getX()==0?scale.getY():scale.getX()) && scale.getY() >= (scale.getZ()==0?scale.getY():scale.getZ())){
// // scale = new Vect3d(scale.getY(), scale.getY(), scale.getY());
// // }else if(scale.getZ() !=0 && scale.getZ() >= (scale.getY()==0?scale.getZ():scale.getY()) && scale.getZ() >= (scale.getX()==0?scale.getZ():scale.getX())){
// // scale = new Vect3d(scale.getZ(), scale.getZ(), scale.getZ());
// // }
// if(scale.getX() !=0 && scale.getX() <= (scale.getY()==0?scale.getX():scale.getY()) && scale.getX() <= (scale.getZ()==0?scale.getX():scale.getZ())){
// scale = new Vect3d(scale.getX(), scale.getX(), scale.getX());
// }else if(scale.getY() !=0 && scale.getY() <= (scale.getX()==0?scale.getY():scale.getX()) && scale.getY() <= (scale.getZ()==0?scale.getY():scale.getZ())){
// scale = new Vect3d(scale.getY(), scale.getY(), scale.getY());
// }else if(scale.getZ() !=0 && scale.getZ() <= (scale.getY()==0?scale.getZ():scale.getY()) && scale.getZ() <= (scale.getX()==0?scale.getZ():scale.getX())){
// scale = new Vect3d(scale.getZ(), scale.getZ(), scale.getZ());
// }
// TreeMap<BoundingBox,Object[]> regionBounds = new TreeMap<>(new Comparator<BoundingBox>() {
// @Override
// public int compare(BoundingBox arg0, BoundingBox arg1) {
// if(arg1 == arg0){
// return 0;
// }
// if(arg0.getLoX() == arg1.getLoX() &&
// arg0.getLoY() == arg1.getLoY() &&
// arg0.getLoZ() == arg1.getLoZ() &&
// arg0.getHiX() == arg1.getHiX() &&
// arg0.getHiY() == arg1.getHiY() &&
// arg0.getHiZ() == arg1.getHiZ()){
// return 0;
// }
// return (arg0.getLoX() >= arg1.getLoX() &&
// arg0.getLoY() >= arg1.getLoY() &&
// arg0.getLoZ() >= arg1.getLoZ() &&
// arg0.getHiX() <= arg1.getHiX() &&
// arg0.getHiY() <= arg1.getHiY() &&
// arg0.getHiZ() <= arg1.getHiZ()?-1:+1);
// }
// });
// for (int j = 0; j < selectedFiles.length; j++) {
// SurfaceCollection surfaceCollection = statsHelper.recalSurfs.get(j);//ClientRequestManager.createSurfaceCollectionFromSurfaceFile(selectedFile);
// for (int i = 0; i < surfaceCollection.getSurfaceCount(); i++) {
// Surface surface = surfaceCollection.getSurfaces(i);
// System.out.println("surface "+i);
// Vect3d xyzLow = new Vect3d(surface.getPolygons(0).getNodes(0).getX(),surface.getPolygons(0).getNodes(1).getX(),surface.getPolygons(0).getNodes(2).getX());
// Vect3d xyzHigh = new Vect3d(xyzLow);
// ComboboxROIName[] existingRoiNames = overlayEditorPanelJAI.getAllCompositeROINamesAndColors();
// byte value = (byte)getUnusedROIColorIndex(existingRoiNames);
// String roiName = FilenameUtils.getBaseName(selectedFiles[j].getName());
// roiName = TokenMangler.fixTokenStrict(roiName, 6);
// roiName+= "_"+i;
// overlayEditorPanelJAI.addROIName(roiName, true, roiName, true, value);
// for (int k = 0; k < surface.getPolygonCount(); k++){
// Polygon polygon = surface.getPolygons(k);
// // for (Node pnode:polygon.getNodes()) {
// // xyzLow.set(Math.min(xyzLow.getX(), pnode.getX()),Math.min(xyzLow.getY(), pnode.getY()),Math.min(xyzLow.getZ(), pnode.getZ()));
// // xyzHigh.set(Math.max(xyzHigh.getX(), pnode.getX()),Math.max(xyzHigh.getY(), pnode.getY()),Math.max(xyzHigh.getZ(), pnode.getZ()));
// // }
// if(k%10000 == 0){
// System.out.println("progress= file "+(j+1)+" of "+selectedFiles.length+" "+((k*100)/surface.getPolygonCount())+"%");
// }
// // System.out.println(" polygon "+j+" "+polygon.getNodes(0)+" "+polygon.getNodes(1)+" "+polygon.getNodes(2));
// Vect3d line1 = new Vect3d(polygon.getNodes(0));
// Vect3d end1 = new Vect3d(polygon.getNodes(2));
// Vect3d incr1 = Vect3d.sub(end1,line1);
// incr1.unit();
// incr1.set(incr1.getX()*scale.getX(), incr1.getY()*scale.getY(), incr1.getZ()*scale.getZ());
// Vect3d line2 = new Vect3d(line1);
// Vect3d end2 = new Vect3d(polygon.getNodes(1));
// Vect3d incr2 = Vect3d.sub(end2,line2);
// incr2.unit();
// incr2.set(incr2.getX()*scale.getX(), incr2.getY()*scale.getY(), incr2.getZ()*scale.getZ());
// while(true){
// Vect3d line3 = new Vect3d(line1);
// Vect3d end3 = new Vect3d(line2);
// Vect3d incr3 = Vect3d.sub(end3,line3);
// if(incr3.length() != 0){
// incr3.unit();
// incr3.set(incr3.getX()*scale.getX(), incr3.getY()*scale.getY(), incr3.getZ()*scale.getZ());
// }
// while(true){
// calcXYZ(line3, worldOrigin, worldCollapsedBoundingBox, primarySampleSizes,subSampleOffset,value);
// line3.add(incr3);
// Vect3d check = Vect3d.sub(end3, line3);
// if((check.length()==0) || Math.signum(check.getX()) != Math.signum(incr3.getX()) || Math.signum(check.getY()) != Math.signum(incr3.getY()) || Math.signum(check.getZ()) != Math.signum(incr3.getZ())){
// calcXYZ(end3, worldOrigin, worldCollapsedBoundingBox, primarySampleSizes,subSampleOffset,value);
// break;
// }
// }
// if(line1.equals(end1) && line2.equals(end2)){
// break;
// }
//
// line1.add(incr1);
// Vect3d check = Vect3d.sub(end1, line1);
// if(check.length()==0 || Math.signum(check.getX()) != Math.signum(incr1.getX()) || Math.signum(check.getY()) != Math.signum(incr1.getY()) || Math.signum(check.getZ()) != Math.signum(incr1.getZ())){
// line1 = new Vect3d(end1);
// }
// line2.add(incr2);
// check = Vect3d.sub(end2, line2);
// if(check.length()==0 || Math.signum(check.getX()) != Math.signum(incr2.getX()) || Math.signum(check.getY()) != Math.signum(incr2.getY()) || Math.signum(check.getZ()) != Math.signum(incr2.getZ())){
// line2 = new Vect3d(end2);
// }
// }
// }
// // regionBounds.put(new BoundingBox(xyzLow.getX(), xyzHigh.getX(),xyzLow.getY(), xyzHigh.getY(),xyzLow.getZ(), xyzHigh.getZ()),new Object[] {new int[] {j,i},roiName});
// }
// }
// // for(BoundingBox bb:regionBounds.keySet()){
// // System.out.println(((int[])regionBounds.get(bb)[0])[0]+","+((int[])regionBounds.get(bb)[0])[1]+" name="+((String)regionBounds.get(bb)[1])+" bound="+bb.getLoX()+","+bb.getHiX()+" "+bb.getLoY()+","+bb.getHiY()+" "+bb.getLoZ()+","+bb.getHiZ());
// // }
// updateAuxiliaryInfo(originalISize, null);
// getUpdateDisplayAfterCropTask().run(null);
//
// new Thread(new Runnable() {
// @Override
// public void run() {
// while(ClientTaskDispatcher.isBusy()){
// try {
// Thread.sleep(1000);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
// TreeMap<RegionInfo, Vect3d[]> regTobb = new TreeMap<>(new Comparator<RegionInfo>() {
// @Override
// public int compare(RegionInfo arg0, RegionInfo arg1) {
// return arg0.getRegionIndex()-arg1.getRegionIndex();
// }
// });
// int allPixCnt = regionImage.getNumX()*regionImage.getNumY()*regionImage.getNumZ();
// for (int z = 0; z < regionImage.getNumZ(); z++) {
// for (int y = 0; y < regionImage.getNumY(); y++) {
// for (int x = 0; x < regionImage.getNumX(); x++) {
// allPixCnt-=1;
// RegionInfo regInfo = regionImage.getRegionInfoFromOffset(allPixCnt);
// Vect3d[] bounds = regTobb.get(regInfo);
// if(bounds == null){
// bounds = new Vect3d[] {new Vect3d(),new Vect3d()};
// regTobb.put(regInfo, bounds);
// }
// bounds[0].set(Math.min(bounds[0].getX(), x),Math.min(bounds[0].getY(), y),Math.min(bounds[0].getZ(), z));
// bounds[1].set(Math.max(bounds[1].getX(), x),Math.max(bounds[1].getY(), y),Math.max(bounds[1].getZ(), z));
//
// }
// }
// }
// for(RegionInfo regInfo:regTobb.keySet()){
// Vect3d[] varr = regTobb.get(regInfo);
// ComboboxROIName cbxroiName = overlayEditorPanelJAI.getComboboxROIName(regInfo);
// regionBounds.put(new BoundingBox(varr[0].getX(), varr[1].getX(),varr[0].getY(), varr[1].getY(),varr[0].getZ(), varr[1].getZ()),new Object[] {cbxroiName});
// }
// for(BoundingBox bb:regionBounds.keySet()){
// ComboboxROIName cbxroiName = ((ComboboxROIName)regionBounds.get(bb)[0]);
// System.out.println(cbxroiName+" bound="+bb.getLoX()+","+bb.getHiX()+" "+bb.getLoY()+","+bb.getHiY()+" "+bb.getLoZ()+","+bb.getHiZ());
//
// }
// }
// }).start();
// }
// }catch (Exception e) {
// e.printStackTrace();
// DialogUtils.showErrorDialog(overlayEditorPanelJAI, e.getMessage());
// }
}
use of cbit.vcell.geometry.surface.Polygon in project vcell by virtualcell.
the class SurfaceRenderer method createScreenPolygon.
/**
* Insert the method's description here.
* Creation date: (10/11/2005 8:12:13 AM)
* @return cbit.vcell.geometry.surface.Polygon
*/
private java.awt.Polygon createScreenPolygon(cbit.vcell.geometry.surface.Polygon polygon, int[] xPoints, int[] yPoints, double xOffset, double xScale, double yOffset, double yScale) {
Vect3d point = new Vect3d();
Vect3d proj = new Vect3d();
int polygonDim = polygon.getNodeCount();
for (int k = 0; k < polygonDim; k++) {
cbit.vcell.geometry.surface.Node node = polygon.getNodes(k);
point.set(node.getX(), node.getY(), node.getZ());
getTrackball().getCamera().projectPoint(point, proj);
xPoints[k] = (int) (xOffset + xScale * proj.getX());
yPoints[k] = (int) (yOffset + yScale * proj.getY());
}
return new java.awt.Polygon(xPoints, yPoints, 4);
}
Aggregations