示例#1
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);
        }
示例#2
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);
        }
示例#3
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);
        }