private IEnumerable <IntersectionInfo> GetApparentIntersectionsKdTree(IEnumerable <CurveVertex> danglingVertices, Transaction transaction)
        {
            // Create a KDTree for dangling vertices.
            var result       = new List <IntersectionInfo>();
            var visitedPairs = new Dictionary <CurveVertex, CurveVertex>();
            var kdTree       = new CurveVertexKdTree <CurveVertex>(danglingVertices, it => it.Point.ToArray(), ignoreZ: true);

            foreach (var danglingVertex in danglingVertices)
            {
                var neighbors = kdTree.NearestNeighbours(danglingVertex.Point.ToArray(), _tolerance * 2.0);
                if (!neighbors.Any())
                {
                    continue;
                }

                foreach (var neighbor in neighbors)
                {
                    if (visitedPairs.ContainsKey(neighbor))
                    {
                        continue;
                    }

                    // Mark it as visited.
                    visitedPairs[neighbor] = danglingVertex;
                    var info = CalcApparentIntersection(danglingVertex, neighbor, transaction);
                    if (info != null)
                    {
                        result.Add(info);
                    }
                }
            }
            return(result);
        }
Beispiel #2
0
        public override void Check(IEnumerable <ObjectId> selectedObjectIds)
        {
            if (!selectedObjectIds.Any())
            {
                return;
            }

            // 1. Create a kd tree
            var database    = Database;
            var allVertices = new List <CurveVertex>();

            using (var transaction = database.TransactionManager.StartTransaction())
            {
                foreach (var objectId in selectedObjectIds)
                {
                    var curve = transaction.GetObject(objectId, OpenMode.ForRead) as Curve;
                    if (curve == null)
                    {
                        continue;
                    }
                    var vertices = CurveUtils.GetDistinctVertices(curve, transaction);
                    allVertices.AddRange(vertices.Select(it => new CurveVertex(it, objectId)));
                }
                transaction.Commit();
            }
            var kdTree = new CurveVertexKdTree <CurveVertex>(allVertices, it => it.Point.ToArray(), ignoreZ: true);

            // 3. Filter out points that on same curve
            var visited = new HashSet <CurveVertex>();

            using (var tolerance = new SafeToleranceOverride(1E-5, 1E-5))
                foreach (var curveVertex in allVertices)
                {
                    if (visited.Contains(curveVertex))
                    {
                        continue;
                    }

                    var nears     = kdTree.NearestNeighbours(curveVertex.Point.ToArray(), _tolerance);
                    var qualified = new List <CurveVertex>();
                    qualified.Add(curveVertex);
                    foreach (var near in nears)
                    {
                        visited.Add(near);
                        if (near.Point == curveVertex.Point)
                        {
                            continue;
                        }
                        qualified.Add(near);
                    }
                    if (qualified.Count > 1)
                    {
                        _nearVertices.Add(qualified);
                    }
                }
        }
Beispiel #3
0
        private IEnumerable <ClusterNodesInfo> GetClusterNodesKdTree(IEnumerable <CurveVertex> danglingVertices, Transaction transaction)
        {
            var result = new List <ClusterNodesInfo>();
            var kdTree = new CurveVertexKdTree <CurveVertex>(danglingVertices, it => it.Point.ToArray(), ignoreZ: true);

            // Get all connections of kdTree
            foreach (var vertex in danglingVertices)
            {
                var neighbors = kdTree.NearestNeighbours(vertex.Point.ToArray(), _tolerance);
                if (!neighbors.Any())
                {
                    continue;
                }

                // Remove some invalid vertices.
                var list         = neighbors.ToList();
                int currentIndex = 0;
                while (currentIndex < list.Count - 1)
                {
                    var current = list[currentIndex];
                    for (int i = currentIndex + 1; i < list.Count; i++)
                    {
                        var dist = current.Point.DistanceTo(list[i].Point);
                        if (dist.Larger(_tolerance))
                        {
                            list.RemoveAt(currentIndex);
                            list.RemoveAt(i - 1);
                            break;
                        }
                    }
                    currentIndex++;
                }

                if (list.Count > 1)
                {
                    var node = new ClusterNodesInfo()
                    {
                        Vertices = list.ToArray()
                    };
                    result.Add(node);
                }
            }

            return(result);
        }
Beispiel #4
0
        private bool CheckDangling(CurveVertex vertex, IEnumerable <ObjectId> allIds,
                                   HashSet <ObjectId> visitedIds, Stack <CurveVertex> candidates,
                                   Dictionary <ObjectId, CurveVertex[]> curveVertices, CurveVertexKdTree <CurveVertex> kdTree)
        {
            bool isDangling = true;

            // Get entities which vertex is on.
            var neighbors   = kdTree.NearestNeighbours(vertex.Point.ToArray(), radius: 0.1);
            var adjacentIds = neighbors.Select(it => it.Id);

            foreach (ObjectId adjacentId in adjacentIds)
            {
                if (adjacentId == vertex.Id)
                {
                    continue;
                }

                // Check adjacent vertices
                if (!curveVertices.ContainsKey(adjacentId))
                {
                    continue;
                }

                var vertices = curveVertices[adjacentId];
                foreach (var adjVertex in vertices)
                {
                    // If it connect to vertex, isDangling is false.
                    if (adjVertex.Point == vertex.Point)
                    {
                        isDangling = false;
                    }
                    else if (!visitedIds.Contains(adjacentId) &&
                             (_forDrawing || allIds.Contains(adjacentId)))
                    {
                        candidates.Push(adjVertex);
                    }
                }

                // Mark it as visited.
                visitedIds.Add(adjacentId);
            }
            return(isDangling);
        }
Beispiel #5
0
        private IEnumerable <SEdge <CurveVertex> > VisitCurve(CurveVertex?sourceVertex, ObjectId id, IEnumerable <ObjectId> allIds,
                                                              HashSet <ObjectId> visitedIds, Stack <KeyValuePair <ObjectId, CurveVertex> > candidates,
                                                              Dictionary <ObjectId, CurveVertex[]> curveVertices, CurveVertexKdTree <CurveVertex> kdTree,
                                                              Transaction transaction)
        {
            // No need to handle it.
            if (!curveVertices.ContainsKey(id))
            {
                visitedIds.Add(id);
                return(new SEdge <CurveVertex> [0]);
            }

            var originVertices = curveVertices[id];
            var points         = originVertices.Select(it => it.Point).ToArray();

            // If none of the curve's end points is equal to the point, just return.
            // It means this curve is not a qualified one because it's not connected to sourceVertex.
            // Then it will be handled later because it belongs to another loop.
            if (sourceVertex != null && points.Length > 0 && !points.Contains(sourceVertex.Value.Point))
            {
                bool contains = false;
                foreach (var point in points)
                {
                    if (point.IsEqualTo(sourceVertex.Value.Point))
                    {
                        contains = true;
                    }
                }

                if (!contains)
                {
                    return(new SEdge <CurveVertex> [0]);
                }
            }

            // Mark it as visited.
            visitedIds.Add(id);
            // If there is no end points on the entity, just return.
            if (originVertices.Length <= 0)
            {
                return(new SEdge <CurveVertex> [0]);
            }

            // Remove the duplicate point
            // Note: if a curve has 2 same end points, means its a self-loop.
            var         distinctVertices = new List <CurveVertex>();
            CurveVertex current          = originVertices[0];

            distinctVertices.Add(current);
            for (int i = 1; i < originVertices.Length; i++)
            {
                if (originVertices[i] != current)
                {
                    current = originVertices[i];
                    distinctVertices.Add(current);
                }
            }

            var result = new List <SEdge <CurveVertex> >();

            // Push vertex and adjacent curve into candidates
            foreach (var vertex in distinctVertices)
            {
                if (sourceVertex != null && sourceVertex.Value.Point.IsEqualTo(vertex.Point))
                {
                    continue;
                }

                var adjacentVertices = kdTree.NearestNeighbours(vertex.Point.ToArray(), radius: 0.1);
                var adjacentIds      = adjacentVertices.Select(it => it.Id);
                foreach (ObjectId adjacentId in adjacentIds)
                {
                    // If it has been visited.
                    if (visitedIds.Contains(adjacentId))
                    {
                        var originVertex = new CurveVertex(vertex.Point, adjacentId);
                        // If this curve has been pushed into the candidates stack before, there must exist a loop.
                        // So we create a back edge to the original vertex.
                        // NOTE: Use candidates.Contains(new KeyValuePair<ObjectId, CurveVertex>(id, originVertex))
                        // will cause a problem if two point are very close to each other
                        // such as (10.5987284839403,10.2186528623464,0) and (10.5987284839408,10.2186528623464,0)
                        var existing = candidates.FirstOrDefault(it => it.Key == id && it.Value == originVertex);
                        if (!existing.Equals(default(KeyValuePair <ObjectId, CurveVertex>)))
                        {
                            // Create edge back to the old vertex.
                            result.Add(new SEdge <CurveVertex>(vertex, existing.Value));
                        }
                        //if (candidates.Contains(new KeyValuePair<ObjectId, CurveVertex>(id, originVertex)))
                        //{
                        //    // Create edge back to the old vertex.
                        //    result.Add(new SEdge<CurveVertex>(vertex, originVertex));
                        //}
                        continue;
                    }

                    // If it's not in the collection of selected ids.
                    if (!allIds.Contains(adjacentId))
                    {
                        continue;
                    }

                    candidates.Push(new KeyValuePair <ObjectId, CurveVertex>(adjacentId, vertex));
                }
            }

            // Create edges
            int start = 0;

            if (sourceVertex != null)
            {
                for (int i = 0; i < distinctVertices.Count; i++)
                {
                    if (distinctVertices[i].Point.IsEqualTo(sourceVertex.Value.Point))
                    {
                        start = i;
                        // Add edge from sourceVertex to nearest point.
                        result.Add(new SEdge <CurveVertex>(sourceVertex.Value, distinctVertices[start]));
                        break;
                    }
                }
            }

            if (distinctVertices.Count == 1)
            {
                // Allan 2015/05/06: Self loop won't be handled - to improve performance.

                // Self loop, create dummy vertex to let it to be a real loop
                if (_includeSelfLoop)
                {
                    var dummy = new CurveVertex(distinctVertices[0].Point + new Vector3d(1, 1, 0), id);
                    result.Add(new SEdge <CurveVertex>(distinctVertices[0], dummy));
                    result.Add(new SEdge <CurveVertex>(dummy, distinctVertices[0]));
                }
            }
            else
            {
                // Must start from the vertex whose poitn is equal to source vertex's point.
                // Up
                var previousVertex = distinctVertices[start];
                for (int i = start + 1; i < distinctVertices.Count; i++)
                {
                    // Create a middle point to fix a bug - to avoid a line's two vertices are both in loop,
                    // but actually the line is not in a loop, for example
                    //        /\             /\
                    //       /  \           /  \
                    //      /____\A_______B/____\
                    // Point A and point B are both in a loop, but line AB is not in a loop.
                    // If I add a dummy point C in the middle of AB, then our graph could easily
                    // to determine C is not in a loop.
                    var middlePoint  = previousVertex.Point + (distinctVertices[i].Point - previousVertex.Point) / 2.0;
                    var middleVertex = new CurveVertex(middlePoint, id);
                    result.Add(new SEdge <CurveVertex>(previousVertex, middleVertex));
                    result.Add(new SEdge <CurveVertex>(middleVertex, distinctVertices[i]));
                    previousVertex = distinctVertices[i];
                }
                // Down
                previousVertex = distinctVertices[start];
                for (int i = start - 1; i >= 0; i--)
                {
                    var middlePoint  = previousVertex.Point + (distinctVertices[i].Point - previousVertex.Point) / 2.0;
                    var middleVertex = new CurveVertex(middlePoint, id);
                    result.Add(new SEdge <CurveVertex>(previousVertex, middleVertex));
                    result.Add(new SEdge <CurveVertex>(middleVertex, distinctVertices[i]));

                    previousVertex = distinctVertices[i];
                }
            }

            return(result);
        }
Beispiel #6
0
        public static Dictionary <ObjectId, List <Point3d> > GetCrotchPoints(Database database,
                                                                             IEnumerable <ObjectId> parcelIds)
        {
            var result = new Dictionary <ObjectId, List <Point3d> >();
            // Create a kd tree.
            var allVertices = new List <CurveVertex>();

            using (var transaction = database.TransactionManager.StartTransaction())
            {
                foreach (var objId in parcelIds)
                {
                    var curve = transaction.GetObject(objId, OpenMode.ForRead) as Curve;
                    if (curve == null)
                    {
                        continue;
                    }
                    var vertices = CurveUtils.GetDistinctVertices(curve, transaction);
                    allVertices.AddRange(vertices.Select(it => new CurveVertex(it, objId)));
                }
                transaction.Commit();
            }
            var kdTree = new CurveVertexKdTree <CurveVertex>(allVertices, it => it.Point.ToArray(), ignoreZ: true);

            // 搜索三岔口
            //using (var tolerance = new ToleranceOverrule(null))
            using (var transaction = database.TransactionManager.StartTransaction())
            {
                foreach (var parcelId in parcelIds)
                {
                    var curve = transaction.GetObject(parcelId, OpenMode.ForRead) as Curve;
                    if (curve == null)
                    {
                        continue;
                    }

                    var ptLink     = LinkedPoint.GetLinkedPoints(curve, transaction, isLoop: true);
                    var ptTraverse = ptLink;
                    // Use records to improve performance.
                    var records = new Dictionary <LinkedPoint, IEnumerable <CurveVertex> >();
                    while (ptTraverse != null)
                    {
                        var prev = ptTraverse.Prev;
                        var next = ptTraverse.Next;
                        if (!records.ContainsKey(prev))
                        {
                            records[prev] = kdTree.NearestNeighbours(prev.Point.ToArray(), radius: 0.001);
                        }
                        if (!records.ContainsKey(ptTraverse))
                        {
                            records[ptTraverse] = kdTree.NearestNeighbours(ptTraverse.Point.ToArray(), radius: 0.001);
                        }
                        if (!records.ContainsKey(next))
                        {
                            records[next] = kdTree.NearestNeighbours(next.Point.ToArray(), radius: 0.001);
                        }

                        foreach (var vertex in records[ptTraverse])
                        {
                            if (result.ContainsKey(parcelId) && result[parcelId].Contains(vertex.Point))
                            {
                                continue;
                            }

                            if (vertex.Id == parcelId || vertex.Point != ptTraverse.Point)
                            {
                                continue;
                            }
                            var prevVertex = new CurveVertex(prev.Point, vertex.Id);
                            var nextVertex = new CurveVertex(next.Point, vertex.Id);
                            if (records[prev].Contains(prevVertex) && records[next].Contains(nextVertex))
                            {
                                continue;
                            }

                            List <Point3d> list = null;
                            if (result.ContainsKey(parcelId))
                            {
                                list = result[parcelId];
                            }
                            else
                            {
                                list             = new List <Point3d>();
                                result[parcelId] = list;
                            }
                            list.Add(ptTraverse.Point);
                        }

                        ptTraverse = ptTraverse.Next;
                        if (ptTraverse == ptLink)
                        {
                            break;
                        }
                    }
                }
                transaction.Commit();
            }

            return(result);
        }
        private IEnumerable <CurveCrossingInfo> SearchDuplicateInfo(KeyValuePair <ObjectId, CurveVertex[]> curveInfo,
                                                                    CurveVertexKdTree <CurveVertex> kdTree, Dictionary <ObjectId, CurveVertex[]> curveInfos, List <Tuple <ObjectId, ObjectId> > visited)
        {
            List <CurveCrossingInfo> crossingInfos = new List <CurveCrossingInfo>();
            HashSet <ObjectId>       candidates    = null;
            Dictionary <ObjectId, List <Point3d> > candidatePoints = new Dictionary <ObjectId, List <Point3d> >();

            foreach (var vertex in curveInfo.Value)
            {
                var neighbours = kdTree.NearestNeighbours(vertex.Point.ToArray(), _tolerance);
                neighbours = neighbours.Except(new CurveVertex[] { vertex });

                // If there is no neighbour vertex, just return.
                if (!neighbours.Any())
                {
                    if (candidates != null)
                    {
                        candidates = null;
                    }
                    break;
                }

                if (candidates == null)
                {
                    candidates = new HashSet <ObjectId>(neighbours.Select(it => it.Id));
                    foreach (var id in candidates)
                    {
                        var points = neighbours.Where(it => it.Id == id)
                                     .Select(it => it.Point);
                        candidatePoints[id] = new List <Point3d>(points);
                    }
                }
                else
                {
                    candidates.IntersectWith(neighbours.Select(it => it.Id));
                    if (candidates.Count <= 0)
                    {
                        break;
                    }

                    foreach (var id in candidates)
                    {
                        var points    = candidatePoints[id];
                        var newPoints = neighbours
                                        .Where(it => it.Id == id)
                                        .Select(it => it.Point);
                        foreach (var newPoint in newPoints)
                        {
                            if (!points.Contains(newPoint))
                            {
                                points.Add(newPoint);
                            }
                        }
                    }
                }
            }

            if (candidates != null && candidates.Count > 0)
            {
                foreach (var candidate in candidates)
                {
                    // If self, just continue.
                    if (candidate == curveInfo.Key)
                    {
                        continue;
                    }

                    var existing = visited.FirstOrDefault(it =>
                                                          (it.Item1 == curveInfo.Key && it.Item2 == candidate) ||
                                                          (it.Item1 == candidate && it.Item2 == curveInfo.Key));

                    if (existing != null)
                    {
                        continue;
                    }

                    visited.Add(new Tuple <ObjectId, ObjectId>(curveInfo.Key, candidate));

                    // Check whether they have same number of vertices.
                    if (curveInfos[candidate].Length != candidatePoints[candidate].Count)
                    {
                        continue;
                    }

                    var points       = curveInfo.Value.Select(it => it.Point).ToArray();
                    var crossingInfo = new CurveCrossingInfo(curveInfo.Key, candidate, points);
                    crossingInfos.Add(crossingInfo);
                }
            }
            return(crossingInfos);
        }