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