vtkGenericAttributeCollection.cxx 14.3 KB
Newer Older
Will Schroeder's avatar
Will Schroeder committed
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
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkGenericAttributeCollection.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.

=========================================================================*/
// .NAME vtkGenericAttributeCollection - objects that own attributes of a data set
// .DESCRIPTION They can also select an active attribute component to process
// (contouring, clipping) and others attributes to interpolate.

#include "vtkGenericAttributeCollection.h"

#include "vtkObjectFactory.h"
#include "vtkGenericAttribute.h"


#include <vtkstd/vector>
#include <assert.h>

Francois Bertel's avatar
Francois Bertel committed
28
vtkCxxRevisionMacro(vtkGenericAttributeCollection,"1.7");
Will Schroeder's avatar
Will Schroeder committed
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
vtkStandardNewMacro(vtkGenericAttributeCollection);

class vtkGenericAttributeInternalVector
{
public:
  typedef vtkstd::vector<vtkGenericAttribute* > VectorType;
  VectorType Vector;
};

//----------------------------------------------------------------------------
// Description:
// Default constructor: empty collection
vtkGenericAttributeCollection::vtkGenericAttributeCollection()
{
  this->AttributeInternalVector = new vtkGenericAttributeInternalVector;
  this->ActiveAttribute = 0;
  this->ActiveComponent = 0;
  this->NumberOfAttributesToInterpolate = 0;
  this->NumberOfComponents = 0;
  this->MaxNumberOfComponents = 0; // cache
  this->ActualMemorySize = 0;
}

//----------------------------------------------------------------------------
vtkGenericAttributeCollection::~vtkGenericAttributeCollection()
{
  for(unsigned int i = 0; i < this->AttributeInternalVector->Vector.size(); ++i)
    {
    this->AttributeInternalVector->Vector[i]->Delete();
    }
  delete this->AttributeInternalVector;
}

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

67
  int i;
Will Schroeder's avatar
Will Schroeder committed
68
69
70
  int c = this->GetNumberOfAttributes();

  os << indent << "Number Of Attributes: " << this->GetNumberOfAttributes() << "\n";
71
  for(i=0; i<c; ++i)
Will Schroeder's avatar
Will Schroeder committed
72
73
74
75
    {
    os << indent << "Attribute #"<<i<<":\n";
    this->GetAttribute(i)->PrintSelf(os,indent.GetNextIndent());
    }
Francois Bertel's avatar
Francois Bertel committed
76
  
77
78
79
80
81
82
83
84
85
86
  c=this->GetNumberOfAttributesToInterpolate();
  os << indent << "Number Of Attributes to interpolate: " << c << endl;
  
  os << indent << "Attributes to interpolate:";
  
  for(i=0; i<c; ++i)
    {
    os << ' ' << this->AttributesToInterpolate[i];
    }
  os << endl;
Francois Bertel's avatar
Francois Bertel committed
87
88
89
90
  
  os << indent << "Active Attribute: " << this->ActiveAttribute << endl;
  
  os << indent << "Active Component" << this->ActiveComponent << endl;
Will Schroeder's avatar
Will Schroeder committed
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
}

//----------------------------------------------------------------------------
// Description:
// Number of attributes.
int vtkGenericAttributeCollection::GetNumberOfAttributes()
{
  int result = this->AttributeInternalVector->Vector.size();
  //assert("post: positive_result" && result>=0); size() is an unsigned type according to STL vector definition
  return result;
}

//----------------------------------------------------------------------------
int vtkGenericAttributeCollection::GetNumberOfComponents()
{
  this->ComputeNumbers();

  return this->NumberOfComponents;
}

//----------------------------------------------------------------------------
// Description:
// Maximum number of components encountered among all attributes.
// \post positive_result: result>=0
// \post valid_result: result<=GetNumberOfComponents()
int vtkGenericAttributeCollection::GetMaxNumberOfComponents()
{
  this->ComputeNumbers();
  
  assert("post: positive_result" && this->MaxNumberOfComponents>=0);
  assert("post: valid_result" && this->MaxNumberOfComponents<=GetNumberOfComponents());
  return this->MaxNumberOfComponents;
}

//----------------------------------------------------------------------------
// Description:
// Actual size of the data in kilobytes; only valid after the pipeline has
// updated. It is guaranteed to be greater than or equal to the memory
// required to represent the data.
unsigned long vtkGenericAttributeCollection::GetActualMemorySize()
{
  this->ComputeNumbers();
  return this->ActualMemorySize;
}

//----------------------------------------------------------------------------
// Description:
// Does `this' have no attribute?
int vtkGenericAttributeCollection::IsEmpty()
{
  return this->GetNumberOfAttributes()==0;
}

//----------------------------------------------------------------------------
// Description:
// Attribute `i'.
vtkGenericAttribute *vtkGenericAttributeCollection::GetAttribute(int i)
{
  assert("pre: not_empty" && !IsEmpty());
  assert("pre: valid_i" && (i>=0)&&(i<this->GetNumberOfAttributes()));
  vtkGenericAttribute *result=this->AttributeInternalVector->Vector[i];
  assert("post: result_exists" && result!=0);
  return result;
}

//----------------------------------------------------------------------------
// Description:
// Return the index of attribute `name', if found. Return -1 otherwise.
int vtkGenericAttributeCollection::FindAttribute(const char *name)
{
  assert("pre: name_exists:" && name!=0);

  int numAtt = this->GetNumberOfAttributes();
  for( int i = 0; i < numAtt; ++i )
    {
    if( strcmp( this->GetAttribute(i)->GetName(), name ) == 0)
      {
      return i;
      }
    }

  return -1;
}

//----------------------------------------------------------------------------
// Description:
// Add attribute `a' at the end.
void vtkGenericAttributeCollection::InsertNextAttribute(vtkGenericAttribute *a)
{
  assert("pre: a_exists" && a!=0);
#ifndef NDEBUG
  int oldnumber=this->GetNumberOfAttributes();
#endif

  this->AttributeInternalVector->Vector.push_back(a);
  this->Modified();
  
  assert("post: more_items" && this->GetNumberOfAttributes()==oldnumber+1);
  assert("post: a_is_set" && this->GetAttribute(this->GetNumberOfAttributes()-1)==a);
}

//----------------------------------------------------------------------------
// Description:
// Replace attribute at index `i' by `a'.
void vtkGenericAttributeCollection::InsertAttribute(int i, vtkGenericAttribute *a)
{
  assert("pre: not_empty" && !this->IsEmpty());
  assert("pre: a_exists" && a!=0);
  assert("pre: valid_i" && (i>=0)&&(i<this->GetNumberOfAttributes()));

#ifndef NDEBUG
int oldnumber = this->GetNumberOfAttributes();
#endif

  this->AttributeInternalVector->Vector[i] = a;
  this->Modified();
  
  assert("post: more_items" && this->GetNumberOfAttributes()==oldnumber);
  assert("post: a_is_set" && this->GetAttribute(i)==a);
}

//----------------------------------------------------------------------------
// Description:
// Remove Attribute at `i'.
void vtkGenericAttributeCollection::RemoveAttribute(int i)
{
  assert("pre: not_empty" && !this->IsEmpty());
  assert("pre: valid_i" && (i>=0)&&(i<this->GetNumberOfAttributes()));
  
#ifndef NDEBUG
  int oldnumber=this->GetNumberOfAttributes();
#endif
  
  this->AttributeInternalVector->Vector.erase(
    this->AttributeInternalVector->Vector.begin()+i);
  this->Modified();
  
  assert("post: fewer_items" && this->GetNumberOfAttributes()==(oldnumber-1));
}

//----------------------------------------------------------------------------
// Description:
// Remove all attributes.
void vtkGenericAttributeCollection::Reset()
{
  for(unsigned int i = 0; i < this->AttributeInternalVector->Vector.size(); ++i)
    {
    this->AttributeInternalVector->Vector[i]->Delete();
    }
  this->AttributeInternalVector->Vector.clear();
  this->Modified();
  
  assert("post: is_empty" && this->IsEmpty());
}

//----------------------------------------------------------------------------
// Description:
// Recursive duplication of `other' in `this'.
void vtkGenericAttributeCollection::DeepCopy(vtkGenericAttributeCollection *other)
{
  assert("pre: other_exists" && other!=0);
  assert("pre: not_self" && other!=this);
  
  this->AttributeInternalVector->Vector.resize(
    other->AttributeInternalVector->Vector.size());
  
  int c = this->AttributeInternalVector->Vector.size();
  for(int i=0; i<c; i++)
    {
    if(this->AttributeInternalVector->Vector[i] == 0)
      {
      this->AttributeInternalVector->Vector[i] = 
        other->AttributeInternalVector->Vector[i]->NewInstance();
      }
    this->AttributeInternalVector->Vector[i]->DeepCopy(
      other->AttributeInternalVector->Vector[i]);
    }
  this->Modified();
  
  assert("post: same_size" && this->GetNumberOfAttributes()==other->GetNumberOfAttributes());
}

//----------------------------------------------------------------------------
// Description:
// Update `this' using fields of `other'.
void vtkGenericAttributeCollection::ShallowCopy(vtkGenericAttributeCollection *other)
{
  assert("pre: other_exists" && other!=0);
  assert("pre: not_self" && other!=this);
  
  this->AttributeInternalVector->Vector = 
    other->AttributeInternalVector->Vector;
  this->Modified();
  
  assert("post: same_size" && this->GetNumberOfAttributes()==other->GetNumberOfAttributes());
}

//----------------------------------------------------------------------------
// Description:
// Collection is composite object and need to check each part for MTime.
unsigned long int vtkGenericAttributeCollection::GetMTime()
{
  unsigned long result;
  unsigned long mtime;
  
  result = vtkObject::GetMTime();
  
  for(int i = 0; i < this->GetNumberOfAttributes(); ++i)
    {
    mtime = this->GetAttribute(i)->GetMTime();
    result = ( mtime > result ? mtime : result );
    }

  return result;
}

//----------------------------------------------------------------------------
// Description:
// Compute number of components, max number of components and actual memory
// size.
void vtkGenericAttributeCollection::ComputeNumbers()
{
  if ( this->GetMTime() > this->ComputeTime )
    {
    int nb = 0;
    int count = 0;
    int maxNb = 0;
    unsigned long memory=0;
    
    int c = this->GetNumberOfAttributes();
    
    for(int i = 0; i < c; ++i)
      {
      count = this->GetAttribute(i)->GetNumberOfComponents();
      memory=memory+this->GetAttribute(i)->GetActualMemorySize();
      if(count > maxNb)
        {
        maxNb = count;
        }
      nb += count;
      }
    
    this->NumberOfComponents = nb;
    this->MaxNumberOfComponents = maxNb;
    this->ActualMemorySize = memory;
    
    assert("check: positive_number" && this->NumberOfComponents>=0);
    assert("check: positiveMaxNumber" && this->MaxNumberOfComponents>=0);
    assert("check: valid_number" && this->MaxNumberOfComponents<=this->NumberOfComponents);
    this->ComputeTime.Modified();
    }
}

//----------------------------------------------------------------------------
// *** ALL THE FOLLOWING METHODS SHOULD BE REMOVED WHEN vtkInformation
// will be ready.
// *** BEGIN

// Description:
// Set the scalar attribute to be processed.
void vtkGenericAttributeCollection::SetActiveAttribute(int attribute,
352
                                                       int component)
Will Schroeder's avatar
Will Schroeder committed
353
354
355
356
357
358
359
360
361
362
363
{
  assert("pre: not_empty" && !IsEmpty());
  assert("pre: valid_attribute" && (attribute>=0)&&(attribute<this->GetNumberOfAttributes()));
  assert("pre: valid_component" && (component>=0)&&(component<this->GetAttribute(attribute)->GetNumberOfComponents()));

  this->ActiveAttribute = attribute;
  this->ActiveComponent = component;
  
  assert("post: is_set" && (this->GetActiveAttribute()==attribute) && (this->GetActiveComponent()==component));
}

364
365
366
367
368
369
370
371
372
373
//----------------------------------------------------------------------------
// Description:
// Indices of attributes to interpolate.
// \pre not_empty: !IsEmpty()
// \post valid_result: GetNumberOfAttributesToInterpolate()>0 implies
//                       result!=0
int *vtkGenericAttributeCollection::GetAttributesToInterpolate()
{
  assert("pre: not_empty" && !IsEmpty());
  
Francois Bertel's avatar
Francois Bertel committed
374
  assert("post: valid_result" && ((!(this->NumberOfAttributesToInterpolate>0))
375
376
377
378
379
380
                                  || this->AttributesToInterpolate!=0));
  // A=>B !A||B
         
  return this->AttributesToInterpolate;
}

Will Schroeder's avatar
Will Schroeder committed
381
382
383
384
//----------------------------------------------------------------------------
// Description
// Does the array `attributes' of size `size' have `attribute'?
int vtkGenericAttributeCollection::HasAttribute(int size,
385
386
                                                int *attributes,
                                                int attribute)
Will Schroeder's avatar
Will Schroeder committed
387
388
{
  assert("pre: positive_size" && size>=0);
Francois Bertel's avatar
Francois Bertel committed
389
  assert("pre: valid_attributes" && ((!(size>0))||(attributes!=0))); // size>0 => attributes!=0 (A=>B: !A||B )
Will Schroeder's avatar
Will Schroeder committed
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408

  int result = 0; // false
  int i;

  if(size != 0)
    {
    i = 0;
    while( !result && i++ < size )
      {
      result = attributes[i] == attribute;
      }
    }
  return result;
}

//----------------------------------------------------------------------------
// Description:
// Set the attributes to interpolate.
void vtkGenericAttributeCollection::SetAttributesToInterpolate(int size,
409
                                                               int *attributes)
Will Schroeder's avatar
Will Schroeder committed
410
411
412
413
{
  assert("pre: not_empty" && !this->IsEmpty());
  assert("pre: positive_size" && size>=0);
  assert("pre: magic_number" && size<=10);
Francois Bertel's avatar
Francois Bertel committed
414
  assert("pre: valid_attributes" && ((!(size>0))||(attributes!=0)));  // size>0 => attributes!=0 (A=>B: !A||B )
415
416
  assert("pre: valid_attributes_contents" && (!(attributes!=0) || !(!this->HasAttribute(size,attributes,this->GetActiveAttribute())))); // attributes!=0 => !this->HasAttribute(size,attributes,this->GetActiveAttribute()) (A=>B: !A||B )
  
Will Schroeder's avatar
Will Schroeder committed
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
  this->NumberOfAttributesToInterpolate = size;
  for(int i=0; i<size; ++i)
    {
    this->AttributesToInterpolate[i] = attributes[i];
    }

  assert("post: is_set" && (this->GetNumberOfAttributesToInterpolate()==size));
}

//----------------------------------------------------------------------------
// Description:
// Set the attributes to interpolate.
void vtkGenericAttributeCollection::SetAttributesToInterpolateToAll()
{
  assert("pre: not_empty" && !this->IsEmpty());

  this->NumberOfAttributesToInterpolate = this->GetMaxNumberOfComponents();
  for(int i=0; i<this->NumberOfAttributesToInterpolate; ++i)
    {
    this->AttributesToInterpolate[i] = i;
    }
}

//----------------------------------------------------------------------------
// *** ALL THE PREVIOUS METHODS SHOULD BE REMOVED WHEN vtkInformation
// will be ready.
// *** END