Changed usage of assert() to throw.

This commit is contained in:
Robert Osfield 2004-09-07 14:34:04 +00:00
parent 94f1f6025c
commit daf8887bb0
5 changed files with 164 additions and 142 deletions

View File

@ -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"<<std::endl;
triangle_stripper::tri_stripper stripifier(taf._in_indices);
stripifier.SetCacheSize(_cacheSize);
stripifier.SetMinStripSize(_minStripSize);
typedef triangle_stripper::tri_stripper::primitives_vector::iterator prim_iterator;
typedef std::multimap<unsigned int,prim_iterator> 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"<<std::endl;
typedef triangle_stripper::tri_stripper::primitives_vector::iterator prim_iterator;
typedef std::multimap<unsigned int,prim_iterator> 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<unsigned int,prim_iterator>(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<unsigned int,prim_iterator>(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;i<colors->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
}
catch(const char* errorMessage)
{
osg::notify(osg::WARN)<<"Warning: '"<<errorMessage<<"' exception thrown from triangle_stripper"<<std::endl;
}
catch(triangle_stripper::tri_stripper::triangles_indices_error&)
{
osg::notify(osg::WARN)<<"Warning: triangles_indices_error exception thrown from triangle_stripper"<<std::endl;
}
catch(...)
{
osg::notify(osg::WARN)<<"Warning: Unhandled exception thrown from triangle_stripper"<<std::endl;
}
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;i<colors->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

View File

@ -255,7 +255,8 @@ inline void graph_array<nodetype, arctype>::setsize(const size_t NbNodes) {
template <class nodetype, class arctype>
inline typename graph_array<nodetype, arctype>::node & graph_array<nodetype, arctype>::operator [] (const nodeid & i) {
// Debug check
assert(i < size());
// assert(i < size());
if (i >= size()) throw "graph_array<nodetype, arctype>::operator [] out of range";
return m_Nodes[i];
}
@ -264,7 +265,8 @@ inline typename graph_array<nodetype, arctype>::node & graph_array<nodetype, arc
template <class nodetype, class arctype>
inline const typename graph_array<nodetype, arctype>::node & graph_array<nodetype, arctype>::operator [] (const nodeid & i) const {
// Debug check
assert(i < size());
// assert(i < size());
if (i >= size()) throw "graph_array<nodetype, arctype>::operator [] out of range";
return m_Nodes[i];
}

View File

@ -148,7 +148,8 @@ inline size_t heap_array<T, CmpT>::size() const {
template <class T, class CmpT>
inline const T & heap_array<T, CmpT>::top() const {
// Debug check to ensure heap is not empty
assert(! empty());
//assert(! empty());
if (empty()) throw "heap_array<T, CmpT>::top() error, heap empty";
return m_Heap.front().m_Elem;
}
@ -157,7 +158,8 @@ inline const T & heap_array<T, CmpT>::top() const {
template <class T, class CmpT>
inline const T & heap_array<T, CmpT>::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<T, CmpT>::peek(size_t i) error";
return (m_Heap[m_Finder[i]].m_Elem);
}
@ -174,8 +176,9 @@ inline void heap_array<T, CmpT>::pop() {
m_Locked = true;
// Debug check to ensure heap is not empty
assert(! empty());
//assert(! empty());
if (empty()) throw "heap_array<T, CmpT>::pop() error, heap empty";
Swap(0, size() - 1);
m_Heap.pop_back();
Adjust(0);
@ -185,7 +188,7 @@ inline void heap_array<T, CmpT>::pop() {
template <class T, class CmpT>
inline size_t heap_array<T, CmpT>::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<T, CmpT>::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<T, CmpT>::erase(size_t i) error";
size_t j = m_Finder[i];
Swap(j, size() - 1);
@ -225,7 +229,8 @@ inline bool heap_array<T, CmpT>::valid(size_t i) const {
template <class T, class CmpT>
inline void heap_array<T, CmpT>::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<T, CmpT>::update(size_t i, const T & Elem) error";
size_t j = m_Finder[i];
m_Heap[j].m_Elem = Elem;

View File

@ -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();

View File

@ -68,8 +68,6 @@
#include <vector>
#include <assert.h>
// namespace triangle_stripper
namespace triangle_stripper {