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 : VTKViewer_Trihedron.cxx
25 // Author : Alexey PETROV
29 #include "VTKViewer_Trihedron.h"
30 #include "utilities.h"
31 #include "SALOME_Actor.h"
35 #include <vtkMapper.h>
36 #include <vtkDataSet.h>
37 #include <vtkRenderer.h>
38 #include <vtkFollower.h>
39 #include <vtkRenderWindow.h>
40 #include <vtkObjectFactory.h>
43 #include <vtkProperty.h>
44 #include <vtkLineSource.h>
45 #include <vtkConeSource.h>
46 #include <vtkPolyDataMapper.h>
48 #include "VTKViewer_VectorText.h"
52 //==============================================================================
54 class VTKViewer_UnScaledActor: public vtkFollower{
55 VTKViewer_UnScaledActor(const VTKViewer_UnScaledActor&);
58 vtkTypeMacro(VTKViewer_UnScaledActor,vtkFollower);
59 static VTKViewer_UnScaledActor *New();
61 virtual void SetSize(int theSize);
62 virtual void Render(vtkRenderer *theRenderer);
65 VTKViewer_UnScaledActor();
69 vtkStandardNewMacro(VTKViewer_UnScaledActor);
71 VTKViewer_UnScaledActor::VTKViewer_UnScaledActor(){}
73 void VTKViewer_UnScaledActor::Render(vtkRenderer *theRenderer){
75 float P[2][3] = {{-1.0, -1.0, 0.0},{+1.0, +1.0, 0.0}};
76 theRenderer->ViewToWorld(P[0][0],P[0][1],P[0][2]);
77 theRenderer->ViewToWorld(P[1][0],P[1][1],P[1][2]);
78 float aWorldDiag = sqrt((P[1][0]-P[0][0])*(P[1][0]-P[0][0])+
79 (P[1][1]-P[0][1])*(P[1][1]-P[0][1])+
80 (P[1][2]-P[0][2])*(P[1][2]-P[0][2]));
81 int* aSize = theRenderer->GetRenderWindow()->GetSize();
82 float aWinDiag = sqrt(float(aSize[0]*aSize[0]+aSize[1]*aSize[1]));
83 vtkDataSet* aDataSet = GetMapper()->GetInput();
84 float aLength = aDataSet->GetLength();
85 float aScale = mySize*aWorldDiag/aWinDiag/aLength*sqrt(float(aSize[0])/float(aSize[1]));
88 vtkFollower::Render(theRenderer);
91 void VTKViewer_UnScaledActor::SetSize(int theSize){
95 //==============================================================================
97 // The base class for concreate Axis
98 // Its only duty is to give correct initialization and destruction
100 class VTKViewer_Axis : public vtkObject{
103 VTKViewer_Axis(const VTKViewer_Axis&);
104 virtual ~VTKViewer_Axis();
106 vtkTypeMacro(VTKViewer_Axis,vtkObject);
108 virtual void AddToRender(vtkRenderer* theRenderer){
109 theRenderer->AddActor(myLabelActor);
110 theRenderer->AddActor(myArrowActor);
111 theRenderer->AddActor(myLineActor);
114 virtual void SetVisibility(VTKViewer_Trihedron::TVisibility theVis);
115 virtual VTKViewer_Trihedron::TVisibility GetVisibility() {
119 virtual void SetCamera(vtkCamera* theCamera){
120 myLabelActor->SetCamera(theCamera);
123 virtual void SetProperty(vtkProperty* theProperty){
124 myLabelActor->SetProperty(theProperty);
125 myArrowActor->SetProperty(theProperty);
126 myLineActor->SetProperty(theProperty);
129 virtual void SetSize(float theSize);
131 virtual VTKViewer_UnScaledActor* GetLabel(){
135 virtual VTKViewer_UnScaledActor* GetArrow(){
140 VTKViewer_Trihedron::TVisibility myVisibility;
141 float myDir[3], myRot[3];
143 vtkActor *myLineActor;
144 VTKViewer_UnScaledActor *myArrowActor;
145 VTKViewer_UnScaledActor *myLabelActor;
147 vtkPolyDataMapper *myMapper[3];
148 vtkLineSource *myLineSource;
149 vtkConeSource *myConeSource;
150 VTKViewer_VectorText* myVectorText;
153 VTKViewer_Axis::VTKViewer_Axis(){
154 // Initialize the Line pipe-line representation
155 myLineSource = vtkLineSource::New();
156 myLineSource->SetPoint1(0.0,0.0,0.0);
158 myMapper[0] = vtkPolyDataMapper::New();
159 myMapper[0]->SetInput(myLineSource->GetOutput());
161 myLineActor = vtkActor::New();
162 myLineActor->SetMapper(myMapper[0]);
163 myLineActor->PickableOff();
165 // Initialize the Arrow pipe-line representation
166 myConeSource = vtkConeSource::New();
167 myConeSource->SetResolution(2);
168 myConeSource->SetAngle(10);
170 myMapper[1] = vtkPolyDataMapper::New();
171 myMapper[1]->SetInput(myConeSource->GetOutput());
173 myArrowActor = VTKViewer_UnScaledActor::New();
174 myArrowActor->SetMapper(myMapper[1]);
175 static int aArrowActorSize = 24;
176 myArrowActor->SetSize(aArrowActorSize);
177 myArrowActor->PickableOff();
179 // Initialize the Label pipe-line representation
180 myVectorText = VTKViewer_VectorText::New();
182 myMapper[2] = vtkPolyDataMapper::New();
183 myMapper[2]->SetInput(myVectorText->GetOutput());
185 myLabelActor = VTKViewer_UnScaledActor::New();
186 myLabelActor->SetMapper(myMapper[2]);
187 static int aLabelActorSize = 12;
188 myLabelActor->SetSize(aLabelActorSize);
189 myLabelActor->PickableOff();
191 // Initialise visibility param.
192 myVisibility = VTKViewer_Trihedron::eOn;
195 VTKViewer_Axis::~VTKViewer_Axis(){
196 // Destroy of the Label pipe-line representation
197 myLabelActor->Delete();
199 myMapper[2]->RemoveAllInputs();
200 myMapper[2]->Delete();
202 myVectorText->Delete();
204 // Destroy of the Arrow pipe-line representation
205 myArrowActor->Delete();
207 myMapper[1]->RemoveAllInputs();
208 myMapper[1]->Delete();
210 myConeSource->Delete();
212 // Destroy of the Arrow pipe-line representation
213 myLineActor->Delete();
215 myMapper[2]->RemoveAllInputs();
216 myMapper[2]->Delete();
218 myLineSource->Delete();
221 void VTKViewer_Axis::SetVisibility(VTKViewer_Trihedron::TVisibility theVis){
223 case VTKViewer_Trihedron::eOff:
224 case VTKViewer_Trihedron::eOn:
225 myLabelActor->SetVisibility(theVis);
226 myArrowActor->SetVisibility(theVis);
227 myLineActor->SetVisibility(theVis);
229 case VTKViewer_Trihedron::eOnlyLineOn:
230 myLabelActor->VisibilityOff();
231 myArrowActor->VisibilityOff();
232 myLineActor->VisibilityOn();
237 myVisibility = theVis;
240 void VTKViewer_Axis::SetSize(float theSize){
241 float aPosition[3] = {myDir[0]*theSize, myDir[1]*theSize, myDir[2]*theSize};
242 myLineSource->SetPoint2(aPosition);
244 myArrowActor->SetPosition(0.0,0.0,0.0);
245 myArrowActor->AddPosition(aPosition);
246 myArrowActor->SetOrientation(myRot);
248 myLabelActor->SetPosition(0.0,0.0,0.0);
249 myLabelActor->AddPosition(aPosition);
252 //==============================================================================
253 class VTKViewer_XAxis : public VTKViewer_Axis{
256 VTKViewer_XAxis(const VTKViewer_XAxis&);
258 vtkTypeMacro(VTKViewer_XAxis,VTKViewer_Axis);
259 static VTKViewer_XAxis *New();
262 vtkStandardNewMacro(VTKViewer_XAxis);
264 VTKViewer_XAxis::VTKViewer_XAxis(){
265 myDir[0] = 1.0; myDir[1] = 0.0; myDir[2] = 0.0;
266 myRot[0] = 0.0; myRot[1] = 0.0; myRot[2] = 0.0;
267 myVectorText->SetText("X");
268 vtkProperty* aProperty = vtkProperty::New();
269 aProperty->SetColor(1.0,0.0,0.0);
270 SetProperty(aProperty);
274 //==============================================================================
275 class VTKViewer_YAxis : public VTKViewer_Axis{
278 VTKViewer_YAxis(const VTKViewer_YAxis&);
280 vtkTypeMacro(VTKViewer_YAxis,VTKViewer_Axis);
281 static VTKViewer_YAxis *New();
284 vtkStandardNewMacro(VTKViewer_YAxis);
286 VTKViewer_YAxis::VTKViewer_YAxis(){
287 myDir[0] = 0.0; myDir[1] = 1.0; myDir[2] = 0.0;
288 myRot[0] = 0.0; myRot[1] = 0.0; myRot[2] = 90.;
289 myVectorText->SetText("Y");
290 vtkProperty* aProperty = vtkProperty::New();
291 aProperty->SetColor(0.0,1.0,0.0);
292 SetProperty(aProperty);
296 //==============================================================================
297 class VTKViewer_ZAxis : public VTKViewer_Axis{
300 VTKViewer_ZAxis(const VTKViewer_ZAxis&);
302 vtkTypeMacro(VTKViewer_ZAxis,VTKViewer_Axis);
303 static VTKViewer_ZAxis *New();
306 vtkStandardNewMacro(VTKViewer_ZAxis);
308 VTKViewer_ZAxis::VTKViewer_ZAxis(){
309 myDir[0] = 0.0; myDir[1] = 0.0; myDir[2] = 1.0;
310 myRot[0] = 0.0; myRot[1] = -90; myRot[2] = 0.0;
311 myVectorText->SetText("Z");
312 vtkProperty* aProperty = vtkProperty::New();
313 aProperty->SetColor(0.0,0.0,1.0);
314 SetProperty(aProperty);
318 //==============================================================================
320 vtkStandardNewMacro(VTKViewer_Trihedron);
322 VTKViewer_Trihedron::VTKViewer_Trihedron(){
323 myPresent = vtkActorCollection::New();
324 myAxis[0] = VTKViewer_XAxis::New();
325 myAxis[1] = VTKViewer_YAxis::New();
326 myAxis[2] = VTKViewer_ZAxis::New();
327 static float aSize = 100;
331 VTKViewer_Trihedron::~VTKViewer_Trihedron(){
332 myPresent->RemoveAllItems();
334 for(int i = 0; i < 3; i++)
338 void VTKViewer_Trihedron::SetSize(float theSize){
340 for(int i = 0; i < 3; i++)
341 myAxis[i]->SetSize(theSize);
344 void VTKViewer_Trihedron::SetVisibility(TVisibility theVis){
345 for(int i = 0; i < 3; i++)
346 myAxis[i]->SetVisibility(theVis);
349 VTKViewer_Trihedron::TVisibility VTKViewer_Trihedron::GetVisibility(){
350 return myAxis[0]->GetVisibility();
353 void VTKViewer_Trihedron::AddToRender(vtkRenderer* theRenderer){
354 vtkCamera* aCamera = theRenderer->GetActiveCamera();
355 for(int i = 0; i < 3; i++){
356 myAxis[i]->AddToRender(theRenderer);
357 myAxis[i]->SetCamera(aCamera);
361 void VTKViewer_Trihedron::RemoveFromRender(vtkRenderer* theRenderer){
362 myPresent->InitTraversal();
363 while(vtkActor* anActor = myPresent->GetNextActor())
364 theRenderer->RemoveActor(anActor);
367 int VTKViewer_Trihedron::GetVisibleActorCount(vtkRenderer* theRenderer){
368 //TVisibility aVis = GetVisibility();
369 //SetVisibility(eOff);
370 vtkActorCollection* aCollection = theRenderer->GetActors();
371 aCollection->InitTraversal();
373 while(vtkActor* prop = aCollection->GetNextActor())
374 if( prop->GetVisibility())
375 if(SALOME_Actor* anActor = SALOME_Actor::SafeDownCast(prop))
376 if(!anActor->IsInfinitive())
378 //int aCount = theRenderer->VisibleActorCount();
379 //SetVisibility(aVis);
383 void VTKViewer_Trihedron::Render(vtkRenderer* theRenderer){
384 for(int i = 0; i < 3; i++)
386 myAxis[i]->GetLabel()->Render(theRenderer);
387 myAxis[i]->GetArrow()->Render(theRenderer);