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