Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cast from Exact_predicates_exact_constructions_kernel to Exact_predicates_exact_constructions_kernel_with_sqrt #1873

Closed
alecjacobson opened this issue Jan 31, 2017 · 4 comments
Assignees
Labels

Comments

@alecjacobson
Copy link

Is it possible to convert a number/geometry object from the CGAL::Exact_predicates_exact_constructions_kernel to the CGAL:: Exact_predicates_exact_constructions_kernel_with_sqrt ?

For a miniature example, consider the following non-working code:

#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/Exact_predicates_exact_constructions_kernel_with_sqrt.h>
int main()
{
  CGAL::Epeck::FT a = 4;
  CGAL::Exact_predicates_exact_constructions_kernel_with_sqrt::FT b = a;
  CGAL::Exact_predicates_exact_constructions_kernel_with_sqrt::FT c = 
    CGAL::sqrt(b);
}

This produces a compilation error:

error: no viable conversion from 'CGAL::Epeck::FT' (aka 'Lazy_exact_nt<CGAL::Gmpq>') to 'CGAL::Exact_predicates_exact_constructions_kernel_with_sqrt::FT' (aka 'CORE::Expr')

In my case, I do not need to go in the other direction: CGAL::Exact_predicates_exact_constructions_kernel_with_sqrt to CGAL::Exact_predicates_exact_constructions_kernel.

@alecjacobson
Copy link
Author

Meanwhile, I figured out an insane way to do it:

#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/Exact_predicates_exact_constructions_kernel_with_sqrt.h>
int main()
{
  CGAL::Epeck::FT a = 2;
  CGAL::Exact_predicates_exact_constructions_kernel_with_sqrt::FT b;
  (std::stringstream() << CGAL::exact(a))>>b;
  CGAL::Exact_predicates_exact_constructions_kernel_with_sqrt::FT c =
    CGAL::sqrt(b);
  std::cout << (
    CGAL::sqrt(
      CGAL::Exact_predicates_exact_constructions_kernel_with_sqrt::FT(2)) == 
    c ? "success" : "failure") << std::end;
}

This indeed prints:

success

Converting via stringstream can't possibly be the most efficient way to do this, though. Right?

@sloriot
Copy link
Member

sloriot commented Jan 31, 2017

Here is a working example using the Cartesian_converter:

#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/Exact_predicates_exact_constructions_kernel_with_sqrt.h>
#include <CGAL/Cartesian_converter.h>


struct Lazy_gmpq_to_Expr_converter
  : public std::unary_function< CGAL::Lazy_exact_nt<CGAL::Gmpq>, CORE::Expr >
{
    CORE::Expr
    operator()(const CGAL::Lazy_exact_nt<CGAL::Gmpq> &a) const
    {
      return ::CORE::BigRat(exact(a).mpq());
    }
};

CGAL::Cartesian_converter<CGAL::Epeck,
                          CGAL::Exact_predicates_exact_constructions_kernel_with_sqrt,
                          Lazy_gmpq_to_Expr_converter> to_sqrt_kernel;

int main()
{
  CGAL::Epeck::FT a;
  CGAL::Exact_predicates_exact_constructions_kernel_with_sqrt::FT b = to_sqrt_kernel(a);
  CGAL::Epeck::Point_3 c;
  CGAL::Exact_predicates_exact_constructions_kernel_with_sqrt::Point_3 d = to_sqrt_kernel(c);
}

Note that I have hardcoded the number types of the Kernel (they might change if you do not use GMP).
Also notice that calling exact() on a lazy (nt or geometric object) will trigger the exact computation.

@sloriot sloriot self-assigned this Jan 31, 2017
@alecjacobson
Copy link
Author

Thanks, this seems to solve my issue.

Should I interpret "trigger the exact computation" as "trigger slow performance" ?

I have an algorithm where the bulk of the computation can be done using CGAL::Epeck and I only need sqrts at the end. In general, am I better off usingEpeck for the first part and converting via your solution, or should I just use Exact_predicates_exact_constructions_kernel_with_sqrt for the entire thing?

@sloriot
Copy link
Member

sloriot commented Jan 31, 2017

I don't think using Exact_predicates_exact_constructions_kernel_with_sqrt from the beginning would be faster than converting at the end. Except maybe if you have no filter failures. It really depends on what you are doing. About the trigger the exact computation, if you do not have a lot of cascaded constructions that should not be problem.

@lrineau lrineau closed this as completed Feb 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants