vtkGeoTransform.cxx 6.01 KB
Newer Older
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
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkGeoTransform.cxx

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
/*-------------------------------------------------------------------------
  Copyright 2008 Sandia Corporation.
  Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
  the U.S. Government retains certain rights in this software.
-------------------------------------------------------------------------*/

#include "vtkGeoTransform.h"

#include "vtkDoubleArray.h"
#include "vtkGeoProjection.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkPoints.h"

#include "vtk_libproj4.h"

vtkStandardNewMacro(vtkGeoTransform);
vtkCxxSetObjectMacro(vtkGeoTransform, SourceProjection, vtkGeoProjection);
vtkCxxSetObjectMacro(vtkGeoTransform, DestinationProjection, vtkGeoProjection);

vtkGeoTransform::vtkGeoTransform()
{
  this->SourceProjection = 0;
  this->DestinationProjection = 0;
}

vtkGeoTransform::~vtkGeoTransform()
{
  if ( this->SourceProjection )
    {
    this->SourceProjection->Delete();
    }
  if ( this->DestinationProjection )
    {
    this->DestinationProjection->Delete();
    }
}

void vtkGeoTransform::PrintSelf( ostream& os, vtkIndent indent )
{
  this->Superclass::PrintSelf( os, indent );
  os << indent << "SourceProjection: " << this->SourceProjection << "\n";
  os << indent << "DestinationProjection: " << this->DestinationProjection << "\n";
}

void vtkGeoTransform::TransformPoints( vtkPoints* srcPts, vtkPoints* dstPts )
{
  if ( ! srcPts || ! dstPts )
    {
    return;
    }

  vtkDoubleArray* srcCoords = vtkDoubleArray::SafeDownCast( srcPts->GetData() );
  vtkDoubleArray* dstCoords = vtkDoubleArray::SafeDownCast( dstPts->GetData() );
  if ( ! srcCoords || ! dstCoords )
    { // data not in a form we can use directly anyway...
    this->Superclass::TransformPoints( srcPts, dstPts );
    return;
    }
  dstCoords->DeepCopy( srcCoords );

  PROJ* src = this->SourceProjection ? this->SourceProjection->GetProjection() : 0;
  PROJ* dst = this->DestinationProjection ? this->DestinationProjection->GetProjection() : 0;
  if ( ! src && ! dst )
    {
    // we've already copied srcCoords to dstCoords and src=dst=0 implies no transform...
    return;
    }

  if ( srcCoords->GetNumberOfComponents() < 2 )
    {
    vtkErrorMacro( << "Source coordinate array " << srcCoords << " only has " << srcCoords->GetNumberOfComponents()
      << " components and at least 2 are required for geographic projections." );
    return;
    }

  this->InternalTransformPoints( dstCoords->GetPointer( 0 ), dstCoords->GetNumberOfTuples(), dstCoords->GetNumberOfComponents() );
}

void vtkGeoTransform::Inverse()
{
  vtkGeoProjection* tmp = this->SourceProjection;
  this->SourceProjection = this->DestinationProjection;
  this->DestinationProjection = tmp;
  this->Modified();
}

void vtkGeoTransform::InternalTransformPoint( const float in[3], float out[3] )
{
  double ind[3];
  double oud[3];
  int i;
  for ( i = 0; i < 3; ++ i )
    ind[i] = in[i];
  this->InternalTransformPoint( ind, oud );
  for ( i = 0; i < 3; ++ i )
Francois Bertel's avatar
Francois Bertel committed
111
    out[i] = static_cast<float>(oud[i]);
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
}

void vtkGeoTransform::InternalTransformPoint( const double in[3], double out[3] )
{
  for ( int i = 0; i < 3; ++ i )
    {
    out[i] = in[i];
    }
  this->InternalTransformPoints( out, 1, 3 );
}

void vtkGeoTransform::InternalTransformDerivative( const float in[3], float out[3], float derivative[3][3] )
{
  double ind[3];
  double oud[3];
  double drd[3][3];
  int i;
  for ( i = 0; i < 3; ++ i )
    ind[i] = in[i];
  this->InternalTransformDerivative( ind, oud, drd );
  for ( i = 0; i < 3; ++ i )
    {
Francois Bertel's avatar
Francois Bertel committed
134
    out[i] = static_cast<float>(oud[i]);
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
    for ( int j = 0; j < 3; ++ j )
      {
      derivative[i][j] = drd[i][j];
      }
    }
}

void vtkGeoTransform::InternalTransformDerivative( const double in[3], double out[3], double derivative[3][3] )
{
  // FIXME: Need to use pj_factors for both source and inverted dest projection
  (void) in;
  (void) out;
  (void) derivative;
}


vtkAbstractTransform* vtkGeoTransform::MakeTransform()
{
  vtkGeoTransform* geoTrans = vtkGeoTransform::New();
  return geoTrans;
}

void vtkGeoTransform::InternalTransformPoints( double* x, vtkIdType numPts, int stride )
{
  PROJ* src = this->SourceProjection ? this->SourceProjection->GetProjection() : 0;
  PROJ* dst = this->DestinationProjection ? this->DestinationProjection->GetProjection() : 0;
  int delta = stride - 2;
  PROJ_LP lp;
  PROJ_XY xy;
  if ( src )
    {
    // Convert from src system to lat/long using inverse of src transform
    double* coord = x;
    for ( vtkIdType i = 0; i < numPts; ++ i )
      {
      xy.x = coord[0]; xy.y = coord[1];
      lp = proj_inv( xy, src );
      coord[0] = lp.lam; coord[1] = lp.phi;
173
      coord += stride;
174
175
      }
    }
176
  else // ! src
177
178
179
180
181
    {
    // src coords are in degrees, convert to radians
    double* coord = x;
    for ( vtkIdType i = 0; i < numPts; ++ i )
      {
182
      for ( int j = 0; j < 2; ++ j, ++ coord )
183
        {
184
        *coord = vtkMath::RadiansFromDegrees( *coord );
185
186
187
188
189
190
191
192
193
194
195
196
        }
      coord += delta;
      }
    }
  if ( dst )
    {
    double* coord = x;
    for ( vtkIdType i = 0; i < numPts; ++ i )
      {
      lp.lam = coord[0]; lp.phi = coord[1];
      xy = proj_fwd( lp, dst );
      coord[0] = xy.x; coord[1] = xy.y;
197
      coord += stride;
198
199
200
201
202
203
204
205
      }
    }
  else // ! dst
    {
    // dst coords are in radians, convert to degrees
    double* coord = x;
    for ( vtkIdType i = 0; i < numPts; ++ i )
      {
206
      for ( int j = 0; j < 2; ++ j, ++ coord )
207
        {
208
        *coord = vtkMath::DegreesFromRadians( *coord );
209
210
211
212
213
        }
      coord += delta;
      }
    }
}