osg::Geometry* multiPolygonToDrawable(OGRMultiPolygon* mpolygon) const { osg::Geometry* geom = new osg::Geometry; for (int i = 0; i < mpolygon->getNumGeometries(); i++ ) { OGRGeometry* ogrGeom = mpolygon->getGeometryRef(i); OGRwkbGeometryType ogrGeomType = ogrGeom->getGeometryType(); if (wkbPolygon != ogrGeomType && wkbPolygon25D != ogrGeomType) continue; // skip OGRPolygon* polygon = static_cast<OGRPolygon*>(ogrGeom); osg::ref_ptr<osg::Drawable> drw = polygonToDrawable(polygon); osg::ref_ptr<osg::Geometry> geometry = drw->asGeometry(); if (geometry.valid() && geometry->getVertexArray() && geometry->getVertexArray()->getNumElements() && geometry->getNumPrimitiveSets() && geometry->getVertexArray()->getType() == osg::Array::Vec3ArrayType ) { if (!geom->getVertexArray()) { // no yet data we put the first in geom->setVertexArray(geometry->getVertexArray()); geom->setPrimitiveSetList(geometry->getPrimitiveSetList()); } else { // already a polygon then append int size = geom->getVertexArray()->getNumElements(); osg::Vec3Array* arrayDst = static_cast<osg::Vec3Array*>(geom->getVertexArray()); osg::ref_ptr<osg::Vec3Array> triangulized = triangulizeGeometry(geometry.get()); if (triangulized.valid()) { arrayDst->insert(arrayDst->end(), triangulized->begin(), triangulized->end()); // shift index geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES, size, triangulized->size())); } } } else { OSG_WARN << "Warning something wrong with a polygon in a multi polygon" << std::endl; } } if (geom->getVertexArray()) { OSG_INFO << "osgOgrFeature::multiPolygonToDrawable " << geom->getVertexArray()->getNumElements() << " vertexes"<< std::endl; } return geom; }
osg::Geometry* polygonToDrawable(OGRPolygon* polygon) const { osg::Geometry* geom = new osg::Geometry(); osg::Vec3Array* vertices = new osg::Vec3Array(); geom->setVertexArray(vertices); { OGRLinearRing *ring = polygon->getExteriorRing(); OGRPoint point; for(int i = 0; i < ring->getNumPoints(); i++) { ring->getPoint(i, &point); vertices->push_back(osg::Vec3(point.getX(), point.getY(), point.getZ())); } geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP, 0, vertices->size())); } if (polygon->getNumInteriorRings()) { for (int i = 0; i < polygon->getNumInteriorRings(); i++) { OGRLinearRing *ring = polygon->getInteriorRing(i); OGRPoint point; for (int j = 0; j < ring->getNumPoints(); j++) { ring->getPoint(j, &point); vertices->push_back(osg::Vec3(point.getX(), point.getY(), point.getZ())); } geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP, vertices->size()-ring->getNumPoints() , ring->getNumPoints())); } } osgUtil::Tessellator tsl; tsl.setTessellationType(osgUtil::Tessellator::TESS_TYPE_GEOMETRY); tsl.setBoundaryOnly(false); tsl.retessellatePolygons(*geom); osg::Vec3Array* array = triangulizeGeometry(geom); geom->setVertexArray(array); geom->removePrimitiveSet(0,geom->getNumPrimitiveSets()); geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES, 0, array->size())); return geom; }
PolygonView::PolygonView(MapPolygon* mapPolygon,bool polygonFill,osg::Vec4 color) { this->mapPolygon=mapPolygon; this->geometryEdgePolygon=new osg::Geometry; /*this->geometryPolygon=new osg::Geometry;*/ this->vertices=new osg::Vec3Array; /*this->geometryPolygon->setVertexArray(this->vertices);*/ this->geometryEdgePolygon->setVertexArray(this->vertices); OGRLinearRing *poExteriorRing=mapPolygon->getExteriorRing(); normals=new osg::Vec3Array; normals->push_back(osg::Vec3(0.0f,-1.0f,0.0f)); this->polygonFillColor=new osg::Vec4Array; this->polygonEdgeColor=new osg::Vec4Array; this->polygonFillColor->push_back(color); this->polygonEdgeColor->push_back(edgeColor); { OGRPoint point; for(int i = 0; i < poExteriorRing->getNumPoints(); i++) { poExteriorRing->getPoint(i, &point); vertices->push_back(osg::Vec3(point.getX(), 0, point.getY())); } this->geometryEdgePolygon->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP, 0, vertices->size())); /*this->geometryPolygon->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP, 0, vertices->size()));*/ } if (mapPolygon->getNumInteriorRings()) { for (int i = 0; i < mapPolygon->getNumInteriorRings(); i++) { OGRLinearRing *ring = mapPolygon->getInteriorRing(i); OGRPoint point; for (int j = 0; j < ring->getNumPoints(); j++) { ring->getPoint(j, &point); vertices->push_back(osg::Vec3(point.getX(), 0, point.getY())); } this->geometryEdgePolygon->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP, vertices->size()-ring->getNumPoints() , ring->getNumPoints())); /*this->geometryPolygon->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP, vertices->size()-ring->getNumPoints() , ring->getNumPoints()));*/ } } this->geometryEdgePolygon->setNormalArray(normals); this->geometryEdgePolygon->setNormalBinding(osg::Geometry::BIND_OVERALL); this->geometryEdgePolygon->setColorArray(this->polygonEdgeColor); this->geometryEdgePolygon->setColorBinding(osg::Geometry::BIND_OVERALL); if (polygonFill==true) { this->geometryPolygon=new osg::Geometry; this->geometryPolygon->setVertexArray(this->vertices); for(int i=0;i<this->geometryEdgePolygon->getNumPrimitiveSets();i++) { this->geometryPolygon->addPrimitiveSet(this->geometryEdgePolygon->getPrimitiveSet(i)); } osgUtil::Tessellator tsl; tsl.setTessellationType(osgUtil::Tessellator::TESS_TYPE_GEOMETRY); tsl.setBoundaryOnly(false); tsl.retessellatePolygons(*this->geometryPolygon); osg::Vec3Array* array = triangulizeGeometry(this->geometryPolygon); this->geometryPolygon->setVertexArray(array); this->geometryPolygon->removePrimitiveSet(0,this->geometryPolygon->getNumPrimitiveSets()); this->geometryPolygon->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES, 0, array->size())); this->geometryPolygon->setNormalArray(normals); this->geometryPolygon->setNormalBinding(osg::Geometry::BIND_OVERALL); this->geometryPolygon->setColorArray(this->polygonFillColor); this->geometryPolygon->setColorBinding(osg::Geometry::BIND_OVERALL); } }
MultiPolygonView::MultiPolygonView(MapMultiPolygon* mapMultiPolygon,bool polygonFill,osg::Vec4 color) { this->geometryMultiPolygons=new osg::Geometry; this->geometryEdgeMultiPolygons=new osg::Geometry; this->vertices=new osg::Vec3Array; osg::ref_ptr<osg::Vec3Array> verticesCopy=new osg::Vec3Array; this->geometryEdgeMultiPolygons->setVertexArray(this->vertices); this->geometryMultiPolygons->setVertexArray(verticesCopy); normals=new osg::Vec3Array; normals->push_back(osg::Vec3(0.0f,-1.0f,0.0f)); this->polygonFillColor=new osg::Vec4Array; this->polygonEdgeColor=new osg::Vec4Array; this->polygonFillColor->push_back(color); this->polygonEdgeColor->push_back(edgeColor); for (int i=0;i<mapMultiPolygon->getNumGeometries();i++) { OGRGeometry* ogrGeom=mapMultiPolygon->getGeometryRef(i); OGRwkbGeometryType ogrGeomType=ogrGeom->getGeometryType(); if (wkbPolygon != ogrGeomType && wkbPolygon25D != ogrGeomType) continue; // skip MapPolygon* mapPolygon=static_cast< MapPolygon*>(ogrGeom); OGRLinearRing *poExteriorRing=mapPolygon->getExteriorRing(); OGRPoint point; for(int i = 0; i < poExteriorRing->getNumPoints(); i++) { poExteriorRing->getPoint(i, &point); vertices->push_back(osg::Vec3(point.getX(), 0, point.getY())); verticesCopy->push_back(osg::Vec3(point.getX(), 0, point.getY())); } this->geometryEdgeMultiPolygons->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP, vertices->size()-poExteriorRing->getNumPoints(), poExteriorRing->getNumPoints())); this->geometryMultiPolygons->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP, verticesCopy->size()-poExteriorRing->getNumPoints(), poExteriorRing->getNumPoints())); if (mapPolygon->getNumInteriorRings()) { for (int i = 0; i < mapPolygon->getNumInteriorRings(); i++) { OGRLinearRing *ring = mapPolygon->getInteriorRing(i); OGRPoint point; for (int j = 0; j < ring->getNumPoints(); j++) { ring->getPoint(j, &point); vertices->push_back(osg::Vec3(point.getX(), 0, point.getY())); verticesCopy->push_back(osg::Vec3(point.getX(), 0, point.getY())); } this->geometryEdgeMultiPolygons->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP, vertices->size()-ring->getNumPoints() , ring->getNumPoints())); this->geometryMultiPolygons->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_LOOP, verticesCopy->size()-ring->getNumPoints() , ring->getNumPoints())); } } } this->geometryEdgeMultiPolygons->setNormalArray(normals); this->geometryEdgeMultiPolygons->setNormalBinding(osg::Geometry::BIND_OVERALL); this->geometryEdgeMultiPolygons->setColorArray(this->polygonEdgeColor); this->geometryEdgeMultiPolygons->setColorBinding(osg::Geometry::BIND_OVERALL); /*if(polygonFill==true) {*/ osgUtil::Tessellator tsl; tsl.setTessellationType(osgUtil::Tessellator::TESS_TYPE_GEOMETRY); tsl.setBoundaryOnly(false); tsl.retessellatePolygons(*this->geometryMultiPolygons); osg::Vec3Array* array = triangulizeGeometry(this->geometryMultiPolygons); this->geometryMultiPolygons->setVertexArray(array); this->geometryMultiPolygons->removePrimitiveSet(0,this->geometryMultiPolygons->getNumPrimitiveSets()); this->geometryMultiPolygons->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES, 0, array->size())); this->geometryMultiPolygons->setNormalArray(normals); this->geometryMultiPolygons->setNormalBinding(osg::Geometry::BIND_OVERALL); this->geometryMultiPolygons->setColorArray(this->polygonFillColor); this->geometryMultiPolygons->setColorBinding(osg::Geometry::BIND_OVERALL); //} }