Skip to content

Commit a922dd5

Browse files
committed
updated loadPLYSimple automatic detection of normals, color, and alpha properties
1 parent 4e6e8a4 commit a922dd5

File tree

1 file changed

+37
-40
lines changed

1 file changed

+37
-40
lines changed

modules/surface_matching/src/ppf_helpers.cpp

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,8 @@ void getRandQuat(double q[4]);
5454
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]);
57-
std::vector<std::string> split(const std::string &text, char sep);
5857

59-
std::vector<std::string> split(const std::string &text, char sep) {
58+
static std::vector<std::string> split(const std::string &text, char sep) {
6059
std::vector<std::string> tokens;
6160
std::size_t start = 0, end = 0;
6261
while ((end = text.find(sep, start)) != std::string::npos) {
@@ -69,72 +68,70 @@ std::vector<std::string> split(const std::string &text, char sep) {
6968

7069

7170

72-
Mat loadPLYSimple(const char* fileName, int withNormals /* = 0 */)
71+
Mat loadPLYSimple(const char* fileName, int withNormals)
7372
{
7473
Mat cloud;
75-
int numVertices=0;
76-
int numCols=3;
77-
bool with_color = false;
78-
bool with_alpha = false; // alpha transparency
74+
int numVertices = 0;
75+
int numCols = 3;
76+
int has_normals = 0;
7977

8078
std::ifstream ifs(fileName);
8179

8280
if (!ifs.is_open())
83-
{
84-
printf("Cannot open file...\n");
85-
return Mat();
86-
}
81+
CV_Error(Error::StsError, String("Error opening input file: ") + String(fileName) + "\n");
8782

8883
std::string str;
89-
while (str.substr(0, 10) !="end_header")
84+
while (str.substr(0, 10) != "end_header")
9085
{
9186
std::vector<std::string> tokens = split(str,' ');
92-
if (tokens.size() == 3 && tokens[0] == "element" && tokens[1] == "vertex")
87+
if (tokens.size() == 3)
9388
{
94-
numVertices = atoi(tokens[2].c_str());
95-
}
96-
if (tokens.size() ==3 && tokens[0] == "property")
97-
{
98-
if(tokens[3]=="nx" || tokens[3]=="normal_x" )
89+
if (tokens[0] == "element" && tokens[1] == "vertex")
9990
{
100-
withNormals = true;
101-
numCols+=3;
91+
numVertices = atoi(tokens[2].c_str());
10292
}
103-
else if(tokens[3]=="r" || tokens[3]=="red" )
93+
else if (tokens[0] == "property")
10494
{
105-
with_color = true;
106-
numCols+=3;
107-
}
108-
else if(tokens[3]=="a" || tokens[3]=="alpha" )
109-
{
110-
with_alpha = true;
111-
numCols+=1;
112-
}
113-
}
114-
else if (tokens.size() > 1 && tokens[0] == "format")
115-
{
116-
if (tokens[1]!="ascii"){
117-
printf("Cannot read file, only ascii ply format is currently supported...\n");
118-
// uncomment below when CV_StsBadArg can be located
119-
//OPENCV_ERROR (CV_StsBadArg, "loadPLYSimple", "Cannot read file, only ascii ply format is currently supported...");
120-
return Mat();
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+
}
121110
}
122111
}
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..."));
123114
std::getline(ifs, str);
124115
}
116+
withNormals &= has_normals;
125117

126-
cloud=Mat(numVertices, numCols, CV_32FC1);
118+
cloud = Mat(numVertices, withNormals ? 6 : 3, CV_32FC1);
127119

128120
for (int i = 0; i < numVertices; i++)
129121
{
130122
float* data = cloud.ptr<float>(i);
131-
for (int col = 0; col < numCols; ++col)
123+
int col = 0;
124+
for (; col < withNormals ? 6 : 3; ++col)
132125
{
133126
ifs >> data[col];
134127
}
128+
for (; col < numCols; ++col)
129+
{
130+
float tmp;
131+
ifs >> tmp;
132+
}
135133
if (withNormals)
136134
{
137-
138135
// normalize to unit norm
139136
double norm = sqrt(data[3]*data[3] + data[4]*data[4] + data[5]*data[5]);
140137
if (norm>0.00001)

0 commit comments

Comments
 (0)