vtkGeoImageNode.cxx 8.34 KB
Newer Older
Ken Martin's avatar
Ken Martin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*=============================================================================

  Program:   Visualization Toolkit
  Module:    vtkGeoImageNode.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 "vtkObjectFactory.h"
#include "vtkGeoImageNode.h"
#include "vtkJPEGWriter.h"
23
#include "vtkTexture.h"
Ken Martin's avatar
Ken Martin committed
24 25 26 27 28 29 30
#include "vtkXMLImageDataReader.h"
#include "vtkXMLImageDataWriter.h"

vtkStandardNewMacro(vtkGeoImageNode);


//----------------------------------------------------------------------------
31
vtkGeoImageNode::vtkGeoImageNode()
Ken Martin's avatar
Ken Martin committed
32 33
{
  this->Image = vtkSmartPointer<vtkImageData>::New();
34
  this->Texture = vtkSmartPointer<vtkTexture>::New();
Ken Martin's avatar
Ken Martin committed
35 36 37
}

//-----------------------------------------------------------------------------
38
vtkGeoImageNode::~vtkGeoImageNode()
Ken Martin's avatar
Ken Martin committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
{
}

//-----------------------------------------------------------------------------
void vtkGeoImageNode::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os,indent);
}

//-----------------------------------------------------------------------------
void vtkGeoImageNode::SetImage(vtkImageData* image)
{
  this->Image = image;
}

//-----------------------------------------------------------------------------
vtkImageData* vtkGeoImageNode::GetImage()
{
  return this->Image;
}

//-----------------------------------------------------------------------------
61 62 63 64 65 66 67 68 69 70 71 72
void vtkGeoImageNode::SetTexture(vtkTexture* texture)
{
  this->Texture = texture;
}

//-----------------------------------------------------------------------------
vtkTexture* vtkGeoImageNode::GetTexture()
{
  return this->Texture;
}

//-----------------------------------------------------------------------------
Ken Martin's avatar
Ken Martin committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
vtkGeoImageNode* vtkGeoImageNode::GetChild(int idx)
{
  if (idx < 0 || idx > 3)
    {
    vtkErrorMacro("Index out of range.");
    return 0;
    }
  return vtkGeoImageNode::SafeDownCast(this->Children[idx]);
}


//-----------------------------------------------------------------------------
vtkGeoImageNode* vtkGeoImageNode::GetParent()
{
  return vtkGeoImageNode::SafeDownCast(this->Parent);
}

//-----------------------------------------------------------------------------
// We have to get a power of 2 for dimensions of the image.  VTK
// resamples every time a tile is selected anr changed otherwise.
//
// We have two choises for dealing with images.
95 96
//
// 1: Treat pixels like cell data.
Ken Martin's avatar
Ken Martin committed
97
//   This makes subsampling easy.  Simply use vtkImageShrink3D.
98 99
//   Tile images do not overlap.
//   Difficult:
Ken Martin's avatar
Ken Martin committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
//   Texture mapping is point data.  TCoords have to be extended half a pixel.
//   vtkImageData is point data.  Have to handle meta data extenal to object.
//   Interpolated texture map will have seams between tiles.
// 2: Treat pixels like point data.
//   We would need a new shrink filter that uses a 3x3 kernel.
//   Tiles would have to duplicate a row of xels with neighbors.
//   This would make dividing image more difficult with natural pixel boundaries
//   every 255 pixels (instead of 256).

void vtkGeoImageNode::CropImageForTile(
  vtkImageData* image,
  double* imageLonLatExt,
  const char* prefix)
{
  int ext[6];
  int wholeExt[6];
116

Ken Martin's avatar
Ken Martin committed
117 118 119 120 121 122
  // I am keeping this all external to the imageData object because
  // I consider pixels are cells not points.
  image->GetExtent(ext);
  image->GetExtent(wholeExt);
  double origin[2];
  double spacing[2];
123 124 125 126
  spacing[0] = (imageLonLatExt[1]-imageLonLatExt[0])/(ext[1]-ext[0]+1);
  spacing[1] = (imageLonLatExt[3]-imageLonLatExt[2])/(ext[3]-ext[2]+1);
  origin[0] = imageLonLatExt[0] - ext[0]*spacing[0];
  origin[1] = imageLonLatExt[2] - ext[2]*spacing[1];
127

Ken Martin's avatar
Ken Martin committed
128
  // Compute the minimum extent that covers the terrain patch.
129 130 131 132
  ext[0] = static_cast<int>(floor((this->LongitudeRange[0]-origin[0])/spacing[0]));
  ext[1] = static_cast<int>(ceil((this->LongitudeRange[1]-origin[0])/spacing[0]));
  ext[2] = static_cast<int>(floor((this->LatitudeRange[0]-origin[1])/spacing[1]));
  ext[3] = static_cast<int>(ceil((this->LatitudeRange[1]-origin[1])/spacing[1]));
133

Ken Martin's avatar
Ken Martin committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
  int dims[2];
  dims[0] = this->PowerOfTwo(ext[1]-ext[0]+1);
  dims[1] = this->PowerOfTwo(ext[3]-ext[2]+1);
  ext[1] = ext[0] + dims[0] - 1;
  ext[3] = ext[2] + dims[1] - 1;
  if (ext[1] > wholeExt[1])
    {
    ext[1] = wholeExt[1];
    }
  if (ext[3] > wholeExt[3])
    {
    ext[3] = wholeExt[3];
    }
  ext[0] = ext[1] - dims[0] + 1;
  ext[2] = ext[3] - dims[1] + 1;
  if (ext[0] < wholeExt[0])
    {
    ext[0] = wholeExt[0];
    }
  if (ext[2] < wholeExt[2])
    {
    ext[2] = wholeExt[2];
    }
157

Ken Martin's avatar
Ken Martin committed
158 159 160 161 162
  if (this->Image == 0)
    {
    this->Image = vtkSmartPointer<vtkImageData>::New();
    }
  this->Image->ShallowCopy(image);
163
  this->Image->Crop(ext);
164

Ken Martin's avatar
Ken Martin committed
165
  // Now set the longitude and latitude range based on the actual image size.
166 167 168 169
  this->LongitudeRange[0] = origin[0] + ext[0]*spacing[0];
  this->LongitudeRange[1] = origin[0] + (ext[1]+1)*spacing[0];
  this->LatitudeRange[0] = origin[1] + ext[2]*spacing[1];
  this->LatitudeRange[1] = origin[1] + (ext[3]+1)*spacing[1];
170

Ken Martin's avatar
Ken Martin committed
171 172 173 174 175 176 177 178 179 180 181
  // Save out the image to verify we are processing properly
  if (prefix)
    {
    vtkImageData* storedImage = vtkImageData::New();
    storedImage->ShallowCopy(this->Image);
    storedImage->SetOrigin(this->LongitudeRange[0], this->LatitudeRange[0], 0);
    storedImage->SetSpacing(this->LongitudeRange[1], this->LatitudeRange[1], 0);
    vtkXMLImageDataWriter* writer = vtkXMLImageDataWriter::New();
    char fn[512];
    sprintf(fn, "%s/tile_%d_%ld.vti", prefix, this->Level, this->Id);
    writer->SetFileName(fn);
182
    writer->SetInputData(storedImage);
Ken Martin's avatar
Ken Martin committed
183 184 185 186 187 188 189
    writer->Write();
    writer->Delete();
    storedImage->Delete();
    }
}

//-----------------------------------------------------------------------------
Ken Martin's avatar
Ken Martin committed
190
void vtkGeoImageNode::LoadAnImage(const char* prefix)
Ken Martin's avatar
Ken Martin committed
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
{
  vtkXMLImageDataReader* reader = vtkXMLImageDataReader::New();
  char fn[512];
  sprintf(fn, "%s/tile_%d_%ld.vti", prefix, this->Level, this->Id);
  reader->SetFileName(fn);
  reader->Update();
  vtkImageData* image = reader->GetOutput();
  this->Image = image;
  this->LongitudeRange[0] = this->Image->GetOrigin()[0];
  this->LatitudeRange[0] = this->Image->GetOrigin()[1];
  this->LongitudeRange[1] = this->Image->GetSpacing()[0];
  this->LatitudeRange[1] = this->Image->GetSpacing()[1];
  reader->Delete();
}

//-----------------------------------------------------------------------------
int vtkGeoImageNode::PowerOfTwo(int val)
{
  // Pick the next highest power of two.
  int tmp;
  bool nextHigherFlag = false;
  tmp = 1;
  while (val)
    {
    if ((val & 1) && val > 1)
      {
      nextHigherFlag = true;
      }
    val = val >> 1;
    tmp = tmp << 1;
    }
222

Ken Martin's avatar
Ken Martin committed
223 224 225 226 227 228 229
  if ( ! nextHigherFlag)
    {
    tmp = tmp >> 1;
    }
  return tmp;
}

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
//-----------------------------------------------------------------------------
void vtkGeoImageNode::ShallowCopy(vtkGeoTreeNode *src)
{
  vtkGeoImageNode *imageNode = vtkGeoImageNode::SafeDownCast(src);

  if(imageNode != NULL)
    {
    this->Image = imageNode->Image;
    this->Texture = imageNode->Texture;
    }
  this->Superclass::ShallowCopy(src);
}

//-----------------------------------------------------------------------------
void vtkGeoImageNode::DeepCopy(vtkGeoTreeNode *src)
{
  vtkGeoImageNode *imageNode = vtkGeoImageNode::SafeDownCast(src);

  if(imageNode != NULL)
    {
    vtkImageData * image = vtkImageData::New();
    image->DeepCopy(imageNode->Image);
    this->SetImage(image);
    image->Delete();
    image = NULL;

    this->Texture = imageNode->Texture;
    }
  this->Superclass::DeepCopy(src);
}

261 262 263 264 265 266 267 268 269 270 271 272
//-----------------------------------------------------------------------------
bool vtkGeoImageNode::HasData()
{
  return (this->Image != 0);
}

//-----------------------------------------------------------------------------
void vtkGeoImageNode::DeleteData()
{
  this->Image = 0;
  this->Texture = 0;
}