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