// Copyright (c) 2020 GeometryFactory SARL (France). // All rights reserved. // // This file is part of CGAL (www.cgal.org). // // $URL$ // $Id$ // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-Commercial // // // Author(s) : Simon Giraudot, Dmitry Anisimov #ifndef CGAL_KSP_UTILS_H #define CGAL_KSP_UTILS_H #include // STL includes. #include #include #include #include #include #include #include #include #include #include #include // CGAL includes. #include #include #include #include #include // Boost includes. #include namespace CGAL { namespace KSP { namespace internal { #ifdef DOXYGEN_RUNNING #else // Convert point to string. template const std::string to_string(const Point_d& p) { std::ostringstream oss; oss.precision(20); oss << p; return oss.str(); } // Distance between two points. template decltype(auto) distance(const Point_d& p, const Point_d& q) { using Traits = typename Kernel_traits::Kernel; using FT = typename Traits::FT; const FT sq_dist = CGAL::squared_distance(p, q); return static_cast(CGAL::approximate_sqrt(sq_dist)); } // Normalize vector. template inline const Vector_d normalize(const Vector_d& v) { using Traits = typename Kernel_traits::Kernel; using FT = typename Traits::FT; const FT dot_product = CGAL::abs(v * v); //CGAL_assertion(dot_product != FT(0)); return v / static_cast(CGAL::approximate_sqrt(dot_product)); } // Intersections. Used only in the 2D version. // For the 3D version, see conversions.h! template inline bool intersection( const Type1& t1, const Type2& t2, ResultType& result) { const auto inter = intersection(t1, t2); if (!inter) return false; if (CGAL::assign(result, inter)) return true; return false; } template inline const ResultType intersection(const Type1& t1, const Type2& t2) { ResultType out; CGAL_assertion_code(const bool is_intersection_found =) intersection(t1, t2, out); CGAL_assertion(is_intersection_found); return out; } // Angles. // Converts radians to degrees. template const FT degrees_2(const FT angle_rad) { return angle_rad * FT(180) / static_cast(CGAL_PI); } // Computes an angle in degrees between two directions. template const typename Kernel_traits::Kernel::FT compute_angle_2(const Direction_2& dir1, const Direction_2& dir2) { using Traits = typename Kernel_traits::Kernel; using FT = typename Traits::FT; const auto v1 = dir2.to_vector(); const auto v2 = -dir1.to_vector(); const FT det = CGAL::determinant(v1, v2); const FT dot = CGAL::scalar_product(v1, v2); const FT angle_rad = static_cast( std::atan2(CGAL::to_double(det), CGAL::to_double(dot))); const FT angle_deg = degrees_2(angle_rad); return angle_deg; } // Converts an angle in degrees from the range [-180, 180] // into the mod 90 angle. template const FT convert_angle_2(const FT angle_2) { FT angle = angle_2; if (angle > FT(90)) angle = FT(180) - angle; else if (angle < -FT(90)) angle = FT(180) + angle; return angle; } // Computes a positive angle in degrees that // is always in the range [0, 90]. template const typename Kernel_traits::Kernel::FT angle_2(const Direction_2& dir1, const Direction_2& dir2) { const auto angle_2 = compute_angle_2(dir1, dir2); return CGAL::abs(convert_angle_2(angle_2)); } #endif } // namespace internal } // namespace KSP } // namespace CGAL #endif // CGAL_KSP_UTILS_H