Exemplo n.º 1
0
        private void MakeNormal()
        {
            uint ptCnt = VertexArray.PointCount;

            if (NormalArray == null)
            {
                NormalArray = new double[ptCnt * 3];
            }
            for (int i = 0; i < 3 * ptCnt; i++)
            {
                NormalArray[i] = 0;
            }
            for (int idp = 0; idp < DrawParts.Count; idp++)
            {
                FaceFieldDrawPart dp = DrawParts[idp];
                uint elemCnt         = dp.ElemCount;
                for (int iElem = 0; iElem < elemCnt; iElem++)
                {
                    uint[]          vertexs = dp.GetVertexs((uint)iElem);
                    OpenTK.Vector3d c0      = new OpenTK.Vector3d(
                        VertexArray.VertexCoordArray[vertexs[0] * 3],
                        VertexArray.VertexCoordArray[vertexs[0] * 3 + 1],
                        VertexArray.VertexCoordArray[vertexs[0] * 3 + 2]);
                    OpenTK.Vector3d c1 = new OpenTK.Vector3d(
                        VertexArray.VertexCoordArray[vertexs[1] * 3],
                        VertexArray.VertexCoordArray[vertexs[1] * 3 + 1],
                        VertexArray.VertexCoordArray[vertexs[1] * 3 + 2]);
                    OpenTK.Vector3d c2 = new OpenTK.Vector3d(
                        VertexArray.VertexCoordArray[vertexs[2] * 3],
                        VertexArray.VertexCoordArray[vertexs[2] * 3 + 1],
                        VertexArray.VertexCoordArray[vertexs[2] * 3 + 2]);
                    double[] n;
                    double   area;
                    CadUtils.UnitNormalAreaTri3D(out n, out area, c0, c1, c2);
                    NormalArray[vertexs[0] * 3 + 0] += n[0];
                    NormalArray[vertexs[0] * 3 + 1] += n[1];
                    NormalArray[vertexs[0] * 3 + 2] += n[2];
                    NormalArray[vertexs[1] * 3 + 0] += n[0];
                    NormalArray[vertexs[1] * 3 + 1] += n[1];
                    NormalArray[vertexs[1] * 3 + 2] += n[2];
                    NormalArray[vertexs[2] * 3 + 0] += n[0];
                    NormalArray[vertexs[2] * 3 + 1] += n[1];
                    NormalArray[vertexs[2] * 3 + 2] += n[2];
                }
            }
            for (int iPt = 0; iPt < ptCnt; iPt++)
            {
                double[] p = new double[3];
                p[0] = NormalArray[iPt * 3];
                p[1] = NormalArray[iPt * 3 + 1];
                p[2] = NormalArray[iPt * 3 + 2];
                double invLen = 1.0 / Math.Sqrt(p[0] * p[0] + p[1] * p[1] + p[2] * p[2]);
                NormalArray[0] *= invLen;
                NormalArray[1] *= invLen;
                NormalArray[2] *= invLen;
            }
        }
Exemplo n.º 2
0
        public double GetArea()
        {
            double[]        co1  = World.GetVertexCoord(VertexCoordIds[0]);
            double[]        co2  = World.GetVertexCoord(VertexCoordIds[1]);
            double[]        co3  = World.GetVertexCoord(VertexCoordIds[2]);
            OpenTK.Vector2d v1   = new OpenTK.Vector2d(co1[0], co1[1]);
            OpenTK.Vector2d v2   = new OpenTK.Vector2d(co2[0], co2[1]);
            OpenTK.Vector2d v3   = new OpenTK.Vector2d(co3[0], co3[1]);
            double          area = CadUtils.TriArea(v1, v2, v3);

            return(area);
        }
Exemplo n.º 3
0
        private void ProjectPoint(double inX, double inY,
                                  ref int minDivIndex,
                                  out double alpha, out double ndist, out double normX, out double normY)
        {
            // dummy
            alpha = 0;
            ndist = 0;
            normX = 0;
            normY = 0;

            double[] x0   = { inX, inY };
            double   dist = Math.Sqrt((IniX[0] + Ut[0] - x0[0]) * (IniX[0] + Ut[0] - x0[0])
                                      + (IniX[1] + Ut[1] - x0[1]) * (IniX[1] + Ut[1] - x0[1]));

            minDivIndex = -1;
            uint ndiv = No - 1;

            for (int idiv = 0; idiv < ndiv; idiv++)
            {
                double[] x1 = { IniX[idiv * 2 + 0] + Ut[idiv * 3 + 0], IniX[idiv * 2 + 1] + Ut[idiv * 3 + 1] };
                double[] x2 = { IniX[idiv * 2 + 2] + Ut[idiv * 3 + 3], IniX[idiv * 2 + 3] + Ut[idiv * 3 + 4] };
                double   t  = CadUtils.FindNearestPointParameterLinePoint(
                    new OpenTK.Vector2d(x0[0], x0[1]),
                    new OpenTK.Vector2d(x1[0], x1[1]),
                    new OpenTK.Vector2d(x2[0], x2[0]));
                if (t < -0.001 || t > 1.001)
                {
                    continue;
                }
                double[] x3   = { x1[0] * (1 - t) + x2[0] * t, x1[1] * (1 - t) + x2[1] * t };
                double[] d    = { x0[0] - x3[0], x0[1] - x3[1] };
                double   elen = Math.Sqrt((x1[0] - x2[0]) * (x1[0] - x2[0]) + (x1[1] - x2[1]) * (x1[1] - x2[1]));
                double[] n    = { (x2[1] - x1[1]) / elen, (x1[0] - x2[0]) / elen };
                double   dlen = Math.Sqrt(d[0] * d[0] + d[1] * d[1]);
                if (dlen < dist)
                {
                    minDivIndex = idiv;
                    dist        = dlen;
                    alpha       = t;
                    ndist       = (n[0] * d[0] + n[1] * d[1]) / elen;
                    normX       = n[0];
                    normY       = n[1];
                }
            }
        }
Exemplo n.º 4
0
        public void CalcTransMatrix(out double[] a, out double[] b, out double[] c)
        {
            a = new double[3];
            b = new double[3];
            c = new double[3];
            double[]        co1 = World.GetVertexCoord(VertexCoordIds[0]);
            double[]        co2 = World.GetVertexCoord(VertexCoordIds[1]);
            double[]        co3 = World.GetVertexCoord(VertexCoordIds[2]);
            OpenTK.Vector2d v1  = new OpenTK.Vector2d(co1[0], co1[1]);
            OpenTK.Vector2d v2  = new OpenTK.Vector2d(co2[0], co2[1]);
            OpenTK.Vector2d v3  = new OpenTK.Vector2d(co3[0], co3[1]);
            double          A   = CadUtils.TriArea(v1, v2, v3);

            OpenTK.Vector2d[] v = { v1, v2, v3 };
            for (int k = 0; k < 3; k++)
            {
                int l = (k + 1) % 3;
                int m = (k + 2) % 3;
                a[k] = (1.0 / (2.0 * A)) * (v[l].X * v[m].Y - v[m].X * v[l].Y);
                b[k] = (1.0 / (2.0 * A)) * (v[l].Y - v[m].Y);
                c[k] = (1.0 / (2.0 * A)) * (v[m].X - v[l].X);
            }
        }
Exemplo n.º 5
0
        public void CalcTransMatrix(out double[] a, out double[] b)
        {
            a = new double[2];
            b = new double[2];
            double[] co1 = World.GetVertexCoord(VertexCoordIds[0]);
            double[] co2 = World.GetVertexCoord(VertexCoordIds[1]);
            co1 = AddDisplacement(0, co1);
            co2 = AddDisplacement(1, co2);
            OpenTK.Vector2d v1 = new OpenTK.Vector2d(co1[0], co1[1]);
            OpenTK.Vector2d v2 = new OpenTK.Vector2d(co2[0], co2[1]);
            var             t  = v2 - v1;

            t = CadUtils.Normalize(t);
            double l = GetLineLength();
            {
                a[0] = (1.0 / l) * OpenTK.Vector2d.Dot(v2, t);
                a[1] = (1.0 / l) * OpenTK.Vector2d.Dot(-v1, t);
            }
            {
                b[0] = (1.0 / l) * (-1.0);
                b[1] = (1.0 / l) * 1.0;
            }
        }
Exemplo n.º 6
0
        public bool SmoothingPolylineEdge(uint eId, uint nIter, OpenTK.Vector2d pos, double radius)
        {
            if (!this.IsElementId(CadElementType.Edge, eId))
            {
                return(false);
            }
            if (GetEdgeCurveType(eId) != CurveType.CurvePolyline)
            {
                return(true);
            }
            if (nIter == 0)
            {
                return(true);
            }
            Edge2D         edge = GetEdge(eId);
            IList <double> axys = edge.GetCurveRelPoint();

            System.Diagnostics.Debug.Assert(axys.Count % 2 == 0);
            uint         nno    = (uint)axys.Count / 2;
            IList <uint> aIndNo = new List <uint>();

            if (radius > 0)
            {
                uint[] tmp = new uint[nno];
                for (int i = 0; i < nno; i++)
                {
                    if (i < aIndNo.Count)
                    {
                        tmp[i] = aIndNo[i];
                    }
                    else
                    {
                        tmp[i] = 0;
                    }
                }
                aIndNo.Clear();
                aIndNo = tmp.ToList();

                OpenTK.Vector2d sPt = edge.GetVertex(true);
                OpenTK.Vector2d ePt = edge.GetVertex(false);
                var             v0  = ePt - sPt;
                OpenTK.Vector2d v1  = new OpenTK.Vector2d(-v0.Y, v0.X);
                for (int ino = 0; ino < nno; ino++)
                {
                    var p = sPt + v0 * axys[ino * 2 + 0] + v1 * axys[ino * 2 + 1];
                    if (CadUtils.SquareLength(p - pos) < radius * radius)
                    {
                        aIndNo.Add((uint)ino);
                    }
                }
            }
            else
            {
                aIndNo = new List <uint>();
                for (int ino = 0; ino < nno; ino++)
                {
                    aIndNo[ino] = (uint)ino;
                }
            }

            // strength of smoothing(0から1)
            double w = 0.8;

            for (uint iiter = 0; iiter < nIter; iiter++)
            {
                for (int iIndNo = 0; iIndNo < aIndNo.Count; iIndNo++)
                {
                    int      ino = (int)aIndNo[iIndNo];
                    double[] po0 = new double[2];
                    double[] po1 = new double[2];
                    double[] po2 = new double[2];
                    if (ino == 0)
                    {
                        po0[0] = 0;
                        po0[1] = 0;
                    }
                    else
                    {
                        po0[0] = axys[ino * 2 - 2];
                        po0[1] = axys[ino * 2 - 1];
                    }
                    po1[0] = axys[ino * 2 + 0];
                    po1[1] = axys[ino * 2 + 1];
                    if (ino == nno - 1)
                    {
                        po2[0] = 1; po2[1] = 0;
                    }
                    else
                    {
                        po2[0] = axys[ino * 2 + 2];
                        po2[1] = axys[ino * 2 + 3];
                    }
                    ////////////////
                    double[] mPt = { (po0[0] + po2[0]) * 0.5, (po0[1] + po2[1]) * 0.5 };
                    double[] dPt = { (1.0 - w) * po1[0] + w * mPt[0], (1.0 - w) * po1[1] + w * mPt[1] };
                    axys[ino * 2 + 0] = dPt[0];
                    axys[ino * 2 + 1] = dPt[1];
                }
            }
            edge.SetCurveRelPoint(axys);
            return(true);
        }
Exemplo n.º 7
0
        public bool DragArc(uint eId, OpenTK.Vector2d vec)
        {
            if (!IsElementId(CadElementType.Edge, eId))
            {
                return(false);
            }
            if (GetEdgeCurveType(eId) != CurveType.CurveArc)
            {
                return(true);
            }

            bool   oldIsLeftSide;
            double oldDist;
            {
                Edge2D e = GetEdge(eId);
                e.GetCurveArc(out oldIsLeftSide, out oldDist);
            }

            Edge2D edge = GetEdge(eId);

            System.Diagnostics.Debug.Assert(edge.CurveType == CurveType.CurveArc);
            {
                OpenTK.Vector2d sPt     = edge.GetVertex(true);
                OpenTK.Vector2d ePt     = edge.GetVertex(false);
                double          edgeLen = Math.Sqrt(CadUtils.SquareLength(sPt, ePt));
                if (Math.Abs(CadUtils.TriHeight(vec, sPt, ePt)) > edgeLen * 0.02)
                {
                    OpenTK.Vector2d cPt;
                    bool            ret = CadUtils.CenterCircumcircle(sPt, ePt, vec, out cPt);
                    System.Diagnostics.Debug.Assert(ret);
                    double dist      = CadUtils.TriHeight(cPt, sPt, ePt);
                    double distRatio = dist / edgeLen;
                    edge.SetCurveArc(CadUtils.TriArea(sPt, ePt, vec) > 0, distRatio);
                }
                else
                {
                    return(true);
                }
            }

            HashSet <uint> setLId = new HashSet <uint>();

            for (VertexEdgeItr vItr = this.BRep.GetVertexEdgeItr(edge.GetVertexId(true)); !vItr.IsEnd(); vItr++)
            {
                uint tmpLId = vItr.GetLoopId();
                if (!setLId.Contains(tmpLId))
                {
                    setLId.Add(tmpLId);
                }
            }
            for (VertexEdgeItr vItr = this.BRep.GetVertexEdgeItr(edge.GetVertexId(false)); !vItr.IsEnd(); vItr++)
            {
                uint tmpLId = vItr.GetLoopId();
                if (!setLId.Contains(tmpLId))
                {
                    setLId.Add(tmpLId);
                }
            }

            foreach (uint lId in setLId)
            {
                if (!this.BRep.IsElementId(CadElementType.Loop, lId))
                {
                    continue;
                }
                if (CheckLoop(lId) != 0)
                {
                    // fail
                    edge.SetCurveArc(oldIsLeftSide, oldDist);
                    return(true);
                }
            }
            return(true);
        }
Exemplo n.º 8
0
        public bool MoveEdgeCtrl(uint eId, uint iCtrl, OpenTK.Vector2d deltaVec)
        {
            if (!IsElementId(CadElementType.Edge, eId))
            {
                return(false);
            }

            IList <double> oldRelCos = null;
            {
                Edge2D e = GetEdge(eId);
                oldRelCos = e.GetCurveRelPoint();
            }

            Edge2D edge = GetEdge(eId);

            if (iCtrl >= edge.GetCurvePoint().Count)
            {
                return(true);
            }
            {
                OpenTK.Vector2d sPt    = edge.GetVertex(true);
                OpenTK.Vector2d ePt    = edge.GetVertex(false);
                double          sqLen  = CadUtils.SquareLength(ePt - sPt);
                var             eh     = (ePt - sPt) * (1 / sqLen);
                OpenTK.Vector2d ev     = new OpenTK.Vector2d(-eh.Y, eh.X);
                var             p0     = edge.GetCurvePoint()[(int)iCtrl] + deltaVec;
                IList <double>  aRelCo = edge.GetCurveRelPoint();
                aRelCo[(int)(iCtrl * 2 + 0)] = OpenTK.Vector2d.Dot(p0 - sPt, eh);
                aRelCo[(int)(iCtrl * 2 + 1)] = OpenTK.Vector2d.Dot(p0 - sPt, ev);
                edge.SetCurveRelPoint(aRelCo);
            }

            HashSet <uint> setLId = new HashSet <uint>();

            for (VertexEdgeItr vItr = this.BRep.GetVertexEdgeItr(edge.GetVertexId(true)); !vItr.IsEnd(); vItr++)
            {
                uint tmpLId = vItr.GetLoopId();
                if (!setLId.Contains(tmpLId))
                {
                    setLId.Add(tmpLId);
                }
            }
            for (VertexEdgeItr vItr = this.BRep.GetVertexEdgeItr(edge.GetVertexId(false)); !vItr.IsEnd(); vItr++)
            {
                uint tmpLId = vItr.GetLoopId();
                if (!setLId.Contains(tmpLId))
                {
                    setLId.Add(tmpLId);
                }
            }

            foreach (uint lId in setLId)
            {
                if (!this.BRep.IsElementId(CadElementType.Loop, lId))
                {
                    continue;
                }
                if (CheckLoop(lId) != 0)
                {
                    // fail
                    edge.SetCurveRelPoint(oldRelCos);
                    return(true);
                }
            }
            return(true);
        }