]> SALOME platform Git repositories - modules/visu.git/blob - src/PIPELINE/VISU_PipeLineUtils.cxx
Salome HOME
Issue 0019818: EDF 703 SMESH VISU : Display Mesh Groups names in viewer (as a caption)
[modules/visu.git] / src / PIPELINE / VISU_PipeLineUtils.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 //  VISU OBJECT : interactive object for VISU entities implementation
23 // File:    VISU_PipeLine.hxx
24 // Author:  Alexey PETROV
25 // Module : VISU
26 //
27 #include "VISU_PipeLineUtils.hxx"
28
29 #include "VISU_OpenGLPointSpriteMapper.hxx"
30
31 #include <vtkDataSetMapper.h>
32 #include <vtkPolyDataMapper.h>
33
34 namespace VISU
35 {
36   //----------------------------------------------------------------------------
37   void
38   Mul(const vtkFloatingPointType A[3], 
39       vtkFloatingPointType b, 
40       vtkFloatingPointType C[3])
41   { // A*b;
42     for(int i = 0; i < 3; i++)  
43       C[i] = A[i]*b;
44   }
45
46
47   //----------------------------------------------------------------------------
48   void
49   Sub(const vtkFloatingPointType A[3], 
50             const vtkFloatingPointType B[3], 
51             vtkFloatingPointType C[3])
52   { //A-B
53     for(int i = 0; i < 3; i++)  
54       C[i] = A[i] - B[i];
55   }
56
57
58   //----------------------------------------------------------------------------
59   bool
60   CheckIsSameValue(vtkFloatingPointType theTarget,
61                    vtkFloatingPointType theSource)
62   {
63     static vtkFloatingPointType TOL = 10.0 / VTK_LARGE_FLOAT;
64     if(fabs(theTarget - theSource)  < TOL)
65       return true;
66     return false;
67   }
68   
69
70   //----------------------------------------------------------------------------
71   bool
72   CheckIsSameRange(vtkFloatingPointType* theTarget,
73                    vtkFloatingPointType* theSource)
74   {
75     return CheckIsSameValue(theTarget[0], theSource[0]) &&
76       CheckIsSameValue(theTarget[1], theSource[1]);
77   }
78   
79
80   //----------------------------------------------------------------------------
81   void
82   CopyMapper(vtkMapper* theTarget, 
83              vtkMapper* theSource,
84              bool theIsCopyInput)
85   {
86     // To customize vtkMapper::ShallowCopy ...
87     theTarget->SetLookupTable(theSource->GetLookupTable());
88     theTarget->SetScalarVisibility(theSource->GetScalarVisibility());
89     if(theIsCopyInput){
90       vtkFloatingPointType* aScalarRange = theSource->GetScalarRange();
91       if(!CheckIsSameRange(theTarget->GetScalarRange(), aScalarRange))
92         theTarget->SetScalarRange(aScalarRange);
93     }
94     theTarget->SetColorMode(theSource->GetColorMode());
95     theTarget->SetScalarMode(theSource->GetScalarMode());
96     theTarget->SetImmediateModeRendering(theSource->GetImmediateModeRendering());
97     theTarget->SetUseLookupTableScalarRange(theSource->GetUseLookupTableScalarRange());
98     theTarget->SetInterpolateScalarsBeforeMapping(theSource->GetInterpolateScalarsBeforeMapping());
99     if(theSource->GetArrayAccessMode() == VTK_GET_ARRAY_BY_ID)
100       theTarget->ColorByArrayComponent(theSource->GetArrayId(), theSource->GetArrayComponent());
101     else
102       theTarget->ColorByArrayComponent(theSource->GetArrayName(), theSource->GetArrayComponent());
103     
104     // To customize vtkAbstractMapper3D::ShallowCopy ...
105     theTarget->SetClippingPlanes(theSource->GetClippingPlanes());
106   }
107
108
109   //----------------------------------------------------------------------------
110   void
111   CopyDataSetMapper(vtkDataSetMapper* theTarget, 
112                     vtkDataSetMapper* theSource,
113                     bool theIsCopyInput)
114   {
115     // To customize vtkDataSetMapper::ShallowCopy ...
116     //theTarget->SetInput(theSource->GetInput());
117     CopyMapper(theTarget, theSource, theIsCopyInput);
118   }
119
120
121   //----------------------------------------------------------------------------
122   void
123   CopyPolyDataMapper(vtkPolyDataMapper* theTarget, 
124                      vtkPolyDataMapper* theSource,
125                      bool theIsCopyInput)
126   {
127     // To customize vtkPolyDataMapper::ShallowCopy ...
128     //theTarget->SetInput(theSource->GetInput());
129     theTarget->SetGhostLevel(theSource->GetGhostLevel());
130     theTarget->SetNumberOfPieces(theSource->GetNumberOfPieces());
131     theTarget->SetNumberOfSubPieces(theSource->GetNumberOfSubPieces());
132
133     CopyMapper(theTarget, theSource, theIsCopyInput);
134   }
135
136
137   //----------------------------------------------------------------------------
138   void
139   CopyPointSpriteDataMapper(VISU_OpenGLPointSpriteMapper* theTarget, 
140                             VISU_OpenGLPointSpriteMapper* theSource,
141                             bool theIsCopyInput)
142   {
143     // To customize VISU_OpenGLPointSpriteMapper::ShallowCopy ...
144     theTarget->SetPrimitiveType( theSource->GetPrimitiveType() );
145     
146     theTarget->SetPointSpriteMode( theSource->GetPointSpriteMode() );
147     
148     theTarget->SetPointSpriteClamp( theSource->GetPointSpriteClamp() );
149     theTarget->SetPointSpriteSize( theSource->GetPointSpriteSize() );
150     theTarget->SetPointSpriteMinSize( theSource->GetPointSpriteMinSize() );
151     theTarget->SetPointSpriteMaxSize( theSource->GetPointSpriteMaxSize() );
152     theTarget->SetPointSpriteMagnification( theSource->GetPointSpriteMagnification() );
153     
154     theTarget->SetImageData( theSource->GetImageData() );
155     theTarget->SetPointSpriteAlphaThreshold( theSource->GetPointSpriteAlphaThreshold() );
156
157     CopyPolyDataMapper(theTarget, theSource, theIsCopyInput);
158   }
159
160
161   //----------------------------------------------------------------------------
162   void
163   ComputeBoundsParam(vtkDataSet* theDataSet,
164                      vtkFloatingPointType theDirection[3], 
165                      vtkFloatingPointType theMinPnt[3],
166                      vtkFloatingPointType& theMaxBoundPrj, 
167                      vtkFloatingPointType& theMinBoundPrj)
168   {
169     vtkFloatingPointType aBounds[6];
170     theDataSet->GetBounds(aBounds);
171     
172     //Enlarge bounds in order to avoid conflicts of precision
173     for(int i = 0; i < 6; i += 2){
174       static double EPS = 1.0E-3;
175       vtkFloatingPointType aDelta = (aBounds[i+1] - aBounds[i])*EPS;
176       aBounds[i] -= aDelta;
177       aBounds[i+1] += aDelta;
178     }
179     
180     vtkFloatingPointType aBoundPoints[8][3] = { {aBounds[0],aBounds[2],aBounds[4]},
181                                                 {aBounds[1],aBounds[2],aBounds[4]},
182                                                 {aBounds[0],aBounds[3],aBounds[4]},
183                                                 {aBounds[1],aBounds[3],aBounds[4]},
184                                                 {aBounds[0],aBounds[2],aBounds[5]},
185                                                 {aBounds[1],aBounds[2],aBounds[5]},
186                                                 {aBounds[0],aBounds[3],aBounds[5]},
187                                                 {aBounds[1],aBounds[3],aBounds[5]}};
188     
189     int aMaxId = 0, aMinId = aMaxId;
190     theMaxBoundPrj = vtkMath::Dot(theDirection,aBoundPoints[aMaxId]);
191     theMinBoundPrj = theMaxBoundPrj;
192     for(int i = 1; i < 8; i++){
193       vtkFloatingPointType aTmp = vtkMath::Dot(theDirection,aBoundPoints[i]);
194       if(theMaxBoundPrj < aTmp){
195         theMaxBoundPrj = aTmp;
196         aMaxId = i;
197       }
198       if(theMinBoundPrj > aTmp){
199         theMinBoundPrj = aTmp;
200         aMinId = i;
201       }
202     }
203     vtkFloatingPointType *aMinPnt = aBoundPoints[aMaxId];
204     theMinPnt[0] = aMinPnt[0];
205     theMinPnt[1] = aMinPnt[1];
206     theMinPnt[2] = aMinPnt[2];
207   }
208
209
210   //----------------------------------------------------------------------------
211   void
212   DistanceToPosition(vtkDataSet* theDataSet,
213                      vtkFloatingPointType theDirection[3], 
214                      vtkFloatingPointType theDist, 
215                      vtkFloatingPointType thePos[3])
216   {
217     vtkFloatingPointType aMaxBoundPrj, aMinBoundPrj, aMinPnt[3];
218     ComputeBoundsParam(theDataSet,
219                        theDirection,
220                        aMinPnt,
221                        aMaxBoundPrj,
222                        aMinBoundPrj);
223     vtkFloatingPointType aLength = (aMaxBoundPrj-aMinBoundPrj)*theDist;
224     thePos[0] = aMinPnt[0] - theDirection[0] * aLength;
225     thePos[1] = aMinPnt[1] - theDirection[1] * aLength;
226     thePos[2] = aMinPnt[2] - theDirection[2] * aLength;
227   }
228   
229
230   //----------------------------------------------------------------------------
231   void
232   PositionToDistance(vtkDataSet* theDataSet,
233                      vtkFloatingPointType theDirection[3], 
234                      vtkFloatingPointType thePos[3], 
235                      vtkFloatingPointType& theDist)
236   {
237     vtkFloatingPointType aMaxBoundPrj, aMinBoundPrj, aMinPnt[3];
238     ComputeBoundsParam(theDataSet,
239                        theDirection,
240                        aMinPnt,
241                        aMaxBoundPrj,
242                        aMinBoundPrj);
243     vtkFloatingPointType aPrj = vtkMath::Dot(theDirection,thePos);
244     theDist = (aPrj-aMinBoundPrj)/(aMaxBoundPrj-aMinBoundPrj);
245   }
246
247
248   //----------------------------------------------------------------------------
249 }