📄 polygon_intersection.cpp
字号:
case 0:
Add_Polygon(pShapes_AB, pShape_A, iShape_A + 1);
break;
case 1:
Add_Polygon(pShapes_AB, pShape_A, iShape_A + 1, 0);
break;
case 2:
Add_Polygon(pShapes_AB, pShape_A, 0, iShape_A + 1);
break;
}
}
}
return( nResult > 0 );
}
///////////////////////////////////////////////////////////
// //
// //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
#define ADD_POLYGON(POLY) if( (pShape_Add = pShapes->Add_Shape()) != NULL )\
{\
pShape_Add->Assign(POLY, false);\
pShape_Add->Get_Record()->Set_Value(0, pShapes->Get_Count());\
pShape_Add->Get_Record()->Set_Value(1, ID_A);\
pShape_Add->Get_Record()->Set_Value(2, ID_B);\
}
void CPolygon_Intersection::Add_Polygon(CSG_Shapes *pShapes, CSG_Shape *pShape, int ID_A, int ID_B)
{
int iPart, jPart, iPoint, nParts;
CSG_Shape *pShape_Add, *pShape_Part;
CSG_Shapes Shapes_Part;
if( bSplitParts && pShape->Get_Part_Count() > 1 )
{
Shapes_Part.Create(SHAPE_TYPE_Polygon);
pShape_Part = Shapes_Part.Add_Shape();
for(iPart=0; iPart<pShape->Get_Part_Count(); iPart++)
{
if( !((CSG_Shape_Polygon *)pShape)->is_Lake(iPart) )
{
pShape_Part->Del_Parts();
nParts = 0;
for(iPoint=0; iPoint<pShape->Get_Point_Count(iPart); iPoint++)
{
pShape_Part->Add_Point(pShape->Get_Point(iPoint, iPart), nParts);
}
for(jPart=0; jPart<pShape->Get_Part_Count(); jPart++)
{
if( ((CSG_Shape_Polygon *)pShape)->is_Lake(jPart)
&& ((CSG_Shape_Polygon *)pShape)->is_Containing(pShape->Get_Point(0, jPart), iPart) )
{
nParts++;
for(iPoint=0; iPoint<pShape->Get_Point_Count(jPart); iPoint++)
{
pShape_Part->Add_Point(pShape->Get_Point(iPoint, jPart), nParts);
}
}
}
ADD_POLYGON(pShape_Part);
}
}
}
else
{
ADD_POLYGON(pShape);
}
}
#undef ADD_POLYGON
//---------------------------------------------------------
#define ADD_POLYGON(POLY) if( (pShape_Add = pShapes->Add_Shape()) != NULL )\
{\
pShape_Add->Assign(POLY, false);\
pShape_Add->Get_Record()->Set_Value(0, pShapes->Get_Count());\
pShape_Add->Get_Record()->Set_Value(1, ID);\
}
void CPolygon_Intersection::Add_Polygon(CSG_Shapes *pShapes, CSG_Shape *pShape, int ID)
{
int iPart, jPart, iPoint, nParts;
CSG_Shape *pShape_Add, *pShape_Part;
CSG_Shapes Shapes_Part;
if( bSplitParts && pShape->Get_Part_Count() > 1 )
{
Shapes_Part.Create(SHAPE_TYPE_Polygon);
pShape_Part = Shapes_Part.Add_Shape();
for(iPart=0; iPart<pShape->Get_Part_Count(); iPart++)
{
if( !((CSG_Shape_Polygon *)pShape)->is_Lake(iPart) )
{
pShape_Part->Del_Parts();
nParts = 0;
for(iPoint=0; iPoint<pShape->Get_Point_Count(iPart); iPoint++)
{
pShape_Part->Add_Point(pShape->Get_Point(iPoint, iPart), nParts);
}
for(jPart=0; jPart<pShape->Get_Part_Count(); jPart++)
{
if( ((CSG_Shape_Polygon *)pShape)->is_Lake(jPart)
&& ((CSG_Shape_Polygon *)pShape)->is_Containing(pShape->Get_Point(0, jPart), iPart) )
{
nParts++;
for(iPoint=0; iPoint<pShape->Get_Point_Count(jPart); iPoint++)
{
pShape_Part->Add_Point(pShape->Get_Point(iPoint, jPart), nParts);
}
}
}
ADD_POLYGON(pShape_Part);
}
}
}
else
{
ADD_POLYGON(pShape);
}
}
#undef ADD_POLYGON
///////////////////////////////////////////////////////////
// //
// //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
bool CPolygon_Intersection::GPC_Polygon_Intersection(CSG_Shape *pShape_A, CSG_Shape *pShape_B, CSG_Shape *pShape_AB)
{
bool bResult;
int iPoint, nPoints, iPart;
gpc_polygon poly_A, poly_B, poly_AB;
gpc_vertex *Contour;
bResult = false;
if( GPC_Create_Polygon(pShape_A, &poly_A)
&& GPC_Create_Polygon(pShape_B, &poly_B) )
{
gpc_polygon_clip(GPC_INT, &poly_A, &poly_B, &poly_AB);
if( poly_AB.num_contours > 0 )
{
pShape_AB->Del_Parts();
for(iPart=0; iPart<poly_AB.num_contours; iPart++)
{
Contour = poly_AB.contour[iPart].vertex;
nPoints = poly_AB.contour[iPart].num_vertices;
for(iPoint=0; iPoint<nPoints; iPoint++)
{
pShape_AB->Add_Point(Contour[iPoint].x, Contour[iPoint].y, iPart);
}
}
bResult = true;
}
gpc_free_polygon(&poly_AB);
}
gpc_free_polygon(&poly_A);
gpc_free_polygon(&poly_B);
return( bResult );
}
//---------------------------------------------------------
bool CPolygon_Intersection::GPC_Difference(CSG_Shape *pShape_A, CSG_Shape *pShape_B, CSG_Shape *pShape_AB)
{
bool bResult;
int iPoint, nPoints, iPart;
gpc_polygon poly_A, poly_B, poly_AB;
gpc_vertex *Contour;
bResult = false;
if( GPC_Create_Polygon(pShape_A, &poly_A)
&& GPC_Create_Polygon(pShape_B, &poly_B) )
{
pShape_AB->Del_Parts();
gpc_polygon_clip(GPC_DIFF, &poly_A, &poly_B, &poly_AB);
if( poly_AB.num_contours > 0 )
{
for(iPart=0; iPart<poly_AB.num_contours; iPart++)
{
Contour = poly_AB.contour[iPart].vertex;
nPoints = poly_AB.contour[iPart].num_vertices;
for(iPoint=0; iPoint<nPoints; iPoint++)
{
pShape_AB->Add_Point(Contour[iPoint].x, Contour[iPoint].y, iPart);
}
}
}
gpc_free_polygon(&poly_AB);
bResult = true;
}
gpc_free_polygon(&poly_A);
gpc_free_polygon(&poly_B);
return( bResult );
}
///////////////////////////////////////////////////////////
// //
// //
// //
///////////////////////////////////////////////////////////
//---------------------------------------------------------
bool CPolygon_Intersection::GPC_Create_Polygon(CSG_Shape *pShape, gpc_polygon *pPolygon)
{
int iPoint, iPart;
TSG_Point Point;
gpc_vertex *Contour;
gpc_vertex_list vList;
pPolygon->contour = NULL;
pPolygon->hole = NULL;
pPolygon->num_contours = 0;
//-----------------------------------------------------
for(iPart=0; iPart<pShape->Get_Part_Count(); iPart++)
{
if( pShape->Get_Point_Count(iPart) > 0 )
{
Contour = (gpc_vertex *)malloc(pShape->Get_Point_Count(iPart) * sizeof(gpc_vertex));
for(iPoint=0; iPoint<pShape->Get_Point_Count(iPart); iPoint++)
{
Point = pShape->Get_Point(iPoint, iPart);
Contour[iPoint].x = Point.x;
Contour[iPoint].y = Point.y;
}
vList.num_vertices = pShape->Get_Point_Count(iPart);
vList.vertex = Contour;
gpc_add_contour(pPolygon, &vList, ((CSG_Shape_Polygon *)pShape)->is_Lake(iPart) ? 1 : 0);
free(Contour);
}
}
return( pPolygon->num_contours > 0 );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -