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 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 222 223 224 225 226 227 228 229 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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkLinearTransform.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.
=========================================================================*/
#include "vtkLinearTransform.h"
#include "vtkDataArray.h"
#include "vtkMath.h"
#include "vtkMatrix4x4.h"
#include "vtkPoints.h"
//------------------------------------------------------------------------
void vtkLinearTransform::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
//------------------------------------------------------------------------
template <class T1, class T2, class T3>
inline void vtkLinearTransformPoint(T1 matrix[4][4],
T2 in[3], T3 out[3])
{
T3 x = static_cast<T3>(
matrix[0][0]*in[0]+matrix[0][1]*in[1]+matrix[0][2]*in[2]+matrix[0][3]);
T3 y = static_cast<T3>(
matrix[1][0]*in[0]+matrix[1][1]*in[1]+matrix[1][2]*in[2]+matrix[1][3]);
T3 z = static_cast<T3>(
matrix[2][0]*in[0]+matrix[2][1]*in[1]+matrix[2][2]*in[2]+matrix[2][3]);
out[0] = x;
out[1] = y;
out[2] = z;
}
//------------------------------------------------------------------------
template <class T1, class T2, class T3, class T4>
inline void vtkLinearTransformDerivative(T1 matrix[4][4],
T2 in[3], T3 out[3],
T4 derivative[3][3])
{
vtkLinearTransformPoint(matrix,in,out);
for (int i = 0; i < 3; i++)
{
derivative[0][i] = static_cast<T4>(matrix[0][i]);
derivative[1][i] = static_cast<T4>(matrix[1][i]);
derivative[2][i] = static_cast<T4>(matrix[2][i]);
}
}
//------------------------------------------------------------------------
template <class T1, class T2, class T3>
inline void vtkLinearTransformVector(T1 matrix[4][4],
T2 in[3], T3 out[3])
{
T3 x = static_cast<T3>(
matrix[0][0]*in[0] + matrix[0][1]*in[1] + matrix[0][2]*in[2]);
T3 y = static_cast<T3>(
matrix[1][0]*in[0] + matrix[1][1]*in[1] + matrix[1][2]*in[2]);
T3 z = static_cast<T3>(
matrix[2][0]*in[0] + matrix[2][1]*in[1] + matrix[2][2]*in[2]);
out[0] = x;
out[1] = y;
out[2] = z;
}
//------------------------------------------------------------------------
template <class T1, class T2, class T3>
inline void vtkLinearTransformNormal(T1 mat[4][4],
T2 in[3], T3 out[3])
{
// to transform the normal, multiply by the transposed inverse matrix
T1 matrix[4][4];
memcpy(*matrix,*mat,16*sizeof(T1));
vtkMatrix4x4::Invert(*matrix,*matrix);
vtkMatrix4x4::Transpose(*matrix,*matrix);
vtkLinearTransformVector(matrix,in,out);
vtkMath::Normalize(out);
}
//------------------------------------------------------------------------
template <class T1, class T2, class T3>
inline void vtkLinearTransformPoints(
T1 matrix[4][4], T2 *in, T3 *out, vtkIdType n)
{
for (vtkIdType i = 0; i < n; i++)
{
vtkLinearTransformPoint(matrix, in, out);
in += 3;
out += 3;
}
}
//------------------------------------------------------------------------
template <class T1, class T2, class T3>
inline void vtkLinearTransformVectors(
T1 matrix[4][4], T2 *in, T3 *out, vtkIdType n)
{
for (vtkIdType i = 0; i < n; i++)
{
vtkLinearTransformVector(matrix, in, out);
in += 3;
out += 3;
}
}
//------------------------------------------------------------------------
template <class T1, class T2, class T3>
inline void vtkLinearTransformNormals(
T1 matrix[4][4], T2 *in, T3 *out, vtkIdType n)
{
for (vtkIdType i = 0; i < n; i++)
{
// matrix has been transposed & inverted, so use TransformVector
vtkLinearTransformVector(matrix, in, out);
vtkMath::Normalize(out);
in += 3;
out += 3;
}
}
//------------------------------------------------------------------------
void vtkLinearTransform::InternalTransformPoint(const float in[3],
float out[3])
{
vtkLinearTransformPoint(this->Matrix->Element,in,out);
}
//------------------------------------------------------------------------
void vtkLinearTransform::InternalTransformPoint(const double in[3],
double out[3])
{
vtkLinearTransformPoint(this->Matrix->Element,in,out);
}
//------------------------------------------------------------------------
void vtkLinearTransform::InternalTransformNormal(const float in[3],
float out[3])
{
vtkLinearTransformNormal(this->Matrix->Element,in,out);
}
//------------------------------------------------------------------------
void vtkLinearTransform::InternalTransformNormal(const double in[3],
double out[3])
{
vtkLinearTransformNormal(this->Matrix->Element,in,out);
}
//------------------------------------------------------------------------
void vtkLinearTransform::InternalTransformVector(const float in[3],
float out[3])
{
vtkLinearTransformVector(this->Matrix->Element,in,out);
}
//------------------------------------------------------------------------
void vtkLinearTransform::InternalTransformVector(const double in[3],
double out[3])
{
vtkLinearTransformVector(this->Matrix->Element,in,out);
}
//----------------------------------------------------------------------------
void vtkLinearTransform::InternalTransformDerivative(const float in[3],
float out[3],
float derivative[3][3])
{
vtkLinearTransformDerivative(this->Matrix->Element,in,out,derivative);
}
//----------------------------------------------------------------------------
void vtkLinearTransform::InternalTransformDerivative(const double in[3],
double out[3],
double derivative[3][3])
{
vtkLinearTransformDerivative(this->Matrix->Element,in,out,derivative);
}
//----------------------------------------------------------------------------
// Transform the normals and vectors using the derivative of the
// transformation. Either inNms or inVrs can be set to NULL.
// Normals are multiplied by the inverse transpose of the transform
// derivative, while vectors are simply multiplied by the derivative.
// Note that the derivative of the inverse transform is simply the
// inverse of the derivative of the forward transform.
void vtkLinearTransform::TransformPointsNormalsVectors(vtkPoints *inPts,
vtkPoints *outPts,
vtkDataArray *inNms,
vtkDataArray *outNms,
vtkDataArray *inVrs,
vtkDataArray *outVrs)
{
this->TransformPoints(inPts, outPts);
if (inNms)
{
this->TransformNormals(inNms, outNms);
}
if (inVrs)
{
this->TransformVectors(inVrs, outVrs);
}
}
//----------------------------------------------------------------------------
void vtkLinearTransform::TransformPoints(vtkPoints *inPts,
vtkPoints *outPts)
{
vtkIdType n = inPts->GetNumberOfPoints();
vtkIdType m = outPts->GetNumberOfPoints();
double (*matrix)[4] = this->Matrix->Element;
this->Update();
// operate directly on the memory to avoid GetPoint()/SetPoint() calls.
vtkDataArray *inArray = inPts->GetData();
vtkDataArray *outArray = outPts->GetData();
int inType = inArray->GetDataType();
int outType = outArray->GetDataType();
void *inPtr = inArray->GetVoidPointer(0);
void *outPtr = outArray->WriteVoidPointer(3*m, 3*n);
if (inType == VTK_FLOAT && outType == VTK_FLOAT)
{
vtkLinearTransformPoints(matrix,
static_cast<float *>(inPtr), static_cast<float *>(outPtr), n);
}
else if (inType == VTK_FLOAT && outType == VTK_DOUBLE)
{
vtkLinearTransformPoints(matrix,
static_cast<float *>(inPtr), static_cast<double *>(outPtr), n);
}
else if (inType == VTK_DOUBLE && outType == VTK_FLOAT)
{
vtkLinearTransformPoints(matrix,
static_cast<double *>(inPtr), static_cast<float *>(outPtr), n);
}
else if (inType == VTK_DOUBLE && outType == VTK_DOUBLE)
{
vtkLinearTransformPoints(matrix,
static_cast<double *>(inPtr), static_cast<double *>(outPtr), n);
}
else
{
double point[3];
for (vtkIdType i = 0; i < n; i++)
{
inPts->GetPoint(i, point);
vtkLinearTransformPoint(matrix, point, point);
outPts->SetPoint(m + i, point);
}
}
}
//----------------------------------------------------------------------------
void vtkLinearTransform::TransformNormals(vtkDataArray *inNms,
vtkDataArray *outNms)
{
vtkIdType n = inNms->GetNumberOfTuples();
vtkIdType m = outNms->GetNumberOfTuples();
double matrix[4][4];
this->Update();
// to transform the normal, multiply by the transposed inverse matrix
vtkMatrix4x4::DeepCopy(*matrix,this->Matrix);
vtkMatrix4x4::Invert(*matrix,*matrix);
vtkMatrix4x4::Transpose(*matrix,*matrix);
// operate directly on the memory to avoid GetTuple()/SetPoint() calls.
int inType = inNms->GetDataType();
int outType = outNms->GetDataType();
void *inPtr = inNms->GetVoidPointer(0);
void *outPtr = outNms->WriteVoidPointer(3*m, 3*n);
if (inType == VTK_FLOAT && outType == VTK_FLOAT)
{
vtkLinearTransformNormals(matrix,
static_cast<float *>(inPtr), static_cast<float *>(outPtr), n);
}
else if (inType == VTK_FLOAT && outType == VTK_DOUBLE)
{
vtkLinearTransformNormals(matrix,
static_cast<float *>(inPtr), static_cast<double *>(outPtr), n);
}
else if (inType == VTK_DOUBLE && outType == VTK_FLOAT)
{
vtkLinearTransformNormals(matrix,
static_cast<double *>(inPtr), static_cast<float *>(outPtr), n);
}
else if (inType == VTK_DOUBLE && outType == VTK_DOUBLE)
{
vtkLinearTransformNormals(matrix,
static_cast<double *>(inPtr), static_cast<double *>(outPtr), n);
}
else
{
for (vtkIdType i = 0; i < n; i++)
{
double norm[3];
inNms->GetTuple(i, norm);
// use TransformVector because matrix is already transposed & inverted
vtkLinearTransformVector(matrix, norm, norm);
vtkMath::Normalize(norm);
outNms->SetTuple(m + i, norm);
}
}
}
//----------------------------------------------------------------------------
void vtkLinearTransform::TransformVectors(vtkDataArray *inVrs,
vtkDataArray *outVrs)
{
vtkIdType n = inVrs->GetNumberOfTuples();
vtkIdType m = outVrs->GetNumberOfTuples();
double (*matrix)[4] = this->Matrix->Element;
this->Update();
// operate directly on the memory to avoid GetTuple()/SetTuple() calls.
int inType = inVrs->GetDataType();
int outType = outVrs->GetDataType();
void *inPtr = inVrs->GetVoidPointer(0);
void *outPtr = outVrs->WriteVoidPointer(3*m, 3*n);
if (inType == VTK_FLOAT && outType == VTK_FLOAT)
{
vtkLinearTransformVectors(matrix,
static_cast<float *>(inPtr), static_cast<float *>(outPtr), n);
}
else if (inType == VTK_FLOAT && outType == VTK_DOUBLE)
{
vtkLinearTransformVectors(matrix,
static_cast<float *>(inPtr), static_cast<double *>(outPtr), n);
}
else if (inType == VTK_DOUBLE && outType == VTK_FLOAT)
{
vtkLinearTransformVectors(matrix,
static_cast<double *>(inPtr), static_cast<float *>(outPtr), n);
}
else if (inType == VTK_DOUBLE && outType == VTK_DOUBLE)
{
vtkLinearTransformVectors(matrix,
static_cast<double *>(inPtr), static_cast<double *>(outPtr), n);
}
else
{
for (vtkIdType i = 0; i < n; i++)
{
double vec[3];
inVrs->GetTuple(i, vec);
vtkLinearTransformVector(matrix, vec, vec);
outVrs->SetTuple(m + i, vec);
}
}
}
|