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

replace much boost usage with C++11 equivalents #104

Merged
merged 4 commits into from
Mar 24, 2016
Merged

replace much boost usage with C++11 equivalents #104

merged 4 commits into from
Mar 24, 2016

Conversation

mamoll
Copy link
Member

@mamoll mamoll commented Mar 23, 2016

A first step in eliminating Boost usage. Mostly low-hanging fruit.

// TODO: Replace with constexpr when we migrate to C++11 or
// boost::math::constants::phi<FCL_REAL>() if boost version is greater
// than 1.50.
const FCL_REAL phi = (1.0 + std::sqrt(5.0)) / 2.0; // golden ratio
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we consider using constexpr for phi, a, and b as we're migrating to using C++11?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but sqrt is not allowed in a constexpr (at least not with latest Xcode).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found that none of the math functions in C++11 are not allowed to use with constexpr (gcc allows it, but it seems not to be allowed in C++14). Okay, we can't use constexpr here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could do it constexpr by precalculation (as I suggested for pi) if you can find a very high-precision calculator.

@isucan
Copy link
Contributor

isucan commented Mar 23, 2016

+1 for merging.

@sherm1
Copy link
Member

sherm1 commented Mar 23, 2016

I'll test this on Windows since CI doesn't. Also, Travis failed.

@mamoll
Copy link
Member Author

mamoll commented Mar 23, 2016

Travis is failing probably because of ancient compilers: gcc 4.6 and clang 3.4. I guess the Travis build matrix needs to be updated. We should also set up Windows builds on Appveyor. I'll make the change. I'll wait a bit to give others a chance to respond.

@@ -111,7 +111,7 @@ class RSS
/// @brief Volume of the RSS
inline FCL_REAL volume() const
{
return (l[0] * l[1] * 2 * r + 4 * boost::math::constants::pi<FCL_REAL>() * r * r * r);
return (l[0] * l[1] * 2 * r + 4 * M_PI * r * r * r);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two problems with M_PI:

  • it is a macro and hence untyped; needs to have type FCL_REAL
  • it is non-standard (it is a Posix feature, not a C++11 one)

Since "pi" is the only boost math constant FCL used, I think the simplest thing would be to define an fcl::kPi or whatever that could be defined in a common header with something like

constexpr FCL_REAL kPi=FCL_REAL(/*lots of digits of pi copied from a reliable source*/);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

M_PI caused compilation failures in Visual Studio 2015.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

M_PI works with visual studio if you define _USE_MATH_DEFINES before including cmath

#ifndef _USE_MATH_DEFINES
# define _USE_MATH_DEFINES
#endif

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

M_PI works with visual studio if you define _USE_MATH_DEFINES

Thanks -- I know about that but it's a non-standard hack so I think Mark's fcl::constants::pi idea is better.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good

@jslee02
Copy link
Member

jslee02 commented Mar 23, 2016

Travis is failing probably because of ancient compilers: gcc 4.6 and clang 3.4.

This can be addressed by using trusty environment of Travis.

std::sort(endpoints[2].begin(), endpoints[2].end(), boost::bind(&EndPoint::value, _1) < boost::bind(&EndPoint::value, _2));
std::sort(endpoints[0].begin(), endpoints[0].end());
std::sort(endpoints[1].begin(), endpoints[1].end());
std::sort(endpoints[2].begin(), endpoints[2].end());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks a lot nicer in C++11 than in boost!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually not a C++11-related change. (Well, sort of, s/boost/std/g didn't work.) The simplification is because of the operator< declaration in the Endpoint class.

@sherm1
Copy link
Member

sherm1 commented Mar 23, 2016

This is great, Mark -- it's so nice to see all those boosts disappear! I hope it still works -- the test suite seems kind of short.

@mamoll
Copy link
Member Author

mamoll commented Mar 23, 2016

Does anyone object to defining all mathematical constants (pi and phi) in, say, include/fcl/math/constants.h in the fcl::constants namespace as hard-coded double values:

namespace fcl
{
   namespace constants
   {
       constexpr FCL_REAL pi = 3.14159....;
       constexpr FCL_REAL phi = 1.618....;
   }
}

@isucan
Copy link
Contributor

isucan commented Mar 23, 2016

I think that's a good idea!

@jslee02
Copy link
Member

jslee02 commented Mar 23, 2016

I was considering letting the compiler do the calculation of phi in compile like this, but @mamoll 's idea looks much simpler!

@@ -1,3 +1,6 @@
# force C++11 mode
add_definitions(-std=c++11)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This causes zillions of warnings on Windows -- that should only be added for gcc & clang.

@sherm1
Copy link
Member

sherm1 commented Mar 23, 2016

The top-level CMakeLists.txt specifies boost components here. Can you remove any of those with this PR?


/// @brief Fitting rule to fit a BV node to a set of geometry primitives
boost::shared_ptr<BVFitterBase<BV> > bv_fitter;
std::shared_ptr<BVFitterBase<BV> > bv_fitter;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a change to the public API, should we bump the major version number?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I know, we might don't need to bump the major version number because FCL is still in 0.y.z.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, we should at least bump the minor version then

@scpeters
Copy link
Contributor

I personally prefer separating big changes into smaller pull requests, but I'll defer to the crowd here.

@@ -58,7 +58,7 @@
#include <map>
#include <string>
#include <iostream>
#include <boost/thread.hpp>
#include <thread>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add #include <mutex> here -- compile failed on Windows. C++11 doesn't promise that mutex will be defined in thread.

@sherm1
Copy link
Member

sherm1 commented Mar 23, 2016

With these changes, this PR builds on Windows VS 2015 Win64, with only the same 4 test failures I saw before:

  • defined pi
  • commented out "-std=c++11"
  • included <mutex>

@sherm1
Copy link
Member

sherm1 commented Mar 23, 2016

I like the constants proposal! I suggest using higher-precision constants that will get rounded down to FCL_REAL, to make sure the last bit is correct. For example:

constexpr FCL_REAL pi = FCL_REAL(3.141592653589793238462643383279502884197169399375105820974944592L);

That is a long double constant because of the trailing L.

…ode per compiler (MS VS doesn't like -std=c++11). eliminate some boost components in CMakeLists.txt
@mamoll
Copy link
Member Author

mamoll commented Mar 23, 2016

I have hopefully addressed the main concerns. After this is merged, I'll fix the Travis CI config.

@scpeters
Copy link
Contributor

There's still packaging issues:

  • bump minor version number
  • export c++11 flags in pkgconfig / cmake config files

@scpeters
Copy link
Contributor

I guess those can be addressed later too?

@mamoll
Copy link
Member Author

mamoll commented Mar 23, 2016

I can fix the pkgconfig / cmake config files, but wouldn't it make more sense to bump the version when you actually release? There's still quite a bit of boost stuff to eliminate.

@mamoll
Copy link
Member Author

mamoll commented Mar 23, 2016

Actually, what cmake config files are you talking about? I thought you meant files like fcl-config.cmake, but this is currently not generated by FCL.

@scpeters
Copy link
Contributor

You're right; there is no cmake config file. I just assumed this had one.

I like to bump the version number in the master branch (CMakeModules/FCLVersion.cmake) when breaking changes happen. This way people building from source can see a difference by the version number. I consider the package released when a new tag has been created.

@scpeters
Copy link
Contributor

Thanks!

@jslee02
Copy link
Member

jslee02 commented Mar 23, 2016

export c++11 flags in pkgconfig / cmake config files

Actually, FCL has a pkgconfig file. Can we simply add -std=c++11 like gazebo does?

Edit: Never mind. @mamoll already added in 65b948e.

@mamoll
Copy link
Member Author

mamoll commented Mar 23, 2016

Apparently, MSVC doesn't like -std=c++11. This is why I add the flag only when not building with MSVC.

@sherm1
Copy link
Member

sherm1 commented Mar 23, 2016

I have hopefully addressed the main concerns. After this is merged, I'll fix the Travis CI config.

Mark, can you fix Travis as part of this PR so we can confirm that it passes? I think that just means changing .travis.yml as a commit with the PR.

@sherm1
Copy link
Member

sherm1 commented Mar 23, 2016

Reviewed changes -- looks great! Ready to merge pending build success.

@scpeters
Copy link
Contributor

I think adding the following to .travis.yml would fix it:

sudo: required
dist: trusty

since trusty has a c++11 compiler

…t system library: test programs link against it
@mamoll mamoll merged commit a22c65d into flexible-collision-library:master Mar 24, 2016
@jslee02 jslee02 added this to the FCL 0.5.0 milestone Mar 24, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants