Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static Adesk::Boolean joinEntities(AcDbPolyline &joined, const AcDbPolyline &primary, const AcDbPolyline &secondary)
- {
- // Find out whether primary and/or secondary are
- // intersecting each other. This may also hold
- // true even if one is contained into the other.
- // Assume false and attempt to prove true
- Adesk::Boolean intersecting = false;
- for (Adesk::uint i = 0; i < primary.numVerts(); ++i)
- {
- AcGeLineSeg2d pSegment;
- primary.getLineSegAt(i, pSegment);
- for (Adesk::uint j = 0; j < secondary.numVerts(); ++j)
- {
- AcGeLineSeg2d sSegment;
- secondary.getLineSegAt(j, sSegment);
- AcGePoint2d segSegIntersect;
- // AcGeLineSeg2d::intersectWith also reports when both
- // lines are on top of each other.
- if (pSegment.intersectWith(sSegment, segSegIntersect))
- {
- intersecting = true;
- break;
- }
- }
- if (intersecting)
- break;
- }
- // Whether one polyline is contained in one other
- Adesk::Boolean pContained, sContained;
- // Assume true and attempt to prove it false
- pContained = true;
- // Test if one polyline contains the other
- // As we are running on the primary, keep an
- // eye of a point that is not contained within
- // the secondary (in which case we anyway would
- // break) to start with later on.
- Adesk::uint pStartIndex;
- for (Adesk::uint i = 0; i < primary.numVerts(); ++i)
- {
- AcGePoint2d pPoint;
- primary.getPointAt(i, pPoint);
- // App::contains also returns true if the given
- // point is on a segment. This is ok for us.
- if (!contains(secondary, pPoint))
- {
- pContained = false;
- pStartIndex = i;
- break;
- }
- }
- // Assume true and attempt to prove it false
- sContained = true;
- // Test if one polyline contains the other
- for (Adesk::uint j = 0; j < secondary.numVerts(); ++j)
- {
- AcGePoint2d sPoint;
- secondary.getPointAt(j, sPoint);
- // App::contains also returns true if the given
- // point is on a segment. This is ok for us.
- if (!contains(primary, sPoint))
- {
- sContained = false;
- break;
- }
- }
- if (!(intersecting || sContained || pContained))
- return false;
- // Contained within each other
- if (sContained || pContained)
- {
- acutPrintf(L"Not yet implemented: poly are contained within each other.\n");
- return false;
- }
- // If they are intersecting but not contained within
- // each other, then we can pick any point of the
- // primary that is not inside the secondary to start
- // with.
- AcGePoint2d pStartPoint, pos;
- primary.getPointAt(pStartIndex, pStartPoint);
- // i is the primary polyline vertex index
- // j is the secondary polyline vertex index
- // k is the joined polyline vertex index
- Adesk::uint i, j, k;
- for (pos = pStartPoint, k = 0, i = pStartIndex; ;)
- {
- AcGePoint2d intersection;
- // Follow the primary poly as long as we are
- // not intersecting
- Adesk::Boolean intersects = false;
- do {
- // Seek next primary segment
- AcGeLineSeg2d pSeg;
- primary.getLineSegAt(i++, pSeg);
- // Check collision
- for (j = 0; j < secondary.numVerts(); ++j)
- {
- AcGeLineSeg2d sSeg;
- secondary.getLineSegAt(j, sSeg);
- if (pSeg.intersectWith(sSeg, intersection))
- {
- intersects = true;
- break;
- }
- }
- // Not intersecting for segment pSeg, so add it
- joined.addVertexAt(k++, pSeg.endPoint());
- } while (!intersects);
- // Intersected secondary poly, add intersection
- // and go on with following the secondary poly.
- joined.addVertexAt(k++, intersection);
- // We are intersecting with segment j of secondary poly.
- // Find out in which direction we have to go.
- AcGeLineSeg2d sSeg;
- secondary.getLineSegAt(j, sSeg);
- // If the end point of the intersecting segment is
- // inside the primary poly, then we have to go into the
- // direction of the start point (reverse), otherwise
- // forward.
- // FIXME: this is not always correct
- Adesk::Boolean reverse = false;
- if (contains(primary, sSeg.endPoint()))
- reverse = true;
- do {
- // Check collision
- for (j = 0; j < secondary.numVerts(); ++j)
- {
- AcGeLineSeg2d pSeg;
- primary.getLineSegAt(i, pSeg);
- if (sSeg.intersectWith(pSeg, intersection))
- {
- intersects = true;
- break;
- }
- }
- // Not intersecting for segment pSeg, so add it
- if (reverse)
- joined.addVertexAt(k++, sSeg.startPoint());
- else
- joined.addVertexAt(k++, sSeg.endPoint());
- // Seek next secondary segment
- // TODO: handle wrap around
- if (reverse)
- secondary.getLineSegAt(j--, sSeg);
- else
- secondary.getLineSegAt(j++, sSeg);
- } while (!intersects);
- // Intersected primary poly, add intersection
- joined.addVertexAt(k++, intersection);
- // We have to end where we started
- if (pos == pStartPoint)
- break;
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement