Skip to content

Instantly share code, notes, and snippets.

@ochafik
Last active July 26, 2023 12:18
Show Gist options
  • Save ochafik/73fb81448d6db6eb4a869f9090a89e40 to your computer and use it in GitHub Desktop.
Save ochafik/73fb81448d6db6eb4a869f9090a89e40 to your computer and use it in GitHub Desktop.
CGAL: convert between CGAL::Cartesian<CGAL::Gmpq> and CGAL::Epeck
// Copyright 2021 Google LLC.
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include <CGAL/Cartesian.h>
#include <CGAL/Gmpq.h>
#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/utils.h>
#include <CGAL/Cartesian_converter.h>
#include <sstream>
#ifdef CGAL_USE_GMPXX
# include <CGAL/gmpxx.h>
#endif
namespace CGAL {
std::size_t hash_value(const mpq_class &x) {
std::hash<double> dh;
return dh(CGAL::to_double(x));
}
std::size_t hash_value(const CGAL::Lazy_exact_nt<mpq_class> &x) {
std::hash<double> dh;
return dh(CGAL::to_double(x));
}
std::size_t hash_value(const CGAL::Lazy_exact_nt<CGAL::Gmpq> &x) {
std::hash<double> dh;
return dh(CGAL::to_double(x));
}
}
template <typename FromKernel, typename ToKernel>
struct KernelFieldTypeConverter {
typename ToKernel::FT operator()(const typename FromKernel::FT &g) const;
};
template <typename FromKernel, typename ToKernel>
CGAL::Cartesian_converter<FromKernel, ToKernel, KernelFieldTypeConverter<FromKernel, ToKernel>>
getCartesianConverter()
{
return CGAL::Cartesian_converter<FromKernel, ToKernel,
KernelFieldTypeConverter<FromKernel, ToKernel>>();
}
// #ifdef CGAL_USE_GMPXX
template <>
CGAL::Lazy_exact_nt<mpq_class> KernelFieldTypeConverter<
CGAL::Cartesian<CGAL::Gmpq>, CGAL::Epeck>::operator()(const CGAL::Gmpq &n) const
{
return mpq_class(mpz_class(n.numerator().mpz()), mpz_class(n.denominator().mpz()));
}
template <>
CGAL::Gmpq KernelFieldTypeConverter<
CGAL::Epeck, CGAL::Cartesian<CGAL::Gmpq>>::operator()(const CGAL::Lazy_exact_nt<mpq_class> &n) const
{
auto &e = n.exact();
return CGAL::Gmpq(CGAL::Gmpz(e.get_num().get_mpz_t()), CGAL::Gmpz(e.get_den().get_mpz_t()));
}
// #endif // CGAL_USE_GMPXX
/*
TODO(ochafik): contribute this to CGAL/include/CGAL/GMP/Gmpq_type.h
#ifdef CGAL_USE_GMPXX
# include <CGAL/gmpxx.h>
#endif
#ifdef CGAL_USE_GMPXX
Gmpq(const mpq_class& c)
{
mpz_set(mpq_numref(mpq()), c.get_num());
mpz_set(mpq_denref(mpq()), c.get_den());
mpq_canonicalize(mpq());
}
operator mpq_class() const {
return mpq_class(mpz_class(numerator().mpz()), mpz_class(denominator().mpz()));
}
#endif
*/
// Copyright 2021 Google LLC.
// SPDX-License-Identifier: Apache-2.0
/*
Compile and run with:
g++ -DCGAL_USE_GMPXX -stdlib=libc++ -std=c++1y -g -lgmp -lmpfr -lgmpxx -arch x86_64 -Isrc -I${CGALDIR} test.cc -o test-kernels
./test-kernels
*/
#include "cgal-kernels.h"
#include <CGAL/Polygon_2.h>
#include <CGAL/utils.h>
int main()
{
typedef CGAL::Cartesian<CGAL::Gmpq> K1;
typedef CGAL::Epeck K2;
CGAL::Point_3<K1> p1(1, 2, 3);
CGAL::Point_3<K2> p2(4, 5, 6);
std::cout << "p1: " << CGAL::to_double(p1.x()) << ", " << CGAL::to_double(p1.y()) << ", "
<< CGAL::to_double(p1.z()) << "\n";
std::cout << "p2: " << CGAL::to_double(p2.x()) << ", " << CGAL::to_double(p2.y()) << ", "
<< CGAL::to_double(p2.z()) << "\n";
auto cconv12 = getCartesianConverter<K1, K2>();
auto cconv21 = getCartesianConverter<K2, K1>();
auto p12 = cconv12(p1);
auto p21 = cconv21(p2);
std::cout << "p12: " << CGAL::to_double(p12.x()) << ", " << CGAL::to_double(p12.y()) << ", "
<< CGAL::to_double(p12.z()) << "\n";
std::cout << "p21: " << CGAL::to_double(p21.x()) << ", " << CGAL::to_double(p21.y()) << ", "
<< CGAL::to_double(p21.z()) << "\n";
assert(CGAL::to_double(p12.x()) == 1.0);
assert(CGAL::to_double(p12.y()) == 2.0);
assert(CGAL::to_double(p12.z()) == 3.0);
assert(CGAL::to_double(p21.x()) == 4.0);
assert(CGAL::to_double(p21.y()) == 5.0);
assert(CGAL::to_double(p21.z()) == 6.0);
}
/*
namespace CGAL {
std::size_t hash_value(const mpq_class &x) {
std::hash<double> dh;
return dh(CGAL::to_double(x));
}
std::size_t hash_value(const CGAL::Lazy_exact_nt<mpq_class> &x) {
std::hash<double> dh;
return dh(CGAL::to_double(x));
}
std::size_t hash_value(const CGAL::Lazy_exact_nt<CGAL::Gmpq> &x) {
std::hash<double> dh;
return dh(CGAL::to_double(x));
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment