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