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