Developer documentation | Axl-2.5.1

axlActorFieldDiscrete.cpp
Go to the documentation of this file.
1 /* axlActorFieldDiscrete.cpp ---
2  *
3  * Author: Thibaud Kloczko
4  * Copyright (C) 2008 - Thibaud Kloczko, Inria.
5  * Created: Mon Dec 6 20:20:58 2010 (+0100)
6  * Version: $Id$
7  * Last-Updated: Mon Dec 17 15:01:58 2012 (+0100)
8  * By: Julien Wintz
9  * Update #: 715
10  */
11 
12 /* Commentary:
13  *
14  */
15 
16 /* Change log:
17  *
18  */
19 
21 #include "axlActorFieldDiscrete.h"
22 #include "axlActor.h"
23 
26 
27 #include <dtkCoreSupport/dtkGlobal.h>
28 
29 #include <vtkActor.h>
30 #include <vtkArrowSource.h>
31 #include <vtkCommand.h>
32 #include <vtkContourFilter.h>
33 #include <vtkDataSetMapper.h>
34 #include <vtkCellData.h>
35 #include <vtkGlyph3D.h>
36 #include <vtkHedgeHog.h>
37 #include <vtkObjectFactory.h>
38 #include <vtkPointData.h>
39 #include <vtkPolyDataMapper.h>
40 #include <vtkRenderWindowInteractor.h>
41 #include <vtkSmartPointer.h>
42 #include <vtkSphere.h>
43 #include <vtkSphereRepresentation.h>
44 #include <vtkSphereSource.h>
45 #include <vtkSphereWidget2.h>
46 #include <vtkStreamTracer.h>
47 #include <vtkTubeFilter.h>
48 #include <vtkPolyData.h>
49 #include <vtkUnstructuredGrid.h>
50 
51 #include <vtkExtractUnstructuredGrid.h>
52 
53 #include "axlActorDataDynamic.h"
55 
56 #include <vtkIntArray.h>
57 #include <vtkFloatArray.h>
58 #include <vtkDoubleArray.h>
59 #include <vtkDataArray.h>
60 
61 // /////////////////////////////////////////////////////////////////
62 // axlActorFieldDiscreteStreamObserver
63 // /////////////////////////////////////////////////////////////////
64 
65 class axlActorFieldDiscreteStreamObserver : public vtkCommand
66 {
67 public:
69  {
71  }
72 
73  virtual void Execute(vtkObject *caller, unsigned long event, void *)
74  {
75  // if(event == vtkCommand::InteractionEvent) {
76 
77  // vector_stream_widget_representation->GetPolyData(vector_stream_widget_data);
78  //#if (VTK_MAJOR_VERSION <= 5)
79  // vector_stream_tracer->SetSource(vector_stream_widget_data);
80  //#else
81  // vector_stream_tracer->SetSourceData(vector_stream_widget_data);
82  //#endif
83  // vector_stream_tracer->Update();
84  // }
85  }
86 
87  vtkSmartPointer<vtkSphereRepresentation> vector_stream_widget_representation;
88  vtkSmartPointer<vtkPolyData> vector_stream_widget_data;
89  vtkSmartPointer<vtkStreamTracer> vector_stream_tracer;
90 };
91 
92 // /////////////////////////////////////////////////////////////////
93 // axlActorFieldDiscretePrivate
94 // /////////////////////////////////////////////////////////////////
95 
96 class axlActorFieldDiscretePrivate
97 {
98 public:
100  axlActor *mesh;
101  vtkDataArray *array;
102 
103 public:
104  bool scalar_display_as_iso;
105  int scalar_iso_count;
106  double scalar_iso_range[2];
107  vtkSmartPointer<vtkTubeFilter> scalar_iso_tube_filter;
108  double isoRadius;
109  vtkSmartPointer<vtkContourFilter> scalar_iso;
110  vtkSmartPointer<vtkPolyDataMapper> scalar_iso_mapper;
111  vtkSmartPointer<vtkActor> scalar_iso_actor;
112 
113  vtkSmartPointer<vtkPolyDataMapper> scalar_iso_color_mapper;
114  vtkSmartPointer<vtkActor> scalar_iso_color_actor;
115 
116 
117  vtkSmartPointer<vtkScalarBarActor> scalar_bar_actor;
118 
119  double scalar_color_range[2];
120  vtkSmartPointer<vtkDataSetMapper> scalar_color_mapper;
121  vtkSmartPointer<vtkActor> scalar_color_actor;
122 
123  vtkSmartPointer<vtkHedgeHog> vector_hedgehog;
124  vtkSmartPointer<vtkPolyDataMapper> vector_hedgehog_mapper;
125  vtkSmartPointer<vtkActor> vector_hedgehog_actor;
126 
127  vtkSmartPointer<vtkArrowSource> vector_glyph_source;
128  vtkSmartPointer<vtkGlyph3D> vector_glyph;
129  vtkSmartPointer<vtkPolyDataMapper> vector_glyph_mapper;
130  vtkSmartPointer<vtkActor> vector_glyph_actor;
131 
132  vtkSmartPointer<vtkSphereWidget2> vector_stream_widget;
133  vtkSmartPointer<vtkSphereRepresentation> vector_stream_widget_representation;
134  vtkSmartPointer<vtkPolyData> vector_stream_widget_data;
135  vtkSmartPointer<vtkStreamTracer> vector_stream_tracer;
136  vtkSmartPointer<vtkTubeFilter> vector_stream_filter;
137  vtkSmartPointer<vtkPolyDataMapper> vector_stream_mapper;
138  vtkSmartPointer<vtkActor> vector_stream_actor;
139  vtkSmartPointer<axlActorFieldDiscreteStreamObserver> vector_stream_observer;
140 
141  vtkSmartPointer<vtkRenderWindowInteractor> interactor;
142 };
143 
144 // /////////////////////////////////////////////////////////////////
145 // axlActorFieldDiscrete
146 // /////////////////////////////////////////////////////////////////
147 
148 #if (VTK_MAJOR_VERSION <= 5)
149 vtkCxxRevisionMacro(axlActorFieldDiscrete, "$Revision: 0.0.1 $");
150 #endif
151 
153 
154 dtkAbstractData *axlActorFieldDiscrete::data(void)
155 {
156  return NULL;
157 }
158 
160 {
161  if(d->field){
162  return d->field;
163  }
164  return NULL;
165 }
166 
168 {
169  if(d->mesh->fields().contains(QString("%1 magnitude").arg(d->field->objectName())))
170  return NULL;
171 
172  QString field_magnitude_name = QString("%1 magnitude").arg(d->field->objectName());
173 
174  axlFieldDiscrete *field_magnitude = new axlFieldDiscrete(field_magnitude_name, d->field->type(), axlFieldDiscrete::Scalar, d->field->support(), d->field->size());
175 
176  double *tuple; for(int i = 0; i < d->field->size(); i++) {
177  tuple = d->field->vector(i);
178  field_magnitude->setScalar(i, qSqrt(tuple[0]*tuple[0]+tuple[0]*tuple[0]+tuple[0]*tuple[0]));
179  }
180  return field_magnitude;
181 }
182 
183 
184 
186 {
187  return d->mesh;
188 }
189 
190 
191 vtkScalarBarActor *axlActorFieldDiscrete::scalarBar(void){
192  return d->scalar_bar_actor;
193 }
194 
196 {
197  return d->scalar_color_range[0];
198 }
199 
201 {
202  return d->scalar_color_range[1];
203 }
204 
206 {
207  return d->scalar_iso->GetNumberOfContours();
208 }
209 
211 {
212  return d->scalar_iso_range[0];
213 }
214 
216 {
217  return d->scalar_iso_range[1];
218 }
219 
221 {
222  return d->vector_glyph->GetScaleFactor();
223 }
224 
226 {
227  return d->vector_stream_filter->GetRadius();
228 }
229 
231 {
232  d->interactor = static_cast<vtkRenderWindowInteractor *>(interactor);
233 
234  this->setup();
235 }
236 
237 
238 
240 
241 
242  if(d->field){
243  int size = 0;
244  if(d->mesh->getUnstructuredGrid()){
245  size = d->mesh->getUnstructuredGrid()->GetNumberOfPoints();
246  }else if(d->mesh->getPolyData()){
247  size = d->mesh->getPolyData()->GetNumberOfPoints();
248  }else{
249  if(dynamic_cast<axlActorDataDynamic *>(d->mesh)){
250  axlActorDataDynamic *actorDD = dynamic_cast<axlActorDataDynamic *>(d->mesh);
251  size = dynamic_cast<axlActor *>(actorDD->outputActor())->getMapper()->GetInput()->GetNumberOfPoints();
252  d->mesh->setPolyData(dynamic_cast<axlActor *>(actorDD->outputActor())->getMapper()->GetInput());
253  //d->mesh->setPoints(d->mesh->getPolyData()->GetPoints());
254  }else{
255  size = d->mesh->getMapper()->GetInput()->GetNumberOfPoints();
256  d->mesh->setPolyData(d->mesh->getMapper()->GetInput());
257  //d->mesh->setPoints(d->mesh->getPolyData()->GetPoints());
258  }
259 
260  }
261 
262  d->array->SetNumberOfTuples(size);
263 
264 
265  double *tuple1 = NULL;
266  double tuple2 = 0;
267  for(int i = 0; i < size; i++){
268  if(d->field->kind() == axlAbstractFieldDiscrete::Scalar){
269 
270  tuple2 = d->field->scalar(i);
271 
272  d->array->SetTuple1(i, tuple2);
273  d->array->Modified();
274  }
275  else if(d->field->kind() == axlAbstractFieldDiscrete::Vector){
276 
277 
278  tuple1 = d->field->vector(i);
279 
280  d->array->SetTuple(i, tuple1);
281  d->array->Modified();
282  }
283  else if(d->field->kind() == axlAbstractFieldDiscrete::Tensor){
284 
285  tuple1 = d->field->tensor(i);
286 
287  d->array->SetTuple(i, tuple1);
288  d->array->Modified();
289  }
290 
291  }
292  }
293 
294 
295  //sets the minimum and maximum values of the field.
296  d->field->setMin(this->minValue());
297  d->field->setMax(this->maxValue());
298 
299  //setRange
300  if(d->scalar_color_range && d->scalar_color_mapper){
301  this->setColRangeMin(d->field->minValue());
302  this->setColRangeMax(d->field->maxValue());
303  }
304 
305 }
306 
307 void axlActorFieldDiscrete::setData(dtkAbstractData *field)
308 {
309  d->field = dynamic_cast<axlAbstractFieldDiscrete *>(field);
310 
311  if(d->field){
312 
313 
314  axlAbstractData * parentData = dynamic_cast<axlAbstractData *>(d->field->parent());
315  if(parentData){
316  connect(parentData, SIGNAL(modifiedGeometry()), this, SLOT(update()));
317  }
318  axlAbstractData *axlData = dynamic_cast<axlAbstractData *>(d->mesh->data());
319  connect(axlData, SIGNAL(modifiedGeometry()), this, SLOT(update()));
320 
321  if(d->field->type() == axlAbstractFieldDiscrete::Int)
322  d->array = vtkIntArray::New();
323  else if(d->field->type() == axlAbstractFieldDiscrete::Float)
324  d->array = vtkFloatArray::New();
325  else if(d->field->type() == axlAbstractFieldDiscrete::Double)
326  d->array = vtkDoubleArray::New();
327 
328  axlAbstractFieldDiscrete::Kind kind = d->field->kind();
329 
330  switch(kind) {
332  d->array->SetNumberOfComponents(1);
333  break;
335  d->array->SetNumberOfComponents(3);
336  break;
338  d->array->SetNumberOfComponents(9);
339  break;
340  default:
341  qDebug() << "Unsupported field kind";
342  };
343 
344  QString name = d->field->objectName();
345  d->array->SetName(qPrintable(name));
346 
347 
348 
349  this->updateArray();
350  this->setup();
351  }
352 
353 }
354 
356 {
357  d->mesh = dynamic_cast<axlActor *>(actorfield);
358 
359  this->setup();
360 }
361 
363 {
364  d->scalar_color_range[0] = min;
365  d->scalar_color_mapper->SetScalarRange(d->scalar_color_range);
366  d->scalar_color_mapper->Update();
367 }
368 
370 {
371  d->scalar_color_range[1] = max;
372  d->scalar_color_mapper->SetScalarRange(d->scalar_color_range);
373  d->scalar_color_mapper->Update();
374 }
375 
377 {
378  d->scalar_iso_count = count;
379  d->scalar_iso->GenerateValues(d->scalar_iso_count, d->scalar_iso_range[0], d->scalar_iso_range[1]);
380  d->scalar_iso->Update();
381 
382  d->scalar_iso_tube_filter->Update();
383 }
384 
386 {
387  d->scalar_iso_range[0] = min;
388  d->scalar_iso->GenerateValues(d->scalar_iso_count, d->scalar_iso_range[0], d->scalar_iso_range[1]);
389  d->scalar_iso->Update();
390  d->scalar_iso_color_mapper->SetScalarRange(d->scalar_iso_range);
391  d->scalar_iso_color_mapper->Update();
392 
393  d->scalar_iso_tube_filter->Update();
394 }
395 
397 {
398  d->scalar_iso_range[1] = max;
399  d->scalar_iso->GenerateValues(d->scalar_iso_count, d->scalar_iso_range[0], d->scalar_iso_range[1]);
400  d->scalar_iso->Update();
401  d->scalar_iso_color_mapper->SetScalarRange(d->scalar_iso_range);
402  d->scalar_iso_color_mapper->Update();
403 
404  d->scalar_iso_tube_filter->Update();
405 }
406 
408 {
409  //compute size of controlPoints
410  double bounds[6]; d->mesh->GetBounds(bounds);
411  double side = qAbs(bounds[1]-bounds[0]);
412  side += qAbs(bounds[3]-bounds[2]);
413  side += qAbs(bounds[5]-bounds[4]);
414 
415  side /= 30;
416  d->vector_glyph->SetScaleFactor(scale * side);
417  d->vector_hedgehog->SetScaleFactor(scale * side * 0.1);
418 }
419 
421 {
422  d->vector_stream_tracer->SetMaximumPropagation(propagation);
423  d->vector_stream_tracer->Update();
424 }
425 
427 {
428  d->vector_stream_filter->SetRadius(radius);
429  d->vector_stream_filter->Update();
430 }
431 
433 {
434  switch (direction) {
435  case 0:
436  d->vector_stream_tracer->SetIntegrationDirectionToForward();
437  break;
438  case 1:
439  d->vector_stream_tracer->SetIntegrationDirectionToBackward();
440  break;
441  case 2:
442  d->vector_stream_tracer->SetIntegrationDirectionToBoth();
443  break;
444  default:
445  break;
446  }
447 
448  d->vector_stream_tracer->Update();
449 }
450 
452 {
453  if(d->mesh->getUnstructuredGrid()){
454  d->mesh->getDataSetMapper()->ScalarVisibilityOn();
455  }else{
456  d->mesh->getMapper()->ScalarVisibilityOn();
457  }
458 
459  if (d->field->kind() != axlFieldDiscrete::Scalar)
460  return;
461 
462  if(d->field->support() == axlFieldDiscrete::Point) {
463  if(d->mesh->getUnstructuredGrid()){
464  static_cast<vtkUnstructuredGrid *>(d->mesh->getUnstructuredGrid())->GetPointData()->SetActiveScalars(qPrintable(d->field->objectName()));
465  }else{
466  static_cast<vtkPolyData *>(d->mesh->getPolyData())->GetPointData()->SetActiveScalars(qPrintable(d->field->objectName()));
467  }
468  }
469  if(d->field->support() == axlFieldDiscrete::Cell)
470  static_cast<vtkPolyData *>(d->mesh->getPolyData())->GetCellData()->SetActiveScalars(qPrintable(d->field->objectName()));
471 
472 
473  d->scalar_color_actor->SetVisibility(1);
474  d->scalar_iso_actor->SetVisibility(0);
475  d->scalar_iso_color_actor->SetVisibility(0);
476  d->scalar_display_as_iso = false;
477  d->scalar_bar_actor->SetVisibility(1);
478 
479 }
480 
482 {
483  if(d->scalar_color_actor)
484  {
485  d->scalar_color_actor->SetVisibility(0);
486  d->scalar_iso_actor->SetVisibility(0);
487  d->scalar_iso_color_actor->SetVisibility(0);
488  d->scalar_display_as_iso = false;
489  d->scalar_bar_actor->SetVisibility(0);
490  }
491 
492  if(d->mesh->getUnstructuredGrid()){
493  d->mesh->getDataSetMapper()->ScalarVisibilityOn();
494  }
495  else{
496  d->mesh->getMapper()->ScalarVisibilityOn();
497  }
498 
499  if(d->field->support() == axlFieldDiscrete::Point) {
500  if(d->mesh->getUnstructuredGrid()){
501  static_cast<vtkUnstructuredGrid *>(d->mesh->getUnstructuredGrid())->GetPointData()->SetActiveScalars("mapperCollorArrayDefaultField");
502  }else{
503  static_cast<vtkPolyData *>(d->mesh->getPolyData())->GetPointData()->SetActiveScalars("mapperCollorArrayDefaultField");
504  }
505  }
506 }
507 
509 {
510  if (d->field->kind() != axlFieldDiscrete::Scalar)
511  return;
512 
513  if(d->field->support() == axlFieldDiscrete::Point) {
514  if(d->mesh->getUnstructuredGrid()){
515  static_cast<vtkUnstructuredGrid *>(d->mesh->getUnstructuredGrid())->GetPointData()->SetActiveScalars(qPrintable(d->field->objectName()));
516  }
517  else{
518  static_cast<vtkPolyData *>(d->mesh->getPolyData())->GetPointData()->SetActiveScalars(qPrintable(d->field->objectName()));
519  }
520  }
521  if(d->field->support() == axlFieldDiscrete::Cell)
522  static_cast<vtkPolyData *>(d->mesh->getPolyData())->GetCellData()->SetActiveScalars(qPrintable(d->field->objectName()));
523 
524  if (d->field->support() == axlFieldDiscrete::Point) {
525  if(d->mesh->getUnstructuredGrid()){
526  d->mesh->getDataSetMapper()->ScalarVisibilityOff();
527  }
528  else{
529  d->mesh->getMapper()->ScalarVisibilityOff();
530  }
531  }
532 
533  this->setIsoCount(d->scalar_iso_count);
534 
535  d->scalar_color_actor->SetVisibility(0);
536  d->scalar_iso_actor->SetVisibility(1);
537  d->scalar_iso_color_actor->SetVisibility(0);
538  d->scalar_display_as_iso = true;
539  d->scalar_bar_actor->SetVisibility(0);
540 }
541 
543 {
544  if (d->field->kind() == axlFieldDiscrete::Vector ) {
545 
546  d->vector_hedgehog_actor->SetVisibility(0);
547  d->vector_glyph_actor->SetVisibility(0);
548  d->vector_stream_actor->SetVisibility(0);
549  d->vector_stream_widget->Off();
550  }
551 }
552 
554 {
555  if (d->field->kind() == axlFieldDiscrete::Vector ) {
556 
557  d->vector_hedgehog_actor->SetVisibility(1);
558  d->vector_glyph_actor->SetVisibility(0);
559  d->vector_stream_actor->SetVisibility(0);
560  d->vector_stream_widget->Off();
561  }
562 }
563 
565 {
566  if (d->field->kind() == axlFieldDiscrete::Vector) {
567  d->vector_hedgehog_actor->SetVisibility(0);
568  d->vector_glyph_actor->SetVisibility(1);
569  d->vector_stream_actor->SetVisibility(0);
570  d->vector_stream_widget->Off();
571  }
572 }
573 
575 {
576  if (d->field->kind() == axlFieldDiscrete::Vector) {
577 
578  d->vector_hedgehog_actor->SetVisibility(0);
579  d->vector_glyph_actor->SetVisibility(0);
580  d->vector_stream_actor->SetVisibility(1);
581  d->vector_stream_widget->On();
582  }
583 }
584 
586 
588 {
589  if(!d->mesh)
590  return;
591 
592  if(d->mesh->getUnstructuredGrid()){
593  d->mesh->getUnstructuredGrid()->Modified();
594 #if (VTK_MAJOR_VERSION <= 5)
595  d->mesh->getUnstructuredGrid()->Update();
596 #endif
597  }else{
598  d->mesh->getPolyData()->Modified();
599 #if (VTK_MAJOR_VERSION <= 5)
600  d->mesh->getPolyData()->Update();
601 #endif
602  }
603 
604  if(!d->field )
605  return;
606 
607  //update the value of the field.
608  this->updateArray();
609 
610  if(d->field->support() == axlFieldDiscrete::Point) {
611  if(d->mesh->getUnstructuredGrid()){
612  static_cast<vtkUnstructuredGrid *>(d->mesh->getUnstructuredGrid())->GetPointData()->RemoveArray(static_cast<vtkDataArray *>(d->array)->GetName());
613  static_cast<vtkUnstructuredGrid *>(d->mesh->getUnstructuredGrid())->GetPointData()->AddArray(static_cast<vtkDataArray *>(d->array));
614  }else{
615  static_cast<vtkPolyData *>(d->mesh->getPolyData())->GetPointData()->RemoveArray(static_cast<vtkDataArray *>(d->array)->GetName());
616  static_cast<vtkPolyData *>(d->mesh->getPolyData())->GetPointData()->AddArray(static_cast<vtkDataArray *>(d->array));
617  }
618  if(d->field->kind() == axlFieldDiscrete::Scalar)
619  if(d->mesh->getUnstructuredGrid()){
620  static_cast<vtkUnstructuredGrid *>(d->mesh->getUnstructuredGrid())->GetPointData()->SetActiveScalars(static_cast<vtkDataArray *>(d->array)->GetName());
621  }else{
622  static_cast<vtkPolyData *>(d->mesh->getPolyData())->GetPointData()->SetActiveScalars(static_cast<vtkDataArray *>(d->array)->GetName());
623  }
624  else if (d->field->kind() == axlFieldDiscrete::Vector)
625  static_cast<vtkPolyData *>(d->mesh->getPolyData())->GetPointData()->SetActiveVectors(static_cast<vtkDataArray *>(d->array)->GetName());
626  else if (d->field->kind() == axlFieldDiscrete::Tensor)
627  static_cast<vtkPolyData *>(d->mesh->getPolyData())->GetPointData()->SetActiveTensors(static_cast<vtkDataArray *>(d->array)->GetName());
628 
629  }
630 
631 
632 
633  if(d->field->support() == axlFieldDiscrete::Cell) {
634  static_cast<vtkPolyData *>(d->mesh->getPolyData())->GetCellData()->RemoveArray(static_cast<vtkDataArray *>(d->array)->GetName());
635  static_cast<vtkPolyData *>(d->mesh->getPolyData())->GetCellData()->AddArray(static_cast<vtkDataArray *>(d->array));
636  if(d->field->kind() == axlFieldDiscrete::Scalar)
637  static_cast<vtkPolyData *>(d->mesh->getPolyData())->GetCellData()->SetActiveScalars(static_cast<vtkDataArray *>(d->array)->GetName());
638  else if (d->field->kind() == axlFieldDiscrete::Vector)
639  static_cast<vtkPolyData *>(d->mesh->getPolyData())->GetCellData()->SetActiveVectors(static_cast<vtkDataArray *>(d->array)->GetName());
640  else if (d->field->kind() == axlFieldDiscrete::Tensor)
641  static_cast<vtkPolyData *>(d->mesh->getPolyData())->GetCellData()->SetActiveTensors(static_cast<vtkDataArray *>(d->array)->GetName());
642 
643 
644  }
645 
646  emit updated();
647 
648 }
649 
651 {
652  if(d->mesh && d->field)
653  if(d->field->support() == axlFieldDiscrete::Point) {
654  if(d->field->kind() == axlFieldDiscrete::Scalar)
655  if(d->mesh->getUnstructuredGrid()){
656  static_cast<vtkUnstructuredGrid *>(d->mesh->getUnstructuredGrid())->GetPointData()->SetActiveScalars(qPrintable(d->field->objectName()));
657  }else{
658  static_cast<vtkPolyData *>(d->mesh->getPolyData())->GetPointData()->SetActiveScalars(qPrintable(d->field->objectName()));
659  }
660  else if(d->field->kind() == axlFieldDiscrete::Vector)
661  static_cast<vtkPolyData *>(d->mesh->getPolyData())->GetPointData()->SetActiveVectors(qPrintable(d->field->objectName()));
662  else if(d->field->kind() == axlFieldDiscrete::Tensor)
663  static_cast<vtkPolyData *>(d->mesh->getPolyData())->GetPointData()->SetActiveTensors(qPrintable(d->field->objectName()));
664  }
665 
666 
667  if(d->mesh && d->field)
668  if(d->field->support() == axlFieldDiscrete::Cell) {
669  if(d->field->kind() == axlFieldDiscrete::Scalar)
670  static_cast<vtkPolyData *>(d->mesh->getPolyData())->GetCellData()->SetActiveScalars(qPrintable(d->field->objectName()));
671  else if(d->field->kind() == axlFieldDiscrete::Vector)
672  static_cast<vtkPolyData *>(d->mesh->getPolyData())->GetCellData()->SetActiveVectors(qPrintable(d->field->objectName()));
673  else if(d->field->kind() == axlFieldDiscrete::Tensor)
674  static_cast<vtkPolyData *>(d->mesh->getPolyData())->GetCellData()->SetActiveTensors(qPrintable(d->field->objectName()));
675  }
676 
677 }
678 
680 {
681  return d->scalar_color_mapper;
682 }
683 
684 axlActorFieldDiscrete::axlActorFieldDiscrete(void) : axlAbstractActorField(), d(new axlActorFieldDiscretePrivate)
685 {
686  d->field = NULL;
687  d->mesh = NULL;
688  d->isoRadius = 0.01;
689 }
690 
692 {
693  delete d;
694 
695  d = NULL;
696 }
697 
699 {
700 
701  if(!d->mesh)
702  return;
703 
704  if(!d->field) {
705  qDebug() << DTK_PRETTY_FUNCTION << "No field.";
706  return;
707  }
708 
709 
710  if(!d->interactor)
711  return;
712 
713 
714  // -- Scalar field
715 
716 
717  if(d->field->kind() == axlFieldDiscrete::Scalar) {
718 
719  if(d->scalar_color_mapper && d->scalar_color_actor && d->scalar_iso_color_actor)
720  return;
721 
722  if(d->field->support() == axlFieldDiscrete::Point) {
723  //remove first eventual array of the same name
724  if(d->mesh->getPolyData()){
725  static_cast<vtkPolyData *>(d->mesh->getPolyData())->GetPointData()->RemoveArray(static_cast<vtkDataArray *>(d->array)->GetName());
726  static_cast<vtkPolyData *>(d->mesh->getPolyData())->GetPointData()->AddArray(static_cast<vtkDataArray *>(d->array));
727  static_cast<vtkPolyData *>(d->mesh->getPolyData())->GetPointData()->SetActiveScalars(static_cast<vtkDataArray *>(d->array)->GetName());
728  }
729  if(d->mesh->getUnstructuredGrid()){
730  static_cast<vtkUnstructuredGrid *>(d->mesh->getUnstructuredGrid())->GetPointData()->RemoveArray(static_cast<vtkDataArray *>(d->array)->GetName());
731  static_cast<vtkUnstructuredGrid *>(d->mesh->getUnstructuredGrid())->GetPointData()->AddArray(static_cast<vtkDataArray *>(d->array));
732  static_cast<vtkUnstructuredGrid *>(d->mesh->getUnstructuredGrid())->GetPointData()->SetActiveScalars(static_cast<vtkDataArray *>(d->array)->GetName());
733  }
734  }
735 
736  if(d->field->support() == axlFieldDiscrete::Cell) {
737 
738  static_cast<vtkPolyData *>(d->mesh->getPolyData())->GetCellData()->RemoveArray(static_cast<vtkDataArray *>(d->array)->GetName());
739  static_cast<vtkPolyData *>(d->mesh->getPolyData())->GetCellData()->AddArray(static_cast<vtkDataArray *>(d->array));
740  static_cast<vtkPolyData *>(d->mesh->getPolyData())->GetPointData()->SetActiveScalars(static_cast<vtkDataArray *>(d->array)->GetName());
741 
742  }
743 
744  // -- iso contours
745 
746  d->scalar_display_as_iso = false;
747  d->scalar_iso_count = 10;
748  static_cast<vtkDataArray *>(d->array)->GetRange(d->scalar_iso_range);
749 
750  d->scalar_iso = vtkSmartPointer<vtkContourFilter>::New();
751  if(d->mesh->getUnstructuredGrid()){
752 #if (VTK_MAJOR_VERSION <= 5)
753  d->scalar_iso->SetInput(static_cast<vtkUnstructuredGrid *>(d->mesh->getUnstructuredGrid()));
754 #else
755  d->scalar_iso->SetInputData(static_cast<vtkUnstructuredGrid *>(d->mesh->getUnstructuredGrid()));
756 #endif
757  }else{
758 #if (VTK_MAJOR_VERSION <= 5)
759  d->scalar_iso->SetInput(static_cast<vtkPolyData *>(d->mesh->getPolyData()));
760 #else
761  d->scalar_iso->SetInputData(static_cast<vtkPolyData *>(d->mesh->getPolyData()));
762 #endif
763  }
764  d->scalar_iso->GenerateValues(d->scalar_iso_count, d->scalar_iso_range[0], d->scalar_iso_range[1]);
765 
766  d->scalar_iso_tube_filter = vtkSmartPointer<vtkTubeFilter>::New();
767  d->scalar_iso_tube_filter->SetRadius(d->isoRadius);
768  d->scalar_iso_tube_filter->SetNumberOfSides(8);
769 #if (VTK_MAJOR_VERSION <= 5)
770  d->scalar_iso_tube_filter->SetInput(d->scalar_iso->GetOutput());
771 #else
772  d->scalar_iso_tube_filter->SetInputData(d->scalar_iso->GetOutput());
773 #endif
774  d->scalar_iso_mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
775 #if (VTK_MAJOR_VERSION <= 5)
776  d->scalar_iso_mapper->SetInput(d->scalar_iso_tube_filter->GetOutput());
777 #else
778  d->scalar_iso_mapper->SetInputData(d->scalar_iso_tube_filter->GetOutput());
779 #endif
780 
781  d->scalar_iso_actor = vtkSmartPointer<vtkActor>::New();
782  d->scalar_iso_actor->SetMapper(d->scalar_iso_mapper);
783  d->scalar_iso_actor->SetVisibility(0);
784 
785  this->AddPart(d->scalar_iso_actor);
786 
787  // -- iso color mapping
788 
789  d->scalar_iso_color_mapper = vtkPolyDataMapper::New();
790 #if (VTK_MAJOR_VERSION <= 5)
791  d->scalar_iso_color_mapper->SetInput(d->scalar_iso->GetOutput());
792 #else
793  d->scalar_iso_color_mapper->SetInputData(d->scalar_iso->GetOutput());
794 #endif
795  d->scalar_iso_color_mapper->SetColorModeToMapScalars();
796 
797  if(d->field->support() == axlFieldDiscrete::Point)
798  d->scalar_iso_color_mapper->SetScalarModeToUsePointData();
799 
800  if(d->field->support() == axlFieldDiscrete::Cell)
801  d->scalar_iso_color_mapper->SetScalarModeToUseCellData();
802 
803  d->scalar_iso_color_mapper->SelectColorArray(static_cast<vtkDataArray *>(d->array)->GetName());
804  d->scalar_iso_color_mapper->SetScalarVisibility(true);
805  d->scalar_iso_color_mapper->SetScalarRange(d->scalar_iso_range);
806 
807  d->scalar_iso_color_actor = vtkSmartPointer<vtkActor>::New();
808  d->scalar_iso_color_actor->SetMapper(d->scalar_iso_color_mapper);
809  d->scalar_iso_color_actor->SetVisibility(0);
810 
811  this->AddPart(d->scalar_iso_color_actor);
812 
813  // -- color mapping
814 
815  static_cast<vtkDataArray *>(d->array)->GetRange(d->scalar_color_range);
816 
817  d->scalar_color_mapper = vtkDataSetMapper::New();
818 #if (VTK_MAJOR_VERSION <= 5)
819  if(d->mesh->getUnstructuredGrid())
820  d->scalar_color_mapper->SetInput(static_cast<vtkUnstructuredGrid *>(d->mesh->getUnstructuredGrid()));
821  else
822  d->scalar_color_mapper->SetInput(static_cast<vtkPolyData *>(d->mesh->getPolyData()));
823 #else
824  if(d->mesh->getUnstructuredGrid())
825  d->scalar_color_mapper->SetInputData(static_cast<vtkUnstructuredGrid *>(d->mesh->getUnstructuredGrid()));
826  else
827  d->scalar_color_mapper->SetInputData(static_cast<vtkPolyData *>(d->mesh->getPolyData()));
828 #endif
829 
830  d->scalar_color_mapper->SetColorModeToMapScalars();
831 
832  if(d->field->support() == axlFieldDiscrete::Point)
833  d->scalar_color_mapper->SetScalarModeToUsePointData();
834 
835  if(d->field->support() == axlFieldDiscrete::Cell)
836  d->scalar_color_mapper->SetScalarModeToUseCellData();
837 
838  d->scalar_color_mapper->SelectColorArray(static_cast<vtkDataArray *>(d->array)->GetName());
839  d->scalar_color_mapper->SetScalarVisibility(true);
840  d->scalar_color_mapper->SetScalarRange(d->scalar_color_range);
841 
842  d->scalar_color_actor = vtkSmartPointer<vtkActor>::New();
843  d->scalar_color_actor->SetMapper(d->scalar_color_mapper);
844  d->scalar_color_actor->SetVisibility(0);
845 
846  this->AddPart(d->scalar_color_actor);
847 
848 
849 
851  d->scalar_bar_actor = vtkSmartPointer<vtkScalarBarActor>::New();
852  d->scalar_bar_actor ->SetLookupTable(d->scalar_color_mapper->GetLookupTable());
853  d->scalar_bar_actor ->SetTitle(qPrintable(d->field->name()));
854  d->scalar_bar_actor ->SetNumberOfLabels(4);
855  d->scalar_bar_actor->SetVisibility(0);
856 
857 
858 
859  }
860 
861  // -- Vector field
862 
863 
864  if(d->field->kind() == axlFieldDiscrete::Vector) {
865 
866  // -- Append field and its magnitude into vtkPolyData
867 
868  if(d->field->support() == axlFieldDiscrete::Point) {
869 
870  static_cast<vtkPolyData *>(d->mesh->getPolyData())->GetPointData()->RemoveArray(static_cast<vtkDataArray *>(d->array)->GetName());
871  static_cast<vtkPolyData *>(d->mesh->getPolyData())->GetPointData()->AddArray(static_cast<vtkDataArray *>(d->array));
872  static_cast<vtkPolyData *>(d->mesh->getPolyData())->GetPointData()->SetActiveVectors(static_cast<vtkDataArray *>(d->array)->GetName());
873 
874  }
875 
876  if(d->field->support() == axlFieldDiscrete::Cell) {
877  static_cast<vtkPolyData *>(d->mesh->getPolyData())->GetPointData()->RemoveArray(static_cast<vtkDataArray *>(d->array)->GetName());
878  static_cast<vtkPolyData *>(d->mesh->getPolyData())->GetCellData()->AddArray(static_cast<vtkDataArray *>(d->array));
879  static_cast<vtkPolyData *>(d->mesh->getPolyData())->GetCellData()->SetActiveVectors(static_cast<vtkDataArray *>(d->array)->GetName());
880 
881 
882  }
883 
884  // -- hedgehog
885 
886  if(! d->vector_hedgehog)
887  d->vector_hedgehog = vtkSmartPointer<vtkHedgeHog>::New();
888 
889 #if (VTK_MAJOR_VERSION <= 5)
890  d->vector_hedgehog->SetInput(static_cast<vtkPolyData *>(d->mesh->getPolyData()));
891 #else
892  d->vector_hedgehog->SetInputData(static_cast<vtkPolyData *>(d->mesh->getPolyData()));
893 #endif
894  d->vector_hedgehog->SetVectorModeToUseVector();
895  d->vector_hedgehog->SetScaleFactor(0.05);
896 
897  if(!d->vector_hedgehog_mapper) {
898  d->vector_hedgehog_mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
899  d->vector_hedgehog_mapper->SetInputConnection(d->vector_hedgehog->GetOutputPort());
900  d->vector_hedgehog_mapper->ScalarVisibilityOn();
901  }
902 
903  if(!d->vector_hedgehog_actor)
904  d->vector_hedgehog_actor = vtkSmartPointer<vtkActor>::New();
905  d->vector_hedgehog_actor->SetMapper(d->vector_hedgehog_mapper);
906  d->vector_hedgehog_actor->SetVisibility(0);
907 
908  this->AddPart(d->vector_hedgehog_actor);
909 
910  // -- glyphs
911 
912  if(!d->vector_glyph)
913  d->vector_glyph = vtkSmartPointer<vtkGlyph3D>::New();
914 
915  if(!d->vector_glyph_source)
916  d->vector_glyph_source = vtkArrowSource::New();
917 
918 
919 #if (VTK_MAJOR_VERSION <= 5)
920  d->vector_glyph->SetInput(static_cast<vtkPolyData *>(d->mesh->getPolyData()));
921  d->vector_glyph->SetSource(d->vector_glyph_source->GetOutput());
922 #else
923  d->vector_glyph->SetSourceData(d->vector_glyph_source->GetOutput());
924  d->vector_glyph->SetSourceConnection(d->vector_glyph_source->GetOutputPort());
925  d->vector_glyph->SetInputData(static_cast<vtkPolyData *>(d->mesh->getPolyData()));
926 #endif
927 
928  //d->vector_glyph->SetColorModeToColorByScale();
929  d->vector_glyph->SetColorModeToColorByVector();
930  d->vector_glyph->SetScaleModeToScaleByVector();
931  d->vector_glyph->SetVectorModeToUseVector();
932  d->vector_glyph->SetScaleModeToDataScalingOff();
933  //d->vector_glyph->ScalingOff();
934  //d->vector_glyph->SetScaleFactor(2);
935  this->setGlyphScale(2);
936  d->vector_glyph->OrientOn();
937 
938  if(!d->vector_glyph_mapper) {
939  d->vector_glyph_mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
940  d->vector_glyph_mapper->SetInputConnection(d->vector_glyph->GetOutputPort());
941  d->vector_glyph_mapper->ScalarVisibilityOn();
942  }
943 
944 
945  if(!d->vector_glyph_actor){
946  d->vector_glyph_actor = vtkSmartPointer<vtkActor>::New();
947  d->vector_glyph_actor->SetMapper(d->vector_glyph_mapper);
948  d->vector_glyph_actor->SetVisibility(0);
949  }
950 
951  this->AddPart(d->vector_glyph_actor);
952 
953  // -- streams
954 
955  d->vector_stream_widget = vtkSmartPointer<vtkSphereWidget2>::New();
956  d->vector_stream_widget->SetInteractor(d->interactor);
957  d->vector_stream_widget->CreateDefaultRepresentation();
958  d->vector_stream_widget->SetTranslationEnabled(true);
959  d->vector_stream_widget->SetScalingEnabled(true);
960  d->vector_stream_widget->Off();
961 
962  d->vector_stream_widget_data = vtkPolyData::New();
963 
964  d->vector_stream_widget_representation = vtkSphereRepresentation::SafeDownCast(d->vector_stream_widget->GetRepresentation());
965  d->vector_stream_widget_representation->HandleVisibilityOff();
966  d->vector_stream_widget_representation->HandleTextOff();
967  d->vector_stream_widget_representation->RadialLineOff();
968  d->vector_stream_widget_representation->SetPhiResolution(64);
969  d->vector_stream_widget_representation->SetThetaResolution(64);
970  d->vector_stream_widget_representation->GetPolyData(d->vector_stream_widget_data);
971 
972  d->vector_stream_tracer = vtkStreamTracer::New();
973 #if (VTK_MAJOR_VERSION <= 5)
974  d->vector_stream_tracer->SetInput(static_cast<vtkPolyData *>(d->mesh->getPolyData()));
975  d->vector_stream_tracer->SetSource(d->vector_stream_widget_data);
976 #else
977  d->vector_stream_tracer->SetInputData(static_cast<vtkPolyData *>(d->mesh->getPolyData()));
978  d->vector_stream_tracer->SetSourceData(d->vector_stream_widget_data);
979 #endif
980  d->vector_stream_tracer->SetMaximumPropagation(100);
981  d->vector_stream_tracer->SetMinimumIntegrationStep(1.0e-4);
982  d->vector_stream_tracer->SetMaximumIntegrationStep(100.0);
983  d->vector_stream_tracer->SetIntegrationDirectionToBoth();
984 
985  d->vector_stream_filter = vtkTubeFilter::New();
986 #if (VTK_MAJOR_VERSION <= 5)
987  d->vector_stream_filter->SetInput(d->vector_stream_tracer->GetOutput());
988 #else
989  d->vector_stream_filter->SetInputData(d->vector_stream_tracer->GetOutput());
990 #endif
991  d->vector_stream_filter->SetRadius(0.01);
992  d->vector_stream_filter->SetNumberOfSides(8);
993 
994  d->vector_stream_mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
995  d->vector_stream_mapper->SetInputConnection(d->vector_stream_filter->GetOutputPort());
996  d->vector_stream_mapper->ScalarVisibilityOn();
997 
998  d->vector_stream_actor = vtkSmartPointer<vtkActor>::New();
999  d->vector_stream_actor->SetMapper(d->vector_stream_mapper);
1000  d->vector_stream_actor->SetVisibility(0);
1001 
1002  d->vector_stream_observer = axlActorFieldDiscreteStreamObserver::New();
1003  d->vector_stream_observer->vector_stream_widget_representation = d->vector_stream_widget_representation;
1004  d->vector_stream_observer->vector_stream_widget_data = d->vector_stream_widget_data;
1005  d->vector_stream_observer->vector_stream_tracer = d->vector_stream_tracer;
1006  d->vector_stream_widget->AddObserver(vtkCommand::InteractionEvent, d->vector_stream_observer);
1007 
1008  this->AddPart(d->vector_stream_actor);
1009  }
1010 
1011 }
1012 
1014 {
1015  double field_range[2];static_cast<vtkDataArray *>(d->array)->GetRange(field_range);
1016  return field_range[0];
1017 }
1018 
1020 {
1021  double field_range[2];static_cast<vtkDataArray *>(d->array)->GetRange(field_range);
1022 
1023  return field_range[1];
1024 }
1025 
1027 {
1028  d->isoRadius = radius;
1029  d->scalar_iso_tube_filter->SetRadius(radius);
1030  d->scalar_iso_tube_filter->Update();
1031 }
void setStreamDirection(int direction)
vtkSmartPointer< vtkSphereRepresentation > vector_stream_widget_representation
void setGlyphScale(double scale)
vtkSmartPointer< vtkStreamTracer > vector_stream_tracer
void setInteractor(void *interactor)
axlAbstractActor * actorField(void)
Class axlAbstractFieldDiscrete defines an API for arrays of numeric data.
axlAbstractField * field(void)
axlAbstractField * magnitude(void)
static axlActorFieldDiscreteStreamObserver * New(void)
void updated(void)
axlAbstractActor * outputActor(void)
void setStreamPropagation(double propagation)
virtual void setScalar(int index, double v1)
virtual void setData(dtkAbstractData *field)
void setActorField(axlAbstractActor *actor)
Class axlAbstractField defines an API for arrays of numeric data.
Class axlFieldDiscrete defines an API for arrays of numeric data.
void setStreamRadius(double radius)
vtkCxxRevisionMacro(axlActorFieldDiscrete,"$Revision: 0.0.1 $")
vtkScalarBarActor * scalarBar(void)
vtkStandardNewMacro(axlActorFieldDiscrete)
vtkSmartPointer< vtkPolyData > vector_stream_widget_data
virtual void Execute(vtkObject *caller, unsigned long event, void *)
dtkAbstractData * data(void)
Class axlAbstractData defines an API for all type of axel data.
void onIsoRadiusChanged(double radius)