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 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkPTSReader.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 "vtkPTSReader.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkCellArray.h"
#include "vtkDataArray.h"
#include "vtkDoubleArray.h"
#include "vtkFloatArray.h"
vtkStandardNewMacro(vtkPTSReader);
//----------------------------------------------------------------------------
vtkPTSReader::vtkPTSReader() :
FileName(NULL),
OutputDataTypeIsDouble(false),
LimitReadToBounds(false),
LimitToMaxNumberOfPoints(false),
MaxNumberOfPoints(1000000)
{
this->SetNumberOfInputPorts(0);
this->ReadBounds[0] = this->ReadBounds[2] = this->ReadBounds[4] = VTK_DOUBLE_MAX;
this->ReadBounds[1] = this->ReadBounds[3] = this->ReadBounds[5] = VTK_DOUBLE_MIN;
this->CreateCells = true;
this->IncludeColorAndLuminance = true;
}
//----------------------------------------------------------------------------
vtkPTSReader::~vtkPTSReader()
{
if (this->FileName)
{
delete [] this->FileName;
this->FileName = NULL;
}
}
//-----------------------------------------------------------------------------
// vtkSetStringMacro except we clear some variables if we update the value
void vtkPTSReader::SetFileName(const char *filename)
{
vtkDebugMacro(<< this->GetClassName() << " (" << this << "): setting FileName to " << filename );
if (this->FileName == NULL && filename == NULL)
{
return;
}
if (this->FileName && filename && !strcmp(this->FileName, filename))
{
return;
}
if (this->FileName)
{
delete [] this->FileName;
}
if (filename)
{
size_t n = strlen(filename) + 1;
char *cp1 = new char[n];
const char *cp2 = (filename);
this->FileName = cp1;
do
{
*cp1++ = *cp2++;
} while ( --n );
}
else
{
this->FileName = NULL;
}
this->Modified();
}
//----------------------------------------------------------------------------
int vtkPTSReader::
RequestInformation(vtkInformation *vtkNotUsed(request),
vtkInformationVector **vtkNotUsed(inputVector),
vtkInformationVector *vtkNotUsed(outputVector))
{
if (!this->FileName)
{
vtkErrorMacro("FileName has to be specified!");
return 0;
}
return 1;
}
//-----------------------------------------------------------------------------
void vtkPTSReader::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "File Name: "
<< (this->FileName ? this->FileName : "(none)") << "\n";
os << indent << "OutputDataType = "
<< (this->OutputDataTypeIsDouble ? "double" : "float") << "\n";
os << indent << "CreateCells = "
<< (this->CreateCells ? "yes" : "no") << "\n";
os << indent << "IncludeColorAndLuminance = "
<< (this->IncludeColorAndLuminance ? "yes" : "no") << "\n";
if (this->LimitReadToBounds)
{
os << indent << "LimitReadToBounds = true\n";
os << indent << "ReadBounds = [" << this->ReadBounds[0] << ","<< this->ReadBounds[1] << ","
<< this->ReadBounds[2] << this->ReadBounds[3] << ","<< this->ReadBounds[4] << ","
<< this->ReadBounds[5]<< "]\n";
}
else
{
os << indent << "LimitReadToBounds = false\n";
}
if (this->LimitToMaxNumberOfPoints)
{
os << indent << "LimitToMaxNumberOfPoints = true\n";
os << indent << "MaxNumberOfPoints" << MaxNumberOfPoints << "\n";
}
else
{
os << indent << "LimitToMaxNumberOfPoints = false\n";
}
}
//-----------------------------------------------------------------------------
int vtkPTSReader::
RequestData(vtkInformation *vtkNotUsed(request),
vtkInformationVector **vtkNotUsed(inputVector),
vtkInformationVector *outputVector)
{
// See if we can open in the file
if (!this->FileName)
{
vtkErrorMacro(<<"FileName must be specified.");
return 0;
}
// Open the new file.
vtkDebugMacro(<< "Opening file " << this->FileName);
ifstream file(this->FileName, ios::in | ios::binary);
if (!file || file.fail())
{
vtkErrorMacro(<< "Could not open file " <<
this->FileName);
return 0;
}
this->UpdateProgress(0);
// Determine the number of points to be read in which should be
// a single int at the top of the file
const unsigned int bufferSize = 2048;
std::string buffer;
char junk[bufferSize];
vtkTypeInt32 numPts = -1, tempNumPts;
for (numPts = -1; !file.eof();)
{
getline(file, buffer);
// Scanf should match the interger part but not the string
int numArgs = sscanf(buffer.c_str(), "%d%s", &tempNumPts, junk);
if (numArgs == 1)
{
numPts = static_cast<vtkTypeInt32>(tempNumPts);
break;
}
if (numArgs != -1)
{
// We have a file that doesn't have a number of points line
// Instead we need to count the number of lines in the file
// Remember we already read in the first line hence numPts starts
// at 1
for (numPts = 1; getline(file, buffer); ++numPts)
{
if (numPts%1000000 == 0)
{
this->UpdateProgress(0.1);
if (this->GetAbortExecute())
{
return 0;
}
}
}
file.clear();
file.seekg(0);
break;
}
}
// Next determine the format the point info. Is it x y z,
// x y z intensity or
// or x y z intensity r g b?
int numValuesPerLine;
double irgb[4], pt[3];
if (numPts == -1)
{
vtkErrorMacro(<< "Could not process file " <<
this->FileName << " - Unknown Format");
return 0;
}
else if (numPts == 0)
{
// Trivial case of no points - lets set it to 3
vtkErrorMacro(<< "Could not process file " <<
this->FileName << " - No points specified");
return 0;
}
else
{
getline(file, buffer);
numValuesPerLine = sscanf(buffer.c_str(), "%lf %lf %lf %lf %lf %lf %lf",
pt, pt+1, pt+2,
irgb, irgb+1, irgb+2, irgb+3);
}
if (!((numValuesPerLine == 3) ||
(numValuesPerLine == 4) ||
(numValuesPerLine == 6) ||
(numValuesPerLine == 7)))
{
// Unsupported line format!
vtkErrorMacro(<< "Invalid Pts Format (point info has "
<< numValuesPerLine << ") in the file:"
<< this->FileName);
return 0;
}
// Lets setup the VTK Arrays and Points
// get the info object
vtkInformation *outInfo = outputVector->GetInformationObject(0);
// get the ouptut
vtkPolyData *output = vtkPolyData::SafeDownCast(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
// If we are trying to limit the max number of points calculate the
// onRatio - else set it to 1
double onRatio = 1.0;
vtkTypeInt32 targetNumPts = numPts;
if (this->LimitToMaxNumberOfPoints)
{
onRatio = static_cast<double>(this->MaxNumberOfPoints) / numPts;
targetNumPts = numPts*onRatio + 1;
}
vtkNew<vtkPoints> newPts;
if (this->OutputDataTypeIsDouble)
{
newPts->SetDataTypeToDouble();
}
else
{
newPts->SetDataTypeToFloat();
}
newPts->Allocate(targetNumPts);
vtkNew<vtkUnsignedCharArray> colors;
vtkNew<vtkFloatArray> intensities;
output->SetPoints( newPts.GetPointer() );
vtkNew<vtkCellArray> newVerts;
if (this->CreateCells)
{
output->SetVerts( newVerts.GetPointer() );
}
bool wantIntensities = ((numValuesPerLine == 4) || (numValuesPerLine == 7));
if (numValuesPerLine > 4)
{
colors->SetNumberOfComponents(3);
colors->SetName("Color");
colors->Allocate(targetNumPts*3);
output->GetPointData()->SetScalars( colors.GetPointer());
if (!this->IncludeColorAndLuminance)
{
wantIntensities = false;
}
}
if (wantIntensities)
{
intensities->SetName("Intensities");
intensities->SetNumberOfComponents(1);
intensities->Allocate(targetNumPts);
output->GetPointData()->AddArray( intensities.GetPointer());
}
if (numPts == 0)
{
// we are done
return 1;
}
this->UpdateProgress( 0.2 );
if (this->GetAbortExecute())
{
this->UpdateProgress( 1.0 );
return 1;
}
// setup the ReadBBox, IF we're limiting the read to specifed ReadBounds
if (this->LimitReadToBounds)
{
this->ReadBBox.Reset();
this->ReadBBox.SetMinPoint(this->ReadBounds[0], this->ReadBounds[2],
this->ReadBounds[4]);
this->ReadBBox.SetMaxPoint(this->ReadBounds[1], this->ReadBounds[3],
this->ReadBounds[5]);
// the ReadBBox is guaranteed to be "valid", regardless of the whether
// ReadBounds is valid. If any of the MonPoint values are greater than
// the corresponding MaxPoint, the MinPoint component will be set to be
// the same as the MaxPoint during the SetMaxPoint fn call.
}
// Lets Process the points! Remember that we have already loaded in
// the first line of points in the buffer
vtkIdType *pids = 0;
vtkIdType pid;
if (this->CreateCells)
{
pids = new vtkIdType[targetNumPts];
}
long lastCount = 0;
for (long i = 0; i < numPts; i++)
{
// Should we process this point? Meaning that we skipped the appropriate number of points
// based on the Max Number of points (onRatio) or the filtering by the read bounding box
// OK to process based on Max Number of Points
if (floor(i*onRatio) > lastCount)
{
lastCount++;
sscanf(buffer.c_str(), "%lf %lf %lf %lf %lf %lf %lf",
pt, pt+1, pt+2,
irgb, irgb+1, irgb+2, irgb+3);
// OK to process based on bounding box
if ((!this->LimitReadToBounds) || this->ReadBBox.ContainsPoint(pt))
{
pid = newPts->InsertNextPoint(pt);
//std::cerr << "Point " << i << " : " << pt[0] << " " << pt[1] << " " << pt[2] << "\n";
if (this->CreateCells)
{
pids[pid] = pid;
}
if (wantIntensities)
{
intensities->InsertNextValue(irgb[0]);
}
if (numValuesPerLine > 4)
{
// if we have intensity then the color info starts with the second value in the array
// else it starts with the first
if(wantIntensities)
{
colors->InsertNextTuple(irgb+1);
}
else
{
colors->InsertNextTuple(irgb);
}
}
}
}
if (file.eof())
{
break;
}
if (i%1000000 == 0)
{
this->UpdateProgress(0.2 + (0.75*i)/numPts);
if (this->GetAbortExecute())
{
return 0;
}
}
getline(file, buffer);
}
// Do we have to squeeze any of the arrays?
if (newPts->GetNumberOfPoints() < targetNumPts)
{
newPts->Squeeze();
if (wantIntensities)
{
intensities->Squeeze();
}
if (numValuesPerLine > 4)
{
colors->Squeeze();
}
}
if (this->CreateCells)
{
newVerts->InsertNextCell(newPts->GetNumberOfPoints(), pids);
delete [] pids;
}
this->UpdateProgress(1.0);
return 1;
}
|