コード例 #1
0
ファイル: FormMain.cs プロジェクト: wuxiaod1987/WCAE
        private void menuViewVoronoi_Click(object sender, EventArgs e)
        {
            if (mesh == null)
            {
                return;
            }

            if (menuViewVoronoi.Checked)
            {
                renderManager.ShowVoronoi = false;
                menuViewVoronoi.Checked   = false;
                return;
            }

            IVoronoi voronoi;

            if (mesh.IsPolygon)
            {
                voronoi = new BoundedVoronoi(mesh);
            }
            else
            {
                voronoi = new Voronoi(mesh);
            }

            renderData.SetVoronoi(voronoi);
            renderManager.SetData(renderData);

            menuViewVoronoi.Checked = true;
        }
コード例 #2
0
        private double Step(Mesh mesh, IVoronoiFactory factory, IPredicates predicates)
        {
            var voronoi = new BoundedVoronoi(mesh, factory, predicates);

            double x, y, maxDistanceMoved = 0;

            foreach (var face in voronoi.Faces)
            {
                if (face.generator.label == 0)
                {
                    Centroid(face, out x, out y);

                    double
                        xShift        = face.generator.x - x,
                        yShift        = face.generator.y - y,
                        distanceMoved = Math.Sqrt(xShift * xShift + yShift * yShift);
                    if (distanceMoved > maxDistanceMoved)
                    {
                        maxDistanceMoved = distanceMoved;
                    }

                    face.generator.x = x;
                    face.generator.y = y;
                }
            }

            // The maximum distance moved from any site.
            return(maxDistanceMoved);
        }
コード例 #3
0
 public CartographicIsland(Bundle bundle, BoundedVoronoi voronoi)
 {
     _bundle         = bundle;
     _voronoi        = voronoi;
     CoastlineMeshes = new List <TNetMesh>();
     CoastlineCells  = new List <VFace>();
     PackageMeshes   = new List <List <TNetMesh> >();
     PackageCells    = new List <List <VFace> >();
     Packages        = new List <Package>();
 }
コード例 #4
0
        private void Step(Mesh mesh, IVoronoiFactory factory, IPredicates predicates)
        {
            var voronoi = new BoundedVoronoi(mesh, factory, predicates);

            double x, y;

            foreach (var face in voronoi.Faces)
            {
                if (face.generator.label == 0)
                {
                    Centroid(face, out x, out y);

                    face.generator.x = x;
                    face.generator.y = y;
                }
            }
        }
コード例 #5
0
        private VFace ClosestCell(float x, float y, BoundedVoronoi voronoi)
        {
            int startingIndex = voronoi.Faces.Count / 2;

            VFace currentCell = voronoi.Faces[startingIndex];

            float xDiff       = (float)(currentCell.Generator.X - x);
            float yDiff       = (float)(currentCell.Generator.Y - y);
            float currentDist = Mathf.Sqrt(xDiff * xDiff + yDiff * yDiff);

            while (true)
            {
                VFace nextCell       = null;
                bool  foundNeighbour = false;
                foreach (VHEdge edge in currentCell.EnumerateEdges())
                {
                    VFace nCell         = edge.Twin.Face;
                    float xDiffNeighbor = (float)(nCell.Generator.X - x);
                    float yDiffNeighbor = (float)(nCell.Generator.Y - y);

                    float distFromNeighbour = Mathf.Sqrt(xDiffNeighbor * xDiffNeighbor
                                                         + yDiffNeighbor * yDiffNeighbor);

                    if (distFromNeighbour < currentDist)
                    {
                        foundNeighbour = true;
                        currentDist    = distFromNeighbour;
                        nextCell       = nCell;
                    }
                }

                if (!foundNeighbour)
                {
                    break;
                }

                currentCell = nextCell;
            }

            return(currentCell);
        }
コード例 #6
0
        public BoundedVoronoi CreateRelaxedVoronoi(int lloydRelaxations, float cellScale, int seed)
        {
            System.Random random = new System.Random(seed);

            List <Vertex> startingVertices = CreatePointsOnPlane(cellScale, cellScale, 50, 50, 1.0f, random);
            List <Vertex> vertices         = new List <Vertex>();

            TriangleNet.Configuration config = new TriangleNet.Configuration();
            SweepLine sweepline = new SweepLine();

            TNetMesh       tMesh   = (TNetMesh)sweepline.Triangulate(startingVertices, config);
            BoundedVoronoi voronoi = new BoundedVoronoi(tMesh);

            for (int i = 0; i < lloydRelaxations; i++)
            {
                foreach (VFace face in voronoi.Faces)
                {
                    if (CheckCell(face))
                    {
                        Vertex newCentroid = new Vertex(0, 0);
                        int    vertexCount = 0;
                        foreach (VHEdge edge in face.EnumerateEdges())
                        {
                            newCentroid.X += edge.Origin.X;
                            newCentroid.Y += edge.Origin.Y;
                            vertexCount++;
                        }
                        newCentroid.X /= vertexCount;
                        newCentroid.Y /= vertexCount;
                        vertices.Add(newCentroid);
                    }
                }

                tMesh   = (TNetMesh)sweepline.Triangulate(vertices, config);
                voronoi = new BoundedVoronoi(tMesh);
                vertices.Clear();
            }

            return(voronoi);
        }
コード例 #7
0
        //return: the unused candidates
        //b[1, 10]: cohesion factor. higher b -> more compact "cohesive" islands
        private Dictionary <int, VFace> constructRegionFromPackage(Package package, CartographicIsland island, Dictionary <int, VFace> startingCandidates, float b)
        {
            BoundedVoronoi          islandVoronoi = island.getVoronoi();
            List <CompilationUnit>  cuList        = package.getCompilationUnits();
            List <VFace>            cellMap       = new List <VFace>();
            Dictionary <int, VFace> newCandidates = new Dictionary <int, VFace>();
            VFace startingCell = selectFromCandidates(startingCandidates, b).Value;

            int maxIterations = 10;
            int counter       = 0;

            while (expandCountries(cuList, cellMap, newCandidates, startingCell, b) == false && counter < maxIterations)
            {
                //Debug.Log("Backtracking");
                startingCell = selectFromCandidates(startingCandidates, b).Value;
                counter++;
            }

            island.addPackage(package);
            island.addPackageCells(cellMap);
            return(newCandidates);
        }
コード例 #8
0
        private CartographicIsland BuildFromBundle(Bundle bundle)
        {
            int seed = bundle.GetHashCode();

            TriangleNet.Configuration config  = new TriangleNet.Configuration();
            BoundedVoronoi            voronoi = VoronoiMaker.Instance.CreateRelaxedVoronoi(1, CELL_SCALE, seed);

            VFace initCell   = ClosestCell(0, 0, voronoi);
            var   startCells = new Dictionary <int, VFace>();

            startCells.Add(initCell.ID, initCell);

            var island   = new CartographicIsland(bundle, voronoi);
            int maxCount = bundle.MostCompilationUnits.CompilationUnitCount;

            foreach (Package package in bundle.Packages)
            {
                ConstructRegion(package, island, startCells, maxCount);
            }

            ShapeCoastArea(startCells, HEIGHT_PROFILE);
            SetCoastline(startCells, island);
            island.Radius = CalculateRadius(startCells, island);

            foreach (List <VFace> cellMap in island.PackageCells)
            {
                List <TNetMesh> packageMeshes = ConstructTNetMeshFromCellmap(cellMap);
                island.PackageMeshes.Add(packageMeshes);
            }

            List <TNetMesh> coastlineMeshes = ConstructTNetMeshFromCellmap(island.CoastlineCells);

            island.CoastlineMeshes.AddRange(coastlineMeshes);

            LinkDependencyVertex(bundle, island);

            return(island);
        }
コード例 #9
0
        /// <summary>
        /// Smooth all free nodes.
        /// </summary>
        private void Step()
        {
            BoundedVoronoi voronoi = new BoundedVoronoi(this.mesh, false);

            var cells = voronoi.Regions;

            float x, y;
            int   n;

            foreach (var cell in cells)
            {
                n = 0;
                x = y = 0.0f;
                foreach (var p in cell.Vertices)
                {
                    n++;
                    x += p.x;
                    y += p.y;
                }

                cell.Generator.x = x / n;
                cell.Generator.y = y / n;
            }
        }
コード例 #10
0
        public static BoundedVoronoi createRelaxedVoronoi(List <Vertex> startingVertices, int numLloydRelaxations)
        {
            TriangleNet.Configuration conf = new TriangleNet.Configuration();
            SweepLine     sl       = new SweepLine();
            List <Vertex> vertices = new List <Vertex>();

            TnetMesh       tMesh   = (TnetMesh)sl.Triangulate(startingVertices, conf);
            BoundedVoronoi voronoi = new BoundedVoronoi(tMesh);

            for (int i = 0; i < numLloydRelaxations; i++)
            {
                foreach (VFace face in voronoi.Faces)
                {
                    if (checkCell(face))
                    {
                        Vertex newCentroid = new Vertex(0, 0);
                        int    vertexCount = 0;
                        foreach (VHEdge edge in face.EnumerateEdges())
                        {
                            newCentroid.X += edge.Origin.X;
                            newCentroid.Y += edge.Origin.Y;
                            vertexCount++;
                        }
                        newCentroid.X /= vertexCount;
                        newCentroid.Y /= vertexCount;
                        vertices.Add(newCentroid);
                    }
                }

                tMesh   = (TnetMesh)sl.Triangulate(vertices, conf);
                voronoi = new BoundedVoronoi(tMesh);
                vertices.Clear();
            }

            return(voronoi);
        }
コード例 #11
0
ファイル: CmdVoronoi.cs プロジェクト: Titifonky/SwExtension
        protected override void Command()
        {
            Feature Fonction = MdlBase.eSelect_RecupererObjet <Feature>(1, -1);

            MdlBase.eEffacerSelection();

            Esquisse = Fonction.GetSpecificFeature2();
            xForm    = Esquisse.ModelToSketchTransform.Inverse();
            Mu       = App.Sw.GetMathUtility();

            String fact = "0.8";

            if (Interaction.InputBox("Facteur de décalage", "F :", ref fact) == DialogResult.OK)
            {
                if (!String.IsNullOrWhiteSpace(fact))
                {
                    Double r = fact.eToDouble();
                    if (r != 0)
                    {
                        FacteurDecal = r;
                    }
                }
            }

            var polygon = new Polygon();

            if (Esquisse.GetSketchRegionCount() == 0)
            {
                return;
            }

            // On ajoute les contours
            SketchRegion region = Esquisse.GetSketchRegions()[0];
            Loop2        loop   = region.GetFirstLoop();

            int i = 0;

            while (loop != null)
            {
                var ListPt = new List <TriangleNet.Geometry.Vertex>();
                foreach (SolidWorks.Interop.sldworks.Vertex v in loop.GetVertices())
                {
                    double[] pt = (double[])v.GetPoint();
                    ListPt.Add(new TriangleNet.Geometry.Vertex(pt[0], pt[1]));
                }

                polygon.Add(new Contour(ListPt, i++), !loop.IsOuter());

                loop = loop.GetNext();
            }


            foreach (SketchPoint pt in Esquisse.GetSketchPoints2())
            {
                polygon.Add(new TriangleNet.Geometry.Vertex(pt.X, pt.Y));
            }

            var contraintes = new ConstraintOptions();

            contraintes.ConformingDelaunay = true;
            contraintes.SegmentSplitting   = 0;

            //var Qualite = new QualityOptions();
            //Qualite.SteinerPoints = 0;
            Mesh mesh = (Mesh)polygon.Triangulate(contraintes);

            var voronoi = new BoundedVoronoi(mesh);

            var SM = MdlBase.SketchManager;

            SelectionnerSupportEsquisse();
            SM.InsertSketch(false);
            SM.AddToDB          = true;
            SM.DisplayWhenAdded = false;

            List <TriangleNet.Geometry.Vertex> lstVertex = new List <TriangleNet.Geometry.Vertex>();

            foreach (var v in mesh.Vertices)
            {
                lstVertex.Add(v);
            }

            foreach (var edge in mesh.Edges)
            {
                try
                {
                    TriangleNet.Geometry.Point p1 = TransformPt(lstVertex[edge.P0]);
                    TriangleNet.Geometry.Point p2 = TransformPt(lstVertex[edge.P1]);
                    SM.CreateLine(p1.X, p1.Y, 0, p2.X, p2.Y, 0);
                }
                catch (Exception errDesc)
                { this.LogMethode(new Object[] { errDesc }); }
            }

            SM.DisplayWhenAdded = true;
            SM.AddToDB          = false;
            SM.InsertSketch(true);
            MdlBase.eEffacerSelection();

            SelectionnerSupportEsquisse();
            SM.InsertSketch(false);
            SM.AddToDB          = true;
            SM.DisplayWhenAdded = false;

            foreach (var t in mesh.Triangles)
            {
                try
                {
                    TriangleNet.Geometry.Point p1 = TransformPt(lstVertex[t.GetVertexID(0)]);
                    TriangleNet.Geometry.Point p2 = TransformPt(lstVertex[t.GetVertexID(1)]);
                    TriangleNet.Geometry.Point p3 = TransformPt(lstVertex[t.GetVertexID(2)]);

                    TriangleNet.Geometry.Point centre = new TriangleNet.Geometry.Point();
                    centre.X = (p1.X + p2.X + p3.X) / 3.0;
                    centre.Y = (p1.Y + p2.Y + p3.Y) / 3.0;

                    TriangleNet.Geometry.Point pt1;
                    TriangleNet.Geometry.Point pt2;

                    pt1 = TransformPt(Echelle(centre, t.GetVertex(0), FacteurDecal));
                    pt2 = TransformPt(Echelle(centre, t.GetVertex(1), FacteurDecal));
                    SM.CreateLine(pt1.X, pt1.Y, 0, pt2.X, pt2.Y, 0);

                    pt1 = TransformPt(Echelle(centre, t.GetVertex(1), FacteurDecal));
                    pt2 = TransformPt(Echelle(centre, t.GetVertex(2), FacteurDecal));
                    SM.CreateLine(pt1.X, pt1.Y, 0, pt2.X, pt2.Y, 0);

                    pt1 = TransformPt(Echelle(centre, t.GetVertex(2), FacteurDecal));
                    pt2 = TransformPt(Echelle(centre, t.GetVertex(0), FacteurDecal));
                    SM.CreateLine(pt1.X, pt1.Y, 0, pt2.X, pt2.Y, 0);
                }
                catch (Exception errDesc)
                { this.LogMethode(new Object[] { errDesc }); }
            }

            SM.DisplayWhenAdded = true;
            SM.AddToDB          = false;
            SM.InsertSketch(true);
            MdlBase.eEffacerSelection();

            SelectionnerSupportEsquisse();
            SM.InsertSketch(false);
            SM.AddToDB          = true;
            SM.DisplayWhenAdded = false;

            foreach (var f in voronoi.Faces)
            {
                try
                {
                    TriangleNet.Geometry.Point centre = f.generator;

                    foreach (var e in f.EnumerateEdges())
                    {
                        var pt1 = TransformPt(Echelle(centre, e.Origin, FacteurDecal));
                        var pt2 = TransformPt(Echelle(centre, e.Twin.Origin, FacteurDecal));
                        SM.CreateLine(pt1.X, pt1.Y, 0, pt2.X, pt2.Y, 0);
                    }
                }
                catch (Exception errDesc)
                { this.LogMethode(new Object[] { errDesc }); }
            }

            SM.DisplayWhenAdded = true;
            SM.AddToDB          = false;
            SM.InsertSketch(true);
        }
コード例 #12
0
 public void setVoronoi(BoundedVoronoi voronoi)
 {
     islandVoronoi = voronoi;
 }
コード例 #13
0
        private CartographicIsland constructIslandFromBundle(Bundle b)
        {
            int rngSeed = b.getName().GetHashCode() + 200;

            RNG = new System.Random(rngSeed);

            #region VoronoiPlane
            TriangleNet.Configuration conf     = new TriangleNet.Configuration();
            List <Vertex>             vertices = Helperfunctions.createPointsOnPlane(GlobalVar.voronoiCellScalefactor, GlobalVar.voronoiCellScalefactor, 50, 50, 1.0f, RNG);
            BoundedVoronoi            voronoi  = Helperfunctions.createRelaxedVoronoi(vertices, 1);
            #endregion

            #region initFirstCell
            VFace firstCell = Helperfunctions.closestCell(0, 0, voronoi);
            Dictionary <int, VFace> startingCandidates = new Dictionary <int, VFace>();
            startingCandidates.Add(firstCell.ID, firstCell);
            #endregion

            List <Package>     packages = b.getPackages();
            CartographicIsland island   = new CartographicIsland(b);
            island.setVoronoi(voronoi);
            #region sort package list
            packages.Sort((x, y) => x.getCompilationUnits().Count.CompareTo(y.getCompilationUnits().Count));
            packages.Reverse();
            #endregion
            //Compute maximal compilation unit count in bundle
            float maxCUCountInIsland = 0;
            foreach (Package package in packages)
            {
                long cuCount = package.getCuCount();
                if (cuCount > maxCUCountInIsland)
                {
                    maxCUCountInIsland = cuCount;
                }
            }
            #region construct regions
            foreach (Package package in packages)
            {
                float cohesionMult = (float)package.getCuCount() / maxCUCountInIsland;
                cohesionMult *= maxCohesion;
                cohesionMult  = Mathf.Max(minCohesion, cohesionMult);
                Dictionary <int, VFace> newCandidates = constructRegionFromPackage(package, island, startingCandidates, cohesionMult);
                updateAndFuseCandidates(startingCandidates, newCandidates);
            }
            #endregion


            #region Shape island coast
            //Advance startingCandidates X cells outwards and ajdust the height of all vertices
            shapeCoastArea(startingCandidates, GlobalVar.islandHeightProfile);
            #endregion

            #region WeightedCenter & set coast
            List <VFace> coastlineList  = new List <VFace>();
            Vector3      weightedCenter = Vector3.zero;
            foreach (KeyValuePair <int, VFace> kvp in startingCandidates)
            {
                coastlineList.Add(kvp.Value);
                float   x       = (float)kvp.Value.generator.X;
                float   z       = (float)kvp.Value.generator.Y;
                Vector3 tilePos = new Vector3(x, 0, z);
                weightedCenter += tilePos;
            }
            weightedCenter /= startingCandidates.Count;
            island.setWeightedCenter(weightedCenter);
            island.setCoastlineCells(coastlineList);
            #endregion

            #region conservative Radius
            List <float> radii = new List <float>();
            foreach (KeyValuePair <int, VFace> kvp in startingCandidates)
            {
                float x      = (float)kvp.Value.generator.X - island.getWeightedCenter().x;
                float z      = (float)kvp.Value.generator.Y - island.getWeightedCenter().z;
                float radius = Mathf.Sqrt(x * x + z * z);
                radii.Add(radius);
            }
            float maxRadius = Helperfunctions.computeMax(radii);
            island.setRadius(maxRadius);
            #endregion

            #region TnetMeshesConstruction
            foreach (List <VFace> cellMap in island.getPackageCells())
            {
                island.addPackageMesh(constructTnetMeshFromCellmap(cellMap));
            }

            island.setCoastlineMesh(constructTnetMeshFromCellmap(coastlineList));
            #endregion

            #region link dependency vertex

            //Find graph vertex associated with the island
            BidirectionalGraph <GraphVertex, GraphEdge> depGraph = b.getParentProject().getDependencyGraph();
            List <GraphVertex> allVertices = depGraph.Vertices.ToList();
            GraphVertex        vert        = allVertices.Find(v => string.Equals(v.getName(), b.getName()));
            if (vert != null)
            {
                //Link GraphVertex-Island
                vert.setIsland(island);
                island.setDependencyVertex(vert);
            }

            #endregion

            return(island);
        }
コード例 #14
0
        private void Step(Mesh mesh, IVoronoiFactory factory, IPredicates predicates)
        {
            var voronoi = new BoundedVoronoi(mesh, factory, predicates);

            double x, y;

            foreach (var face in voronoi.Faces)
            {
                if (face.generator.label == 0)
                {
                    Centroid(face, out x, out y);

                    face.generator.x = x;
                    face.generator.y = y;
                }
            }
        }