]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/QT/SimpleMPRWidget.cxx
More plugins added
[cpPlugins.git] / lib / cpExtensions / QT / SimpleMPRWidget.cxx
1 #include <cpExtensions/QT/SimpleMPRWidget.h>
2
3 #ifdef cpExtensions_QT4
4
5 #include <cpExtensions/ui_SimpleMPRWidget.h>
6
7 #include <vtkProperty.h>
8 #include <vtkRendererCollection.h>
9
10 double cpExtensions::QT::SimpleMPRWidget::
11 cm_Colors[ 8 ][ 3 ] =
12 {
13   { 1.0, 0.0, 0.0 },
14   { 0.0, 1.0, 0.0 },
15   { 0.0, 0.0, 1.0 },
16   { 0.0, 1.0, 1.0 },
17   { 1.0, 0.0, 1.0 },
18   { 1.0, 1.0, 0.0 },
19   { 1.0, 0.5, 0.0 },
20   { 1.0, 0.0, 0.5 }
21 };
22
23 // -------------------------------------------------------------------------
24 cpExtensions::QT::SimpleMPRWidget::
25 SimpleMPRWidget( QWidget* parent )
26   : QWidget( parent ),
27     m_UI( new Ui::SimpleMPRWidget ),
28     m_MainImage( "" )
29 {
30   this->m_UI->setupUi( this );
31
32   // Configure VTK widgets
33   this->m_VTK[ 0 ] = this->m_UI->VTK01;
34   this->m_VTK[ 1 ] = this->m_UI->VTK00;
35   this->m_VTK[ 2 ] = this->m_UI->VTK10;
36   this->m_VTK[ 3 ] = this->m_UI->VTK11;
37
38   this->m_MPRObjects = vtkSmartPointer< TMPRObjects >::New( );
39   this->m_MPRObjects->SetRenderWindows(
40     this->m_VTK[ 0 ]->GetRenderWindow( ),
41     this->m_VTK[ 1 ]->GetRenderWindow( ),
42     this->m_VTK[ 2 ]->GetRenderWindow( ),
43     this->m_VTK[ 3 ]->GetRenderWindow( )
44     );
45
46   // Connect slots
47   QObject::connect(
48     this->m_UI->TopSplitter, SIGNAL( splitterMoved( int, int ) ),
49     this, SLOT( _SyncBottom( int, int ) )
50     );
51   QObject::connect(
52     this->m_UI->BottomSplitter, SIGNAL( splitterMoved( int, int ) ),
53     this, SLOT( _SyncTop( int, int ) )
54     );
55 }
56
57 // -------------------------------------------------------------------------
58 cpExtensions::QT::SimpleMPRWidget::
59 ~SimpleMPRWidget( )
60 {
61   delete this->m_UI;
62 }
63
64 // -------------------------------------------------------------------------
65 unsigned int cpExtensions::QT::SimpleMPRWidget::
66 GetNumberOfData( ) const
67 {
68   return( this->m_Data.size( ) );
69 }
70
71 // -------------------------------------------------------------------------
72 bool cpExtensions::QT::SimpleMPRWidget::
73 AddData(
74   vtkImageData* data, const std::string& name,
75   const std::string& parent
76   )
77 {
78   if( name == "" )
79     return( false );
80
81   auto iIt = this->m_Data.find( name );
82   if( iIt == this->m_Data.end( ) )
83   {
84     if( parent != "" )
85     {
86       auto pIt = this->m_Data.find( parent );
87       if( pIt == this->m_Data.end( ) )
88         return( false );
89
90     } // fi
91
92     // Add new data
93     this->m_Data[ name ].SetImageData( data );
94
95     // Set color
96     auto iIt = this->m_Data.find( name );
97     if( iIt != this->m_Data.end( ) )
98     {
99       vtkActor* actor =
100         dynamic_cast< vtkActor* >( iIt->second.GetMeshActor( ) );
101       if( actor != NULL )
102       {
103         unsigned int idx = this->m_Data.size( ) % 8;
104         actor->GetProperty( )->SetColor(
105           Self::cm_Colors[ idx ][ 0 ],
106           Self::cm_Colors[ idx ][ 1 ],
107           Self::cm_Colors[ idx ][ 2 ]
108           );
109
110       } // fi
111
112     } // fi
113
114     // Add to tree view
115     // TODO: this->_UpdateTreeItem( name, parent );
116     return( true );
117   }
118   else
119     return( false );
120 }
121
122 // -------------------------------------------------------------------------
123 bool cpExtensions::QT::SimpleMPRWidget::
124 AddData( vtkPolyData* data, const std::string& name )
125 {
126   if( name == "" )
127     return( false );
128
129   auto iIt = this->m_Data.find( name );
130   if( iIt == this->m_Data.end( ) )
131   {
132     // Add new data
133     this->m_Data[ name ].SetPolyData( data );
134
135     // Set color
136     auto iIt = this->m_Data.find( name );
137     if( iIt != this->m_Data.end( ) )
138     {
139       vtkActor* actor =
140         dynamic_cast< vtkActor* >( iIt->second.GetMeshActor( ) );
141       if( actor != NULL )
142       {
143         unsigned int idx = this->m_Data.size( ) % 8;
144         actor->GetProperty( )->SetColor(
145           Self::cm_Colors[ idx ][ 0 ],
146           Self::cm_Colors[ idx ][ 1 ],
147           Self::cm_Colors[ idx ][ 2 ]
148           );
149
150       } // fi
151
152     } // fi
153
154     // Add to tree view
155     // TODO: this->_UpdateTreeItem( name, parent );
156     return( true );
157   }
158   else
159     return( false );
160 }
161
162 // -------------------------------------------------------------------------
163 const std::string& cpExtensions::QT::SimpleMPRWidget::
164 GetMainImage( ) const
165 {
166   return( this->m_MainImage );
167 }
168
169 // -------------------------------------------------------------------------
170 bool cpExtensions::QT::SimpleMPRWidget::
171 SetMainImage( const std::string& name )
172 {
173   auto iIt = this->m_Data.find( name );
174   if( iIt != this->m_Data.end( ) )
175   {
176     if( iIt->second.Tag == Data::IMAGE )
177     {
178       this->m_MainImage = name;
179       return( true );
180     }
181     else
182       return( false );
183   }
184   else
185     return( false );
186 }
187
188 // -------------------------------------------------------------------------
189 void cpExtensions::QT::SimpleMPRWidget::
190 DeleteData( const std::string& name )
191 {
192   auto iIt = this->m_Data.find( name );
193   if( iIt != this->m_Data.end( ) )
194   {
195     this->m_Data.erase( iIt );
196
197     // Get children
198     std::vector< std::string > to_erase;
199     auto tIt = this->m_Tree.begin( );
200     for( ; tIt != this->m_Tree.end( ); ++tIt )
201       if( tIt->second == name )
202         to_erase.push_back( tIt->first );
203
204     // Delete from tree
205     tIt = this->m_Tree.find( name );
206     if( tIt != this->m_Tree.end( ) )
207       this->m_Tree.erase( tIt );
208
209     // Recursive erase
210     auto dIt = to_erase.begin( );
211     for( ; dIt != to_erase.end( ); ++dIt )
212       this->DeleteData( *dIt );
213
214     // Delete from tree widget
215     /* TODO
216        QTreeWidgetItem* item = this->_FindItemInTree( name );
217        if( item != NULL )
218        this->m_UI->LoadedData->removeItemWidget( item, 0 );
219     */
220
221     // Reset main image, just in case
222     if( this->m_Data.size( ) == 0 )
223       this->m_MainImage = "";
224
225   } // fi
226 }
227
228 // -------------------------------------------------------------------------
229 void cpExtensions::QT::SimpleMPRWidget::
230 DeleteAllData( )
231 {
232   this->m_MPRObjects->Clear( );
233   this->m_Data.clear( );
234   this->m_Tree.clear( );
235   // TODO: this->m_UI->LoadedData->clear( );
236   this->m_MainImage = "";
237 }
238
239 // -------------------------------------------------------------------------
240 void cpExtensions::QT::SimpleMPRWidget::
241 SetDataColor(
242   const std::string& name, const double& r, const double& g, const double& b
243   )
244 {
245   /*
246     auto iIt = this->m_Data.find( name );
247     if( iIt == this->m_Data.end( ) )
248     return;
249
250     if( iIt->second.Tag == Data::IMAGE )
251     {
252     }
253     else if( iIt->second.Tag == Data::MESH )
254     {
255     } // fi
256   */
257 }
258
259 // -------------------------------------------------------------------------
260 void cpExtensions::QT::SimpleMPRWidget::
261 ShowData( const std::string& name )
262 {
263   auto iIt = this->m_Data.find( name );
264   if( iIt == this->m_Data.end( ) )
265     return;
266
267   if( iIt->second.Tag == Data::IMAGE )
268   {
269     if( name == this->m_MainImage )
270       this->m_MPRObjects->SetInputImage( iIt->second.Image );
271     else
272     {
273       unsigned int i = ( this->m_MPRObjects->GetNumberOfImages( ) - 1 ) % 8;
274       this->m_MPRObjects->AddBinaryImage(
275         iIt->second.Image,
276         Self::cm_Colors[ i ][ 0 ],
277         Self::cm_Colors[ i ][ 1 ],
278         Self::cm_Colors[ i ][ 2 ]
279         );
280     }
281     this->m_MPRObjects->Show( );
282   }
283   else if( iIt->second.Tag == Data::MESH )
284   {
285     vtkRenderer* ren =
286       this->m_VTK[ 3 ]->GetRenderWindow( )->
287       GetRenderers( )->GetFirstRenderer( );
288     if( ren == NULL )
289       return;
290     ren->AddActor( iIt->second.GetMeshActor( ) );
291     this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
292
293   } // fi
294 }
295
296 // -------------------------------------------------------------------------
297 void cpExtensions::QT::SimpleMPRWidget::
298 HideData( const std::string& name )
299 {
300 }
301
302 // -------------------------------------------------------------------------
303 void cpExtensions::QT::SimpleMPRWidget::
304 SetWindowLevel( const double& w, const double& l )
305 {
306   this->m_MPRObjects->SetWindowLevel( w, l );
307 }
308
309 // -------------------------------------------------------------------------
310 double cpExtensions::QT::SimpleMPRWidget::
311 GetWindow( ) const
312 {
313   return( this->m_MPRObjects->GetWindow( ) );
314 }
315
316 // -------------------------------------------------------------------------
317 double cpExtensions::QT::SimpleMPRWidget::
318 GetLevel( ) const
319 {
320   return( this->m_MPRObjects->GetLevel( ) );
321 }
322
323 // -------------------------------------------------------------------------
324 vtkRenderWindowInteractor* cpExtensions::QT::SimpleMPRWidget::
325 GetInteractor( unsigned int i )
326 {
327   if( i < 4 )
328   {
329     if( this->m_VTK[ i ] != NULL )
330       return( this->m_VTK[ i ]->GetInteractor( ) );
331     else
332       return( NULL );
333   }
334   else
335     return( NULL );
336 }
337
338 // -------------------------------------------------------------------------
339 /*
340   bool cpExtensions::QT::SimpleMPRWidget::
341   ShowImage(
342   vtkImageData* image,
343   const std::string& name,
344   const std::string& parent
345   )
346   {
347   // Update tree view
348   QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
349   if( new_item == NULL )
350   return( false );
351
352   // Associate new data
353   this->m_Images[ name ] = image;
354   this->m_Tree[ name ] = parent;
355
356   // Show image and return
357   this->m_MPRObjects->AddImage( image );
358   return( true );
359   }
360
361   // -------------------------------------------------------------------------
362   bool cpExtensions::QT::SimpleMPRWidget::
363   ShowImage(
364   vtkImageData* image,
365   const std::string& name,
366   const std::string& parent,
367   const double& r, const double& g, const double& b
368   )
369   {
370   // Update tree view
371   QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
372   if( new_item == NULL )
373   return( false );
374
375   // Associate new data
376   this->m_Images[ name ] = image;
377   this->m_Tree[ name ] = parent;
378
379   // Show image and return
380   this->m_MPRObjects->AddImage( image );
381   return( true );
382   }
383
384   // -------------------------------------------------------------------------
385   bool cpExtensions::QT::SimpleMPRWidget::
386   ShowMesh(
387   vtkPolyData* mesh,
388   const std::string& name,
389   const std::string& parent
390   )
391   {
392   // Update tree view
393   QTreeWidgetItem* new_item = this->_UpdateTreeItem( name, parent );
394   if( new_item == NULL )
395   return( false );
396
397   // Associate new data
398   PolyDataActor* actor = new PolyDataActor( mesh );
399   this->m_Meshes[ name ] = actor;
400   this->m_Tree[ name ] = parent;
401
402   // Show mesh
403   this->_Add3DActor( actor->Actor );
404   return( true );
405   }
406
407   // -------------------------------------------------------------------------
408   bool cpExtensions::QT::SimpleMPRWidget::
409   ShowMesh(
410   vtkPolyData* mesh,
411   const std::string& name,
412   const std::string& parent,
413   const double& r, const double& g, const double& b
414   )
415   {
416   return false;
417   }
418
419   // -------------------------------------------------------------------------
420   void cpExtensions::QT::SimpleMPRWidget::
421   ClearAll( )
422   {
423   this->m_MPRObjects->ClearAll( );
424   this->m_Images.clear( );
425   this->m_Meshes.clear( );
426   }
427 */
428
429 // -------------------------------------------------------------------------
430 std::string cpExtensions::QT::SimpleMPRWidget::
431 GetSelectedData( ) const
432 {
433   /* TODO
434      QTreeWidgetItem* item = this->m_UI->LoadedData->currentItem( );
435      if( item != NULL )
436      return( item->text( 0 ).toStdString( ) );
437      else
438   */
439   return( "" );
440 }
441
442 // -------------------------------------------------------------------------
443 /* TODO
444 QTreeWidgetItem* cpExtensions::QT::SimpleMPRWidget::
445 _FindItemInTree( const std::string& name ) const
446 {
447   QList< QTreeWidgetItem* > items =
448     this->m_UI->LoadedData->findItems(
449       name.c_str( ), Qt::MatchExactly | Qt::MatchRecursive
450       );
451   if( items.size( ) > 0 )
452     return( items[ 0 ] );
453   else
454     return( NULL );
455 }
456
457 // -------------------------------------------------------------------------
458 QTreeWidgetItem* cpExtensions::QT::SimpleMPRWidget::
459 _UpdateTreeItem( const std::string& name, const std::string& parent )
460 {
461   // Update tree view
462   QTreeWidgetItem* new_item = NULL;
463   if( parent != "" )
464   {
465     QTreeWidgetItem* parent_item = this->_FindItemInTree( parent );
466     if( parent_item != NULL )
467     {
468       QTreeWidgetItem* old_item = this->_FindItemInTree( name );
469       if( old_item == NULL )
470       {
471         new_item =
472           new QTreeWidgetItem( parent_item, QStringList( name.c_str( ) ) );
473         parent_item->setExpanded( true );
474
475       } // fi
476
477     } // fi
478   }
479   else
480   {
481     new_item = new QTreeWidgetItem(
482       ( QTreeWidgetItem* )( NULL ), QStringList( name.c_str( ) )
483       );
484     this->m_UI->LoadedData->addTopLevelItem( new_item );
485
486   } // fi
487   return( new_item );
488 }
489 */
490
491 // -------------------------------------------------------------------------
492 /*
493   void cpExtensions::QT::SimpleMPRWidget::
494   _Add3DActor( vtkProp3D* prop )
495   {
496   vtkRenderer* ren =
497   this->m_VTK[ 3 ]->GetRenderWindow( )->GetRenderers( )->GetFirstRenderer( );
498   if( ren == NULL )
499   return;
500   ren->AddActor( prop );
501   this->m_VTK[ 3 ]->GetRenderWindow( )->Render( );
502   }
503 */
504
505 // -------------------------------------------------------------------------
506 void cpExtensions::QT::SimpleMPRWidget::
507 _SyncBottom( int a, int b )
508 {
509   this->m_UI->BottomSplitter->setSizes( this->m_UI->TopSplitter->sizes( ) );
510 }
511
512 // -------------------------------------------------------------------------
513 void cpExtensions::QT::SimpleMPRWidget::
514 _SyncTop( int a, int b )
515 {
516   this->m_UI->TopSplitter->setSizes( this->m_UI->BottomSplitter->sizes( ) );
517 }
518
519 // -------------------------------------------------------------------------
520 cpExtensions::QT::SimpleMPRWidget::PolyDataActor::
521 PolyDataActor( )
522   : Mesh( NULL ),
523     Normals( NULL ),
524     Stripper( NULL ),
525     Mapper( NULL ),
526     Actor( NULL )
527 {
528 }
529
530 // -------------------------------------------------------------------------
531 cpExtensions::QT::SimpleMPRWidget::PolyDataActor::
532 ~PolyDataActor( )
533 {
534   if( this->Actor != NULL )    this->Actor->Delete( );
535   if( this->Mapper != NULL )   this->Mapper->Delete( );
536   if( this->Stripper != NULL ) this->Stripper->Delete( );
537   if( this->Normals != NULL )  this->Normals->Delete( );
538 }
539
540 // -------------------------------------------------------------------------
541 void cpExtensions::QT::SimpleMPRWidget::PolyDataActor::
542 Configure( vtkPolyData* pd )
543 {
544   if( pd ==  NULL )
545     return;
546
547   double range[ 2 ];
548   pd->GetScalarRange( range );
549
550   this->Normals = vtkPolyDataNormals::New( );
551   this->Stripper = vtkStripper::New( );
552   this->Mapper = vtkPolyDataMapper::New( );
553   this->Actor = vtkQuadricLODActor::New( );
554
555   this->Mesh = pd;
556   this->Normals->SetInputData( pd );
557   this->Normals->SetFeatureAngle( 60.0 );
558   this->Stripper->SetInputConnection( this->Normals->GetOutputPort( ) );
559   this->Mapper->SetInputConnection( this->Stripper->GetOutputPort( ) );
560   this->Mapper->UseLookupTableScalarRangeOff( );
561   this->Mapper->SetScalarRange(
562     range[ 0 ], ( ( range[ 1 ] - range[ 0 ] ) * 0.75 ) + range[ 0 ]
563     );
564   this->Actor->SetMapper( this->Mapper );
565   this->Actor->GetProperty( )->SetPointSize( 10 );
566   this->Actor->DeferLODConstructionOff( );
567 }
568
569 // -------------------------------------------------------------------------
570 cpExtensions::QT::SimpleMPRWidget::Data::
571 Data( )
572 {
573   this->Tag = Data::IMAGE;
574   this->Image = NULL;
575 }
576
577 // -------------------------------------------------------------------------
578 cpExtensions::QT::SimpleMPRWidget::Data::
579 ~Data( )
580 {
581 }
582
583 // -------------------------------------------------------------------------
584 cpExtensions::QT::SimpleMPRWidget::
585 Data& cpExtensions::QT::SimpleMPRWidget::Data::
586 operator=( const Data& data )
587 {
588   this->Tag = data.Tag;
589   if( this->Tag == Data::IMAGE )
590     this->Image = data.Image;
591   else if( this->Tag == Data::MESH )
592     this->Mesh = data.Mesh;
593   return( *this );
594 }
595
596 // -------------------------------------------------------------------------
597 void cpExtensions::QT::SimpleMPRWidget::Data::
598 SetImageData( vtkImageData* data )
599 {
600   this->Tag = Data::IMAGE;
601   this->Image = data;
602 }
603
604 // -------------------------------------------------------------------------
605 void cpExtensions::QT::SimpleMPRWidget::Data::
606 SetPolyData( vtkPolyData* data )
607 {
608   this->Tag = Data::MESH;
609   this->Mesh.Configure( data );
610 }
611
612 // -------------------------------------------------------------------------
613 vtkImageData* cpExtensions::QT::SimpleMPRWidget::Data::
614 GetImage( )
615 {
616   if( this->Tag == Data::IMAGE )
617     return( this->Image );
618   else
619     return( NULL );
620 }
621
622 // -------------------------------------------------------------------------
623 vtkPolyData* cpExtensions::QT::SimpleMPRWidget::Data::
624 GetMesh( )
625 {
626   if( this->Tag == Data::MESH )
627     return( this->Mesh.Mesh );
628   else
629     return( NULL );
630 }
631
632 // -------------------------------------------------------------------------
633 vtkProp* cpExtensions::QT::SimpleMPRWidget::Data::
634 GetMeshActor( )
635 {
636   if( this->Tag == Data::MESH )
637     return( this->Mesh.Actor );
638   else
639     return( NULL );
640 }
641
642 #endif // cpExtensions_QT4
643
644 // eof - $RCSfile$