1 // Copyright (C) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 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.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // VISU OBJECT : interactive object for VISU entities implementation
24 // File : VISU_Convertor_impl.cxx
25 // Author : Alexey PETROV
28 #include "VISU_ConvertorUtils.hxx"
30 #include <vtkCellType.h>
32 #include <vtkUnstructuredGridWriter.h>
33 #include <vtkUnstructuredGrid.h>
35 #include <vtkPolyDataWriter.h>
36 #include <vtkPolyData.h>
38 #include <vtkInformationVector.h>
39 #include <vtkInformation.h>
40 #include <vtkExecutive.h>
42 #include <vtkTimerLog.h>
43 #include <vtkPointData.h>
44 #include <vtkCellData.h>
45 #include <vtkDataSet.h>
46 #include <vtkIdList.h>
48 #include <vtkIntArray.h>
52 static int MYDEBUG = 0;
54 static int MYDEBUG = 0;
59 //---------------------------------------------------------------
61 VISUGeom2NbNodes(EGeometry theGeom)
64 #ifndef VISU_ENABLE_QUADRATIC
89 //---------------------------------------------------------------
91 VISUGeom2VTK(EGeometry theGeom)
105 return VTK_HEXAHEDRON;
111 case VISU::ePOLYGONE:
113 case VISU::ePOLYEDRE:
114 return VTK_CONVEX_POINT_SET;
116 #ifndef VISU_ENABLE_QUADRATIC
126 return VTK_HEXAHEDRON;
135 #if defined(VTK_QUADRATIC_EDGE) && defined(VISU_USE_VTK_QUADRATIC)
136 return VTK_QUADRATIC_EDGE;
138 return VTK_POLY_LINE;
142 #if defined(VTK_QUADRATIC_TRIANGLE) && defined(VISU_USE_VTK_QUADRATIC)
143 return VTK_QUADRATIC_TRIANGLE;
149 #if defined(VTK_QUADRATIC_QUAD) && defined(VISU_USE_VTK_QUADRATIC)
150 return VTK_QUADRATIC_QUAD;
156 #if defined(VTK_QUADRATIC_TETRA) && defined(VISU_USE_VTK_QUADRATIC)
157 return VTK_QUADRATIC_TETRA;
159 return VTK_CONVEX_POINT_SET;
163 #if defined(VTK_QUADRATIC_HEXAHEDRON) && defined(VISU_USE_VTK_QUADRATIC)
164 return VTK_QUADRATIC_HEXAHEDRON;
166 return VTK_CONVEX_POINT_SET;
170 #if defined(VTK_QUADRATIC_WEDGE) && defined(VISU_USE_VTK_QUADRATIC)
171 return VTK_QUADRATIC_WEDGE;
173 return VTK_CONVEX_POINT_SET;
177 #if defined(VTK_QUADRATIC_PYRAMID) && defined(VISU_USE_VTK_QUADRATIC)
178 return VTK_QUADRATIC_PYRAMID;
180 return VTK_CONVEX_POINT_SET;
183 #endif //VISU_ENABLE_QUADRATIC
191 //---------------------------------------------------------------
193 WriteToFile(vtkUnstructuredGrid* theDataSet,
194 const std::string& theFileName)
196 vtkUnstructuredGridWriter* aWriter = vtkUnstructuredGridWriter::New();
197 //aWriter->SetFileType(VTK_BINARY);
198 aWriter->SetFileName(theFileName.c_str());
199 aWriter->SetInput(theDataSet);
205 //---------------------------------------------------------------
207 WriteToFile(vtkPolyData* theDataSet,
208 const std::string& theFileName)
210 vtkPolyDataWriter* aWriter = vtkPolyDataWriter::New();
211 //aWriter->SetFileType(VTK_BINARY);
212 aWriter->SetFileName(theFileName.c_str());
213 aWriter->SetInput(theDataSet);
219 //---------------------------------------------------------------
221 IsDataOnPoints(vtkDataSet* theDataSet)
223 theDataSet->Update();
224 vtkDataSetAttributes *aDataSetAttributes = theDataSet->GetPointData();
225 return aDataSetAttributes->GetArray("VISU_FIELD") != NULL;
229 //---------------------------------------------------------------
231 IsDataOnCells(vtkDataSet* theDataSet)
233 theDataSet->Update();
234 vtkDataSetAttributes *aDataSetAttributes = theDataSet->GetCellData();
235 return aDataSetAttributes->GetArray("VISU_FIELD") != NULL;
238 //---------------------------------------------------------------
240 IsElnoData(vtkDataSet* theDataSet)
242 theDataSet->Update();
244 if ( vtkDataSetAttributes *aDataSetAttributes = theDataSet->GetCellData() )
245 if ( aDataSetAttributes->GetArray( "ELNO_FIELD" ) != NULL )
248 if ( vtkDataSetAttributes *aDataSetAttributes = theDataSet->GetPointData() )
249 if ( aDataSetAttributes->GetArray( "ELNO_POINT_COORDS" ) != NULL )
256 //---------------------------------------------------------------
258 GetVTKID(vtkDataArray *theIDDataArray, vtkIdType theID, int theEntity)
260 if(vtkIntArray *anIntArray = dynamic_cast<vtkIntArray*>(theIDDataArray)){
261 int aNbTuples = anIntArray->GetNumberOfTuples();
262 int* aPointer = anIntArray->GetPointer(0);
263 for(int aTupleId = 0; aTupleId < aNbTuples; aTupleId++){
264 if(*aPointer == theID && *(aPointer + 1) == theEntity){
274 //---------------------------------------------------------------
276 GetObjectID(vtkDataArray *theIDDataArray, vtkIdType theID)
278 if(vtkIntArray *anIntArray = dynamic_cast<vtkIntArray*>(theIDDataArray)){
279 int aNbComp = anIntArray->GetNumberOfComponents();
280 int* aPointer = anIntArray->GetPointer(theID*aNbComp);
287 //---------------------------------------------------------------
289 GetElemVTKID(vtkDataSet *theDataSet, vtkIdType theID, int theEntity)
291 theDataSet->Update();
292 vtkDataSetAttributes *aDataSetAttributes = theDataSet->GetCellData();
293 if(vtkDataArray *aDataArray = aDataSetAttributes->GetArray("VISU_CELLS_MAPPER")){
296 vtkIdType anID = GetVTKID(aDataArray, theID, VISU::CELL_ENTITY);
301 vtkIdType anID = GetVTKID(aDataArray, theID, VISU::FACE_ENTITY);
306 vtkIdType anID = GetVTKID(aDataArray, theID, VISU::EDGE_ENTITY);
311 vtkIdType anID = GetVTKID(aDataArray, theID, VISU::NODE_ENTITY);
316 return GetVTKID(aDataArray, theID, theEntity);
322 //---------------------------------------------------------------
324 GetElemObjID(vtkDataSet *theDataSet, vtkIdType theID)
326 theDataSet->Update();
327 vtkDataSetAttributes *aDataSetAttributes = theDataSet->GetCellData();
328 if(vtkDataArray *aDataArray = aDataSetAttributes->GetArray("VISU_CELLS_MAPPER"))
329 return GetObjectID(aDataArray, theID);
335 //---------------------------------------------------------------
337 GetElemCell(vtkDataSet *theDataSet, vtkIdType theObjID)
339 vtkIdType aVTKID = GetElemVTKID(theDataSet, theObjID);
340 return theDataSet->GetCell(aVTKID);
344 //---------------------------------------------------------------
346 GetNodeVTKID(vtkDataSet *theDataSet, vtkIdType theID)
348 theDataSet->Update();
349 vtkDataSetAttributes *aDataSetAttributes = theDataSet->GetPointData();
350 if(vtkDataArray *aDataArray = aDataSetAttributes->GetArray("VISU_POINTS_MAPPER"))
351 return GetVTKID(aDataArray, theID, VISU::NODE_ENTITY);
357 //---------------------------------------------------------------
359 GetNodeObjID(vtkDataSet *theDataSet, vtkIdType theID)
361 theDataSet->Update();
362 vtkDataSetAttributes *aDataSetAttributes = theDataSet->GetPointData();
363 if(vtkDataArray *aDataArray = aDataSetAttributes->GetArray("VISU_POINTS_MAPPER"))
364 return GetObjectID(aDataArray, theID);
370 //---------------------------------------------------------------
371 vtkFloatingPointType*
372 GetNodeCoord(vtkDataSet *theDataSet, vtkIdType theObjID)
374 vtkIdType aVTKID = GetNodeVTKID(theDataSet, theObjID);
375 if ( aVTKID >= 0 ) return theDataSet->GetPoint(aVTKID);
380 //---------------------------------------------------------------
382 GetObjID(vtkDataSet *theDataSet, vtkIdType theID)
384 theDataSet->Update();
385 vtkDataSetAttributes *aDataSetAttributes = theDataSet->GetCellData();
386 if(vtkDataArray *aDataArray = aDataSetAttributes->GetArray("VISU_CELLS_MAPPER")){
387 if(vtkIntArray *anIntArray = dynamic_cast<vtkIntArray*>(aDataArray)){
388 vtkIdType anID = 2 * theID;
389 TCellID aCellID = anIntArray->GetValue(anID);
390 TLocalPntID aLocalPntID = anIntArray->GetValue(anID + 1);
391 return TGaussPointID(aCellID, aLocalPntID);
394 return TGaussPointID();
398 //---------------------------------------------------------------
400 GetInputCellID(vtkDataSet *theDataSet, vtkIdType theObjID)
402 theDataSet->Update();
403 vtkDataSetAttributes *aDataSetAttributes = theDataSet->GetCellData();
404 if(vtkDataArray *aDataArray = aDataSetAttributes->GetArray("VISU_INPUTS_MAPPER")){
405 if(vtkIntArray *anIntArray = dynamic_cast<vtkIntArray*>(aDataArray)){
406 vtkIdType aVTKID = GetElemVTKID(theDataSet, theObjID);
407 vtkIdType aTupleID = 2 * aVTKID;
408 TCellID aCellID = anIntArray->GetValue(aTupleID);
409 TInputID anInputID = anIntArray->GetValue(aTupleID + 1);
410 return TInputCellID(anInputID, aCellID);
413 return TInputCellID();
417 //---------------------------------------------------------------
419 GetInput(vtkInformationVector **theInputVector,
420 vtkIdType theInputId)
422 if(vtkInformation* anInformation = theInputVector[0]->GetInformationObject(theInputId))
423 return vtkDataSet::SafeDownCast(anInformation->Get(vtkDataObject::DATA_OBJECT()));
428 //---------------------------------------------------------------
430 GetOutput(vtkInformationVector *theOutputVector)
432 if(vtkInformation* anInformation = theOutputVector->GetInformationObject(0))
433 return vtkDataSet::SafeDownCast(anInformation->Get(vtkDataObject::DATA_OBJECT()));
437 //---------------------------------------------------------------
439 GetElnoPoints(vtkDataSet *theDataSet, vtkIdType theNodeObjID)
442 if(theDataSet && IsElnoData(theDataSet)) {
443 vtkDataSetAttributes *aDataSetAttributes = theDataSet->GetPointData();
444 vtkDataArray *aDataArray = aDataSetAttributes->GetArray("VISU_POINTS_MAPPER");
446 if(vtkIntArray *anIntArray = dynamic_cast<vtkIntArray*>(aDataArray)){
447 int aNbTuples = anIntArray->GetNumberOfTuples();
448 int* aPointer = anIntArray->GetPointer(0);
449 for(int aTupleId = 0; aTupleId < aNbTuples; aTupleId++){
450 if( *aPointer == theNodeObjID ) {
451 vtkIdList *aCellIds = vtkIdList::New();
452 theDataSet->GetPointCells(aTupleId,aCellIds);
453 if(aCellIds->GetNumberOfIds() == 1){
454 aResult.push_back(TElnoPointID(aTupleId,aCellIds->GetId(0)));
465 //---------------------------------------------------------------
467 ::TTimerLog(int theIsDebug,
468 const std::string& theName):
469 myIsDebug(MYDEBUG + theIsDebug),
470 myTimerLog(vtkTimerLog::New()),
471 myPrefixPrinter(myIsDebug == 1),
474 myCPUTime = myTimerLog->GetCPUTime();
475 BEGMSG(myIsDebug > 1,"{\n");
478 //---------------------------------------------------------------
482 myCPUTime = myTimerLog->GetCPUTime() - myCPUTime;
485 BEGMSG(myIsDebug,"} = "<<myCPUTime<<" secs ("<<myName<<")\n");
487 BEGMSG(myIsDebug,myName<<" takes "<<myCPUTime<<" secs\n");
490 myTimerLog->Delete();
495 //---------------------------------------------------------------