cloudCompare插件开发——保存点云颜色

这篇博客介绍了如何使用插件从三维点云软件中导出用户选定的点云实体信息。插件首先获取选中的点云对象,然后让用户选择保存路径,最后根据点云的着色方式(RGB颜色或标量域)将点云坐标和颜色信息写入TXT文件。点云坐标在输出前会通过toGlobal3d()方法转换为原始坐标。博客提供了两种不同情况的代码实现,并给出了插件的下载链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一. 插件介绍

该插件用于保存用户选中的点云实体,导出为txt文件,仅记录点云的坐标以及颜色。

二. 主要代码

1. 获取点云对象
//获取选中的点云对象
	ccHObject::Container container = m_app->getSelectedEntities();
	ccHObject *entity= container[0];
	if (!entity || !entity->isA(CC_TYPES::POINT_CLOUD)) {
		m_app->dispToConsole("Run error,please select a point cloud!");
		return;
	}
	ccPointCloud *pc = ccHObjectCaster::ToPointCloud(entity);
	ccScalarField *sf = pc->getCurrentDisplayedScalarField();
2. 获取保存路径
//用户选择保存的文件夹
	QString dirPath = QFileDialog::getExistingDirectory(nullptr,"please select a saving path");
	if (dirPath.isEmpty()) {
		m_app->dispToConsole("The user did not select a folder.");
		return;
	}
3. 获取点云信息并输出

要注意点云的着色方式有两种:一是根据自身的rgb值进行渲染;二是根据标量域来渲染。此外点云对象中点的坐标是经过偏移处理的,因此输出时要进行坐标转换,只需调用点云对象的toGlobal3d()方法即可。

//判断当前点云对象采用的是color还是scalefields来呈现颜色的
	if (pc->hasColors() && pc->colorsShown()) {	//rgb
		//创建文件以及IO流
		QString filename = pc->getName() + "_color";
		QFile file(dirAbPath + "\\" + filename + ".txt");
		//文件不存在则创建文件
		if (!file.exists()) {
			file.open(QIODevice::ReadWrite | QIODevice::Text);
			file.close();
		}
		//打开文件,创建输出流
		file.open(QIODevice::Text | QIODevice::Truncate | QIODevice::WriteOnly);
		QTextStream out(&file);
		
		//获取并输出点云信息(x,y,z,sf,r,g,b)
		ccColor::Rgba color;
		CCVector3d point;
		for (int i = 0; i<pc->size(); i++) {
			color = pc->getPointColor(i);
			//将cc中偏移后的坐标转为转原始坐标
			point = pc->toGlobal3d(*pc->getPoint(i));	//cc源码中用的是 *pc->getPointPersistentPtr(i);  但是这两个方法内部调用的接口一样
			out << QString("%1 %2 %3 %4 %5 %6 %7 %8")
				.arg(point.x, 0, 'r', 8).arg(point.y, 0, 'r', 8).arg(point.z, 0, 'r', 8)
				.arg(pc->getPointScalarValue(i), 0, 'r', 8).arg(color.r).arg(color.g).arg(color.b).arg(color.a) << endl;
		}
		file.close();
	}else if (pc->hasScalarFields() && pc->sfShown()) {	//scalar fields
		sqb.exec();
		QString filename = pc->getName() + "_" + QString(sf->getName()) + "_" + sf->getColorScale()->getUuid() + "_" + QString::number(sf->getColorRampSteps());
		QFile file(dirAbPath + "\\" + filename + ".txt");
		if (!file.exists()) {
			file.open(QIODevice::ReadWrite | QIODevice::Text);
			file.close();
		}
		file.open(QIODevice::Text | QIODevice::Truncate | QIODevice::WriteOnly);
		QTextStream out(&file);
		
		const ccColor::Rgb* color;
		CCVector3d point;
		for (int i = 0; i<pc->size(); i++) {
			color=pc->getPointScalarValueColor(i);
			point=pc->toGlobal3d(*pc->getPoint(i));	
			out << QString("%1 %2 %3 %4 %5 %6 %7")
				.arg(point.x,0,'r', precision).arg(point.y, 0, 'r', precision).arg(point.z, 0, 'r', precision)
				.arg(pc->getPointScalarValue(i), 0, 'r', precision).arg(color->r).arg(color->g).arg(color->b)<< endl;
		}
		file.close();
	}
	else {
		m_app->dispToConsole("Please set the color of the point");
		return;
	}

四. 插件下载

百度网盘链接:https://pan.baidu.com/s/1e3NrrFloSiXzQE4IVvwgeg
提取码:piwz

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

萘和

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值