示例#1
0
        /*
         * public void serializeTest()
         * {
         *  const char* name = "lineList";
         *  QSharedPointer<LDIndexLineList> src(new LDIndexLineList);
         *  src->push_back(LDIndexLine(0, 1));
         *
         *  //シリアライズ
         *  SerializeHelper::writeBoostXml(name, src);
         *
         *  //デシリアライズ
         *  auto dst = SerializeHelper::readBoostXml<LDIndexLineList>(name);
         *
         *  QVERIFY(*(dst.data()) == *(src.data()));
         * }
         */

        public void hitTest()
        {
            LDIndexLineList lines  = new LDIndexLineList();
            LDIndexLine     line_1 = new LDIndexLine(0, 1);
            LDIndexLine     line_2 = new LDIndexLine(1, 2);
            LDIndexLine     line_3 = new LDIndexLine(2, 3);
            LDIndexLine     line_4 = new LDIndexLine(3, 0);

            lines.add(line_1);
            lines.add(line_2);
            lines.add(line_3);
            lines.add(line_4);

            LDPointList points = new LDPointList();

            points.Add(new LDPoint(0, 0));
            points.Add(new LDPoint(100, 0));
            points.Add(new LDPoint(100, 100));
            points.Add(new LDPoint(0, 100));


            TestUtil.COMPARE(lines.isHit(points, new LDPoint(0, 0), 1), true);    //頂点上
            TestUtil.COMPARE(lines.isHit(points, new LDPoint(50, 0), 1), true);   //線上
            TestUtil.COMPARE(lines.isHit(points, new LDPoint(50, 1), 3), true);   //線からちょっと離れたところ
            TestUtil.COMPARE(lines.isHit(points, new LDPoint(50, 4), 3), false);  //線から範囲外に離れたところ
            TestUtil.COMPARE(lines.isHit(points, new LDPoint(200, 0), 3), false); //線の延長戦上に離れたところ
            TestUtil.COMPARE(lines.isHit(points, new LDPoint(200, 1), 3), false); //線の延長戦上に離れたところ
        }
示例#2
0
 public void setClockWise(LDPointList form, ClockWise cw)
 {
     for (int i = 0; i < size(); i++)
     {
         this[i].setClockWise(form, cw);
     }
 }
示例#3
0
        public LDLine toLine(LDPointList form)
        {
            Debug.Assert(form.length() > m_index1);
            Debug.Assert(form.length() > m_index2);

            return(new LDLine(form.at(m_index1), form.at(m_index2)));
        }
示例#4
0
        public void replacePointList(LDPointList form)
        {
            Debug.Assert(m_points.length() == form.length());

            for (int i = 0; i < form.length(); ++i)
            {
                m_points.replace(i, form[i]);
            }
        }
示例#5
0
        public LDPointList inverseTransform(LDPointList pList, bool clip = false)
        {
            LDPointList result = new LDPointList();

            foreach (var pt in pList)
            {
                result.Add(inverseTransform(pt, clip));
            }
            return(result);
        }
示例#6
0
        public LDPointList inverseTransform(LDPointList points, bool clip = false)
        {
            LDPointList result = new LDPointList();
            int         length = points.length();

            for (int i = 0; i < length; i++)
            {
                result.Add(inverseTransform(points[i], clip));
            }
            return(result);
        }
示例#7
0
        public LDPointList transform(LDPointList form)
        {
            LDPointList result = new LDPointList();

            foreach (var pt in form)
            {
                result.Add(transform(pt));
            }

            return(result);
        }
示例#8
0
        public LDPointList getPointList()
        {
            LDPointList result = new LDPointList();

            result.Add(m_topLeft);
            result.Add(m_topRight);
            result.Add(m_bottomRight);
            result.Add(m_bottomLeft);

            return(result);
        }
示例#9
0
        /*
         * public void serializeTest()
         * {
         *  string name = "gridMesh";
         *  LDGridTransform src = new LDGridTransform(0, 0, 100, 100, 2, 2));
         *
         *  //シリアライズ
         *  SerializeHelper.writeBoostXml(name, src);
         *
         *  //デシリアライズ
         *  var dst = SerializeHelper.readBoostXml<LDGridTransform>(name);
         *
         *  TestUtil.VERIFY(dst->getGridPoints() == src->getGridPoints());
         * }
         */
        public void simpleTransformTest()
        {
            LDGridTransform grid = new LDGridTransform(20, 20, 40, 40, 1, 1);

            {
                LDPoint src = new LDPoint(0.5f, 0.5f);

                LDPoint dst;

                dst = grid.transform(src);

                TestUtil.COMPARE(dst.x(), 40.0);
                TestUtil.COMPARE(dst.y(), 40.0);
            }

            {
                LDPointList src = new LDPointList();
                src.add(new LDPoint(0.25f, 0.25f)).add(new LDPoint(0.75f, 0.75f));

                LDPointList dst;

                dst = grid.transform(src);

                TestUtil.VERIFY(dst.length() == 2);
                TestUtil.COMPARE(dst[0], new LDPoint(30.0f, 30));
                TestUtil.COMPARE(dst[1], new LDPoint(50.0f, 50));
            }

            {
                LDGridTransform src = new LDGridTransform(0.5f, 0.5f, 0.5f, 0.5f, 1, 1);

                LDGridTransform dst = new LDGridTransform();

                dst = grid.transform(src);

                TestUtil.COMPARE(dst.getPoint(0, 0), new LDPoint(40, 40));
                TestUtil.COMPARE(dst.getPoint(0, 1), new LDPoint(60, 40));
                TestUtil.COMPARE(dst.getPoint(1, 1), new LDPoint(60, 60));
                TestUtil.COMPARE(dst.getPoint(1, 0), new LDPoint(40, 60));
            }

            {
                LDAffineTransform src = new LDAffineTransform();
                src.translate(0.5f, 0.5f);
                src.rotate(30);
                src.scale(2, 2);

                LDAffineTransform dst = new LDAffineTransform();

                dst = grid.transform(src);

                TestUtil.COMPARE(dst.getTranslate(), new LDPoint(40, 40));
            }
        }
示例#10
0
 public bool isHit(LDPointList form, LDPoint p, float hitRange)
 {
     for (int i = 0; i < this.size(); i++)
     {
         if (this.at(i).isHit(form, p, hitRange))
         {
             return(true);
         }
     }
     return(false);
 }
示例#11
0
        public LDPointList inverseTransform(LDPointList points)
        {
            LDPointList result = new LDPointList();

            foreach (var pt in points)
            {
                result.Add(inverseTransform(pt));
            }

            return(result);
        }
示例#12
0
        public LDPointList getOutlinePoints()
        {
            List <int> indices = getOutlinePointIndices();

            LDPointList result = new LDPointList();

            foreach (var index in indices)
            {
                result.Add(getPoint(index));
            }
            return(result);
        }
示例#13
0
        public LDPointList toForm()
        {
            LDPointList result = new LDPointList();

            for (int i = 0; i < getRow() + 1; ++i)
            {
                for (int j = 0; j < getColumn() + 1; ++j)
                {
                    result.Add(m_gridPoints[i][j]);
                }
            }
            return(result);
        }
示例#14
0
        public LDPolygon toPolygon(LDPointList points)
        {
            Debug.Assert((points.length() > m_index1));
            Debug.Assert(points.length() > m_index2);
            Debug.Assert(points.length() > m_index3);

            LDPolygon v = new LDPolygon();

            v.Add(new LDPoint(points.at(m_index1)));
            v.Add(new LDPoint(points.at(m_index2)));
            v.Add(new LDPoint(points.at(m_index3)));
            return(v);
        }
示例#15
0
        public LDPolygon getOutlinePolygon(LDPointList points)
        {
            List <int> indices = getOutlinePointIndices(points);

            LDPolygon result = new LDPolygon();

            foreach (var index in indices)
            {
                common.LD_ASSERT_OUT_OF_INDEX(points, index);
                result.Add(points[index]);
            }
            return(result);
        }
示例#16
0
        public void setForm(LDPointList value)
        {
            Debug.Assert(getPointCount() == value.length());
            for (int i = 0; i < getRow() + 1; ++i)
            {
                for (int j = 0; j < getColumn() + 1; ++j)
                {
                    m_gridPoints[i][j] = value[i * (getColumn() + 1) + j];
                }
            }

            clearBoundsCache();
        }
示例#17
0
        public LDLineList getLines(LDPointList points)
        {
            LDLineList result = new LDLineList();

            for (int i = 0; i < size(); i++)
            {
                int     index1 = this.at(i).getIndex1();
                int     index2 = this.at(i).getIndex2();
                LDPoint point1 = points.at(index1);
                LDPoint point2 = points.at(index2);
                result.Add(new LDLine(point1, point2));
            }
            return(result);
        }
示例#18
0
        public void getOutlinePointsTest_simple()
        {
            LDTriangleList triangles = simpleTriangle();
            LDPointList    points    = new LDPointList();

            points.Add(new LDPoint(0, 0));
            points.Add(new LDPoint(0, 100));
            points.Add(new LDPoint(100, 100));

            LDPointList compare = points;

            LDPointList result = triangles.getOutlinePoints(points);

            TestUtil.COMPARE(result, compare);
        }
示例#19
0
        public bool isHit(LDPointList form, LDPoint p, float hitRange)
        {
            LDLine l = this.toLine(form);

            LDPoint startPt = l.p1();
            LDPoint endPt   = l.p2();

            //端点から一定の距離内だったら当たり
            if (PointUtil.isHit(startPt, p, hitRange))
            {
                return(true);
            }
            if (PointUtil.isHit(endPt, p, hitRange))
            {
                return(true);
            }


            //点が一直線上にないか確認
            if (TriangleUtil.isTriangle(startPt, endPt, p))
            {
                //鈍角三角形なら判定外
                if (TriangleUtil.isObtuseAngle(startPt, endPt, p))
                {
                    return(false);
                }

                //三角形の面積を算出して、その底面で割れば高さ=線と点の距離
                float distance = TriangleUtil.getTriangleHeight(startPt, endPt, p);

                if (distance <= hitRange)
                {
                    return(true);
                }
                return(false);
            }
            //一直線上にあるが線分外にあるか判定
            LDVector2 ab    = new LDVector2(endPt - startPt);
            LDVector2 ap    = new LDVector2(p - startPt);
            LDVector2 bp    = new LDVector2(p - endPt);
            float     omega = 0.0001f;//NOTE:誤差の基準値 かなり適当に指定

            if (ap.length() + bp.length() > ab.length() + omega)
            {
                return(false);
            }
            return(true);
        }
示例#20
0
 public static bool fuzzyCompare(LDPointList a, LDPointList b, float fuzz)
 {
     if (a.length() != b.length())
     {
         return(false);
     }
     for (int i = 0; i < a.length(); ++i)
     {
         if (Math.Abs(a[i].x() - b[i].x()) > fuzz)
         {
             return(false);
         }
         if (Math.Abs(a[i].y() - b[i].y()) > fuzz)
         {
             return(false);
         }
     }
     return(true);
 }
示例#21
0
        public void setClockWise(LDPointList form, ClockWise clockWise)
        {
            Debug.Assert(form.length() > m_index1);
            Debug.Assert(form.length() > m_index2);
            Debug.Assert(form.length() > m_index3);

            LDPoint v0 = form.at(m_index1);
            LDPoint v1 = form.at(m_index2);
            LDPoint v2 = form.at(m_index3);

            //行列式で時計回りか判定
            //行列式の計算。 QMatrix3x3にはないので手動で作成。

            double[,] m = new double[3, 3] {
                { v0.x(), v0.y(), 1 },
                { v1.x(), v1.y(), 1 },
                { v2.x(), v2.y(), 1 }
            };

            double determinant = m[0, 0] * m[1, 1] * m[2, 2]
                                 + m[0, 1] * m[1, 2] * m[2, 0]
                                 + m[0, 2] * m[1, 0] * m[2, 1]
                                 - m[0, 2] * m[1, 1] * m[2, 0]
                                 - m[0, 0] * m[1, 2] * m[2, 1]
                                 - m[0, 1] * m[1, 0] * m[2, 2];
            ClockWise current;

            if (determinant < 0) //CW
            {
                current = ClockWise.CW;
            }
            else            //CCWまたは3点が一直線上など
            {
                current = ClockWise.CCW;
            }

            if (clockWise != current) //設定した順番と異なる場合 Indexを入れ替える
            {
                var p = m_index1;
                m_index1 = m_index2;
                m_index2 = p;
            }
        }
示例#22
0
        public void getOutlinePointIndicesTest_simple()
        {
            LDTriangleList triangles = simpleTriangle();

            LDPointList points = new LDPointList();

            points.Add(new LDPoint(0, 0));
            points.Add(new LDPoint(0, 100));
            points.Add(new LDPoint(100, 100));

            List <int> compare = new List <int>();

            compare.Add(0);
            compare.Add(1);
            compare.Add(2);

            List <int> result = triangles.getOutlinePointIndices(points);

            TestUtil.COMPARELIST(result, compare);
        }
示例#23
0
        // 三角形を与えてその外接円を求める
        public static LDCircle getCircumscribedCirclesOfTriangle(LDPointList form, LDTriangle t)
        {
            // 三角形の各頂点座標を (x1, y1), (x2, y2), (x3, y3) とし、
            // その外接円の中心座標を (x, y) とすると、
            //     (x - x1) * (x - x1) + (y - y1) * (y - y1)
            //   = (x - x2) * (x - x2) + (y - y2) * (y - y2)
            //   = (x - x3) * (x - x3) + (y - y3) * (y - y3)
            // より、以下の式が成り立つ
            //
            // x = { (y3 - y1) * (x2 * x2 - x1 * x1 + y2 * y2 - y1 * y1)
            //     + (y1 - y2) * (x3 * x3 - x1 * x1 + y3 * y3 - y1 * y1)} / c
            //
            // y = { (x1 - x3) * (x2 * x2 - x1 * x1 + y2 * y2 - y1 * y1)
            //     + (x2 - x1) * (x3 * x3 - x1 * x1 + y3 * y3 - y1 * y1)} / c
            //
            // ただし、
            //   c = 2 * {(x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1)}

            LDPoint p1 = t.getPoint1(form);
            LDPoint p2 = t.getPoint2(form);
            LDPoint p3 = t.getPoint3(form);
            float   x1 = (float)p1.x();
            float   y1 = (float)p1.y();
            float   x2 = (float)p2.x();
            float   y2 = (float)p2.y();
            float   x3 = (float)p3.x();
            float   y3 = (float)p3.y();

            float c = 2.0f * ((x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1));
            float x = ((y3 - y1) * (x2 * x2 - x1 * x1 + y2 * y2 - y1 * y1)
                       + (y1 - y2) * (x3 * x3 - x1 * x1 + y3 * y3 - y1 * y1)) / c;
            float y = ((x1 - x3) * (x2 * x2 - x1 * x1 + y2 * y2 - y1 * y1)
                       + (x2 - x1) * (x3 * x3 - x1 * x1 + y3 * y3 - y1 * y1)) / c;

            LDPoint center = new LDPoint(x, y);

            // 外接円の半径 r は、半径から三角形の任意の頂点までの距離に等しい
            double r = math.PointUtil.distance(center, p1);

            return(new LDCircle(center, (float)r));
        }
示例#24
0
 /// <summary>
 /// 未実装
 /// </summary>
 /// <param name="form"></param>
 public void replaceForm(LDPointList form)
 {
     throw new NotImplementedException();
 }
示例#25
0
        public void simpleTransformTest()
        {
            LDQuadTransform quad = new LDQuadTransform(new LDPoint(10, 10), new LDPoint(30, 30));

            {
                LDPoint src = new LDPoint(0, 0);

                LDPoint dst;

                dst = quad.transform(src);

                TestUtil.COMPARE(dst.x(), 10.0);
                TestUtil.COMPARE(dst.y(), 10.0);
            }

            {
                LDPoint src = new LDPoint(1, 1);

                LDPoint dst;

                dst = quad.transform(src);

                TestUtil.COMPARE(dst.x(), 30.0);
                TestUtil.COMPARE(dst.y(), 30.0);
            }


            {
                LDPoint src = new LDPoint(0.5f, 0.25f);

                LDPoint dst;

                dst = quad.transform(src);

                TestUtil.COMPARE(dst.x(), 20.0f);
                TestUtil.COMPARE(dst.y(), 15.0f);
            }
            {
                LDPoint src = new LDPoint(0.75f, 0.8f);

                LDPoint dst;

                dst = quad.transform(src);

                TestUtil.COMPARE(dst.x(), 25.0f);
                TestUtil.COMPARE(dst.y(), 26.0f);
            }
            {
                LDPointList src = new LDPointList();
                src.add(new LDPoint(0.25f, 0.4f)).add(new LDPoint(0.75f, 0.8f));

                LDPointList dst;

                dst = quad.transform(src);

                TestUtil.VERIFY(dst.length() == 2);

                TestUtil.COMPARE(dst[0], new LDPoint(15.0f, 18.0f));
                TestUtil.COMPARE(dst[1], new LDPoint(25, 26));
            }

            //	{
            //		LDGridTransform src(0.5,0.5,0.5,0.5,1,1);

            //		LDGridTransform dst;

            //		dst=grid.transform(src);

            //		TestUtil.COMPARE(dst.getPoint(0,0),LDPoint(40,40));
            //		TestUtil.COMPARE(dst.getPoint(0,1),LDPoint(60,40));
            //		TestUtil.COMPARE(dst.getPoint(1,1),LDPoint(60,60));
            //		TestUtil.COMPARE(dst.getPoint(1,0),LDPoint(40,60));
            //	}

            //	{
            //		LDAffineTransform src;
            //		src.translate(0.5,0.5);
            //		src.rotate(30);
            //		src.scale(2,2);

            //		LDAffineTransform dst;

            //		dst=grid.transform(src);

            //		TestUtil.COMPARE(dst.getTranslate(),LDPoint(40,40));
            //	}
        }
示例#26
0
        public void inverseTransformTest()
        {
            {
                LDGridTransform grid = new LDGridTransform(20, 20, 40, 40, 2, 2);
                LDPoint         src  = new LDPoint(30, 30);

                LDPoint dst = new LDPoint();

                dst = grid.inverseTransform(src);

                TestUtil.COMPARE(dst.x(), 0.25);
                TestUtil.COMPARE(dst.y(), 0.25);
            }

            {
                LDGridTransform grid = new LDGridTransform(20, 20, 40, 40, 2, 2);
                LDPointList     src  = new LDPointList();
                src.add(new LDPoint(30, 30)).add(new LDPoint(50, 50));

                LDPointList dst;

                dst = grid.inverseTransform(src);

                TestUtil.COMPARE(dst[0], new LDPoint(0.25f, 0.25f));
                TestUtil.COMPARE(dst[1], new LDPoint(0.75f, 0.75f));
            }

            /*
             * {
             *  LDGridTransform grid = new LDGridTransform(20, 20, 40, 40, 2, 2);
             *  LDGridTransform src = new LDGridTransform(24, 24, 52 - 24, 52 - 24, 2, 2);
             *
             *  LDGridTransform dst = src;
             *
             *  var points = grid.inverseTransform(src.toForm());
             *
             *
             *  dst.setForm(points);
             *  LDFUZZY_COMPARE(dst.getPoint(0, 0).x(), 0.1f, 0.0000001f);
             *  LDFUZZY_COMPARE(dst.getPoint(2, 2).x(), 0.8f, 0.0000001f);
             *  LDFUZZY_COMPARE(dst.getPoint(2, 0).x(), 0.1f, 0.0000001f);
             *  LDFUZZY_COMPARE(dst.getPoint(2, 0).y(), 0.8f, 0.0000001f);
             * }
             */
            {
                LDGridTransform grid = new LDGridTransform(20.53125f, 20.62423f, 40.614312f, 40.94645f, 2, 2);
                LDGridTransform src  = new LDGridTransform(24.0134623f, 24.9143f, 52 - 24.090023f, 52 - 24.00001f, 2, 2);

                LDGridTransform dst = new LDGridTransform(src);

                var points = grid.inverseTransform(src.toForm());
                var rest   = grid.transform(points);

                dst.setForm(points);
                TestUtil.VERIFY(LDMathUtil.fuzzyCompare(rest, src.toForm(), 0.0000001f));
            }
            {
                LDGridTransform grid = new LDGridTransform(2530.53125f, 2540.62423f, 4015.614312f, 4026.94645f, 2, 2);
                LDGridTransform src  = new LDGridTransform(2594.0134623f, 2594.9143f, 5274 - 2594.090023f, 5276 - 2594.00001f, 2, 2);

                LDGridTransform dst = new LDGridTransform(src);

                var points = grid.inverseTransform(src.toForm());
                var rest   = grid.transform(points);

                dst.setForm(points);
                TestUtil.VERIFY(LDMathUtil.fuzzyCompare(rest, src.toForm(), 0.0000001f));
            }
            {
                LDGridTransform grid = new LDGridTransform(
                    new LDPoint(20.53125f, 20.62423f)
                    , new LDPoint(40.53125f, 20.62423f)
                    , new LDPoint(45.53125f, 45.62423f)
                    , new LDPoint(20.614312f, 40.94645f), 2, 2);
                LDGridTransform src = new LDGridTransform(34.0134623f, 24.9143f, 52 - 24.090023f, 52 - 24.00001f, 8, 8);

                LDGridTransform dst = new LDGridTransform(src);

                var points = grid.inverseTransform(src.toForm());
                var rest   = grid.transform(points);

                dst.setForm(points);
                TestUtil.VERIFY(LDMathUtil.fuzzyCompare(rest, src.toForm(), 0.0000001f));
            }
            {
                LDGridTransform grid = new LDGridTransform(
                    new LDPoint(2012.53125f, 2051.62423f)
                    , new LDPoint(4097.53125f, 2033.62423f)
                    , new LDPoint(4575.53125f, 4566.62423f)
                    , new LDPoint(2062.614312f, 4000.94645f), 2, 2);
                LDGridTransform src = new LDGridTransform(3444.0134623f, 2442.9143f, 5242 - 2412.090023f, 5211 - 2467.00001f, 8, 8);

                LDGridTransform dst = new LDGridTransform(src);

                var points = grid.inverseTransform(src.toForm());
                var rest   = grid.transform(points);

                dst.setForm(points);
                TestUtil.VERIFY(LDMathUtil.fuzzyCompare(rest, src.toForm(), 0.0000001f));
            }
        }
示例#27
0
        //外周の頂点インデックスを取得。yが一番小さい点から時計周りで取得。
        public List <int> getOutlinePointIndices(LDPointList points)
        {
            List <int> result = new List <int>();

            // 外周をたどる。始点に戻ったら終了。
            int startIndex = math.PointUtil.findMinYPointIndex(points);

            Debug.Assert(startIndex >= 0);

            result.Add(startIndex);

            int lastIndex    = -1;
            int currentIndex = startIndex;

            for (int i = 0; i < points.length(); ++i)
            {
                //現在の頂点と接続される点一覧を取得し、その中から進行方向に対してもっとも左側に位置するものを取得
                List <int> related = getRelatedPointIndices(currentIndex);

                LDPoint lastPoint;
                LDPoint currentPoint = points[currentIndex];
                if (lastIndex == -1)
                {
                    lastPoint = currentPoint - new LDPoint(0, 1);
                }
                else
                {
                    lastPoint = points[lastIndex];
                }


                int    nextIndex = -1;
                double minAngle  = 360;
                foreach (var targetIndex in related)
                {
                    LDPoint targetPoint = points[targetIndex];
                    if (targetIndex == lastIndex)
                    {
                        continue;
                    }
                    LDVector2 v1 = new LDVector2(lastPoint - currentPoint);
                    LDVector2 v2 = new LDVector2(targetPoint - currentPoint);

                    double angle = LDMathUtil.getAngle(v2, v1);
                    if (angle < minAngle)
                    {
                        minAngle  = angle;
                        nextIndex = targetIndex;
                    }
                }

                if (nextIndex == startIndex)
                {
                    //一周した
                    break;
                }
                result.Add(nextIndex);

                lastIndex    = currentIndex;
                currentIndex = nextIndex;
            }

            return(result);
        }
示例#28
0
 public LDLineList getLines(LDPointList points)
 {
     return(toIndexLineList().getLines(points));
 }
示例#29
0
 public LDPoint getPoint3(LDPointList points)
 {
     return(points[m_index2]);
 }
示例#30
0
 public LDPoint getPoint1(LDPointList points)
 {
     return(points[m_index1]);
 }