vtkDataR.cc 27.2 KB
Newer Older
Will Schroeder's avatar
Will Schroeder committed
1
2
/*=========================================================================

Ken Martin's avatar
Ken Martin committed
3
4
  Program:   Visualization Toolkit
  Module:    vtkDataR.cc
Will Schroeder's avatar
Will Schroeder committed
5
6
7
8
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

Ken Martin's avatar
Ken Martin committed
9
This file is part of the Visualization Toolkit. No part of this file
Will Schroeder's avatar
Will Schroeder committed
10
11
12
13
14
15
or its contents may be copied, reproduced or altered in any way
without the express written consent of the authors.

Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen 1993, 1994 

=========================================================================*/
Ken Martin's avatar
Ken Martin committed
16
#include "vtkDataR.hh"
Will Schroeder's avatar
Will Schroeder committed
17
#include <ctype.h>
Will Schroeder's avatar
Will Schroeder committed
18

Will Schroeder's avatar
Will Schroeder committed
19
#include "BScalars.hh"
20
#include "UCScalar.hh"
Will Schroeder's avatar
Will Schroeder committed
21
22
#include "FScalars.hh"
#include "SScalars.hh"
Will Schroeder's avatar
Will Schroeder committed
23
#include "IScalars.hh"
Will Schroeder's avatar
Will Schroeder committed
24
25
26
27
28
29
30
31
32
33
#include "FPoints.hh"
#include "IPoints.hh"
#include "FNormals.hh"
#include "FTensors.hh"
#include "FTCoords.hh"
#include "Graymap.hh"
#include "AGraymap.hh"
#include "Pixmap.hh"
#include "APixmap.hh"
#include "Lut.hh"
Will Schroeder's avatar
Will Schroeder committed
34

Will Schroeder's avatar
Will Schroeder committed
35
36
// Description:
// Construct object.
Ken Martin's avatar
Ken Martin committed
37
vtkDataReader::vtkDataReader()
Will Schroeder's avatar
Will Schroeder committed
38
{
39
  this->Filename = NULL;
Will Schroeder's avatar
Will Schroeder committed
40
41
42
43
44
45
46
47
48
  this->ScalarsName = NULL;
  this->VectorsName = NULL;
  this->TensorsName = NULL;
  this->NormalsName = NULL;
  this->TCoordsName = NULL;
  this->LookupTableName = NULL;
  this->ScalarLut = NULL;
}  

Ken Martin's avatar
Ken Martin committed
49
vtkDataReader::~vtkDataReader()
Will Schroeder's avatar
Will Schroeder committed
50
{
51
  if (this->Filename) delete [] this->Filename;
Will Schroeder's avatar
Will Schroeder committed
52
53
54
55
56
57
58
59
60
  if (this->ScalarsName) delete [] this->ScalarsName;
  if (this->VectorsName) delete [] this->VectorsName;
  if (this->TensorsName) delete [] this->TensorsName;
  if (this->NormalsName) delete [] this->NormalsName;
  if (this->TCoordsName) delete [] this->TCoordsName;
  if (this->LookupTableName) delete [] this->LookupTableName;
  if (this->ScalarLut) delete [] this->ScalarLut;
}

Will Schroeder's avatar
Will Schroeder committed
61
// Description:
Ken Martin's avatar
Ken Martin committed
62
// Open a vtk data file. Returns NULL if error.
Ken Martin's avatar
Ken Martin committed
63
FILE *vtkDataReader::OpenVTKFile()
Will Schroeder's avatar
Will Schroeder committed
64
{
65
  FILE *fptr;
Will Schroeder's avatar
Will Schroeder committed
66

Ken Martin's avatar
Ken Martin committed
67
  vtkDebugMacro(<< "Opening vtk file");
Will Schroeder's avatar
Will Schroeder committed
68

69
  if ( !this->Filename )
Will Schroeder's avatar
Will Schroeder committed
70
    {
Ken Martin's avatar
Ken Martin committed
71
    vtkErrorMacro(<< "No file specified!");
Will Schroeder's avatar
Will Schroeder committed
72
    return NULL;
73
74
75
76
    }

  if ( (fptr=fopen(this->Filename, "rb")) == NULL )
    {
Ken Martin's avatar
Ken Martin committed
77
    vtkErrorMacro(<< "Unable to open file: "<< this->Filename);
78
    return NULL;
Will Schroeder's avatar
Will Schroeder committed
79
80
81
    }

  return fptr;
Will Schroeder's avatar
Will Schroeder committed
82
83
84
}

// Description:
Ken Martin's avatar
Ken Martin committed
85
86
// Read the header of a vtk data file. Returns 0 if error.
int vtkDataReader::ReadHeader(FILE *fp)
Will Schroeder's avatar
Will Schroeder committed
87
{
Will Schroeder's avatar
Will Schroeder committed
88
89
90
  char line[257];
  int retStat;

Ken Martin's avatar
Ken Martin committed
91
  vtkDebugMacro(<< "Reading vtk file header");
Will Schroeder's avatar
Will Schroeder committed
92
93
94
//
// read header
//
Will Schroeder's avatar
Will Schroeder committed
95
96
  if ( fgets (line, 256, fp) == NULL )
    {
Ken Martin's avatar
Ken Martin committed
97
    vtkErrorMacro(<<"Premature EOF reading first line!");
Will Schroeder's avatar
Will Schroeder committed
98
99
    return 0;
    }
100
  if ( strncmp ("# vtk DataFile Version", line, 20) )
Will Schroeder's avatar
Will Schroeder committed
101
    {
102
    vtkErrorMacro(<< "Unrecognized file type: "<< line);
Will Schroeder's avatar
Will Schroeder committed
103
104
105
106
107
    return 0;
    }
//
// read title
//
Will Schroeder's avatar
Will Schroeder committed
108
109
  if ( fgets (line, 256, fp) == NULL )
    {
Ken Martin's avatar
Ken Martin committed
110
    vtkErrorMacro(<<"Premature EOF reading title!");
Will Schroeder's avatar
Will Schroeder committed
111
112
    return 0;
    }
Will Schroeder's avatar
Will Schroeder committed
113
  line[256] = '\0';
Ken Martin's avatar
Ken Martin committed
114
  vtkDebugMacro(<< "Reading vtk file entitled: " << line);
Will Schroeder's avatar
Will Schroeder committed
115
116
117
//
// read type
//
Will Schroeder's avatar
Will Schroeder committed
118
  if ( (retStat=fscanf(fp,"%256s",line)) == EOF || retStat < 1 ) 
Will Schroeder's avatar
Will Schroeder committed
119
    {
Ken Martin's avatar
Ken Martin committed
120
    vtkErrorMacro(<<"Premature EOF reading file type!");
Will Schroeder's avatar
Will Schroeder committed
121
122
    return 0;
    }
Will Schroeder's avatar
Will Schroeder committed
123
124
125
126
127

  if ( !strncmp(this->LowerCase(line),"ascii",5) ) this->FileType = ASCII;
  else if ( !strncmp(line,"binary",6) ) this->FileType = BINARY;
  else
    {
Ken Martin's avatar
Ken Martin committed
128
    vtkErrorMacro(<< "Unrecognized file type: "<< line);
Will Schroeder's avatar
Will Schroeder committed
129
    this->FileType = NULL;
Will Schroeder's avatar
Will Schroeder committed
130
131
132
    return 0;
    }

Will Schroeder's avatar
Will Schroeder committed
133
134
135
136
  return 1;
}

// Description:
Ken Martin's avatar
Ken Martin committed
137
// Read the point data of a vtk data file. The number of points (from the 
Will Schroeder's avatar
Will Schroeder committed
138
139
// dataset) must match the number of points defined in point attributes (unless
// no geometry was defined).
Ken Martin's avatar
Ken Martin committed
140
int vtkDataReader::ReadPointData(FILE *fp, vtkDataSet *ds, int numPts)
Will Schroeder's avatar
Will Schroeder committed
141
{
Will Schroeder's avatar
Will Schroeder committed
142
143
144
  int retStat;
  char line[257];

Ken Martin's avatar
Ken Martin committed
145
  vtkDebugMacro(<< "Reading vtk point data");
Will Schroeder's avatar
Will Schroeder committed
146
147
148
//
// Read keywords until end-of-file
//
Will Schroeder's avatar
Will Schroeder committed
149
  while ( (retStat=fscanf(fp, "%256s", line)) != EOF && retStat == 1 ) 
Will Schroeder's avatar
Will Schroeder committed
150
151
152
153
154
155
    {
//
// read scalar data
//
    if ( ! strncmp(this->LowerCase(line), "scalars", 7) )
      {
Will Schroeder's avatar
Will Schroeder committed
156
      if ( ! this->ReadScalarData(fp, ds, numPts) ) return 0;
Will Schroeder's avatar
Will Schroeder committed
157
158
159
160
161
162
      }
//
// read vector data
//
    else if ( ! strncmp(line, "vectors", 7) )
      {
Will Schroeder's avatar
Will Schroeder committed
163
      if ( ! this->ReadVectorData(fp, ds, numPts) ) return 0;
Will Schroeder's avatar
Will Schroeder committed
164
165
166
167
168
169
      }
//
// read 3x3 tensor data
//
    else if ( ! strncmp(line, "tensors", 7) )
      {
Will Schroeder's avatar
Will Schroeder committed
170
      if ( ! this->ReadTensorData(fp, ds, numPts) ) return 0;
Will Schroeder's avatar
Will Schroeder committed
171
172
173
174
175
176
      }
//
// read normals data
//
    else if ( ! strncmp(line, "normals", 7) )
      {
Will Schroeder's avatar
Will Schroeder committed
177
      if ( ! this->ReadNormalData(fp, ds, numPts) ) return 0;
Will Schroeder's avatar
Will Schroeder committed
178
179
180
181
182
183
      }
//
// read texture coordinates data
//
    else if ( ! strncmp(line, "texture_coordinates", 19) )
      {
Will Schroeder's avatar
Will Schroeder committed
184
      if ( ! this->ReadTCoordsData(fp, ds, numPts) ) return 0;
Will Schroeder's avatar
Will Schroeder committed
185
186
187
188
189
190
      }
//
// read color scalars data
//
    else if ( ! strncmp(line, "color_scalars", 13) )
      {
Will Schroeder's avatar
Will Schroeder committed
191
      if ( ! this->ReadCoScalarData(fp, ds, numPts) ) return 0;
Will Schroeder's avatar
Will Schroeder committed
192
193
194
195
196
197
      }
//
// read lookup table. Associate with scalar data.
//
    else if ( ! strncmp(line, "lookup_table", 12) )
      {
Will Schroeder's avatar
Will Schroeder committed
198
      if ( ! this->ReadLutData(fp, ds, numPts) ) return 0;
Will Schroeder's avatar
Will Schroeder committed
199
200
201
202
      }

    else
      {
Ken Martin's avatar
Ken Martin committed
203
      vtkErrorMacro(<< "Unsupported point attribute type: " << line);
Will Schroeder's avatar
Will Schroeder committed
204
205
206
207
      return 0;
      }
    }

Will Schroeder's avatar
Will Schroeder committed
208
  return 1;
Will Schroeder's avatar
Will Schroeder committed
209
210
211
212
}

// Description:
// Read point coordinates. Return 0 if error.
Ken Martin's avatar
Ken Martin committed
213
int vtkDataReader::ReadPoints(FILE *fp, vtkPointSet *ps, int numPts)
Will Schroeder's avatar
Will Schroeder committed
214
215
216
217
{
  int retStat, i;
  char line[257];

Will Schroeder's avatar
Will Schroeder committed
218
  if ((retStat=fscanf(fp, "%256s", line)) ==  EOF || retStat < 1) 
Will Schroeder's avatar
Will Schroeder committed
219
    {
Ken Martin's avatar
Ken Martin committed
220
    vtkErrorMacro(<<"Cannot read points type!");
Will Schroeder's avatar
Will Schroeder committed
221
222
    return 0;
    }
Will Schroeder's avatar
Will Schroeder committed
223
224
225

  if ( ! strncmp(this->LowerCase(line), "int", 3) )
    {
Ken Martin's avatar
Ken Martin committed
226
    vtkIntPoints *points = new vtkIntPoints(numPts);
Will Schroeder's avatar
Will Schroeder committed
227
228
229
    int *ptr = points->WritePtr(0,numPts);
    if ( this->FileType == BINARY)
      {
Will Schroeder's avatar
Will Schroeder committed
230
231
232
      if ( (fgets(line,256,fp) == NULL) || //suck up newline
      (fread(ptr,sizeof(int),3*numPts,fp) != (3*numPts)) )
        {
Ken Martin's avatar
Ken Martin committed
233
        vtkErrorMacro(<<"Error reading binary points!");
Will Schroeder's avatar
Will Schroeder committed
234
235
236
        return 0;
        }
      points->WrotePtr();
Will Schroeder's avatar
Will Schroeder committed
237
238
239
240
241
242
      }
    else // ascii
      {
      int p[3];
      for (i=0; i<numPts; i++)
        {
Will Schroeder's avatar
Will Schroeder committed
243
244
        if ((retStat=fscanf(fp,"%d %d %d",p,p+1,p+2)) == EOF || retStat < 3)
          {
Ken Martin's avatar
Ken Martin committed
245
          vtkErrorMacro(<<"Error reading points!");
Will Schroeder's avatar
Will Schroeder committed
246
247
          return 0;
          }
Will Schroeder's avatar
Will Schroeder committed
248
249
250
251
        points->SetPoint(i,p);
        }
      }
    ps->SetPoints(points);
Will Schroeder's avatar
Will Schroeder committed
252
    points->Delete();
Will Schroeder's avatar
Will Schroeder committed
253
254
255
256
    }

  else if ( ! strncmp(line, "float", 5) )
    {
Ken Martin's avatar
Ken Martin committed
257
    vtkFloatPoints *points = new vtkFloatPoints(numPts);
Will Schroeder's avatar
Will Schroeder committed
258
259
260
    float *ptr = points->WritePtr(0,numPts);
    if ( this->FileType == BINARY)
      {
Will Schroeder's avatar
Will Schroeder committed
261
262
263
      if ( (fgets(line,256,fp) == NULL) || //suck up newline
      (fread(ptr,sizeof(float),3*numPts,fp) != (3*numPts)) )
        {
Ken Martin's avatar
Ken Martin committed
264
        vtkErrorMacro(<<"Error reading binary points!");
Will Schroeder's avatar
Will Schroeder committed
265
266
267
        return 0;
        }
      points->WrotePtr();
Will Schroeder's avatar
Will Schroeder committed
268
269
270
271
272
273
      }
    else // ascii
      {
      float p[3];
      for (i=0; i<numPts; i++)
        {
Will Schroeder's avatar
Will Schroeder committed
274
275
        if ((retStat=fscanf(fp,"%f %f %f",p,p+1,p+2)) == EOF || retStat < 3)
          {
Ken Martin's avatar
Ken Martin committed
276
          vtkErrorMacro(<<"Error reading points!");
Will Schroeder's avatar
Will Schroeder committed
277
278
          return 0;
          }
Will Schroeder's avatar
Will Schroeder committed
279
280
281
282
        points->SetPoint(i,p);
        }
      }
    ps->SetPoints(points);
Will Schroeder's avatar
Will Schroeder committed
283
    points->Delete();
Will Schroeder's avatar
Will Schroeder committed
284
285
286
    }

  else 
Will Schroeder's avatar
Will Schroeder committed
287
    {
Ken Martin's avatar
Ken Martin committed
288
    vtkErrorMacro(<< "Unsupported points type: " << line);
Will Schroeder's avatar
Will Schroeder committed
289
    return 0;
Will Schroeder's avatar
Will Schroeder committed
290
291
    }

Ken Martin's avatar
Ken Martin committed
292
  vtkDebugMacro(<<"Read " << ps->GetNumberOfPoints() << " points");
Will Schroeder's avatar
Will Schroeder committed
293
  return 1;
Will Schroeder's avatar
Will Schroeder committed
294
295
296
297
}

// Description:
// Read scalar point attributes. Return 0 if error.
Ken Martin's avatar
Ken Martin committed
298
int vtkDataReader::ReadScalarData(FILE *fp, vtkDataSet *ds, int numPts)
Will Schroeder's avatar
Will Schroeder committed
299
{
Will Schroeder's avatar
Will Schroeder committed
300
301
302
303
304
  char line[257], name[257], key[128], tableName[257];
  int retStat, i, skipScalar=0;

  if ( (retStat=fscanf(fp, "%256s %256s", name, line)) == EOF || retStat < 2 ||
  ((retStat=fscanf(fp, "%256s %256s", key, tableName)) == EOF || retStat < 2) )
Will Schroeder's avatar
Will Schroeder committed
305
    {
Ken Martin's avatar
Ken Martin committed
306
    vtkErrorMacro(<<"Cannot read scalar header!");
Will Schroeder's avatar
Will Schroeder committed
307
308
    return 0;
    }
Will Schroeder's avatar
Will Schroeder committed
309
310

  if ( strcmp(this->LowerCase(key), "lookup_table") )
Will Schroeder's avatar
Will Schroeder committed
311
    {
Ken Martin's avatar
Ken Martin committed
312
    vtkErrorMacro(<<"Lookup table must be specified with scalar.\n" <<
Will Schroeder's avatar
Will Schroeder committed
313
314
315
                   "Use \"LOOKUP_TABLE default\" to use default table.");
    return 0;
    }
Will Schroeder's avatar
Will Schroeder committed
316
317
318
319
320
321
322
323
324
325
  //
  // See whether scalar has been already read or scalar name (if specified) 
  // matches name in file. 
  //
  if ( ds->GetPointData()->GetScalars() != NULL || 
  (this->ScalarsName && strcmp(name,this->ScalarsName)) )
    skipScalar = 1;
  else
    this->SetScalarLut(tableName); //may be "default"

Will Schroeder's avatar
Will Schroeder committed
326
327
328

  if ( ! strncmp(this->LowerCase(line), "bit", 3) )
    {
Ken Martin's avatar
Ken Martin committed
329
    vtkBitScalars *scalars = new vtkBitScalars(numPts);
Will Schroeder's avatar
Will Schroeder committed
330
331
332
    unsigned char *ptr = scalars->WritePtr(0,numPts);
    if ( this->FileType == BINARY)
      {
Will Schroeder's avatar
Will Schroeder committed
333
334
335
      if ( (fgets(line,256,fp) == NULL) || //suck up newline
      (fread(ptr,sizeof(unsigned char),(numPts+1)/8,fp) != ((numPts+1)/8)) )
        {
Ken Martin's avatar
Ken Martin committed
336
        vtkErrorMacro(<<"Error reading binary bit scalars!");
Will Schroeder's avatar
Will Schroeder committed
337
338
        return 0;
        }
Will Schroeder's avatar
Will Schroeder committed
339
      scalars->WrotePtr();
Will Schroeder's avatar
Will Schroeder committed
340
341
342
343
344
345
      }
    else // ascii
      {
      int iv;
      for (i=0; i<numPts; i++)
        {
Will Schroeder's avatar
Will Schroeder committed
346
347
        if ((retStat=fscanf(fp,"%d",&iv)) == EOF || retStat < 1)
          {
Ken Martin's avatar
Ken Martin committed
348
          vtkErrorMacro(<<"Error reading bit scalars!");
Will Schroeder's avatar
Will Schroeder committed
349
350
          return 0;
          }
Will Schroeder's avatar
Will Schroeder committed
351
352
353
        scalars->SetScalar(i,iv);
        }
      }
Will Schroeder's avatar
Will Schroeder committed
354
355
    if ( ! skipScalar ) ds->GetPointData()->SetScalars(scalars);
    scalars->Delete();
Will Schroeder's avatar
Will Schroeder committed
356
357
    }

358
  else if ( ! strncmp(line, "unsigned char", 13) )
Will Schroeder's avatar
Will Schroeder committed
359
    {
360
    vtkUnsignedCharScalars *scalars = new vtkUnsignedCharScalars(numPts);
Will Schroeder's avatar
Will Schroeder committed
361
362
363
    unsigned char *ptr = scalars->WritePtr(0,numPts);
    if ( this->FileType == BINARY)
      {
Will Schroeder's avatar
Will Schroeder committed
364
365
366
      if ( (fgets(line,256,fp) == NULL) || //suck up newline
      (fread(ptr,sizeof(unsigned char),numPts,fp) != numPts) )
        {
Ken Martin's avatar
Ken Martin committed
367
        vtkErrorMacro(<<"Error reading binary char scalars!");
Will Schroeder's avatar
Will Schroeder committed
368
369
        return 0;
        }
Will Schroeder's avatar
Will Schroeder committed
370
      scalars->WrotePtr();
Will Schroeder's avatar
Will Schroeder committed
371
372
373
374
375
376
      }
    else // ascii
      {
      unsigned char c;
      for (i=0; i<numPts; i++)
        {
Will Schroeder's avatar
Will Schroeder committed
377
378
        if ((retStat=fscanf(fp,"%c",&c)) == EOF || retStat < 1)
          {
Ken Martin's avatar
Ken Martin committed
379
          vtkErrorMacro(<<"Error reading char scalars!");
Will Schroeder's avatar
Will Schroeder committed
380
381
          return 0;
          }
Will Schroeder's avatar
Will Schroeder committed
382
383
384
        scalars->SetScalar(i,c);
        }
      }
Will Schroeder's avatar
Will Schroeder committed
385
386
    if ( ! skipScalar ) ds->GetPointData()->SetScalars(scalars);
    scalars->Delete();
Will Schroeder's avatar
Will Schroeder committed
387
388
389
390
    }

  else if ( ! strncmp(line, "short", 5) )
    {
Ken Martin's avatar
Ken Martin committed
391
    vtkShortScalars *scalars = new vtkShortScalars(numPts);
Will Schroeder's avatar
Will Schroeder committed
392
393
394
    short *ptr = scalars->WritePtr(0,numPts);
    if ( this->FileType == BINARY)
      {
Will Schroeder's avatar
Will Schroeder committed
395
396
397
      if ( (fgets(line,256,fp) == NULL) || //suck up newline
      (fread(ptr,sizeof(short),numPts,fp) != numPts) )
        {
Ken Martin's avatar
Ken Martin committed
398
        vtkErrorMacro(<<"Error reading binary short scalars!");
Will Schroeder's avatar
Will Schroeder committed
399
400
        return 0;
        }
Will Schroeder's avatar
Will Schroeder committed
401
      scalars->WrotePtr();
Will Schroeder's avatar
Will Schroeder committed
402
403
404
405
406
407
      }
    else // ascii
      {
      short s;
      for (i=0; i<numPts; i++)
        {
Will Schroeder's avatar
Will Schroeder committed
408
409
        if ((retStat=fscanf(fp,"%hd",&s)) == EOF || retStat < 1)
          {
Ken Martin's avatar
Ken Martin committed
410
          vtkErrorMacro(<<"Error reading short scalars!");
Will Schroeder's avatar
Will Schroeder committed
411
412
          return 0;
          }
Will Schroeder's avatar
Will Schroeder committed
413
414
415
        scalars->SetScalar(i,s);
        }
      }
Will Schroeder's avatar
Will Schroeder committed
416
417
    if ( ! skipScalar ) ds->GetPointData()->SetScalars(scalars);
    scalars->Delete();
Will Schroeder's avatar
Will Schroeder committed
418
419
420
421
    }

  else if ( ! strncmp(line, "int", 3) )
    {
Ken Martin's avatar
Ken Martin committed
422
    vtkIntScalars *scalars = new vtkIntScalars(numPts);
Will Schroeder's avatar
Will Schroeder committed
423
424
425
    int *ptr = scalars->WritePtr(0,numPts);
    if ( this->FileType == BINARY)
      {
Will Schroeder's avatar
Will Schroeder committed
426
427
428
      if ( (fgets(line,256,fp) == NULL) || //suck up newline
      (fread(ptr,sizeof(int),numPts,fp) != numPts) )
        {
Ken Martin's avatar
Ken Martin committed
429
        vtkErrorMacro(<<"Error reading binary int scalars!");
Will Schroeder's avatar
Will Schroeder committed
430
431
        return 0;
        }
Will Schroeder's avatar
Will Schroeder committed
432
      scalars->WrotePtr();
Will Schroeder's avatar
Will Schroeder committed
433
434
435
436
437
438
      }
    else // ascii
      {
      int iv;
      for (i=0; i<numPts; i++)
        {
Will Schroeder's avatar
Will Schroeder committed
439
440
        if ((retStat=fscanf(fp,"%d",&iv)) == EOF || retStat < 1)
          {
Ken Martin's avatar
Ken Martin committed
441
          vtkErrorMacro(<<"Error reading int scalars!");
Will Schroeder's avatar
Will Schroeder committed
442
443
          return 0;
          }
Will Schroeder's avatar
Will Schroeder committed
444
445
446
        scalars->SetScalar(i,iv);
        }
      }
Will Schroeder's avatar
Will Schroeder committed
447
448
    if ( ! skipScalar ) ds->GetPointData()->SetScalars(scalars);
    scalars->Delete();
Will Schroeder's avatar
Will Schroeder committed
449
450
451
452
    }

  else if ( ! strncmp(line, "float", 5) )
    {
Ken Martin's avatar
Ken Martin committed
453
    vtkFloatScalars *scalars = new vtkFloatScalars(numPts);
Will Schroeder's avatar
Will Schroeder committed
454
455
456
    float *ptr = scalars->WritePtr(0,numPts);
    if ( this->FileType == BINARY)
      {
Will Schroeder's avatar
Will Schroeder committed
457
458
459
      if ( (fgets(line,256,fp) == NULL) || //suck up newline
      (fread(ptr,sizeof(int),numPts,fp) != numPts) )
        {
Ken Martin's avatar
Ken Martin committed
460
        vtkErrorMacro(<<"Error reading binary int scalars!");
Will Schroeder's avatar
Will Schroeder committed
461
462
        return 0;
        }
Will Schroeder's avatar
Will Schroeder committed
463
      scalars->WrotePtr();
Will Schroeder's avatar
Will Schroeder committed
464
465
466
467
468
469
      }
    else // ascii
      {
      float f;
      for (i=0; i<numPts; i++)
        {
Will Schroeder's avatar
Will Schroeder committed
470
471
        if ((retStat=fscanf(fp,"%f",&f)) == EOF || retStat < 1)
          {
Ken Martin's avatar
Ken Martin committed
472
          vtkErrorMacro(<<"Error reading float scalars!");
Will Schroeder's avatar
Will Schroeder committed
473
474
          return 0;
          }
Will Schroeder's avatar
Will Schroeder committed
475
476
477
        scalars->SetScalar(i,f);
        }
      }
Will Schroeder's avatar
Will Schroeder committed
478
479
    if ( ! skipScalar ) ds->GetPointData()->SetScalars(scalars);
    scalars->Delete();
Will Schroeder's avatar
Will Schroeder committed
480
481
482
    }

  else 
Will Schroeder's avatar
Will Schroeder committed
483
    {
Ken Martin's avatar
Ken Martin committed
484
    vtkErrorMacro(<< "Unsupported scalar data type: " << line);
Will Schroeder's avatar
Will Schroeder committed
485
    return 0;
Will Schroeder's avatar
Will Schroeder committed
486
487
488
    }

  return 1;
Will Schroeder's avatar
Will Schroeder committed
489
490
491
492
}

// Description:
// Read vector point attributes. Return 0 if error.
Ken Martin's avatar
Ken Martin committed
493
int vtkDataReader::ReadVectorData(FILE *fp, vtkDataSet *ds, int numPts)
Will Schroeder's avatar
Will Schroeder committed
494
{
Will Schroeder's avatar
Will Schroeder committed
495
496
497
498
  int retStat, i, skipVector=0;
  char line[257], name[257];

  if ((retStat=fscanf(fp, "%256s %256s", name, line)) == EOF || retStat < 2) 
Will Schroeder's avatar
Will Schroeder committed
499
    {
Ken Martin's avatar
Ken Martin committed
500
    vtkErrorMacro(<<"Cannot read vector data!");
Will Schroeder's avatar
Will Schroeder committed
501
502
    return 0;
    }
Will Schroeder's avatar
Will Schroeder committed
503
504
505
506
507
508
509
510
511
512

  //
  // See whether vector has been already read or vector name (if specified) 
  // matches name in file. 
  //
  if ( ds->GetPointData()->GetVectors() != NULL || 
  (this->VectorsName && strcmp(name,this->VectorsName)) )
    {
    skipVector = 1;
    }
Will Schroeder's avatar
Will Schroeder committed
513
514
515

  if ( ! strncmp(this->LowerCase(line), "float", 5) )
    {
Ken Martin's avatar
Ken Martin committed
516
    vtkFloatVectors *vectors = new vtkFloatVectors(numPts);
Will Schroeder's avatar
Will Schroeder committed
517
518
519
    float *ptr = vectors->WritePtr(0,numPts);
    if ( this->FileType == BINARY)
      {
Will Schroeder's avatar
Will Schroeder committed
520
521
522
      if ( (fgets(line,256,fp) == NULL) || //suck up newline
      (fread(ptr,sizeof(float),3*numPts,fp) != (3*numPts)) )
        {
Ken Martin's avatar
Ken Martin committed
523
        vtkErrorMacro(<<"Error reading binary vectors!");
Will Schroeder's avatar
Will Schroeder committed
524
525
        return 0;
        }
Will Schroeder's avatar
Will Schroeder committed
526
      vectors->WrotePtr();
Will Schroeder's avatar
Will Schroeder committed
527
528
529
530
531
532
      }
    else // ascii
      {
      float v[3];
      for (i=0; i<numPts; i++)
        {
Will Schroeder's avatar
Will Schroeder committed
533
534
        if ((retStat=fscanf(fp,"%f %f %f",v,v+1,v+2)) == EOF || retStat < 3)
          {
Ken Martin's avatar
Ken Martin committed
535
          vtkErrorMacro(<<"Error reading vectors!");
Will Schroeder's avatar
Will Schroeder committed
536
537
          return 0;
          }
Will Schroeder's avatar
Will Schroeder committed
538
539
540
        vectors->SetVector(i,v);
        }
      }
Will Schroeder's avatar
Will Schroeder committed
541
542
    if ( ! skipVector ) ds->GetPointData()->SetVectors(vectors);
    vectors->Delete();
Will Schroeder's avatar
Will Schroeder committed
543
544
545
    }

  else 
Will Schroeder's avatar
Will Schroeder committed
546
    {
Ken Martin's avatar
Ken Martin committed
547
    vtkErrorMacro(<< "Unsupported vector type: " << line);
Will Schroeder's avatar
Will Schroeder committed
548
    return 0;
Will Schroeder's avatar
Will Schroeder committed
549
    }
Will Schroeder's avatar
Will Schroeder committed
550

Will Schroeder's avatar
Will Schroeder committed
551
  return 1;
Will Schroeder's avatar
Will Schroeder committed
552
553
554
555
}

// Description:
// Read normal point attributes. Return 0 if error.
Ken Martin's avatar
Ken Martin committed
556
int vtkDataReader::ReadNormalData(FILE *fp, vtkDataSet *ds, int numPts)
Will Schroeder's avatar
Will Schroeder committed
557
{
Will Schroeder's avatar
Will Schroeder committed
558
559
560
561
  int retStat, i, skipNormal;
  char line[257], name[257];

  if ((retStat=fscanf(fp, "%256s %256s", name, line)) == EOF || retStat < 2) 
Will Schroeder's avatar
Will Schroeder committed
562
    {
Ken Martin's avatar
Ken Martin committed
563
    vtkErrorMacro(<<"Cannot read normal data!");
Will Schroeder's avatar
Will Schroeder committed
564
565
    return 0;
    }
Will Schroeder's avatar
Will Schroeder committed
566
567
568
569
570
571
572
573
574
  //
  // See whether normal has been already read or normal name (if specified) 
  // matches name in file. 
  //
  if ( ds->GetPointData()->GetNormals() != NULL || 
  (this->NormalsName && strcmp(name,this->NormalsName)) )
    {
    skipNormal = 1;
    }
Will Schroeder's avatar
Will Schroeder committed
575
576
577

  if ( ! strncmp(this->LowerCase(line), "float", 5) )
    {
Ken Martin's avatar
Ken Martin committed
578
    vtkFloatNormals *normals = new vtkFloatNormals(numPts);
Will Schroeder's avatar
Will Schroeder committed
579
580
581
    float *ptr = normals->WritePtr(0,numPts);
    if ( this->FileType == BINARY)
      {
Will Schroeder's avatar
Will Schroeder committed
582
583
584
      if ( (fgets(line,256,fp) == NULL) || //suck up newline
      (fread(ptr,sizeof(float),3*numPts,fp) != (3*numPts)) )
        {
Ken Martin's avatar
Ken Martin committed
585
        vtkErrorMacro(<<"Error reading binary normals!");
Will Schroeder's avatar
Will Schroeder committed
586
587
        return 0;
        }
Will Schroeder's avatar
Will Schroeder committed
588
      normals->WrotePtr();
Will Schroeder's avatar
Will Schroeder committed
589
590
591
592
593
594
      }
    else // ascii
      {
      float n[3];
      for (i=0; i<numPts; i++)
        {
Will Schroeder's avatar
Will Schroeder committed
595
596
        if ((retStat=fscanf(fp,"%f %f %f",n,n+1,n+2)) == EOF || retStat < 3)
          {
Ken Martin's avatar
Ken Martin committed
597
          vtkErrorMacro(<<"Error reading normals!");
Will Schroeder's avatar
Will Schroeder committed
598
599
          return 0;
          }
Will Schroeder's avatar
Will Schroeder committed
600
601
602
        normals->SetNormal(i,n);
        }
      }
Will Schroeder's avatar
Will Schroeder committed
603
604
    if ( ! skipNormal ) ds->GetPointData()->SetNormals(normals);
    normals->Delete();
Will Schroeder's avatar
Will Schroeder committed
605
606
607
    }

  else 
Will Schroeder's avatar
Will Schroeder committed
608
    {
Ken Martin's avatar
Ken Martin committed
609
    vtkErrorMacro(<< "Unsupported normals type: " << line);
Will Schroeder's avatar
Will Schroeder committed
610
    return 0;
Will Schroeder's avatar
Will Schroeder committed
611
    }
Will Schroeder's avatar
Will Schroeder committed
612

Will Schroeder's avatar
Will Schroeder committed
613
  return 1;
Will Schroeder's avatar
Will Schroeder committed
614
615
616
617
}

// Description:
// Read tensor point attributes. Return 0 if error.
Ken Martin's avatar
Ken Martin committed
618
int vtkDataReader::ReadTensorData(FILE *fp, vtkDataSet *ds, int numPts)
Will Schroeder's avatar
Will Schroeder committed
619
{
Will Schroeder's avatar
Will Schroeder committed
620
621
622
623
  int retStat, i, skipTensor;
  char line[257], name[257];

  if ((retStat=fscanf(fp, "%256s %256s", name, line)) == EOF || retStat < 2) 
Will Schroeder's avatar
Will Schroeder committed
624
    {
Ken Martin's avatar
Ken Martin committed
625
    vtkErrorMacro(<<"Cannot read tensor data!");
Will Schroeder's avatar
Will Schroeder committed
626
627
    return 0;
    }
Will Schroeder's avatar
Will Schroeder committed
628
629
630
631
632
633
634
635
636
  //
  // See whether tensor has been already read or tensor name (if specified) 
  // matches name in file. 
  //
  if ( ds->GetPointData()->GetTensors() != NULL || 
  (this->TensorsName && strcmp(name,this->TensorsName)) )
    {
    skipTensor = 1;
    }
Will Schroeder's avatar
Will Schroeder committed
637
638
639

  if ( ! strncmp(this->LowerCase(line), "float", 5) )
    {
Ken Martin's avatar
Ken Martin committed
640
    vtkFloatTensors *tensors = new vtkFloatTensors(numPts);
Will Schroeder's avatar
Will Schroeder committed
641
642
643
644
    tensors->SetDimension(3);
    float *ptr = tensors->WritePtr(0,numPts);
    if ( this->FileType == BINARY)
      {
Will Schroeder's avatar
Will Schroeder committed
645
646
647
      if ( (fgets(line,256,fp) == NULL) || //suck up newline
      (fread(ptr,sizeof(float),9*numPts,fp) != (9*numPts)) )
        {
Ken Martin's avatar
Ken Martin committed
648
        vtkErrorMacro(<<"Error reading binary tensors!");
Will Schroeder's avatar
Will Schroeder committed
649
650
        return 0;
        }
Will Schroeder's avatar
Will Schroeder committed
651
      tensors->WrotePtr();
Will Schroeder's avatar
Will Schroeder committed
652
653
654
      }
    else // ascii
      {
Ken Martin's avatar
Ken Martin committed
655
      vtkTensor tensor;
Will Schroeder's avatar
Will Schroeder committed
656
657
658
      float t[9];
      for (i=0; i<numPts; i++)
        {
Will Schroeder's avatar
Will Schroeder committed
659
660
        if ((retStat=fscanf(fp,"%f %f %f",t,t+1,t+2,t+3,t+4,t+5,t+6,t+7,t+8)) == EOF || retStat < 9)
          {
Ken Martin's avatar
Ken Martin committed
661
          vtkErrorMacro(<<"Error reading tensors!");
Will Schroeder's avatar
Will Schroeder committed
662
663
          return 0;
          }
Will Schroeder's avatar
Will Schroeder committed
664
        tensor = t;
Will Schroeder's avatar
Will Schroeder committed
665
        tensors->SetTensor(i,&tensor);
Will Schroeder's avatar
Will Schroeder committed
666
667
        }
      }
Will Schroeder's avatar
Will Schroeder committed
668
669
    if ( ! skipTensor ) ds->GetPointData()->SetTensors(tensors);
    tensors->Delete();
Will Schroeder's avatar
Will Schroeder committed
670
671
672
    }

  else 
Will Schroeder's avatar
Will Schroeder committed
673
    {
Ken Martin's avatar
Ken Martin committed
674
    vtkErrorMacro(<< "Unsupported tensors type: " << line);
Will Schroeder's avatar
Will Schroeder committed
675
    return 0;
Will Schroeder's avatar
Will Schroeder committed
676
    }
Will Schroeder's avatar
Will Schroeder committed
677

Will Schroeder's avatar
Will Schroeder committed
678
  return 1;
Will Schroeder's avatar
Will Schroeder committed
679
680
681
682
}

// Description:
// Read color scalar point attributes. Return 0 if error.
Ken Martin's avatar
Ken Martin committed
683
int vtkDataReader::ReadCoScalarData(FILE *fp, vtkDataSet *ds, int numPts)
Will Schroeder's avatar
Will Schroeder committed
684
{
685
  int retStat, i, nValues, skipScalar=0;
Will Schroeder's avatar
Will Schroeder committed
686
687
  char line[257], name[257];

Will Schroeder's avatar
Will Schroeder committed
688
689
  if ((retStat=fscanf(fp, "%256s %d", name, &nValues)) ==  EOF || retStat < 2)
    {
Ken Martin's avatar
Ken Martin committed
690
    vtkErrorMacro(<<"Cannot read color scalar data!");
Will Schroeder's avatar
Will Schroeder committed
691
692
    return 0;
    }
Will Schroeder's avatar
Will Schroeder committed
693
694
695
696
697
698
699
700
701
  //
  // See whether scalar has been already read or scalar name (if specified) 
  // matches name in file. 
  //
  if ( ds->GetPointData()->GetScalars() != NULL || 
  (this->ScalarsName && strcmp(name,this->ScalarsName)) )
    {
    skipScalar = 1;
    }
Will Schroeder's avatar
Will Schroeder committed
702

Will Schroeder's avatar
Will Schroeder committed
703
  if ( nValues == 1 )
Will Schroeder's avatar
Will Schroeder committed
704
    {
Ken Martin's avatar
Ken Martin committed
705
    vtkGraymap *scalars = new vtkGraymap(numPts);
Will Schroeder's avatar
Will Schroeder committed
706
707
708
    unsigned char *ptr = scalars->WritePtr(0,numPts);
    if ( this->FileType == BINARY)
      {
Will Schroeder's avatar
Will Schroeder committed
709
710
711
      if ( (fgets(line,256,fp) == NULL) || //suck up newline
      (fread(ptr,sizeof(unsigned char),numPts,fp) != numPts) )
        {
Ken Martin's avatar
Ken Martin committed
712
        vtkErrorMacro(<<"Error reading binary graymap!");
Will Schroeder's avatar
Will Schroeder committed
713
714
        return 0;
        }
Will Schroeder's avatar
Will Schroeder committed
715
      scalars->WrotePtr();
Will Schroeder's avatar
Will Schroeder committed
716
717
718
      }
    else // ascii
      {
Will Schroeder's avatar
Will Schroeder committed
719
      float f;
Will Schroeder's avatar
Will Schroeder committed
720
721
722
723
      unsigned char rgba[4];
      rgba[1] = rgba[2] = rgba[3] = 0;
      for (i=0; i<numPts; i++)
        {
Will Schroeder's avatar
Will Schroeder committed
724
725
        if ((retStat=fscanf(fp,"%f",&f)) == EOF || retStat < 1)
          {
Ken Martin's avatar
Ken Martin committed
726
          vtkErrorMacro(<<"Error reading graymap!");
Will Schroeder's avatar
Will Schroeder committed
727
728
          return 0;
          }
Will Schroeder's avatar
Will Schroeder committed
729
        rgba[0] = (unsigned char)((float)f*255.0);
Will Schroeder's avatar
Will Schroeder committed
730
731
732
        scalars->SetColor(i,rgba);
        }
      }
Will Schroeder's avatar
Will Schroeder committed
733
734
    if ( ! skipScalar ) ds->GetPointData()->SetScalars(scalars);
    scalars->Delete();
Will Schroeder's avatar
Will Schroeder committed
735
736
    }

Will Schroeder's avatar
Will Schroeder committed
737
  else if ( nValues == 2 )
Will Schroeder's avatar
Will Schroeder committed
738
    {
Ken Martin's avatar
Ken Martin committed
739
    vtkAGraymap *scalars = new vtkAGraymap(numPts);
Will Schroeder's avatar
Will Schroeder committed
740
741
742
    unsigned char *ptr = scalars->WritePtr(0,numPts);
    if ( this->FileType == BINARY)
      {
Will Schroeder's avatar
Will Schroeder committed
743
744
745
      if ( (fgets(line,256,fp) == NULL) || //suck up newline
      (fread(ptr,sizeof(unsigned char),2*numPts,fp) != (2*numPts)) )
        {
Ken Martin's avatar
Ken Martin committed
746
        vtkErrorMacro(<<"Error reading binary alpha-graymap!");
Will Schroeder's avatar
Will Schroeder committed
747
748
        return 0;
        }
Will Schroeder's avatar
Will Schroeder committed
749
      scalars->WrotePtr();
Will Schroeder's avatar
Will Schroeder committed
750
751
752
      }
    else // ascii
      {
Will Schroeder's avatar
Will Schroeder committed
753
      float f[2];
Will Schroeder's avatar
Will Schroeder committed
754
755
756
757
      unsigned char rgba[4];
      rgba[1] = rgba[2] = 0;
      for (i=0; i<numPts; i++)
        {
Will Schroeder's avatar
Will Schroeder committed
758
759
        if ((retStat=fscanf(fp,"%f %f",f,f+1)) == EOF || retStat < 2)
          {
Ken Martin's avatar
Ken Martin committed
760
          vtkErrorMacro(<<"Error reading alpha-graymap!");
Will Schroeder's avatar
Will Schroeder committed
761
762
          return 0;
          }
Will Schroeder's avatar
Will Schroeder committed
763
764
        rgba[0] = (unsigned char)((float)f[0]*255.0);
        rgba[3] = (unsigned char)((float)f[1]*255.0);
Will Schroeder's avatar
Will Schroeder committed
765
766
767
        scalars->SetColor(i,rgba);
        }
      }
Will Schroeder's avatar
Will Schroeder committed
768
769
    if ( ! skipScalar ) ds->GetPointData()->SetScalars(scalars);
    scalars->Delete();
Will Schroeder's avatar
Will Schroeder committed
770
771
    }

Will Schroeder's avatar
Will Schroeder committed
772
  else if ( nValues == 3 )
Will Schroeder's avatar
Will Schroeder committed
773
    {
Ken Martin's avatar
Ken Martin committed
774
    vtkPixmap *scalars = new vtkPixmap(numPts);
Will Schroeder's avatar
Will Schroeder committed
775
776
777
    unsigned char *ptr = scalars->WritePtr(0,numPts);
    if ( this->FileType == BINARY)
      {
Will Schroeder's avatar
Will Schroeder committed
778
779
780
      if ( (fgets(line,256,fp) == NULL) || //suck up newline
      (fread(ptr,sizeof(unsigned char),3*numPts,fp) != (3*numPts)) )
        {
Ken Martin's avatar
Ken Martin committed
781
        vtkErrorMacro(<<"Error reading binary pixmap!");
Will Schroeder's avatar
Will Schroeder committed
782
783
        return 0;
        }
Will Schroeder's avatar
Will Schroeder committed
784
      scalars->WrotePtr();
Will Schroeder's avatar
Will Schroeder committed
785
786
787
      }
    else // ascii
      {
Will Schroeder's avatar
Will Schroeder committed
788
      float f[3];
Will Schroeder's avatar
Will Schroeder committed
789
790
791
792
      unsigned char rgba[4];
      rgba[3] = 0;
      for (i=0; i<numPts; i++)
        {
Will Schroeder's avatar
Will Schroeder committed
793
794
        if ((retStat=fscanf(fp,"%f %f %f",f,f+1,f+2)) == EOF || retStat < 3) 
          {
Ken Martin's avatar
Ken Martin committed
795
          vtkErrorMacro(<<"Error reading pixmap!");
Will Schroeder's avatar
Will Schroeder committed
796
797
          return 0;
          }
Will Schroeder's avatar
Will Schroeder committed
798
799
800
        rgba[0] = (unsigned char)((float)f[0]*255.0);
        rgba[1] = (unsigned char)((float)f[1]*255.0);
        rgba[2] = (unsigned char)((float)f[2]*255.0);
Will Schroeder's avatar
Will Schroeder committed
801
802
803
        scalars->SetColor(i,rgba);
        }
      }
Will Schroeder's avatar
Will Schroeder committed
804
805
    if ( ! skipScalar ) ds->GetPointData()->SetScalars(scalars);
    scalars->Delete();
Will Schroeder's avatar
Will Schroeder committed
806
807
    }

Will Schroeder's avatar
Will Schroeder committed
808
  else if ( nValues == 4 )
Will Schroeder's avatar
Will Schroeder committed
809
    {
Ken Martin's avatar
Ken Martin committed
810
    vtkAPixmap *scalars = new vtkAPixmap(numPts);
Will Schroeder's avatar
Will Schroeder committed
811
812
813
    unsigned char *ptr = scalars->WritePtr(0,numPts);
    if ( this->FileType == BINARY)
      {
Will Schroeder's avatar
Will Schroeder committed
814
815
816
      if ( (fgets(line,256,fp) == NULL) || //suck up newline
      (fread(ptr,sizeof(unsigned char),4*numPts,fp) != (4*numPts)) )
        {
Ken Martin's avatar
Ken Martin committed
817
        vtkErrorMacro(<<"Error reading binary alpha-pixmap!");
Will Schroeder's avatar
Will Schroeder committed
818
819
        return 0;
        }
Will Schroeder's avatar
Will Schroeder committed
820
      scalars->WrotePtr();
Will Schroeder's avatar
Will Schroeder committed
821
822
823
      }
    else // ascii
      {
Will Schroeder's avatar
Will Schroeder committed
824
      float f[4];
Will Schroeder's avatar
Will Schroeder committed
825
826
827
      unsigned char rgba[4];
      for (i=0; i<numPts; i++)
        {
Will Schroeder's avatar
Will Schroeder committed
828
829
        if ((retStat=fscanf(fp,"%f %f %f %f",f,f+1,f+2,f+3)) == EOF || retStat < 4)
          {
Ken Martin's avatar
Ken Martin committed
830
          vtkErrorMacro(<<"Error reading alpha-pixmap!");
Will Schroeder's avatar
Will Schroeder committed
831
832
          return 0;
          }
Will Schroeder's avatar
Will Schroeder committed
833
834
835
836
        rgba[0] = (unsigned char)((float)f[0]*255.0);
        rgba[1] = (unsigned char)((float)f[1]*255.0);
        rgba[2] = (unsigned char)((float)f[2]*255.0);
        rgba[3] = (unsigned char)((float)f[3]*255.0);
Will Schroeder's avatar
Will Schroeder committed
837
838
839
        scalars->SetColor(i,rgba);
        }
      }
Will Schroeder's avatar
Will Schroeder committed
840
841
    if ( ! skipScalar ) ds->GetPointData()->SetScalars(scalars);
    scalars->Delete();
Will Schroeder's avatar
Will Schroeder committed
842
843
844
    }

  else
Will Schroeder's avatar
Will Schroeder committed
845
    {
Ken Martin's avatar
Ken Martin committed
846
    vtkErrorMacro(<< "Unsupported number values per scalar: " << nValues);
Will Schroeder's avatar
Will Schroeder committed
847
    return 0;
Will Schroeder's avatar
Will Schroeder committed
848
    }
Will Schroeder's avatar
Will Schroeder committed
849

Will Schroeder's avatar
Will Schroeder committed
850
  return 1;
Will Schroeder's avatar
Will Schroeder committed
851
852
853
854
}

// Description:
// Read texture coordinates point attributes. Return 0 if error.
Ken Martin's avatar
Ken Martin committed
855
int vtkDataReader::ReadTCoordsData(FILE *fp, vtkDataSet *ds, int numPts)
Will Schroeder's avatar
Will Schroeder committed
856
{
Will Schroeder's avatar
Will Schroeder committed
857
858
  int retStat, i, dim, skipTCoord;
  char line[257], name[257];
Will Schroeder's avatar
Will Schroeder committed
859

Will Schroeder's avatar
Will Schroeder committed
860
  if ((retStat=fscanf(fp, "%256s %d %256s", name, &dim, line)) == EOF || retStat < 3) 
Will Schroeder's avatar
Will Schroeder committed
861
    {
Ken Martin's avatar
Ken Martin committed
862
    vtkErrorMacro(<<"Cannot read texture data!");
Will Schroeder's avatar
Will Schroeder committed
863
864
    return 0;
    }
Will Schroeder's avatar
Will Schroeder committed
865

Will Schroeder's avatar
Will Schroeder committed
866
867
  if ( dim < 1 || dim > 3 )
    {
Ken Martin's avatar
Ken Martin committed
868
    vtkErrorMacro(<< "Unsupported texture coordinates dimension: " << dim);
Will Schroeder's avatar
Will Schroeder committed
869
870
    return 0;
    }
Will Schroeder's avatar
Will Schroeder committed
871

Will Schroeder's avatar
Will Schroeder committed
872
873
874
875
876
877
878
879
880
881
  //
  // See whether texture coords have been already read or texture coords name
  // (if specified) matches name in file. 
  //
  if ( ds->GetPointData()->GetTCoords() != NULL || 
  (this->TCoordsName && strcmp(name,this->TCoordsName)) )
    {
    skipTCoord = 1;
    }

Will Schroeder's avatar
Will Schroeder committed
882
883
  if ( ! strncmp(this->LowerCase(line), "float", 5) )
    {
Ken Martin's avatar
Ken Martin committed
884
    vtkFloatTCoords *tcoords = new vtkFloatTCoords(numPts);
Will Schroeder's avatar
Will Schroeder committed
885
886
887
888
    tcoords->SetDimension(dim);
    float *ptr = tcoords->WritePtr(0,numPts);
    if ( this->FileType == BINARY)
      {
Will Schroeder's avatar
Will Schroeder committed
889
890
891
      if ( (fgets(line,256,fp) == NULL) || //suck up newline
      (fread(ptr,sizeof(float),dim*numPts,fp) != (dim*numPts)) )
        {
Ken Martin's avatar
Ken Martin committed
892
        vtkErrorMacro(<<"Error reading binary tensors!");
Will Schroeder's avatar
Will Schroeder committed
893
894
        return 0;
        }
Will Schroeder's avatar
Will Schroeder committed
895
      tcoords->WrotePtr();
Will Schroeder's avatar
Will Schroeder committed
896
897
898
899
900
901
902
903
      }
    else // ascii
      {
      float tc[3];
      int j;
      for (i=0; i<numPts; i++)
        {
        for (j=0; j<dim; j++)
Will Schroeder's avatar
Will Schroeder committed
904
905
906
          {
          if ((retStat=fscanf(fp,"%f",tc+j)) == EOF || retStat < 1)
            {
Ken Martin's avatar
Ken Martin committed
907
            vtkErrorMacro(<<"Error reading texture coordinates!");
Will Schroeder's avatar
Will Schroeder committed
908
909
910
            return 0;
            }
          }
Will Schroeder's avatar
Will Schroeder committed
911
912
913
        tcoords->SetTCoord(i,tc);
        }
      }
Will Schroeder's avatar
Will Schroeder committed
914
915
    if ( ! skipTCoord ) ds->GetPointData()->SetTCoords(tcoords);
    tcoords->Delete();
Will Schroeder's avatar
Will Schroeder committed
916
917
918
    }

  else 
Will Schroeder's avatar
Will Schroeder committed
919
    {
Ken Martin's avatar
Ken Martin committed
920
    vtkErrorMacro(<< "Unsupported texture coordinates data type: " << line);
Will Schroeder's avatar
Will Schroeder committed
921
    return 0;
Will Schroeder's avatar
Will Schroeder committed
922
  }
Will Schroeder's avatar
Will Schroeder committed
923

Will Schroeder's avatar
Will Schroeder committed
924
  return 1;
Will Schroeder's avatar
Will Schroeder committed
925
926
927
928
}

// Description:
// Read lookup table. Return 0 if error.
Ken Martin's avatar
Ken Martin committed
929
int vtkDataReader::ReadLutData(FILE *fp, vtkDataSet *ds, int numPts)
Will Schroeder's avatar
Will Schroeder committed
930
{
Will Schroeder's avatar
Will Schroeder committed
931
  int retStat, i, size, skipTable;
Ken Martin's avatar
Ken Martin committed
932
  vtkLookupTable *lut;
Will Schroeder's avatar
Will Schroeder committed
933
  unsigned char *ptr;
Will Schroeder's avatar
Will Schroeder committed
934
935
936
  char line[257], name[257];

  if ((retStat=fscanf(fp, "%256s %d", name, &size)) ==  EOF || retStat < 2) 
Will Schroeder's avatar
Will Schroeder committed
937
    {
Ken Martin's avatar
Ken Martin committed
938
    vtkErrorMacro(<<"Cannot read lookup table data!");
Will Schroeder's avatar
Will Schroeder committed
939
940
    return 0;
    }
Will Schroeder's avatar
Will Schroeder committed
941

Will Schroeder's avatar
Will Schroeder committed
942
943
944
945
946
947
  if ( ds->GetPointData()->GetScalars() == NULL ||
  (this->LookupTableName && strcmp(name,this->LookupTableName)) ||
  (this->ScalarLut && strcmp(name,this->ScalarLut)) )
    {
    skipTable = 1;
    }
Will Schroeder's avatar
Will Schroeder committed
948

Ken Martin's avatar
Ken Martin committed
949
  lut = new vtkLookupTable(size);
Will Schroeder's avatar
Will Schroeder committed
950
951
952
953
  ptr = lut->WritePtr(0,size);

  if ( this->FileType == BINARY)
    {
Will Schroeder's avatar
Will Schroeder committed
954
955
956
      if ( (fgets(line,256,fp) == NULL) || //suck up newline
      (fread(ptr,sizeof(unsigned char),4*size,fp) != (4*size)) )
        {
Ken Martin's avatar
Ken Martin committed
957
        vtkErrorMacro(<<"Error reading binary lookup table!");
Will Schroeder's avatar
Will Schroeder committed
958
959
        return 0;
        }
Will Schroeder's avatar
Will Schroeder committed
960
    lut->WrotePtr();
Will Schroeder's avatar
Will Schroeder committed
961
962
963
    }
  else // ascii
    {