根据OBJ文件以及3D点坐标查找最近的序号

记录一下代码

#include <string>
#include<iostream>
#include<array>
#include<vector>
#include<fstream>
#include<sstream>
void readTXTFile(std::string filename, std::vector<std::array<float, 3> >&pts) {
	pts.clear();
	std::fstream pointCouldStream;
	pointCouldStream.open(filename.c_str(), std::ifstream::in);
	if (!pointCouldStream.is_open())
	{
		std::cout << "Error opening pointCould file\n"; exit(1);
	}
	while (!pointCouldStream.eof())
	{
		char tmpLine[400] = { 0 };
		pointCouldStream.getline(tmpLine, 200);
		std::istringstream  istr;
		istr.str(tmpLine);
		float XCoord, YCoord, ZCoord, garbageVal;
	    istr >> XCoord >> YCoord >> ZCoord;
		std::cout << XCoord << " " << YCoord << " " << ZCoord << "\n";
		pts.push_back(std::array<float, 3>{ {XCoord, YCoord, ZCoord}});

	}
}

void calOBJIndex(std::vector<std::array<float, 3> >&objPts, std::vector<std::array<float, 3> >&pts1, std::vector<int >&inds) {
	inds.clear();
	for (int i = 0; i < pts1.size(); i++) {
		double lastDis = INT_MAX;
		double curDis;
		int curIdx;
	
		for (int j = 0; j < objPts.size(); j++) {
			curDis = (objPts[j][0] - pts1[i][0])*(objPts[j][0] - pts1[i][0]) +
				     (objPts[j][1] - pts1[i][1])*(objPts[j][1] - pts1[i][1]) +
				     (objPts[j][2] - pts1[i][2])*(objPts[j][2] - pts1[i][2]);
			if (curDis < lastDis) {
				lastDis = curDis;
				curIdx = j;
			}
		}
		inds.push_back(curIdx);

	}

}
void writeIndx(std::vector<int >&inds, std::vector<int >&inds2) {
	std::fstream fileHandle;
	fileHandle.open("lm.txt", std::ios::out);
	for (int i = 0; i < inds.size(); i++) {
		fileHandle << inds[i] << " " << inds2[i] << std::endl;
	}
	fileHandle.close();
}
void readObjFile(std::string filename, std::vector<std::array<float, 3> >&pts, std::vector<std::array<int, 3> >&facets) {
	pts.clear();
	facets.clear();
	std::fstream pointCouldStream;
	pointCouldStream.open(filename.c_str(), std::ifstream::in);
	if (!pointCouldStream.is_open())
	{
		std::cout << "Error opening pointCould file\n"; exit(1);
	}
	while (!pointCouldStream.eof())
	{
		char tmpLine[400] = { 0 };
		pointCouldStream.getline(tmpLine, 200);
		std::istringstream  istr;
		istr.str(tmpLine);
		std::string lineType;
		istr >> lineType;

		float XCoord, YCoord, ZCoord, garbageVal;
		int  p1, p2, p3;
		if (lineType.compare("v") == 0) {
			istr >> XCoord >> YCoord >> ZCoord;
			pts.push_back(std::array<float, 3>{ {XCoord, YCoord, ZCoord}});
		}
		else if (lineType.compare("f") == 0) {//只考虑单存顶点的情况
			istr >> p1 >> p2 >> p3;
			facets.push_back(std::array<int, 3>{ {p1 - 1, p2 - 1, p3 - 1}});
		}
	}
	pointCouldStream.close();
	std::cout << "\nload data From obj file done!\n\n";

}
void tst(std::vector<std::array<float, 3> >&spts) {
	for (int i = 0; i < 12; i++) {
		std::cout << spts[i][0] << " " << spts[i][1] << " " << spts[i][2] << " \n";
	}
}


void main() {
	std::vector<std::array<float, 3> >spts; std::vector<std::array<int, 3> >sfacets;
	std::vector<std::array<float, 3> >tpts; std::vector<std::array<int, 3> >tfacets;
		readObjFile("D:\\x64\\x64\\source.obj", spts, sfacets);
		readObjFile("D:\\x64\\x64\\target.obj", tpts, tfacets);
		tst(spts);
		std::vector<std::array<float, 3> >slms;
		std::vector<std::array<float, 3> >tlms;
		readTXTFile("D:\\x64\\x64\\source.txt", slms);
		readTXTFile("D:\\x64\\x64\\target.txt", tlms);
		std::vector<int >inds1;
	    std::vector<int >inds2;
		calOBJIndex(spts, slms, inds1);
		calOBJIndex(tpts, tlms, inds2);
		writeIndx(inds1, inds2);

		while (std::cin.get());


}
  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值