Пример #1
0
 public GridConverter(VoronoiBoundary boundary, PeriodicMap periodicMap = null)
 {
     this.boundary     = boundary;
     boundaryConverter = new BoundaryConverter(boundary, periodicMap);
 }
Пример #2
0
        static (GridCommons, int[][]) ExtractGridCommonsAndCellAggregation(
            IEnumerable <MeshCell <T> > cells,
            BoundaryConverter boundaryConverter)
        {
            List <BoSSS.Foundation.Grid.Classic.Cell> cellsGridCommons = new List <BoSSS.Foundation.Grid.Classic.Cell>();
            List <int[]> aggregation = new List <int[]>();

            foreach (MeshCell <T> cell in cells)
            {
                //Convert to BoSSSCell : Triangulate
                Vector[] voronoiCellVertices = cell.Vertices.Select(voVtx => voVtx.Position).ToArray();
                int[,] iVtxTri = PolygonTesselation.TesselatePolygon(voronoiCellVertices);
                //SortDescendingArea(iVtxTri, voronoiCellVertices);
                int[] Agg2Pt = new int[iVtxTri.GetLength(0)];

                bool isBoundaryCell = IsBoundary(cell);

                for (int iTri = 0; iTri < iVtxTri.GetLength(0); iTri++)
                { // loop over triangles of voronoi cell
                    int iV0 = iVtxTri[iTri, 0];
                    int iV1 = iVtxTri[iTri, 1];
                    int iV2 = iVtxTri[iTri, 2];

                    Vector V0 = voronoiCellVertices[iV0];
                    Vector V1 = voronoiCellVertices[iV1];
                    Vector V2 = voronoiCellVertices[iV2];

                    (iV0, iV1, iV2) = Rearrange(iV0, iV1, iV2, V0, V1, V2);

                    V0 = voronoiCellVertices[iV0];
                    V1 = voronoiCellVertices[iV1];
                    V2 = voronoiCellVertices[iV2];

                    Vector D1 = V1 - V0;
                    Vector D2 = V2 - V0;

                    bool positive = true;
                    if (D1.CrossProduct2D(D2) < 0)
                    {
                        if (isBoundaryCell)
                        {
                            Console.WriteLine($"Cell{cell.Node.AsVoronoiNode().Position} not positive. It is probably not convex.");
                            positive = false;
                            int it = iV0;
                            iV0 = iV2;
                            iV2 = it;

                            Vector vt = V0;
                            V0 = V2;
                            V2 = vt;

                            D1 = V1 - V0;
                            D2 = V2 - V0;
                        }
                        else
                        {
                            throw new Exception($"Created negatively oriented cell. Node = {cell.Node.AsVoronoiNode().Position}");
                        }
                    }

                    //if(D1.CrossProduct2D(D2) < 1.0e-7)
                    //{
                    //    Console.WriteLine($"Created very small cell. Node = {cell.Node.AsVoronoiNode().Position}, Area= {D1.CrossProduct2D(D2)}, positive = {positive}");
                    //}

                    Cell Cj = new Cell()
                    {
                        GlobalID = cellsGridCommons.Count,
                        Type     = CellType.Triangle_3,
                    };
                    Cj.TransformationParams = MultidimensionalArray.Create(3, 2);
                    Cj.TransformationParams.SetRowPt(0, V0);
                    Cj.TransformationParams.SetRowPt(1, V1);
                    Cj.TransformationParams.SetRowPt(2, V2);

                    Agg2Pt[iTri] = cellsGridCommons.Count;
                    cellsGridCommons.Add(Cj);

                    //Save BoundaryInformation
                    if (isBoundaryCell)
                    {
                        List <BoundaryFace> tags;
                        if (positive)
                        {
                            tags = GetBoundaryFacesOfTriangle(cell, iV0, iV1, iV2);
                        }
                        else
                        {
                            tags = GetBoundaryFacesOfNegativeTriangle(cell, iV0, iV1, iV2);
                        }
                        boundaryConverter.RegisterBoundaries(Cj, tags);
                    }
                    Cj.NodeIndices = new int[]
                    {
                        cell.Vertices[iV0].ID,
                        cell.Vertices[iV1].ID,
                        cell.Vertices[iV2].ID
                    };
                }
                aggregation.Add(Agg2Pt);
            }

            GridCommons grid = new Grid2D(Triangle.Instance)
            {
                Cells = cellsGridCommons.ToArray()
            };

            //PrintEdgeTags(cellsGridCommons);
            boundaryConverter.RegisterEdgesTo(grid);
            return(grid, aggregation.ToArray());
        }