]> SALOME platform Git repositories - modules/visu.git/blob - src/PIPELINE/VISU_PipeLineUtils.cxx
Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/visu.git] / src / PIPELINE / VISU_PipeLineUtils.cxx
1 //  VISU OBJECT : interactive object for VISU entities implementation
2 //
3 //  Copyright (C) 2003  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 //
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     if(theSource->GetArrayAccessMode() == VTK_GET_ARRAY_BY_ID)
99       theTarget->ColorByArrayComponent(theSource->GetArrayId(), theSource->GetArrayComponent());
100     else
101       theTarget->ColorByArrayComponent(theSource->GetArrayName(), theSource->GetArrayComponent());
102     
103     // To customize vtkAbstractMapper3D::ShallowCopy ...
104     theTarget->SetClippingPlanes(theSource->GetClippingPlanes());
105   }
106
107
108   //----------------------------------------------------------------------------
109   void
110   CopyDataSetMapper(vtkDataSetMapper* theTarget, 
111                     vtkDataSetMapper* theSource,
112                     bool theIsCopyInput)
113   {
114     // To customize vtkDataSetMapper::ShallowCopy ...
115     //theTarget->SetInput(theSource->GetInput());
116     CopyMapper(theTarget, theSource, theIsCopyInput);
117   }
118
119
120   //----------------------------------------------------------------------------
121   void
122   CopyPolyDataMapper(vtkPolyDataMapper* theTarget, 
123                      vtkPolyDataMapper* theSource,
124                      bool theIsCopyInput)
125   {
126     // To customize vtkPolyDataMapper::ShallowCopy ...
127     //theTarget->SetInput(theSource->GetInput());
128     theTarget->SetGhostLevel(theSource->GetGhostLevel());
129     theTarget->SetNumberOfPieces(theSource->GetNumberOfPieces());
130     theTarget->SetNumberOfSubPieces(theSource->GetNumberOfSubPieces());
131
132     CopyMapper(theTarget, theSource, theIsCopyInput);
133   }
134
135
136   //----------------------------------------------------------------------------
137   void
138   CopyPointSpriteDataMapper(VISU_OpenGLPointSpriteMapper* theTarget, 
139                             VISU_OpenGLPointSpriteMapper* theSource,
140                             bool theIsCopyInput)
141   {
142     // To customize VISU_OpenGLPointSpriteMapper::ShallowCopy ...
143     theTarget->SetPrimitiveType( theSource->GetPrimitiveType() );
144     
145     theTarget->SetPointSpriteMode( theSource->GetPointSpriteMode() );
146     
147     theTarget->SetPointSpriteClamp( theSource->GetPointSpriteClamp() );
148     theTarget->SetPointSpriteSize( theSource->GetPointSpriteSize() );
149     theTarget->SetPointSpriteMinSize( theSource->GetPointSpriteMinSize() );
150     theTarget->SetPointSpriteMaxSize( theSource->GetPointSpriteMaxSize() );
151     theTarget->SetPointSpriteMagnification( theSource->GetPointSpriteMagnification() );
152     
153     theTarget->SetImageData( theSource->GetImageData() );
154     theTarget->SetPointSpriteAlphaThreshold( theSource->GetPointSpriteAlphaThreshold() );
155
156     CopyPolyDataMapper(theTarget, theSource, theIsCopyInput);
157   }
158
159
160   //----------------------------------------------------------------------------
161   void
162   ComputeBoundsParam(vtkDataSet* theDataSet,
163                      vtkFloatingPointType theDirection[3], 
164                      vtkFloatingPointType theMinPnt[3],
165                      vtkFloatingPointType& theMaxBoundPrj, 
166                      vtkFloatingPointType& theMinBoundPrj)
167   {
168     vtkFloatingPointType aBounds[6];
169     theDataSet->GetBounds(aBounds);
170     
171     //Enlarge bounds in order to avoid conflicts of precision
172     for(int i = 0; i < 6; i += 2){
173       static double EPS = 1.0E-3;
174       vtkFloatingPointType aDelta = (aBounds[i+1] - aBounds[i])*EPS;
175       aBounds[i] -= aDelta;
176       aBounds[i+1] += aDelta;
177     }
178     
179     vtkFloatingPointType aBoundPoints[8][3] = { {aBounds[0],aBounds[2],aBounds[4]},
180                                                 {aBounds[1],aBounds[2],aBounds[4]},
181                                                 {aBounds[0],aBounds[3],aBounds[4]},
182                                                 {aBounds[1],aBounds[3],aBounds[4]},
183                                                 {aBounds[0],aBounds[2],aBounds[5]},
184                                                 {aBounds[1],aBounds[2],aBounds[5]},
185                                                 {aBounds[0],aBounds[3],aBounds[5]},
186                                                 {aBounds[1],aBounds[3],aBounds[5]}};
187     
188     int aMaxId = 0, aMinId = aMaxId;
189     theMaxBoundPrj = vtkMath::Dot(theDirection,aBoundPoints[aMaxId]);
190     theMinBoundPrj = theMaxBoundPrj;
191     for(int i = 1; i < 8; i++){
192       vtkFloatingPointType aTmp = vtkMath::Dot(theDirection,aBoundPoints[i]);
193       if(theMaxBoundPrj < aTmp){
194         theMaxBoundPrj = aTmp;
195         aMaxId = i;
196       }
197       if(theMinBoundPrj > aTmp){
198         theMinBoundPrj = aTmp;
199         aMinId = i;
200       }
201     }
202     vtkFloatingPointType *aMinPnt = aBoundPoints[aMaxId];
203     theMinPnt[0] = aMinPnt[0];
204     theMinPnt[1] = aMinPnt[1];
205     theMinPnt[2] = aMinPnt[2];
206   }
207
208
209   //----------------------------------------------------------------------------
210   void
211   DistanceToPosition(vtkDataSet* theDataSet,
212                      vtkFloatingPointType theDirection[3], 
213                      vtkFloatingPointType theDist, 
214                      vtkFloatingPointType thePos[3])
215   {
216     vtkFloatingPointType aMaxBoundPrj, aMinBoundPrj, aMinPnt[3];
217     ComputeBoundsParam(theDataSet,
218                        theDirection,
219                        aMinPnt,
220                        aMaxBoundPrj,
221                        aMinBoundPrj);
222     vtkFloatingPointType aLength = (aMaxBoundPrj-aMinBoundPrj)*theDist;
223     thePos[0] = aMinPnt[0] - theDirection[0] * aLength;
224     thePos[1] = aMinPnt[1] - theDirection[1] * aLength;
225     thePos[2] = aMinPnt[2] - theDirection[2] * aLength;
226   }
227   
228
229   //----------------------------------------------------------------------------
230   void
231   PositionToDistance(vtkDataSet* theDataSet,
232                      vtkFloatingPointType theDirection[3], 
233                      vtkFloatingPointType thePos[3], 
234                      vtkFloatingPointType& theDist)
235   {
236     vtkFloatingPointType aMaxBoundPrj, aMinBoundPrj, aMinPnt[3];
237     ComputeBoundsParam(theDataSet,
238                        theDirection,
239                        aMinPnt,
240                        aMaxBoundPrj,
241                        aMinBoundPrj);
242     vtkFloatingPointType aPrj = vtkMath::Dot(theDirection,thePos);
243     theDist = (aPrj-aMinBoundPrj)/(aMaxBoundPrj-aMinBoundPrj);
244   }
245
246
247   //----------------------------------------------------------------------------
248 }