Salome HOME
PAL14047 : Problem to select merged face with Create group window
[modules/geom.git] / src / OBJECT / GEOM_AssemblyBuilder.cxx
1 //  GEOM OBJECT : interactive object for Geometry entities visualization
2 //
3 //  Copyright (C) 2003  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 //
24 //  File   : GEOM_AssemblyBuilder.cxx
25 //  Author : Christophe ATTANASIO
26 //  Module : GEOM
27 //  $Header$
28
29 /*!
30   \class GEOM_AssemblyBuilder GEOM_AssemblyBuilder.h
31   \brief ....
32 */
33
34 #include "GEOM_AssemblyBuilder.h"
35 #include "GEOM_Actor.h"
36
37 #include <SUIT_Session.h>
38
39 #include <vtkProperty.h>
40
41 // Open CASCADE Includes
42 #include <TopExp_Explorer.hxx>
43 #include <Bnd_Box.hxx>
44 #include <BRepMesh_IncrementalMesh.hxx>
45 #include <Poly_Triangulation.hxx>
46 #include <BRepBndLib.hxx>
47 #include <BRep_Tool.hxx>
48 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
49 #include <TopExp.hxx>
50 #include <TopTools_ListOfShape.hxx>
51 #include <TopoDS_Iterator.hxx>
52
53 #include "utilities.h"
54
55 using namespace std;
56 // SALOME
57
58 #define MAX2(X, Y)      (  Abs(X) > Abs(Y)? Abs(X) : Abs(Y) )
59 #define MAX3(X, Y, Z)   ( MAX2 ( MAX2(X,Y) , Z) )
60
61
62
63
64
65 void GEOM_AssemblyBuilder::InitProperties(vtkProperty* IsoProp,
66                                           vtkProperty* FaceProp,
67                                           vtkProperty* EdgeFProp,
68                                           vtkProperty* EdgeSProp,
69                                           vtkProperty* EdgeIProp,
70                                           vtkProperty* VertexProp,
71                                           vtkProperty* IsoPVProp,
72                                           vtkProperty* EdgePVProp,
73                                           vtkProperty* VertexPVProp)
74 {
75   // Shading like default OCC material
76   FaceProp->SetRepresentationToSurface();
77   FaceProp->SetInterpolationToGouraud();
78   FaceProp->SetAmbient(1.0);
79   FaceProp->SetDiffuse(1.0);
80   FaceProp->SetSpecular(0.4);
81   FaceProp->SetAmbientColor(0.329412, 0.223529, 0.027451);
82   FaceProp->SetDiffuseColor(0.780392, 0.568627, 0.113725);
83   FaceProp->SetSpecularColor(0.992157, 0.941176, 0.807843);
84
85   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
86   QColor aColor;
87
88   // Wireframe for iso
89   aColor = aResMgr->colorValue( "Geometry", "isos_color", QColor( int(0.5*255), int(0.5*255), int(0.5*255) ) );
90   float red = aColor.red()/255.0;
91   float green = aColor.green()/255.0;
92   float blue = aColor.blue()/255.0;
93   IsoProp->SetRepresentationToWireframe();
94   IsoProp->SetAmbientColor(red, green, blue);
95   IsoProp->SetDiffuseColor(red, green, blue);
96   IsoProp->SetSpecularColor(red, green, blue);
97   
98   // Wireframe for iso
99   IsoPVProp->SetRepresentationToWireframe();
100   IsoPVProp->SetAmbientColor(0, 1, 1);
101   IsoPVProp->SetDiffuseColor(0, 1, 1);
102   IsoPVProp->SetSpecularColor(0, 1, 1);
103
104   // Wireframe for shared edge 
105   aColor = aResMgr->colorValue( "Geometry", "wireframe_color", QColor( 255, 255, 0 ) );
106   red = aColor.red()/255.0;
107   green = aColor.green()/255.0;
108   blue = aColor.blue()/255.0;
109   EdgeSProp->SetRepresentationToWireframe();
110   EdgeSProp->SetAmbientColor(red, green, blue);
111   EdgeSProp->SetDiffuseColor(red, green, blue);
112   EdgeSProp->SetSpecularColor(red, green, blue);
113   
114   // Wireframe for free edge 
115   aColor = aResMgr->colorValue( "Geometry", "free_bound_color", QColor( 0, 255, 0 ) );
116   red = aColor.red()/255.0;
117   green = aColor.green()/255.0;
118   blue = aColor.blue()/255.0;
119   EdgeFProp->SetRepresentationToWireframe();
120   EdgeFProp->SetAmbientColor(red, green, blue);
121   EdgeFProp->SetDiffuseColor(red, green, blue);
122   EdgeFProp->SetSpecularColor(red, green, blue);
123
124   // Wireframe for isolated edge 
125   aColor = aResMgr->colorValue( "Geometry", "line_color", QColor( 255, 0, 0 ) );
126   red = aColor.red()/255.0;
127   green = aColor.green()/255.0;
128   blue = aColor.blue()/255.0;
129   EdgeIProp->SetRepresentationToWireframe();
130   EdgeIProp->SetAmbientColor(red, green, blue);
131   EdgeIProp->SetDiffuseColor(red, green, blue);
132   
133   // Wireframe for Preview edge 
134   EdgePVProp->SetRepresentationToWireframe();
135   EdgePVProp->SetAmbientColor(1, 1, 0);
136   EdgePVProp->SetDiffuseColor(1, 1, 0);
137   EdgePVProp->SetSpecularColor(1, 1, 0);
138
139   // Wireframe for vertex 
140   aColor = aResMgr->colorValue( "Geometry", "point_color", QColor( 255, 255, 0 ) );
141   red = aColor.red()/255.0;
142   green = aColor.green()/255.0;
143   blue = aColor.blue()/255.0;
144   VertexProp->SetRepresentationToWireframe();
145   VertexProp->SetAmbientColor(red, green, blue);
146   VertexProp->SetDiffuseColor(red, green, blue);
147   VertexProp->SetSpecularColor(red, green, blue);
148   
149   // Wireframe for vertex 
150   VertexPVProp->SetRepresentationToWireframe();
151   VertexPVProp->SetAmbientColor(0, 1, 1);
152   VertexPVProp->SetDiffuseColor(0, 1, 1);
153   VertexPVProp->SetSpecularColor(0, 1, 1);
154 }
155
156
157 void GEOM_AssemblyBuilder::MeshShape(const TopoDS_Shape myShape,
158                                          Standard_Real deflection,
159                                          Standard_Boolean forced)
160 {
161   // Mesh the shape if necessary
162   Standard_Boolean alreadymesh = Standard_True;
163   TopExp_Explorer ex;
164   TopLoc_Location aLoc;
165
166   for (ex.Init(myShape, TopAbs_FACE); ex.More(); ex.Next()) {
167     const TopoDS_Face& aFace = TopoDS::Face(ex.Current());
168     Handle(Poly_Triangulation) aPoly = BRep_Tool::Triangulation(aFace,aLoc);
169     if(aPoly.IsNull()) { alreadymesh = Standard_False; break; }
170   }
171
172   if(!alreadymesh || forced) {
173     if(deflection<=0) {
174       // Compute default deflection
175       Bnd_Box B;
176       BRepBndLib::Add(myShape, B);
177       if ( B.IsVoid() ) return; // NPAL15983 (Bug when displaying empty groups) 
178       Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
179       B.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
180       deflection = MAX3( aXmax-aXmin , aYmax-aYmin , aZmax-aZmin) * 0.001 *4;
181     }
182     BRepMesh_IncrementalMesh MESH(myShape,deflection);
183   }
184 }
185
186
187
188 vtkActorCollection* GEOM_AssemblyBuilder::BuildActors(const TopoDS_Shape& myShape,
189                                                       Standard_Real    deflection,
190                                                       Standard_Integer mode,
191                                                       Standard_Boolean forced,
192                                                       Standard_Boolean isVector)
193 {
194   /*
195   vtkProperty* IsoProp = vtkProperty::New();
196   vtkProperty* FaceProp = vtkProperty::New();
197   vtkProperty* EdgeFProp = vtkProperty::New();
198   vtkProperty* EdgeSProp = vtkProperty::New();
199   vtkProperty* EdgeIProp = vtkProperty::New();
200   vtkProperty* VertexProp = vtkProperty::New();
201   vtkProperty* IsoPVProp = vtkProperty::New();
202   vtkProperty* EdgePVProp = vtkProperty::New();
203   vtkProperty* VertexPVProp = vtkProperty::New();
204   InitProperties(IsoProp,FaceProp,EdgeFProp,EdgeSProp,EdgeIProp,VertexProp,IsoPVProp,EdgePVProp,VertexPVProp);
205   */
206
207   vtkActorCollection* AISActors = vtkActorCollection::New();
208   MeshShape(myShape,deflection,forced);
209   GEOM_Actor* aGeomActor = GEOM_Actor::New();
210   aGeomActor->SetShape(myShape,(float)deflection,false,isVector);
211   AISActors->AddItem(aGeomActor);
212
213   aGeomActor->Delete();
214   
215   /*
216   if(myShape.ShapeType() == TopAbs_COMPOUND) {
217     TopoDS_Iterator anItr(myShape);
218     for(; anItr.More(); anItr.Next()) {
219       vtkActorCollection* theActors =
220         GEOM_AssemblyBuilder::BuildActors(anItr.Value(), deflection, mode, forced);
221       theActors->InitTraversal();
222       vtkActor* anActor = (vtkActor*)theActors->GetNextActor();
223       while(!(anActor==NULL)) {
224         AISActors->AddItem(anActor);
225         anActor = (vtkActor*)theActors->GetNextActor();
226       }
227     }
228   }
229   // Create graphics properties
230
231   vtkProperty* IsoProp = vtkProperty::New();
232   vtkProperty* FaceProp = vtkProperty::New();
233   vtkProperty* EdgeFProp = vtkProperty::New();
234   vtkProperty* EdgeSProp = vtkProperty::New();
235   vtkProperty* EdgeIProp = vtkProperty::New();
236   vtkProperty* VertexProp = vtkProperty::New();
237
238   vtkProperty* IsoPVProp = vtkProperty::New();
239   vtkProperty* EdgePVProp = vtkProperty::New();
240   vtkProperty* VertexPVProp = vtkProperty::New();
241
242   InitProperties(IsoProp,FaceProp,EdgeFProp,EdgeSProp,EdgeIProp,VertexProp,IsoPVProp,EdgePVProp,VertexPVProp);
243
244   MeshShape(myShape,deflection,forced);
245
246   if ( myShape.ShapeType() <= 4 && myShape.ShapeType() != TopAbs_COMPOUND) {
247     
248     // FACE Actor
249     // look if edges are free or shared 
250     TopTools_IndexedDataMapOfShapeListOfShape edgemap;
251     TopExp::MapShapesAndAncestors(myShape,TopAbs_EDGE,TopAbs_FACE,edgemap);
252     
253     TopExp_Explorer ex;
254     
255     for (ex.Init(myShape, TopAbs_FACE); ex.More(); ex.Next()) {
256       
257       GEOM_Actor* FaceActor = GEOM_Actor::New();
258       FaceActor->SetShadingProperty(FaceProp);
259       FaceActor->SetWireframeProperty(IsoProp);
260
261       FaceActor->SetPreviewProperty(IsoPVProp);
262       
263       FaceActor->setInputShape(ex.Current(),deflection,mode);
264       
265       AISActors->AddItem(FaceActor);
266       
267       TopExp_Explorer ex2;
268       for (ex2.Init(ex.Current(), TopAbs_EDGE); ex2.More(); ex2.Next()) {
269         const TopoDS_Edge& aEdge = TopoDS::Edge(ex2.Current());
270         
271         if (BRep_Tool::Degenerated(aEdge)) {    
272           continue;
273         }
274         
275         // compute the number of faces
276         Standard_Integer nbf = edgemap.FindFromKey(ex2.Current()).Extent();
277         GEOM_Actor* EdgeActor = GEOM_Actor::New();
278         EdgeActor->SubShapeOn();
279         EdgeActor->setInputShape(ex2.Current(),deflection,mode);
280         
281         switch (nbf) {
282           
283         case 0 : // isolated edge
284           {
285             EdgeActor->SetShadingProperty(EdgeIProp);
286             EdgeActor->SetWireframeProperty(EdgeIProp);
287           }
288           break;
289           
290         case 1 :// edge in only one face
291           {
292             EdgeActor->SetShadingProperty(EdgeFProp);
293             EdgeActor->SetWireframeProperty(EdgeFProp);
294           }
295           break;
296           
297         default :   // edge shared by at least two faces      
298           {
299             EdgeActor->SetShadingProperty(EdgeSProp);
300             EdgeActor->SetWireframeProperty(EdgeSProp);
301           }
302         }
303         
304         EdgeActor->SetPreviewProperty(EdgePVProp);
305         AISActors->AddItem(EdgeActor);
306       }
307     }
308   } else if ( myShape.ShapeType() == TopAbs_WIRE ) { // WIRE Actor
309     TopExp_Explorer ex;
310     for (ex.Init(myShape, TopAbs_EDGE); ex.More(); ex.Next()) {
311       const TopoDS_Edge& aEdge = TopoDS::Edge(ex.Current());
312         
313       if (BRep_Tool::Degenerated(aEdge)) {    
314         continue;
315       }
316         
317       GEOM_Actor* EdgeActor = GEOM_Actor::New();
318       EdgeActor->setInputShape(ex.Current(),deflection,mode);
319       EdgeActor->SetShadingProperty(EdgeIProp);
320       EdgeActor->SetWireframeProperty(EdgeIProp);
321       EdgeActor->SetPreviewProperty(EdgePVProp);
322       
323       AISActors->AddItem(EdgeActor);
324     }
325   } else if ( myShape.ShapeType() == TopAbs_EDGE ) { // EDGE Actor
326     GEOM_Actor* EdgeActor = GEOM_Actor::New();
327     EdgeActor->setInputShape(myShape,deflection,mode,isVector);
328     EdgeActor->SetShadingProperty(EdgeIProp);
329     EdgeActor->SetWireframeProperty(EdgeIProp);
330     EdgeActor->SetPreviewProperty(EdgePVProp);
331     
332     AISActors->AddItem(EdgeActor);
333   } else if ( myShape.ShapeType() == TopAbs_VERTEX ) { // VERTEX Actor
334     GEOM_Actor* VertexActor = GEOM_Actor::New();
335     VertexActor->setInputShape(myShape,deflection,mode);
336     VertexActor->SetShadingProperty(VertexProp);
337     VertexActor->SetWireframeProperty(VertexProp);
338     VertexActor->SetPreviewProperty(VertexPVProp);
339     
340     AISActors->AddItem(VertexActor);
341   
342   } 
343   */
344   
345   
346   return AISActors;
347
348 }
349
350
351
352 //-------------------------------------------------------------
353 // BUILD ASSEMBLY
354 //-------------------------------------------------------------
355 vtkAssembly*  GEOM_AssemblyBuilder::BuildAssembly(const TopoDS_Shape& myShape,
356                                                       Standard_Real deflection,
357                                                       Standard_Integer mode,
358                                                       Standard_Boolean forced)
359 {
360   // Create a new vtkAssembly
361
362   vtkAssembly* myVTKShape = vtkAssembly::New();
363   /*
364   // Create graphics properties
365
366   vtkProperty* IsoProp = vtkProperty::New();
367   vtkProperty* FaceProp = vtkProperty::New();
368   vtkProperty* EdgeFProp = vtkProperty::New();
369   vtkProperty* EdgeSProp = vtkProperty::New();
370   vtkProperty* EdgeIProp = vtkProperty::New();
371   vtkProperty* VertexProp = vtkProperty::New();
372   vtkProperty* EdgePVProp = vtkProperty::New();
373   vtkProperty* VertexPVProp = vtkProperty::New();
374   vtkProperty* IsoPVProp = vtkProperty::New();
375
376   InitProperties(IsoProp,FaceProp,EdgeFProp,EdgeSProp,EdgeIProp,VertexProp,IsoPVProp,EdgePVProp,VertexPVProp);
377
378   MeshShape(myShape,deflection,forced);
379
380   
381   // FACE Actor
382   
383   // look if edges are free or shared 
384   TopTools_IndexedDataMapOfShapeListOfShape edgemap;
385   TopExp::MapShapesAndAncestors(myShape,TopAbs_EDGE,TopAbs_FACE,edgemap);
386   
387   TopExp_Explorer ex;
388
389   for (ex.Init(myShape, TopAbs_FACE); ex.More(); ex.Next()) {
390     //const TopoDS_Face& aFace = TopoDS::Face(ex.Current());
391     
392     GEOM_Actor* FaceActor = GEOM_Actor::New();
393     FaceActor->SetShadingProperty(FaceProp);
394     FaceActor->SetWireframeProperty(IsoProp);
395     
396     vtkAssembly* myFaceAssembly = vtkAssembly::New();
397
398    
399     FaceActor->setInputShape(ex.Current(),deflection,mode);
400     myFaceAssembly->AddPart(FaceActor);
401     
402     TopExp_Explorer ex2;
403     for (ex2.Init(ex.Current(), TopAbs_EDGE); ex2.More(); ex2.Next()) {
404       const TopoDS_Edge& aEdge = TopoDS::Edge(ex2.Current());
405  
406       if (BRep_Tool::Degenerated(aEdge)) {    
407         continue;
408       }
409       
410     
411       // compute the number of faces
412       Standard_Integer nbf = edgemap.FindFromKey(ex2.Current()).Extent();
413       GEOM_Actor* EdgeActor = GEOM_Actor::New();
414       switch (nbf) {
415         
416       case 0 : // isolated edge
417         {
418           EdgeActor->SetShadingProperty(EdgeIProp);
419           EdgeActor->SetWireframeProperty(EdgeIProp);
420         }
421         break;
422         
423       case 1 :// edge in only one face
424         {
425           EdgeActor->SetShadingProperty(EdgeFProp);
426           EdgeActor->SetWireframeProperty(EdgeFProp);
427         }
428         break;
429         
430       default :   // edge shared by at least two faces      
431         {
432           EdgeActor->SetShadingProperty(EdgeSProp);
433           EdgeActor->SetWireframeProperty(EdgeSProp);
434         }
435       }
436   
437       EdgeActor->setInputShape(ex2.Current(),deflection,mode);
438       myFaceAssembly->AddPart(EdgeActor);
439     }
440     myVTKShape->AddPart(myFaceAssembly);
441   }
442
443   */
444   
445   return myVTKShape;
446  
447 }
448
449 //-------------------------------------------------------------
450 // CHANGE SPECIFIC DISPLAY MODE
451 //-------------------------------------------------------------
452 void  GEOM_AssemblyBuilder::SwitchDisplayMode(vtkAssembly* aOCCAssembly)
453 {
454 }
455
456 void  GEOM_AssemblyBuilder::SwitchDisplayMode(vtkActorCollection* aOCCAssembly)
457 {
458 }
459
460 //-------------------------------------------------------------
461 // DISPLAY/ERASE
462 //-------------------------------------------------------------
463
464 void GEOM_AssemblyBuilder::DisplayErase(vtkAssembly* mySALOMEAssembly)
465 {
466 }
467
468
469 void GEOM_AssemblyBuilder::DisplayErase(vtkActorCollection* mySALOMEActors)
470 {
471 }
472
473
474
475
476