vtkProjectedTexture.cxx 8.81 KB
Newer Older
1 2
/*=========================================================================

Will Schroeder's avatar
Will Schroeder committed
3 4 5
  Program:   Visualization Toolkit
  Module:    vtkProjectedTexture.cxx

6
  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 8
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
Will Schroeder's avatar
Will Schroeder committed
9

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

=========================================================================*/
#include "vtkProjectedTexture.h"
16 17

#include "vtkDataSet.h"
18
#include "vtkFloatArray.h"
19 20
#include "vtkInformation.h"
#include "vtkInformationVector.h"
21
#include "vtkMath.h"
22
#include "vtkObjectFactory.h"
23
#include "vtkPointData.h"
24

Brad King's avatar
Brad King committed
25
vtkStandardNewMacro(vtkProjectedTexture);
26 27 28

// Description:
// Initialize the projected texture filter with a position of (0, 0, 1),
29
// a focal point of (0, 0, 0), an up vector on the +y axis,
30 31 32 33
// an aspect ratio of the projection frustum of equal width, height, and focal
// length, an S range of (0, 1) and a T range of (0, 1).
vtkProjectedTexture::vtkProjectedTexture()
{
34 35 36 37
  this->Position[0] = 0.0;
  this->Position[1] = 0.0;
  this->Position[2] = 1.0;
  this->Orientation[0] = this->Orientation[1] = this->Orientation[2] = 0.0;
38
  this->SetFocalPoint(0.0, 0.0, 0.0);
39 40 41
  this->Up[0] = 0.0;
  this->Up[1] = 1.0;
  this->Up[2] = 0.0;
42
  this->AspectRatio[0] = 1.0;
43
  this->AspectRatio[1] = 1.0;
44
  this->AspectRatio[2] = 1.0;
45 46
  this->MirrorSeparation = 1.0;
  this->CameraMode = VTK_PROJECTED_TEXTURE_USE_PINHOLE;
47 48 49 50
  this->SRange[0] = 0.0;
  this->SRange[1] = 1.0;
  this->TRange[0] = 0.0;
  this->TRange[1] = 1.0;
51 52 53
}


Ken Martin's avatar
Ken Martin committed
54
void vtkProjectedTexture::SetFocalPoint(double fp[3])
55 56 57 58
{
  this->SetFocalPoint(fp[0], fp[1], fp[2]);
}

Ken Martin's avatar
Ken Martin committed
59
void vtkProjectedTexture::SetFocalPoint(double x, double y, double z)
60
{
Ken Martin's avatar
Ken Martin committed
61
  double orientation[3];
62 63 64 65 66 67 68 69 70 71 72 73 74 75

  orientation[0] = x - this->Position[0];
  orientation[1] = y - this->Position[1];
  orientation[2] = z - this->Position[2];
  vtkMath::Normalize(orientation);
  if (this->Orientation[0] != orientation[0] ||
      this->Orientation[1] != orientation[1] ||
      this->Orientation[2] != orientation[2])
    {
    this->Orientation[0] = orientation[0];
    this->Orientation[1] = orientation[1];
    this->Orientation[2] = orientation[2];
    this->Modified();
    }
Bill Lorensen's avatar
Bill Lorensen committed
76 77 78
  this->FocalPoint[0] = x;
  this->FocalPoint[1] = y;
  this->FocalPoint[2] = z;
79 80
}

81 82 83 84
int vtkProjectedTexture::RequestData(
  vtkInformation *vtkNotUsed(request),
  vtkInformationVector **inputVector,
  vtkInformationVector *outputVector)
85
{
86 87 88 89
  // get the info objects
  vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
  vtkInformation *outInfo = outputVector->GetInformationObject(0);

90
  // get the input and output
91 92 93 94 95
  vtkDataSet *input = vtkDataSet::SafeDownCast(
    inInfo->Get(vtkDataObject::DATA_OBJECT()));
  vtkDataSet *output = vtkDataSet::SafeDownCast(
    outInfo->Get(vtkDataObject::DATA_OBJECT()));

Ken Martin's avatar
Ken Martin committed
96
  double tcoords[2];
Amy Squillacote's avatar
Amy Squillacote committed
97
  vtkIdType numPts;
98
  vtkFloatArray *newTCoords;
Amy Squillacote's avatar
Amy Squillacote committed
99 100
  vtkIdType i;
  int j;
Ken Martin's avatar
Ken Martin committed
101 102 103 104
  double proj;
  double rightv[3], upv[3], diff[3];
  double sScale, tScale, sOffset, tOffset, sSize, tSize, s, t;
  double p[3];
105 106

  vtkDebugMacro(<<"Generating texture coordinates!");
107 108 109 110

  // First, copy the input to the output as a starting point
  output->CopyStructure( input );

111 112 113 114 115 116
  numPts=input->GetNumberOfPoints();

  //
  //  Allocate texture data
  //

117
  newTCoords = vtkFloatArray::New();
Charles Law's avatar
Charles Law committed
118
  newTCoords->SetName("ProjectedTextureCoordinates");
119 120
  newTCoords->SetNumberOfComponents(2);
  newTCoords->SetNumberOfTuples(numPts);
121 122

  vtkMath::Normalize (this->Orientation);
123

124 125 126 127 128 129 130 131 132
  vtkMath::Cross (this->Orientation, this->Up, rightv);
  vtkMath::Normalize (rightv);

  vtkMath::Cross (rightv, this->Orientation, upv);
  vtkMath::Normalize (upv);

  sSize = this->AspectRatio[0] / this->AspectRatio[2];
  tSize = this->AspectRatio[1] / this->AspectRatio[2];

Bill Lorensen's avatar
Bill Lorensen committed
133 134
  sScale = (this->SRange[1] - this->SRange[0])/sSize;
  tScale = (this->TRange[1] - this->TRange[0])/tSize;
135

Bill Lorensen's avatar
Bill Lorensen committed
136 137
  sOffset = (this->SRange[1] - this->SRange[0])/2.0 + this->SRange[0];
  tOffset = (this->TRange[1] - this->TRange[0])/2.0 + this->TRange[0];
138

139 140

  // compute s-t coordinates
141
  for (i = 0; i < numPts; i++)
142
    {
143
      output->GetPoint(i, p);
144

145
      for (j = 0; j < 3; j++)
146
        {
147
        diff[j] = p[j] - this->Position[j];
148
        }
149

150
      proj = vtkMath::Dot(diff, this->Orientation);
151

152
      // New mode to handle a two mirror camera with separation of
153 154 155
      // MirrorSeparation -- In this case, we assume that the first mirror
      // controls the elevation and the second controls the azimuth.  Texture
      // coordinates for the elevation are handled as normal, while those for
156
      // the azimuth must be calculated based on a new baseline difference to
157 158 159 160
      // include the mirror separation.
      if (this->CameraMode == VTK_PROJECTED_TEXTURE_USE_TWO_MIRRORS)
        {
        // First calculate elevation coordinate t.
161
        if(proj < 1.0e-10 && proj > -1.0e-10)
162 163 164
          {
          vtkWarningMacro(<<"Singularity:  point located at elevation frustum Position");
          tcoords[1] = tOffset;
165
          }
166
        else
167
          {
168 169 170 171
          for (j = 0; j < 3; j++)
            {
            diff[j] = diff[j]/proj - this->Orientation[j];
            }
172

173 174
          t = vtkMath::Dot(diff, upv);
          tcoords[1] = t * tScale + tOffset;
175
          }
176

177 178 179
        // Now with t complete, continue on to calculate coordinate s
        // by offsetting the center of the lens back by MirrorSeparation
        // in direction opposite to the orientation.
180
        for (j = 0; j < 3; j++)
181 182 183
          {
          diff[j] = p[j] - this->Position[j] + (this->MirrorSeparation*this->Orientation[j]);
          }
184

185
        proj = vtkMath::Dot(diff, this->Orientation);
186 187

        if(proj < 1.0e-10 && proj > -1.0e-10)
188 189 190
          {
          vtkWarningMacro(<<"Singularity:  point located at azimuth frustum Position");
          tcoords[0] = sOffset;
191
          }
192 193 194 195 196 197
        else
          {
          for (j = 0; j < 3; j++)
            {
            diff[j] = diff[j]/proj - this->Orientation[j];
            }
198

199 200 201 202 203 204 205 206 207
          s = vtkMath::Dot(diff, rightv);
          sSize = this->AspectRatio[0] / (this->AspectRatio[2] + this->MirrorSeparation);
          sScale = (this->SRange[1] - this->SRange[0])/sSize;
          sOffset = (this->SRange[1] - this->SRange[0])/2.0 + this->SRange[0];
          tcoords[0] = s * sScale + sOffset;
          }
        }
      else
        {
208
        if(proj < 1.0e-10 && proj > -1.0e-10)
209 210 211 212
          {
          vtkWarningMacro(<<"Singularity:  point located at frustum Position");
          tcoords[0] = sOffset;
          tcoords[1] = tOffset;
213 214
          }
        else
215 216 217 218 219
          {
          for (j = 0; j < 3; j++)
            {
            diff[j] = diff[j]/proj - this->Orientation[j];
            }
220

221 222
          s = vtkMath::Dot(diff, rightv);
          t = vtkMath::Dot(diff, upv);
223

224 225 226 227
          tcoords[0] = s * sScale + sOffset;
          tcoords[1] = t * tScale + tOffset;
          }
        }
228
      newTCoords->SetTuple(i,tcoords);
229 230 231 232 233 234
    }
  //
  // Update ourselves
  //
  output->GetPointData()->CopyTCoordsOff();
  output->GetPointData()->PassData(input->GetPointData());
235

236 237
  output->GetPointData()->SetTCoords(newTCoords);
  newTCoords->Delete();
238 239

  return 1;
240 241
}

242
void vtkProjectedTexture::PrintSelf(ostream& os, vtkIndent indent)
243
{
Brad King's avatar
Brad King committed
244
  this->Superclass::PrintSelf(os,indent);
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259

  os << indent << "S Range: (" << this->SRange[0] << ", "
                               << this->SRange[1] << ")\n";

  os << indent << "T Range: (" << this->TRange[0] << ", "
                               << this->TRange[1] << ")\n";

  os << indent << "Position: (" << this->Position[0] << ", "
                                << this->Position[1] << ", "
                                << this->Position[2] << ")\n";

  os << indent << "Orientation: (" << this->Orientation[0] << ", "
                                << this->Orientation[1] << ", "
                                << this->Orientation[2] << ")\n";

260
  os << indent << "Focal Point: (" << this->FocalPoint[0] << ", "
Bill Lorensen's avatar
Bill Lorensen committed
261 262
                                   << this->FocalPoint[1] << ", "
                                   << this->FocalPoint[2] << ")\n";
263

264 265 266 267 268 269 270 271
  os << indent << "Up: (" << this->Up[0] << ", "
                                << this->Up[1] << ", "
                                << this->Up[2] << ")\n";

  os << indent << "AspectRatio: (" << this->AspectRatio[0] << ", "
                                << this->AspectRatio[1] << ", "
                                << this->AspectRatio[2] << ")\n";

272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
  os << indent << "CameraMode: ";
  if (this->CameraMode == VTK_PROJECTED_TEXTURE_USE_PINHOLE)
  {
    os << "Pinhole\n";
  }
  else if (this->CameraMode == VTK_PROJECTED_TEXTURE_USE_TWO_MIRRORS)
  {
    os << "Two Mirror\n";
  }
  else
  {
    os << "Illegal Mode\n";
  }

  os << indent << "MirrorSeparation: " << this->MirrorSeparation << "\n";
287
}