1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
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. *
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. *
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. *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
37 \*---------------------------------------------------------------------------*/
39 #if __GNUC__ >= 4 || __GNUC_MINOR__ >=3
40 #pragma GCC diagnostic warning "-Wold-style-cast"
46 #include "OSGConfig.h"
50 #include "OSGIntersectAction.h"
51 #include "OSGRenderAction.h"
53 #ifdef OSG_HAVE_ACTION //CHECK
54 #include "OSGIntersectActor.h"
57 #include "OSGVTKPolyDataMapper.h"
58 #include "OSGVolume.h"
59 #include "OSGGeometry.h"
60 #include "OSGChunkMaterial.h"
61 #include "OSGMaterialChunk.h"
62 #include "OSGTwoSidedLightingChunk.h"
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"
75 #include "vtkProperty.h"
76 #include "vtkDataArray.h"
77 #include "vtkCellData.h"
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 /*-------------------------------------------------------------------------*/
92 void VTKPolyDataMapper::changed(ConstFieldMaskArg whichField
,
96 Inherited::changed(whichField
, origin
, details
);
99 /*-------------------------------------------------------------------------*/
102 void VTKPolyDataMapper::initGeometries(void)
104 NodeUnrecPtr pRoot
= Node::create();
106 pRoot
->setCore(Group::create());
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();
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
);
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
);
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
,
182 vtkCellArray
*primArray
,
187 // get polyData from vtkActor
189 vtkPolyData
*polyData
=
190 static_cast<vtkPolyData
*>(actor
->GetMapper()->GetInput());
192 int numPrimitives
= primArray
->GetNumberOfCells();
194 if(numPrimitives
== 0)
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
);
229 UInt32 normalPerVertex
= 0;
230 UInt32 normalPerCell
= 0;
231 vtkDataArray
*normalArray
= polyData
->GetPointData()->GetNormals();
233 if(actor
->GetProperty()->GetInterpolation() == VTK_FLAT
)
238 if(normalArray
!= NULL
)
244 normalArray
= polyData
->GetCellData()->GetNormals();
246 if(normalArray
!= NULL
)
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
277 objectColor
= actor
->GetProperty()->GetColor();
280 fprintf(stderr
, "Got normals %d %d %p\n",
285 fprintf(stderr
, "Got colors %d %d %d %p %p\n",
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
);
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(
332 if(colorPerVertex
== 1 && colorArray
!= NULL
)
334 aColor
= colorArray
->GetPointer(4 * pts
[i
]);
339 (*pColorField
)[vert
].setValuesRGBA(
347 if(colorPerObject
== 1 && objectColor
!= NULL
)
349 (*pColorField
)[vert
].setValuesRGBA(objectColor
[0],
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
]);
368 (*pNormalField
)[vert
].setValues(Real32(aNormal
[0]),
381 void VTKPolyDataMapper::execute(void)
389 vtkMapper
*pMapper
= _pActor
->GetMapper();
396 if( (_pActor
-> GetMTime() < this->_executeTime
) &&
397 ( pMapper
-> GetMTime() < this->_executeTime
) &&
398 ( pMapper
->GetInput()->GetMTime() < this->_executeTime
) &&
399 ( this->_modifiedTime
< this->_executeTime
) )
405 fprintf(stderr
, "%ld %ld | %ld %ld %ld\n",
406 _executeTime
.GetMTime(),
407 _modifiedTime
.GetMTime(),
409 _pActor
->GetMapper()->GetMTime(),
410 _pActor
->GetMapper()->GetInput()->GetMTime());
413 if(_sfRoot
.getValue() == NULL
)
418 fprintf(stderr
, "Mapper::execute %p\n", _pActor
);
421 vtkPolyData
*polyData
=
422 static_cast<vtkPolyData
*>(_pActor
->GetMapper()->GetInput());
426 fprintf(stderr
, "Strange no data\n");
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",
449 const VTKPolyDataMapper
*pThis
= this;
453 bool rc
= processPrimitive(pThis
->_mfPositions
[0],
454 pThis
->_mfColors
[0],
455 pThis
->_mfNormals
[0],
456 pThis
->_mfLength
[0],
466 pThis
->_mfGeoRoots
[0]->setTravMask(
467 OSG::TypeTraits
<OSG::UInt32
>::BitsSet
);
469 pThis
->_mfGeoRoots
[0]->invalidateVolume();
475 bool rc
= processPrimitive(pThis
->_mfPositions
[1],
476 pThis
->_mfColors
[1],
477 pThis
->_mfNormals
[1],
478 pThis
->_mfLength
[1],
488 pThis
->_mfGeoRoots
[1]->setTravMask(
489 OSG::TypeTraits
<OSG::UInt32
>::BitsSet
);
491 pThis
->_mfGeoRoots
[1]->invalidateVolume();
498 bool rc
= processPrimitive(pThis
->_mfPositions
[2],
499 pThis
->_mfColors
[2],
500 pThis
->_mfNormals
[2],
501 pThis
->_mfLength
[2],
511 pThis
->_mfGeoRoots
[2]->setTravMask(
512 OSG::TypeTraits
<OSG::UInt32
>::BitsSet
);
514 pThis
->_mfGeoRoots
[2]->invalidateVolume();
520 bool rc
= processPrimitive(pThis
->_mfPositions
[3],
521 pThis
->_mfColors
[3],
522 pThis
->_mfNormals
[3],
523 pThis
->_mfLength
[3],
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();
549 void VTKPolyDataMapper::setActor(vtkActor
*pActor
)
552 _pActor
->UnRegister(NULL
);
557 _pActor
->Register(NULL
);
559 this->_modifiedTime
.Modified();
564 void VTKPolyDataMapper::adjustVolume(Volume
&volume
)
568 if(_sfRoot
.getValue() != NULL
)
570 _sfRoot
.getValue()->updateVolume();
572 volume
.extendBy(_sfRoot
.getValue()->getVolume());
576 /*-------------------------------------------------------------------------*/
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
);
596 indentLog(uiIndent
+ 8, PLOG
);
597 PLOG
<< "(NULL)" << std::endl
;
600 indentLog(uiIndent
, PLOG
);
601 PLOG
<< "}" << std::endl
;
604 /*-------------------------------------------------------------------------*/
607 VTKPolyDataMapper::VTKPolyDataMapper(void) :
617 VTKPolyDataMapper::VTKPolyDataMapper(const VTKPolyDataMapper
&source
) :
627 /*-------------------------------------------------------------------------*/
630 VTKPolyDataMapper::~VTKPolyDataMapper(void)
634 void VTKPolyDataMapper::resolveLinks(void)
636 Inherited::resolveLinks();
640 _pActor
->UnRegister(NULL
);
644 /*-------------------------------------------------------------------------*/
647 Action::ResultE
VTKPolyDataMapper::renderEnter(Action
*action
)
649 RenderAction
*pAction
= dynamic_cast<RenderAction
*>(action
);
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 /*-------------------------------------------------------------------------*/
681 Action::ResultE
VTKPolyDataMapper::intersectEnter(Action
*action
)
684 IntersectAction *ia = dynamic_cast<IntersectAction *>(action);
685 Matrix m = this->getMatrix();
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();
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 /*-------------------------------------------------------------------------*/
726 void VTKPolyDataMapper::initMethod(InitPhase ePhase
)
728 Inherited::initMethod(ePhase
);
730 if(ePhase
== TypeObject::SystemPost
)
732 IntersectAction::registerEnterDefault(
735 Action::Callback
>(&VTKPolyDataMapper::intersectEnter
));
737 IntersectAction::registerLeaveDefault(
740 Action::Callback
>(&VTKPolyDataMapper::intersectLeave
));
743 RenderAction::registerEnterDefault(
746 Action::Callback
>(&VTKPolyDataMapper::renderEnter
));
748 RenderAction::registerLeaveDefault(
751 Action::Callback
>(&VTKPolyDataMapper::renderLeave
));