Salome HOME
Compatibility CMake
[modules/visu.git] / src / CONVERTOR / VISU_PointCoords.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:
24 //  Author:
25 //  Module : VISU
26 //
27 #include "VISU_PointCoords.hxx"
28 #include "VISU_ConvertorUtils.hxx"
29
30 #include <vtkUnstructuredGrid.h>
31 #include <vtkPointData.h>
32 #include <vtkIntArray.h>
33
34 #ifdef _DEBUG_
35 static int MYDEBUG = 0;
36 #else
37 static int MYDEBUG = 0;
38 #endif
39
40 namespace VISU
41 {
42   //---------------------------------------------------------------
43   void
44   TCoordHolderBase
45   ::Init(vtkIdType theNbPoints,
46          vtkIdType theDim)
47   {
48     myDim = theDim;
49     myNbPoints = theNbPoints;
50   }
51
52   vtkIdType
53   TCoordHolderBase
54   ::GetNbPoints() const
55   {
56     return myNbPoints; 
57   }
58
59   vtkIdType
60   TCoordHolderBase
61   ::GetDim() const
62   {
63     return myDim; 
64   }
65
66   size_t
67   TCoordHolderBase
68   ::size() const
69   {
70     return GetNbPoints() * GetDim(); 
71   }
72
73   unsigned long int
74   TCoordHolderBase
75   ::GetMemorySize()
76   {
77     return sizeof(TCoord) * size();
78   }
79
80
81   //---------------------------------------------------------------
82   TPointCoords
83   ::TPointCoords():
84     myPointSet(vtkUnstructuredGrid::New())
85   {
86     vtkPoints* aPoints = vtkPoints::New();
87     myPointSet->SetPoints(aPoints);
88     aPoints->SetDataType(VTK_DOUBLE);
89     aPoints->Delete();
90
91     myPointSet->Delete();
92   }
93
94   void
95   TPointCoords
96   ::Init(const PCoordHolder& theCoord)
97   {
98     myPointSet->GetPoints()->SetNumberOfPoints(theCoord->GetNbPoints());
99     myCoord = theCoord;
100   }
101
102   vtkIdType
103   TPointCoords
104   ::GetNbPoints() const
105   {
106     return myCoord->GetNbPoints(); 
107   }
108
109   vtkIdType
110   TPointCoords
111   ::GetDim() const
112   {
113     return myCoord->GetDim(); 
114   }
115
116   TCCoordSlice 
117   TPointCoords
118   ::GetCoordSlice(vtkIdType theNodeId) const
119   {
120     return myCoord->GetCoordSlice(theNodeId);
121   }
122   
123   TCoordSlice 
124   TPointCoords
125   ::GetCoordSlice(vtkIdType theNodeId)
126   {
127     return myCoord->GetCoordSlice(theNodeId);
128   }
129
130   vtkIdType
131   TPointCoords
132   ::GetObjID(vtkIdType theID) const
133   {
134     return theID;
135   }
136
137   vtkIdType
138   TPointCoords
139   ::GetVTKID(vtkIdType theID) const
140   {
141     return theID;
142   }
143
144   void 
145   TPointCoords
146   ::SetVoidArray() const
147   {
148     vtkDataArray* aDataArray = myPointSet->GetPoints()->GetData();
149     aDataArray->SetVoidArray(myCoord->GetValuePtr(), myCoord->size(), true);
150   }
151
152   vtkPointSet*
153   TPointCoords
154   ::GetPointSet() const
155   { 
156     if(!myIsVTKDone){
157       TTimerLog aTimerLog(MYDEBUG,"TPointCoords::GetPoints()");
158       vtkIdType aNbPoints = GetNbPoints();
159       vtkIdType aDim = GetDim();
160
161       INITMSG(MYDEBUG,"TPointCoords::GetPoints - aNbPoints = "<<aNbPoints<<
162               "; aDim = "<<aDim<<
163               endl);
164       
165       if(GetDim() == 3){
166         INITMSG(MYDEBUG,"TPointCoords::GetPoints - SetVoidArray()"<<endl);
167         SetVoidArray();
168       }else{
169         vtkPoints* aPoints = myPointSet->GetPoints();
170         for(vtkIdType aPointId = 0; aPointId < aNbPoints; aPointId++){
171           TCCoordSlice aSlice = GetCoordSlice(aPointId);
172       
173           vtkFloatingPointType aCoords[3] = {0.0, 0.0, 0.0};
174           for(vtkIdType aDimId = 0; aDimId < aDim; aDimId++)
175             aCoords[aDimId] = aSlice[aDimId];
176
177           aPoints->SetPoint(aPointId, aCoords);
178         }
179       }
180
181       myIsVTKDone = true;
182     }
183     
184     return myPointSet.GetPointer();
185   }
186
187   unsigned long int
188   TPointCoords
189   ::GetMemorySize()
190   {
191     size_t aSize = myCoord->GetMemorySize();
192     aSize += myPointSet->GetActualMemorySize() * 1024;
193     return aSize;
194   }
195
196
197   //---------------------------------------------------------------
198   void
199   TNamedPointCoords
200   ::Init(const PCoordHolder& theCoord)
201   {
202     TPointCoords::Init(theCoord);
203     myPointsDim.resize(theCoord->GetDim());
204   }
205
206   std::string&
207   TNamedPointCoords
208   ::GetName(vtkIdType theDim)
209   {
210     return myPointsDim[theDim];
211   }
212
213   const std::string&
214   TNamedPointCoords
215   ::GetName(vtkIdType theDim) const
216   {
217     return myPointsDim[theDim];
218   }
219
220   std::string 
221   TNamedPointCoords
222   ::GetNodeName(vtkIdType theObjID) const
223   {
224     return "";
225   }
226
227
228   //---------------------------------------------------------------
229   enum ECoordName{eX, eY, eZ, eNoneCoord};
230   typedef VISU::TCoord (*TGetCoord)(const VISU::TCCoordSlice& theCoordSlice);
231   
232   template<ECoordName TCoordId>
233   VISU::TCoord 
234   GetCoord(const VISU::TCCoordSlice& theCoordSlice)
235   {
236     return theCoordSlice[TCoordId];
237   }
238   
239   template<>
240   VISU::TCoord 
241   GetCoord<eNoneCoord>(const VISU::TCCoordSlice& theCoordSlice)
242   {
243     return 0.0;
244   }
245   
246   
247   TGetCoord aXYZGetCoord[3] = {
248     &GetCoord<eX>, 
249     &GetCoord<eY>, 
250     &GetCoord<eZ>
251   };
252   
253   
254   TGetCoord aXYGetCoord[3] = {
255     &GetCoord<eX>, 
256     &GetCoord<eY>, 
257     &GetCoord<eNoneCoord>
258   };
259   
260   TGetCoord aYZGetCoord[3] = {
261     &GetCoord<eNoneCoord>,
262     &GetCoord<eX>, 
263     &GetCoord<eY>
264   };
265   
266   TGetCoord aXZGetCoord[3] = {
267     &GetCoord<eX>, 
268     &GetCoord<eNoneCoord>,
269     &GetCoord<eY>
270   };
271   
272   
273   TGetCoord aXGetCoord[3] = {
274     &GetCoord<eX>, 
275     &GetCoord<eNoneCoord>,
276     &GetCoord<eNoneCoord>
277   };
278   
279   TGetCoord aYGetCoord[3] = {
280     &GetCoord<eNoneCoord>,
281     &GetCoord<eX>, 
282     &GetCoord<eNoneCoord>
283   };
284
285   TGetCoord aZGetCoord[3] = {
286     &GetCoord<eNoneCoord>,
287     &GetCoord<eNoneCoord>,
288     &GetCoord<eX>
289   };
290
291   
292   class TCoordHelper{
293     TGetCoord* myGetCoord;
294   public:
295     TCoordHelper(TGetCoord* theGetCoord):
296       myGetCoord(theGetCoord)
297     {}
298
299     virtual
300     ~TCoordHelper()
301     {}
302
303     VISU::TCoord 
304     GetCoord(VISU::TCCoordSlice& theCoordSlice, 
305              int theCoordId)
306     {
307       return (*myGetCoord[theCoordId])(theCoordSlice);
308     }
309   };
310   typedef std::auto_ptr<TCoordHelper> TCoordHelperPtr;
311   
312
313   //---------------------------------------------------------------
314   vtkPointSet*
315   TNamedPointCoords
316   ::GetPointSet() const
317   { 
318     if(!myIsVTKDone){
319       TTimerLog aTimerLog(MYDEBUG,"TNamedPointCoords::GetPoints()");
320       TCoordHelperPtr aCoordHelperPtr;
321       bool anIsDimPresent[3] = {false, false, false};
322       for(int iDim = 0; iDim < GetDim(); iDim++){
323         // PAL16857(SMESH not conform to the MED convention) ->
324         // 1D - always along X
325         // 2D - always in XOY plane
326         anIsDimPresent[iDim] = iDim < GetDim();
327 //      std::string aName = GetName(iDim);
328 //      if ( aName.size() > 1 ) // PAL13021 (PAL12148), aName has size 8 or 16
329 //        aName = aName.substr(0,1);
330 //      if(aName == "x" || aName == "X")
331 //        anIsDimPresent[eX] = true;
332 //      else if(aName == "y" || aName == "Y")
333 //        anIsDimPresent[eY] = true;
334 //      else if(aName == "z" || aName == "Z")
335 //        anIsDimPresent[eZ] = true;
336       }
337       
338       switch(GetDim()){
339       case 3:
340         aCoordHelperPtr.reset(new TCoordHelper(aXYZGetCoord));
341         break;
342       case 2:
343         if(anIsDimPresent[eY] && anIsDimPresent[eZ])
344           aCoordHelperPtr.reset(new TCoordHelper(aYZGetCoord));
345         else if(anIsDimPresent[eX] && anIsDimPresent[eZ])
346           aCoordHelperPtr.reset(new TCoordHelper(aXZGetCoord));
347         else
348           aCoordHelperPtr.reset(new TCoordHelper(aXYGetCoord));
349         break;
350       case 1:
351         if(anIsDimPresent[eY])
352           aCoordHelperPtr.reset(new TCoordHelper(aYGetCoord));
353         else if(anIsDimPresent[eZ])
354           aCoordHelperPtr.reset(new TCoordHelper(aZGetCoord));
355         else
356           aCoordHelperPtr.reset(new TCoordHelper(aXGetCoord));
357         break;
358       }
359       
360       INITMSG(MYDEBUG,"TNamedPointCoords::GetPoints - aNbPoints = "<<GetNbPoints()<<
361               "; aDim = "<<GetDim()<<
362               endl);
363       
364       if(anIsDimPresent[eX] && anIsDimPresent[eY] && anIsDimPresent[eZ]){
365         INITMSG(MYDEBUG,"TNamedPointCoords::GetPoints - SetVoidArray()"<<endl);
366         SetVoidArray();
367       }else{
368         vtkPoints* aPoints = myPointSet->GetPoints();
369         for(vtkIdType aNodeId = 0; aNodeId < GetNbPoints(); aNodeId++){ 
370           TCCoordSlice aCoordSlice = GetCoordSlice(aNodeId);
371           aPoints->SetPoint(aNodeId,
372                             aCoordHelperPtr->GetCoord(aCoordSlice,eX),
373                             aCoordHelperPtr->GetCoord(aCoordSlice,eY),
374                             aCoordHelperPtr->GetCoord(aCoordSlice,eZ));
375         }
376       }
377       
378       {
379         vtkIdType aNbTuples = GetNbPoints();
380         int anEntity = int(VISU::NODE_ENTITY);
381         vtkIntArray *aDataArray = vtkIntArray::New();
382         aDataArray->SetName("VISU_POINTS_MAPPER");
383         aDataArray->SetNumberOfComponents(2);
384         aDataArray->SetNumberOfTuples(aNbTuples);
385         int *aPtr = aDataArray->GetPointer(0);
386         for(vtkIdType aTupleId = 0; aTupleId < aNbTuples; aTupleId++){
387           vtkIdType anObjID = GetObjID(aTupleId);
388           *aPtr++ = anObjID;
389           *aPtr++ = anEntity;
390         }
391         myPointSet->GetPointData()->AddArray(aDataArray);
392         aDataArray->Delete();
393       }
394
395       myIsVTKDone = true;
396     }
397     
398     return myPointSet.GetPointer();
399   }
400
401   unsigned long int
402   TNamedPointCoords
403   ::GetMemorySize()
404   {
405     return TPointCoords::GetMemorySize();
406   }
407
408
409   //---------------------------------------------------------------
410 }