Skip to content

Commit 9b275d5

Browse files
committed
Merge pull request #1393 from terfendail:surface_matching_ply
2 parents e80393b + a922dd5 commit 9b275d5

File tree

2 files changed

+60
-23
lines changed

2 files changed

+60
-23
lines changed

modules/surface_matching/include/opencv2/surface_matching/ppf_helpers.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ namespace ppf_match_3d
6161
* and whether it should be loaded or not
6262
* @return Returns the matrix on successfull load
6363
*/
64-
CV_EXPORTS Mat loadPLYSimple(const char* fileName, int withNormals);
64+
CV_EXPORTS Mat loadPLYSimple(const char* fileName, int withNormals = 0);
6565

6666
/**
6767
* @brief Write a point cloud to PLY file

modules/surface_matching/src/ppf_helpers.cpp

Lines changed: 59 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,55 +55,92 @@ void getRandomRotation(double R[9]);
5555
void meanCovLocalPC(const float* pc, const int ws, const int point_count, double CovMat[3][3], double Mean[4]);
5656
void meanCovLocalPCInd(const float* pc, const int* Indices, const int ws, const int point_count, double CovMat[3][3], double Mean[4]);
5757

58+
static std::vector<std::string> split(const std::string &text, char sep) {
59+
std::vector<std::string> tokens;
60+
std::size_t start = 0, end = 0;
61+
while ((end = text.find(sep, start)) != std::string::npos) {
62+
tokens.push_back(text.substr(start, end - start));
63+
start = end + 1;
64+
}
65+
tokens.push_back(text.substr(start));
66+
return tokens;
67+
}
68+
69+
70+
5871
Mat loadPLYSimple(const char* fileName, int withNormals)
5972
{
6073
Mat cloud;
61-
int numVertices=0;
74+
int numVertices = 0;
75+
int numCols = 3;
76+
int has_normals = 0;
6277

6378
std::ifstream ifs(fileName);
6479

6580
if (!ifs.is_open())
66-
{
67-
printf("Cannot open file...\n");
68-
return Mat();
69-
}
81+
CV_Error(Error::StsError, String("Error opening input file: ") + String(fileName) + "\n");
7082

7183
std::string str;
72-
while (str.substr(0, 10) !="end_header")
84+
while (str.substr(0, 10) != "end_header")
7385
{
74-
std::string entry = str.substr(0, 14);
75-
if (entry == "element vertex")
86+
std::vector<std::string> tokens = split(str,' ');
87+
if (tokens.size() == 3)
7688
{
77-
numVertices = atoi(str.substr(15, str.size()-15).c_str());
89+
if (tokens[0] == "element" && tokens[1] == "vertex")
90+
{
91+
numVertices = atoi(tokens[2].c_str());
92+
}
93+
else if (tokens[0] == "property")
94+
{
95+
if (tokens[2] == "nx" || tokens[2] == "normal_x")
96+
{
97+
has_normals = -1;
98+
numCols += 3;
99+
}
100+
else if (tokens[2] == "r" || tokens[2] == "red")
101+
{
102+
//has_color = true;
103+
numCols += 3;
104+
}
105+
else if (tokens[2] == "a" || tokens[2] == "alpha")
106+
{
107+
//has_alpha = true;
108+
numCols += 1;
109+
}
110+
}
78111
}
112+
else if (tokens.size() > 1 && tokens[0] == "format" && tokens[1] != "ascii")
113+
CV_Error(Error::StsBadArg, String("Cannot read file, only ascii ply format is currently supported..."));
79114
std::getline(ifs, str);
80115
}
116+
withNormals &= has_normals;
81117

82-
if (withNormals)
83-
cloud=Mat(numVertices, 6, CV_32FC1);
84-
else
85-
cloud=Mat(numVertices, 3, CV_32FC1);
118+
cloud = Mat(numVertices, withNormals ? 6 : 3, CV_32FC1);
86119

87120
for (int i = 0; i < numVertices; i++)
88121
{
89122
float* data = cloud.ptr<float>(i);
123+
int col = 0;
124+
for (; col < withNormals ? 6 : 3; ++col)
125+
{
126+
ifs >> data[col];
127+
}
128+
for (; col < numCols; ++col)
129+
{
130+
float tmp;
131+
ifs >> tmp;
132+
}
90133
if (withNormals)
91134
{
92-
ifs >> data[0] >> data[1] >> data[2] >> data[3] >> data[4] >> data[5];
93-
94135
// normalize to unit norm
95136
double norm = sqrt(data[3]*data[3] + data[4]*data[4] + data[5]*data[5]);
96137
if (norm>0.00001)
97138
{
98-
data[3]/=(float)norm;
99-
data[4]/=(float)norm;
100-
data[5]/=(float)norm;
139+
data[3]/=static_cast<float>(norm);
140+
data[4]/=static_cast<float>(norm);
141+
data[5]/=static_cast<float>(norm);
101142
}
102143
}
103-
else
104-
{
105-
ifs >> data[0] >> data[1] >> data[2];
106-
}
107144
}
108145

109146
//cloud *= 5.0f;

0 commit comments

Comments
 (0)