Пример #1
0
    private IEnumerator Co()
    {
        string         fullPath = Application.streamingAssetsPath + "/" + filename;
        List <Vector3> vertices = new List <Vector3>();

        using (TextReader r = new StreamReader(fullPath)) {
            ChainLine mainLine = lineFactory.CreateLine(null, liveTime, gradient);
            ChainLine subLine  = lineFactory.CreateLine(null, subColor);

            string   line = null;
            string[] result;

            float max = 16777215f;

            while ((line = r.ReadLine()) != null)
            {
                //頂点
                result = line.Split(separator);
                Vector3 pos = new Vector3(
                    ValueAdjust(float.Parse(result[0]), max),
                    ValueAdjust(float.Parse(result[1]), max),
                    ValueAdjust(float.Parse(result[2]), max)) * 0.00001f;

                //線二頂点を追加
                mainLine.AddVertex(pos);
                subLine.AddVertex(pos, subColor);
                yield return(0f);
            }
        }
    }
Пример #2
0
    /// <summary>
    /// ボロノイ図生成コルーチン
    /// </summary>
    private IEnumerator Voronoi()
    {
        PseudoHalfPlaneGenerator halfPlaneGenerator = new PseudoHalfPlaneGenerator(100f);

        while (true)
        {
            List <Vector2>       sites   = new List <Vector2>(siteTranses.Select(elem => (Vector2)elem.position));
            List <ConvexPolygon> results = new List <ConvexPolygon>();
            List <ChainLine>     lines   = new List <ChainLine>();

            for (int i = 0; i < sites.Count; ++i)
            {
                Vector3       s1     = sites[i];
                ConvexPolygon region = null;                    //途中計算結果格納用の領域
                for (int j = 0; j < sites.Count; ++j)
                {
                    Vector3 s2 = sites[j];
                    if (i == j)
                    {
                        continue;
                    }

                    //s1とs2の垂直二等分線を求める
                    Line line = Line.PerpendicularBisector(s1, s2);
                    //垂直二等分線による半平面のうち,s1を含む方を求める
                    ConvexPolygon halfPlane = halfPlaneGenerator.Execute(line, s1);

                    if (region == null)
                    {
                        //初回計算時
                        region = IntersectionOperation.Execute(areaPolygon, halfPlane);
                    }
                    else
                    {
                        //二回目以降
                        region = IntersectionOperation.Execute(region, halfPlane);
                    }
                    Debug.Log(region + " : " + halfPlane);

                    //halfPlaneを可視化
                    List <Vector3> vertices = halfPlane.GetVertices3Copy();
                    vertices.Add(vertices[0]);                          //末尾を追加
                    ChainLine subl = lineFactory.CreateLine(vertices, subColor);

                    //regionを可視化
                    vertices = region.GetVertices3Copy();
                    vertices.Add(vertices[0]);                          //末尾を追加
                    ChainLine l = lineFactory.CreateLine(vertices, lineColor.Evaluate((float)j / sites.Count));
                    yield return(StartCoroutine(Wait()));

                    lineFactory.DeleteLine(subl);
                    lineFactory.DeleteLine(l);
                }
            }
            Debug.Log("Complete");

            yield return(StartCoroutine(Wait()));
        }
    }
Пример #3
0
    private void Update()
    {
        List <Vector2>       sites   = new List <Vector2>(siteTranses.Select(elem => (Vector2)elem.position));
        List <ConvexPolygon> results = new List <ConvexPolygon>();

        //線の削除
        if (lines.Count > 0)
        {
            for (int i = 0; i < lines.Count; ++i)
            {
                lineFactory.DeleteLine(lines[i]);
            }
            lines.Clear();
        }

        foreach (Vector2 s1 in sites)
        {
            ConvexPolygon region = null;                //途中計算結果格納用の領域
            foreach (Vector2 s2 in sites)
            {
                if (s1 == s2)
                {
                    continue;
                }
                //s1とs2の垂直二等分線を求める
                Line line = Line.PerpendicularBisector(s1, s2);
                //垂直二等分線による半平面のうち,s1を含む方を求める
                ConvexPolygon halfPlane = halfPlaneGenerator.Execute(line, s1);
                if (region == null)
                {
                    //初回計算時
                    region = IntersectionOperation.Execute(areaPolygon, halfPlane);
                }
                else
                {
                    //二回目以降
                    region = IntersectionOperation.Execute(region, halfPlane);
                }
            }
            if (region != null)
            {
                results.Add(region);
            }
            else
            {
                Debug.Log("Region is null");
            }
        }

        for (int i = 0; i < results.Count; ++i)
        {
            results[i].Scale(sites[i], 0.9f);
            List <Vector3> vertices = results[i].GetVertices3Copy();
            vertices.Add(vertices[0]);
            lines.Add(lineFactory.CreateLine(vertices, Color.green));
        }
    }
Пример #4
0
    /// <summary>
    /// 図の更新
    /// </summary>
    private void UpdateDiagram(List <Vector2> sites)
    {
        //ボロノイ図の作成
        areaPolygon      = ConvexPolygon.SquarePolygon(10f);
        voronoiGenerator = new VoronoiDiagramGenerator();
        regions          = voronoiGenerator.Execute(areaPolygon, sites);

        //線の削除
        for (int i = 0; i < lines.Count; ++i)
        {
            lineFactory.DeleteLine(lines[i]);
        }
        lines.Clear();

        for (int i = 0; i < regions.Count; ++i)
        {
            ConvexPolygon region = regions[i];

            //線の描画
            if (drawRegionLine)
            {
                List <Vector3> vertices = region.GetVertices3Copy();
                vertices.Add(vertices[0]);
                lines.Add(lineFactory.CreateLine(vertices));
            }

            //メッシュ
            meshes[i] = region.Scale(sites[i], 0.5f).ToMesh();            //AngleSubdivisionOperation.Execute(region, 170f).Scale(sites[i], 1f).ToMesh();

            //あたり判定の設定
            colliders[i].sharedMesh = meshes[i];
        }
    }
Пример #5
0
    private void FixedUpdate()
    {
        for (int i = 0; i < 2; ++i)
        {
            int     octave = i;
            float[] noise  = PerlinNoiseD1.Noise(ctrlCount, noiseScale, octave);

            ChainLine line = lineFactory.CreateLine(null, new LiveTimeUpdater(liveTime, true), new LiveTimeGradientUpdater(gradient));
            for (int j = 0; j < noise.Length; ++j)
            {
                line.AddVertex(new Vector3(j / (float)noise.Length * 100f, noise[j] * amplitude + (i * amplitude)));
            }
        }
    }
Пример #6
0
    private void DrawEdgeLine(ConvexPolygon p, ChainLine line)
    {
        IntersectionOperation.Edge left     = IntersectionOperation.CreateEdgeChain(p, true);
        List <Vector3>             vertices = new List <Vector3>();

        vertices.Add(left.startPoint);
        foreach (var e in left)
        {
            vertices.Add(e.endPoint);
        }
        if (line != null)
        {
            lineFactory.DeleteLine(line);
        }
        line = lineFactory.CreateLine(vertices, Color.red);
    }
Пример #7
0
    private void Update()
    {
        List <Vector2> vertices = new List <Vector2> (this.vertices.Select(elem => (Vector2)elem.position));

        //Convex Polygon
        polygon = new ConvexPolygon(vertices);

        //DrawLine
        List <Vector3> points = polygon.GetVertices3Copy();

        points.Add(points[0]);          //末尾に先頭を追加
        if (line != null)
        {
            lineFactory.DeleteLine(line);
        }

        line = lineFactory.CreateLine(points, lineColor);
    }
Пример #8
0
    private void Update()
    {
        List <Vector2> sites = new List <Vector2>(siteTranses.Select(elem => (Vector2)elem.position));

        //ボロノイ図の作成
        areaPolygon      = ConvexPolygon.SquarePolygon(10f);
        voronoiGenerator = new VoronoiDiagramGenerator();
        List <ConvexPolygon> regions = voronoiGenerator.Execute(areaPolygon, sites);

        List <Vector3> vertices;

        for (int i = 0; i < lines.Count; ++i)
        {
            lineFactory.DeleteLine(lines[i]);
        }
        //lines.Clear();

        for (int i = 0; i < regions.Count; ++i)
        {
            ConvexPolygon region = regions[i];
            if (drawRegionLine)
            {
                vertices = region.GetVertices3Copy();
                vertices.Add(vertices[0]);
                lines.Add(lineFactory.CreateLine(vertices));
            }
            //描画
            Mesh mesh = null;
            if (angled)
            {
                mesh = AngleSubdivisionOperation.Execute(region, 170f).Scale(sites[i], 0.9f).ToAltMesh();
            }
            else
            {
                mesh = LerpSubdivisionOperation.Execute(region, i, lerpT).Scale(sites[i], 0.9f).ToAltMesh();
            }
            UnityEngine.Graphics.DrawMesh(mesh, Matrix4x4.identity, mat, 0);
        }
    }
Пример #9
0
    private void Update()
    {
        List <WaitedVoronoiSite> sites =
            new List <WaitedVoronoiSite>(siteObjects.Select(elem => new WaitedVoronoiSite(elem.transform.position, elem.Wait)));

        //ボロノイ図の作成
        List <ConvexPolygon> regions = voronoiGenerator.Execute(areaPolygon, sites);

        List <Vector3> vertices;

        for (int i = 0; i < lines.Count; ++i)
        {
            lineFactory.DeleteLine(lines[i]);
        }
        lines.Clear();

        for (int i = 0; i < regions.Count; ++i)
        {
            ConvexPolygon region = regions[i];
            vertices = region.GetVertices3Copy();
            vertices.Add(vertices[0]);
            lines.Add(lineFactory.CreateLine(vertices));
        }
    }
Пример #10
0
        public static ConvexPolygon Execute(ConvexPolygon polygon1, ConvexPolygon polygon2, ChainLineFactory lineFactory)
        {
            //どちらかがnullならnullを返す
            if (polygon1 == null || polygon2 == null)
            {
                return(null);
            }

            //ステータスなど下準備
            Status      status    = new Status();
            List <Edge> leftEdge  = new List <Edge>();              //左側の計算結果
            List <Edge> rightEdge = new List <Edge>();              //右側の計算結果

            //一回目のイベントを処理
            StepResult result = FirstPass(polygon1, polygon2, status,
                                          leftEdge, rightEdge);

            //二回目以降のイベントを処理
            while (result.mustContinue)
            {
                //二番目以降のイベントを処
                result = SecondPass(status, leftEdge, rightEdge);
            }

            //左側と右側の計算結果を統合するリスト
            List <Edge> totalEdge = new List <Edge>();

            totalEdge.AddRange(leftEdge);
            rightEdge.Reverse();                                //反転して連結
            totalEdge.AddRange(rightEdge);

            //線の描画
            List <Vector3> vs = new List <Vector3>();

            vs.Add(leftEdge[0].startPoint);
            foreach (var e in leftEdge)
            {
                vs.Add(e.endPoint);
            }
            lineFactory.CreateLine(vs, Color.red);

            vs = new List <Vector3>();
            vs.Add(rightEdge[0].startPoint);
            foreach (var e in rightEdge)
            {
                vs.Add(e.endPoint);
            }
            lineFactory.CreateLine(vs, Color.blue);

            //交差凸多角形の交点リスト
            List <Vector2> resultPoints = new List <Vector2>();
            Vector2        lastPoint    = Vector2.one * float.NaN;
            int            totalSize    = totalEdge.Count;

            for (int i = 0; i < totalSize; ++i)
            {
                Edge    e1 = totalEdge[i];
                Edge    e2 = totalEdge[(i + 1) % totalSize];
                Vector2 p  = Vector2.zero;
                if (e1.GetIntersectionPoint(e2, ref p))
                {
                    resultPoints.Add(p);
                }
            }

            //頂点が3つ以上なら凸多角形を作成
            if (resultPoints.Count >= 3)
            {
                return(new ConvexPolygon(resultPoints));
            }
            else
            {
                return(null);
            }
        }