Exemplo n.º 1
0
        public IList <KeyValuePair <uint, bool> > GetEdgesForConnectVertex(VertexEdgeItr vItr1, VertexEdgeItr vItr2)
        {
            IList <KeyValuePair <uint, bool> > eId2Dir = new List <KeyValuePair <uint, bool> >();;

            uint uVId1 = vItr1.GetUseVertexId();
            uint uVId2 = vItr2.GetUseVertexId();

            if (!BRep.IsUseVertexId(uVId1))
            {
                return(eId2Dir);
            }
            if (!BRep.IsUseVertexId(uVId2))
            {
                return(eId2Dir);
            }
            if (uVId1 == uVId2)
            {
                return(eId2Dir);
            }
            if (vItr1.GetLoopId() != vItr2.GetLoopId())
            {
                return(eId2Dir);
            }

            uint hEId1 = vItr1.GetHalfEdgeId();
            uint uLId1;
            {
                System.Diagnostics.Debug.Assert(BRep.IsHalfEdgeId(hEId1));
                HalfEdge hE1 = BRep.GetHalfEdge(hEId1);
                uLId1 = hE1.ULId;
                System.Diagnostics.Debug.Assert(hE1.EId != 0);
            }
            uint hEId2 = vItr2.GetHalfEdgeId();
            uint uLId2;

            {
                System.Diagnostics.Debug.Assert(BRep.IsHalfEdgeId(hEId2));
                HalfEdge hE2 = BRep.GetHalfEdge(hEId2);
                uLId2 = hE2.ULId;
                System.Diagnostics.Debug.Assert(hE2.EId != 0);
            }

            if (uLId1 != uLId2)
            {
                return(eId2Dir);
            }
            uint hEId = hEId2;

            while (true)
            {
                HalfEdge hE = BRep.GetHalfEdge(hEId);
                eId2Dir.Add(new KeyValuePair <uint, bool>(hE.EId, hE.IsSameDir));
                hEId = hE.FHEId;
                if (hEId == hEId1)
                {
                    break;
                }
            }
            return(eId2Dir);
        }
Exemplo n.º 2
0
 public VertexEdgeItr(VertexEdgeItr src)
 {
     IsValid   = src.IsValid;
     IsInitial = src.IsInitial;
     UVId      = src.UVId;
     HEId      = src.HEId;
     BRep2D.Copy(src.BRep2D);
 }
Exemplo n.º 3
0
        public bool RemoveVertex(uint vId)
        {
            uint uVId = vId;

            if (!BRep.IsUseVertexId(uVId))
            {
                return(false);
            }

            uint nEdgeAroundVtx = 0;

            {
                VertexEdgeItr vItr = new VertexEdgeItr(this, vId);
                nEdgeAroundVtx = vItr.GetEdgeCount();
            }

            if (nEdgeAroundVtx == 0)
            {
                System.Diagnostics.Debug.Assert(BRep.IsUseVertexId(uVId));
                if (!BRep.KVEL(uVId))
                {
                    System.Diagnostics.Debug.Assert(false);
                }
            }
            else if (nEdgeAroundVtx == 2)
            {
                UseVertex uV    = BRep.GetUseVertex(uVId);
                uint      hEId1 = uV.HEId;
                HalfEdge  hE1   = BRep.GetHalfEdge(hEId1);
                uint      eId1  = hE1.EId;
                uint      hEId2 = hE1.OHEId;
                HalfEdge  hE2   = BRep.GetHalfEdge(hEId2);
                {
                    uint        uLId1 = hE1.ULId;
                    uint        uLId2 = hE2.ULId;
                    LoopEdgeItr lItr1 = new LoopEdgeItr(this, hEId1, uLId1);
                    LoopEdgeItr lItr2 = new LoopEdgeItr(this, hEId2, uLId2);
                    uint        nLV1  = lItr1.GetUseLoopVertexCount();
                    uint        nLV2  = lItr2.GetUseLoopVertexCount();
                    System.Diagnostics.Debug.Assert(nLV1 > 1 && nLV2 > 1);
                    if (nLV1 == 2 || nLV2 == 2)
                    {
                        return(false);
                    }
                }
                uint uVId2 = hE2.UVId;
                System.Diagnostics.Debug.Assert(BRep.IsUseVertexId(uVId2));
                uint removeHEId1 = hEId1;
                if (!BRep.KVE(removeHEId1))
                {
                    System.Diagnostics.Debug.Assert(false);
                }
                System.Diagnostics.Debug.Assert(BRep.AssertValidUse() == 0);
                Edge2HalfEdge.Remove(eId1);
            }
            System.Diagnostics.Debug.Assert(AssertValid());
            return(true);
        }
Exemplo n.º 4
0
        public bool IsSameUseLoop(VertexEdgeItr lItr)
        {
            if (!IsValid)
            {
                return(false);
            }
            System.Diagnostics.Debug.Assert(BRep2D.BRep.IsHalfEdgeId(HEId));
            HalfEdge hE0 = BRep2D.BRep.GetHalfEdge(HEId);

            if (!lItr.IsValid)
            {
                return(false);
            }
            System.Diagnostics.Debug.Assert(lItr.BRep2D.BRep.IsHalfEdgeId(lItr.HEId));
            HalfEdge hE1 = lItr.BRep2D.BRep.GetHalfEdge(lItr.HEId);

            return(hE0.ULId == hE1.ULId);
        }
Exemplo n.º 5
0
        public static VertexEdgeItr operator ++(VertexEdgeItr src)
        {
            VertexEdgeItr dest = new VertexEdgeItr(src);

            if (!src.IsValid)
            {
                return(dest);
            }
            dest.IsInitial = false;
            System.Diagnostics.Debug.Assert(dest.BRep2D.BRep.IsHalfEdgeId(dest.HEId));
            HalfEdge hE    = dest.BRep2D.BRep.GetHalfEdge(dest.HEId);
            uint     bHEId = hE.BHEId;

            System.Diagnostics.Debug.Assert(dest.BRep2D.BRep.IsHalfEdgeId(bHEId));
            HalfEdge bHE = dest.BRep2D.BRep.GetHalfEdge(bHEId);

            dest.HEId = bHE.OHEId;
            return(dest);
        }
Exemplo n.º 6
0
        public ConnectVertexRes ConnectVertex(VertexEdgeItr vItr1, VertexEdgeItr vItr2, bool isLeftAddL)
        {
            ConnectVertexRes res = new ConnectVertexRes();

            uint uVId1 = vItr1.GetUseVertexId();
            uint uVId2 = vItr2.GetUseVertexId();

            if (!BRep.IsUseVertexId(uVId1))
            {
                return(res);
            }
            if (!BRep.IsUseVertexId(uVId2))
            {
                return(res);
            }
            if (uVId1 == uVId2)
            {
                return(res);
            }
            res.VId1 = BRep.GetUseVertex(uVId1).VId;
            res.VId2 = BRep.GetUseVertex(uVId2).VId;

            if (vItr1.GetLoopId() != vItr2.GetLoopId())
            {
                return(res);
            }
            uint lId = vItr1.GetLoopId();

            res.LId = lId;

            uint hEId1 = vItr1.GetHalfEdgeId();
            uint hEId2 = vItr2.GetHalfEdgeId();

            uint uLId1;
            bool isFloat1;
            {
                System.Diagnostics.Debug.Assert(BRep.IsHalfEdgeId(hEId1));
                HalfEdge hE1 = BRep.GetHalfEdge(hEId1);
                uLId1 = hE1.ULId;
                System.Diagnostics.Debug.Assert(BRep.IsUseLoopId(uLId1));
                isFloat1 = (hE1.EId == 0);
            }
            uint uLId2;
            bool isFloat2;

            {
                System.Diagnostics.Debug.Assert(BRep.IsHalfEdgeId(hEId2));
                HalfEdge hE2 = BRep.GetHalfEdge(hEId2);
                uLId2 = hE2.ULId;
                System.Diagnostics.Debug.Assert(BRep.IsUseLoopId(uLId2));
                isFloat2 = (hE2.EId == 0);
            }

            if (uLId1 != uLId2)
            {
                if (isFloat1 && !isFloat2)
                {
                    uint addHEId1;
                    if (!BRep.MEKLOneFloatingVertex(out addHEId1, hEId2, hEId1))
                    {
                        System.Diagnostics.Debug.Assert(false);
                    }
                    uint addEId = GetFreeKey(Edge2HalfEdge);
                    BRep.SetHalfEdgeEdgeId(addHEId1, addEId, false);
                    BRep.SetHalfEdgeEdgeId(hEId1, addEId, true);
                    Edge2HalfEdge.Add(addEId, hEId1);
                    System.Diagnostics.Debug.Assert(AssertValid());
                    res.AddEId = addEId;
                    return(res);
                }
                else if (isFloat2 && !isFloat1)
                {
                    uint addHEId1;
                    if (!BRep.MEKLOneFloatingVertex(out addHEId1, hEId1, hEId2))
                    {
                        System.Diagnostics.Debug.Assert(false);
                    }
                    uint addEId = GetFreeKey(Edge2HalfEdge);
                    BRep.SetHalfEdgeEdgeId(addHEId1, addEId, true);
                    BRep.SetHalfEdgeEdgeId(hEId2, addEId, false);
                    Edge2HalfEdge.Add(addEId, addHEId1);
                    System.Diagnostics.Debug.Assert(AssertValid());
                    res.AddEId = addEId;
                    return(res);
                }
                else if (isFloat1 && isFloat2)
                {
                    if (!BRep.MEKLTwoFloatingVertex(hEId1, hEId2))
                    {
                        System.Diagnostics.Debug.Assert(false);
                    }
                    uint addEId = GetFreeKey(Edge2HalfEdge);
                    BRep.SetHalfEdgeEdgeId(hEId1, addEId, true);
                    BRep.SetHalfEdgeEdgeId(hEId2, addEId, false);
                    Edge2HalfEdge.Add(addEId, hEId1);
                    System.Diagnostics.Debug.Assert(AssertValid());
                    res.AddEId = addEId;
                    return(res);
                }
                else
                {
                    uint addHEId1;
                    uint addHEId2;
                    if (!BRep.MEKL(out addHEId1, out addHEId2, hEId1, hEId2))
                    {
                        System.Diagnostics.Debug.Assert(false);
                    }
                    uint addEId = GetFreeKey(Edge2HalfEdge);
                    BRep.SetHalfEdgeEdgeId(addHEId1, addEId, true);
                    BRep.SetHalfEdgeEdgeId(addHEId2, addEId, false);
                    Edge2HalfEdge.Add(addEId, addHEId1);
                    if (lId != 0)
                    {
                        uint parentULId1 = BRep.GetUseLoop(uLId1).ParentULId;
                        System.Diagnostics.Debug.Assert(parentULId1 != 0);
                        System.Diagnostics.Debug.Assert(Loop2UseLoop.ContainsKey(lId));
                        Loop2UseLoop[lId] = parentULId1;
                    }
                    else
                    {
                    }
                    System.Diagnostics.Debug.Assert(AssertValid());
                    res.AddEId = addEId;
                    return(res);
                }
            }
            else if (uLId1 == uLId2)
            {
                uint addHEId1;
                uint addHEId2;
                uint addULId;
                if (!BRep.MEL(out addHEId1, out addHEId2, out addULId, hEId1, hEId2))
                {
                    System.Diagnostics.Debug.Assert(false);
                }
                System.Diagnostics.Debug.Assert(BRep.AssertValidUse() == 0);
                uint addEId = GetFreeKey(Edge2HalfEdge);
                BRep.SetHalfEdgeEdgeId(addHEId1, addEId, true);
                BRep.SetHalfEdgeEdgeId(addHEId2, addEId, false);

                Edge2HalfEdge.Add(addEId, addHEId1);
                bool iflag = true;
                if (lId == 0)
                {
                    iflag = !isLeftAddL;
                }
                else
                {
                    uint parentULId = Loop2UseLoop[lId];
                    if (uLId1 != parentULId)
                    {
                        if (isLeftAddL)
                        {
                            BRep.SwapUseLoop(addULId, uLId1);
                            BRep.SetUseLoopLoopId(addULId, lId);
                            BRep.AssertValidUse();
                            iflag = false;
                        }
                        else
                        {
                        }
                    }
                    else
                    {
                    }
                }

                uint addLId = GetFreeKey(Loop2UseLoop);
                if (iflag)
                {
                    Loop2UseLoop.Add(addLId, addULId);
                    BRep.SetUseLoopLoopId(addULId, addLId);
                }
                else
                {
                    Loop2UseLoop.Add(addLId, uLId1);
                    BRep.SetUseLoopLoopId(uLId1, addLId);
                }
                System.Diagnostics.Debug.Assert(AssertValid());
                res.AddEId     = addEId;
                res.AddLId     = addLId;
                res.IsLeftAddL = !iflag;
                return(res);
            }
            System.Diagnostics.Debug.Assert(false);
            return(res);
        }
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 MoveVertex(uint vId, OpenTK.Vector2d vec)
        {
            if (!VertexArray.IsObjectId(vId))
            {
                return(false);
            }

            OpenTK.Vector2d preVec;
            {
                // store point to move point back in case it fails
                Vertex2D vertex = VertexArray.GetObject(vId);
                preVec = vertex.Point;
            }

            OpenTK.Vector2d dist = vec;
            {
                // move point
                Vertex2D vertex = VertexArray.GetObject(vId);
                vertex.Point = dist;
            }

            try
            {
                VertexEdgeItr vItr = this.BRep.GetVertexEdgeItr(vId);
                if (vItr.GetEdgeCount() == 0)
                {
                    // move point inside loop
                    uint lId = vItr.GetLoopId();
                    if (IsElementId(CadElementType.Loop, lId))
                    {
                        // ignore vtx(id_v) in the signd distance computation
                        double distance = SignedDistancePointLoop(lId, vec, vId);
                        if (distance < this.MinClearance)
                        {
                            throw new InvalidOperationException();
                        }
                    }
                }
                else
                {
                    // move point adjacent to loop
                    HashSet <uint> lIds = new HashSet <uint>();
                    for (; !vItr.IsEnd(); vItr++)
                    {
                        uint lId = vItr.GetLoopId();
                        if (IsElementId(CadElementType.Loop, lId))
                        {
                            if (!lIds.Contains(lId))
                            {
                                lIds.Add(lId);
                            }
                            else
                            {
                                continue;
                            }
                            if (CheckLoop(lId) != 0)
                            {
                                throw new InvalidOperationException();
                            }
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                ////////////////////////////////
                // fail
                {   // reset the moved point
                    Vertex2D vertex = VertexArray.GetObject(vId);
                    vertex.Point = preVec;
                }
                return(false);
            }
            return(true);
        }
Exemplo n.º 9
0
        public bool MoveEdge(uint eId, OpenTK.Vector2d vecDelta)
        {
            System.Diagnostics.Debug.Assert(IsElementId(CadElementType.Edge, eId));
            uint sVId = GetEdgeVertexId(eId, true);
            uint eVId = GetEdgeVertexId(eId, false);

            OpenTK.Vector2d preSVec;
            {
                // 点を動かす→駄目だったら元に戻す
                Vertex2D vertex = VertexArray.GetObject(sVId);
                preSVec = vertex.Point;
                {
                    double oldX = vertex.Point.X;
                    double oldY = vertex.Point.Y;
                    vertex.Point = new OpenTK.Vector2d(oldX + vecDelta.X, oldY + vecDelta.Y);
                }
            }

            OpenTK.Vector2d preEVec;
            {   // 点を動かす→駄目だったら元に戻す
                Vertex2D ver = VertexArray.GetObject(eVId);
                preEVec = ver.Point;
                {
                    double oldX = ver.Point.X;
                    double oldY = ver.Point.Y;
                    ver.Point = new OpenTK.Vector2d(oldX + vecDelta.X, oldY + vecDelta.Y);
                }
            }

            try
            {
                // Check Interfarance
                HashSet <uint> lIds = new HashSet <uint>();
                for (VertexEdgeItr vItr = this.BRep.GetVertexEdgeItr(sVId); !vItr.IsEnd(); vItr++)
                {
                    uint lId = vItr.GetLoopId();
                    if (IsElementId(CadElementType.Loop, lId))
                    {
                        if (!lIds.Contains(lId))
                        {
                            lIds.Add(lId);
                        }
                        else
                        {
                            continue;
                        }
                        if (CheckLoop(lId) != 0)
                        {
                            throw new InvalidOperationException();
                        }
                    }
                }
                for (VertexEdgeItr vItr = this.BRep.GetVertexEdgeItr(eVId); !vItr.IsEnd(); vItr++)
                {
                    uint lId = vItr.GetLoopId();
                    if (IsElementId(CadElementType.Loop, lId))
                    {
                        if (!lIds.Contains(lId))
                        {
                            lIds.Add(lId);
                        }
                        else
                        {
                            continue;
                        }
                        if (CheckLoop(lId) != 0)
                        {
                            throw new InvalidOperationException();
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                ////////////////////////////////
                //if the operation fails
                {   // 動かした点を元に戻す
                    Vertex2D vertex = VertexArray.GetObject(sVId);
                    vertex.Point = preSVec;
                }
                {   // 動かした点を元に戻す
                    Vertex2D vertex = VertexArray.GetObject(eVId);
                    vertex.Point = preEVec;
                }
                return(false);
            }

            return(true);
        }
Exemplo n.º 10
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);
        }