Salome HOME
NRI : First integration.
[modules/geom.git] / src / OBJECT / GEOM_AssemblyBuilder.cxx
1 using namespace std;
2 //  File      : GEOM_AssemblyBuilder.cxx
3 //  Created   : Wed Feb 20 17:24:59 2002
4 //  Author    : Christophe ATTANASIO
5 //  Project   : SALOME
6 //  Module    : GEOM
7 //  Copyright : Open CASCADE 2002
8 //  $Header$
9
10 /*!
11   \class GEOM_AssemblyBuilder GEOM_AssemblyBuilder.h
12   \brief ....
13 */
14
15 #include "GEOM_AssemblyBuilder.h"
16 #include "GEOM_Actor.h"
17 #include "utilities.h"
18
19 // Open CASCADE Includes
20 #include <TopExp_Explorer.hxx>
21 #include <Bnd_Box.hxx>
22 #include <BRepMesh_IncrementalMesh.hxx>
23 #include <Poly_Triangulation.hxx>
24 #include <BRepBndLib.hxx>
25 #include <BRep_Tool.hxx>
26 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
27 #include <TopExp.hxx>
28 #include <TopTools_ListOfShape.hxx>
29
30 // SALOME
31
32 #define MAX2(X, Y)      (  Abs(X) > Abs(Y)? Abs(X) : Abs(Y) )
33 #define MAX3(X, Y, Z)   ( MAX2 ( MAX2(X,Y) , Z) )
34
35
36
37
38
39 void GEOM_AssemblyBuilder::InitProperties(vtkProperty* IsoProp,
40                                           vtkProperty* FaceProp,
41                                           vtkProperty* EdgeFProp,
42                                           vtkProperty* EdgeSProp,
43                                           vtkProperty* EdgeIProp,
44                                           vtkProperty* VertexProp,
45                                           vtkProperty* IsoPVProp,
46                                           vtkProperty* EdgePVProp,
47                                           vtkProperty* VertexPVProp)
48 {
49   // Shading like default OCC material
50   FaceProp->SetRepresentationToSurface();
51   FaceProp->SetInterpolation(1);
52   FaceProp->SetAmbient(1.0);
53   FaceProp->SetDiffuse(1.0);
54   FaceProp->SetSpecular(0.4);
55   FaceProp->SetAmbientColor(0.329412, 0.223529, 0.027451);
56   FaceProp->SetDiffuseColor(0.780392, 0.568627, 0.113725);
57   FaceProp->SetSpecularColor(0.992157, 0.941176, 0.807843);
58
59   // Wireframe for iso
60   IsoProp->SetRepresentationToWireframe();
61   IsoProp->SetAmbientColor(0.5, 0.5, 0.5);
62   IsoProp->SetDiffuseColor(0.5, 0.5, 0.5);
63   IsoProp->SetSpecularColor(0.5, 0.5, 0.5);
64
65   // Wireframe for iso
66   IsoPVProp->SetRepresentationToWireframe();
67   IsoPVProp->SetAmbientColor(0, 1, 1);
68   IsoPVProp->SetDiffuseColor(0, 1, 1);
69   IsoPVProp->SetSpecularColor(0, 1, 1);
70
71   // Wireframe for shared edge 
72   EdgeSProp->SetRepresentationToWireframe();
73   EdgeSProp->SetAmbientColor(1, 1, 0);
74   EdgeSProp->SetDiffuseColor(1, 1, 0);
75   EdgeSProp->SetSpecularColor(1, 1, 0);
76
77   // Wireframe for free edge 
78   EdgeFProp->SetRepresentationToWireframe();
79   EdgeFProp->SetAmbientColor(0, 1, 0);
80   EdgeFProp->SetDiffuseColor(0, 1, 0);
81   EdgeFProp->SetSpecularColor(0, 1, 0);
82
83   // Wireframe for isolated edge 
84   EdgeIProp->SetRepresentationToWireframe();
85   EdgeIProp->SetAmbientColor(1, 0, 0);
86   EdgeIProp->SetDiffuseColor(1, 0, 0);
87   EdgeIProp->SetSpecularColor(1, 0, 0);
88
89   // Wireframe for Preview edge 
90   EdgePVProp->SetRepresentationToWireframe();
91   EdgePVProp->SetAmbientColor(0, 1, 1);
92   EdgePVProp->SetDiffuseColor(0, 1, 1);
93   EdgePVProp->SetSpecularColor(0, 1, 1);
94
95   // Wireframe for vertex 
96   VertexProp->SetRepresentationToWireframe();
97   VertexProp->SetAmbientColor(1, 1, 0);
98   VertexProp->SetDiffuseColor(1, 1, 0);
99   VertexProp->SetSpecularColor(1, 1, 0);
100
101   // Wireframe for vertex 
102   VertexPVProp->SetRepresentationToWireframe();
103   VertexPVProp->SetAmbientColor(0, 1, 1);
104   VertexPVProp->SetDiffuseColor(0, 1, 1);
105   VertexPVProp->SetSpecularColor(0, 1, 1);
106 }
107
108
109 void GEOM_AssemblyBuilder::MeshShape(const TopoDS_Shape myShape,
110                                          Standard_Real deflection,
111                                          Standard_Boolean forced)
112 {
113   // Mesh the shape if necessary
114   Standard_Boolean alreadymesh = Standard_True;
115   TopExp_Explorer ex;
116   TopLoc_Location aLoc;
117
118   for (ex.Init(myShape, TopAbs_FACE); ex.More(); ex.Next()) {
119     const TopoDS_Face& aFace = TopoDS::Face(ex.Current());
120     Handle(Poly_Triangulation) aPoly = BRep_Tool::Triangulation(aFace,aLoc);
121     if(aPoly.IsNull()) { alreadymesh = Standard_False; break; }
122   }
123
124   if(!alreadymesh || forced) {
125     if(deflection<=0) {
126       // Compute default deflection
127       Bnd_Box B;
128       BRepBndLib::Add(myShape, B);
129       Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
130       B.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
131       deflection = MAX3( aXmax-aXmin , aYmax-aYmin , aZmax-aZmin) * 0.001 *4;
132     }
133     BRepMesh_IncrementalMesh MESH(myShape,deflection);
134   }
135 }
136
137
138
139 vtkActorCollection* GEOM_AssemblyBuilder::BuildActors(const TopoDS_Shape& myShape,
140                                                           Standard_Real deflection,
141                                                           Standard_Integer mode,
142                                                           Standard_Boolean forced) {
143
144   vtkActorCollection* AISActors = vtkActorCollection::New();
145
146   // Create graphics properties
147
148   vtkProperty* IsoProp = vtkProperty::New();
149   vtkProperty* FaceProp = vtkProperty::New();
150   vtkProperty* EdgeFProp = vtkProperty::New();
151   vtkProperty* EdgeSProp = vtkProperty::New();
152   vtkProperty* EdgeIProp = vtkProperty::New();
153   vtkProperty* VertexProp = vtkProperty::New();
154
155   vtkProperty* IsoPVProp = vtkProperty::New();
156   vtkProperty* EdgePVProp = vtkProperty::New();
157   vtkProperty* VertexPVProp = vtkProperty::New();
158
159   InitProperties(IsoProp,FaceProp,EdgeFProp,EdgeSProp,EdgeIProp,VertexProp,IsoPVProp,EdgePVProp,VertexPVProp);
160
161   MeshShape(myShape,deflection,forced);
162
163   if ( myShape.ShapeType() <= 4 ) {
164     
165     // FACE Actor
166     // look if edges are free or shared 
167     TopTools_IndexedDataMapOfShapeListOfShape edgemap;
168     TopExp::MapShapesAndAncestors(myShape,TopAbs_EDGE,TopAbs_FACE,edgemap);
169     
170     TopExp_Explorer ex;
171     
172     for (ex.Init(myShape, TopAbs_FACE); ex.More(); ex.Next()) {
173       
174       GEOM_Actor* FaceActor = GEOM_Actor::New();
175       FaceActor->SetShadingProperty(FaceProp);
176       FaceActor->SetWireframeProperty(IsoProp);
177
178       FaceActor->SetPreviewProperty(IsoPVProp);
179       
180       FaceActor->setInputShape(ex.Current(),deflection,mode);
181       
182       AISActors->AddItem(FaceActor);
183       
184       TopExp_Explorer ex2;
185       for (ex2.Init(ex.Current(), TopAbs_EDGE); ex2.More(); ex2.Next()) {
186         const TopoDS_Edge& aEdge = TopoDS::Edge(ex2.Current());
187         
188         if (BRep_Tool::Degenerated(aEdge)) {    
189           continue;
190         }
191         
192         // compute the number of faces
193         Standard_Integer nbf = edgemap.FindFromKey(ex2.Current()).Extent();
194         GEOM_Actor* EdgeActor = GEOM_Actor::New();
195         EdgeActor->SubShapeOn();
196         EdgeActor->setInputShape(ex2.Current(),deflection,mode);
197         switch (nbf) {
198           
199         case 0 : // isolated edge
200           {
201             EdgeActor->SetShadingProperty(EdgeIProp);
202             EdgeActor->SetWireframeProperty(EdgeIProp);
203           }
204           break;
205           
206         case 1 :// edge in only one face
207           {
208             EdgeActor->SetShadingProperty(EdgeFProp);
209             EdgeActor->SetWireframeProperty(EdgeFProp);
210           }
211           break;
212           
213         default :   // edge shared by at least two faces      
214           {
215             EdgeActor->SetShadingProperty(EdgeSProp);
216             EdgeActor->SetWireframeProperty(EdgeSProp);
217           }
218         }
219         EdgeActor->SetPreviewProperty(EdgePVProp);
220         AISActors->AddItem(EdgeActor);
221       }
222     }
223   } else if ( myShape.ShapeType() == TopAbs_WIRE ) { // WIRE Actor
224     TopExp_Explorer ex;
225     for (ex.Init(myShape, TopAbs_EDGE); ex.More(); ex.Next()) {
226       const TopoDS_Edge& aEdge = TopoDS::Edge(ex.Current());
227         
228       if (BRep_Tool::Degenerated(aEdge)) {    
229         continue;
230       }
231         
232       GEOM_Actor* EdgeActor = GEOM_Actor::New();
233       EdgeActor->setInputShape(ex.Current(),deflection,mode);
234       EdgeActor->SetShadingProperty(EdgeIProp);
235       EdgeActor->SetWireframeProperty(EdgeIProp);
236       EdgeActor->SetPreviewProperty(EdgePVProp);
237       
238       AISActors->AddItem(EdgeActor);
239     }
240   } else if ( myShape.ShapeType() == TopAbs_EDGE ) { // EDGE Actor
241     GEOM_Actor* EdgeActor = GEOM_Actor::New();
242     EdgeActor->setInputShape(myShape,deflection,mode);
243     EdgeActor->SetShadingProperty(EdgeIProp);
244     EdgeActor->SetWireframeProperty(EdgeIProp);
245     EdgeActor->SetPreviewProperty(EdgePVProp);
246     
247     AISActors->AddItem(EdgeActor);
248   } else if ( myShape.ShapeType() == TopAbs_VERTEX ) { // VERTEX Actor
249     GEOM_Actor* VertexActor = GEOM_Actor::New();
250     VertexActor->setInputShape(myShape,deflection,mode);
251     VertexActor->SetShadingProperty(VertexProp);
252     VertexActor->SetWireframeProperty(VertexProp);
253     VertexActor->SetPreviewProperty(VertexPVProp);
254     
255     AISActors->AddItem(VertexActor);
256   
257   } 
258   
259   return AISActors;
260
261 }
262
263
264
265 //-------------------------------------------------------------
266 // BUILD ASSEMBLY
267 //-------------------------------------------------------------
268 vtkAssembly*  GEOM_AssemblyBuilder::BuildAssembly(const TopoDS_Shape& myShape,
269                                                       Standard_Real deflection,
270                                                       Standard_Integer mode,
271                                                       Standard_Boolean forced)
272 {
273   // Create a new vtkAssembly
274
275   vtkAssembly* myVTKShape = vtkAssembly::New();
276
277
278   // Create graphics properties
279
280   vtkProperty* IsoProp = vtkProperty::New();
281   vtkProperty* FaceProp = vtkProperty::New();
282   vtkProperty* EdgeFProp = vtkProperty::New();
283   vtkProperty* EdgeSProp = vtkProperty::New();
284   vtkProperty* EdgeIProp = vtkProperty::New();
285   vtkProperty* VertexProp = vtkProperty::New();
286   vtkProperty* EdgePVProp = vtkProperty::New();
287   vtkProperty* VertexPVProp = vtkProperty::New();
288   vtkProperty* IsoPVProp = vtkProperty::New();
289
290   InitProperties(IsoProp,FaceProp,EdgeFProp,EdgeSProp,EdgeIProp,VertexProp,IsoPVProp,EdgePVProp,VertexPVProp);
291
292   MeshShape(myShape,deflection,forced);
293
294   
295   // FACE Actor
296   
297   // look if edges are free or shared 
298   TopTools_IndexedDataMapOfShapeListOfShape edgemap;
299   TopExp::MapShapesAndAncestors(myShape,TopAbs_EDGE,TopAbs_FACE,edgemap);
300   
301   TopExp_Explorer ex;
302
303   for (ex.Init(myShape, TopAbs_FACE); ex.More(); ex.Next()) {
304     //const TopoDS_Face& aFace = TopoDS::Face(ex.Current());
305     
306     GEOM_Actor* FaceActor = GEOM_Actor::New();
307     FaceActor->SetShadingProperty(FaceProp);
308     FaceActor->SetWireframeProperty(IsoProp);
309     
310     vtkAssembly* myFaceAssembly = vtkAssembly::New();
311
312    
313     FaceActor->setInputShape(ex.Current(),deflection,mode);
314     myFaceAssembly->AddPart(FaceActor);
315     
316     TopExp_Explorer ex2;
317     for (ex2.Init(ex.Current(), TopAbs_EDGE); ex2.More(); ex2.Next()) {
318       const TopoDS_Edge& aEdge = TopoDS::Edge(ex2.Current());
319  
320       if (BRep_Tool::Degenerated(aEdge)) {    
321         continue;
322       }
323       
324     
325       // compute the number of faces
326       Standard_Integer nbf = edgemap.FindFromKey(ex2.Current()).Extent();
327       GEOM_Actor* EdgeActor = GEOM_Actor::New();
328       switch (nbf) {
329         
330       case 0 : // isolated edge
331         {
332           EdgeActor->SetShadingProperty(EdgeIProp);
333           EdgeActor->SetWireframeProperty(EdgeIProp);
334         }
335         break;
336         
337       case 1 :// edge in only one face
338         {
339           EdgeActor->SetShadingProperty(EdgeFProp);
340           EdgeActor->SetWireframeProperty(EdgeFProp);
341         }
342         break;
343         
344       default :   // edge shared by at least two faces      
345         {
346           EdgeActor->SetShadingProperty(EdgeSProp);
347           EdgeActor->SetWireframeProperty(EdgeSProp);
348         }
349       }
350   
351       EdgeActor->setInputShape(ex2.Current(),deflection,mode);
352       myFaceAssembly->AddPart(EdgeActor);
353     }
354     myVTKShape->AddPart(myFaceAssembly);
355   }
356   
357   return myVTKShape;
358  
359 }
360
361 //-------------------------------------------------------------
362 // CHANGE SPECIFIC DISPLAY MODE
363 //-------------------------------------------------------------
364 void  GEOM_AssemblyBuilder::SwitchDisplayMode(vtkAssembly* aOCCAssembly)
365 {
366 }
367
368 void  GEOM_AssemblyBuilder::SwitchDisplayMode(vtkActorCollection* aOCCAssembly)
369 {
370 }
371
372 //-------------------------------------------------------------
373 // DISPLAY/ERASE
374 //-------------------------------------------------------------
375
376 void GEOM_AssemblyBuilder::DisplayErase(vtkAssembly* mySALOMEAssembly)
377 {
378 }
379
380
381 void GEOM_AssemblyBuilder::DisplayErase(vtkActorCollection* mySALOMEActors)
382 {
383 }
384
385
386
387
388