Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Commit

Permalink
Merge pull request #157 from sloriot/CGAL_gmpxx_compatibility
Browse files Browse the repository at this point in the history
Compatibility with gmpxx
  • Loading branch information
vmora authored Apr 5, 2018
2 parents 5499795 + e2d656b commit b632e6c
Show file tree
Hide file tree
Showing 19 changed files with 228 additions and 163 deletions.
10 changes: 6 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
language: cpp
language: cpp
dist: trusty
sudo: required

# os:
# - linux
Expand All @@ -9,14 +11,14 @@ compiler:
- clang

env:
- CGAL_VERSION=4.3
- CGAL_VERSION=4.7
- CGAL_VERSION=4.10.2
- CGAL_VERSION=4.11.1

before_install:
- ./travis/${TRAVIS_OS_NAME}/before_install.sh $CGAL_VERSION

before_script:
- cmake -DSFCGAL_BUILD_TESTS=ON
- CGAL_DIR=/usr/local/lib/CGAL cmake -DSFCGAL_BUILD_TESTS=ON

script:
- make
Expand Down
93 changes: 0 additions & 93 deletions cmake/Modules/FindCGAL.cmake

This file was deleted.

8 changes: 4 additions & 4 deletions src/Coordinate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,10 @@ class RoundVisitor : public boost::static_visitor<> {

Kernel::FT _roundFT( const Kernel::FT& v ) const {
#ifdef CGAL_USE_GMPXX
return Kernel::FT( ::mpq_class(
SFCGAL::round( v.exact() * _scaleFactor ),
_scaleFactor
) ) ;
::mpq_class q( SFCGAL::round( v.exact() * _scaleFactor ),
_scaleFactor) ;
q.canonicalize();
return Kernel::FT(q);
#else
return Kernel::FT( CGAL::Gmpq(
SFCGAL::round( v.exact() * _scaleFactor ),
Expand Down
48 changes: 48 additions & 0 deletions src/detail/io/Serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,5 +169,53 @@ void load( boost::archive::binary_iarchive& ar, CGAL::Gmpz& z, const unsigned in
}
}

#ifdef CGAL_USE_GMPXX
void save( boost::archive::text_oarchive& ar, const mpz_class& z, const unsigned int /*version*/ )
{
std::ostringstream ostr;
ostr << z;
std::string str = ostr.str();
ar << str;
}

// specialization for binary archives
void save ( boost::archive::binary_oarchive& ar, const mpz_class& z, const unsigned int/* version*/ )
{
mpz_srcptr mpz = z.get_mpz_t();
int32_t size = mpz->_mp_size;
ar& size;
uint32_t rsize = size >= 0 ? size : -size;

for ( uint32_t i = 0; i < rsize; ++i ) {
ar& mpz->_mp_d[i];
}
}


void load( boost::archive::text_iarchive& ar, mpz_class& z, const unsigned int /*version*/ )
{
std::string line;
ar >> line;
std::istringstream istr( line );
istr >> z;
}

void load( boost::archive::binary_iarchive& ar, mpz_class& z, const unsigned int /*version*/ )
{
int32_t size;
uint32_t rsize;
mpz_ptr mpz = z.get_mpz_t();
ar& size;
rsize = size >= 0 ? size : -size;
mpz->_mp_size = size;
_mpz_realloc( mpz, rsize );
uint32_t i;

for ( i = 0; i < rsize; ++i ) {
ar& mpz->_mp_d[i];
}
}
#endif

}
}
61 changes: 61 additions & 0 deletions src/detail/io/Serialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#ifdef CGAL_USE_GMPXX
#include <CGAL/mpz_class.h>
#include <CGAL/mpq_class.h>
#endif

namespace SFCGAL {

Expand Down Expand Up @@ -132,6 +136,63 @@ void serialize( Archive& ar, CGAL::Gmpq& q, const unsigned int version )
split_free( ar, q, version );
}

#ifdef CGAL_USE_GMPXX
/**
* Serialization of mpz_class for text archives
*/
SFCGAL_API void save( boost::archive::text_oarchive& ar, const mpz_class& z, const unsigned int version );

/**
* Serialization of mpz_class for binary archives
*/
SFCGAL_API void save ( boost::archive::binary_oarchive& ar, const mpz_class& z, const unsigned int version );

/**
* Unserialization of mpz_class for text archives
*/
SFCGAL_API void load( boost::archive::text_iarchive& ar, mpz_class& z, const unsigned int version );

/**
* Unserialization of mpz_class for binary archives
*/
SFCGAL_API void load( boost::archive::binary_iarchive& ar, mpz_class& z, const unsigned int version );

template<class Archive>
void serialize( Archive& ar, mpz_class& z, const unsigned int version )
{
split_free( ar, z, version );
}

/**
* Serializer of mpq_class
*/
template<class Archive>
void save( Archive& ar, const mpq_class& q, const unsigned int /*version*/ )
{
mpz_class n = q.get_num();
mpz_class d = q.get_den();
ar& n;
ar& d;
}

/**
* Unserializer of mpq_class
*/
template<class Archive>
void load( Archive& ar, mpq_class& q, const unsigned int /*version*/ )
{
mpz_class n;
mpz_class d;
ar& n;
ar& d;
q = mpq_class( n, d );
}
template<class Archive>
void serialize( Archive& ar, mpq_class& q, const unsigned int version )
{
split_free( ar, q, version );
}
#endif

/**
* Serializer of Kernel::FT
Expand Down
4 changes: 2 additions & 2 deletions src/detail/io/WktReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ bool WktReader::readPointCoordinate( Point& p )
}

p = Point( coordinates[0], coordinates[1], coordinates[2] );
p.setM( coordinates[3].to_double() );
p.setM( CGAL::to_double(coordinates[3]) );
}
else if ( _isMeasured && ! _is3D ) {
// XYM
Expand All @@ -656,7 +656,7 @@ bool WktReader::readPointCoordinate( Point& p )
}

p = Point( coordinates[0], coordinates[1] );
p.setM( coordinates[2].to_double() );
p.setM( CGAL::to_double(coordinates[2]) );
}
else if ( coordinates.size() == 3 ) {
// XYZ
Expand Down
22 changes: 20 additions & 2 deletions src/detail/io/WktWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ namespace SFCGAL {
namespace detail {
namespace io {

namespace impl {
std::ostream& writeFT(std::ostream& s, const CGAL::Gmpq& ft)
{
s << ft;
return s;
}

#ifdef CGAL_USE_GMPXX
std::ostream& writeFT(std::ostream& s, const mpq_class& ft)
{
s << ft.get_num() << "/" << ft.get_den();
return s;
}
#endif
} //end of impl namespace

///
///
///
Expand Down Expand Up @@ -135,10 +151,12 @@ void WktWriter::writeCoordinateType( const Geometry& g )
void WktWriter::writeCoordinate( const Point& g )
{
if ( _exactWrite ) {
_s << CGAL::exact( g.x() ) << " " << CGAL::exact( g.y() );
impl::writeFT(_s, CGAL::exact( g.x() )) << " ";
impl::writeFT(_s, CGAL::exact( g.y() ));

if ( g.is3D() ) {
_s << " " << CGAL::exact( g.z() );
_s << " ";
impl::writeFT(_s, CGAL::exact( g.z() ));
}
}
else {
Expand Down
2 changes: 1 addition & 1 deletion src/detail/tools/InputStreamReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class BasicInputStreamReader {
skipWhiteSpaces();
}

if ( _s >> value ) {
if( _s >> CGAL::iformat(value) ) {
commit();
return true ;
}
Expand Down
42 changes: 42 additions & 0 deletions src/numeric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,48 @@ CGAL::Gmpz round( const CGAL::Gmpq& v )
}
}

#ifdef CGAL_USE_GMPXX
///
///
///
mpz_class floor( const mpq_class& v )
{
return v.get_num() / v.get_den() ;
}

///
///
///
mpz_class ceil( const mpq_class& v )
{
mpz_class result( 0 ) ;
mpz_cdiv_q( result.get_mpz_t(), v.get_num().get_mpz_t(), v.get_den().get_mpz_t() ) ;
return result ;
}

///
///
///
mpz_class round( const mpq_class& v )
{
if ( v < 0 ) {
//ceil( v - 0.5 ) ;
mpq_class tmp = v - mpq_class( 1,2 );
return ceil( tmp );
}
else if ( v == 0 ) {
return 0 ;
}
else {
//floor( v + 0.5 ) ;
mpq_class tmp = v + mpq_class( 1,2 );
return floor( tmp );
}
}
#endif



}//SFCGAL


8 changes: 3 additions & 5 deletions test/garden/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ add_executable( ${REGRESS_NAME} ${SFCGAL_REGRESS_GARDEN_TEST_SOURCES} )

target_link_libraries( ${REGRESS_NAME}
SFCGAL
${Boost_LIBRARIES}
${CGAL_LIBRARIES}
CGAL::CGAL
CGAL::CGAL_Core
)
if( ${SFCGAL_WITH_MPFR} )
target_link_libraries( ${REGRESS_NAME} ${MPFR_LIBRARIES} )
endif( ${SFCGAL_WITH_MPFR} )
target_link_libraries( ${REGRESS_NAME} ${CGAL_3RD_PARTY_LIBRARIES} )

set_target_properties( ${REGRESS_NAME} PROPERTIES DEBUG_POSTFIX "d" )
install( TARGETS ${REGRESS_NAME} DESTINATION bin )
Expand Down
Loading

0 comments on commit b632e6c

Please sign in to comment.