⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ugklinestring.cpp

📁 linux下一款GIS程序源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
void UGKLineString::setPoints( int nPointsIn, UGKRawPoint * paoPointsIn,                               double * padfZ ){    setNumPoints( nPointsIn );    memcpy( paoPoints, paoPointsIn, sizeof(UGKRawPoint) * nPointsIn);/* -------------------------------------------------------------------- *//*      Check 2D/3D.                                                    *//* -------------------------------------------------------------------- */    if( padfZ != NULL )    {        int     i, bIs3D = FALSE;        for( i = 0; i < nPointsIn && !bIs3D; i++ )        {            if( padfZ[i] != 0.0 )                bIs3D = TRUE;        }        if( !bIs3D )            padfZ = NULL;    }    if( padfZ == NULL )    {        if( this->padfZ != NULL )//如果原来的不是2D,强制转换为2D            Make2D();    }    else    {        Make3D();        memcpy( this->padfZ, padfZ, sizeof(double) * nPointsIn );    }}/************************************************************************//*                             setPoints()                              *//************************************************************************//** * Assign all points in a line string. * * This method clear any existing points assigned to this line string, * and assigns a whole new set. * * @param nPointsIn number of points being passed in padfX and padfY. * @param padfX list of X coordinates of points being assigned. * @param padfY list of Y coordinates of points being assigned. * @param padfZ list of Z coordinates of points being assigned (defaults to * NULL for 2D objects). */void UGKLineString::setPoints( int nPointsIn, double * padfX, double * padfY,                               double * padfZ ){    int         i; /* -------------------------------------------------------------------- *//*      Check 2D/3D.                                                    *//* -------------------------------------------------------------------- */    if( padfZ != NULL )    {        int     bIs3D = FALSE;        for( i = 0; i < nPointsIn && !bIs3D; i++ )        {            if( padfZ[i] != 0.0 )                bIs3D = TRUE;        }        if( !bIs3D )            padfZ = NULL;    }    if( padfZ == NULL )        Make2D();    else        Make3D();    /* -------------------------------------------------------------------- *//*      Assign values.                                                  *//* -------------------------------------------------------------------- */    setNumPoints( nPointsIn );    for( i = 0; i < nPointsIn; i++ )    {        paoPoints[i].x = padfX[i];        paoPoints[i].y = padfY[i];    }    if( this->padfZ != NULL )        memcpy( this->padfZ, padfZ, sizeof(double) * nPointsIn );}/************************************************************************//*                             get_Length()                             *//*                                                                      *//*      For now we return a simple euclidian 2D distance.               *//************************************************************************/double UGKLineString::get_Length() const{    double      dfLength = 0;    int         i;    for( i = 0; i < nPointCount-1; i++ )    {        double      dfDeltaX, dfDeltaY;        dfDeltaX = paoPoints[i+1].x - paoPoints[i].x;        dfDeltaY = paoPoints[i+1].y - paoPoints[i].y;        dfLength += sqrt(dfDeltaX*dfDeltaX + dfDeltaY*dfDeltaY);    }        return dfLength;}/************************************************************************//*                             StartPoint()                             *//************************************************************************/void UGKLineString::StartPoint( UGKPoint * poPoint ) const{    getPoint( 0, poPoint );}/************************************************************************//*                              EndPoint()                              *//************************************************************************/void UGKLineString::EndPoint( UGKPoint * poPoint ) const{    getPoint( nPointCount-1, poPoint );}/************************************************************************//*                               Value()                                *//*                                                                      *//*      Get an interpolated point at some distance along the curve.     *//************************************************************************/void UGKLineString::Value( double dfDistance, UGKPoint * poPoint ) const{    double      dfLength = 0;    int         i;    if( dfDistance < 0 )    {        StartPoint( poPoint );        return;    }    for( i = 0; i < nPointCount-1; i++ )    {        double      dfDeltaX, dfDeltaY, dfSegLength;        dfDeltaX = paoPoints[i+1].x - paoPoints[i].x;        dfDeltaY = paoPoints[i+1].y - paoPoints[i].y;        dfSegLength = sqrt(dfDeltaX*dfDeltaX + dfDeltaY*dfDeltaY);        if (dfSegLength > 0)        {    //dfLength为累加长度,dfSegLength为两点之间长度            if( (dfLength <= dfDistance) && ((dfLength + dfSegLength) >=                                              dfDistance) )            {                double      dfRatio;                dfRatio = (dfDistance - dfLength) / dfSegLength;                poPoint->setX( paoPoints[i].x * (1 - dfRatio)                               + paoPoints[i+1].x * dfRatio );                poPoint->setY( paoPoints[i].y * (1 - dfRatio)                               + paoPoints[i+1].y * dfRatio );                if( getCoordinateDimension() == 3 )                    poPoint->setZ( padfZ[i] * (1 - dfRatio)                                   + padfZ[i] * dfRatio );                                return;            }            dfLength += dfSegLength;        }    }        EndPoint( poPoint );}/************************************************************************//*                            getEnvelope()                             *//************************************************************************//*    返回UGKEnvelope结构体,包含了这个UGKLineString的边界*/void UGKLineString::getEnvelope( UGKEnvelope * psEnvelope ) const{    double      dfMinX, dfMinY, dfMaxX, dfMaxY;    if( nPointCount == 0 )        return;        dfMinX = dfMaxX = paoPoints[0].x;    dfMinY = dfMaxY = paoPoints[0].y;    for( int iPoint = 1; iPoint < nPointCount; iPoint++ )    {        if( dfMaxX < paoPoints[iPoint].x )            dfMaxX = paoPoints[iPoint].x;        if( dfMaxY < paoPoints[iPoint].y )            dfMaxY = paoPoints[iPoint].y;        if( dfMinX > paoPoints[iPoint].x )            dfMinX = paoPoints[iPoint].x;        if( dfMinY > paoPoints[iPoint].y )            dfMinY = paoPoints[iPoint].y;    }    psEnvelope->MinX = dfMinX;    psEnvelope->MaxX = dfMaxX;    psEnvelope->MinY = dfMinY;    psEnvelope->MaxY = dfMaxY;}/************************************************************************//*                               Equals()                                *//************************************************************************/UGKBool UGKLineString::Equals( UGKGeometry * poOther ) const{    UGKLineString       *poOLine = (UGKLineString *) poOther;        if( poOLine == this )        return TRUE;        if( poOther->getGeometryType() != getGeometryType() )//类型不一样        return FALSE;    if( getNumPoints() != poOLine->getNumPoints() )        return FALSE;    for( int iPoint = 0; iPoint < getNumPoints(); iPoint++ )    {        if( getX(iPoint) != poOLine->getX(iPoint)            || getY(iPoint) != poOLine->getY(iPoint)             || getZ(iPoint) != poOLine->getZ(iPoint) )            return FALSE;    }    return TRUE;}/********************************************************************** *                       TABGenerateArc() * * Generate the coordinates for an arc and ADD the coordinates to the  * geometry object.  If the geometry already contains some points then * these won't be lost. * * poLine can be a UGKLineString or one of its derived classes, such as  *        UGKLinearRing * numPoints is the number of points to generate. * Angles are specified in radians, valid values are in the range [0..2*PI] * * Arcs are always generated counterclockwise, even if StartAngle > EndAngle * * Returns 0 on success, -1 on error. **********************************************************************/int TABGenerateArc(UGKLineString *poLine, int numPoints,                    double dCenterX, double dCenterY,                   double dXRadius, double dYRadius,                   double dStartAngle, double dEndAngle){    double dX, dY, dAngleStep, dAngle=0.0;    int i;    // Adjust angles to go counterclockwise    if (dEndAngle < dStartAngle)        dEndAngle += 2.0*PI;    dAngleStep = (dEndAngle-dStartAngle)/(numPoints-1.0);    for(i=0; i<numPoints; i++)    {        dAngle = (dStartAngle + (double)i*dAngleStep);        dX = dCenterX + dXRadius*cos(dAngle);        dY = dCenterY + dYRadius*sin(dAngle);        poLine->addPoint(dX, dY);    }    // Complete the arc with the last EndAngle, to make sure that     // the arc is correcly close.    dX = dCenterX + dXRadius*cos(dAngle);    dY = dCenterY + dYRadius*sin(dAngle);    poLine->addPoint(dX,dY);    return 0;}/********************************************************************** *                       TABCloseRing() * * Check if a ring is closed, and add a point to close it if necessary. * * Returns 0 on success, -1 on error. **********************************************************************/int TABCloseRing(UGKLineString *poRing){    if ( poRing->getNumPoints() > 0 && !poRing->get_IsClosed() )    {        poRing->addPoint(poRing->getX(0), poRing->getY(0));    }    return 0;}/********************************************************************** *                   UGKPointInRing() * * Returns TRUE is point is inside ring, FALSE otherwise * * Adapted version of msPointInPolygon() from MapServer's mapsearch.c **********************************************************************/UGKBool UGKPointInRing(UGKPoint *poPoint, UGKLineString *poRing){    int i, j, numpoints;    UGKBool status = FALSE;    double x, y;    numpoints = poRing->getNumPoints();    x = poPoint->getX();    y = poPoint->getY();    for (i = 0, j = numpoints-1; i < numpoints; j = i++)     {        if ((((poRing->getY(i)<=y) && (y<poRing->getY(j))) ||              ((poRing->getY(j)<=y) && (y<poRing->getY(i)))) &&             (x < (poRing->getX(j) - poRing->getX(i)) * (y - poRing->getY(i)) /                  (poRing->getY(j) - poRing->getY(i)) + poRing->getX(i)))            status = !status;    }    return status;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -