From 603c280b625135e1cac737783d015a185537a42f Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Thu, 20 Jun 2013 15:18:51 +0000 Subject: [PATCH] Added local namespace for util functions, and fixed the return type of checkDeprecatedData() --- src/osg/Geometry.cpp | 254 ++++++++++++++++++++++--------------------- 1 file changed, 130 insertions(+), 124 deletions(-) diff --git a/src/osg/Geometry.cpp b/src/osg/Geometry.cpp index c45a46a0f..66008fe52 100644 --- a/src/osg/Geometry.cpp +++ b/src/osg/Geometry.cpp @@ -1234,111 +1234,117 @@ void Geometry::duplicateSharedArrays() } } -inline osg::IndexArray* getIndexArray(osg::Array* array) +namespace GeometryUtilFunctions { - return array ? dynamic_cast(array->getUserData()) : 0; -} - -inline bool containsDeprecatedUsage(osg::Array* array) -{ - if (array) + + inline osg::IndexArray* getIndexArray(osg::Array* array) { - if (array->getBinding()==3 /* BIND_PER_PRIMITIVE */) return true; - if (dynamic_cast(array->getUserData())!=0) return true; + return array ? dynamic_cast(array->getUserData()) : 0; } - return true; + + inline bool containsDeprecatedUsage(osg::Array* array) + { + if (array) + { + if (array->getBinding()==3 /* BIND_PER_PRIMITIVE */) return true; + if (dynamic_cast(array->getUserData())!=0) return true; + } + return false; + } + + static osg::Array* expandIndexArray(const osg::Array* sourceArray, const osg::IndexArray* indices) + { + osg::ref_ptr targetArray = osg::cloneType(sourceArray); + targetArray->setBinding(sourceArray->getBinding()); + targetArray->setNormalize(sourceArray->getNormalize()); + targetArray->setPreserveDataType(sourceArray->getPreserveDataType()); + targetArray->resizeArray(indices->getNumElements()); + + unsigned int elementSize = sourceArray->getElementSize(); + const char* sourcePtr = static_cast(sourceArray->getDataPointer()); + char* targetPtr = const_cast(static_cast(targetArray->getDataPointer())); + for(unsigned int i=0; igetNumElements(); ++i) + { + unsigned int vi = indices->index(i); + const char* sourceElementPtr = sourcePtr + elementSize*vi; + char* targetElementPtr = targetPtr + elementSize*i; + for(unsigned int j=0; j, osg::ref_ptr > ArrayPair; + typedef std::vector< ArrayPair > ArrayPairs; + static void duplicateArray(ArrayPairs& pairs, osg::ref_ptr& sourceArray, unsigned int numVertices) + { + osg::Array* targetArray = osg::cloneType(sourceArray.get()); + targetArray->setBinding(osg::Array::BIND_PER_VERTEX); + targetArray->setNormalize(sourceArray->getNormalize()); + targetArray->setPreserveDataType(sourceArray->getPreserveDataType()); + targetArray->resizeArray(numVertices); + pairs.push_back(ArrayPair(sourceArray, targetArray)); + sourceArray = targetArray; + } + + struct PtrData + { + char* source; + char* target; + unsigned int elementSize; + + PtrData():source(0),target(0),elementSize(0) {} + + PtrData(osg::Array* s, osg::Array* t): + source(const_cast(static_cast(s->getDataPointer()))), + target(const_cast(static_cast(t->getDataPointer()))), + elementSize(s->getElementSize()) {} + + PtrData(const PtrData& rhs): + source(rhs.source), + target(rhs.target), + elementSize(rhs.elementSize) {} + + PtrData& operator = (const PtrData& rhs) + { + source = rhs.source; + target = rhs.target; + elementSize = rhs.elementSize; + return *this; + } + }; + } bool Geometry::checkForDeprecatedData() { _containsDeprecatedData = false; - if (containsDeprecatedUsage(_vertexArray.get())) _containsDeprecatedData = true; + if (GeometryUtilFunctions::containsDeprecatedUsage(_vertexArray.get())) _containsDeprecatedData = true; - if (containsDeprecatedUsage(_normalArray.get())) _containsDeprecatedData = true; + if (GeometryUtilFunctions::containsDeprecatedUsage(_normalArray.get())) _containsDeprecatedData = true; - if (containsDeprecatedUsage(_colorArray.get())) _containsDeprecatedData = true; + if (GeometryUtilFunctions::containsDeprecatedUsage(_colorArray.get())) _containsDeprecatedData = true; - if (containsDeprecatedUsage(_secondaryColorArray.get())) _containsDeprecatedData = true; + if (GeometryUtilFunctions::containsDeprecatedUsage(_secondaryColorArray.get())) _containsDeprecatedData = true; - if (containsDeprecatedUsage(_fogCoordArray.get())) _containsDeprecatedData = true; + if (GeometryUtilFunctions::containsDeprecatedUsage(_fogCoordArray.get())) _containsDeprecatedData = true; for(unsigned int ti=0;ti targetArray = osg::cloneType(sourceArray); - targetArray->setBinding(sourceArray->getBinding()); - targetArray->setNormalize(sourceArray->getNormalize()); - targetArray->setPreserveDataType(sourceArray->getPreserveDataType()); - targetArray->resizeArray(indices->getNumElements()); - - unsigned int elementSize = sourceArray->getElementSize(); - const char* sourcePtr = static_cast(sourceArray->getDataPointer()); - char* targetPtr = const_cast(static_cast(targetArray->getDataPointer())); - for(unsigned int i=0; igetNumElements(); ++i) - { - unsigned int vi = indices->index(i); - const char* sourceElementPtr = sourcePtr + elementSize*vi; - char* targetElementPtr = targetPtr + elementSize*i; - for(unsigned int j=0; j, osg::ref_ptr > ArrayPair; -typedef std::vector< ArrayPair > ArrayPairs; -static void duplicateArray(ArrayPairs& pairs, osg::ref_ptr& sourceArray, unsigned int numVertices) -{ - osg::Array* targetArray = osg::cloneType(sourceArray.get()); - targetArray->setBinding(osg::Array::BIND_PER_VERTEX); - targetArray->setNormalize(sourceArray->getNormalize()); - targetArray->setPreserveDataType(sourceArray->getPreserveDataType()); - targetArray->resizeArray(numVertices); - pairs.push_back(ArrayPair(sourceArray, targetArray)); - sourceArray = targetArray; -} - -struct PtrData -{ - char* source; - char* target; - unsigned int elementSize; - - PtrData():source(0),target(0),elementSize(0) {} - - PtrData(osg::Array* s, osg::Array* t): - source(const_cast(static_cast(s->getDataPointer()))), - target(const_cast(static_cast(t->getDataPointer()))), - elementSize(s->getElementSize()) {} - - PtrData(const PtrData& rhs): - source(rhs.source), - target(rhs.target), - elementSize(rhs.elementSize) {} - - PtrData& operator = (const PtrData& rhs) - { - source = rhs.source; - target = rhs.target; - elementSize = rhs.elementSize; - return *this; - } -}; void Geometry::fixDeprecatedData() { @@ -1349,36 +1355,36 @@ void Geometry::fixDeprecatedData() OSG_NOTICE<<"Geometry::fixDeprecatedData()"<getBinding()==osg::Array::BIND_PER_VERTEX) duplicateArray(perVertexArrays, _normalArray, numVertices); - else if (_normalArray->getBinding()==3 /*osg::Array::BIND_PER_PRIMITIVE*/) duplicateArray(perPrimitiveArrays, _normalArray, numVertices); + if (_normalArray->getBinding()==osg::Array::BIND_PER_VERTEX) GeometryUtilFunctions::duplicateArray(perVertexArrays, _normalArray, numVertices); + else if (_normalArray->getBinding()==3 /*osg::Array::BIND_PER_PRIMITIVE*/) GeometryUtilFunctions::duplicateArray(perPrimitiveArrays, _normalArray, numVertices); } if (_colorArray.valid()) { - if (_colorArray->getBinding()==osg::Array::BIND_PER_VERTEX) duplicateArray(perVertexArrays, _colorArray, numVertices); - else if (_colorArray->getBinding()==3 /*osg::Array::BIND_PER_PRIMITIVE*/) duplicateArray(perPrimitiveArrays, _colorArray, numVertices); + if (_colorArray->getBinding()==osg::Array::BIND_PER_VERTEX) GeometryUtilFunctions::duplicateArray(perVertexArrays, _colorArray, numVertices); + else if (_colorArray->getBinding()==3 /*osg::Array::BIND_PER_PRIMITIVE*/) GeometryUtilFunctions::duplicateArray(perPrimitiveArrays, _colorArray, numVertices); } if (_secondaryColorArray.valid()) { - if (_secondaryColorArray->getBinding()==osg::Array::BIND_PER_VERTEX) duplicateArray(perVertexArrays, _secondaryColorArray, numVertices); - else if (_secondaryColorArray->getBinding()==3 /*osg::Array::BIND_PER_PRIMITIVE*/) duplicateArray(perPrimitiveArrays, _secondaryColorArray, numVertices); + if (_secondaryColorArray->getBinding()==osg::Array::BIND_PER_VERTEX) GeometryUtilFunctions::duplicateArray(perVertexArrays, _secondaryColorArray, numVertices); + else if (_secondaryColorArray->getBinding()==3 /*osg::Array::BIND_PER_PRIMITIVE*/) GeometryUtilFunctions::duplicateArray(perPrimitiveArrays, _secondaryColorArray, numVertices); } if (_fogCoordArray.valid()) { - if (_fogCoordArray->getBinding()==osg::Array::BIND_PER_VERTEX) duplicateArray(perVertexArrays, _fogCoordArray, numVertices); - else if (_fogCoordArray->getBinding()==3 /*osg::Array::BIND_PER_PRIMITIVE*/) duplicateArray(perPrimitiveArrays, _fogCoordArray, numVertices); + if (_fogCoordArray->getBinding()==osg::Array::BIND_PER_VERTEX) GeometryUtilFunctions::duplicateArray(perVertexArrays, _fogCoordArray, numVertices); + else if (_fogCoordArray->getBinding()==3 /*osg::Array::BIND_PER_PRIMITIVE*/) GeometryUtilFunctions::duplicateArray(perPrimitiveArrays, _fogCoordArray, numVertices); } for(ArrayList::iterator itr = _texCoordList.begin(); @@ -1478,8 +1484,8 @@ void Geometry::fixDeprecatedData() { if (itr->valid()) { - if ((*itr)->getBinding()==osg::Array::BIND_PER_VERTEX) duplicateArray(perVertexArrays, *itr, numVertices); - else if ((*itr)->getBinding()==3 /*osg::Array::BIND_PER_PRIMITIVE*/) duplicateArray(perPrimitiveArrays, *itr, numVertices); + if ((*itr)->getBinding()==osg::Array::BIND_PER_VERTEX) GeometryUtilFunctions::duplicateArray(perVertexArrays, *itr, numVertices); + else if ((*itr)->getBinding()==3 /*osg::Array::BIND_PER_PRIMITIVE*/) GeometryUtilFunctions::duplicateArray(perPrimitiveArrays, *itr, numVertices); } } @@ -1489,27 +1495,27 @@ void Geometry::fixDeprecatedData() { if (itr->valid()) { - if ((*itr)->getBinding()==osg::Array::BIND_PER_VERTEX) duplicateArray(perVertexArrays, *itr, numVertices); - else if ((*itr)->getBinding()==3 /*osg::Array::BIND_PER_PRIMITIVE*/) duplicateArray(perPrimitiveArrays, *itr, numVertices); + if ((*itr)->getBinding()==osg::Array::BIND_PER_VERTEX) GeometryUtilFunctions::duplicateArray(perVertexArrays, *itr, numVertices); + else if ((*itr)->getBinding()==3 /*osg::Array::BIND_PER_PRIMITIVE*/) GeometryUtilFunctions::duplicateArray(perPrimitiveArrays, *itr, numVertices); } } - typedef std::vector PtrList; + typedef std::vector PtrList; PtrList perVertexPtrs; PtrList perPrimitivePtrs; - for(ArrayPairs::iterator itr = perVertexArrays.begin(); + for(GeometryUtilFunctions::ArrayPairs::iterator itr = perVertexArrays.begin(); itr != perVertexArrays.end(); ++itr) { - perVertexPtrs.push_back(PtrData(itr->first.get(), itr->second.get())); + perVertexPtrs.push_back(GeometryUtilFunctions::PtrData(itr->first.get(), itr->second.get())); } - for(ArrayPairs::iterator itr = perPrimitiveArrays.begin(); + for(GeometryUtilFunctions::ArrayPairs::iterator itr = perPrimitiveArrays.begin(); itr != perPrimitiveArrays.end(); ++itr) { - perPrimitivePtrs.push_back(PtrData(itr->first.get(), itr->second.get())); + perPrimitivePtrs.push_back(GeometryUtilFunctions::PtrData(itr->first.get(), itr->second.get())); } @@ -1559,7 +1565,7 @@ void Geometry::fixDeprecatedData() itr != perVertexPtrs.end(); ++itr) { - PtrData& ptrs = *itr; + GeometryUtilFunctions::PtrData& ptrs = *itr; char* source = ptrs.source + vindex*ptrs.elementSize; char* target = ptrs.target + target_vindex*ptrs.elementSize; for(unsigned int c=0; c