@@ -54,9 +54,8 @@ void getRandQuat(double q[4]);
54
54
void getRandomRotation (double R[9 ]);
55
55
void meanCovLocalPC (const float * pc, const int ws, const int point_count, double CovMat[3 ][3 ], double Mean[4 ]);
56
56
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);
58
57
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) {
60
59
std::vector<std::string> tokens;
61
60
std::size_t start = 0 , end = 0 ;
62
61
while ((end = text.find (sep, start)) != std::string::npos) {
@@ -69,72 +68,70 @@ std::vector<std::string> split(const std::string &text, char sep) {
69
68
70
69
71
70
72
- Mat loadPLYSimple (const char * fileName, int withNormals /* = 0 */ )
71
+ Mat loadPLYSimple (const char * fileName, int withNormals)
73
72
{
74
73
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 ;
79
77
80
78
std::ifstream ifs (fileName);
81
79
82
80
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 " );
87
82
88
83
std::string str;
89
- while (str.substr (0 , 10 ) !=" end_header" )
84
+ while (str.substr (0 , 10 ) != " end_header" )
90
85
{
91
86
std::vector<std::string> tokens = split (str,' ' );
92
- if (tokens.size () == 3 && tokens[ 0 ] == " element " && tokens[ 1 ] == " vertex " )
87
+ if (tokens.size () == 3 )
93
88
{
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" )
99
90
{
100
- withNormals = true ;
101
- numCols+=3 ;
91
+ numVertices = atoi (tokens[2 ].c_str ());
102
92
}
103
- else if (tokens[3 ]== " r " || tokens[ 3 ]== " red " )
93
+ else if (tokens[0 ] == " property " )
104
94
{
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
+ }
121
110
}
122
111
}
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..." ));
123
114
std::getline (ifs, str);
124
115
}
116
+ withNormals &= has_normals;
125
117
126
- cloud= Mat (numVertices, numCols , CV_32FC1);
118
+ cloud = Mat (numVertices, withNormals ? 6 : 3 , CV_32FC1);
127
119
128
120
for (int i = 0 ; i < numVertices; i++)
129
121
{
130
122
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)
132
125
{
133
126
ifs >> data[col];
134
127
}
128
+ for (; col < numCols; ++col)
129
+ {
130
+ float tmp;
131
+ ifs >> tmp;
132
+ }
135
133
if (withNormals)
136
134
{
137
-
138
135
// normalize to unit norm
139
136
double norm = sqrt (data[3 ]*data[3 ] + data[4 ]*data[4 ] + data[5 ]*data[5 ]);
140
137
if (norm>0.00001 )
0 commit comments