20 #include <boost/lexical_cast.hpp> 54 int pointNo = point_list.size();
59 for (
int i=0; i<pointNo; i++)
62 if (point_list[j][1] <= point[1])
65 if (point_list[i][1] >= point[1])
67 const double is_left = (point_list[i][0] - point_list[j][0]) * (point[1] - point_list[j][1])
68 - (point[0] - point_list[j][0]) * (point_list[i][1] - point_list[j][1]);
70 if ( is_left > 0 && point_list[i][1] > point[1])
75 else if ( is_left == 0)
79 const double dot_product = (point - point_list[j])*(point_list[i] - point_list[j]);
83 const double squaredlength = (point_list[i] - point_list[j]).norm_square();
85 if (dot_product <= squaredlength)
96 if (point_list[i][1] <= point[1])
98 const double is_left = (point_list[i][0] - point_list[j][0]) * (point[1] - point_list[j][1])
99 - (point[0] - point_list[j][0]) * (point_list[i][1] - point_list[j][1]);
106 else if ( is_left == 0)
111 const double dot_product = (point - point_list[j])*(point_list[i] - point_list[j]);
113 if (dot_product >= 0)
115 const double squaredlength = (point_list[i] - point_list[j]).norm_square();
117 if (dot_product <= squaredlength)
152 const unsigned int n_poly_points = point_list.size();
153 WBAssertThrow(n_poly_points >= 3,
"Not enough polygon points were specified.");
156 std::vector<double> distances(n_poly_points, 1e23);
159 std::vector<Point<2> > shifted_point_list(n_poly_points);
160 shifted_point_list[0] = point_list[n_poly_points-1];
162 for (
unsigned int i = 0; i < n_poly_points-1; ++i)
163 shifted_point_list[i+1] = point_list[i];
165 for (
unsigned int i = 0; i < n_poly_points; ++i)
168 Point<2> vector_segment = shifted_point_list[i] - point_list[i];
170 Point<2> vector_point_segment = point - point_list[i];
173 const double c1 = vector_point_segment * vector_segment;
174 const double c2 = vector_segment * vector_segment;
178 distances[i] = (
Point<2> (point_list[i] - point)).
norm();
181 distances[i] = (
Point<2> (shifted_point_list[i] - point)).norm();
185 const Point<2> point_on_segment = point_list[i] + (c1/c2) * vector_segment;
186 distances[i] = (
Point<2> (point - point_on_segment)).
norm();
191 return *std::min_element(distances.begin(),distances.end()) * sign;
242 std::array<double,2> coordinate;
244 switch (coordinate_system)
247 coordinate[0] = coordinates[0];
248 coordinate[1] = coordinates[1];
252 coordinate[0] = coordinates[1];
253 coordinate[1] = coordinates[2];
257 WBAssert (
false,
"Coordinate system not implemented.");
266 switch (coordinate_system)
269 return coordinates[2];
272 return coordinates[0];
275 WBAssert (
false,
"Coordinate system not implemented.");
285 std::array<double,3> scoord;
287 scoord[0] = position.
norm();
288 scoord[1] = std::atan2(position[1],position[0]);
290 scoord[1] += 2.0*M_PI;
292 if (scoord[0] > std::numeric_limits<double>::min())
293 scoord[2] = std::acos(position[2]/scoord[0]);
305 ccoord[0] = scoord[0] * std::sin(scoord[2]) * std::cos(scoord[1]);
306 ccoord[1] = scoord[0] * std::sin(scoord[2]) * std::sin(scoord[1]);
307 ccoord[2] = scoord[0] * std::cos(scoord[2]);
318 if (coordinate_system ==
"cartesian")
320 else if (coordinate_system ==
"spherical")
330 const std::array<double,dim>
333 std::array<double,dim> array;
334 for (
unsigned int i = 0; i < dim; ++i)
335 array[i] = point_[i];
343 std::string s = string;
344 while ((s.size() > 0) && (s[0] ==
' '))
346 while ((s.size() > 0) && (s[s.size() - 1] ==
' '))
347 s.erase(s.end() - 1);
352 d = boost::lexical_cast<
double>(s);
354 catch (
const boost::bad_lexical_cast &e)
356 WBAssertThrow(
false,
"Conversion of \"" <<
string <<
"\" to double failed (bad cast): " << e.what() << std::endl);
366 std::string s = string;
367 while ((s.size() > 0) && (s[0] ==
' '))
369 while ((s.size() > 0) && (s[s.size() - 1] ==
' '))
370 s.erase(s.end() - 1);
375 d = boost::lexical_cast<
int>(s);
377 catch (
const boost::bad_lexical_cast &e)
379 WBAssertThrow(
false,
"Conversion of \"" <<
string <<
"\" to int failed (bad cast): " << e.what() << std::endl);
390 std::string s = string;
391 while ((s.size() > 0) && (s[0] ==
' '))
393 while ((s.size() > 0) && (s[s.size() - 1] ==
' '))
394 s.erase(s.end() - 1);
399 d = boost::lexical_cast<
unsigned int>(s);
401 catch (
const boost::bad_lexical_cast &e)
403 WBAssertThrow(
false,
"Conversion of \"" <<
string <<
"\" to unsigned int failed (bad cast): " << e.what() << std::endl);
409 boost::optional<std::string>
411 const std::string &path,
412 const std::string &key,
414 const std::string &path_separator)
416 boost::optional<std::string> value = tree.get_optional<std::string> (key);
417 WBAssertThrow ((value && required ==
true) || required ==
false,
"Entry undeclared: " + path + path_separator + key +
418 ". Tree: " << std::endl <<
print_tree(tree,0).str() << std::endl);
422 boost::optional<std::string>
424 const std::string &path,
425 const std::string &key,
427 const std::string &path_separator)
429 std::string use_path = path ==
"" ? key : path + path_separator + key;
430 boost::optional<std::string> value = tree.get_optional<std::string> (use_path);
431 WBAssertThrow ((value && required ==
true) || required ==
false,
"Entry undeclared: " + use_path +
432 ". Tree: " << std::endl <<
print_tree(tree,0).str() << std::endl);
450 for (
int i=0; i<level; i++) s +=
" ";
456 std::stringstream ss;
459 ss <<
"\""<< pt.data()<<
"\"";
464 if (level) ss << std::endl;
466 ss <<
indent(level) <<
"{" << std::endl;
468 for (ptree::const_iterator pos = pt.begin(); pos != pt.end();)
470 ss <<
indent(level+1) <<
"\"" << pos->first <<
"\": ";
472 ss <<
print_tree(pos->second, level + 1).str();
480 ss <<
indent(level) <<
" }";
boost::optional< std::string > get_from_ptree(const ptree &tree, const std::string &path, const std::string &key, const bool required, const std::string &path_separator)
std::array< double, 3 > cartesian_to_spherical_coordinates(const Point< 3 > &position)
const std::array< double, 2 > get_surface_coordinates() const
virtual CoordinateSystem natural_coordinate_system() const =0
std::stringstream print_tree(const ptree &pt, int level)
Point< 3 > spherical_to_cartesian_coordinates(const std::array< double, 3 > &scoord)
double string_to_unsigned_int(const std::string &string)
const std::array< double, dim > & get_array() const
#define WBAssert(condition, message)
const std::array< double, dim > convert_point_to_array(const Point< dim > &point_)
std::string indent(int level)
boost::optional< std::string > get_from_ptree_abs(const ptree &tree, const std::string &path, const std::string &key, const bool required, const std::string &path_separator)
virtual std::array< double, 3 > cartesian_to_natural_coordinates(const std::array< double, 3 > &position) const =0
const std::array< double, 3 > & get_coordinates()
#define WBAssertThrow(condition, message)
double string_to_double(const std::string &string)
double get_depth_coordinate() const
NaturalCoordinate(const std::array< double, 3 > &position, const ::WorldBuilder::CoordinateSystems::Interface &coordinate_system)
bool polygon_contains_point(const std::vector< Point< 2 > > &point_list, const Point< 2 > &point)
double string_to_int(const std::string &string)
CoordinateSystem string_to_coordinate_system(const std::string &coordinate_system)
template const std::array< double, 2 > convert_point_to_array< 2 >(const Point< 2 > &point_)
template const std::array< double, 3 > convert_point_to_array< 3 >(const Point< 3 > &point_)
double signed_distance_to_polygon(const std::vector< Point< 2 > > &point_list, const Point< 2 > &point)