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!=0)
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 kilobytes 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!=0);
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!=0);
197 
198   this->AllocateInternalTuple(c->GetNumberOfPoints()*this->GetNumberOfComponents());
199   this->GetTuple(c,this->InternalTuple);
200 
201   assert("post: result_exists" && this->InternalTuple!=0);
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!=0);
215   assert("pre: tuple_exists" && tuple!=0);
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!=0)
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!=0);
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!=0);
277   assert("pre: c_valid" && !c->IsAtEnd());
278   assert("pre: tuple_exists" && tuple!=0);
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!=0);
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!=0);
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!=0);
313   assert("pre: p_valid" && !p->IsAtEnd());
314   assert("pre: tuple_exists" && tuple!=0);
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!=0);
329   assert("pre: c_valid" && !c->IsAtEnd());
330 
331   int j;
332   int id;
333   int size;
334   vtkBridgeCellIterator *c2=static_cast<vtkBridgeCellIterator *>(c);
335 
336   if(this->Pd!=0)
337     {
338     j=0;
339     size=c2->GetCell()->GetNumberOfPoints();
340     while(j<size)
341       {
342       id=static_cast<vtkBridgeCell *>(c2->GetCell())->Cell->GetPointId(j);
343       values[j]=this->Data->GetArray(this->AttributeNumber)->GetComponent(id,i);
344       ++j;
345       }
346     }
347   else
348     {
349     values[0]=this->Data->GetArray(this->AttributeNumber)->GetComponent(c2->GetCell()->GetId(),i);
350     // duplicate:
351     size=c2->GetCell()->GetNumberOfPoints();
352     j=1;
353     while(j<size)
354       {
355       values[j]=values[0];
356       ++j;
357       }
358     }
359 }
360 
361 //-----------------------------------------------------------------------------
362 // Description:
363 // Value of the component `i' of the attribute at position `p'.
364 // \pre valid_component: (i>=0) && (i<GetNumberOfComponents())
365 // \pre p_exists: p!=0
366 // \pre p_valid: !p->IsAtEnd()
GetComponent(int i,vtkGenericPointIterator * p)367 double vtkBridgeAttribute::GetComponent(int i,vtkGenericPointIterator *p)
368 {
369   assert("pre: p_exists" && p!=0);
370   assert("pre: p_valid" && !p->IsAtEnd());
371   // Only relevant if GetCentering()==vtkCenteringPoint?
372   return this->Data->GetArray(this->AttributeNumber)->GetComponent(p->GetId(),i);
373 }
374 
375 //-----------------------------------------------------------------------------
376 // Description:
377 // Recursive duplication of `other' in `this'.
378 // \pre other_exists: other!=0
379 // \pre not_self: other!=this
DeepCopy(vtkGenericAttribute * other)380 void vtkBridgeAttribute::DeepCopy(vtkGenericAttribute *other)
381 {
382   assert("pre: other_exists" && other!=0);
383   assert("pre: not_self" && other!=this);
384   vtkBridgeAttribute *o=static_cast<vtkBridgeAttribute *>(other);
385 
386   vtkSetObjectBodyMacro(Pd,vtkPointData,o->Pd);
387   vtkSetObjectBodyMacro(Cd,vtkCellData,o->Cd);
388   this->Data=o->Data;
389   this->AttributeNumber=o->AttributeNumber;
390   this->AllocateInternalTuple(this->GetNumberOfComponents());
391 }
392 
393 //-----------------------------------------------------------------------------
394 // Description:
395 // Update `this' using fields of `other'.
396 // \pre other_exists: other!=0
397 // \pre not_self: other!=this
ShallowCopy(vtkGenericAttribute * other)398 void vtkBridgeAttribute::ShallowCopy(vtkGenericAttribute *other)
399 {
400   assert("pre: other_exists" && other!=0);
401   assert("pre: not_self" && other!=this);
402   vtkBridgeAttribute *o=static_cast<vtkBridgeAttribute *>(other);
403 
404   vtkSetObjectBodyMacro(Pd,vtkPointData,o->Pd);
405   vtkSetObjectBodyMacro(Cd,vtkCellData,o->Cd);
406   this->Data=o->Data;
407   this->AttributeNumber=o->AttributeNumber;
408   this->AllocateInternalTuple(this->GetNumberOfComponents());
409 }
410 
411 //-----------------------------------------------------------------------------
412 // Description:
413 // Set the current attribute to be centered on points with attribute `i' of
414 // `d'.
415 // \pre d_exists: d!=0
416 // \pre valid_range: (i>=0) && (i<d->GetNumberOfArrays())
InitWithPointData(vtkPointData * d,int i)417 void vtkBridgeAttribute::InitWithPointData(vtkPointData *d,
418                                            int i)
419 {
420   assert("pre: d_exists" && d!=0);
421   assert("pre: valid_range" && (i>=0) && (i<d->GetNumberOfArrays()));
422   vtkSetObjectBodyMacro(Cd,vtkCellData,0);
423   vtkSetObjectBodyMacro(Pd,vtkPointData,d);
424   this->Data=d;
425   this->AttributeNumber=i;
426   this->AllocateInternalTuple(this->GetNumberOfComponents());
427 }
428 
429 //-----------------------------------------------------------------------------
430 // Description:
431 // Set the current attribute to be centered on cells with attribute `i' of `d'.
432 // \pre d_exists: d!=0
433 // \pre valid_range: (i>=0) && (i<d->GetNumberOfArrays())
InitWithCellData(vtkCellData * d,int i)434 void vtkBridgeAttribute::InitWithCellData(vtkCellData *d,
435                                           int i)
436 {
437   assert("pre: d_exists" && d!=0);
438   assert("pre: valid_range" && (i>=0) && (i<d->GetNumberOfArrays()));
439   vtkSetObjectBodyMacro(Pd,vtkPointData,0);
440   vtkSetObjectBodyMacro(Cd,vtkCellData,d);
441   this->Data=d;
442   this->AttributeNumber=i;
443   this->AllocateInternalTuple(this->GetNumberOfComponents());
444 }
445 
446 //-----------------------------------------------------------------------------
447 // Description:
448 // Default constructor: empty attribute, not valid
vtkBridgeAttribute()449 vtkBridgeAttribute::vtkBridgeAttribute()
450 {
451   this->Pd=0;
452   this->Cd=0;
453   this->Data=0;
454   this->AttributeNumber=0;
455   this->InternalTuple=0;
456   this->InternalTupleCapacity=0;
457 }
458 
459 //-----------------------------------------------------------------------------
460 // Description:
461 // Destructor.
~vtkBridgeAttribute()462 vtkBridgeAttribute::~vtkBridgeAttribute()
463 {
464   if(this->Pd!=0)
465     {
466     this->Pd->Delete();
467     }
468   else
469     {
470     if(this->Cd!=0)
471       {
472       this->Cd->Delete();
473       }
474     }
475   if(this->InternalTuple!=0)
476     {
477     delete[] this->InternalTuple;
478     }
479 }
480 
481 //-----------------------------------------------------------------------------
482 // Description:
483 // If size>InternalTupleCapacity, allocate enough memory.
484 // \pre positive_size: size>0
AllocateInternalTuple(int size)485 void vtkBridgeAttribute::AllocateInternalTuple(int size)
486 {
487   // size=this->GetNumberOfComponents()
488   assert("pre: positive_size" && size>0);
489 
490   if(this->InternalTuple==0)
491     {
492     this->InternalTupleCapacity = size;
493     this->InternalTuple = new double[this->InternalTupleCapacity];
494     }
495   else
496     {
497     if(InternalTupleCapacity<size)
498       {
499       this->InternalTupleCapacity = size;
500       delete [] this->InternalTuple;
501       this->InternalTuple = new double[this->InternalTupleCapacity];
502       }
503     }
504 }
505