1 /*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: vtkBridgeAttribute.cxx
5
6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 All rights reserved.
8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notice for more information.
13
14 =========================================================================*/
15 // .NAME vtkBridgeAttribute - Implementation of vtkGenericAttribute.
16 // .SECTION Description
17 // It is just an example that show how to implement the Generic. It is also
18 // used for testing and evaluating the Generic.
19 // .SECTION See Also
20 // vtkGenericAttribute, vtkBridgeDataSet
21
22 #include "vtkBridgeAttribute.h"
23
24 #include "vtkBridgeCell.h"
25 #include "vtkBridgeCellIterator.h"
26 #include "vtkCellData.h"
27 #include "vtkDataArray.h"
28 #include "vtkDataSetAttributes.h"
29 #include "vtkGenericCell.h"
30 #include "vtkGenericPointIterator.h"
31 #include "vtkObjectFactory.h"
32 #include "vtkPointData.h"
33 #include "vtkSetGet.h"
34
35 #include <cassert>
36
37 vtkStandardNewMacro(vtkBridgeAttribute);
38
PrintSelf(ostream & os,vtkIndent indent)39 void vtkBridgeAttribute::PrintSelf(ostream& os, vtkIndent indent)
40 {
41 this->Superclass::PrintSelf(os,indent);
42 }
43
44 //-----------------------------------------------------------------------------
45 // Description:
46 // Name of the attribute. (e.g. "velocity")
47 // \post result_may_not_exist: result!=0 || result==0
GetName()48 const char *vtkBridgeAttribute::GetName()
49 {
50 return this->Data->GetArray(this->AttributeNumber)->GetName();
51 }
52
53 //-----------------------------------------------------------------------------
54 // Description:
55 // Dimension of the attribute. (1 for scalar, 3 for velocity)
56 // \post positive_result: result>=0
GetNumberOfComponents()57 int vtkBridgeAttribute::GetNumberOfComponents()
58 {
59 int result=this->Data->GetArray(this->AttributeNumber)->GetNumberOfComponents();
60 assert("post: positive_result" && result>=0);
61 return result;
62 }
63
64 //-----------------------------------------------------------------------------
65 // Description:
66 // Is the attribute centered either on points, cells or boundaries?
67 // \post valid_result: (result==vtkPointCentered) ||
68 // (result==vtkCellCentered) || (result==vtkBoundaryCentered)
GetCentering()69 int vtkBridgeAttribute::GetCentering()
70 {
71 int result;
72 if(this->Pd!=nullptr)
73 {
74 result=vtkPointCentered;
75 }
76 else
77 {
78 result=vtkCellCentered;
79 }
80 assert("post: valid_result" && ((result==vtkPointCentered) || (result==vtkCellCentered) || (result==vtkBoundaryCentered)));
81 return result;
82 }
83
84 //-----------------------------------------------------------------------------
85 // Description:
86 // Type of the attribute: scalar, vector, normal, texture coordinate, tensor
87 // \post valid_result: (result==vtkDataSetAttributes::SCALARS)
88 // ||(result==vtkDataSetAttributes::VECTORS)
89 // ||(result==vtkDataSetAttributes::NORMALS)
90 // ||(result==vtkDataSetAttributes::TCOORDS)
91 // ||(result==vtkDataSetAttributes::TENSORS)
GetType()92 int vtkBridgeAttribute::GetType()
93 {
94 int result=this->Data->IsArrayAnAttribute(this->AttributeNumber);
95 if(result==-1)
96 {
97 switch(this->GetNumberOfComponents())
98 {
99 case 1:
100 result=vtkDataSetAttributes::SCALARS;
101 break;
102 case 3:
103 result=vtkDataSetAttributes::VECTORS;
104 break;
105 case 9:
106 result=vtkDataSetAttributes::TENSORS;
107 break;
108 default:
109 assert("check: unknown attribute type" && 0);
110 break;
111 }
112 }
113 return result;
114 }
115
116 //-----------------------------------------------------------------------------
117 // Description:
118 // Type of the components of the attribute: int, float, double
119 // \post valid_result: (result==VTK_BIT) ||(result==VTK_CHAR)
120 // ||(result==VTK_UNSIGNED_CHAR) ||(result==VTK_SHORT)
121 // ||(result==VTK_UNSIGNED_SHORT)||(result==VTK_INT)
122 // ||(result==VTK_UNSIGNED_INT) ||(result==VTK_LONG)
123 // ||(result==VTK_UNSIGNED_LONG) ||(result==VTK_FLOAT)
124 // ||(result==VTK_DOUBLE) ||(result==VTK_ID_TYPE)
GetComponentType()125 int vtkBridgeAttribute::GetComponentType()
126 {
127 return this->Data->GetArray(this->AttributeNumber)->GetDataType();
128 }
129
130 //-----------------------------------------------------------------------------
131 // Description:
132 // Number of tuples.
133 // \post valid_result: result>=0
GetSize()134 vtkIdType vtkBridgeAttribute::GetSize()
135 {
136 vtkIdType result=this->Data->GetArray(this->AttributeNumber)->GetNumberOfTuples();
137 assert("post: valid_result" && result>=0);
138 return result;
139 }
140
141 //-----------------------------------------------------------------------------
142 // Description:
143 // Size in kibibytes (1024 bytes) taken by the attribute.
GetActualMemorySize()144 unsigned long vtkBridgeAttribute::GetActualMemorySize()
145 {
146 return this->Data->GetArray(this->AttributeNumber)->GetActualMemorySize();
147 }
148
149 //-----------------------------------------------------------------------------
150 // Description:
151 // Range of the attribute component `component'. It returns double, even if
152 // GetType()==VTK_INT.
153 // NOT THREAD SAFE
154 // \pre valid_component: (component>=-1)&&(component<GetNumberOfComponents())
155 // \post result_exists: result!=0
GetRange(int component)156 double *vtkBridgeAttribute::GetRange(int component)
157 {
158 assert("pre: valid_component" && (component>=-1)&&(component<this->GetNumberOfComponents()));
159 double *result=this->Data->GetArray(this->AttributeNumber)->GetRange(component);
160 assert("post: result_exists" && result!=nullptr);
161 return result;
162 }
163
164 //-----------------------------------------------------------------------------
165 // Description:
166 // Range of the attribute component `component'.
167 // THREAD SAFE
168 // \pre valid_component: (component>=-1)&&(component<GetNumberOfComponents())
GetRange(int component,double range[2])169 void vtkBridgeAttribute::GetRange(int component,
170 double range[2])
171 {
172 assert("pre: valid_component" && (component>=-1)&&(component<this->GetNumberOfComponents()));
173 this->Data->GetArray(this->AttributeNumber)->GetRange(range,component);
174 }
175
176 //-----------------------------------------------------------------------------
177 // Description:
178 // Return the maximum euclidean norm for the tuples.
179 // \post positive_result: result>=0
GetMaxNorm()180 double vtkBridgeAttribute::GetMaxNorm()
181 {
182 double result=this->Data->GetArray(this->AttributeNumber)->GetMaxNorm();
183 assert("post: positive_result" && result>=0);
184 return result;
185 }
186
187 //-----------------------------------------------------------------------------
188 // Description:
189 // Attribute at all points of cell `c'.
190 // \pre c_exists: c!=0
191 // \pre c_valid: !c->IsAtEnd()
192 // \post result_exists: result!=0
193 // \post valid_result: sizeof(result)==GetNumberOfComponents()*c->GetCell()->GetNumberOfPoints()
GetTuple(vtkGenericAdaptorCell * c)194 double *vtkBridgeAttribute::GetTuple(vtkGenericAdaptorCell *c)
195 {
196 assert("pre: c_exists" && c!=nullptr);
197
198 this->AllocateInternalTuple(c->GetNumberOfPoints()*this->GetNumberOfComponents());
199 this->GetTuple(c,this->InternalTuple);
200
201 assert("post: result_exists" && this->InternalTuple!=nullptr);
202 return this->InternalTuple;
203 }
204
205 //-----------------------------------------------------------------------------
206 // Description:
207 // Put attribute at all points of cell `c' in `tuple'.
208 // \pre c_exists: c!=0
209 // \pre c_valid: !c->IsAtEnd()
210 // \pre tuple_exists: tuple!=0
211 // \pre valid_tuple: sizeof(tuple)>=GetNumberOfComponents()*c->GetCell()->GetNumberOfPoints()
GetTuple(vtkGenericAdaptorCell * c,double * tuple)212 void vtkBridgeAttribute::GetTuple(vtkGenericAdaptorCell *c, double *tuple)
213 {
214 assert("pre: c_exists" && c!=nullptr);
215 assert("pre: tuple_exists" && tuple!=nullptr);
216
217 double *p=tuple;
218 int i;
219 int j;
220 int size;
221 vtkBridgeCell *c2=static_cast<vtkBridgeCell *>(c);
222
223
224 if(this->Pd!=nullptr)
225 {
226 i=0;
227 size=c2->GetNumberOfPoints();
228 while(i<size)
229 {
230 j=c2->Cell->GetPointId(i);
231 this->Data->GetArray(this->AttributeNumber)->GetTuple(j,p);
232 ++i;
233 p=p+this->GetNumberOfComponents();
234 }
235 }
236 else
237 {
238 this->Data->GetArray(this->AttributeNumber)->GetTuple(c2->GetId(),tuple);
239 // duplicate:
240 size=c2->GetNumberOfPoints();
241 i=1;
242 p=p+this->GetNumberOfComponents();
243 while(i<size)
244 {
245 memcpy(p,tuple,sizeof(double)*this->GetNumberOfComponents());
246 p=p+this->GetNumberOfComponents();
247 ++i;
248 }
249 }
250 }
251
252 //-----------------------------------------------------------------------------
253 // Description:
254 // Attribute at all points of cell `c'.
255 // \pre c_exists: c!=0
256 // \pre c_valid: !c->IsAtEnd()
257 // \post result_exists: result!=0
258 // \post valid_result: sizeof(result)==GetNumberOfComponents()*c->GetCell()->GetNumberOfPoints()
GetTuple(vtkGenericCellIterator * c)259 double *vtkBridgeAttribute::GetTuple(vtkGenericCellIterator *c)
260 {
261 assert("pre: c_exists" && c!=nullptr);
262 assert("pre: c_valid" && !c->IsAtEnd());
263
264 return this->GetTuple(c->GetCell());
265 }
266
267 //-----------------------------------------------------------------------------
268 // Description:
269 // Put attribute at all points of cell `c' in `tuple'.
270 // \pre c_exists: c!=0
271 // \pre c_valid: !c->IsAtEnd()
272 // \pre tuple_exists: tuple!=0
273 // \pre valid_tuple: sizeof(tuple)>=GetNumberOfComponents()*c->GetCell()->GetNumberOfPoints()
GetTuple(vtkGenericCellIterator * c,double * tuple)274 void vtkBridgeAttribute::GetTuple(vtkGenericCellIterator *c, double *tuple)
275 {
276 assert("pre: c_exists" && c!=nullptr);
277 assert("pre: c_valid" && !c->IsAtEnd());
278 assert("pre: tuple_exists" && tuple!=nullptr);
279
280 this->GetTuple(c->GetCell(),tuple);
281 }
282
283 //-----------------------------------------------------------------------------
284 // Description:
285 // Value of the attribute at position `p'.
286 // \pre p_exists: p!=0
287 // \pre p_valid: !p->IsAtEnd()
288 // \post result_exists: result!=0
289 // \post valid_result_size: sizeof(result)==GetNumberOfComponents()
GetTuple(vtkGenericPointIterator * p)290 double *vtkBridgeAttribute::GetTuple(vtkGenericPointIterator *p)
291 {
292 assert("pre: p_exists" && p!=nullptr);
293 assert("pre: p_valid" && !p->IsAtEnd());
294
295 this->AllocateInternalTuple(this->GetNumberOfComponents());
296
297 this->Data->GetArray(this->AttributeNumber)->GetTuple(p->GetId(),this->InternalTuple);
298
299 assert("post: result_exists" && this->InternalTuple!=nullptr);
300 return this->InternalTuple;
301 }
302
303 //-----------------------------------------------------------------------------
304 // Description:
305 // Put the value of the attribute at position `p' into `tuple'.
306 // \pre p_exists: p!=0
307 // \pre p_valid: !p->IsAtEnd()
308 // \pre tuple_exists: tuple!=0
309 // \pre valid_tuple_size: sizeof(tuple)>=GetNumberOfComponents()
GetTuple(vtkGenericPointIterator * p,double * tuple)310 void vtkBridgeAttribute::GetTuple(vtkGenericPointIterator *p, double *tuple)
311 {
312 assert("pre: p_exists" && p!=nullptr);
313 assert("pre: p_valid" && !p->IsAtEnd());
314 assert("pre: tuple_exists" && tuple!=nullptr);
315 this->Data->GetArray(this->AttributeNumber)->GetTuple(p->GetId(),tuple);
316 }
317
318 //-----------------------------------------------------------------------------
319 // Description:
320 // Put component `i' of the attribute at all points of cell `c' in `values'.
321 // \pre valid_component: (i>=0) && (i<GetNumberOfComponents())
322 // \pre c_exists: c!=0
323 // \pre c_valid: !c->IsAtEnd()
324 // \pre values_exist: values!=0
325 // \pre valid_values: sizeof(values)>=c->GetCell()->GetNumberOfPoints()
GetComponent(int i,vtkGenericCellIterator * c,double * values)326 void vtkBridgeAttribute::GetComponent(int i,vtkGenericCellIterator *c, double *values)
327 {
328 assert("pre: c_exists" && c!=nullptr);
329 assert("pre: c_valid" && !c->IsAtEnd());
330
331 int j;
332 int size;
333 vtkBridgeCellIterator *c2=static_cast<vtkBridgeCellIterator *>(c);
334
335 if(this->Pd!=nullptr)
336 {
337 j=0;
338 size=c2->GetCell()->GetNumberOfPoints();
339 while(j<size)
340 {
341 vtkIdType id=static_cast<vtkBridgeCell *>(c2->GetCell())->Cell->GetPointId(j);
342 values[j]=this->Data->GetArray(this->AttributeNumber)->GetComponent(id,i);
343 ++j;
344 }
345 }
346 else
347 {
348 values[0]=this->Data->GetArray(this->AttributeNumber)->GetComponent(c2->GetCell()->GetId(),i);
349 // duplicate:
350 size=c2->GetCell()->GetNumberOfPoints();
351 j=1;
352 while(j<size)
353 {
354 values[j]=values[0];
355 ++j;
356 }
357 }
358 }
359
360 //-----------------------------------------------------------------------------
361 // Description:
362 // Value of the component `i' of the attribute at position `p'.
363 // \pre valid_component: (i>=0) && (i<GetNumberOfComponents())
364 // \pre p_exists: p!=0
365 // \pre p_valid: !p->IsAtEnd()
GetComponent(int i,vtkGenericPointIterator * p)366 double vtkBridgeAttribute::GetComponent(int i,vtkGenericPointIterator *p)
367 {
368 assert("pre: p_exists" && p!=nullptr);
369 assert("pre: p_valid" && !p->IsAtEnd());
370 // Only relevant if GetCentering()==vtkCenteringPoint?
371 return this->Data->GetArray(this->AttributeNumber)->GetComponent(p->GetId(),i);
372 }
373
374 //-----------------------------------------------------------------------------
375 // Description:
376 // Recursive duplication of `other' in `this'.
377 // \pre other_exists: other!=0
378 // \pre not_self: other!=this
DeepCopy(vtkGenericAttribute * other)379 void vtkBridgeAttribute::DeepCopy(vtkGenericAttribute *other)
380 {
381 assert("pre: other_exists" && other!=nullptr);
382 assert("pre: not_self" && other!=this);
383 vtkBridgeAttribute *o=static_cast<vtkBridgeAttribute *>(other);
384
385 vtkSetObjectBodyMacro(Pd,vtkPointData,o->Pd);
386 vtkSetObjectBodyMacro(Cd,vtkCellData,o->Cd);
387 this->Data=o->Data;
388 this->AttributeNumber=o->AttributeNumber;
389 this->AllocateInternalTuple(this->GetNumberOfComponents());
390 }
391
392 //-----------------------------------------------------------------------------
393 // Description:
394 // Update `this' using fields of `other'.
395 // \pre other_exists: other!=0
396 // \pre not_self: other!=this
ShallowCopy(vtkGenericAttribute * other)397 void vtkBridgeAttribute::ShallowCopy(vtkGenericAttribute *other)
398 {
399 assert("pre: other_exists" && other!=nullptr);
400 assert("pre: not_self" && other!=this);
401 vtkBridgeAttribute *o=static_cast<vtkBridgeAttribute *>(other);
402
403 vtkSetObjectBodyMacro(Pd,vtkPointData,o->Pd);
404 vtkSetObjectBodyMacro(Cd,vtkCellData,o->Cd);
405 this->Data=o->Data;
406 this->AttributeNumber=o->AttributeNumber;
407 this->AllocateInternalTuple(this->GetNumberOfComponents());
408 }
409
410 //-----------------------------------------------------------------------------
411 // Description:
412 // Set the current attribute to be centered on points with attribute `i' of
413 // `d'.
414 // \pre d_exists: d!=0
415 // \pre valid_range: (i>=0) && (i<d->GetNumberOfArrays())
InitWithPointData(vtkPointData * d,int i)416 void vtkBridgeAttribute::InitWithPointData(vtkPointData *d,
417 int i)
418 {
419 assert("pre: d_exists" && d!=nullptr);
420 assert("pre: valid_range" && (i>=0) && (i<d->GetNumberOfArrays()));
421 vtkSetObjectBodyMacro(Cd,vtkCellData,0);
422 vtkSetObjectBodyMacro(Pd,vtkPointData,d);
423 this->Data=d;
424 this->AttributeNumber=i;
425 this->AllocateInternalTuple(this->GetNumberOfComponents());
426 }
427
428 //-----------------------------------------------------------------------------
429 // Description:
430 // Set the current attribute to be centered on cells with attribute `i' of `d'.
431 // \pre d_exists: d!=0
432 // \pre valid_range: (i>=0) && (i<d->GetNumberOfArrays())
InitWithCellData(vtkCellData * d,int i)433 void vtkBridgeAttribute::InitWithCellData(vtkCellData *d,
434 int i)
435 {
436 assert("pre: d_exists" && d!=nullptr);
437 assert("pre: valid_range" && (i>=0) && (i<d->GetNumberOfArrays()));
438 vtkSetObjectBodyMacro(Pd,vtkPointData,0);
439 vtkSetObjectBodyMacro(Cd,vtkCellData,d);
440 this->Data=d;
441 this->AttributeNumber=i;
442 this->AllocateInternalTuple(this->GetNumberOfComponents());
443 }
444
445 //-----------------------------------------------------------------------------
446 // Description:
447 // Default constructor: empty attribute, not valid
vtkBridgeAttribute()448 vtkBridgeAttribute::vtkBridgeAttribute()
449 {
450 this->Pd=nullptr;
451 this->Cd=nullptr;
452 this->Data=nullptr;
453 this->AttributeNumber=0;
454 this->InternalTuple=nullptr;
455 this->InternalTupleCapacity=0;
456 }
457
458 //-----------------------------------------------------------------------------
459 // Description:
460 // Destructor.
~vtkBridgeAttribute()461 vtkBridgeAttribute::~vtkBridgeAttribute()
462 {
463 if(this->Pd!=nullptr)
464 {
465 this->Pd->Delete();
466 }
467 else
468 {
469 if(this->Cd!=nullptr)
470 {
471 this->Cd->Delete();
472 }
473 }
474 delete[] this->InternalTuple;
475 }
476
477 //-----------------------------------------------------------------------------
478 // Description:
479 // If size>InternalTupleCapacity, allocate enough memory.
480 // \pre positive_size: size>0
AllocateInternalTuple(int size)481 void vtkBridgeAttribute::AllocateInternalTuple(int size)
482 {
483 // size=this->GetNumberOfComponents()
484 assert("pre: positive_size" && size>0);
485
486 if(this->InternalTuple==nullptr)
487 {
488 this->InternalTupleCapacity = size;
489 this->InternalTuple = new double[this->InternalTupleCapacity];
490 }
491 else
492 {
493 if(InternalTupleCapacity<size)
494 {
495 this->InternalTupleCapacity = size;
496 delete [] this->InternalTuple;
497 this->InternalTuple = new double[this->InternalTupleCapacity];
498 }
499 }
500 }
501