From 3f4d185b1def495fb3a14eaa36940457ce910d94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20M=C3=B6ller?= Date: Fri, 20 Nov 2015 13:39:25 +0100 Subject: [PATCH] Use branchless min/max --- AABB_tree/include/CGAL/AABB_traits.h | 25 +++++++++---------- .../AABB_tree/AABB_ray_intersection.h | 11 ++++---- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/AABB_tree/include/CGAL/AABB_traits.h b/AABB_tree/include/CGAL/AABB_traits.h index 2005febfe71..bd2cc07abb2 100644 --- a/AABB_tree/include/CGAL/AABB_traits.h +++ b/AABB_tree/include/CGAL/AABB_traits.h @@ -122,17 +122,16 @@ struct AABB_traits_base_2{ FT t_near = -DBL_MAX; // std::numeric_limits::lowest(); C++1903 FT t_far = DBL_MAX; - Construct_cartesian_const_iterator_3 construct_cartesian_const_iterator_3 + const Construct_cartesian_const_iterator_3 construct_cartesian_const_iterator_3 = GeomTraits().construct_cartesian_const_iterator_3_object(); - Construct_source_3 construct_source_3 = GeomTraits().construct_source_3_object(); - Construct_vector_3 construct_vector_3 = GeomTraits().construct_vector_3_object(); + const Construct_source_3 construct_source_3 = GeomTraits().construct_source_3_object(); + const Construct_vector_3 construct_vector_3 = GeomTraits().construct_vector_3_object(); const Point_3 source = construct_source_3(ray); const Vector_3 direction = construct_vector_3(ray); Cartesian_const_iterator_3 source_iter = construct_cartesian_const_iterator_3(source); Cartesian_const_iterator_3 direction_iter = construct_cartesian_const_iterator_3(direction); for(int i = 0; i < 3; ++i, ++source_iter, ++direction_iter) { - // we only use i as invariant to safe some checks if(*direction_iter == 0) { if((*source_iter < bbox.min(i)) || (*source_iter > bbox.max(i))) { return boost::none; @@ -141,17 +140,17 @@ struct AABB_traits_base_2{ FT t1 = (bbox.min(i) - *source_iter) / *direction_iter; FT t2 = (bbox.max(i) - *source_iter) / *direction_iter; + t_near = (std::max)(t_near, (std::min)(t1, t2)); + t_far = (std::min)(t_far, (std::max)(t1, t2)); - if(t1 > t2) - std::swap(t1, t2); - if(t1 > t_near) - t_near = t1; - if(t2 < t_far) - t_far = t2; + // if(t1 > t2) + // std::swap(t1, t2); + // if(t1 > t_near) + // t_near = t1; + // if(t2 < t_far) + // t_far = t2; - if(t_near > t_far) - return boost::none; - if(t_far < 0) + if(t_near > t_far || t_far < FT(0.)) return boost::none; } } diff --git a/AABB_tree/include/CGAL/internal/AABB_tree/AABB_ray_intersection.h b/AABB_tree/include/CGAL/internal/AABB_tree/AABB_ray_intersection.h index 04ddf1f2c05..e2b554d6832 100644 --- a/AABB_tree/include/CGAL/internal/AABB_tree/AABB_ray_intersection.h +++ b/AABB_tree/include/CGAL/internal/AABB_tree/AABB_ray_intersection.h @@ -152,10 +152,10 @@ private: struct Node_ptr_with_ft { Node_ptr_with_ft(const Node* node, const FT& value, size_type nb_primitives) - : node(node), value(value), nb_primitives(nb_primitives) {} + : node(node), nb_primitives(nb_primitives), value(value) {} const Node* node; - FT value; size_type nb_primitives; + FT value; bool operator<(const Node_ptr_with_ft& other) const { return value < other.value; } bool operator>(const Node_ptr_with_ft& other) const { return value > other.value; } }; @@ -171,14 +171,13 @@ private: FT operator()(const Point& point) { typename AABB_traits::Geom_traits::Vector_3 x(ray->source(), point); typename AABB_traits::Geom_traits::Vector_3 v = ray->to_vector(); - FT dist = 0; for(int i = 0; i < 3; ++i) { - if(v[0] != 0) { - dist = std::max(dist, x[0] / v[0]); + if(v[0] != FT(0.)) { + return x[0] / v[0]; } } - return dist; + return FT(0.); } const Ray* ray;