Skip to content

Commit 4e6e8a4

Browse files
ahundtterfendail
authored andcommitted
loadPLYSimple basic automatic detection of normals, color, and alpha properties
1 parent 47237bf commit 4e6e8a4

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
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: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,13 @@ std::vector<std::string> split(const std::string &text, char sep) {
6969

7070

7171

72-
Mat loadPLYSimple(const char* fileName, int withNormals)
72+
Mat loadPLYSimple(const char* fileName, int withNormals /* = 0 */)
7373
{
7474
Mat cloud;
7575
int numVertices=0;
76+
int numCols=3;
77+
bool with_color = false;
78+
bool with_alpha = false; // alpha transparency
7679

7780
std::ifstream ifs(fileName);
7881

@@ -90,41 +93,57 @@ Mat loadPLYSimple(const char* fileName, int withNormals)
9093
{
9194
numVertices = atoi(tokens[2].c_str());
9295
}
96+
if (tokens.size() ==3 && tokens[0] == "property")
97+
{
98+
if(tokens[3]=="nx" || tokens[3]=="normal_x" )
99+
{
100+
withNormals = true;
101+
numCols+=3;
102+
}
103+
else if(tokens[3]=="r" || tokens[3]=="red" )
104+
{
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+
}
93114
else if (tokens.size() > 1 && tokens[0] == "format")
94115
{
95116
if (tokens[1]!="ascii"){
96117
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...");
97120
return Mat();
98121
}
99122
}
100123
std::getline(ifs, str);
101124
}
102125

103-
if (withNormals)
104-
cloud=Mat(numVertices, 6, CV_32FC1);
105-
else
106-
cloud=Mat(numVertices, 3, CV_32FC1);
126+
cloud=Mat(numVertices, numCols, CV_32FC1);
107127

108128
for (int i = 0; i < numVertices; i++)
109129
{
110130
float* data = cloud.ptr<float>(i);
131+
for (int col = 0; col < numCols; ++col)
132+
{
133+
ifs >> data[col];
134+
}
111135
if (withNormals)
112136
{
113-
ifs >> data[0] >> data[1] >> data[2] >> data[3] >> data[4] >> data[5];
114137

115138
// normalize to unit norm
116139
double norm = sqrt(data[3]*data[3] + data[4]*data[4] + data[5]*data[5]);
117140
if (norm>0.00001)
118141
{
119-
data[3]/=(float)norm;
120-
data[4]/=(float)norm;
121-
data[5]/=(float)norm;
142+
data[3]/=static_cast<float>(norm);
143+
data[4]/=static_cast<float>(norm);
144+
data[5]/=static_cast<float>(norm);
122145
}
123146
}
124-
else
125-
{
126-
ifs >> data[0] >> data[1] >> data[2];
127-
}
128147
}
129148

130149
//cloud *= 5.0f;

0 commit comments

Comments
 (0)