Search in sources :

Example 1 with VisLine

use of org.vcell.vis.vismesh.thrift.VisLine in project vcell by virtualcell.

the class ChomboMeshMapping method cropQuads.

private void cropQuads(VisMesh visMesh) {
    if (visMesh.getDimension() != 2) {
        throw new RuntimeException("expecting 2D mesh");
    }
    List<VisPolygon> polygons = visMesh.getPolygons();
    List<VisPoint> points = visMesh.getPoints();
    List<VisLine> lines = visMesh.getVisLines();
    for (VisPolygon polygon : polygons) {
        List<Integer> polygonPointIndices = polygon.getPointIndices();
        double fraction = polygon.getChomboVolumeIndex().getFraction();
        if (fraction < 1.0 && polygonPointIndices.size() == 4) {
            int p0 = polygonPointIndices.get(0);
            int p1 = polygonPointIndices.get(1);
            int p2 = polygonPointIndices.get(2);
            int p3 = polygonPointIndices.get(3);
            VisPoint point0 = points.get(p0);
            VisPoint point1 = points.get(p1);
            VisPoint point2 = points.get(p2);
            VisPoint point3 = points.get(p3);
            double p0x = point0.x;
            double p0y = point0.y;
            double p1x = point1.x;
            double p1y = point1.y;
            double p2x = point2.x;
            double p2y = point2.y;
            double p3x = point3.x;
            double p3y = point3.y;
            double pLowX = p0x;
            double pLowY = p0y;
            double pHiX = p2x;
            double pHiY = p2y;
            for (VisLine segment : lines) {
                VisPoint segpoint1 = points.get(segment.getP1());
                double s1x = segpoint1.x;
                double s1y = segpoint1.y;
                if (s1x <= pHiX && s1x >= pLowX && s1y <= pHiY && s1y >= pLowY) {
                    VisPoint segpoint2 = points.get(segment.getP2());
                    double s2x = segpoint2.x;
                    double s2y = segpoint2.y;
                    if (s2x <= pHiX && s2x >= pLowX && s2y <= pHiY && s2y >= pLowY) {
                        // 
                        if (p0x == s1x && p0y == s2y) {
                            if (fraction > 0.5) {
                                polygon.setPointIndices(Arrays.asList(new Integer[] { segment.getP2(), segment.getP1(), p1, p2, p3 }));
                            } else {
                                polygon.setPointIndices(Arrays.asList(new Integer[] { segment.getP2(), p0, segment.getP1() }));
                            }
                        } else if (p0x == s2x && p0y == s1y) {
                            if (fraction > 0.5) {
                                polygon.setPointIndices(Arrays.asList(new Integer[] { segment.getP1(), segment.getP2(), p1, p2, p3 }));
                            } else {
                                polygon.setPointIndices(Arrays.asList(new Integer[] { segment.getP1(), p0, segment.getP2() }));
                            }
                        // case 2) { (remove point one && replace with segment
                        // 
                        // 0      3                         0      3
                        // s1          =>  s1        or     s1
                        // 1  s2  2        1  s2               s2  2
                        // 
                        } else if (p1x == s1x && p1y == s2y) {
                            if (fraction > 0.5) {
                                polygon.setPointIndices(Arrays.asList(new Integer[] { p0, segment.getP1(), segment.getP2(), p2, p3 }));
                            } else {
                                polygon.setPointIndices(Arrays.asList(new Integer[] { segment.getP1(), p1, segment.getP2() }));
                            }
                        } else if (p1x == s2x && p1y == s1y) {
                            if (fraction > 0.5) {
                                polygon.setPointIndices(Arrays.asList(new Integer[] { p0, segment.getP2(), segment.getP1(), p2, p3 }));
                            } else {
                                polygon.setPointIndices(Arrays.asList(new Integer[] { segment.getP2(), p1, segment.getP1() }));
                            }
                        // case 3) { (remove point two && replace with segment
                        // 
                        // 0      3                           0      3
                        // s2   =>         s2   or            s2
                        // 1  s1  2           s1  2           1  s1
                        // 
                        } else if (p2x == s1x && p2y == s2y) {
                            if (fraction > 0.5) {
                                polygon.setPointIndices(Arrays.asList(new Integer[] { p0, p1, segment.getP2(), segment.getP1(), p3 }));
                            } else {
                                polygon.setPointIndices(Arrays.asList(new Integer[] { segment.getP2(), p2, segment.getP1() }));
                            }
                        } else if (p2x == s2x && p2y == s1y) {
                            if (fraction > 0.5) {
                                polygon.setPointIndices(Arrays.asList(new Integer[] { p0, p1, segment.getP1(), segment.getP2(), p3 }));
                            } else {
                                polygon.setPointIndices(Arrays.asList(new Integer[] { segment.getP1(), p2, segment.getP2() }));
                            }
                        // case 4) { (remove point three && replace with segment
                        // 
                        // 0  s2  3           s2  3           0  s2
                        // s1   =>         s1   or            s1
                        // 1      2                           1      2
                        // 
                        } else if (p3x == s1x && p3y == s2y) {
                            if (fraction > 0.5) {
                                polygon.setPointIndices(Arrays.asList(new Integer[] { p0, p1, p2, segment.getP1(), segment.getP2() }));
                            } else {
                                polygon.setPointIndices(Arrays.asList(new Integer[] { segment.getP1(), p3, segment.getP2() }));
                            }
                        } else if (p3x == s2x && p3y == s1y) {
                            if (fraction > 0.5) {
                                polygon.setPointIndices(Arrays.asList(new Integer[] { p0, p1, p2, segment.getP2(), segment.getP1() }));
                            } else {
                                polygon.setPointIndices(Arrays.asList(new Integer[] { segment.getP2(), p3, segment.getP1() }));
                            }
                        // case 5) { (remove points 0 && 1 verticle cut)
                        // 
                        // 0  s1  3        0  s1                 s1  3
                        // =>               or
                        // 1  s2  2        1  s2                 s2  2
                        // 
                        } else if (p0y == s1y && p1y == s2y) {
                            boolean bigleft = ((s1x - p0x) + (s2x - p0x) > p3x - p0x);
                            if ((fraction > 0.5 && bigleft) || (fraction <= 0.5 && !bigleft)) {
                                polygon.setPointIndices(Arrays.asList(new Integer[] { p0, p1, segment.getP2(), segment.getP1() }));
                            } else {
                                polygon.setPointIndices(Arrays.asList(new Integer[] { segment.getP1(), segment.getP2(), p2, p3 }));
                            }
                        } else if (p0y == s2y && p1y == s1y) {
                            boolean bigleft = ((s1x - p0x) + (s2x - p0x) > p3x - p0x);
                            if ((fraction > 0.5 && bigleft) || (fraction <= 0.5 && !bigleft)) {
                                polygon.setPointIndices(Arrays.asList(new Integer[] { p0, p1, segment.getP1(), segment.getP2() }));
                            } else {
                                polygon.setPointIndices(Arrays.asList(new Integer[] { segment.getP2(), segment.getP1(), p2, p3 }));
                            }
                        // case 6) { (remove points 0 && 1 horizontal cut)
                        // 
                        // 0      3        0      3
                        // s1     s2  =>   s1     s2   or     s1     s2
                        // 1      2                           1      2
                        // 
                        } else if (p0x == s1x && p3x == s2x) {
                            boolean bigtop = ((s1y - p0y) + (s2y - p0y) > p1y - p0y);
                            if ((fraction > 0.5 && bigtop) || (fraction <= 0.5 && !bigtop)) {
                                polygon.setPointIndices(Arrays.asList(new Integer[] { p0, segment.getP1(), segment.getP2(), p3 }));
                            } else {
                                polygon.setPointIndices(Arrays.asList(new Integer[] { segment.getP2(), segment.getP1(), p1, p2 }));
                            }
                        } else if (p0x == s2x && p3x == s1x) {
                            boolean bigtop = ((s1y - p0y) + (s2y - p0y) > p1y - p0y);
                            if ((fraction > 0.5 && bigtop) || (fraction <= 0.5 && !bigtop)) {
                                polygon.setPointIndices(Arrays.asList(new Integer[] { p0, segment.getP2(), segment.getP1(), p3 }));
                            } else {
                                polygon.setPointIndices(Arrays.asList(new Integer[] { segment.getP1(), segment.getP2(), p1, p2 }));
                            }
                        } else {
                            LG.warn("found the segment for this polygon, don't know how to crop this one yet");
                        }
                    }
                }
            }
        }
    }
}
Also used : VisPolygon(org.vcell.vis.vismesh.thrift.VisPolygon) VisLine(org.vcell.vis.vismesh.thrift.VisLine) VisPoint(org.vcell.vis.vismesh.thrift.VisPoint) VisPoint(org.vcell.vis.vismesh.thrift.VisPoint)

Example 2 with VisLine

use of org.vcell.vis.vismesh.thrift.VisLine in project vcell by virtualcell.

the class ChomboMeshMapping method fromMeshData2D.

private VisMesh fromMeshData2D(ChomboMeshData chomboMeshData) {
    ChomboMesh chomboMesh = chomboMeshData.getMesh();
    ChomboLevel finestLevel = chomboMesh.getLevel(chomboMesh.getNumLevels() - 1);
    int finestAbsRefinement = finestLevel.getAbsoluteRefinement();
    ISize size = finestLevel.getSize();
    int numX = size.getX();
    int numY = size.getY();
    int numZ = size.getZ();
    int dimension = chomboMeshData.getMesh().getDimension();
    int z = 0;
    // invoke VisMesh() constructor
    VisMesh visMesh = new VisMesh(chomboMesh.getDimension(), toThrift(chomboMesh.getOrigin()), toThrift(chomboMesh.getExtent()));
    int currPointIndex = 0;
    HashMap<String, Integer> pointDict = new HashMap<String, Integer>();
    double originX = chomboMesh.getOrigin().x;
    double originY = chomboMesh.getOrigin().y;
    double originZ = chomboMesh.getOrigin().z;
    double extentX = chomboMesh.getExtent().x;
    double extentY = chomboMesh.getExtent().y;
    double extentZ = chomboMesh.getExtent().z;
    ChomboBoundaries chomboBoundaries = chomboMesh.getBoundaries();
    for (ChomboBoundaries.Point chomboPoint : chomboBoundaries.getPoints()) {
        double px = chomboPoint.x;
        double py = chomboPoint.y;
        double pz = z;
        px = (px - originX) * (numX) / extentX * 2 - 1;
        py = (py - originY) * (numY) / extentY * 2 - 1;
        pz = (pz - originZ) * (numZ) / extentZ * 2 - 1;
        if (dimension == 2) {
            pz = z;
        }
        VisPoint newVisPoint = new VisPoint(px, py, pz);
        String coordKey = toStringKey(newVisPoint);
        pointDict.put(coordKey, currPointIndex);
        visMesh.addToPoints(newVisPoint);
        visMesh.addToSurfacePoints(newVisPoint);
        currPointIndex += 1;
    }
    for (ChomboBoundaries.Segment segment : chomboBoundaries.getSegments()) {
        VisLine newVisLine = new VisLine(segment.getP1(), segment.getP2());
        newVisLine.setChomboSurfaceIndex(new ChomboSurfaceIndex(segment.getChomboIndex()));
        visMesh.addToVisLines(newVisLine);
    }
    for (int levelIndex = 0; levelIndex < chomboMesh.getNumLevels(); levelIndex++) {
        ChomboLevelData chomboLevelData = chomboMeshData.getLevelData(levelIndex);
        ChomboLevel currLevel = chomboMesh.getLevel(levelIndex);
        int currAbsRefinement = currLevel.getAbsoluteRefinement();
        Covering covering = currLevel.getCovering();
        int[] levelMap = covering.getLevelMap();
        int[] boxNumberMap = covering.getBoxNumberMap();
        int[] boxIndexMap = covering.getBoxIndexMap();
        int levelNumX = currLevel.getSize().getX();
        int levelNumY = currLevel.getSize().getY();
        for (int x = 0; x < levelNumX; x++) {
            for (int y = 0; y < levelNumY; y++) {
                int mapIndex = x + y * levelNumX;
                if (levelMap[mapIndex] == levelIndex) {
                    // 
                    // if fraction (volume fraction of element in box) is 0 ... then skip this element
                    // 
                    int boxNumber = boxNumberMap[mapIndex];
                    int boxIndex = boxIndexMap[mapIndex];
                    double fraction = chomboLevelData.getCellFraction(currLevel, boxNumber, boxIndex);
                    if (fraction > 0) {
                        // 
                        // add cell
                        // 
                        ChomboBox chomboBox = new ChomboBox(currLevel, x, x, y, y, z, z, dimension).getProjectedBox(currAbsRefinement, finestAbsRefinement);
                        double minX = 2 * chomboBox.getMinX() - 1;
                        double maxX = 2 * chomboBox.getMaxX() + 1;
                        double minY = 2 * chomboBox.getMinY() - 1;
                        double maxY = 2 * chomboBox.getMaxY() + 1;
                        // 
                        // counter clockwise points for a VisPolygon ... initially a quad ... then may be
                        // 
                        // minX,minY
                        // minX,maxY
                        // maxX,maxY
                        // maxX,minY
                        // 
                        VisPoint p1Coord = new VisPoint(minX, minY, z);
                        String p1Key = toStringKey(p1Coord);
                        Integer i1 = pointDict.get(p1Key);
                        if (i1 == null) {
                            pointDict.put(p1Key, currPointIndex);
                            i1 = currPointIndex;
                            visMesh.addToPoints(p1Coord);
                            currPointIndex++;
                        }
                        VisPoint p2Coord = new VisPoint(minX, maxY, z);
                        String p2Key = toStringKey(p2Coord);
                        Integer i2 = pointDict.get(p2Key);
                        if (i2 == null) {
                            pointDict.put(p2Key, currPointIndex);
                            i2 = currPointIndex;
                            visMesh.addToPoints(p2Coord);
                            currPointIndex++;
                        }
                        VisPoint p3Coord = new VisPoint(maxX, maxY, z);
                        String p3Key = toStringKey(p3Coord);
                        Integer i3 = pointDict.get(p3Key);
                        if (i3 == null) {
                            pointDict.put(p3Key, currPointIndex);
                            i3 = currPointIndex;
                            visMesh.addToPoints(p3Coord);
                            currPointIndex++;
                        }
                        VisPoint p4Coord = new VisPoint(maxX, minY, z);
                        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.setChomboVolumeIndex(new ChomboVolumeIndex(levelIndex, boxNumber, boxIndex, fraction));
                        // print('adding a cell at level '+str(currLevel.getLevel())+" from "+str(p1Coord)+" to "+str(p3Coord))
                        visMesh.addToPolygons(quad);
                    }
                }
            }
        }
    }
    cropQuads(visMesh);
    return visMesh;
}
Also used : ChomboSurfaceIndex(org.vcell.vis.vismesh.thrift.ChomboSurfaceIndex) HashMap(java.util.HashMap) ISize(org.vcell.util.ISize) VisLine(org.vcell.vis.vismesh.thrift.VisLine) Covering(org.vcell.vis.chombo.ChomboLevel.Covering) ChomboBoundaries(org.vcell.vis.chombo.ChomboBoundaries) ChomboVolumeIndex(org.vcell.vis.vismesh.thrift.ChomboVolumeIndex) VisPoint(org.vcell.vis.vismesh.thrift.VisPoint) ChomboLevelData(org.vcell.vis.chombo.ChomboLevelData) VisMesh(org.vcell.vis.vismesh.thrift.VisMesh) VisPolygon(org.vcell.vis.vismesh.thrift.VisPolygon) ChomboBox(org.vcell.vis.chombo.ChomboBox) ChomboMesh(org.vcell.vis.chombo.ChomboMesh) ChomboLevel(org.vcell.vis.chombo.ChomboLevel) VisPoint(org.vcell.vis.vismesh.thrift.VisPoint)

Aggregations

VisLine (org.vcell.vis.vismesh.thrift.VisLine)2 VisPoint (org.vcell.vis.vismesh.thrift.VisPoint)2 VisPolygon (org.vcell.vis.vismesh.thrift.VisPolygon)2 HashMap (java.util.HashMap)1 ISize (org.vcell.util.ISize)1 ChomboBoundaries (org.vcell.vis.chombo.ChomboBoundaries)1 ChomboBox (org.vcell.vis.chombo.ChomboBox)1 ChomboLevel (org.vcell.vis.chombo.ChomboLevel)1 Covering (org.vcell.vis.chombo.ChomboLevel.Covering)1 ChomboLevelData (org.vcell.vis.chombo.ChomboLevelData)1 ChomboMesh (org.vcell.vis.chombo.ChomboMesh)1 ChomboSurfaceIndex (org.vcell.vis.vismesh.thrift.ChomboSurfaceIndex)1 ChomboVolumeIndex (org.vcell.vis.vismesh.thrift.ChomboVolumeIndex)1 VisMesh (org.vcell.vis.vismesh.thrift.VisMesh)1