- Timestamp:
- 03/21/12 18:36:20 (14 months ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
OpenSceneGraph/trunk/src/osgPlugins/ogr/ReaderWriterOGR.cpp
r12697 r13041 1 /* -*- mode: c++; c-default-style: k&r; tab-width: 4; c-basic-offset: 4; -*- 1 /* -*- mode: c++; c-default-style: k&r; tab-width: 4; c-basic-offset: 4; -*- 2 2 * Copyright (C) 2007 Cedric Pinson - mornifle@plopbyte.net 3 3 * 4 * This library is open source and may be redistributed and/or modified under 5 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or 4 * This library is open source and may be redistributed and/or modified under 5 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or 6 6 * (at your option) any later version. The full license is in LICENSE file 7 7 * included with this distribution, and on the openscenegraph.org website. 8 * 8 * 9 9 * This library is distributed in the hope that it will be useful, 10 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 12 * OpenSceneGraph Public License for more details. 13 13 */ … … 37 37 #include <ogrsf_frmts.h> 38 38 39 #define SERIALIZER() OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(_serializerMutex) 40 41 void CPL_STDCALL CPLOSGErrorHandler( CPLErr eErrClass, int nError, 39 #define SERIALIZER() OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(_serializerMutex) 40 41 void CPL_STDCALL CPLOSGErrorHandler( CPLErr eErrClass, int nError, 42 42 const char * pszErrorMsg ) 43 43 { … … 67 67 } 68 68 69 struct TriangulizeFunctor 69 struct TriangulizeFunctor 70 70 { 71 71 osg::Vec3Array* _vertexes; … … 81 81 static osg::Vec3Array* triangulizeGeometry(osg::Geometry* src) 82 82 { 83 if (src->getNumPrimitiveSets() == 1 && 83 if (src->getNumPrimitiveSets() == 1 && 84 84 src->getPrimitiveSet(0)->getType() == osg::PrimitiveSet::DrawArraysPrimitiveType && 85 85 src->getVertexArray() && … … 109 109 virtual ~ReaderWriterOGR() 110 110 { 111 CPLSetErrorHandler(oldHandler); 112 } 113 111 CPLSetErrorHandler(oldHandler); 112 } 113 114 114 virtual const char* className() const { return "OGR file reader"; } 115 115 … … 117 117 { 118 118 OSG_INFO<<"OGR::readNode("<<file<<")"<<std::endl; 119 119 120 120 if (file.empty()) return ReadResult::FILE_NOT_FOUND; 121 121 122 122 if (osgDB::equalCaseInsensitive(osgDB::getFileExtension(file),"ogr")) 123 123 { … … 145 145 bool useRandomColorByFeature = false; 146 146 bool addGroupPerFeature = false; 147 if (options) 147 if (options) 148 148 { 149 149 if (options->getOptionString().find("UseRandomColorByFeature") != std::string::npos) … … 157 157 osg::Group* group = new osg::Group; 158 158 159 for (int i = 0; i < file->GetLayerCount(); i++) 159 for (int i = 0; i < file->GetLayerCount(); i++) 160 160 { 161 161 osg::Group* node = readLayer(file->GetLayer(i), file->GetName(), useRandomColorByFeature, addGroupPerFeature); … … 177 177 178 178 OGRFeature* ogrFeature = NULL; 179 while ((ogrFeature = ogrLayer->GetNextFeature()) != NULL) 179 while ((ogrFeature = ogrLayer->GetNextFeature()) != NULL) 180 180 { 181 181 osg::Geode* feature = readFeature(ogrFeature, useRandomColorByFeature); … … 228 228 osg::Vec3Array* vertices = new osg::Vec3Array(); 229 229 OGRPoint point; 230 for(int j = 0; j < lineString->getNumPoints(); j++) 230 for(int j = 0; j < lineString->getNumPoints(); j++) 231 231 { 232 232 lineString->getPoint(j, &point); … … 245 245 osg::Geometry* pointGeom = new osg::Geometry(); 246 246 osg::Vec3Array* vertices = new osg::Vec3Array(); 247 247 248 248 vertices->reserve(mpoint->getNumGeometries()); 249 for (int i = 0; i < mpoint->getNumGeometries(); i++ ) 249 for (int i = 0; i < mpoint->getNumGeometries(); i++ ) 250 250 { 251 251 OGRGeometry* ogrGeom = mpoint->getGeometryRef(i); … … 256 256 257 257 OGRPoint* points = static_cast<OGRPoint*>(ogrGeom); 258 258 259 259 vertices->push_back(osg::Vec3(points->getX(), points->getY(), points->getZ())); 260 260 } 261 261 262 262 pointGeom->setVertexArray(vertices); 263 263 pointGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS, 0, vertices->size())); 264 264 265 265 if (pointGeom->getVertexArray()) 266 266 { … … 275 275 osg::Geometry* geom = new osg::Geometry; 276 276 277 for (int i = 0; i < mpolygon->getNumGeometries(); i++ ) 277 for (int i = 0; i < mpolygon->getNumGeometries(); i++ ) 278 278 { 279 279 OGRGeometry* ogrGeom = mpolygon->getGeometryRef(i); … … 286 286 osg::ref_ptr<osg::Drawable> drw = polygonToDrawable(polygon); 287 287 osg::ref_ptr<osg::Geometry> geometry = drw->asGeometry(); 288 if (geometry.valid() && geometry->getVertexArray() && 289 geometry->getVertexArray()->getNumElements() && 288 if (geometry.valid() && geometry->getVertexArray() && 289 geometry->getVertexArray()->getNumElements() && 290 290 geometry->getNumPrimitiveSets() && 291 geometry->getVertexArray()->getType() == osg::Array::Vec3ArrayType ) 292 { 293 294 if (!geom->getVertexArray()) 291 geometry->getVertexArray()->getType() == osg::Array::Vec3ArrayType ) 292 { 293 294 if (!geom->getVertexArray()) 295 295 { // no yet data we put the first in 296 296 geom->setVertexArray(geometry->getVertexArray()); 297 297 geom->setPrimitiveSetList(geometry->getPrimitiveSetList()); 298 298 299 } 300 else 299 } 300 else 301 301 { // already a polygon then append 302 302 int size = geom->getVertexArray()->getNumElements(); 303 303 osg::Vec3Array* arrayDst = static_cast<osg::Vec3Array*>(geom->getVertexArray()); 304 304 osg::ref_ptr<osg::Vec3Array> triangulized = triangulizeGeometry(geometry.get()); 305 if (triangulized.valid()) 305 if (triangulized.valid()) 306 306 { 307 307 arrayDst->insert(arrayDst->end(), triangulized->begin(), triangulized->end()); 308 // shift index 308 // shift index 309 309 geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES, size, triangulized->size())); 310 310 } 311 311 } 312 } 312 } 313 313 else 314 314 { … … 341 341 } 342 342 343 if (polygon->getNumInteriorRings()) 343 if (polygon->getNumInteriorRings()) 344 344 { 345 345 for (int i = 0; i < polygon->getNumInteriorRings(); i++) … … 347 347 OGRLinearRing *ring = polygon->getInteriorRing(i); 348 348 OGRPoint point; 349 for (int j = 0; j < ring->getNumPoints(); j++) 349 for (int j = 0; j < ring->getNumPoints(); j++) 350 350 { 351 351 ring->getPoint(j, &point); … … 364 364 geom->removePrimitiveSet(0,geom->getNumPrimitiveSets()); 365 365 geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES, 0, array->size())); 366 366 367 367 return geom; 368 368 } … … 371 371 osg::Geometry* geom = new osg::Geometry; 372 372 373 for (int i = 0; i < mlineString->getNumGeometries(); i++ ) 373 for (int i = 0; i < mlineString->getNumGeometries(); i++ ) 374 374 { 375 375 OGRGeometry* ogrGeom = mlineString->getGeometryRef(i); … … 381 381 OGRLineString* lineString = static_cast<OGRLineString*>(ogrGeom); 382 382 osg::ref_ptr<osg::Geometry> geometry = lineStringToDrawable(lineString); 383 if (geometry.valid() && 384 geometry->getVertexArray() && 383 if (geometry.valid() && 384 geometry->getVertexArray() && 385 385 geometry->getNumPrimitiveSets() && 386 geometry->getVertexArray()->getType() == osg::Array::Vec3ArrayType) 386 geometry->getVertexArray()->getType() == osg::Array::Vec3ArrayType) 387 387 { 388 388 … … 392 392 geom->setPrimitiveSetList(geometry->getPrimitiveSetList()); 393 393 394 } 395 else 394 } 395 else 396 396 { 397 397 int size = geom->getVertexArray()->getNumElements(); … … 400 400 osg::Vec3Array* arrayDst = static_cast<osg::Vec3Array*>(geom->getVertexArray()); 401 401 arrayDst->insert(arrayDst->end(), arraySrc->begin(), arraySrc->end()); 402 // shift index 402 // shift index 403 403 geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP, size, arraySrc->size())); 404 404 } … … 413 413 if (!ogrFeature || !ogrFeature->GetGeometryRef()) 414 414 return 0; 415 415 416 416 osg::Geometry* drawable = 0; 417 417 bool disableCulling = false; … … 424 424 drawable = pointsToDrawable(static_cast<OGRPoint *>(ogrFeature->GetGeometryRef())); 425 425 disableCulling = true; 426 break; 426 break; 427 427 428 428 case wkbLinearRing:
