//--------------------------------------------------------- CSG_Shape * CSG_PointCloud::Get_Shape(TSG_Point Point, double Epsilon) { CSG_Rect r(Point.x - Epsilon, Point.y - Epsilon, Point.x + Epsilon, Point.y + Epsilon); if( r.Intersects(Get_Extent()) != INTERSECTION_None ) { int iPoint = -1; double iDistance = -1.0; for(int iRecord=0; iRecord<Get_Count(); iRecord++) { Set_Cursor(iRecord); if( r.Contains(Get_X(), Get_Y()) ) { if( iPoint < 0 || iDistance > SG_Get_Distance(Point.x, Point.y, Get_X(), Get_Y()) ) { iPoint = iRecord; iDistance = SG_Get_Distance(Point.x, Point.y, Get_X(), Get_Y()); } } } if( iPoint >= 0 ) { return( CSG_Shapes::Get_Shape(iPoint) ); } } return( NULL ); }
//--------------------------------------------------------- void CWKSP_PointCloud::On_Draw(CWKSP_Map_DC &dc_Map, bool bEdit) { if( Get_Extent().Intersects(dc_Map.m_rWorld) != INTERSECTION_None && dc_Map.IMG_Draw_Begin(m_Parameters("DISPLAY_TRANSPARENCY")->asDouble() / 100.0) ) { _Draw_Points (dc_Map); dc_Map.IMG_Draw_End(); } }
//--------------------------------------------------------- bool CShapes_Cut::On_Execute(void) { int Method; CSG_Shapes *pExtent, *pCut; CSG_Parameter_Shapes_List *pShapes, *pCuts; //----------------------------------------------------- pShapes = Parameters("SHAPES") ->asShapesList(); pCuts = Parameters("CUT") ->asShapesList(); pExtent = Parameters("EXTENT") ->asShapes(); Method = Parameters("METHOD") ->asInt(); //----------------------------------------------------- if( pShapes->Get_Count() > 0 ) { int iLayer; CSG_Rect r(pShapes->asShapes(0)->Get_Extent()); for(iLayer=1; iLayer<pShapes->Get_Count(); iLayer++) { r.Union(pShapes->asShapes(iLayer)->Get_Extent()); } if( Get_Extent(r) ) { pCuts->Del_Items(); Cut_Set_Extent(r, pExtent, true); for(iLayer=0; iLayer<pShapes->Get_Count(); iLayer++) { if( m_pPolygons ) { if( Cut_Shapes(m_pPolygons, Method, pShapes->asShapes(iLayer), pCut = SG_Create_Shapes()) ) pCuts->Add_Item(pCut); else delete(pCut); } else { if( Cut_Shapes(r, Method, pShapes->asShapes(iLayer), pCut = SG_Create_Shapes()) ) pCuts->Add_Item(pCut); else delete(pCut); } } return( pCuts->Get_Count() > 0 ); } } return( false ); }
//--------------------------------------------------------- TSG_Intersection CSG_Shape_Line::On_Intersects(TSG_Rect Region) { // called if polygon's bounding box contains or overlaps with region. // now let's figure out how region intersects with polygon itself //----------------------------------------------------- for(int iPart=0; iPart<m_nParts; iPart++) { CSG_Shape_Part *pPart = m_pParts[iPart]; switch( pPart->Get_Extent().Intersects(Region) ) { case INTERSECTION_None: // region and line part are distinct break; case INTERSECTION_Identical: // region contains line part case INTERSECTION_Contained: return( Get_Extent().Intersects(Region) ); case INTERSECTION_Contains: case INTERSECTION_Overlaps: // region overlaps with line part's extent, now let's look at the line part itself! if( pPart->Get_Count() > 1 ) { TSG_Point *pa, *pb, c; pb = pPart->m_Points; pa = pb + 1; for(int iPoint=1; iPoint<pPart->Get_Count(); iPoint++, pb=pa++) { if( SG_Get_Crossing_InRegion(c, *pa, *pb, Region) ) { return( INTERSECTION_Overlaps ); } } } break; } } //----------------------------------------------------- TSG_Point p = Get_Point(0, 0); if( Region.xMin <= p.x && p.x <= Region.xMax && Region.yMin <= p.y && p.y <= Region.yMax ) { return( INTERSECTION_Contained ); } return( INTERSECTION_None ); }
//--------------------------------------------------------- bool CShapes_Cut_Interactive::On_Execute_Position(CSG_Point ptWorld, TSG_Module_Interactive_Mode Mode) { switch( Mode ) { //----------------------------------------------------- case MODULE_INTERACTIVE_LDOWN: if( m_bDown == false ) { m_bDown = true; m_pDown = ptWorld; } return( true ); //----------------------------------------------------- case MODULE_INTERACTIVE_LUP: if( m_bDown == true ) { m_bDown = false; CSG_Rect r(m_pDown, ptWorld); if( Get_Extent(r) ) { if( Cut_Set_Extent(r, m_pExtent, true) ) { DataObject_Update(m_pExtent); } if( Cut_Shapes(r, m_Method, m_pShapes, m_pCut) ) { DataObject_Update(m_pCut); } else { Message_Add(_TL("No shapes in selection")); } } } return( true ); } return( true ); }
//--------------------------------------------------------- void CWKSP_TIN::On_Draw(CWKSP_Map_DC &dc_Map, int Flags) { if( Get_Extent().Intersects(dc_Map.m_rWorld) != INTERSECTION_None ) { if( m_fValue >= 0 ) { _Draw_Triangles(dc_Map); } if( m_Parameters("DISPLAY_EDGES" )->asBool() ) { _Draw_Edges (dc_Map); } if( m_Parameters("DISPLAY_POINTS")->asBool() ) { _Draw_Points (dc_Map); } } }
//--------------------------------------------------------- CSG_Shape * CSG_Shapes::Get_Shape(TSG_Point Point, double Epsilon) { int iShape; double d, dNearest; CSG_Rect r(Point.x - Epsilon, Point.y - Epsilon, Point.x + Epsilon, Point.y + Epsilon); CSG_Shape *pShape, *pNearest; pNearest = NULL; if( r.Intersects(Get_Extent()) != INTERSECTION_None ) { for(iShape=0, dNearest=-1.0; iShape<Get_Count(); iShape++) { pShape = Get_Shape(iShape); if( pShape->Intersects(r) ) { for(int iPart=0; iPart<pShape->Get_Part_Count(); iPart++) { if( r.Intersects(pShape->Get_Extent(iPart)) ) { d = pShape->Get_Distance(Point, iPart); if( d == 0.0 ) { return( pShape ); } else if( d > 0.0 && d <= Epsilon && (pNearest == NULL || d < dNearest) ) { dNearest = d; pNearest = pShape; } } } } } } return( pNearest ); }
//--------------------------------------------------------- bool CSG_PointCloud::Select(TSG_Rect Extent, bool bInvert) { if( !bInvert ) // clear selection { Select(-1, false); } if( Get_Extent().Intersects(Extent) != INTERSECTION_None ) { for(int i=0; i<Get_Count(); i++) { Set_Cursor(i); if( Extent.xMin <= Get_X() && Get_X() <= Extent.xMax && Extent.yMin <= Get_Y() && Get_Y() <= Extent.yMax ) { Select(i, true); } } } return( Get_Selection_Count() > 0 ); }
//--------------------------------------------------------- bool CSTL_Import::On_Execute(void) { int Method; DWORD iFacette, nFacettes; TSTL_Point p[3]; CSG_String sFile, sHeader; CSG_File Stream; //----------------------------------------------------- sFile = Parameters("FILE") ->asString(); Method = Parameters("METHOD") ->asInt(); r_sin_x = sin(Parameters("ROT_X")->asDouble() * M_DEG_TO_RAD); r_sin_y = sin(Parameters("ROT_Y")->asDouble() * M_DEG_TO_RAD); r_sin_z = sin(Parameters("ROT_Z")->asDouble() * M_DEG_TO_RAD); r_cos_x = cos(Parameters("ROT_X")->asDouble() * M_DEG_TO_RAD); r_cos_y = cos(Parameters("ROT_Y")->asDouble() * M_DEG_TO_RAD); r_cos_z = cos(Parameters("ROT_Z")->asDouble() * M_DEG_TO_RAD); //----------------------------------------------------- if( !Stream.Open(sFile) ) { return( false ); } if( !Stream.Read(sHeader, 80) ) { return( false ); } Message_Add(sHeader); if( !Stream.Read(&nFacettes, sizeof(nFacettes)) ) { return( false ); } Message_Add(CSG_String::Format(SG_T("%s: %d"), _TL("Number of Facettes"), nFacettes)); //----------------------------------------------------- switch( Method ) { //----------------------------------------------------- case 0: { // Point Cloud CSG_Rect Extent; if( Get_Extent(Stream, Extent, nFacettes) ) { CSG_PRQuadTree Points(Extent); CSG_PointCloud *pPoints = SG_Create_PointCloud(); Parameters("POINTS")->Set_Value(pPoints); pPoints->Set_Name(SG_File_Get_Name(sFile, false)); pPoints->Add_Field((const char *)NULL, SG_DATATYPE_Undefined); for(iFacette=0; iFacette<nFacettes && !Stream.is_EOF() && Set_Progress(iFacette, nFacettes); iFacette++) { if( Read_Facette(Stream, p) ) { for(int i=0; i<3; i++) { if( Points.Add_Point(p[i].x, p[i].y, p[i].z) ) { pPoints->Add_Point(p[i].x, p[i].y, p[i].z); } } } } } break; } //----------------------------------------------------- case 1: { // Point Cloud (centered) CSG_PointCloud *pPoints = SG_Create_PointCloud(); Parameters("POINTS")->Set_Value(pPoints); pPoints->Set_Name(SG_File_Get_Name(sFile, false)); pPoints->Add_Field((const char *)NULL, SG_DATATYPE_Undefined); for(iFacette=0; iFacette<nFacettes && !Stream.is_EOF() && Set_Progress(iFacette, nFacettes); iFacette++) { if( Read_Facette(Stream, p) ) { pPoints->Add_Point( (p[0].x + p[1].x + p[2].x) / 3.0, (p[0].y + p[1].y + p[2].y) / 3.0, (p[0].z + p[1].z + p[2].z) / 3.0 ); } } break; } //----------------------------------------------------- case 2: { // Points CSG_Shapes *pPoints = SG_Create_Shapes(SHAPE_TYPE_Point, SG_File_Get_Name(sFile, false)); pPoints->Add_Field(SG_T("Z"), SG_DATATYPE_Float); Parameters("SHAPES")->Set_Value(pPoints); for(iFacette=0; iFacette<nFacettes && !Stream.is_EOF() && Set_Progress(iFacette, nFacettes); iFacette++) { if( Read_Facette(Stream, p) ) { CSG_Shape *pPoint = pPoints->Add_Shape(); pPoint->Add_Point( (p[0].x + p[1].x + p[2].x) / 3.0, (p[0].y + p[1].y + p[2].y) / 3.0 ); pPoint->Set_Value(0, (p[0].z + p[1].z + p[2].z) / 3.0 ); } } break; } //----------------------------------------------------- case 3: { // Raster CSG_Rect Extent; if( Get_Extent(Stream, Extent, nFacettes) ) { int nx, ny; double d; nx = Parameters("GRID_RES")->asInt(); d = Extent.Get_XRange() / nx; ny = 1 + (int)(Extent.Get_YRange() / d); m_pGrid = SG_Create_Grid(SG_DATATYPE_Float, nx, ny, d, Extent.Get_XMin(), Extent.Get_YMin()); m_pGrid->Set_Name(SG_File_Get_Name(sFile, false)); m_pGrid->Set_NoData_Value(-99999); m_pGrid->Assign_NoData(); Parameters("GRID")->Set_Value(m_pGrid); //--------------------------------------------- for(iFacette=0; iFacette<nFacettes && !Stream.is_EOF() && Set_Progress(iFacette, nFacettes); iFacette++) { if( Read_Facette(Stream, p) ) { TSG_Point_Z Point[3]; for(int i=0; i<3; i++) { Point[i].x = (p[i].x - m_pGrid->Get_XMin()) / m_pGrid->Get_Cellsize(); Point[i].y = (p[i].y - m_pGrid->Get_YMin()) / m_pGrid->Get_Cellsize(); Point[i].z = p[i].z; } Set_Triangle(Point); } } } break; } } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool CGCS_Graticule::Get_Graticule(const CSG_Rect &Extent) { double x, y, Interval; CSG_Rect r; if( !Get_Extent(Extent, r) || (Interval = Get_Interval(r)) <= 0.0 ) { return( false ); } //----------------------------------------------------- r.m_rect.xMin = Interval * floor(r.Get_XMin() / Interval); r.m_rect.xMax = Interval * ceil (r.Get_XMax() / Interval); r.m_rect.yMin = Interval * floor(r.Get_YMin() / Interval); r.m_rect.yMax = Interval * ceil (r.Get_YMax() / Interval); r.Inflate(Interval, false); if( r.Get_XMin() < -180.0 ) r.m_rect.xMin = -180.0; if( r.Get_XMax() > 180.0 ) r.m_rect.xMax = 180.0; if( r.Get_YMin() < -90.0 ) r.m_rect.yMin = -90.0; if( r.Get_YMax() > 90.0 ) r.m_rect.yMax = 90.0; //----------------------------------------------------- double Resolution = Parameters("RESOLUTION")->asDouble(); if( Resolution <= 0.0 ) Resolution = Interval; if( Interval > Resolution ) { Resolution = Interval / ceil(Interval / Resolution); } //----------------------------------------------------- CSG_Shapes *pGraticule = Parameters("GRATICULE")->asShapes(); pGraticule->Create(SHAPE_TYPE_Line); pGraticule->Set_Name(_TL("Graticule")); pGraticule->Add_Field("TYPE" , SG_DATATYPE_String); pGraticule->Add_Field("LABEL" , SG_DATATYPE_String); pGraticule->Add_Field("DEGREE", SG_DATATYPE_Double); //----------------------------------------------------- CSG_Shapes *pCoordinates = Parameters("COORDS")->asShapes(); if( pCoordinates ) { pCoordinates->Create(SHAPE_TYPE_Point); pCoordinates->Set_Name(_TL("Coordinates")); pCoordinates->Add_Field("TYPE" , SG_DATATYPE_String); pCoordinates->Add_Field("LABEL", SG_DATATYPE_String); } //----------------------------------------------------- CSG_Shapes Clip(SHAPE_TYPE_Polygon); CSG_Shape *pClip = Clip.Add_Shape(); pClip->Add_Point(Extent.Get_XMin(), Extent.Get_YMin()); pClip->Add_Point(Extent.Get_XMin(), Extent.Get_YMax()); pClip->Add_Point(Extent.Get_XMax(), Extent.Get_YMax()); pClip->Add_Point(Extent.Get_XMax(), Extent.Get_YMin()); pClip->Add_Point(Extent.Get_XMin(), Extent.Get_YMin()); //----------------------------------------------------- for(y=r.Get_YMin(); y<=r.Get_YMax(); y+=Interval) { CSG_Shape *pLine = pGraticule->Add_Shape(); pLine->Set_Value(0, "LAT"); pLine->Set_Value(1, Get_Degree(y, DEG_PREC_DEG)); pLine->Set_Value(2, y); for(x=r.Get_XMin(); x<=r.Get_XMax(); x+=Interval) { CSG_Point p(x, y); m_Projector.Get_Projection(p); pLine->Add_Point(p); if( Resolution < Interval && x < r.Get_XMax() ) { for(double i=x+Resolution; i<x+Interval; i+=Resolution) { CSG_Point p(i, y); m_Projector.Get_Projection(p); pLine->Add_Point(p); } } } Get_Coordinate(Extent, pCoordinates, pLine, AXIS_LEFT); Get_Coordinate(Extent, pCoordinates, pLine, AXIS_RIGHT); if( !SG_Polygon_Intersection(pLine, pClip) ) { pGraticule->Del_Shape(pLine); } } //----------------------------------------------------- for(x=r.Get_XMin(); x<=r.Get_XMax(); x+=Interval) { CSG_Shape *pLine = pGraticule->Add_Shape(); pLine->Set_Value(0, "LON"); pLine->Set_Value(1, Get_Degree(x, DEG_PREC_DEG)); pLine->Set_Value(2, x); for(y=r.Get_YMin(); y<=r.Get_YMax(); y+=Interval) { CSG_Point p(x, y); m_Projector.Get_Projection(p); pLine->Add_Point(p); if( Resolution < Interval && y < r.Get_YMax() ) { for(double i=y+Resolution; i<y+Interval; i+=Resolution) { CSG_Point p(x, i); m_Projector.Get_Projection(p); pLine->Add_Point(p); } } } Get_Coordinate(Extent, pCoordinates, pLine, AXIS_BOTTOM); Get_Coordinate(Extent, pCoordinates, pLine, AXIS_TOP); if( !SG_Polygon_Intersection(pLine, pClip) ) { pGraticule->Del_Shape(pLine); } } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- // This function is executed if the user is pressing the OK button //--------------------------------------------------------- bool CGrid_Polygon_Clip::On_Execute(void) { int x, y, ix, iy, ax, ay, nx, ny; CSG_Parameter_Grid_List *pGrids_in, *pGrids_out; CSG_Grid *pGrid_in, *pGrid_out, Mask; CSG_Shapes *pShapes; //----------------------------------------------------- pGrids_in = Parameters("INPUT" )->asGridList(); pGrids_out = Parameters("OUTPUT" )->asGridList(); pShapes = Parameters("POLYGONS")->asShapes(); m_bNoData = Parameters("NODATA" )->asBool(); //----------------------------------------------------- if( pShapes->Get_Type() == SHAPE_TYPE_Polygon && pShapes->Get_Count() > 0 && Get_System()->Get_Extent().Intersects(pShapes->Get_Extent()) ) { // create temporary grid. // Cells within the shapefile get the value +1 // Cells outside the shapefile get the value -1 Mask.Create(*Get_System(), SG_DATATYPE_Byte); //------------------------------------------------- // Get_Mask assignes +1 values to gridcells within the shapefile // The function has been copied from Module: 'Grid_Statistics_AddTo_Polygon' // Function: Get_ShapeIDs(...) // and check extent of valid values in Mask to // calculate GridSystem parameters pGrid_out if( Get_Mask(pShapes, &Mask) && Get_Extent(ax, nx, ay, ny, &Mask, pGrids_in) ) { for(int iGrid=0; iGrid<pGrids_in->Get_Count(); iGrid++) { pGrid_in = pGrids_in->asGrid(iGrid); pGrid_out = SG_Create_Grid( // creating the output grid GridSystem pGrid_in->Get_Type(), nx, ny, Get_Cellsize(), Get_XMin() + ax * Get_Cellsize(), Get_YMin() + ay * Get_Cellsize() ); pGrid_out ->Set_Name(pGrid_in->Get_Name()); pGrid_out ->Set_NoData_Value(pGrid_in->Get_NoData_Value()); pGrids_out ->Add_Item(pGrid_out); // Assign valid values from input grid to the cells of the // output grid that are within the borders of the shapefile // Assign NoData values to the cells outside the shapefile borders for(y=0, iy=ay; y<ny && Set_Progress(y, ny); y++, iy++) { for(x=0, ix=ax; x<nx; x++, ix++) { if( Mask.asDouble(ix, iy) == MASK_ON ) // -1 = NoData_Value { pGrid_out->Set_Value(x, y, pGrid_in->asDouble(ix, iy)); } else { pGrid_out->Set_NoData(x, y); } } } } return( true ); } } return( false ); }
//--------------------------------------------------------- TSG_Point CSG_Shape_Line::Get_Centroid(void) { return( Get_Extent().Get_Center() ); }