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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : GEOM_Actor.cxx
25 // Author : Christophe ATTANASIO
31 \class GEOM_Actor GEOM_Actor.h
32 \brief This class allows to display an OpenCASCADE CAD model in a VTK viewer.
35 #include "GEOM_Actor.h"
37 // OpenCASCADE Includes
38 #include "GEOM_OCCReader.h"
39 #include <BRep_Tool.hxx>
41 //-------------------------------------------------------------
43 //-------------------------------------------------------------
46 GEOM_Actor* GEOM_Actor::New()
48 // First try to create the object from the vtkObjectFactory
49 vtkObject* ret = vtkObjectFactory::CreateInstance("GEOM_Actor");
52 return (GEOM_Actor*)ret;
54 // If the factory was unable to create the object, then create it here.
55 return new GEOM_Actor;
59 GEOM_Actor::GEOM_Actor()
61 this->Device = vtkActor::New();
63 this->WireframeMapper = NULL;
64 this->ShadingMapper = NULL;
66 this->ShadingProperty = NULL;
67 this->WireframeProperty = NULL;
75 this->HighlightProperty = NULL;
76 this->ishighlighted = false;
78 this->subshape = false;
81 GEOM_Actor::~GEOM_Actor()
83 if (WireframeMapper != NULL)
84 WireframeMapper->Delete();
85 if (ShadingMapper != NULL)
86 ShadingMapper->Delete();
87 if (ShadingProperty != NULL)
88 ShadingProperty->Delete();
89 if (WireframeProperty != NULL)
90 WireframeProperty->Delete();
91 if (HighlightProperty != NULL)
92 HighlightProperty->Delete();
96 void GEOM_Actor::ShallowCopy(vtkProp *prop)
98 GEOM_Actor *f = GEOM_Actor::SafeDownCast(prop);
101 this->setInputShape(f->getTopo(),f->getDeflection(),f->getDisplayMode());
102 this->setName( f->getName() );
104 this->setIO( f->getIO() );
105 this->ShadingMapper = NULL;
106 this->WireframeMapper = NULL;
110 this->ShadingMapper = NULL;
111 this->WireframeMapper = NULL;
115 this->SALOME_Actor::ShallowCopy(prop);
118 //-------------------------------------------------------------
120 //-------------------------------------------------------------
123 void GEOM_Actor::setDisplayMode(int thenewmode) {
124 myDisplayMode = thenewmode;
125 if ( thenewmode >=1 ) {
126 if ((myShape.ShapeType() == TopAbs_WIRE) ||
127 (myShape.ShapeType() == TopAbs_EDGE) ||
128 (myShape.ShapeType() == TopAbs_VERTEX)) {
130 CreateWireframeMapper();
134 CreateShadingMapper();
136 CreateWireframeMapper();
139 void GEOM_Actor::setDeflection(double adef) {
143 void GEOM_Actor::setInputShape(const TopoDS_Shape& aShape,double adef,int imode) {
146 setDisplayMode(imode);
149 //-------------------------------------------------------------
151 //-------------------------------------------------------------
153 const TopoDS_Shape& GEOM_Actor::getTopo() {
157 double GEOM_Actor::getDeflection() {
161 void GEOM_Actor::SetWireframeProperty(vtkProperty* Prop) {
162 this->WireframeProperty = Prop;
165 void GEOM_Actor::SetShadingProperty(vtkProperty* Prop) {
166 this->ShadingProperty = Prop;
170 //-------------------------------------------------------------
171 // Mapper creating function
172 //-------------------------------------------------------------
173 void GEOM_Actor::CreateMapper(int theMode) {
174 if(myShape.ShapeType() == TopAbs_VERTEX) {
175 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(myShape));
176 this->SetPosition(aPnt.X(),aPnt.Y(),aPnt.Z());
178 GEOM_OCCReader* aread = GEOM_OCCReader::New();
179 aread->setTopo(myShape);
180 aread->setDisplayMode(theMode);
181 aread->GetOutput()->ReleaseDataFlagOn();
183 vtkPolyDataMapper* aMapper = vtkPolyDataMapper::New();
185 aMapper->SetInput(aread->GetOutput());
187 vtkPolyDataNormals *normals = vtkPolyDataNormals::New();
188 normals->SetInput(aread->GetOutput());
189 aMapper->SetInput(normals->GetOutput());
192 this->SetMapper(theMode == 0? WireframeMapper = aMapper : ShadingMapper = aMapper);
195 void GEOM_Actor::CreateShadingMapper() {
200 void GEOM_Actor::CreateWireframeMapper() {
204 //-------------------------------------------------------------
206 //-------------------------------------------------------------
208 void GEOM_Actor::Render(vtkRenderer *ren, vtkMapper *Mapper)
210 /* render the property */
211 if (!this->Property) {
212 // force creation of a property
214 this->Property->SetInterpolation(1);
215 this->Property->SetRepresentationToSurface();
216 this->Property->SetAmbient(0.3);
217 this->Property->SetAmbientColor(0.88,0.86,0.2);
218 this->Property->SetDiffuseColor(0.99,0.7,0.21);
219 this->Property->SetSpecularColor(0.99,0.98,0.83);
223 if(myDisplayMode >= 1) {
225 this->Property = ShadingProperty;
228 this->Property = WireframeProperty;
232 this->Property = PreviewProperty;
235 this->Property->Render(this, ren);
236 if (this->BackfaceProperty) {
237 this->BackfaceProperty->BackfaceRender(this, ren);
238 this->Device->SetBackfaceProperty(this->BackfaceProperty);
240 this->Device->SetProperty(this->Property);
241 // Store information on time it takes to render.
242 // We might want to estimate time from the number of polygons in mapper.
243 if(myDisplayMode >= 1) {
244 if((myShape.ShapeType() == TopAbs_WIRE) ||
245 (myShape.ShapeType() == TopAbs_EDGE) ||
246 (myShape.ShapeType() == TopAbs_VERTEX)) {
248 if(WireframeMapper==NULL) CreateWireframeMapper();
253 if(ShadingMapper==NULL) CreateShadingMapper();
257 if(WireframeMapper==NULL) CreateWireframeMapper();
259 if(myShape.ShapeType() == TopAbs_VERTEX) {
261 //The parameter determine size of vertex actor relate to diagonal of RendererWindow
262 static float delta = 0.01;
263 float X1 = -1, Y1 = -1, Z1 = 0;
264 ren->ViewToWorld(X1,Y1,Z1);
265 float X2 = +1, Y2 = +1, Z2 = 0;
266 ren->ViewToWorld(X2,Y2,Z2);
267 Z2 = sqrt((X2-X1)*(X2-X1) + (Y2-Y1)*(Y2-Y1) + (Z2-Z1)*(Z2-Z1));
268 this->SetScale(Z2*delta);
270 vtkMatrix4x4 *aMatrix = vtkMatrix4x4::New();
271 this->GetMatrix(ren->GetActiveCamera(), aMatrix);
272 this->Device->SetUserMatrix(aMatrix);
273 this->Device->Render(ren,this->Mapper);
276 this->Device->Render(ren, this->Mapper);
277 this->EstimatedRenderTime = WireframeMapper->GetTimeToDraw();
281 void GEOM_Actor::SubShapeOn()
285 void GEOM_Actor::SubShapeOff()
290 //-------------------------------------------------------------
292 //-------------------------------------------------------------
294 void GEOM_Actor::SetOpacity(float opa)
296 //HighlightProperty->SetOpacity(opa);
297 SALOME_Actor::SetOpacity(opa);
298 ShadingProperty->SetOpacity(opa);
301 float GEOM_Actor::GetOpacity() {
302 return ShadingProperty->GetOpacity();
305 //-------------------------------------------------------------
307 //-------------------------------------------------------------
308 void GEOM_Actor::SetColor(float r,float g,float b) {
309 ShadingProperty->SetColor(r,g,b);
312 void GEOM_Actor::GetColor(float& r,float& g,float& b) {
314 ShadingProperty->GetColor(color);
320 //-------------------------------------------------------------
322 //-------------------------------------------------------------
324 void GEOM_Actor::highlight(Standard_Boolean highlight) {
326 if(highlight && !ishighlighted) {
328 // build highlight property is necessary
329 if(HighlightProperty==NULL) {
330 HighlightProperty = vtkProperty::New();
331 HighlightProperty->SetAmbient(0.5);
332 HighlightProperty->SetDiffuse(0.3);
333 HighlightProperty->SetSpecular(0.2);
334 HighlightProperty->SetRepresentationToSurface();
335 HighlightProperty->SetAmbientColor(1, 1, 1);
336 HighlightProperty->SetDiffuseColor(1, 1, 1);
337 HighlightProperty->SetSpecularColor(0.5, 0.5, 0.5);
340 this->Property = HighlightProperty;
343 else if (!highlight) {
346 if(myDisplayMode==1) {
347 //unhilight in shading
348 this->Property = ShadingProperty;
351 //unhilight in wireframe
352 this->Property = WireframeProperty;
358 bool GEOM_Actor::hasHighlight()
363 void GEOM_Actor::SetHighlightProperty(vtkProperty* Prop) {
364 this->HighlightProperty = Prop;
368 void GEOM_Actor::ReleaseGraphicsResources(vtkWindow *renWin)
370 vtkActor::ReleaseGraphicsResources(renWin);
372 // broadcast the message down to the individual LOD mappers
374 if(WireframeMapper) this->WireframeMapper->ReleaseGraphicsResources(renWin);
375 if(ShadingMapper) this->ShadingMapper->ReleaseGraphicsResources(renWin);
379 // Copy the follower's composite 4x4 matrix into the matrix provided.
380 void GEOM_Actor::GetMatrix(vtkCamera* theCam, vtkMatrix4x4 *result)
383 double Rx[3], Ry[3], Rz[3], p1[3];
384 vtkMatrix4x4 *matrix = vtkMatrix4x4::New();
388 this->GetOrientation();
389 this->Transform->Push();
390 this->Transform->PostMultiply();
391 this->Transform->Identity();
393 // apply user defined matrix last if there is one
394 if (this->UserMatrix)
396 this->Transform->Concatenate(this->UserMatrix);
399 this->Transform->Translate(-this->Origin[0],
403 this->Transform->Scale(this->Scale[0],
408 this->Transform->RotateY(this->Orientation[1]);
409 this->Transform->RotateX(this->Orientation[0]);
410 this->Transform->RotateZ(this->Orientation[2]);
416 pos = theCam->GetPosition();
417 vup = theCam->GetViewUp();
419 if (theCam->GetParallelProjection())
421 theCam->GetDirectionOfProjection(Rz);
426 (pos[0] - this->Position[0])*(pos[0] - this->Position[0]) +
427 (pos[1] - this->Position[1])*(pos[1] - this->Position[1]) +
428 (pos[2] - this->Position[2])*(pos[2] - this->Position[2]));
429 for (i = 0; i < 3; i++)
431 Rz[i] = (pos[i] - this->Position[i])/distance;
435 vtkMath::Cross(vup,Rz,Rx);
436 vtkMath::Normalize(Rx);
437 vtkMath::Cross(Rz,Rx,Ry);
439 matrix->Element[0][0] = Rx[0];
440 matrix->Element[1][0] = Rx[1];
441 matrix->Element[2][0] = Rx[2];
442 matrix->Element[0][1] = Ry[0];
443 matrix->Element[1][1] = Ry[1];
444 matrix->Element[2][1] = Ry[2];
445 matrix->Element[0][2] = Rz[0];
446 matrix->Element[1][2] = Rz[1];
447 matrix->Element[2][2] = Rz[2];
449 this->Transform->Concatenate(matrix);
452 // translate to projection reference point PRP
453 // this is the camera's position blasted through
454 // the current matrix
455 p1[0] = this->Origin[0] + this->Position[0];
456 p1[1] = this->Origin[1] + this->Position[1];
457 p1[2] = this->Origin[2] + this->Position[2];
459 this->Transform->Translate(p1[0],p1[1],p1[2]);
460 this->Transform->GetMatrix(result);
463 this->Transform->Pop();