1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
|
/******************************************************************************
* $Id: gdal_rpc.cpp,v 1.3 2004/12/26 16:12:21 fwarmerdam Exp $
*
* Project: Image Warper
* Purpose: Implements a rational polynomail (RPC) based transformer.
* Author: Frank Warmerdam, warmerdam@pobox.com
*
******************************************************************************
* Copyright (c) 2003, Frank Warmerdam <warmerdam@pobox.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
******************************************************************************
*
* $Log: gdal_rpc.cpp,v $
* Revision 1.3 2004/12/26 16:12:21 fwarmerdam
* thin plate spline support now implemented
*
* Revision 1.2 2003/06/03 19:41:58 warmerda
* working implmentation, but inverse is still not iteratively solved
*
* Revision 1.1 2003/06/03 17:36:43 warmerda
* New
*
*/
#include "gdal_priv.h"
#include "gdal_alg.h"
#include "ogr_spatialref.h"
CPL_CVSID("$Id: gdal_rpc.cpp,v 1.3 2004/12/26 16:12:21 fwarmerdam Exp $");
/************************************************************************/
/* RPCComputeTerms() */
/************************************************************************/
static void RPCComputeTerms( double dfLong, double dfLat, double dfHeight,
double *padfTerms )
{
padfTerms[0] = 1.0;
padfTerms[1] = dfLong;
padfTerms[2] = dfLat;
padfTerms[3] = dfHeight;
padfTerms[4] = dfLong * dfLat;
padfTerms[5] = dfLong * dfHeight;
padfTerms[6] = dfLat * dfHeight;
padfTerms[7] = dfLong * dfLong;
padfTerms[8] = dfLat * dfLat;
padfTerms[9] = dfHeight * dfHeight;
padfTerms[10] = dfLong * dfLat * dfHeight;
padfTerms[11] = dfLong * dfLong * dfLong;
padfTerms[12] = dfLong * dfLat * dfLat;
padfTerms[13] = dfLong * dfHeight * dfHeight;
padfTerms[14] = dfLong * dfLong * dfLat;
padfTerms[15] = dfLat * dfLat * dfLat;
padfTerms[16] = dfLat * dfHeight * dfHeight;
padfTerms[17] = dfLong * dfLong * dfHeight;
padfTerms[18] = dfLat * dfLat * dfHeight;
padfTerms[19] = dfHeight * dfHeight * dfHeight;
}
/************************************************************************/
/* RPCEvaluate() */
/************************************************************************/
static double RPCEvaluate( double *padfTerms, double *padfCoefs )
{
double dfSum = 0.0;
int i;
for( i = 0; i < 20; i++ )
dfSum += padfTerms[i] * padfCoefs[i];
return dfSum;
}
/************************************************************************/
/* RPCTransformPoint() */
/************************************************************************/
static void RPCTransformPoint( GDALRPCInfo *psRPC,
double dfLong, double dfLat, double dfHeight,
double *pdfPixel, double *pdfLine )
{
double dfResultX, dfResultY;
double adfTerms[20];
RPCComputeTerms(
(dfLong - psRPC->dfLONG_OFF) / psRPC->dfLONG_SCALE,
(dfLat - psRPC->dfLAT_OFF) / psRPC->dfLAT_SCALE,
(dfHeight - psRPC->dfHEIGHT_OFF) / psRPC->dfHEIGHT_SCALE,
adfTerms );
dfResultX = RPCEvaluate( adfTerms, psRPC->adfSAMP_NUM_COEFF )
/ RPCEvaluate( adfTerms, psRPC->adfSAMP_DEN_COEFF );
dfResultY = RPCEvaluate( adfTerms, psRPC->adfLINE_NUM_COEFF )
/ RPCEvaluate( adfTerms, psRPC->adfLINE_DEN_COEFF );
*pdfPixel = dfResultX * psRPC->dfSAMP_SCALE + psRPC->dfSAMP_OFF;
*pdfLine = dfResultY * psRPC->dfLINE_SCALE + psRPC->dfLINE_OFF;
}
/************************************************************************/
/* ==================================================================== */
/* GDALRPCTransformer */
/* ==================================================================== */
/************************************************************************/
typedef struct {
GDALTransformerInfo sTI;
GDALRPCInfo sRPC;
double adfPLToLatLongGeoTransform[6];
int bReversed;
double dfPixErrThreshold;
} GDALRPCTransformInfo;
/************************************************************************/
/* GDALCreateRPCTransformer() */
/************************************************************************/
void *GDALCreateRPCTransformer( GDALRPCInfo *psRPCInfo, int bReversed,
double dfPixErrThreshold )
{
GDALRPCTransformInfo *psTransform;
/* -------------------------------------------------------------------- */
/* Initialize core info. */
/* -------------------------------------------------------------------- */
psTransform = (GDALRPCTransformInfo *)
CPLCalloc(sizeof(GDALRPCTransformInfo),1);
memcpy( &(psTransform->sRPC), psRPCInfo, sizeof(GDALRPCInfo) );
psTransform->bReversed = bReversed;
psTransform->dfPixErrThreshold = dfPixErrThreshold;
strcpy( psTransform->sTI.szSignature, "GTI" );
psTransform->sTI.pszClassName = "GDALRPCTransformer";
psTransform->sTI.pfnTransform = GDALRPCTransform;
psTransform->sTI.pfnCleanup = GDALDestroyRPCTransformer;
psTransform->sTI.pfnSerialize = NULL;
/* -------------------------------------------------------------------- */
/* Establish a reference point for calcualating an affine */
/* geotransform approximate transformation. */
/* -------------------------------------------------------------------- */
double adfGTFromLL[6], dfRefPixel, dfRefLine;
double dfRefLong = (psRPCInfo->dfMIN_LONG + psRPCInfo->dfMAX_LONG) * 0.5;
double dfRefLat = (psRPCInfo->dfMIN_LAT + psRPCInfo->dfMAX_LAT ) * 0.5;
RPCTransformPoint( psRPCInfo, dfRefLong, dfRefLat, 0.0,
&dfRefPixel, &dfRefLine );
/* -------------------------------------------------------------------- */
/* Transform nearby locations to establish affine direction */
/* vectors. */
/* -------------------------------------------------------------------- */
double dfRefPixelDelta, dfRefLineDelta, dfLLDelta = 0.0001;
RPCTransformPoint( psRPCInfo, dfRefLong+dfLLDelta, dfRefLat, 0.0,
&dfRefPixelDelta, &dfRefLineDelta );
adfGTFromLL[1] = (dfRefPixelDelta - dfRefPixel) / dfLLDelta;
adfGTFromLL[2] = (dfRefLineDelta - dfRefLine) / dfLLDelta;
RPCTransformPoint( psRPCInfo, dfRefLong, dfRefLat+dfLLDelta, 0.0,
&dfRefPixelDelta, &dfRefLineDelta );
adfGTFromLL[4] = (dfRefPixelDelta - dfRefPixel) / dfLLDelta;
adfGTFromLL[5] = (dfRefLineDelta - dfRefLine) / dfLLDelta;
adfGTFromLL[0] = dfRefPixel
- adfGTFromLL[1] * dfRefLong - adfGTFromLL[2] * dfRefLat;
adfGTFromLL[3] = dfRefLine
- adfGTFromLL[4] * dfRefLong - adfGTFromLL[5] * dfRefLat;
GDALInvGeoTransform( adfGTFromLL, psTransform->adfPLToLatLongGeoTransform);
return psTransform;
}
/************************************************************************/
/* GDALDestroyReprojectionTransformer() */
/************************************************************************/
void GDALDestroyRPCTransformer( void *pTransformAlg )
{
CPLFree( pTransformAlg );
}
/************************************************************************/
/* GDALRPCTransform() */
/************************************************************************/
int GDALRPCTransform( void *pTransformArg, int bDstToSrc,
int nPointCount,
double *padfX, double *padfY, double *padfZ,
int *panSuccess )
{
GDALRPCTransformInfo *psTransform = (GDALRPCTransformInfo *) pTransformArg;
GDALRPCInfo *psRPC = &(psTransform->sRPC);
int i;
if( psTransform->bReversed )
bDstToSrc = !bDstToSrc;
/* -------------------------------------------------------------------- */
/* The simple case is transforming from lat/long to pixel/line. */
/* Just apply the equations directly. */
/* -------------------------------------------------------------------- */
if( bDstToSrc )
{
for( i = 0; i < nPointCount; i++ )
{
RPCTransformPoint( psRPC, padfX[i], padfY[i], padfZ[i],
padfX + i, padfY + i );
panSuccess[i] = TRUE;
}
return TRUE;
}
/* -------------------------------------------------------------------- */
/* The more complicated issue is how to reverse this. For now */
/* we use a dead simple linear approximation. In the case of */
/* image warping, this direction is only used to pick bound for */
/* the newly created output file anyways. */
/* -------------------------------------------------------------------- */
for( i = 0; i < nPointCount; i++ )
{
double dfResultX, dfResultY;
dfResultX = psTransform->adfPLToLatLongGeoTransform[0]
+ psTransform->adfPLToLatLongGeoTransform[1] * padfX[i]
+ psTransform->adfPLToLatLongGeoTransform[2] * padfY[i];
dfResultY = psTransform->adfPLToLatLongGeoTransform[3]
+ psTransform->adfPLToLatLongGeoTransform[4] * padfX[i]
+ psTransform->adfPLToLatLongGeoTransform[5] * padfY[i];
padfX[i] = dfResultX;
padfY[i] = dfResultY;
panSuccess[i] = TRUE;
}
return TRUE;
}
|