1 // GEOM OBJECT : interactive object for Geometry entities visualization
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 // File : GEOM_AssemblyBuilder.cxx
25 // Author : Christophe ATTANASIO
30 \class GEOM_AssemblyBuilder GEOM_AssemblyBuilder.h
34 #include "GEOM_AssemblyBuilder.h"
35 #include "GEOM_Actor.h"
37 #include <SUIT_Session.h>
38 #include <SUIT_ResourceMgr.h>
40 #include <vtkProperty.h>
41 #include <vtkAssembly.h>
42 #include <vtkActorCollection.h>
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>
53 #include <TopTools_ListOfShape.hxx>
54 #include <TopoDS_Iterator.hxx>
59 #include "utilities.h"
64 #define MAX2(X, Y) ( Abs(X) > Abs(Y)? Abs(X) : Abs(Y) )
65 #define MAX3(X, Y, Z) ( MAX2 ( MAX2(X,Y) , Z) )
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)
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);
91 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
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);
105 IsoPVProp->SetRepresentationToWireframe();
106 IsoPVProp->SetAmbientColor(0, 1, 1);
107 IsoPVProp->SetDiffuseColor(0, 1, 1);
108 IsoPVProp->SetSpecularColor(0, 1, 1);
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);
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);
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);
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);
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);
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);
163 void GEOM_AssemblyBuilder::MeshShape(const TopoDS_Shape myShape,
164 Standard_Real deflection,
165 Standard_Boolean forced)
167 // Mesh the shape if necessary
168 Standard_Boolean alreadymesh = Standard_True;
170 TopLoc_Location aLoc;
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; }
178 if(!alreadymesh || forced) {
180 // Compute default deflection
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;
188 BRepMesh_IncrementalMesh MESH(myShape,deflection);
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)
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);
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);
219 aGeomActor->Delete();
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();
235 // Create graphics properties
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();
244 vtkProperty* IsoPVProp = vtkProperty::New();
245 vtkProperty* EdgePVProp = vtkProperty::New();
246 vtkProperty* VertexPVProp = vtkProperty::New();
248 InitProperties(IsoProp,FaceProp,EdgeFProp,EdgeSProp,EdgeIProp,VertexProp,IsoPVProp,EdgePVProp,VertexPVProp);
250 MeshShape(myShape,deflection,forced);
252 if ( myShape.ShapeType() <= 4 && myShape.ShapeType() != TopAbs_COMPOUND) {
255 // look if edges are free or shared
256 TopTools_IndexedDataMapOfShapeListOfShape edgemap;
257 TopExp::MapShapesAndAncestors(myShape,TopAbs_EDGE,TopAbs_FACE,edgemap);
261 for (ex.Init(myShape, TopAbs_FACE); ex.More(); ex.Next()) {
263 GEOM_Actor* FaceActor = GEOM_Actor::New();
264 FaceActor->SetShadingProperty(FaceProp);
265 FaceActor->SetWireframeProperty(IsoProp);
267 FaceActor->SetPreviewProperty(IsoPVProp);
269 FaceActor->setInputShape(ex.Current(),deflection,mode);
271 AISActors->AddItem(FaceActor);
274 for (ex2.Init(ex.Current(), TopAbs_EDGE); ex2.More(); ex2.Next()) {
275 const TopoDS_Edge& aEdge = TopoDS::Edge(ex2.Current());
277 if (BRep_Tool::Degenerated(aEdge)) {
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);
289 case 0 : // isolated edge
291 EdgeActor->SetShadingProperty(EdgeIProp);
292 EdgeActor->SetWireframeProperty(EdgeIProp);
296 case 1 :// edge in only one face
298 EdgeActor->SetShadingProperty(EdgeFProp);
299 EdgeActor->SetWireframeProperty(EdgeFProp);
303 default : // edge shared by at least two faces
305 EdgeActor->SetShadingProperty(EdgeSProp);
306 EdgeActor->SetWireframeProperty(EdgeSProp);
310 EdgeActor->SetPreviewProperty(EdgePVProp);
311 AISActors->AddItem(EdgeActor);
314 } else if ( myShape.ShapeType() == TopAbs_WIRE ) { // WIRE Actor
316 for (ex.Init(myShape, TopAbs_EDGE); ex.More(); ex.Next()) {
317 const TopoDS_Edge& aEdge = TopoDS::Edge(ex.Current());
319 if (BRep_Tool::Degenerated(aEdge)) {
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);
329 AISActors->AddItem(EdgeActor);
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);
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);
346 AISActors->AddItem(VertexActor);
358 //-------------------------------------------------------------
360 //-------------------------------------------------------------
361 vtkAssembly* GEOM_AssemblyBuilder::BuildAssembly(const TopoDS_Shape& myShape,
362 Standard_Real deflection,
363 Standard_Integer mode,
364 Standard_Boolean forced)
366 // Create a new vtkAssembly
368 vtkAssembly* myVTKShape = vtkAssembly::New();
370 // Create graphics properties
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();
382 InitProperties(IsoProp,FaceProp,EdgeFProp,EdgeSProp,EdgeIProp,VertexProp,IsoPVProp,EdgePVProp,VertexPVProp);
384 MeshShape(myShape,deflection,forced);
389 // look if edges are free or shared
390 TopTools_IndexedDataMapOfShapeListOfShape edgemap;
391 TopExp::MapShapesAndAncestors(myShape,TopAbs_EDGE,TopAbs_FACE,edgemap);
395 for (ex.Init(myShape, TopAbs_FACE); ex.More(); ex.Next()) {
396 //const TopoDS_Face& aFace = TopoDS::Face(ex.Current());
398 GEOM_Actor* FaceActor = GEOM_Actor::New();
399 FaceActor->SetShadingProperty(FaceProp);
400 FaceActor->SetWireframeProperty(IsoProp);
402 vtkAssembly* myFaceAssembly = vtkAssembly::New();
405 FaceActor->setInputShape(ex.Current(),deflection,mode);
406 myFaceAssembly->AddPart(FaceActor);
409 for (ex2.Init(ex.Current(), TopAbs_EDGE); ex2.More(); ex2.Next()) {
410 const TopoDS_Edge& aEdge = TopoDS::Edge(ex2.Current());
412 if (BRep_Tool::Degenerated(aEdge)) {
417 // compute the number of faces
418 Standard_Integer nbf = edgemap.FindFromKey(ex2.Current()).Extent();
419 GEOM_Actor* EdgeActor = GEOM_Actor::New();
422 case 0 : // isolated edge
424 EdgeActor->SetShadingProperty(EdgeIProp);
425 EdgeActor->SetWireframeProperty(EdgeIProp);
429 case 1 :// edge in only one face
431 EdgeActor->SetShadingProperty(EdgeFProp);
432 EdgeActor->SetWireframeProperty(EdgeFProp);
436 default : // edge shared by at least two faces
438 EdgeActor->SetShadingProperty(EdgeSProp);
439 EdgeActor->SetWireframeProperty(EdgeSProp);
443 EdgeActor->setInputShape(ex2.Current(),deflection,mode);
444 myFaceAssembly->AddPart(EdgeActor);
446 myVTKShape->AddPart(myFaceAssembly);
455 //-------------------------------------------------------------
456 // CHANGE SPECIFIC DISPLAY MODE
457 //-------------------------------------------------------------
458 void GEOM_AssemblyBuilder::SwitchDisplayMode(vtkAssembly* aOCCAssembly)
462 void GEOM_AssemblyBuilder::SwitchDisplayMode(vtkActorCollection* aOCCAssembly)
466 //-------------------------------------------------------------
468 //-------------------------------------------------------------
470 void GEOM_AssemblyBuilder::DisplayErase(vtkAssembly* mySALOMEAssembly)
475 void GEOM_AssemblyBuilder::DisplayErase(vtkActorCollection* mySALOMEActors)