]> SALOME platform Git repositories - modules/visu.git/blob - src/VISUGUI/VisuGUI_Selection.cxx
Salome HOME
This commit was generated by cvs2git to track changes on a CVS vendor
[modules/visu.git] / src / VISUGUI / VisuGUI_Selection.cxx
1 using namespace std;
2 //  File      : VisuGUI_Selection.cxx
3 //  Created   : Wed Aug 01 10:23:06 2001
4 //  Author    : Laurent CORNABE & Hubert ROLLAND 
5 //  Project   : SALOME
6 //  Module    : VISUGUI
7 //  Copyright : PRINCIPIA
8 //  $Header$
9
10 #include "utilities.h"
11 #include <TColStd_MapOfInteger.hxx>
12 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
13 #include "SALOME_ListIteratorOfListIO.hxx"
14
15 #include "SALOME_Selection.h"
16 #include "VISU_Actor.h"
17
18 #include <qstring.h>
19 #include <qfiledialog.h>
20 #include <qapplication.h>
21 #include <qcolordialog.h>
22 #include <qlabel.h>
23 #include <qspinbox.h>
24 #include <qlistbox.h>
25
26 #include <vtkDataSetMapper.h>
27 #include <vtkDataSetReader.h>
28 #include <vtkPointPicker.h>
29 #include <vtkLookupTable.h>
30 #include <vtkPolyDataSource.h>
31 #include <vtkDataSet.h>
32 #include <vtkCell.h>
33 #include <vtkMaskPoints.h>
34 #include <vtkUnstructuredGrid.h>
35
36 #include "VisuGUI_CellsResultsDlg.h"
37 #include "VisuGUI_PointsResultsDlg.h"
38 #include "VisuGUI_Selection.h"
39
40    int mode=0;
41    int scal=0;
42    int vect=0;
43    char Msg[80]="";
44        
45    int id=0;
46    char Msg_1[150]="";
47    char Msg_2[150]="";
48    float Coord[3];
49    float ScalarValue=0;
50    float VectorValue[3];
51                                 
52 int VisuGUI_Selection::PickViewer(QAD_Study *ActiveStudy, int mode)
53 {
54   SALOME_Selection *Sel = SALOME_Selection::Selection(ActiveStudy->getSelection());
55   SALOME_ListIteratorOfListIO It(Sel->StoredIObjects());
56   for(;It.More();It.Next())
57   {
58       Handle(SALOME_InteractiveObject)IObject = It.Value();
59       if (Sel->SelectionMode() == mode ) // Selection MODE
60       {
61            if (Sel->HasIndex(IObject))
62            {
63              TColStd_MapOfInteger MapIndex ;
64              Sel->GetIndex(IObject, MapIndex) ;
65              TColStd_MapIteratorOfMapOfInteger ite(MapIndex);
66                 for (;ite.More(); ite.Next())
67                 {
68                       MESSAGE("Cell ID =" << ite.Key())
69                       return (ite.Key());
70                 }
71            }
72       }
73   }
74   return (-1);
75 }
76
77 void VisuGUI_Selection::PickingResults(QAD_Study *ActiveStudy, int mode, VISU_Actor *Actor)
78 {
79                
80   if ( (mode == SelectionCell) || (mode == SelectionEdge) ) // Cell mode
81   {
82           PickingCell(ActiveStudy, Actor);
83   }
84
85   else if (mode == SelectionPoint)                          // Point Mode
86   {
87           PickingPoint(ActiveStudy, Actor);
88   }
89 }
90
91 // ============================================================================================ //
92 //                                      PICKING CELL                                            //
93 // ============================================================================================ //
94
95 void VisuGUI_Selection::PickingCell(QAD_Study *ActiveStudy, VISU_Actor *Actor)
96 {
97   vtkDataSet *Data ;
98   vtkScalars *Scalar;
99   vtkVectors *Vector;
100   vtkCell *Cell;
101   vtkIdList *PointList;
102   vtkCellData *CellData;
103   vtkPointData *PointData;
104   vtkPoints *Points = vtkPoints::New();
105   float Point_Coord[12][3];
106   float CoordCurrent[3];
107   int idCurrent;
108   
109   mode = SelectionCell;
110
111   vtkActor* anActor = vtkActor::SafeDownCast( Actor );
112   id=PickViewer(ActiveStudy,mode);
113   if (id >= 0)   // Cell exist
114   {
115         VisuGUI_CellsResultsDlg *ResultsDlg = new VisuGUI_CellsResultsDlg(QAD_Application::getDesktop(), 0, TRUE, 0); 
116
117         vtkDataSetMapper *Mapper = (vtkDataSetMapper*) (anActor->GetMapper());
118         Mapper->SetInput(anActor->GetMapper()->GetInput());
119         Data=Mapper->GetInput();
120         Cell=Data->GetCell(id);
121         CellData=Data->GetCellData();
122         int NbOfPoints=Cell->GetNumberOfPoints();
123         PointList=Cell->GetPointIds();
124         QListViewItem* item = 0;
125         for(int i=0; i<NbOfPoints; i++)
126         {
127                 idCurrent = PointList->GetId(i);
128                 Data->GetPoint(idCurrent,CoordCurrent);
129                 for (int j=0; j<3; j++)
130                         Point_Coord[i][j]=CoordCurrent[j];
131                 item = new QListViewItem(ResultsDlg->ListPoints, 
132                                          item, 
133                                          QString::number( idCurrent ),
134                                          QString::number( Point_Coord[i][0] ),
135                                          QString::number( Point_Coord[i][1] ),
136                                          QString::number( Point_Coord[i][2] ));
137         }
138                 
139         // Scalar result
140         Scalar=CellData->GetScalars();
141         if (Scalar!=NULL)
142         {
143                 scal = 1;
144                 ScalarValue=Scalar->GetScalar(id);
145                 sprintf(Msg_1,"( %s ) %4f",Actor->FieldName,ScalarValue);
146         }
147         else
148         {
149                 scal = 2;
150                 sprintf(Msg_1,"No scalar on Cell");
151         }
152         
153         // Vector result
154         Vector=CellData->GetVectors();
155         if (Vector!=NULL)
156         {
157                 vect = 1;
158                 Vector->GetVector(id,VectorValue);
159                 sprintf(Msg_2,"( %s ) %.4f %.4f %.4f",Actor->FieldName,VectorValue[0],VectorValue[1],VectorValue[2]);
160         }
161         else
162         {
163                 vect = 2;
164                 sprintf(Msg_2,"No Vector on Cell");
165         }
166
167         ResultsDlg->IdCell->setText( QString::number( id ) );
168         ResultsDlg->NbPoint->setText( QString::number( NbOfPoints ) );
169         ResultsDlg->ScalValue->setText( Msg_1 );
170         ResultsDlg->VectTxt->setText( Msg_2 );
171         ResultsDlg->exec(); 
172         delete ResultsDlg;
173   }
174 }
175
176
177 // ============================================================================================ //
178 //                                      PICKING POINT                                           //
179 // ============================================================================================ //
180
181 void VisuGUI_Selection::PickingPoint(QAD_Study *ActiveStudy, VISU_Actor *Actor)
182 {
183   vtkDataSet *Data ;
184   vtkScalars *Scalar;
185   vtkVectors *Vector;
186   vtkPointData *PointData;
187
188   mode = SelectionPoint;
189
190   id=PickViewer(ActiveStudy,mode);
191   if (id >= 0)
192   {
193         VisuGUI_PointsResultsDlg *ResultsDlg = new VisuGUI_PointsResultsDlg(QAD_Application::getDesktop(), 0, TRUE, 0); 
194         vtkDataSetMapper *Mapper = (vtkDataSetMapper*) (Actor->GetMapper());
195         Data=Mapper->GetInput();
196         PointData=Data->GetPointData();
197
198         Data->GetPoint(id,Coord);
199         ResultsDlg->CoordX->setText( QString::number( Coord[0] ) );
200         ResultsDlg->CoordY->setText( QString::number( Coord[1] ) );
201         ResultsDlg->CoordZ->setText( QString::number( Coord[2] ) );
202 //      ActiveStudy->setMessage(Msg);
203
204         Scalar=PointData->GetScalars();
205         if (Scalar!=NULL)
206         {
207                 scal = 1;
208                 ScalarValue=Scalar->GetScalar(id);
209                 sprintf(Msg_1,"( %s ) %4f",Actor->FieldName,ScalarValue);
210         }
211         else
212         {
213                 scal = 2;
214                 sprintf(Msg_1,"No scalar at Point");
215         }
216 //      ActiveStudy->setMessage(Msg_1);
217         
218         // Vector result
219         Vector=PointData->GetVectors();
220         if (Vector!=NULL)
221         {
222                 vect = 1;
223                 Vector->GetVector(id,VectorValue);
224                 sprintf(Msg_2,"( %s ) %.4f %.4f %.4f",Actor->FieldName,VectorValue[0],VectorValue[1],VectorValue[2]);
225         }
226         else
227         {
228                 vect = 2;
229                 sprintf(Msg_2,"No Vector at Point");
230         }
231         ResultsDlg->IdPoint->setText( QString::number(id) );
232         ResultsDlg->ScalValue->setText(Msg_1);
233         ResultsDlg->VectTxt->setText(Msg_2);
234         ResultsDlg->exec(); 
235         delete ResultsDlg;
236         
237 //      ActiveStudy->setMessage( Msg_2 ) ;
238   }
239 }
240
241 // ============================================================================================ //
242 //                                      HIGHLIGHT CELL                                          //
243 // ============================================================================================ //
244 //
245 void VisuGUI_Selection::HighlightCell(int idCell, VISU_Actor *Actor, vtkRenderer *ren) 
246 {
247   MESSAGE("HighlightCell")
248   vtkActor *Cell_Actor = vtkActor::New();
249
250   vtkUnstructuredGrid* Cell_UGrid = vtkUnstructuredGrid::New();
251   if ( Actor->GetMapper()->GetInput()->IsA("vtkUnstructuredGrid") ) {
252   vtkUnstructuredGrid* UGrid = vtkUnstructuredGrid::SafeDownCast( Actor->GetMapper()->GetInput() );
253 //  if (UGrid != NULL) // LCO 1707
254 //  {
255           vtkIdList *ptIds = vtkIdList::New(); ptIds->Allocate(12);
256           vtkDataSetMapper* Cell_Mapper = vtkDataSetMapper::New();
257           
258           Cell_UGrid->SetPoints( UGrid->GetPoints() );
259           UGrid->GetCellPoints( idCell, ptIds );
260           Cell_UGrid->InsertNextCell(Actor->GetMapper()->GetInput()->GetCellType( idCell ), ptIds);
261                                                                                            
262           Cell_Mapper->SetInput(Cell_UGrid);
263           Cell_Actor->SetMapper(Cell_Mapper);
264           
265           ren->AddActor( Cell_Actor );
266   
267           Cell_Actor->GetProperty()->SetColor(1,1,0);
268           Cell_Actor->GetProperty()->SetRepresentationToSurface();
269           Cell_Actor->VisibilityOn();
270   
271           ren->Render();                                     
272   } // LCO
273   Cell_Actor->Delete();
274 }                                     
275
276 // ============================================================================================ //
277 //                                      HIGHLIGHT POINT                                         //
278 // ============================================================================================ //
279 //
280 void VisuGUI_Selection::HighlightPoint(int idPoint, int nbPoints, VISU_Actor *Actor, vtkRenderer *ren) 
281 {
282   vtkActor *Point_Actor = vtkActor::New();
283    
284   MESSAGE("HighlightPoint")
285   if ( Actor->GetMapper()->GetInput()->IsA("vtkUnstructuredGrid") ) {
286   vtkUnstructuredGrid* UGrid = vtkUnstructuredGrid::SafeDownCast( Actor->GetMapper()->GetInput() );
287 //   if (UGrid != NULL) // LCO 1707
288 //  {
289         vtkUnstructuredGrid* Point_UGrid = vtkUnstructuredGrid::New();
290         float pts[3];
291                                        
292           vtkPoints *Points = vtkPoints::New();
293           vtkMaskPoints* verts = vtkMaskPoints::New();
294           vtkPolyDataMapper* vertMapper = vtkPolyDataMapper::New();
295           Points->SetNumberOfPoints(nbPoints);
296                                                                            
297           UGrid->GetPoint( idPoint, pts) ;
298         //  Points->SetPoint(idPoint, 0., 0., 0. );
299           Points->SetPoint(idPoint, pts);
300           Point_UGrid->SetPoints(Points);
301           verts->SetInput(Point_UGrid);
302           verts->SetGenerateVertices(1);
303           verts->SetOnRatio(1);
304           vertMapper->SetInput(verts->GetOutput());
305           Point_Actor->SetMapper(vertMapper);
306           ren->AddActor( Point_Actor );
307           Point_Actor->GetProperty()->SetColor(1,1,0);
308           Point_Actor->GetProperty()->SetRepresentationToSurface();
309           Point_Actor->VisibilityOn();
310           ren->Render();
311   } //LCO
312   Point_Actor->Delete();
313 }                                     
314
315 // ============================================================================================ //
316 //                               Function to return results individual                          //
317 // ============================================================================================ //
318
319 int VisuGUI_Selection::getId()
320 {
321         return (id);
322 }
323
324 char *VisuGUI_Selection::getMsg(int nb)
325 {
326         if (nb==1)
327                 return (Msg_1);
328         else
329                 return (Msg_2);
330 }
331
332 float *VisuGUI_Selection::getCoord()
333 {
334         return (Coord);
335 }
336  
337 float VisuGUI_Selection::getScalar()
338 {
339         if (scal == 1)
340                 return (ScalarValue);
341         else
342                 return (0);
343 }
344
345 float *VisuGUI_Selection::getVector()
346 {
347         if (vect == 1)
348                 return (VectorValue);
349         else
350                 return (0);
351 }
352
353 int VisuGUI_Selection::getMode()
354 {
355         return (mode    );
356 }