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