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