ReadResult result;
osg::ref_ptr<osgDB::ReaderWriter> reader = osgDB::Registry::instance()->getReaderWriterForExtension("tif");
std::string name("D:\\gd.tif");
osgDB::ReaderWriter::Options* opt= NULL;
osgDB::ReaderWriter::ReadResult rr = reader->readImage(name, opt); if (rr.validImage())
{
result = ReadResult(rr.takeImage());
result.getImage()->setName("nameNoUse.tif");
} if (result.succeeded())
{
result.getObject();
result.metadata();
osg::ref_ptr<osg::Image> image = result.getImage(); osgEarth::ImageToHeightFieldConverter conv;
osg::HeightField* hf = conv.convert(image.get()); for (unsigned col = ; col < hf->getNumColumns(); ++col)
{
for (unsigned row = ; row < hf->getNumRows(); ++row)
{
float height = hf->getHeight(col, row);
if (height < 1.0)
{
float newh = cos(height*3.141593f);
//float rf = rand()% 500;
hf->setHeight(col, row, -* newh);
}
else// if(height > 1)
{
//height = 100;//下断点看看
}
}
} osg::Image* newimage = conv.convert(hf);
std::string nameofnew("D:\\gd2.tif");
reader->writeImage(*newimage, nameofnew); }

如题

加SB的“平滑”功能

float fBegin = 0.1;
//float fEnd = 0.000001;
float fLowestValue = 1000.0;
int fWide = 100.0; ReadResult result;
osg::ref_ptr<osgDB::ReaderWriter> reader = osgDB::Registry::instance()->getReaderWriterForExtension("tif");
std::string name("D:\\gd.tif");
osgDB::ReaderWriter::Options* opt= NULL;
osgDB::ReaderWriter::ReadResult rr = reader->readImage(name, opt); if (rr.validImage())
{
result = ReadResult(rr.takeImage());
result.getImage()->setName("guandao.tif");
} if (result.succeeded())
{
result.getObject();
result.metadata();
osg::ref_ptr<osg::Image> image = result.getImage(); osgEarth::ImageToHeightFieldConverter conv;
osg::HeightField* hf = conv.convert(image.get()); int *fFlag = new int[hf->getNumColumns()*hf->getNumRows()]; for (unsigned col = ; col < hf->getNumColumns(); ++col)
{
for (unsigned row = ; row < hf->getNumRows(); ++row)
{
fFlag[col*hf->getNumRows() + row] = ;
float height = hf->getHeight(col, row);
if (height < fBegin)
{
fFlag[col*hf->getNumRows() + row] = ;
hf->setHeight(col, row, -fLowestValue);
/*
float newh = -1000.0;
if(height > 0.00001)
newh = 0.1 - (0.1 - height)/ (0.1-0.00001)*1000.0;
hf->setHeight(col, row, newh);*/
}
}
} for (int i = ; i < hf->getNumColumns()*hf->getNumRows(); i++)
{
if (fFlag[i] == )//如果这值在海面以下
{
bool isNearSide = false;
int nowX = i/hf->getNumRows();
int nowY = i%hf->getNumRows();
for (int j = ; j <= fWide; j++)
{
//从离此值最近的值开始找附近的岸边,往外延伸
//向东南西北四个方向找,没层都遍历一圈
for ( int x = ;x <= j;x++ )
{
//如果找到有岸边
int fDifValueX = x;
int fDifValueY = j - x;
int realX = nowX - fDifValueX;
if (realX > )
{
int realY = nowY - fDifValueY;
if (realY > )
{
if (fFlag[realX*hf->getNumRows() + realY] == )//如果是岸边
isNearSide = true;
}
realY = nowY + fDifValueY;
if (realY < hf->getNumRows())
{
if (fFlag[realX*hf->getNumRows() + realY] == )//如果是岸边
isNearSide = true;
}
} realX = nowX + fDifValueX;
if (realX < hf->getNumColumns())
{
int realY = nowY - fDifValueY;
if (realY > )
{
if (fFlag[realX*hf->getNumRows() + realY] == )//如果是岸边
isNearSide = true;
}
realY = nowY + fDifValueY;
if (realY < hf->getNumRows())
{
if (fFlag[realX*hf->getNumRows() + realY] == )//如果是岸边
isNearSide = true;
}
}
} //查找这个范围内是否有值,如果有值则用此值
if (isNearSide)
{
float fRealHeight = fBegin - j * fLowestValue / fWide;
hf->setHeight((i / hf->getNumRows()), (i % hf->getNumRows()), fRealHeight);
break;//退出当前寻找的延伸
}
}
}
} osg::Image* newimage = conv.convert(hf);
std::string nameofnew("D:\\gd2.tif");
reader->writeImage(*newimage, nameofnew);
delete[]fFlag;
}
04-04 11:28