On 26 June 2015 at 13:39, Tony Vasile wrote:
> Not sure if you came up with this but since I couldn't see your version of
> the patch I recreated it.
>
Oppps. Must have missed attaching the file...
You file looks roughly the same. In my change I cleaned up one of the
commented out lines in your code.
Robert.
/* -*- mode: c++; c-default-style: k&r; tab-width: 4; c-basic-offset: 4; -*-
* Copyright (C) 2007 Cedric Pinson - morni...@plopbyte.net
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define SERIALIZER() OpenThreads::ScopedLock lock(_serializerMutex)
void CPL_STDCALL CPLOSGErrorHandler( CPLErr eErrClass, int nError,
const char * pszErrorMsg )
{
if( eErrClass == CE_Debug )
{
OSG_DEBUG << pszErrorMsg << std::endl;
}
else if( eErrClass == CE_Warning )
{
OSG_WARN << nError << " " << pszErrorMsg << std::endl;
}
else
{
OSG_FATAL << nError << " " << pszErrorMsg << std::endl;
}
}
static osg::Material* createDefaultMaterial()
{
osg::Vec4 color;
for (int i = 0; i < 3; i++)
color[i] = (1.0 * (rand() / (1.0*RAND_MAX)));
color[3] = 1;
osg::Material* mat = new osg::Material;
mat->setDiffuse(osg::Material::FRONT_AND_BACK, color);
return mat;
}
struct TriangulizeFunctor
{
osg::Vec3Array* _vertexes;
// do nothing
void operator ()(const osg::Vec3& v1, const osg::Vec3& v2, const osg::Vec3& v3, bool treatVertexDataAsTemporary) {
_vertexes->push_back(v1);
_vertexes->push_back(v2);
_vertexes->push_back(v3);
}
};
static osg::Vec3Array* triangulizeGeometry(osg::Geometry* src)
{
if (src->getNumPrimitiveSets() == 1 &&
src->getPrimitiveSet(0)->getType() == osg::PrimitiveSet::DrawArraysPrimitiveType &&
src->getVertexArray() &&
src->getVertexArray()->getType() == osg::Array::Vec3ArrayType)
return static_cast(src->getVertexArray());
osg::TriangleFunctor functor;
osg::Vec3Array* array = new osg::Vec3Array;
functor._vertexes = array;
src->accept(functor);
return array;
}
class ReaderWriterOGR : public osgDB::ReaderWriter
{
public:
ReaderWriterOGR()
{
supportsExtension("ogr","OGR file reader");
supportsOption("useRandomColorByFeature", "Assign a random color to each feature.");
supportsOption("addGroupPerFeature", "Places each feature in a separate group.");
oldHandler = CPLSetErrorHandler(CPLOSGErrorHandler);
}
virtual ~ReaderWriterOGR()
{
CPLSetErrorHandler(oldHandler);
}
virtual const char* className() const { return "OGR file reader"; }
virtual ReadResult readNode(const std::string& file, const osgDB::ReaderWriter::Options* options) const
{
OSG_INFO<<"OGR::readNode("< lock(_serializerMutex);
return readFile(osgDB::getNameLessExtension(file), options);
}
OpenThreads::ScopedLock lock(_serializerMutex);
std::string fileName = osgDB::findDataFile( file, options );
if (fileName.empty()) return readFile(file, options); // ReadResult::FILE_NOT_FOUND;
return readFile(fileName, options);
}
virtual ReadResult readFile(const std::string& fileName, const osgDB::ReaderWriter::Options* options) const
{
#if GDAL_VERSION_MAJOR<2
if (OGRSFDriverRegistrar::GetRegistrar()->GetDriverCount() == 0)
OGRRegisterAll();
// Try to open data source
OGRDataSource* file = OGRSFDriverRegistrar::Open(fileName.c_str());
#else
if (GDALGetDriverCount() == 0)
GDALAllRegister();
// Try to open data source
GDALDataset* file = (GDALDataset*) GDALOpenEx( fileName.c_str(), GDAL_OF_VECTOR, NULL, NULL, NULL );
#endif
if (!file)
return 0;
bool useRandomColorByFeature = false;
bool addGroupPerFeature = false;
if (options)
{
if (options->getOptionString().find("UseRandomColorByFeature") != std::string::npos)
useRandomColorByFeature = true;
if (options->getOptionString().find("useRandomColorByFeature") != std::string::npos)
useRandomColorByFeature = true;
if (options->getOpti