Removed usaged of throw and catch to enable better compatibility with embedded systems that don't support C++ exceptions efficiently.

This commit is contained in:
Robert Osfield 2009-11-16 14:47:52 +00:00
parent 52fbe9723f
commit d4c58cf1a1
6 changed files with 184 additions and 175 deletions

View File

@ -26,6 +26,8 @@
#include <OpenThreads/ScopedLock>
#include <OpenThreads/Mutex>
// #define CHECK_CONSISTENCY
using namespace osg;
// static cache of deleted buffer object lists which can only
@ -456,8 +458,9 @@ GLBufferObjectSet::~GLBufferObjectSet()
bool GLBufferObjectSet::checkConsistency() const
{
#ifndef CHECK_CONSISTENCY
return true;
#else
// osg::notify(osg::NOTICE)<<"GLBufferObjectSet::checkConsistency()"<<std::endl;
// check consistency of linked list.
unsigned int numInList = 0;
@ -470,16 +473,16 @@ bool GLBufferObjectSet::checkConsistency() const
{
if ((to->_next)->_previous != to)
{
osg::notify(osg::NOTICE)<<"Error (to->_next)->_previous != to "<<std::endl;
throw "Error (to->_next)->_previous != to ";
osg::notify(osg::NOTICE)<<"GLBufferObjectSet::checkConsistency() : Error (to->_next)->_previous != to "<<std::endl;
return false;
}
}
else
{
if (_tail != to)
{
osg::notify(osg::NOTICE)<<"Error _trail != to"<<std::endl;
throw "Error _trail != to";
osg::notify(osg::NOTICE)<<"GLBufferObjectSet::checkConsistency() : Error _trail != to"<<std::endl;
return false;
}
}
@ -498,6 +501,7 @@ bool GLBufferObjectSet::checkConsistency() const
}
return true;
#endif
}
void GLBufferObjectSet::handlePendingOrphandedGLBufferObjects()

View File

@ -659,7 +659,7 @@ void Image::allocateImage(int s,int t,int r,
else
{
// throw exception?? not for now, will simply set values to 0.
// failed to allocate memory, for now, will simply set values to 0.
_s = 0;
_t = 0;
_r = 0;

View File

@ -49,6 +49,8 @@
// #define DO_TIMING
#define CHECK_CONSISTENCY
namespace osg {
ApplicationUsageProxy Texture_e0(ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_MAX_TEXTURE_SIZE","Set the maximum size of textures.");
@ -169,8 +171,9 @@ Texture::TextureObjectSet::~TextureObjectSet()
bool Texture::TextureObjectSet::checkConsistency() const
{
// return true;
#ifndef CHECK_CONSISTENCY
return true;
#else
// osg::notify(osg::NOTICE)<<"TextureObjectSet::checkConsistency()"<<std::endl;
// check consistency of linked list.
unsigned int numInList = 0;
@ -183,16 +186,16 @@ bool Texture::TextureObjectSet::checkConsistency() const
{
if ((to->_next)->_previous != to)
{
osg::notify(osg::NOTICE)<<"Error (to->_next)->_previous != to "<<std::endl;
throw "Error (to->_next)->_previous != to ";
osg::notify(osg::NOTICE)<<"Texture::TextureObjectSet::checkConsistency() : Error (to->_next)->_previous != to "<<std::endl;
return false;
}
}
else
{
if (_tail != to)
{
osg::notify(osg::NOTICE)<<"Error _trail != to"<<std::endl;
throw "Error _trail != to";
osg::notify(osg::NOTICE)<<"Texture::TextureObjectSet::checkConsistency() : Error _trail != to"<<std::endl;
return false;
}
}
@ -211,6 +214,7 @@ bool Texture::TextureObjectSet::checkConsistency() const
}
return true;
#endif
}
void Texture::TextureObjectSet::handlePendingOrphandedTextureObjects()

View File

@ -403,171 +403,158 @@ void TriStripVisitor::stripify(Geometry& geom)
RemapArray ra(copyMapping);
arrayComparitor.accept(ra);
try
triangle_stripper::tri_stripper stripifier(taf._in_indices);
stripifier.SetCacheSize(_cacheSize);
stripifier.SetMinStripSize(_minStripSize);
triangle_stripper::tri_stripper::primitives_vector outPrimitives;
if (!stripifier.Strip(&outPrimitives))
{
triangle_stripper::tri_stripper stripifier(taf._in_indices);
stripifier.SetCacheSize(_cacheSize);
stripifier.SetMinStripSize(_minStripSize);
osg::notify(osg::WARN)<<"Error: TriStripVisitor::stripify(Geometry& geom) failed."<<std::endl;
return;
}
triangle_stripper::tri_stripper::primitives_vector outPrimitives;
stripifier.Strip(&outPrimitives);
triangle_stripper::tri_stripper::primitives_vector::iterator pitr;
if (_generateFourPointPrimitivesQuads)
{
osg::notify(osg::INFO)<<"Collecting all quads"<<std::endl;
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;
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(QuadMap::value_type(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
// pick out quads and place them in the quadMap, and also look for the max
for(pitr=outPrimitives.begin();
pitr!=outPrimitives.end();
++pitr)
{
if (!_generateFourPointPrimitivesQuads || pitr->m_Indices.size()!=4)
if (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);
}
}
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(QuadMap::value_type(minValue,pitr));
}
}
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)
// 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)
{
(*colors)[i].set(((float)rand()/(float)RAND_MAX),
((float)rand()/(float)RAND_MAX),
((float)rand()/(float)RAND_MAX),
1.0f);
if (pitr->m_Indices[min_pos]>pitr->m_Indices[i])
min_pos = i;
}
geom.setColorArray(colors);
geom.setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE_SET);
#endif
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]);
}
}
catch(const char* errorMessage)
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)
{
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;
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);
}
}
}
}
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

@ -12,14 +12,14 @@
*/
// tri_stripper.cpp: implementation of the Tri Stripper class.
//
// Copyright (C) 2002 Tanguy Fautré.
// Copyright (C) 2002 Tanguy Fautr<EFBFBD>.
// For conditions of distribution and use,
// see copyright notice in tri_stripper.h
//
//////////////////////////////////////////////////////////////////////
#include "TriStrip_tri_stripper.h"
#include <osg/Notify>
// namespace triangle_stripper
@ -38,11 +38,14 @@ namespace triangle_stripper {
// Members Functions
//////////////////////////////////////////////////////////////////////
void tri_stripper::Strip(primitives_vector * out_pPrimitivesVector)
bool tri_stripper::Strip(primitives_vector * out_pPrimitivesVector)
{
// verify that the number of indices is correct
if (m_TriIndices.size() % 3 != 0)
throw triangles_indices_error();
{
osg::notify(osg::NOTICE)<<"Warning: tri_stripper::Strip(..) invalid number of triangle indices."<<std::endl;
return false;
}
// clear possible garbage
m_PrimitivesVector.clear();
@ -58,7 +61,10 @@ void tri_stripper::Strip(primitives_vector * out_pPrimitivesVector)
InitCache();
// Launch the triangle strip generator
Stripify();
if (!Stripify())
{
return false;
}
// Add the triangles that couldn't be stripped
AddLeftTriangles();
@ -69,6 +75,8 @@ void tri_stripper::Strip(primitives_vector * out_pPrimitivesVector)
// Put the results into the user's vector
std::swap(m_PrimitivesVector, (* out_pPrimitivesVector));
// m_PrimitivesVector.swap(* out_pPrimitivesVector);
return true;
}
@ -157,7 +165,7 @@ void tri_stripper::InitCache()
void tri_stripper::Stripify()
bool tri_stripper::Stripify()
{
// Reset the triangle strip id selector
m_StripID = 0;
@ -182,7 +190,9 @@ void tri_stripper::Stripify()
// Build it if it's long enough, otherwise discard it
// Note: BuildStrip refills m_NextCandidates
if (TriStrip.Size() >= m_MinStripSize)
BuildStrip(TriStrip);
{
if (!BuildStrip(TriStrip)) return false;
}
}
// We must discard the triangle we inserted in the candidate list from the heap
@ -195,6 +205,8 @@ void tri_stripper::Stripify()
while ((! m_TriHeap.empty()) && (m_TriHeap.top().Degree() == 0))
m_TriHeap.pop();
}
return true;
}
@ -376,7 +388,7 @@ triangle_edge tri_stripper::GetLatestEdge(const triangle & Triangle, const trian
void tri_stripper::BuildStrip(const triangle_strip TriStrip)
bool tri_stripper::BuildStrip(const triangle_strip TriStrip)
{
typedef triangles_graph::const_out_arc_iterator const_tri_link_iter;
typedef triangles_graph::node_iterator tri_node_iter;
@ -439,7 +451,11 @@ void tri_stripper::BuildStrip(const triangle_strip TriStrip)
// Debug check: we must have found the next triangle
//assert(LinkIt != TriNodeIt->out_end());
if (LinkIt == TriNodeIt->out_end()) throw "tri_stripper::BuildStrip(,) error, next triangle not found";
if (LinkIt == TriNodeIt->out_end())
{
osg::notify(osg::NOTICE)<<"Warning: tri_stripper::BuildStrip(,) error, next triangle not found."<<std::endl;
return false;
}
// Go to the next triangle
@ -449,6 +465,7 @@ void tri_stripper::BuildStrip(const triangle_strip TriStrip)
// Setup for the next triangle
ClockWise = ! ClockWise;
}
return true;
}

View File

@ -2,7 +2,7 @@
//
//////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2002 Tanguy Fautré.
// Copyright (C) 2002 Tanguy Fautr<EFBFBD>.
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
@ -21,7 +21,7 @@
// misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
//
// Tanguy Fautré
// Tanguy Fautr<EFBFBD>
// softdev@pandora.be
//
//////////////////////////////////////////////////////////////////////
@ -203,9 +203,6 @@ public:
typedef std::vector<primitives> primitives_vector;
struct triangles_indices_error { };
// constructor/initializer
inline tri_stripper(const indices & TriIndices);
@ -214,7 +211,7 @@ public:
inline void SetMinStripSize(const size_t MinStripSize = 2);
// Stripper
void Strip(primitives_vector * out_pPrimitivesVector); // throw triangles_indices_error();
bool Strip(primitives_vector * out_pPrimitivesVector);
private:
@ -232,7 +229,7 @@ private:
void InitCache();
void InitTriGraph();
void InitTriHeap();
void Stripify();
bool Stripify();
void AddLeftTriangles();
void LinkNeighboursTri(const triangle_edges & TriInterface, const triangle_edge Edge);
@ -242,7 +239,7 @@ private:
triangle_strip FindBestStrip();
triangle_strip ExtendTriToStrip(const size_t StartTriPos, const triangle_strip::start_order StartOrder);
void BuildStrip(const triangle_strip TriStrip);
bool BuildStrip(const triangle_strip TriStrip);
void AddIndice(const indice i);
void AddIndiceToCache(const indice i, bool CacheHitCount = false);
void AddTriToCache(const triangle & Tri, const triangle_strip::start_order Order);