31                              const QVector<QgsPointXY> &destinationCoordinates,
 
   32                              QgsPointXY &origin, 
double &pixelXSize, 
double &pixelYSize )
 
   34  const int n = destinationCoordinates.size();
 
   37    throw std::domain_error( QObject::tr( 
"Fit to a linear transform requires at least 2 points." ).toLocal8Bit().constData() );
 
   40  double sumPx( 0 ), sumPy( 0 ), sumPx2( 0 ), sumPy2( 0 ), sumPxMx( 0 ), sumPyMy( 0 ), sumMx( 0 ), sumMy( 0 );
 
   41  for ( 
int i = 0; i < n; ++i )
 
   43    sumPx += sourceCoordinates.at( i ).x();
 
   44    sumPy += sourceCoordinates.at( i ).y();
 
   45    sumPx2 += std::pow( sourceCoordinates.at( i ).x(), 2 );
 
   46    sumPy2 += std::pow( sourceCoordinates.at( i ).y(), 2 );
 
   47    sumPxMx += sourceCoordinates.at( i ).x() * destinationCoordinates.at( i ).x();
 
   48    sumPyMy += sourceCoordinates.at( i ).y() * destinationCoordinates.at( i ).y();
 
   49    sumMx += destinationCoordinates.at( i ).x();
 
   50    sumMy += destinationCoordinates.at( i ).y();
 
   53  const double deltaX = n * sumPx2 - std::pow( sumPx, 2 );
 
   54  const double deltaY = n * sumPy2 - std::pow( sumPy, 2 );
 
   56  const double aX = ( sumPx2 * sumMx - sumPx * sumPxMx ) / deltaX;
 
   57  const double aY = ( sumPy2 * sumMy - sumPy * sumPyMy ) / deltaY;
 
   58  const double bX = ( n * sumPxMx - sumPx * sumMx ) / deltaX;
 
   59  const double bY = ( n * sumPyMy - sumPy * sumMy ) / deltaY;
 
   64  pixelXSize = std::fabs( bX );
 
   65  pixelYSize = std::fabs( bY );
 
 
   70                               const QVector<QgsPointXY> &destinationCoordinates,
 
   75  ( void )sourceCoordinates;
 
   76  ( void )destinationCoordinates;
 
   80  throw QgsNotSupportedException( QObject::tr( 
"Calculating a helmert transformation requires a QGIS build based GSL" ) );
 
   82  const int n = destinationCoordinates.size();
 
   85    throw std::domain_error( QObject::tr( 
"Fit to a Helmert transform requires at least 2 points." ).toLocal8Bit().constData() );
 
   98  for ( 
int i = 0; i < n; ++i )
 
  100    A += sourceCoordinates.at( i ).x();
 
  101    B += sourceCoordinates.at( i ).y();
 
  102    C += destinationCoordinates.at( i ).x();
 
  103    D += destinationCoordinates.at( i ).y();
 
  104    E += destinationCoordinates.at( i ).x() * sourceCoordinates.at( i ).x();
 
  105    F += destinationCoordinates.at( i ).y() * sourceCoordinates.at( i ).y();
 
  106    G += std::pow( sourceCoordinates.at( i ).x(), 2 );
 
  107    H += std::pow( sourceCoordinates.at( i ).y(), 2 );
 
  108    I += destinationCoordinates.at( i ).x() * sourceCoordinates.at( i ).y();
 
  109    J += sourceCoordinates.at( i ).x() * destinationCoordinates.at( i ).y();
 
  117  double MData[] = { A,   -B, ( double ) n,    0.,
 
  118                     B,    A,    0., ( 
double ) n,
 
  123  double bData[] = { C,    D,    E + F,  J - I };
 
  126  gsl_matrix_view M = gsl_matrix_view_array( MData, 4, 4 );
 
  127  const gsl_vector_view b = gsl_vector_view_array( bData, 4 );
 
  128  gsl_vector *x = gsl_vector_alloc( 4 );
 
  129  gsl_permutation *p = gsl_permutation_alloc( 4 );
 
  131  gsl_linalg_LU_decomp( &M.matrix, p, &s );
 
  132  gsl_linalg_LU_solve( &M.matrix, p, &b.vector, x );
 
  133  gsl_permutation_free( p );
 
  135  origin.
setX( gsl_vector_get( x, 2 ) );
 
  136  origin.
setY( gsl_vector_get( x, 3 ) );
 
  137  pixelSize = std::sqrt( std::pow( gsl_vector_get( x, 0 ), 2 ) +
 
  138                         std::pow( gsl_vector_get( x, 1 ), 2 ) );
 
  139  rotation = std::atan2( gsl_vector_get( x, 1 ), gsl_vector_get( x, 0 ) );
 
  141  gsl_vector_free( x );
 
 
  206                           double normalizeMatrix[9], 
double denormalizeMatrix[9] )
 
  209  double cogX = 0.0, cogY = 0.0;
 
  210  for ( 
int i = 0; i < coords.size(); i++ )
 
  212    cogX += coords[i].x();
 
  213    cogY += coords[i].y();
 
  215  cogX *= 1.0 / coords.size();
 
  216  cogY *= 1.0 / coords.size();
 
  219  double meanDist = 0.0;
 
  220  for ( 
int i = 0; i < coords.size(); i++ )
 
  222    const double X = ( coords[i].x() - cogX );
 
  223    const double Y = ( coords[i].y() - cogY );
 
  224    meanDist += std::sqrt( X * X + Y * Y );
 
  226  meanDist *= 1.0 / coords.size();
 
  228  const double OOD = meanDist * M_SQRT1_2;
 
  229  const double D   = 1.0 / OOD;
 
  230  normalizedCoords.resize( coords.size() );
 
  231  for ( 
int i = 0; i < coords.size(); i++ )
 
  233    normalizedCoords[i] = 
QgsPointXY( ( coords[i].x() - cogX ) * D, ( coords[i].y() - cogY ) * D );
 
  236  normalizeMatrix[0] = D;
 
  237  normalizeMatrix[1] = 0.0;
 
  238  normalizeMatrix[2] = -cogX * D;
 
  239  normalizeMatrix[3] = 0.0;
 
  240  normalizeMatrix[4] = D;
 
  241  normalizeMatrix[5] = -cogY * D;
 
  242  normalizeMatrix[6] = 0.0;
 
  243  normalizeMatrix[7] = 0.0;
 
  244  normalizeMatrix[8] = 1.0;
 
  246  denormalizeMatrix[0] = OOD;
 
  247  denormalizeMatrix[1] = 0.0;
 
  248  denormalizeMatrix[2] = cogX;
 
  249  denormalizeMatrix[3] = 0.0;
 
  250  denormalizeMatrix[4] = OOD;
 
  251  denormalizeMatrix[5] = cogY;
 
  252  denormalizeMatrix[6] = 0.0;
 
  253  denormalizeMatrix[7] = 0.0;
 
  254  denormalizeMatrix[8] = 1.0;
 
 
  260                                  const QVector<QgsPointXY> &destinationCoordinates,
 
  264  ( void )sourceCoordinates;
 
  265  ( void )destinationCoordinates;
 
  267  throw QgsNotSupportedException( QObject::tr( 
"Calculating a projective transformation requires a QGIS build based GSL" ) );
 
  269  Q_ASSERT( sourceCoordinates.size() == destinationCoordinates.size() );
 
  271  if ( destinationCoordinates.size() < 4 )
 
  273    throw std::domain_error( QObject::tr( 
"Fitting a projective transform requires at least 4 corresponding points." ).toLocal8Bit().constData() );
 
  276  QVector<QgsPointXY> sourceCoordinatesNormalized;
 
  277  QVector<QgsPointXY> destinationCoordinatesNormalized;
 
  279  double normSource[9], denormSource[9];
 
  280  double normDest[9], denormDest[9];
 
  281  normalizeCoordinates( sourceCoordinates, sourceCoordinatesNormalized, normSource, denormSource );
 
  282  normalizeCoordinates( destinationCoordinates, destinationCoordinatesNormalized, normDest, denormDest );
 
  286  const uint m = std::max( 9u, ( uint )destinationCoordinatesNormalized.size() * 2u );
 
  288  gsl_matrix *S = gsl_matrix_alloc( m, n );
 
  290  for ( 
int i = 0; i < destinationCoordinatesNormalized.size(); i++ )
 
  292    gsl_matrix_set( S, i * 2, 0, sourceCoordinatesNormalized[i].x() );
 
  293    gsl_matrix_set( S, i * 2, 1, sourceCoordinatesNormalized[i].y() );
 
  294    gsl_matrix_set( S, i * 2, 2, 1.0 );
 
  296    gsl_matrix_set( S, i * 2, 3, 0.0 );
 
  297    gsl_matrix_set( S, i * 2, 4, 0.0 );
 
  298    gsl_matrix_set( S, i * 2, 5, 0.0 );
 
  300    gsl_matrix_set( S, i * 2, 6, -destinationCoordinatesNormalized[i].x()*sourceCoordinatesNormalized[i].x() );
 
  301    gsl_matrix_set( S, i * 2, 7, -destinationCoordinatesNormalized[i].x()*sourceCoordinatesNormalized[i].y() );
 
  302    gsl_matrix_set( S, i * 2, 8, -destinationCoordinatesNormalized[i].x() * 1.0 );
 
  304    gsl_matrix_set( S, i * 2 + 1, 0, 0.0 );
 
  305    gsl_matrix_set( S, i * 2 + 1, 1, 0.0 );
 
  306    gsl_matrix_set( S, i * 2 + 1, 2, 0.0 );
 
  308    gsl_matrix_set( S, i * 2 + 1, 3, sourceCoordinatesNormalized[i].x() );
 
  309    gsl_matrix_set( S, i * 2 + 1, 4, sourceCoordinatesNormalized[i].y() );
 
  310    gsl_matrix_set( S, i * 2 + 1, 5, 1.0 );
 
  312    gsl_matrix_set( S, i * 2 + 1, 6, -destinationCoordinatesNormalized[i].y()*sourceCoordinatesNormalized[i].x() );
 
  313    gsl_matrix_set( S, i * 2 + 1, 7, -destinationCoordinatesNormalized[i].y()*sourceCoordinatesNormalized[i].y() );
 
  314    gsl_matrix_set( S, i * 2 + 1, 8, -destinationCoordinatesNormalized[i].y() * 1.0 );
 
  317  if ( destinationCoordinatesNormalized.size() == 4 )
 
  325    for ( 
int j = 0; j < 9; j++ )
 
  327      gsl_matrix_set( S, 8, j, gsl_matrix_get( S, 7, j ) );
 
  335  gsl_matrix *V = gsl_matrix_alloc( n, n );
 
  336  gsl_vector *singular_values = gsl_vector_alloc( n );
 
  337  gsl_vector *work = gsl_vector_alloc( n );
 
  341  gsl_linalg_SV_decomp( S, V, singular_values, work );
 
  344  for ( 
unsigned int i = 0; i < n; i++ )
 
  346    H[i] = gsl_matrix_get( V, i, n - 1 );
 
  349  gsl_matrix *prodMatrix = gsl_matrix_alloc( 3, 3 );
 
  351  gsl_matrix_view Hmatrix = gsl_matrix_view_array( H, 3, 3 );
 
  352  const gsl_matrix_view normSourceMatrix = gsl_matrix_view_array( normSource, 3, 3 );
 
  353  const gsl_matrix_view denormDestMatrix = gsl_matrix_view_array( denormDest, 3, 3 );
 
  357  gsl_blas_dgemm( CblasNoTrans, CblasNoTrans, 1.0, &Hmatrix.matrix, &normSourceMatrix.matrix, 0.0, prodMatrix );
 
  358  gsl_blas_dgemm( CblasNoTrans, CblasNoTrans, 1.0, &denormDestMatrix.matrix, prodMatrix, 0.0, &Hmatrix.matrix );
 
  360  gsl_matrix_free( prodMatrix );
 
  361  gsl_matrix_free( S );
 
  362  gsl_matrix_free( V );
 
  363  gsl_vector_free( singular_values );
 
  364  gsl_vector_free( work );