static float point_angle(const t_point2i& p) { assert((seed_point()).x() != 0); assert((seed_point()).y() != 0); const t_point2i s = seed_point(); const t_point2f v = t_point2f(p.x() - s.x(), p.y() - s.y()); if (p == s) return 0.0f; const float r = v.radius(); assert(r != 0.0f); const t_point2f n = t_point2f(v.x() / r, v.y() / r); const float rca = n.dot(t_point2f(1.0f, 0.0f)); const float cca = std::max(-1.0f, std::min(1.0f, rca)); // return angle in radians return (std::acos(cca)); }
void setup(std::vector<t_point2i>& data_points, std::vector<t_point2i>& hull_points) { hull_points.reserve(data_points.size()); randomize_data_points(data_points); add_data_points_to_bins(data_points, POINT_BIN_GRID); // find algorithm "seed" (point with lowest y-coordinate) // put it in front of all other points s.t it is the only // point not touched by sorting const unsigned int seed_point_idx = get_seed_point_index(data_points); // initialize the seed, idx is always 0 if using raw points seed_point(data_points[seed_point_idx]); std::swap(data_points[0], data_points[seed_point_idx]); std::sort(++(data_points.begin()), data_points.end(), point_sort_func); }
bool algorithm_pipeline::add_algorithm( pugi::xml_node const & algorithm_node ) { pugi::xml_attribute algorithm_name_attribute = algorithm_node.attribute("name"); std::string algorithm_name; if ( !algorithm_name_attribute.empty() ) algorithm_name = algorithm_name_attribute.as_string(); pugi::xml_attribute algorithm_type_attribute = algorithm_node.attribute("type"); if ( algorithm_type_attribute.empty() ) { error(1) << "Algorithm \"" << algorithm_name << "\" has no type attribute" << std::endl; return false; } std::string algorithm_type = algorithm_type_attribute.as_string(); algorithm_pipeline_element pipeline_element(algorithm_name); try { pipeline_element.algorithm = context.make_algorithm( algorithm_type ); } catch (viennamesh::exception const & ex) { error(1) << "Algorithm with id \"" << algorithm_type << "\" creation from factory failed" << std::endl; return false; } { pugi::xml_attribute algorithm_info_log_level_attribute = algorithm_node.attribute("info_log_level"); if ( !algorithm_info_log_level_attribute.empty() ) pipeline_element.info_log_level = boost::lexical_cast<int>( algorithm_info_log_level_attribute.as_string() ); } { pugi::xml_attribute algorithm_error_log_level_attribute = algorithm_node.attribute("error_log_level"); if ( !algorithm_error_log_level_attribute.empty() ) pipeline_element.error_log_level = boost::lexical_cast<int>( algorithm_error_log_level_attribute.as_string() ); } { pugi::xml_attribute algorithm_warning_log_level_attribute = algorithm_node.attribute("warning_log_level"); if ( !algorithm_warning_log_level_attribute.empty() ) pipeline_element.warning_log_level = boost::lexical_cast<int>( algorithm_warning_log_level_attribute.as_string() ); } { pugi::xml_attribute algorithm_debug_log_level_attribute = algorithm_node.attribute("warning_debug_level"); if ( !algorithm_debug_log_level_attribute.empty() ) pipeline_element.debug_log_level = boost::lexical_cast<int>( algorithm_debug_log_level_attribute.as_string() ); } { pugi::xml_attribute algorithm_stack_log_level_attribute = algorithm_node.attribute("warning_stack_level"); if ( !algorithm_stack_log_level_attribute.empty() ) pipeline_element.stack_log_level = boost::lexical_cast<int>( algorithm_stack_log_level_attribute.as_string() ); } algorithm_handle & algorithm = pipeline_element.algorithm; if (!algorithm.valid()) { error(1) << "Algorithm with id \"" << algorithm_type << "\" creation from factory failed" << std::endl; return false; } pugi::xml_node default_source_algorithm_node = algorithm_node.child("default_source"); if (default_source_algorithm_node) { std::string default_source_algorithm_name = default_source_algorithm_node.text().as_string(); algorithm_pipeline_element * default_source_element = get_element( default_source_algorithm_name ); if (!default_source_element) return false; algorithm.set_default_source( default_source_element->algorithm ); ++(default_source_element->reference_count); pipeline_element.referenced_elements.push_back( default_source_element ); } for (pugi::xml_node paramater_node = algorithm_node.child("parameter"); paramater_node; paramater_node = paramater_node.next_sibling("parameter")) { pugi::xml_attribute parameter_name_attribute = paramater_node.attribute("name"); if (parameter_name_attribute.empty()) { error(1) << "Parameter has no name attribute" << std::endl; return false; } std::string parameter_name = parameter_name_attribute.as_string(); pugi::xml_attribute parameter_type_attribute = paramater_node.attribute("type"); if (parameter_type_attribute.empty()) { error(1) << "Parameter \"" << parameter_name << "\" has no type attribute" << std::endl; return false; } std::string parameter_type = parameter_type_attribute.as_string(); std::string parameter_value = paramater_node.text().as_string(); if (parameter_type == "xml") { std::stringstream ss; for (pugi::xml_node child = paramater_node.first_child(); child; child = child.next_sibling()) child.print(ss); algorithm.set_input( parameter_name, ss.str() ); } else { if (parameter_value.empty()) { error(1) << "Parameter \"" << parameter_name << "\" has no value" << std::endl; return false; } if (parameter_type == "string") { algorithm.push_back_input( parameter_name, parameter_value ); } else if (parameter_type == "bool") { algorithm.push_back_input( parameter_name, boost::lexical_cast<bool>(parameter_value) ); } else if (parameter_type == "int") { algorithm.push_back_input( parameter_name, boost::lexical_cast<int>(parameter_value) ); } else if (parameter_type == "double") { algorithm.push_back_input( parameter_name, boost::lexical_cast<double>(parameter_value) ); } else if (parameter_type == "point") { algorithm.push_back_input( parameter_name, boost::lexical_cast<point>(parameter_value) ); } else if ((parameter_type == "points") || (parameter_type == "matrix")) { std::list<std::string> split_mappings = split_string_brackets( parameter_value, "," ); point_container points; for (std::list<std::string>::const_iterator sit = split_mappings.begin(); sit != split_mappings.end(); ++sit) points.push_back( boost::lexical_cast<point>(*sit) ); data_handle<viennamesh_point> point_handle = context.make_data<point>(points); algorithm.set_input( parameter_name, point_handle ); } else if (parameter_type == "seed_point") { std::list<std::string> split_mappings = split_string_brackets( parameter_value, ";" ); if (split_mappings.size() != 2) { error(1) << "String to seed point conversion: an entry has no point and no region id: " << parameter_value << std::endl; return false; } std::list<std::string>::const_iterator it = split_mappings.begin(); // std::cout << "seed point to point: " << *it << std::endl; point p = boost::lexical_cast<point>( *it ); ++it; // std::cout << "seed point to region id: " << *it << std::endl; viennagrid_int region_id = boost::lexical_cast<viennagrid_int>( *it ); algorithm.push_back_input( parameter_name, seed_point(p, region_id) ); } else if (parameter_type == "dynamic") { std::string source_algorithm_name = parameter_value.substr( 0, parameter_value.find("/") ); std::string source_parameter_name = parameter_value.substr( parameter_value.find("/")+1 ); algorithm_pipeline_element * default_source_element = get_element( source_algorithm_name ); if (!default_source_element) return false; algorithm.link_input( parameter_name, default_source_element->algorithm, source_parameter_name ); ++(default_source_element->reference_count); pipeline_element.referenced_elements.push_back( default_source_element ); } else { error(1) << "Parameter \"" << parameter_name << "\": type \"" << parameter_type << "\" is not supported" << std::endl; return false; } } } algorithms.push_back( pipeline_element ); return true; }