From daf8887bb0f80a1b9cd876bf3349a0dd3c59e24f Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Tue, 7 Sep 2004 14:34:04 +0000 Subject: [PATCH] Changed usage of assert() to throw. --- src/osgUtil/TriStripVisitor.cpp | 275 ++++++++++++++------------ src/osgUtil/TriStrip_graph_array.h | 6 +- src/osgUtil/TriStrip_heap_array.h | 19 +- src/osgUtil/TriStrip_tri_stripper.cpp | 4 +- src/osgUtil/TriStrip_tri_stripper.h | 2 - 5 files changed, 164 insertions(+), 142 deletions(-) diff --git a/src/osgUtil/TriStripVisitor.cpp b/src/osgUtil/TriStripVisitor.cpp index 0bb9fe1b9..6dec8fd90 100644 --- a/src/osgUtil/TriStripVisitor.cpp +++ b/src/osgUtil/TriStripVisitor.cpp @@ -390,155 +390,170 @@ void TriStripVisitor::stripify(Geometry& geom) RemapArray ra(copyMapping); arrayComparitor.accept(ra); - triangle_stripper::tri_stripper stripifier(taf._in_indices); - stripifier.SetCacheSize(_cacheSize); - stripifier.SetMinStripSize(_minStripSize); - - triangle_stripper::tri_stripper::primitives_vector outPrimitives; - stripifier.Strip(&outPrimitives); - - triangle_stripper::tri_stripper::primitives_vector::iterator pitr; - if (_generateFourPointPrimitivesQuads) + try { - osg::notify(osg::WARN)<<"Collecting all quads"< QuadMap; - QuadMap quadMap; + triangle_stripper::tri_stripper::primitives_vector outPrimitives; + stripifier.Strip(&outPrimitives); - // pick out quads and place them in the quadMap, and also look for the max + triangle_stripper::tri_stripper::primitives_vector::iterator pitr; + if (_generateFourPointPrimitivesQuads) + { + osg::notify(osg::WARN)<<"Collecting all quads"< QuadMap; + QuadMap quadMap; + + // pick out quads and place them in the quadMap, and also look for the max + for(pitr=outPrimitives.begin(); + pitr!=outPrimitives.end(); + ++pitr) + { + if (pitr->m_Indices.size()==4) + { + std::swap(pitr->m_Indices[2],pitr->m_Indices[3]); + unsigned int minValue = *(std::max_element(pitr->m_Indices.begin(),pitr->m_Indices.end())); + quadMap.insert(std::pair(minValue,pitr)); + } + } + + + // handle the quads + if (!quadMap.empty()) + { + IndexList indices; + indices.reserve(4*quadMap.size()); + + // adds all the quads into the quad primitive, in ascending order + // and the QuadMap stores the quad's in ascending order. + for(QuadMap::iterator qitr=quadMap.begin(); + qitr!=quadMap.end(); + ++qitr) + { + pitr = qitr->second; + + unsigned int min_pos = 0; + for(i=1;i<4;++i) + { + if (pitr->m_Indices[min_pos]>pitr->m_Indices[i]) + min_pos = i; + } + indices.push_back(pitr->m_Indices[min_pos]); + indices.push_back(pitr->m_Indices[(min_pos+1)%4]); + indices.push_back(pitr->m_Indices[(min_pos+2)%4]); + indices.push_back(pitr->m_Indices[(min_pos+3)%4]); + } + + bool inOrder = true; + unsigned int previousValue = indices.front(); + for(IndexList::iterator qi_itr=indices.begin()+1; + qi_itr!=indices.end() && inOrder; + ++qi_itr) + { + inOrder = (previousValue+1)==*qi_itr; + previousValue = *qi_itr; + } + + + if (inOrder) + { + new_primitives.push_back(new osg::DrawArrays(GL_QUADS,indices.front(),indices.size())); + } + else + { + unsigned int maxValue = *(std::max_element(indices.begin(),indices.end())); + + if (maxValue>=65536) + { + osg::DrawElementsUInt* elements = new osg::DrawElementsUInt(GL_QUADS); + std::copy(indices.begin(),indices.end(),std::back_inserter(*elements)); + new_primitives.push_back(elements); + } + else + { + osg::DrawElementsUShort* elements = new osg::DrawElementsUShort(GL_QUADS); + std::copy(indices.begin(),indices.end(),std::back_inserter(*elements)); + new_primitives.push_back(elements); + } + } + } + } + + // handle non quad primitives for(pitr=outPrimitives.begin(); pitr!=outPrimitives.end(); ++pitr) { - if (pitr->m_Indices.size()==4) + if (!_generateFourPointPrimitivesQuads || pitr->m_Indices.size()!=4) { - std::swap(pitr->m_Indices[2],pitr->m_Indices[3]); - unsigned int minValue = *(std::max_element(pitr->m_Indices.begin(),pitr->m_Indices.end())); - quadMap.insert(std::pair(minValue,pitr)); - } - } - - - // handle the quads - if (!quadMap.empty()) - { - IndexList indices; - indices.reserve(4*quadMap.size()); - - // adds all the quads into the quad primitive, in ascending order - // and the QuadMap stores the quad's in ascending order. - for(QuadMap::iterator qitr=quadMap.begin(); - qitr!=quadMap.end(); - ++qitr) - { - pitr = qitr->second; - - unsigned int min_pos = 0; - for(i=1;i<4;++i) + bool inOrder = true; + unsigned int previousValue = pitr->m_Indices.front(); + for(triangle_stripper::tri_stripper::indices::iterator qi_itr=pitr->m_Indices.begin()+1; + qi_itr!=pitr->m_Indices.end() && inOrder; + ++qi_itr) { - if (pitr->m_Indices[min_pos]>pitr->m_Indices[i]) - min_pos = i; + inOrder = (previousValue+1)==*qi_itr; + previousValue = *qi_itr; } - indices.push_back(pitr->m_Indices[min_pos]); - indices.push_back(pitr->m_Indices[(min_pos+1)%4]); - indices.push_back(pitr->m_Indices[(min_pos+2)%4]); - indices.push_back(pitr->m_Indices[(min_pos+3)%4]); - } - bool inOrder = true; - unsigned int previousValue = indices.front(); - for(IndexList::iterator qi_itr=indices.begin()+1; - qi_itr!=indices.end() && inOrder; - ++qi_itr) - { - inOrder = (previousValue+1)==*qi_itr; - previousValue = *qi_itr; - } - - - if (inOrder) - { - new_primitives.push_back(new osg::DrawArrays(GL_QUADS,indices.front(),indices.size())); - } - else - { - unsigned int maxValue = *(std::max_element(indices.begin(),indices.end())); - - if (maxValue>=65536) + if (inOrder) { - osg::DrawElementsUInt* elements = new osg::DrawElementsUInt(GL_QUADS); - std::copy(indices.begin(),indices.end(),std::back_inserter(*elements)); - new_primitives.push_back(elements); + new_primitives.push_back(new osg::DrawArrays(pitr->m_Type,pitr->m_Indices.front(),pitr->m_Indices.size())); } else { - osg::DrawElementsUShort* elements = new osg::DrawElementsUShort(GL_QUADS); - std::copy(indices.begin(),indices.end(),std::back_inserter(*elements)); - new_primitives.push_back(elements); - } - } - } - } - - // handle non quad primitives - for(pitr=outPrimitives.begin(); - pitr!=outPrimitives.end(); - ++pitr) - { - if (!_generateFourPointPrimitivesQuads || pitr->m_Indices.size()!=4) - { - bool inOrder = true; - unsigned int previousValue = pitr->m_Indices.front(); - for(triangle_stripper::tri_stripper::indices::iterator qi_itr=pitr->m_Indices.begin()+1; - qi_itr!=pitr->m_Indices.end() && inOrder; - ++qi_itr) - { - inOrder = (previousValue+1)==*qi_itr; - previousValue = *qi_itr; - } - - if (inOrder) - { - new_primitives.push_back(new osg::DrawArrays(pitr->m_Type,pitr->m_Indices.front(),pitr->m_Indices.size())); - } - else - { - unsigned int maxValue = *(std::max_element(pitr->m_Indices.begin(),pitr->m_Indices.end())); - if (maxValue>=65536) - { - osg::DrawElementsUInt* elements = new osg::DrawElementsUInt(pitr->m_Type); - elements->reserve(pitr->m_Indices.size()); - std::copy(pitr->m_Indices.begin(),pitr->m_Indices.end(),std::back_inserter(*elements)); - new_primitives.push_back(elements); - } - else - { - osg::DrawElementsUShort* elements = new osg::DrawElementsUShort(pitr->m_Type); - elements->reserve(pitr->m_Indices.size()); - std::copy(pitr->m_Indices.begin(),pitr->m_Indices.end(),std::back_inserter(*elements)); - new_primitives.push_back(elements); + unsigned int maxValue = *(std::max_element(pitr->m_Indices.begin(),pitr->m_Indices.end())); + if (maxValue>=65536) + { + osg::DrawElementsUInt* elements = new osg::DrawElementsUInt(pitr->m_Type); + elements->reserve(pitr->m_Indices.size()); + std::copy(pitr->m_Indices.begin(),pitr->m_Indices.end(),std::back_inserter(*elements)); + new_primitives.push_back(elements); + } + else + { + osg::DrawElementsUShort* elements = new osg::DrawElementsUShort(pitr->m_Type); + elements->reserve(pitr->m_Indices.size()); + std::copy(pitr->m_Indices.begin(),pitr->m_Indices.end(),std::back_inserter(*elements)); + new_primitives.push_back(elements); + } } } } + + geom.setPrimitiveSetList(new_primitives); + + #if 0 + // debugging code for indentifying the tri-strips. + osg::Vec4Array* colors = new osg::Vec4Array(new_primitives.size()); + for(i=0;isize();++i) + { + (*colors)[i].set(((float)rand()/(float)RAND_MAX), + ((float)rand()/(float)RAND_MAX), + ((float)rand()/(float)RAND_MAX), + 1.0f); + } + geom.setColorArray(colors); + geom.setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE_SET); + #endif + + } + catch(const char* errorMessage) + { + osg::notify(osg::WARN)<<"Warning: '"<size();++i) - { - (*colors)[i].set(((float)rand()/(float)RAND_MAX), - ((float)rand()/(float)RAND_MAX), - ((float)rand()/(float)RAND_MAX), - 1.0f); - } - geom.setColorArray(colors); - geom.setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE_SET); - #endif } else diff --git a/src/osgUtil/TriStrip_graph_array.h b/src/osgUtil/TriStrip_graph_array.h index e4935a092..eccd8e1d0 100644 --- a/src/osgUtil/TriStrip_graph_array.h +++ b/src/osgUtil/TriStrip_graph_array.h @@ -255,7 +255,8 @@ inline void graph_array::setsize(const size_t NbNodes) { template inline typename graph_array::node & graph_array::operator [] (const nodeid & i) { // Debug check - assert(i < size()); + // assert(i < size()); + if (i >= size()) throw "graph_array::operator [] out of range"; return m_Nodes[i]; } @@ -264,7 +265,8 @@ inline typename graph_array::node & graph_array inline const typename graph_array::node & graph_array::operator [] (const nodeid & i) const { // Debug check - assert(i < size()); + // assert(i < size()); + if (i >= size()) throw "graph_array::operator [] out of range"; return m_Nodes[i]; } diff --git a/src/osgUtil/TriStrip_heap_array.h b/src/osgUtil/TriStrip_heap_array.h index 78f8e5e92..920b8d405 100644 --- a/src/osgUtil/TriStrip_heap_array.h +++ b/src/osgUtil/TriStrip_heap_array.h @@ -148,7 +148,8 @@ inline size_t heap_array::size() const { template inline const T & heap_array::top() const { // Debug check to ensure heap is not empty - assert(! empty()); + //assert(! empty()); + if (empty()) throw "heap_array::top() error, heap empty"; return m_Heap.front().m_Elem; } @@ -157,7 +158,8 @@ inline const T & heap_array::top() const { template inline const T & heap_array::peek(size_t i) const { // Debug check to ensure element is still present - assert(! removed(i)); + //assert(! removed(i)); + if (removed(i)) throw "heap_array::peek(size_t i) error"; return (m_Heap[m_Finder[i]].m_Elem); } @@ -174,8 +176,9 @@ inline void heap_array::pop() { m_Locked = true; // Debug check to ensure heap is not empty - assert(! empty()); - + //assert(! empty()); + if (empty()) throw "heap_array::pop() error, heap empty"; + Swap(0, size() - 1); m_Heap.pop_back(); Adjust(0); @@ -185,7 +188,7 @@ inline void heap_array::pop() { template inline size_t heap_array::push(const T & Elem) { if (m_Locked) - throw heap_is_locked(); + throw "heap_is_locked"; size_t Id = size(); m_Finder.push_back(Id); @@ -201,7 +204,8 @@ inline void heap_array::erase(size_t i) { m_Locked = true; // Debug check to ensure element is still present - assert(! removed(i)); + // assert(! removed(i)); + if (removed(i)) throw "heap_array::erase(size_t i) error"; size_t j = m_Finder[i]; Swap(j, size() - 1); @@ -225,7 +229,8 @@ inline bool heap_array::valid(size_t i) const { template inline void heap_array::update(size_t i, const T & Elem) { // Debug check to ensure element is still present - assert(! removed(i)); + // assert(! removed(i)); + if (removed(i)) throw "heap_array::update(size_t i, const T & Elem) error"; size_t j = m_Finder[i]; m_Heap[j].m_Elem = Elem; diff --git a/src/osgUtil/TriStrip_tri_stripper.cpp b/src/osgUtil/TriStrip_tri_stripper.cpp index bfd945e95..8dedf20aa 100644 --- a/src/osgUtil/TriStrip_tri_stripper.cpp +++ b/src/osgUtil/TriStrip_tri_stripper.cpp @@ -438,7 +438,9 @@ void tri_stripper::BuildStrip(const triangle_strip TriStrip) } // Debug check: we must have found the next triangle - assert(LinkIt != TriNodeIt->out_end()); + //assert(LinkIt != TriNodeIt->out_end()); + if (LinkIt == TriNodeIt->out_end()) throw "tri_stripper::BuildStrip(,) error, next triangle not found"; + // Go to the next triangle TriNodeIt = LinkIt->terminal(); diff --git a/src/osgUtil/TriStrip_tri_stripper.h b/src/osgUtil/TriStrip_tri_stripper.h index 96cc696ca..d8d07728f 100644 --- a/src/osgUtil/TriStrip_tri_stripper.h +++ b/src/osgUtil/TriStrip_tri_stripper.h @@ -68,8 +68,6 @@ #include -#include - // namespace triangle_stripper namespace triangle_stripper {