由于代码不能冗余,又怕找不到,备份到这里
在这里插#include <osg/LineWidth>
#include <osg/Point>
osg::Geode *PCL_MainWindow::TransformOsgData_1(PointCloud_rgb cloud1)
{
size_t cloudnum = cloud1->size();
osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry();
osg::ref_ptr<osg::Vec3Array> coord = new osg::Vec3Array(); /**创建顶点容器**/
osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array();/**创建颜色容器**/
//----------------将距离值存入容器中,判定容器中的最小值 和 最大值,作为rgb 的基础色值-------
for (size_t v = 0; v < cloudnum; v++)
{
if (max < cloud1->points[v].intensity)
max = cloud1->points[v].intensity;
if (min > cloud1->points[v].intensity)
min = cloud1->points[v].intensity;
}
median = (max-min)/2; //中间值,绿色
//--------------------------------------------------------------------------------
/**将点云数据存至 osg 容器中**/
for (size_t i = 0; i < cloudnum; i++)
{
coord->push_back(osg::Vec3(cloud1->points[i].x, cloud1->points[i].y, cloud1->points[i].z));
colors->push_back(computeColor(int(cloud1->points[i].intensity))); //写个内联判定范围着色
}
geometry->setVertexArray(coord.get()); /**设置坐标顶点**/
geometry->setColorArray(colors.get()); /**设置绑定颜色**/
geometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX); /**绑定顶点颜色**/
geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS, 0, int(cloudnum)));/**设置顶点的关联方式**/
osg::ref_ptr<osg::Point> point = new osg::Point;
point->setSize(2);
// 添加颜色关键点,必须设置 geode 这个叶子节点属性
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
geode->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
geode->getOrCreateStateSet()->setAttribute(point, osg::StateAttribute::ON);
geode->addDrawable(geometry);
// PCL_THROW_EXCEPTION() 抛出异常的宏
return geode.release();
}
/*!
* \brief computeColor 用点云库随机生成颜色
* \param intensity 如果需要指定颜色,调用 OSG_PCLVisualizer类中的 pclDataTransformOsgData
* \return
*/
inline osg::Vec4 computeColor(const int intensity)
{
osg::Vec4 color;
double r, g, b;
pcl::visualization::getRandomColors (r, g, b);
color = osg::Vec4(float(r), float(g), float(b), 1);
return color;
}
/*!
* \brief computeColor_2 根据距离分配颜色
* \param intensity 距离值
* \return
*/
inline osg::Vec4 computeColor_2(const float intensity)
{
osg::Vec4 color;
float r=1.0f;
float g=1.0f;
float b=1.0f;
float ddd = max/4;
float dd = max/2;
float d = dd+ddd;
if(int(intensity) == 0)
{
color = osg::Vec4(r, 0.0f, 0.0f, 1.0f);
}
if(intensity > 0 && intensity < ddd )
{
g = intensity/ddd;
color = osg::Vec4(1, g, 0.0f, 1.0f);
}
if(intensity > ddd && intensity < dd )
{
g = intensity/dd;
color = osg::Vec4(0.0f, g, 1.0f, 1.0f);
}
if(intensity > dd && intensity < d )
{
b = intensity/d;
color = osg::Vec4(0.0f, 1.0f, b, 1.0f);
}
else if(intensity > d && intensity < max)
{
b = intensity/max;
color = osg::Vec4(0.0f, 0.0f, b, 1.0f);
};
return color;
}
入代码片