From f5ff3298b29b1f772b2561b5af9021d01baf7bfc Mon Sep 17 00:00:00 2001 From: BRUNO LATHUILIERE Date: Wed, 21 Feb 2024 12:58:25 +0100 Subject: [PATCH] avoid multiple division in calculatePolygonBarycenter : more accurate (if no overflow) and fastr --- src/INTERP_KERNEL/TransformedTriangle.cxx | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/INTERP_KERNEL/TransformedTriangle.cxx b/src/INTERP_KERNEL/TransformedTriangle.cxx index 65ada4f2c..4b1892045 100644 --- a/src/INTERP_KERNEL/TransformedTriangle.cxx +++ b/src/INTERP_KERNEL/TransformedTriangle.cxx @@ -646,20 +646,19 @@ namespace INTERP_KERNEL const std::size_t m = polygon.size(); for(int j = 0 ; j < 3 ; ++j) - { - barycenter[j] = 0.0; - } + barycenter[j] = 0.0; + + for(std::size_t i = 0 ; i < m ; ++i) + { + const double* pt = polygon[i]; + for(int j = 0 ; j < 3 ; ++j) + barycenter[j] += pt[j]; + } if(m != 0) { - for(std::size_t i = 0 ; i < m ; ++i) - { - const double* pt = polygon[i]; - for(int j = 0 ; j < 3 ; ++j) - { - barycenter[j] += pt[j] / double(m); - } - } + for(int j = 0 ; j < 3 ; ++j) + barycenter[j] = barycenter[j] / double(m); } LOG(3,"Barycenter is " << vToStr(barycenter)); } -- 2.39.2