Commit 4e6e8a4e authored by Andrew Hundt's avatar Andrew Hundt Committed by Vitaly Tuzov

loadPLYSimple basic automatic detection of normals, color, and alpha properties

parent 47237bf1
......@@ -61,7 +61,7 @@ namespace ppf_match_3d
* and whether it should be loaded or not
* @return Returns the matrix on successfull load
*/
CV_EXPORTS Mat loadPLYSimple(const char* fileName, int withNormals);
CV_EXPORTS Mat loadPLYSimple(const char* fileName, int withNormals = 0);
/**
* @brief Write a point cloud to PLY file
......
......@@ -69,10 +69,13 @@ std::vector<std::string> split(const std::string &text, char sep) {
Mat loadPLYSimple(const char* fileName, int withNormals)
Mat loadPLYSimple(const char* fileName, int withNormals /* = 0 */)
{
Mat cloud;
int numVertices=0;
int numCols=3;
bool with_color = false;
bool with_alpha = false; // alpha transparency
std::ifstream ifs(fileName);
......@@ -90,41 +93,57 @@ Mat loadPLYSimple(const char* fileName, int withNormals)
{
numVertices = atoi(tokens[2].c_str());
}
if (tokens.size() ==3 && tokens[0] == "property")
{
if(tokens[3]=="nx" || tokens[3]=="normal_x" )
{
withNormals = true;
numCols+=3;
}
else if(tokens[3]=="r" || tokens[3]=="red" )
{
with_color = true;
numCols+=3;
}
else if(tokens[3]=="a" || tokens[3]=="alpha" )
{
with_alpha = true;
numCols+=1;
}
}
else if (tokens.size() > 1 && tokens[0] == "format")
{
if (tokens[1]!="ascii"){
printf("Cannot read file, only ascii ply format is currently supported...\n");
// uncomment below when CV_StsBadArg can be located
//OPENCV_ERROR (CV_StsBadArg, "loadPLYSimple", "Cannot read file, only ascii ply format is currently supported...");
return Mat();
}
}
std::getline(ifs, str);
}
if (withNormals)
cloud=Mat(numVertices, 6, CV_32FC1);
else
cloud=Mat(numVertices, 3, CV_32FC1);
cloud=Mat(numVertices, numCols, CV_32FC1);
for (int i = 0; i < numVertices; i++)
{
float* data = cloud.ptr<float>(i);
for (int col = 0; col < numCols; ++col)
{
ifs >> data[col];
}
if (withNormals)
{
ifs >> data[0] >> data[1] >> data[2] >> data[3] >> data[4] >> data[5];
// normalize to unit norm
double norm = sqrt(data[3]*data[3] + data[4]*data[4] + data[5]*data[5]);
if (norm>0.00001)
{
data[3]/=(float)norm;
data[4]/=(float)norm;
data[5]/=(float)norm;
data[3]/=static_cast<float>(norm);
data[4]/=static_cast<float>(norm);
data[5]/=static_cast<float>(norm);
}
}
else
{
ifs >> data[0] >> data[1] >> data[2];
}
}
//cloud *= 5.0f;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment