28 if (point.size() != 3) {
29 throw std::invalid_argument(
"Point::Point(): point must be of dimension 3");
36 Point::Point(
const std::vector<double> &point,
const std::set<size_t> &cluster_ids_in)
38 if (point.size() != 3) {
39 throw std::invalid_argument(
"Point::Point(): point must be of dimension 3");
54 Point::Point(
double xin,
double yin,
double zin,
const std::set<size_t> &cluster_ids_in)
65 std::vector<double> point(3);
75 return sqrt((
x *
x) + (
y *
y) + (
z *
z));
81 return (
x *
x) + (
y *
y) + (
z *
z);
95 return (
x == p.
x &&
y == p.
y &&
z == p.
z);
101 return strm << p.
x <<
" " << p.
y <<
" " << p.
z;
108 ret.
x = this->
x + p.
x;
109 ret.
y = this->
y + p.
y;
110 ret.
z = this->
z + p.
z;
118 ret.
x = this->
x - p.
x;
119 ret.
y = this->
y - p.
y;
120 ret.
z = this->
z - p.
z;
127 return this->
x * p.
x + this->
y * p.
y + this->
z * p.
z;
150 this->points2d =
false;
155 this->points2d =
is2d;
160 return this->points2d;
165 void split(
const std::string &input, std::vector<std::string> &result,
const char delimiter)
167 std::stringstream ss(input);
170 while (std::getline(ss, element, delimiter)) {
171 result.push_back(element);
185 std::ifstream infile(fname);
187 std::vector<std::string> items;
188 size_t count = 0, count2d = 0, skiped = 0, countpoints = 0;
190 throw std::exception();
191 for (
size_t i = 0; i < skip; ++i) {
193 std::getline(infile, line,
'\n');
196 while (!infile.eof()) {
198 if (countpoints > 1000)
199 throw std::length_error(
"Number of points limited to 1000 in demo mode");
202 std::getline(infile, line,
'\n');
206 if (line[0] ==
'#' || line.empty() || line.find_first_not_of(
"\n\r\t ") == std::string::npos)
211 split(line, items, delimiter);
212 if (items.size() < 2) {
213 std::ostringstream oss;
214 oss <<
"row " << count + skiped <<
": "
215 <<
"To few columns!";
216 throw std::invalid_argument(oss.str());
217 }
else if (items.size() == 2) {
218 items.push_back(
"0");
224 point.
x =
stod(items[0].c_str());
226 point.
y =
stod(items[1].c_str());
228 point.
z =
stod(items[2].c_str());
229 cloud.push_back(point);
230 }
catch (std::invalid_argument e) {
231 std::ostringstream oss;
232 oss <<
"row " << count + skiped <<
" column " << column <<
": " << e.what();
233 throw std::invalid_argument(oss.str());
239 if (count2d && count2d != cloud.size()) {
240 throw std::invalid_argument(
"Mixed 2d and 3d points.");
241 }
else if (count2d) {
260 result_cloud = cloud;
265 for (
size_t i = 0; i < cloud.size(); ++i) {
266 nodes.push_back(cloud[i].as_vector());
270 for (
size_t i = 0; i < cloud.size(); ++i) {
272 Point new_point, point = cloud[i];
276 result_size = result.size();
279 std::vector<double> x_list;
280 std::vector<double> y_list;
281 std::vector<double> z_list;
283 for (Kdtree::KdNodeVector::iterator it = result.begin(); it != result.end(); ++it) {
284 x_list.push_back(it->point[0]);
285 y_list.push_back(it->point[1]);
286 z_list.push_back(it->point[2]);
289 new_point.
x = std::accumulate(x_list.begin(), x_list.end(), 0.0) / result_size;
291 new_point.
y = std::accumulate(y_list.begin(), y_list.end(), 0.0) / result_size;
293 new_point.
z = std::accumulate(z_list.begin(), z_list.end(), 0.0) / result_size;
295 result_cloud.push_back(new_point);