]> SALOME platform Git repositories - modules/geom.git/blob - src/OBJECT/GEOM_Actor.cxx
Salome HOME
Implementation of the "0021239: EDF 1829 OCC: Bring to front selected objects" issue.
[modules/geom.git] / src / OBJECT / GEOM_Actor.cxx
1 // Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  GEOM OBJECT : interactive object for Geometry entities visualization
24 //  File   : GEOM_Actor.cxx
25 //  Author : Christophe ATTANASIO
26 //  Module : GEOM
27 //  $Header$
28 //
29 /*!
30   \class GEOM_Actor GEOM_Actor.h
31   \brief This class allows to display an OpenCASCADE CAD model in a VTK viewer.
32 */
33 #include "GEOM_Actor.h" 
34  
35 #include "GEOM_DeviceActor.h" 
36 #include "GEOM_VertexSource.h" 
37 #include "GEOM_EdgeSource.h" 
38 #include "GEOM_WireframeFace.h" 
39 #include "GEOM_ShadingFace.h"
40 #include "GEOM_PainterPolyDataMapper.h"
41 #include "SVTK_Actor.h"
42
43 #include <OCC2VTK_Tools.h>
44
45 #include <vtkObjectFactory.h> 
46 #include <vtkRenderer.h> 
47 #include <vtkProperty.h> 
48 #include <vtkPointPicker.h>
49 #include <vtkCellPicker.h>
50  
51 #include <TopAbs_ShapeEnum.hxx>
52 #include <TopExp_Explorer.hxx>
53 #include <TopoDS.hxx>
54 #include <BRep_Tool.hxx>
55 #include <TopExp.hxx>
56  
57 #include <vtkPolyDataWriter.h> 
58  
59 #include <vtkAppendPolyData.h>  
60 #include <vtkPolyDataMapper.h>  
61 #include <vtkPolyData.h>  
62 #include <vtkTransform.h>
63 #include <vtkMatrix4x4.h>
64 #include <vtkMath.h>
65 #include <vtkCamera.h>
66
67 #include "utilities.h"
68
69 #include "SALOME_InteractiveObject.hxx"
70
71 //vtkStandardNewMacro(GEOM_Actor);
72
73 #ifndef MYDEBUG
74 //#define MYDEBUG
75 #endif
76
77 GEOM_Actor::GEOM_Actor(): 
78   isOnlyVertex(false),
79
80   myDeflection(-1),
81   myIsForced(false),
82
83   //  myDisplayMode(eWireframe), 
84   myIsSelected(false), 
85   myVectorMode(false),
86
87   myVertexActor(GEOM_DeviceActor::New(),true), 
88   myVertexSource(GEOM_VertexSource::New(),true), 
89  
90   myIsolatedEdgeActor(GEOM_DeviceActor::New(),true), 
91   myIsolatedEdgeSource(GEOM_EdgeSource::New(),true), 
92  
93   myOneFaceEdgeActor(GEOM_DeviceActor::New(),true), 
94   myOneFaceEdgeSource(GEOM_EdgeSource::New(),true), 
95  
96   mySharedEdgeActor(GEOM_DeviceActor::New(),true), 
97   mySharedEdgeSource(GEOM_EdgeSource::New(),true), 
98  
99   myWireframeFaceActor(GEOM_DeviceActor::New(),true), 
100   myWireframeFaceSource(GEOM_WireframeFace::New(),true), 
101  
102   myShadingFaceActor(GEOM_DeviceActor::New(),true), 
103   myShadingFaceSource(GEOM_ShadingFace::New(),true), 
104  
105   myHighlightActor(GEOM_DeviceActor::New(),true), 
106   myAppendFilter(vtkAppendPolyData::New(),true), 
107   // Use mapper as an instance of GEOM_PainterPolyDataMapper class
108   // to prevent drawing of mappers' content (due to an empty definition
109   // of GEOM_PainterPolyDataMapper::RenderPiece(...)).
110   // !!! Presentation of GEOM_Actor is drawing only with help of actors
111   // defined in this class !!!
112   myPolyDataMapper(GEOM_PainterPolyDataMapper::New(),true),
113
114   myHighlightProp(vtkProperty::New()),
115   myPreHighlightProp(vtkProperty::New()),
116   myShadingFaceProp(vtkProperty::New()),
117   myShadingBackFaceProp(vtkProperty::New())
118
119 #ifdef MYDEBUG
120   MESSAGE (this<< " GEOM_Actor::GEOM_Actor");
121 #endif
122
123   myPolyDataMapper->SetInput(myAppendFilter->GetOutput()); 
124   vtkProperty* aProperty; 
125
126   myHighlightProp->SetAmbient(0.5);
127   myHighlightProp->SetDiffuse(0.3);
128   myHighlightProp->SetSpecular(0.2);
129   myHighlightProp->SetRepresentationToSurface();
130   myHighlightProp->SetAmbientColor(1, 1, 1);
131   myHighlightProp->SetDiffuseColor(1, 1, 1);
132   myHighlightProp->SetSpecularColor(0.5, 0.5, 0.5);
133   myHighlightProp->SetPointSize(SALOME_POINT_SIZE);
134   myHighlightActor->SetProperty(myHighlightProp.GetPointer());
135
136   this->myHighlightActor->SetInput(myAppendFilter->GetOutput(),false);
137
138   myPreHighlightProp->SetColor(0,1,1);
139   myPreHighlightProp->SetPointSize(SALOME_POINT_SIZE+2);
140   myPreHighlightProp->SetLineWidth(SALOME_LINE_WIDTH+1);
141   myPreHighlightProp->SetRepresentationToWireframe();
142
143   myAppendFilter->AddInput(myVertexSource->GetOutput()); 
144   myVertexActor->SetInput(myVertexSource->GetOutput(),false); 
145   aProperty = myVertexActor->GetProperty(); 
146   aProperty->SetRepresentation(VTK_POINTS); 
147   aProperty->SetPointSize(3); 
148   aProperty->SetColor(1, 1, 0);
149  
150   myAppendFilter->AddInput(myIsolatedEdgeSource->GetOutput());
151   myIsolatedEdgeActor->SetInput(myIsolatedEdgeSource->GetOutput(),false); 
152   aProperty = myIsolatedEdgeActor->GetProperty(); 
153   aProperty->SetRepresentation(VTK_WIREFRAME); 
154   aProperty->SetColor(1, 0, 0);
155  
156   myAppendFilter->AddInput(myOneFaceEdgeSource->GetOutput());
157   myOneFaceEdgeActor->SetInput(myOneFaceEdgeSource->GetOutput(),false); 
158   aProperty = myOneFaceEdgeActor->GetProperty(); 
159   aProperty->SetRepresentation(VTK_WIREFRAME); 
160   aProperty->SetColor(0, 1, 0);
161  
162   myAppendFilter->AddInput(mySharedEdgeSource->GetOutput()); 
163   mySharedEdgeActor->SetInput(mySharedEdgeSource->GetOutput(),false); 
164   aProperty = mySharedEdgeActor->GetProperty(); 
165   aProperty->SetRepresentation(VTK_WIREFRAME); 
166   aProperty->SetColor(1, 1, 0);
167  
168   myAppendFilter->AddInput(myWireframeFaceSource->GetOutput()); 
169   myWireframeFaceActor->SetInput(myWireframeFaceSource->GetOutput(),false); 
170   aProperty = myWireframeFaceActor->GetProperty(); 
171   aProperty->SetRepresentation(VTK_WIREFRAME); 
172   aProperty->SetColor(0.5, 0.5, 0.5);
173
174   myShadingFaceActor->SetInput(myShadingFaceSource->GetOutput(),true); 
175
176   myShadingFaceProp->SetRepresentation(VTKViewer::Representation::Surface); 
177   myShadingFaceProp->SetInterpolationToGouraud(); 
178   myShadingFaceProp->SetAmbient(1.0);
179   myShadingFaceProp->SetDiffuse(1.0);
180   myShadingFaceProp->SetSpecular(0.4);
181   myShadingFaceProp->SetAmbientColor(0.329412, 0.223529, 0.027451);
182   myShadingFaceProp->SetDiffuseColor(0.780392, 0.568627, 0.113725);
183   myShadingFaceProp->SetSpecularColor(0.992157, 0.941176, 0.807843);
184
185   myShadingFaceActor->SetProperty(myShadingFaceProp.GetPointer());
186
187   StoreBoundaryColors();
188
189   myNbIsos[0] = -1;
190   myNbIsos[1] = -1;
191
192   // Toggle display mode 
193   setDisplayMode(0); // WIRE FRAME
194   SetVectorMode(0);  //
195
196  
197  
198 GEOM_Actor::~GEOM_Actor() 
199
200 #ifdef MYDEBUG
201   MESSAGE (this<< " ~GEOM_Actor::GEOM_Actor");
202 #endif
203   myHighlightProp->Delete();
204   myPreHighlightProp->Delete();
205   myShadingFaceProp->Delete();
206   myShadingBackFaceProp->Delete();
207
208  
209 GEOM_Actor*  
210 GEOM_Actor:: 
211 New() 
212
213   GEOM_Actor* anObject = new GEOM_Actor(); 
214   anObject->SetMapper(anObject->myPolyDataMapper.Get()); 
215   return anObject; 
216
217  
218  
219 void Write(vtkPolyData* theDataSet, const char* theFileName){ 
220   vtkPolyDataWriter* aWriter = vtkPolyDataWriter::New(); 
221   MESSAGE ("Write - "<<theFileName<<"' : "<<theDataSet->GetNumberOfPoints()<<"; "<<theDataSet->GetNumberOfCells()); 
222   aWriter->SetInput(theDataSet); 
223   aWriter->SetFileName(theFileName); 
224   //aWriter->Write(); 
225   aWriter->Delete(); 
226
227  
228 void 
229 GEOM_Actor:: 
230 SetModified() 
231
232   this->myVertexSource->Modified(); 
233   this->myIsolatedEdgeSource->Modified(); 
234   this->myOneFaceEdgeSource->Modified(); 
235   this->mySharedEdgeSource->Modified(); 
236   this->myWireframeFaceSource->Modified(); 
237   this->myShadingFaceSource->Modified(); 
238
239
240 void  
241 GEOM_Actor:: 
242 SetMapper(vtkMapper* theMapper) 
243
244   SALOME_Actor::SetMapper(theMapper); 
245
246
247 void 
248 GEOM_Actor:: 
249 AddToRender(vtkRenderer* theRenderer)
250 {
251   //SALOME_Actor::AddToRender(theRenderer);
252   
253   theRenderer->AddActor(this); 
254  
255   this->myHighlightActor->AddToRender(theRenderer); 
256   
257
258   myShadingFaceActor->AddToRender(theRenderer); 
259   myWireframeFaceActor->AddToRender(theRenderer); 
260  
261   mySharedEdgeActor->AddToRender(theRenderer); 
262   myOneFaceEdgeActor->AddToRender(theRenderer); 
263   myIsolatedEdgeActor->AddToRender(theRenderer); 
264  
265   myVertexActor->AddToRender(theRenderer); 
266 }
267  
268 void 
269 GEOM_Actor:: 
270 RemoveFromRender(vtkRenderer* theRenderer)
271 {
272   //SALOME_Actor::RemoveFromRender(theRenderer);
273
274   
275   theRenderer->RemoveActor(this);
276
277   myHighlightActor->RemoveFromRender(theRenderer); 
278   myShadingFaceActor->RemoveFromRender(theRenderer); 
279   myWireframeFaceActor->RemoveFromRender(theRenderer); 
280  
281   mySharedEdgeActor->RemoveFromRender(theRenderer); 
282   myOneFaceEdgeActor->RemoveFromRender(theRenderer); 
283   myIsolatedEdgeActor->RemoveFromRender(theRenderer); 
284  
285   myVertexActor->RemoveFromRender(theRenderer);
286
287   
288   SetSelected(false);
289   SetVisibility(false);
290 }
291
292 void  
293 GEOM_Actor:: 
294 setDisplayMode(int theMode) 
295
296 #ifdef MYDEBUG
297   MESSAGE ( "GEOM_Actor::setDisplayMode = "<<theMode );
298 #endif
299   
300   if ( theMode == (int)eWireframe ) {
301     RestoreIsoNumbers();
302     // Restore wireframe edges colors
303     RestoreBoundaryColors();
304   }
305   else if ( theMode == (int)eShading || theMode == (int)eShadingWithEdges ) {
306     // Temporary store number of iso lines in order to recover its later 
307     // when display mode is achnged to 'Wirefame' or 'Shading'.
308     // Iso lines are not displayed in 'Shading with edges' mode.
309     StoreIsoNumbers();
310
311     // Reset number of iso lines to 0
312     ResetIsoNumbers();
313
314     if ( theMode == (int)eShadingWithEdges ) {
315       // Store wireframe edges colors
316       StoreBoundaryColors();
317
318       // Coloring edges
319       myIsolatedEdgeActor->GetProperty()->SetColor(myEdgesInShadingColor[0],
320                                                    myEdgesInShadingColor[1],
321                                                    myEdgesInShadingColor[2]);
322       myOneFaceEdgeActor->GetProperty()->SetColor(myEdgesInShadingColor[0],
323                                                   myEdgesInShadingColor[1],
324                                                   myEdgesInShadingColor[2]);
325       mySharedEdgeActor->GetProperty()->SetColor(myEdgesInShadingColor[0],
326                                                  myEdgesInShadingColor[1],
327                                                  myEdgesInShadingColor[2]);
328       //SetModified();
329     } else {
330       myIsolatedEdgeActor->GetProperty()->SetColor(myEdgesInWireframeColor[0],
331                                                    myEdgesInWireframeColor[1],
332                                                    myEdgesInWireframeColor[2]);
333     }
334   }
335
336   VTKViewer_Actor::setDisplayMode(theMode);
337   SetVisibility(GetVisibility()); 
338
339
340 void  
341 GEOM_Actor:: 
342 SetSelected(bool theIsSelected) 
343
344 #ifdef MYDEBUG
345   MESSAGE ( "GEOM_Actor::SetSelected = "<<theIsSelected  );
346 #endif
347
348   myIsSelected = theIsSelected; 
349   SetVisibility(GetVisibility()); 
350
351
352 void  
353 GEOM_Actor:: 
354 SetVisibility(int theVisibility) 
355
356 #ifdef MYDEBUG
357   MESSAGE ( "GEOM_Actor::SetVisibility = "<<theVisibility <<"  myIsSelected="<< myIsSelected
358             << " theVisibility="<<theVisibility<<" myIsPreselected="<<myIsPreselected );
359 #endif
360
361   SALOME_Actor::SetVisibility(theVisibility);
362
363   this->myHighlightActor->SetVisibility(theVisibility && (myIsSelected || myIsPreselected));
364   
365   myShadingFaceActor->SetVisibility(theVisibility && (myDisplayMode == (int)eShading || myDisplayMode == (int)eShadingWithEdges) && (!myIsSelected || !myIsPreselected)); 
366   myWireframeFaceActor->SetVisibility(theVisibility && (myDisplayMode == (int)eWireframe) && !myIsSelected);
367
368   mySharedEdgeActor->SetVisibility(theVisibility && (myDisplayMode == (int)eWireframe || myDisplayMode == (int)eShadingWithEdges) && !myIsSelected);
369   myOneFaceEdgeActor->SetVisibility(theVisibility && (myDisplayMode == (int)eWireframe || myDisplayMode == (int)eShadingWithEdges) && !myIsSelected);
370   myIsolatedEdgeActor->SetVisibility(theVisibility && !myIsSelected);
371
372   myVertexActor->SetVisibility(theVisibility && myDisplayMode == (int)eWireframe && !myIsSelected);// must be added new mode points
373 }
374  
375
376 void
377 GEOM_Actor
378 ::SetNbIsos(const int theNb[2])
379 {
380   myWireframeFaceSource->SetNbIso(theNb);
381 }
382
383 void
384 GEOM_Actor
385 ::GetNbIsos(int &theNbU,int &theNbV)
386 {
387   myWireframeFaceSource->GetNbIso(theNbU, theNbV);
388 }
389
390 void
391 GEOM_Actor
392 ::SetVectorMode(bool theMode)
393 {
394   myVectorMode = theMode;
395   myIsolatedEdgeSource->SetVectorMode(theMode);
396   myOneFaceEdgeSource->SetVectorMode(theMode);
397   mySharedEdgeSource->SetVectorMode(theMode);
398   SetModified();
399 }
400
401 bool
402 GEOM_Actor
403 ::GetVectorMode()
404 {
405   return myVectorMode;
406 }
407
408 void  
409 GEOM_Actor:: 
410 SetDeflection(float theDeflection) 
411
412   if( myDeflection == theDeflection ) 
413     return;
414     
415   myDeflection = theDeflection; 
416  
417   GEOM::MeshShape(myShape,myDeflection);
418   
419   SetModified(); 
420 }
421
422 void GEOM_Actor::SetShape (const TopoDS_Shape& theShape,
423                            float theDeflection,
424                            bool theIsVector)
425 {
426   myShape = theShape;
427
428   myVertexSource->Clear();
429   myIsolatedEdgeSource->Clear();
430   myOneFaceEdgeSource->Clear();
431   mySharedEdgeSource->Clear();
432   myWireframeFaceSource->Clear();
433   myShadingFaceSource->Clear();
434
435   TopExp_Explorer aVertexExp (theShape,TopAbs_VERTEX);
436   for (; aVertexExp.More(); aVertexExp.Next())
437   {
438      const TopoDS_Vertex& aVertex = TopoDS::Vertex(aVertexExp.Current());
439      myVertexSource->AddVertex(aVertex);
440   }
441   
442   SetDeflection(theDeflection);
443
444   // look if edges are free or shared
445   TopTools_IndexedDataMapOfShapeListOfShape anEdgeMap;
446   TopExp::MapShapesAndAncestors(theShape,TopAbs_EDGE,TopAbs_FACE,anEdgeMap);
447   
448   GEOM::SetShape(theShape,anEdgeMap,theIsVector,
449                  myIsolatedEdgeSource.Get(),
450                  myOneFaceEdgeSource.Get(),
451                  mySharedEdgeSource.Get(),
452                  myWireframeFaceSource.Get(),
453                  myShadingFaceSource.Get());
454   isOnlyVertex =  
455     myIsolatedEdgeSource->IsEmpty() &&
456     myOneFaceEdgeSource->IsEmpty() &&
457     mySharedEdgeSource->IsEmpty() &&
458     myWireframeFaceSource->IsEmpty() &&
459     myShadingFaceSource->IsEmpty();
460
461   
462   if((bool)myShape.Infinite() || isOnlyVertex ){
463     myVertexActor->GetDeviceActor()->SetInfinitive(true);
464     myHighlightActor->GetDeviceActor()->SetInfinitive(true);
465   }
466 }
467
468 // OLD METHODS
469 void GEOM_Actor::setDeflection(double adef) {
470 #ifdef MYDEBUG
471   MESSAGE ( "GEOM_Actor::setDeflection" );
472 #endif
473   SetDeflection((float)adef);
474 }
475
476
477 // warning! must be checked!
478 // SetHighlightProperty
479 // SetWireframeProperty
480 // SetShadingProperty
481
482 void GEOM_Actor::SetHighlightProperty(vtkProperty* Prop)
483 {
484 #ifdef MYDEBUG
485   MESSAGE ( "GEOM_Actor::SetHighlightProperty" );
486 #endif
487   this->myHighlightActor->GetProperty()->DeepCopy(Prop);
488   
489 }
490
491 void GEOM_Actor::SetWireframeProperty(vtkProperty* Prop)
492 {
493 #ifdef MYDEBUG
494   MESSAGE ( this << " GEOM_Actor::SetWireframeProperty" );
495 #endif
496   // must be filled
497   myWireframeFaceActor->SetProperty(Prop);
498 }
499
500
501 vtkProperty* GEOM_Actor::GetWireframeProperty()
502 {
503   return myWireframeFaceActor->GetProperty();
504 }
505
506 void GEOM_Actor::SetShadingProperty(vtkProperty* Prop)
507 {
508 #ifdef MYDEBUG
509   MESSAGE ( "GEOM_Actor::SetShadingProperty" );
510 #endif
511   myShadingFaceProp->DeepCopy(Prop);
512 }
513
514
515 vtkProperty* GEOM_Actor::GetShadingProperty()
516 {
517   return myShadingFaceProp;
518 }
519
520
521 void GEOM_Actor::Render(vtkRenderer *ren, vtkMapper *theMapper)
522 {
523 #ifdef MYDEBUG
524   MESSAGE ( "GEOM_Actor::Render" );
525 #endif
526
527   if(!GetVisibility())
528     return;
529
530   /* render the property */
531   if (!this->Property) {
532     // force creation of a property
533     this->GetProperty();
534     this->Property->SetInterpolation(1);
535     this->Property->SetRepresentationToSurface();
536     this->Property->SetAmbient(0.3);
537     this->Property->SetAmbientColor(0.88,0.86,0.2);
538     this->Property->SetDiffuseColor(0.99,0.7,0.21);
539     this->Property->SetSpecularColor(0.99,0.98,0.83);
540   }
541
542   switch(myDisplayMode){
543   case eWireframe://wireframe
544     myPreHighlightProp->SetRepresentationToWireframe();
545     myHighlightProp->SetRepresentationToWireframe();
546     break;
547   case eShading://shading
548   case eShadingWithEdges://shading with edges
549     myPreHighlightProp->SetRepresentationToSurface();
550     myHighlightProp->SetRepresentationToSurface();
551     break;
552   }
553
554   if(!myIsSelected){
555     if(myIsPreselected){
556       this->myHighlightActor->SetProperty(myPreHighlightProp.GetPointer());
557       myShadingFaceActor->SetProperty(myPreHighlightProp.GetPointer());
558       myShadingFaceActor->SetBackfaceProperty(myPreHighlightProp.GetPointer());
559     } else {
560       this->myHighlightActor->SetProperty(myShadingFaceProp.GetPointer());
561       myShadingFaceActor->SetProperty(myShadingFaceProp.GetPointer());
562       myShadingFaceActor->SetBackfaceProperty(myShadingBackFaceProp.GetPointer());
563     }
564   }
565   else{
566     this->myHighlightActor->SetProperty(myHighlightProp.GetPointer());
567     myShadingFaceActor->SetProperty(myHighlightProp.GetPointer());
568     myShadingFaceActor->SetBackfaceProperty(myHighlightProp.GetPointer());
569   }
570
571   this->Property->Render(this, ren);
572   if (this->BackfaceProperty) {
573     this->BackfaceProperty->BackfaceRender(this, ren);
574     this->Device->SetBackfaceProperty(this->BackfaceProperty);
575   }
576   this->Device->SetProperty(this->Property);
577   /*  if(myShape.ShapeType() == TopAbs_VERTEX) {
578     if(ren){
579       //The parameter determine size of vertex actor relate to diagonal of RendererWindow
580       static vtkFloatingPointType delta = 0.01;
581       vtkFloatingPointType X1 = -1, Y1 = -1, Z1 = 0;
582       ren->ViewToWorld(X1,Y1,Z1);
583       vtkFloatingPointType X2 = +1, Y2 = +1, Z2 = 0;
584       ren->ViewToWorld(X2,Y2,Z2);
585       Z2 = sqrt((X2-X1)*(X2-X1) + (Y2-Y1)*(Y2-Y1) + (Z2-Z1)*(Z2-Z1));
586       this->SetScale(Z2*delta);
587     }
588     vtkMatrix4x4 *aMatrix = vtkMatrix4x4::New();
589     this->GetMatrix(ren->GetActiveCamera(), aMatrix);
590     this->Device->SetUserMatrix(aMatrix);
591     this->Device->Render(ren,theMapper);
592     aMatrix->Delete();    
593     } else*/
594     this->Device->Render(ren, theMapper);
595 }
596
597 void GEOM_Actor::ReleaseGraphicsResources(vtkWindow *)
598 {
599 #ifdef MYDEBUG
600   MESSAGE ( "GEOM_Actor::ReleaseGraphicsResources" );
601 #endif  
602 }
603
604
605
606 void GEOM_Actor::ShallowCopy(vtkProp *prop)
607 {
608 #ifdef MYDEBUG
609   MESSAGE ( "GEOM_Actor::ShallowCopy" );
610 #endif
611   GEOM_Actor *f = GEOM_Actor::SafeDownCast(prop);
612   if ( f != NULL )
613     {
614       this->SetShape(f->getTopo(),f->GetDeflection());
615     }
616
617   // Now do superclass
618   this->SALOME_Actor::ShallowCopy(prop);
619 }
620
621 const TopoDS_Shape& GEOM_Actor::getTopo() {
622 #ifdef MYDEBUG
623   MESSAGE ( "GEOM_Actor::getTopo" );
624 #endif
625   return myShape;
626 }
627
628 void GEOM_Actor::setInputShape(const TopoDS_Shape& ashape, double adef1,
629                                int imode, bool isVector)
630 {
631 #ifdef MYDEBUG
632   MESSAGE ( "GEOM_Actor::setInputShape" );
633 #endif
634 }
635
636 double GEOM_Actor::getDeflection()
637 {
638 #ifdef MYDEBUG
639   MESSAGE ( "GEOM_Actor::getDeflection" );
640 #endif
641   return (double) GetDeflection();
642 }
643
644
645 double GEOM_Actor::isVector()
646 {
647 #ifdef MYDEBUG
648   MESSAGE ( "GEOM_Actor::isVector" );
649 #endif  
650   return 0;
651 }
652
653 void GEOM_Actor::SubShapeOn()
654 {
655 #ifdef MYDEBUG
656   MESSAGE ( "GEOM_Actor::SubShapeOn" );
657 #endif  
658 }
659
660 void GEOM_Actor::SubShapeOff()
661 {
662 #ifdef MYDEBUG
663   MESSAGE ( "GEOM_Actor::SubShapeOff" );
664 #endif
665 }
666
667 void GEOM_Actor::highlight(bool highlight)
668 {
669 #ifdef MYDEBUG
670   MESSAGE ( this << " GEOM_Actor::highlight highlight="<<highlight );
671 #endif
672   SALOME_Actor::highlight(highlight);  
673 }
674
675 void GEOM_Actor::SetOpacity(vtkFloatingPointType opa)
676 {
677   // enk:tested OK
678   myShadingFaceProp->SetOpacity(opa);
679   myShadingBackFaceProp->SetOpacity(opa);
680   myHighlightProp->SetOpacity(opa);
681   myPreHighlightProp->SetOpacity(opa);
682   myVertexActor->GetProperty()->SetOpacity(opa);
683 }
684
685 vtkFloatingPointType GEOM_Actor::GetOpacity()
686 {
687   // enk:tested OK
688   return myShadingFaceProp->GetOpacity(); 
689 }
690
691 void GEOM_Actor::SetColor(vtkFloatingPointType r,vtkFloatingPointType g,vtkFloatingPointType b)
692 {
693   // enk:tested OK
694   myShadingFaceProp->SetColor(r,g,b);                          // shading color (Shading)
695   myShadingBackFaceProp->SetColor(r,g,b);                      // back face shading color (Shading)
696   myVertexActor->GetProperty()->SetColor(r,g,b);               // vertex actor (Shading/Wireframe)
697   if ( myDisplayMode != (int)eShadingWithEdges ) {
698     myIsolatedEdgeActor->GetProperty()->SetColor(r,g,b);         // standalone edge color (Wireframe)
699     myOneFaceEdgeActor->GetProperty()->SetColor(r,g,b);          // standalone face edge color (Wireframe)
700     mySharedEdgeActor->GetProperty()->SetColor(r,g,b);           // share edge color (Wireframe)
701   }
702
703   StoreBoundaryColors();
704 }
705
706 void GEOM_Actor::GetColor(vtkFloatingPointType& r,vtkFloatingPointType& g,vtkFloatingPointType& b)
707 {
708   // enk:tested OK
709   vtkFloatingPointType aRGB[3];
710   myShadingFaceProp->GetColor(aRGB);
711   r = aRGB[0];
712   g = aRGB[1];
713   b = aRGB[2];
714 }
715
716 void GEOM_Actor::SetMaterial(std::vector<vtkProperty*> theProps)
717 {
718   int aSize = theProps.size();
719
720   if ( aSize < 1 || aSize > 2)
721     return;
722
723   // theProps[0] -- front material properties
724   // theProps[1] -- back material properties (if exist)
725   // If there are no back material properties,
726   // we get front material properties as back material
727
728   double aCoefnt;
729
730   // Set reflection coefficients
731   aCoefnt = theProps[0]->GetAmbient();
732   myShadingFaceProp->SetAmbient(aCoefnt);
733   myVertexActor->GetProperty()->SetAmbient(aCoefnt);
734   if ( aSize == 2 )
735     aCoefnt = theProps[1]->GetAmbient();
736   myShadingBackFaceProp->SetAmbient(aCoefnt);
737
738   // Set diffuse coefficients
739   aCoefnt = theProps[0]->GetDiffuse();
740   myShadingFaceProp->SetDiffuse(aCoefnt);
741   myVertexActor->GetProperty()->SetDiffuse(aCoefnt);
742   if ( aSize == 2 )
743     aCoefnt = theProps[1]->GetDiffuse();
744   myShadingBackFaceProp->SetDiffuse(aCoefnt);
745   
746   // Set specular coefficients
747   aCoefnt = theProps[0]->GetSpecular();
748   myShadingFaceProp->SetSpecular(aCoefnt);
749   myVertexActor->GetProperty()->SetSpecular(aCoefnt);
750   if ( aSize == 2 )
751     aCoefnt = theProps[1]->GetSpecular();
752   myShadingBackFaceProp->SetSpecular(aCoefnt);
753
754
755   double* aColor;
756
757   // Set reflection colors
758   aColor = theProps[0]->GetAmbientColor();
759   myShadingFaceProp->SetAmbientColor(aColor[0], aColor[1], aColor[2]);
760   myVertexActor->GetProperty()->SetAmbientColor(aColor[0], aColor[1], aColor[2]);
761   if ( aSize == 2 )
762     aColor = theProps[1]->GetAmbientColor();
763   myShadingBackFaceProp->SetAmbientColor(aColor[0], aColor[1], aColor[2]);
764
765   // Set diffuse colors
766   aColor = theProps[0]->GetDiffuseColor();
767   myShadingFaceProp->SetDiffuseColor(aColor[0], aColor[1], aColor[2]);
768   myVertexActor->GetProperty()->SetDiffuseColor(aColor[0], aColor[1], aColor[2]);
769   if ( aSize == 2 )
770     aColor = theProps[1]->GetDiffuseColor();
771   myShadingBackFaceProp->SetDiffuseColor(aColor[0], aColor[1], aColor[2]);
772
773   // Set specular colors
774   aColor = theProps[0]->GetSpecularColor();
775   myShadingFaceProp->SetSpecularColor(aColor[0], aColor[1], aColor[2]);
776   myVertexActor->GetProperty()->SetSpecularColor(aColor[0], aColor[1], aColor[2]);
777   if ( aSize == 2 )
778     aColor = theProps[1]->GetSpecularColor();
779   myShadingBackFaceProp->SetSpecularColor(aColor[0], aColor[1], aColor[2]);
780
781   // Set shininess
782   aCoefnt = theProps[0]->GetSpecularPower();
783   myShadingFaceProp->SetSpecularPower(aCoefnt);
784   myVertexActor->GetProperty()->SetSpecularPower(aCoefnt);
785   if ( aSize == 2 )
786     aCoefnt = theProps[1]->GetSpecularPower();
787   myShadingBackFaceProp->SetSpecularPower(aCoefnt);
788
789   // Set back face material property
790   myShadingFaceActor->SetBackfaceProperty(myShadingBackFaceProp.GetPointer());
791 }
792
793 vtkProperty* GEOM_Actor::GetFrontMaterial()
794 {
795   return myShadingFaceProp;
796 }
797
798 vtkProperty* GEOM_Actor::GetBackMaterial()
799 {
800   return myShadingBackFaceProp;
801 }
802
803 bool GEOM_Actor::IsInfinitive()
804 {
805   return ((bool)myShape.Infinite() || isOnlyVertex);
806 }
807
808 /*!
809   To map current selection to VTK representation
810 */
811 void
812 GEOM_Actor
813 ::Highlight(bool theIsHighlight)
814 {
815   myIsSelected = theIsHighlight;
816 #ifdef MYDEBUG
817   MESSAGE ( this << " GEOM_Actor::Highlight myIsSelected="<<myIsSelected );
818 #endif
819
820   if ( myDisplayMode == (int)eShading || myDisplayMode == (int)eShadingWithEdges ) {
821     if ( theIsHighlight )
822       RestoreIsoNumbers();
823     else
824       // Reset number of iso lines to 0
825       ResetIsoNumbers();
826   }
827
828   SALOME_Actor::Highlight(theIsHighlight); // this method call ::highlight(theIsHighlight) in the end
829   SetVisibility(GetVisibility());
830 }
831
832 /*!
833   To process prehighlight (called from SVTK_InteractorStyle)
834 */
835 bool
836 GEOM_Actor
837 ::PreHighlight(vtkInteractorStyle *theInteractorStyle, 
838                SVTK_SelectionEvent* theSelectionEvent,
839                bool theIsHighlight)
840 {
841 #ifdef MYDEBUG
842   MESSAGE ( this<<" GEOM_Actor::PreHighlight (3) theIsHighlight="<<theIsHighlight );
843 #endif
844
845   if ( !GetPickable() )
846     return false;  
847
848   if ( myDisplayMode == (int)eShading || myDisplayMode == (int)eShadingWithEdges ) {
849     if ( theIsHighlight )
850       RestoreIsoNumbers();
851     else
852       // Reset number of iso lines to 0
853       ResetIsoNumbers();
854   }
855
856   myPreHighlightActor->SetVisibility( false );
857   bool anIsPreselected = myIsPreselected;
858   
859   Selection_Mode aSelectionMode = theSelectionEvent->mySelectionMode;
860   bool anIsChanged = (mySelectionMode != aSelectionMode);
861
862   if( !theIsHighlight ) {
863     SetPreSelected( false );
864   }else{
865     switch(aSelectionMode){
866     case ActorSelection : 
867     {
868 //       cout << "=============== " << myIO->getEntry() << endl;
869 //       int nbio = mySelector->IObjectCount();
870 //       cout << " nbio = " << nbio << endl;
871
872       if( !mySelector->IsSelected( myIO ) ) {
873         //      printf ("!!!!!!!!!!!!!!!!\n");
874         SetPreSelected( true );
875       }
876     }
877     default:
878       break;
879     }
880   }
881
882   mySelectionMode = aSelectionMode;
883   anIsChanged |= (anIsPreselected != myIsPreselected);
884
885   SetVisibility(GetVisibility());
886   return anIsChanged;
887 }
888
889 /*!
890   To process highlight (called from SVTK_InteractorStyle)
891 */
892 bool
893 GEOM_Actor
894 ::Highlight(vtkInteractorStyle *theInteractorStyle, 
895             SVTK_SelectionEvent* theSelectionEvent,
896             bool theIsHighlight)
897 {
898   // define the selection of object
899 #ifdef MYDEBUG
900   MESSAGE ( std::endl << this << " GEOM_Actor::Highlight (3) myIsSelected="<<myIsSelected );
901 #endif
902   bool aRet = SALOME_Actor::Highlight(theInteractorStyle,theSelectionEvent,theIsHighlight);
903   SetSelected(theIsHighlight);
904   if(theIsHighlight)
905     SetPreSelected(false);
906   
907  
908   return aRet;
909 }
910
911 // Copy the follower's composite 4x4 matrix into the matrix provided.
912 void GEOM_Actor::GetMatrix(vtkCamera* theCam, vtkMatrix4x4 *result)
913 {
914   double *pos, *vup;
915   double Rx[3], Ry[3], Rz[3], p1[3];
916   vtkMatrix4x4 *matrix = vtkMatrix4x4::New();
917   int i;
918   double distance;
919   
920   this->GetOrientation();
921   this->Transform->Push();  
922   this->Transform->PostMultiply();  
923   this->Transform->Identity();
924
925   // apply user defined matrix last if there is one 
926   if (this->UserMatrix)
927     {
928     this->Transform->Concatenate(this->UserMatrix);
929     }
930
931   this->Transform->Translate(-this->Origin[0],
932                              -this->Origin[1],
933                              -this->Origin[2]);
934   // scale
935   this->Transform->Scale(this->Scale[0],
936                          this->Scale[1],
937                          this->Scale[2]);
938   
939   // rotate
940   this->Transform->RotateY(this->Orientation[1]);
941   this->Transform->RotateX(this->Orientation[0]);
942   this->Transform->RotateZ(this->Orientation[2]);
943
944   if (theCam)
945     {
946     // do the rotation
947     // first rotate y 
948     pos = theCam->GetPosition();
949     vup = theCam->GetViewUp();
950
951     if (theCam->GetParallelProjection())
952       {
953       theCam->GetDirectionOfProjection(Rz);
954       }
955     else
956       {
957       distance = sqrt(
958         (pos[0] - this->Position[0])*(pos[0] - this->Position[0]) +
959         (pos[1] - this->Position[1])*(pos[1] - this->Position[1]) +
960         (pos[2] - this->Position[2])*(pos[2] - this->Position[2]));
961       for (i = 0; i < 3; i++)
962         {
963         Rz[i] = (pos[i] - this->Position[i])/distance;
964         }
965       }
966   
967     vtkMath::Cross(vup,Rz,Rx);
968     vtkMath::Normalize(Rx);
969     vtkMath::Cross(Rz,Rx,Ry);
970     
971     matrix->Element[0][0] = Rx[0];
972     matrix->Element[1][0] = Rx[1];
973     matrix->Element[2][0] = Rx[2];
974     matrix->Element[0][1] = Ry[0];
975     matrix->Element[1][1] = Ry[1];
976     matrix->Element[2][1] = Ry[2];
977     matrix->Element[0][2] = Rz[0];
978     matrix->Element[1][2] = Rz[1];
979     matrix->Element[2][2] = Rz[2];
980     
981     this->Transform->Concatenate(matrix);
982     }
983   
984   // translate to projection reference point PRP
985   // this is the camera's position blasted through
986   // the current matrix
987   p1[0] = this->Origin[0] + this->Position[0];
988   p1[1] = this->Origin[1] + this->Position[1];
989   p1[2] = this->Origin[2] + this->Position[2];
990
991   this->Transform->Translate(p1[0],p1[1],p1[2]);
992   this->Transform->GetMatrix(result);
993   
994   matrix->Delete();
995   this->Transform->Pop();  
996 }  
997
998 void GEOM_Actor::SetEdgesInShadingColor(vtkFloatingPointType r,vtkFloatingPointType g,vtkFloatingPointType b)
999 {
1000   myEdgesInShadingColor[0] = r;
1001   myEdgesInShadingColor[1] = g;
1002   myEdgesInShadingColor[2] = b;
1003 }
1004
1005 void GEOM_Actor::StoreIsoNumbers()
1006 {  
1007   myWireframeFaceSource->GetNbIso(myNbIsos[0], myNbIsos[1]);
1008 }
1009
1010 void GEOM_Actor::SetIsosWidth(const int width) {
1011   myWireframeFaceActor->GetProperty()->SetLineWidth(width);
1012 }
1013
1014 int GEOM_Actor::GetIsosWidth() const {
1015   return (int)myWireframeFaceActor->GetProperty()->GetLineWidth();
1016 }
1017
1018 void GEOM_Actor::SetWidth(const int width) {
1019   myIsolatedEdgeActor->GetProperty()->SetLineWidth(width);
1020   myOneFaceEdgeActor->GetProperty()->SetLineWidth(width);
1021   mySharedEdgeActor->GetProperty()->SetLineWidth(width);
1022   myHighlightProp->SetLineWidth(width);
1023   myPreHighlightProp->SetLineWidth(width+1);
1024
1025 }
1026
1027 int GEOM_Actor::GetWidth() const {
1028   return (int)myIsolatedEdgeActor->GetProperty()->GetLineWidth();
1029 }
1030     
1031 void GEOM_Actor::RestoreIsoNumbers()
1032 {
1033   if ( myNbIsos[0] > 0 || myNbIsos[1] > 0 )
1034     // Restore number of U and (or) V iso lines
1035     myWireframeFaceSource->SetNbIso(myNbIsos);
1036 }
1037   
1038 void GEOM_Actor::ResetIsoNumbers()
1039 {
1040   int aNb[2] = {0, 0};
1041   myWireframeFaceSource->SetNbIso(aNb);
1042 }
1043
1044 void GEOM_Actor::StoreBoundaryColors()
1045 {
1046   myShadingFaceProp->GetColor(myEdgesInWireframeColor);
1047
1048 }
1049  
1050 void GEOM_Actor::RestoreBoundaryColors()
1051 {
1052   myIsolatedEdgeActor->GetProperty()->SetColor(myEdgesInWireframeColor[0],
1053                                                myEdgesInWireframeColor[1],
1054                                                myEdgesInWireframeColor[2]);
1055   myOneFaceEdgeActor->GetProperty()->SetColor(myEdgesInWireframeColor[0],
1056                                               myEdgesInWireframeColor[1],
1057                                               myEdgesInWireframeColor[2]);
1058   mySharedEdgeActor->GetProperty()->SetColor(myEdgesInWireframeColor[0],
1059                                              myEdgesInWireframeColor[1],
1060                                              myEdgesInWireframeColor[2]);
1061 }