fixed: gcc8 compile issues
[opensg.git] / Source / Contrib / VTK / OSGVTKPolyDataMapper.cpp
bloba947e99f72817ccfa514a95b5ec8fbca07e5fbdc
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
18 * *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 #if __GNUC__ >= 4 || __GNUC_MINOR__ >=3
40 #pragma GCC diagnostic warning "-Wold-style-cast"
41 #endif
43 #include <cstdlib>
44 #include <cstdio>
46 #include "OSGConfig.h"
48 #include "OSGGL.h"
50 #include "OSGIntersectAction.h"
51 #include "OSGRenderAction.h"
53 #ifdef OSG_HAVE_ACTION //CHECK
54 #include "OSGIntersectActor.h"
55 #endif
57 #include "OSGVTKPolyDataMapper.h"
58 #include "OSGVolume.h"
59 #include "OSGGeometry.h"
60 #include "OSGChunkMaterial.h"
61 #include "OSGMaterialChunk.h"
62 #include "OSGTwoSidedLightingChunk.h"
64 #ifdef OSG_WITH_VTK
65 #include "vtkDataSetReader.h"
66 #include "vtkPointSource.h"
67 #include "vtkStreamTracer.h"
68 #include "vtkStructuredGrid.h"
69 #include "vtkPolyData.h"
70 #include "vtkRenderWindowInteractor.h"
71 #include "vtkPointData.h"
72 #include "vtkPolyDataMapper.h"
73 #include "vtkCellArray.h"
74 #include "vtkActor.h"
75 #include "vtkProperty.h"
76 #include "vtkDataArray.h"
77 #include "vtkCellData.h"
78 #endif
80 #include <typeinfo>
82 OSG_USING_NAMESPACE
84 // Documentation for this class is emited in the
85 // OSGVTKPolyDataMapperBase.cpp file.
86 // To modify it, please change the .fcd file (OSGVTKPolyDataMapper.fcd) and
87 // regenerate the base file.
89 /*-------------------------------------------------------------------------*/
90 /* Sync */
92 void VTKPolyDataMapper::changed(ConstFieldMaskArg whichField,
93 UInt32 origin,
94 BitVector details)
96 Inherited::changed(whichField, origin, details);
99 /*-------------------------------------------------------------------------*/
100 /* Helper */
102 void VTKPolyDataMapper::initGeometries(void)
104 NodeUnrecPtr pRoot = Node::create();
106 pRoot->setCore(Group::create());
108 setRoot(pRoot);
110 for(UInt32 i = 0; i < 4; ++i)
112 GeometryUnrecPtr pGeo = Geometry::create();
114 ChunkMaterialUnrecPtr pMat = ChunkMaterial::create();
115 MaterialChunkUnrecPtr pMatChunk = MaterialChunk::create();
117 GeoPnt3fPropertyUnrecPtr pPoints = GeoPnt3fProperty ::create();
118 GeoUInt32PropertyUnrecPtr pLengths = GeoUInt32Property ::create();
119 GeoUInt8PropertyUnrecPtr pTypes = GeoUInt8Property ::create();
120 GeoColor4fPropertyUnrecPtr pColors = GeoColor4fProperty::create();
121 GeoVec3fPropertyUnrecPtr pNormals = GeoVec3fProperty ::create();
123 if(i < 2)
125 pMatChunk->setLit(false);
128 pMatChunk->setDiffuse (OSG::Color4f(1.0, 1.0, 1.0, 1.0));
129 pMatChunk->setSpecular (OSG::Color4f(0.0, 0.0, 0.0, 1.0));
130 pMatChunk->setShininess(10.0f);
132 pMat->addChunk(pMatChunk);
134 TwoSidedLightingChunkUnrecPtr pTSLChunk =
135 TwoSidedLightingChunk::create();
137 pMat->addChunk(pTSLChunk);
139 pGeo->setDlistCache(false );
141 pGeo->setMaterial (pMat );
142 pGeo->setPositions (pPoints );
143 pGeo->setLengths (pLengths);
144 pGeo->setTypes (pTypes );
145 pGeo->setColors (pColors );
147 if(i > 1)
149 pGeo->setNormals(pNormals);
152 OSG::NodeUnrecPtr pGeoRoot = OSG::Node::create();
154 pGeoRoot->setCore (pGeo);
155 pGeoRoot->setTravMask(0 );
157 pRoot->addChild(pGeoRoot);
159 this->pushToGeometries (pGeo );
160 this->pushToMaterials (pMat );
161 this->pushToMaterialChunks(pMatChunk);
162 this->pushToPositions (pPoints );
163 this->pushToLength (pLengths );
164 this->pushToTypes (pTypes );
165 this->pushToColors (pColors );
166 this->pushToNormals (pNormals );
167 this->pushToGeoRoots (pGeoRoot );
171 #ifdef OSG_WITH_VTK
172 typedef double vtkReal;
174 bool VTKPolyDataMapper::processPrimitive(
175 OSG::GeoPnt3fProperty *pPoints,
176 OSG::GeoColor4fProperty *pColors,
177 OSG::GeoVec3fProperty *pNormals,
178 OSG::GeoUInt32Property *pLengths,
179 OSG::GeoUInt8Property *pTypes,
181 vtkActor *actor,
182 vtkCellArray *primArray,
183 int primType,
184 int verbose)
187 // get polyData from vtkActor
189 vtkPolyData *polyData =
190 static_cast<vtkPolyData *>(actor->GetMapper()->GetInput());
192 int numPrimitives = primArray->GetNumberOfCells();
194 if(numPrimitives == 0)
195 return false;
197 OSG::GeoUInt32Property::StoredFieldType *pLengthField =
198 pLengths->editFieldPtr();
200 OSG::GeoUInt8Property::StoredFieldType *pTypeField =
201 pTypes ->editFieldPtr();
203 OSG::GeoPnt3fProperty::StoredFieldType *pPointField =
204 pPoints ->editFieldPtr();
206 OSG::GeoColor4fProperty::StoredFieldType *pColorField =
207 pColors ->editFieldPtr();
209 OSG::GeoVec3fProperty::StoredFieldType *pNormalField =
210 pNormals->editFieldPtr();
212 // get number of indices in the vtk prim array. Each vtkCell has the length
213 // (not counted), followed by the indices.
214 int primArraySize = primArray->GetNumberOfConnectivityEntries();
215 int numIndices = primArraySize - numPrimitives;
217 pTypeField ->clear();
218 pLengthField->clear();
219 pPointField ->clear();
220 pColorField ->clear();
221 pNormalField->clear();
223 pLengthField->resize(numPrimitives);
224 pTypeField ->resize(numPrimitives);
226 pPointField ->resize(numIndices );
228 // get the data
229 UInt32 normalPerVertex = 0;
230 UInt32 normalPerCell = 0;
231 vtkDataArray *normalArray = polyData->GetPointData()->GetNormals();
233 if(actor->GetProperty()->GetInterpolation() == VTK_FLAT)
235 normalArray = NULL;
238 if(normalArray != NULL)
240 normalPerVertex = 1;
242 else
244 normalArray = polyData->GetCellData()->GetNormals();
246 if(normalArray != NULL)
248 normalPerCell = 1;
252 UInt32 colorPerCell = 0;
253 UInt32 colorPerVertex = 0;
254 UInt32 colorPerObject = 0;
255 Real64 *objectColor = NULL;
257 vtkUnsignedCharArray *colorArray = actor->GetMapper()->MapScalars(1.0);
259 if(actor->GetMapper()->GetScalarVisibility() && colorArray != NULL)
261 int scalarMode = actor->GetMapper()->GetScalarMode();
263 if(scalarMode == VTK_SCALAR_MODE_USE_CELL_DATA ||
264 !polyData->GetPointData()->GetScalars()) // no point data
266 colorPerCell = 1;
268 else
270 colorPerVertex = 1;
273 else
275 colorPerObject = 1;
277 objectColor = actor->GetProperty()->GetColor();
280 fprintf(stderr, "Got normals %d %d %p\n",
281 normalPerCell,
282 normalPerVertex,
283 normalArray);
285 fprintf(stderr, "Got colors %d %d %d %p %p\n",
286 colorPerCell,
287 colorPerVertex,
288 colorPerObject,
289 colorArray,
290 objectColor);
292 if(normalPerCell == 1 || normalPerVertex == 1)
294 pNormalField->resize(numIndices);
297 pColorField ->resize(numIndices);
299 int prim = 0, vert = 0;
300 vtkIdType i, npts, *pts;
301 // int transparentFlag = 0;
303 unsigned char *aColor = NULL;
304 vtkReal *aNormal = NULL;
306 for(primArray->InitTraversal();
307 primArray->GetNextCell(npts, pts);
308 prim++)
310 (*pTypeField )[prim] = primType;
311 (*pLengthField)[prim] = npts;
313 if(colorPerCell == 1)
315 aColor = colorArray->GetPointer(4 * prim);
318 if(normalPerCell == 1)
320 aNormal = normalArray->GetTuple(prim);
323 for(i = 0; i < npts; i++)
325 vtkReal *aVertex = polyData->GetPoint(pts[i]);
327 (*pPointField)[vert].setValues(
328 Real32(aVertex[0]),
329 Real32(aVertex[1]),
330 Real32(aVertex[2]));
332 if(colorPerVertex == 1 && colorArray != NULL)
334 aColor = colorArray->GetPointer(4 * pts[i]);
337 if(aColor != NULL)
339 (*pColorField)[vert].setValuesRGBA(
340 aColor[0]/255.0f,
341 aColor[1]/255.0f,
342 aColor[2]/255.0f,
343 aColor[3]/255.0f);
345 else
347 if(colorPerObject == 1 && objectColor != NULL)
349 (*pColorField)[vert].setValuesRGBA(objectColor[0],
350 objectColor[1],
351 objectColor[2],
352 1.f);
354 else
356 (*pColorField)[vert].setValuesRGBA(
357 1.0f, 1.0f, 1.0f, 1.0f);
361 if(normalPerVertex == 1 && normalArray != NULL)
363 aNormal = normalArray->GetTuple(pts[i]);
366 if(aNormal != NULL)
368 (*pNormalField)[vert].setValues(Real32(aNormal[0]),
369 Real32(aNormal[1]),
370 Real32(aNormal[2]));
373 vert++;
377 return true;
379 #endif
381 void VTKPolyDataMapper::execute(void)
383 #ifdef OSG_WITH_VTK
384 if(_pActor == NULL)
386 return;
389 vtkMapper *pMapper = _pActor->GetMapper();
391 if(pMapper == NULL)
392 return;
394 pMapper->Update();
396 if( (_pActor -> GetMTime() < this->_executeTime) &&
397 ( pMapper-> GetMTime() < this->_executeTime) &&
398 ( pMapper->GetInput()->GetMTime() < this->_executeTime) &&
399 ( this->_modifiedTime < this->_executeTime) )
401 return;
403 else
405 fprintf(stderr, "%ld %ld | %ld %ld %ld\n",
406 _executeTime.GetMTime(),
407 _modifiedTime.GetMTime(),
408 _pActor->GetMTime(),
409 _pActor->GetMapper()->GetMTime(),
410 _pActor->GetMapper()->GetInput()->GetMTime());
413 if(_sfRoot.getValue() == NULL)
415 initGeometries();
418 fprintf(stderr, "Mapper::execute %p\n", _pActor);
420 // get poly data
421 vtkPolyData *polyData =
422 static_cast<vtkPolyData *>(_pActor->GetMapper()->GetInput());
424 if(polyData == NULL)
426 fprintf(stderr, "Strange no data\n");
427 return;
430 // get primitive arrays
431 vtkCellArray *points, *lines, *polys, *strips;
433 points = polyData->GetVerts();
434 lines = polyData->GetLines();
435 polys = polyData->GetPolys();
436 strips = polyData->GetStrips();
438 int numPts = points->GetNumberOfCells();
439 int numLines = lines ->GetNumberOfCells();
440 int numPolys = polys ->GetNumberOfCells();
441 int numStrips = strips->GetNumberOfCells();
443 fprintf(stderr, "P : %d | L : %d | Poly : %d | S : %d\n",
444 numPts,
445 numLines,
446 numPolys,
447 numStrips);
449 const VTKPolyDataMapper *pThis = this;
451 if(numPts != 0)
453 bool rc = processPrimitive(pThis->_mfPositions[0],
454 pThis->_mfColors [0],
455 pThis->_mfNormals [0],
456 pThis->_mfLength [0],
457 pThis->_mfTypes [0],
459 _pActor,
460 points,
461 GL_POINTS,
462 true);
464 if(rc == true)
466 pThis->_mfGeoRoots[0]->setTravMask(
467 OSG::TypeTraits<OSG::UInt32>::BitsSet);
469 pThis->_mfGeoRoots[0]->invalidateVolume();
473 if(numLines != 0)
475 bool rc = processPrimitive(pThis->_mfPositions[1],
476 pThis->_mfColors [1],
477 pThis->_mfNormals [1],
478 pThis->_mfLength [1],
479 pThis->_mfTypes [1],
481 _pActor,
482 lines,
483 GL_LINE_STRIP,
484 true);
486 if(rc == true)
488 pThis->_mfGeoRoots[1]->setTravMask(
489 OSG::TypeTraits<OSG::UInt32>::BitsSet);
491 pThis->_mfGeoRoots[1]->invalidateVolume();
496 if(numPolys != 0)
498 bool rc = processPrimitive(pThis->_mfPositions[2],
499 pThis->_mfColors [2],
500 pThis->_mfNormals [2],
501 pThis->_mfLength [2],
502 pThis->_mfTypes [2],
504 _pActor,
505 polys,
506 GL_POLYGON,
507 true);
509 if(rc == true)
511 pThis->_mfGeoRoots[2]->setTravMask(
512 OSG::TypeTraits<OSG::UInt32>::BitsSet);
514 pThis->_mfGeoRoots[2]->invalidateVolume();
518 if(numStrips != 0)
520 bool rc = processPrimitive(pThis->_mfPositions[3],
521 pThis->_mfColors [3],
522 pThis->_mfNormals [3],
523 pThis->_mfLength [3],
524 pThis->_mfTypes [3],
526 _pActor,
527 strips,
528 GL_TRIANGLE_STRIP,
529 true);
531 if(rc == true)
533 pThis->_mfGeoRoots[3]->setTravMask(
534 OSG::TypeTraits<OSG::UInt32>::BitsSet);
536 pThis->_mfGeoRoots[3]->invalidateVolume();
540 _sfRoot.getValue()->updateVolume();
542 this->_executeTime.Modified();
544 // OSG::Thread::getCurrentChangeList()->commitChanges();
545 #endif
548 #ifdef OSG_WITH_VTK
549 void VTKPolyDataMapper::setActor(vtkActor *pActor)
551 if(_pActor != NULL)
552 _pActor->UnRegister(NULL);
554 _pActor = pActor;
556 if(_pActor != NULL)
557 _pActor->Register(NULL);
559 this->_modifiedTime.Modified();
561 #endif
564 void VTKPolyDataMapper::adjustVolume(Volume &volume)
566 this->execute();
568 if(_sfRoot.getValue() != NULL)
570 _sfRoot.getValue()->updateVolume();
572 volume.extendBy(_sfRoot.getValue()->getVolume());
576 /*-------------------------------------------------------------------------*/
577 /* Dump */
579 void VTKPolyDataMapper::dump( UInt32 uiIndent,
580 const BitVector bvFlags ) const
582 Inherited::dump(uiIndent, bvFlags);
584 indentLog(uiIndent, PLOG);
585 PLOG << "{" << std::endl;
587 indentLog(uiIndent + 4, PLOG);
588 PLOG << "Root" << std::endl;
590 if(_sfRoot.getValue() != NULL)
592 _sfRoot.getValue()->dump(uiIndent + 8, bvFlags);
594 else
596 indentLog(uiIndent + 8, PLOG);
597 PLOG << "(NULL)" << std::endl;
600 indentLog(uiIndent, PLOG);
601 PLOG << "}" << std::endl;
604 /*-------------------------------------------------------------------------*/
605 /* Constructors */
607 VTKPolyDataMapper::VTKPolyDataMapper(void) :
608 Inherited ( )
609 #ifdef OSG_WITH_VTK
610 ,_pActor (NULL),
611 _executeTime ( ),
612 _modifiedTime( )
613 #endif
617 VTKPolyDataMapper::VTKPolyDataMapper(const VTKPolyDataMapper &source) :
618 Inherited (source)
619 #ifdef OSG_WITH_VTK
620 ,_pActor (NULL ),
621 _executeTime ( ),
622 _modifiedTime( )
623 #endif
627 /*-------------------------------------------------------------------------*/
628 /* Destructor */
630 VTKPolyDataMapper::~VTKPolyDataMapper(void)
634 void VTKPolyDataMapper::resolveLinks(void)
636 Inherited::resolveLinks();
638 #ifdef OSG_WITH_VTK
639 if(_pActor != NULL)
640 _pActor->UnRegister(NULL);
641 #endif
644 /*-------------------------------------------------------------------------*/
645 /* Render */
647 Action::ResultE VTKPolyDataMapper::renderEnter(Action *action)
649 RenderAction *pAction = dynamic_cast<RenderAction *>(action);
651 this->execute();
654 if(pAction == NULL)
655 return Action::Skip;
657 if(pAction!= NULL)
659 pAction->useNodeList();
661 // if(pAction->isVisible(getCPtr(_sfRoot.getValue())))
663 pAction->addNode(_sfRoot.getValue());
667 return Action::Continue;
670 Action::ResultE VTKPolyDataMapper::renderLeave(Action *action)
672 // RenderAction *pAction = dynamic_cast<RenderAction *>(action);
674 return Action::Continue;
678 /*-------------------------------------------------------------------------*/
679 /* Intersect */
681 Action::ResultE VTKPolyDataMapper::intersectEnter(Action *action)
684 IntersectAction *ia = dynamic_cast<IntersectAction *>(action);
685 Matrix m = this->getMatrix();
687 m.invert();
689 Pnt3f pos;
690 Vec3f dir;
692 m.multFullMatrixPnt(ia->getLine().getPosition (), pos);
693 m.multMatrixVec (ia->getLine().getDirection(), dir);
695 ia->setLine(Line(pos, dir), ia->getMaxDist());
696 ia->scale(dir.length());
699 return Action::Continue;
702 Action::ResultE VTKPolyDataMapper::intersectLeave(Action *action)
705 IntersectAction *ia = dynamic_cast<IntersectAction *>(action);
706 Matrix m = this->getMatrix();
708 Pnt3f pos;
709 Vec3f dir;
711 m.multFullMatrixPnt(ia->getLine().getPosition (), pos);
712 m.multMatrixVec (ia->getLine().getDirection(), dir);
714 ia->setLine(Line(pos, dir), ia->getMaxDist());
715 ia->scale(dir.length());
718 return Action::Continue;
723 /*-------------------------------------------------------------------------*/
724 /* Init */
726 void VTKPolyDataMapper::initMethod(InitPhase ePhase)
728 Inherited::initMethod(ePhase);
730 if(ePhase == TypeObject::SystemPost)
732 IntersectAction::registerEnterDefault(
733 getClassType(),
734 reinterpret_cast<
735 Action::Callback>(&VTKPolyDataMapper::intersectEnter));
737 IntersectAction::registerLeaveDefault(
738 getClassType(),
739 reinterpret_cast<
740 Action::Callback>(&VTKPolyDataMapper::intersectLeave));
743 RenderAction::registerEnterDefault(
744 getClassType(),
745 reinterpret_cast<
746 Action::Callback>(&VTKPolyDataMapper::renderEnter));
748 RenderAction::registerLeaveDefault(
749 getClassType(),
750 reinterpret_cast<
751 Action::Callback>(&VTKPolyDataMapper::renderLeave));