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
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkGeoFileImageSource.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 "vtkGeoFileImageSource.h"
#include "vtkDoubleArray.h"
#include "vtkGeoImageNode.h"
#include "vtkImageData.h"
#include "vtkImageGridSource.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkSmartPointer.h"
#include "vtkStdString.h"
#include "vtkTexture.h"
#include "vtkTransform.h"
#include "vtkXMLImageDataReader.h"
#include <vtksys/ios/fstream>
#include <vtksys/ios/sstream>
#include <vtksys/stl/utility>
vtkStandardNewMacro(vtkGeoFileImageSource);
//----------------------------------------------------------------------------
vtkGeoFileImageSource::vtkGeoFileImageSource()
{
this->Path = 0;
}
//----------------------------------------------------------------------------
vtkGeoFileImageSource::~vtkGeoFileImageSource()
{
this->SetPath(0);
}
//----------------------------------------------------------------------------
bool vtkGeoFileImageSource::FetchRoot(vtkGeoTreeNode* r)
{
vtkGeoImageNode* root = 0;
if (!(root = vtkGeoImageNode::SafeDownCast(r)))
{
vtkErrorMacro(<< "Can only fetch image nodes from this source.");
return false;
}
root->SetLatitudeRange(-270, 90);
root->SetLongitudeRange(-180, 180);
this->ReadImage(-1, 0, root);
return true;
}
//----------------------------------------------------------------------------
bool vtkGeoFileImageSource::FetchChild(vtkGeoTreeNode* p, int index, vtkGeoTreeNode* c)
{
vtkGeoImageNode* parent = 0;
if (!(parent = vtkGeoImageNode::SafeDownCast(p)))
{
vtkErrorMacro(<< "Can only fetch image nodes from this source.");
return false;
}
vtkGeoImageNode* child = 0;
if (!(child = vtkGeoImageNode::SafeDownCast(c)))
{
vtkErrorMacro(<< "Can only fetch image nodes from this source.");
return false;
}
if (parent->GetLevel() == -1)
{
// Child 0 is the dummy western hemisphere, child 1 is the dummy eastern hemisphere
// Child 2 is the western hemisphere, child 3 is the eastern hemisphere
if (index == 0)
{
vtkSmartPointer<vtkImageData> dummyImageWest = vtkSmartPointer<vtkImageData>::New();
dummyImageWest->SetOrigin(-180.0, -270.0, 0.0);
dummyImageWest->SetSpacing(0.0, -90.0, 0.0);
child->GetTexture()->SetInputData(dummyImageWest);
child->SetLatitudeRange(-270, -90);
child->SetLongitudeRange(-180, 0);
}
else if (index == 1)
{
vtkSmartPointer<vtkImageData> dummyImageEast = vtkSmartPointer<vtkImageData>::New();
dummyImageEast->SetOrigin(0.0, -270.0, 0.0);
dummyImageEast->SetSpacing(180.0, -90.0, 0.0);
child->GetTexture()->SetInputData(dummyImageEast);
child->SetLatitudeRange(-270, -90);
child->SetLongitudeRange(0, 180);
}
else if (index == 2)
{
this->ReadImage(0, 0, child);
}
else
{
this->ReadImage(0, 1, child);
}
return true;
}
int level = parent->GetLevel() + 1;
int id = parent->GetId() | (index << (2*level-1));
return this->ReadImage(level, id, child);
}
//----------------------------------------------------------------------------
bool vtkGeoFileImageSource::ReadImage(int level, int id, vtkGeoImageNode* node)
{
// Load the image
node->SetId(id);
node->SetLevel(level);
vtkSmartPointer<vtkXMLImageDataReader> reader = vtkSmartPointer<vtkXMLImageDataReader>::New();
vtksys_ios::stringstream ss;
ss.str("");
ss << this->Path << "/tile_" << level << "_" << id << ".vti";
// Check if the file exists
vtksys_ios::ifstream in;
in.open(ss.str().c_str(), vtksys_ios::ifstream::in);
if (in.fail())
{
// Make a dummy image
in.close();
vtkSmartPointer<vtkImageData> dummy = vtkSmartPointer<vtkImageData>::New();
dummy->SetDimensions(1, 1, 1);
vtkSmartPointer<vtkDoubleArray> scalar = vtkSmartPointer<vtkDoubleArray>::New();
scalar->InsertNextValue(0.0);
dummy->GetPointData()->SetScalars(scalar);
dummy->SetOrigin(node->GetLongitudeRange()[0], node->GetLatitudeRange()[0], 0.0);
dummy->SetSpacing(node->GetLongitudeRange()[1], node->GetLatitudeRange()[1], 0.0);
node->GetTexture()->SetInputData(dummy);
return false;
}
in.close();
// Read the file
reader->SetFileName(ss.str().c_str());
reader->Update();
vtkImageData* image = reader->GetOutput();
// Retrieve the image bounds using origin and spacing
double x1[3];
double x2[3];
image->GetOrigin(x1);
image->GetSpacing(x2);
double lonRange[2] = {x1[0], x2[0]};
double latRange[2] = {x1[1], x2[1]};
node->SetLatitudeRange(latRange);
node->SetLongitudeRange(lonRange);
// Make the texture with the correct transform
vtkSmartPointer<vtkTexture> texture = vtkSmartPointer<vtkTexture>::New();
vtkSmartPointer<vtkTransform> texTrans = vtkSmartPointer<vtkTransform>::New();
// Start with (lat,lon)
texTrans->PostMultiply();
texTrans->RotateZ(90.0); // (-lon,lat)
texTrans->Scale(-1.0, 1.0, 1.0); // (lon,lat)
texTrans->Translate(-lonRange[0], -latRange[0], 0.0); // to origin
texTrans->Scale(1.0/(lonRange[1] - lonRange[0]), 1.0/(latRange[1] - latRange[0]), 1.0); // to [0,1]
texture->SetInputConnection(reader->GetOutputPort());
texture->SetTransform(texTrans);
texture->RepeatOff();
texture->InterpolateOn();
texture->EdgeClampOn();
node->SetTexture(texture);
return true;
}
//-----------------------------------------------------------------------------
void vtkGeoFileImageSource::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "Path: " << (this->Path ? this->Path : "(none)") << endl;
}
|