@@ -55,55 +55,92 @@ 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
57
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
+
58
71
Mat loadPLYSimple (const char * fileName, int withNormals)
59
72
{
60
73
Mat cloud;
61
- int numVertices=0 ;
74
+ int numVertices = 0 ;
75
+ int numCols = 3 ;
76
+ int has_normals = 0 ;
62
77
63
78
std::ifstream ifs (fileName);
64
79
65
80
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 " );
70
82
71
83
std::string str;
72
- while (str.substr (0 , 10 ) !=" end_header" )
84
+ while (str.substr (0 , 10 ) != " end_header" )
73
85
{
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 )
76
88
{
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
+ }
78
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..." ));
79
114
std::getline (ifs, str);
80
115
}
116
+ withNormals &= has_normals;
81
117
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);
86
119
87
120
for (int i = 0 ; i < numVertices; i++)
88
121
{
89
122
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
+ }
90
133
if (withNormals)
91
134
{
92
- ifs >> data[0 ] >> data[1 ] >> data[2 ] >> data[3 ] >> data[4 ] >> data[5 ];
93
-
94
135
// normalize to unit norm
95
136
double norm = sqrt (data[3 ]*data[3 ] + data[4 ]*data[4 ] + data[5 ]*data[5 ]);
96
137
if (norm>0.00001 )
97
138
{
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) ;
101
142
}
102
143
}
103
- else
104
- {
105
- ifs >> data[0 ] >> data[1 ] >> data[2 ];
106
- }
107
144
}
108
145
109
146
// cloud *= 5.0f;
0 commit comments