From b0e7e5b90929e85cb9a11bec7a4601c3a91f8375 Mon Sep 17 00:00:00 2001
From: Gerardo Tauriello <gerardo.tauriello@unibas.ch>
Date: Thu, 9 Aug 2018 20:10:45 +0200
Subject: [PATCH] SCHWED-3473: Revert round changes. Use C++ version of
 functions.

---
 modules/config/base.hh                        | 38 +++----------------
 modules/gfx/src/impl/cartoon_renderer.cc      |  2 +-
 modules/gfx/src/vertex_array.cc               |  2 +-
 .../gui/src/data_viewer/drawing_functions.cc  | 12 +++---
 modules/img/alg/src/polar.cc                  |  6 +--
 modules/img/base/src/image_list.cc            |  4 +-
 modules/img/base/src/point.hh                 | 16 ++++----
 .../img/base/src/raster_image/raster_image.cc |  4 +-
 modules/info/src/item_type_cast.hh            |  2 +-
 modules/io/src/formatted_line.hh              |  2 +-
 modules/io/src/img/map_io_tiff_handler.cc     |  4 +-
 modules/io/tests/test_mmcif_reader.cc         |  3 +-
 modules/mol/alg/src/entity_to_density.cc      |  6 +--
 modules/mol/alg/src/filter_clashes.cc         |  2 +-
 modules/mol/base/src/spatial_organizer.hh     |  6 +--
 modules/mol/mm/src/modeller.cc                |  6 +--
 16 files changed, 44 insertions(+), 71 deletions(-)

diff --git a/modules/config/base.hh b/modules/config/base.hh
index ade55f5fc..b10638e7e 100644
--- a/modules/config/base.hh
+++ b/modules/config/base.hh
@@ -53,38 +53,13 @@ typedef unsigned short Word;
 
 typedef std::string String;
 
-#ifndef round_real_function
-#define round_real_function
-#ifndef round_real
-inline Real round_real(Real d) 
-{
-  return floor(d+Real(0.5));
-}
-#endif
-#endif
-
-#ifndef round_float_function
-#define round_float_function
-#ifndef round_float
-inline float round_float(float d) 
-{
-  return (float)floor(d+float(0.5));
-}
-#endif
-#endif
-
-#ifndef round_double_function
-#define round_double_function
-#ifndef round_double
-inline double round_double(double d) 
-{
-  return (double)floor(d+double(0.5));
-}
-#endif
-#endif
-
-
+// NOTE: Before OST 1.8, we used to have round and rint functions defined here
+// -> round and rint are available for any compiler since many years now
+// -> Tested for GCC 4.1.2 - 9.0.0, clang 3.3.0 - 8.0.0, MSVC 2015 - 2017 using
+//    godbolt.org. In all cases: call with float is not casted to double, but
+//    kept as float which is desired behaviour for good performance.
 
+// NOTE: OST has not been tested in MSVC for a very long time!
 #if _MSC_VER
 #pragma warning(disable:4251)
 #pragma warning(disable:4275)
@@ -116,5 +91,4 @@ inline double log2( double n )
 #endif
 
 
-
 #endif
diff --git a/modules/gfx/src/impl/cartoon_renderer.cc b/modules/gfx/src/impl/cartoon_renderer.cc
index 060668cce..00894483d 100644
--- a/modules/gfx/src/impl/cartoon_renderer.cc
+++ b/modules/gfx/src/impl/cartoon_renderer.cc
@@ -380,7 +380,7 @@ void CartoonRenderer::rebuild_spline_obj(IndexedVertexArray& va,
         double ang=-geom::SignedAngle(geom::Cross(slist.at(sc-1).normal,dir),
                                       geom::Cross(slist.at(sc).normal,dir),
                                       dir);
-        offset=psize+static_cast<size_t>(round_real(ang*of)+psized);
+        offset=psize+static_cast<size_t>(round(ang*of)+psized);
       }
       if(slist.at(sc).type==3) {
         if(slist.at(sc-1).type!=3) {
diff --git a/modules/gfx/src/vertex_array.cc b/modules/gfx/src/vertex_array.cc
index b175a96a7..847e38d14 100644
--- a/modules/gfx/src/vertex_array.cc
+++ b/modules/gfx/src/vertex_array.cc
@@ -508,7 +508,7 @@ namespace {
   unsigned int col_to_index(float* c)
   {
     // don't look too closely - I am lacking sufficient caffeine to do this more elegantly
-    int ret= std::max(0,std::min<int>(511,static_cast<int>(round_real(c[0]*7.0f))*64+static_cast<int>(round_real(c[1]*7.0f))*8+static_cast<unsigned int>(round_real(c[2]*7.0f))));
+    int ret= std::max(0,std::min<int>(511,static_cast<int>(round(c[0]*7.0f))*64+static_cast<int>(round(c[1]*7.0f))*8+static_cast<unsigned int>(round(c[2]*7.0f))));
     return static_cast<unsigned int>(ret);
   }
 }
diff --git a/modules/gui/src/data_viewer/drawing_functions.cc b/modules/gui/src/data_viewer/drawing_functions.cc
index 324c29af9..f27b3eb0d 100644
--- a/modules/gui/src/data_viewer/drawing_functions.cc
+++ b/modules/gui/src/data_viewer/drawing_functions.cc
@@ -64,7 +64,7 @@ void DrawEllipse(QPainter& pnt, const QPoint& center, Real rx, Real ry, Real ang
     Real ang=static_cast<Real>(j)/static_cast<Real>(numpoints)*2.0*M_PI;
     Real dx=rx*cos(ang);
     Real dy=ry*sin(ang);
-    qpoly << QPoint(static_cast<int>(round_real(center.x()+dx*cos(-angle)+dy*sin(-angle))),static_cast<int>(round_real(center.y()-dx*sin(-angle)+dy*cos(-angle))));
+    qpoly << QPoint(static_cast<int>(round(center.x()+dx*cos(-angle)+dy*sin(-angle))),static_cast<int>(round(center.y()-dx*sin(-angle)+dy*cos(-angle))));
   }
   pnt.drawPolyline(qpoly);
 }
@@ -86,13 +86,13 @@ void DrawHyperbola(QPainter& pnt, const QSize& size, const geom::Vec2& center, R
       x=sqrt((y*y/ry/ry-1.0)*rx*rx);
     }
     geom::Vec2 pos=Rotate(Vec2(x,y),-angle)+center;
-    p1=QPoint(static_cast<int>(round_real(pos[0])),static_cast<int>(round_real(pos[1])));
+    p1=QPoint(static_cast<int>(round(pos[0])),static_cast<int>(round(pos[1])));
     pos=Rotate(Vec2(x,-y),-angle)+center;
-    p2=QPoint(static_cast<int>(round_real(pos[0])),static_cast<int>(round_real(pos[1])));
+    p2=QPoint(static_cast<int>(round(pos[0])),static_cast<int>(round(pos[1])));
     pos=Rotate(Vec2(-x,y),-angle)+center;
-    p3=QPoint(static_cast<int>(round_real(pos[0])),static_cast<int>(round_real(pos[1])));
+    p3=QPoint(static_cast<int>(round(pos[0])),static_cast<int>(round(pos[1])));
     pos=Rotate(Vec2(-x,-y),-angle)+center;
-    p4=QPoint(static_cast<int>(round_real(pos[0])),static_cast<int>(round_real(pos[1])));
+    p4=QPoint(static_cast<int>(round(pos[0])),static_cast<int>(round(pos[1])));
     if(orientation){
       if(flagp1){
         points1.push_back(p1);
@@ -150,7 +150,7 @@ void DrawHalfEllipse(QPainter& pnt, const QPoint& center, Real rx, Real ry, Real
     Real ang=j/static_cast<Real>(numpoints)*M_PI+M_PI;
     Real dx=rx*cos(ang);
     Real dy=ry*sin(ang);
-    qpoly << QPoint(static_cast<int>(round_real(center.x()+dx*cos(-angle)+dy*sin(-angle))),static_cast<int>(round_real(center.y()-dx*sin(-angle)+dy*cos(-angle))));
+    qpoly << QPoint(static_cast<int>(round(center.x()+dx*cos(-angle)+dy*sin(-angle))),static_cast<int>(round(center.y()-dx*sin(-angle)+dy*cos(-angle))));
   }
   pnt.drawPolyline(qpoly);
 }
diff --git a/modules/img/alg/src/polar.cc b/modules/img/alg/src/polar.cc
index dc77c0655..aa5fa3745 100644
--- a/modules/img/alg/src/polar.cc
+++ b/modules/img/alg/src/polar.cc
@@ -36,7 +36,7 @@ public:
   ImageStateBasePtr VisitState(const img::ImageStateImpl<T,D>& isi) {
     Size s=isi.GetExtent().GetSize();
 //    unsigned int rmax=static_cast<unsigned int>(std::min(s[0],s[1])*0.5);
-    unsigned int rmax=static_cast<unsigned int>(round_real(sqrt(s[0]*s[0]+s[1]*s[1])*0.5));
+    unsigned int rmax=static_cast<unsigned int>(round(sqrt(s[0]*s[0]+s[1]*s[1])*0.5));
     Extent e(Size(rmax,static_cast<unsigned int>(rmax*2.0*M_PI*sampling_)));
     std::cerr << e << std::endl;
     boost::shared_ptr<ImageStateImpl<T,image_state::SpatialDomain> > result(new ImageStateImpl<T,image_state::SpatialDomain>(e,isi.GetSampling()));
@@ -76,7 +76,7 @@ ImageHandle Polar::Visit(const ConstImageHandle& i)
 #else
   Size s=i.GetExtent().GetSize();
 //  unsigned int rmax=static_cast<unsigned int>(std::min(s[0],s[1])*0.5);
-  unsigned int rmax=static_cast<unsigned int>(round_double(sqrt(static_cast<double>(s[0]*s[0]+s[1]*s[1]))*0.5));
+  unsigned int rmax=static_cast<unsigned int>(round(sqrt(static_cast<double>(s[0]*s[0]+s[1]*s[1]))*0.5));
   Extent e(Size(rmax,static_cast<unsigned int>(rmax*2.0*M_PI*sampling_)));
   ImageHandle result = CreateImage(e,i.GetType(),SPATIAL);
   Real prefac=1/(rmax*sampling_);
@@ -103,7 +103,7 @@ ImageHandle EllipticPolar::Visit(const ConstImageHandle& i)
   Size s=i.GetExtent().GetSize();
   Real ang=atan(static_cast<double>(s[1]/s[0]));
   Real mdist=std::min(Length(ellipse.AtAngle(ang)),Length(ellipse.AtAngle(-ang)));
-  unsigned int rmax=static_cast<unsigned int>(round_double(sqrt(static_cast<double>(s[0]*s[0]+s[1]*s[1]))*0.5));
+  unsigned int rmax=static_cast<unsigned int>(round(sqrt(static_cast<double>(s[0]*s[0]+s[1]*s[1]))*0.5));
   Extent e(Size(rmax,static_cast<unsigned int>(rmax*2.0*M_PI*sampling_)));
   ImageHandle result = CreateImage(e,i.GetType(),SPATIAL);
   Real prefac=1/(rmax*sampling_);
diff --git a/modules/img/base/src/image_list.cc b/modules/img/base/src/image_list.cc
index a0f5365f1..522e5f7af 100644
--- a/modules/img/base/src/image_list.cc
+++ b/modules/img/base/src/image_list.cc
@@ -43,9 +43,9 @@ ImageHandle ImageList::GetGallery(unsigned int columns,bool border)
       bsize=1;
     }
     if(columns==0){
-        columns=static_cast<int>(round_real(sqrt(static_cast<Real>(size()))));
+        columns=static_cast<int>(round(sqrt(static_cast<Real>(size()))));
     }
-    unsigned int rows=static_cast<int>(round_real(static_cast<Real>(size())/static_cast<Real>(columns)));
+    unsigned int rows=static_cast<int>(round(static_cast<Real>(size())/static_cast<Real>(columns)));
     Size s=get_max_size_();
     DataType datatype=get_data_type_();
     if(s[2]>1){
diff --git a/modules/img/base/src/point.hh b/modules/img/base/src/point.hh
index 079530385..0d9f307e7 100644
--- a/modules/img/base/src/point.hh
+++ b/modules/img/base/src/point.hh
@@ -87,8 +87,8 @@ public:
   
   //! conversion from Vec2
   explicit Point(const Vec2& v):
-    x(static_cast<int>(round_real(v[0]))),
-    y(static_cast<int>(round_real(v[1]))),
+    x(static_cast<int>(round(v[0]))),
+    y(static_cast<int>(round(v[1]))),
     z()
   {
   } 
@@ -96,18 +96,18 @@ public:
 
   //! conversion from Vec3
   explicit Point(const Vec3& v):
-    x(static_cast<int>(round_real(v[0]))),
-    y(static_cast<int>(round_real(v[1]))),
-    z(static_cast<int>(round_real(v[2])))
+    x(static_cast<int>(round(v[0]))),
+    y(static_cast<int>(round(v[1]))),
+    z(static_cast<int>(round(v[2])))
   {
   } 
 
 
   //! conversion from Vec4 (normalization)
   explicit Point(const Vec4& v):
-    x(static_cast<int>(round_real(v[0]))),
-    y(static_cast<int>(round_real(v[1]))),
-    z(static_cast<int>(round_real(v[2])))
+    x(static_cast<int>(round(v[0]))),
+    y(static_cast<int>(round(v[1]))),
+    z(static_cast<int>(round(v[2])))
   {
     if(std::abs(v[3])<1e-100) {
       throw geom::OutOfRangeException("4th element of Vec4 is too close to zero for normalization");
diff --git a/modules/img/base/src/raster_image/raster_image.cc b/modules/img/base/src/raster_image/raster_image.cc
index febbe0314..143e1110b 100644
--- a/modules/img/base/src/raster_image/raster_image.cc
+++ b/modules/img/base/src/raster_image/raster_image.cc
@@ -191,7 +191,7 @@ RasterImage::Pixel get_value_t(const Data& d, const Point& p, const geom::Vec3&
       if(signcolor) vv = std::abs(vv);
       rv = n->Convert(vv);
     }
-    uchar v = (unsigned char)::round_real(rv);
+    uchar v = (unsigned char)::round(rv);
     if(signcolor) {
       float hsv[3];
       hsv[0] = sg ? 0.0 : 120.0;
@@ -268,7 +268,7 @@ RasterImage::Pixel isi_to_pixel(const ImageStateImpl<T,D>& isi,
   } else {
     rv = n->Convert(Val2Val<T,Real>(isi.Value(p)));
   }
-  uchar v = (unsigned char)::round_real(rv);
+  uchar v = (unsigned char)::round(rv);
   return RasterImage::Pixel(v,v,v);
 }
 
diff --git a/modules/info/src/item_type_cast.hh b/modules/info/src/item_type_cast.hh
index 89c0aedfa..0b364d575 100644
--- a/modules/info/src/item_type_cast.hh
+++ b/modules/info/src/item_type_cast.hh
@@ -163,7 +163,7 @@ template <> void set_new_type<IT_FLOAT,IT_STRING>(EleImpl& item)
 
 template <> void set_new_type<IT_FLOAT,IT_INT>(EleImpl& item)
 {
-  item.SetIntRepr(static_cast<int>(round_real(item.GetFloatRepr())));
+  item.SetIntRepr(static_cast<int>(round(item.GetFloatRepr())));
 }
 
 template <> void set_new_type<IT_FLOAT,IT_FLOAT>(EleImpl& item)
diff --git a/modules/io/src/formatted_line.hh b/modules/io/src/formatted_line.hh
index 40772bc18..444a4e4aa 100644
--- a/modules/io/src/formatted_line.hh
+++ b/modules/io/src/formatted_line.hh
@@ -88,7 +88,7 @@ struct LPaddedFloat {
         len = snprintf(data, sizeof(data), "%.3f", val);
         break;
       default:
-        Real rounded_val=round_real(val*pow(Real(10), prec))*pow(Real(0.1), prec);
+        Real rounded_val=round(val*pow(Real(10), prec))*pow(Real(0.1), prec);
         size_t curr=0;
         bool minus=rounded_val<0;
         rounded_val=std::abs(rounded_val);
diff --git a/modules/io/src/img/map_io_tiff_handler.cc b/modules/io/src/img/map_io_tiff_handler.cc
index b47429642..cd0ad364c 100644
--- a/modules/io/src/img/map_io_tiff_handler.cc
+++ b/modules/io/src/img/map_io_tiff_handler.cc
@@ -847,11 +847,11 @@ void MapIOTiffHandler::load_image_data(TIFF* tfile, img::ImageHandle& image,  co
     break;
   case RESUNIT_INCH:
     image.SetPixelSampling(geom::Vec3(xreso*Units::in,yreso*Units::in,1));
-    image.SetSpatialOrigin(img::Point(static_cast<int>(round_float(xpos/xreso)),static_cast<int>(round_float(ypos/yreso)),0));
+    image.SetSpatialOrigin(img::Point(static_cast<int>(round(xpos/xreso)),static_cast<int>(round(ypos/yreso)),0));
     break;
   case RESUNIT_CENTIMETER:
     image.SetPixelSampling(geom::Vec3(xreso*Units::cm,yreso*Units::cm,1));
-    image.SetSpatialOrigin(img::Point(static_cast<int>(round_float(xpos/xreso)),static_cast<int>(round_float(ypos/yreso)),0));
+    image.SetSpatialOrigin(img::Point(static_cast<int>(round(xpos/xreso)),static_cast<int>(round(ypos/yreso)),0));
     break;
   }
   _TIFFfree(buf);
diff --git a/modules/io/tests/test_mmcif_reader.cc b/modules/io/tests/test_mmcif_reader.cc
index 6c97689dc..69210a1b9 100644
--- a/modules/io/tests/test_mmcif_reader.cc
+++ b/modules/io/tests/test_mmcif_reader.cc
@@ -1327,8 +1327,7 @@ BOOST_AUTO_TEST_CASE(mmcif_test_chain_mappings)
   BOOST_TEST_MESSAGE("  Running mmcif_test_chain_mappings tests...");
   
   // check compound lib
-  bool compound_lib_available = 
-  static_cast<bool>(SetDefaultCompoundLib());
+  bool compound_lib_available = static_cast<bool>(SetDefaultCompoundLib());
 
   // load data
   mol::EntityHandle eh = mol::CreateEntity();
diff --git a/modules/mol/alg/src/entity_to_density.cc b/modules/mol/alg/src/entity_to_density.cc
index 002b7ff1e..b6ac49efc 100644
--- a/modules/mol/alg/src/entity_to_density.cc
+++ b/modules/mol/alg/src/entity_to_density.cc
@@ -307,9 +307,9 @@ public:
 
             geom::Vec3 adjusted_coord  = coord-map_start;
             geom::Vec3 pixel_coord=is.CoordToIndex(coord);
-            img::Point rounded_pixel_coord(round_real(pixel_coord[0]),
-                                            round_real(pixel_coord[1]),
-                                            round_real(pixel_coord[2]));
+            img::Point rounded_pixel_coord(round(pixel_coord[0]),
+                                            round(pixel_coord[1]),
+                                            round(pixel_coord[2]));
 
             uint x_limit = ceil(2.0*four_sigma/sampling[0])+1;
             uint y_limit = ceil(2.0*four_sigma/sampling[1])+1;
diff --git a/modules/mol/alg/src/filter_clashes.cc b/modules/mol/alg/src/filter_clashes.cc
index fee4dfa96..2f2c68b92 100644
--- a/modules/mol/alg/src/filter_clashes.cc
+++ b/modules/mol/alg/src/filter_clashes.cc
@@ -118,7 +118,7 @@ std::pair<Real,Real> ClashingDistances::GetClashingDistance(const String& ele1,c
   std::map <String,std::pair<Real,Real> >::const_iterator find_ci= min_distance_.find(key);
   if (find_ci == min_distance_.end()) {
       std::stringstream serr;
-      serr << "Entry for distance " << key <<  " not found in the parameter table";   
+      serr << "Entry for distance " << key <<  " not found in the parameter table";
       throw Error(serr.str());
   }    
   return find_ci->second;
diff --git a/modules/mol/base/src/spatial_organizer.hh b/modules/mol/base/src/spatial_organizer.hh
index c48c0f293..0e9dce740 100644
--- a/modules/mol/base/src/spatial_organizer.hh
+++ b/modules/mol/base/src/spatial_organizer.hh
@@ -215,9 +215,9 @@ private:
   Index max_;
 
   Index gen_index(const VEC& pos) const {
-    Index nrvo(static_cast<int>(round_real(pos[0]/delta_)),
-               static_cast<int>(round_real(pos[1]/delta_)),
-               static_cast<int>(round_real(pos[2]/delta_)));
+    Index nrvo(static_cast<int>(round(pos[0]/delta_)),
+               static_cast<int>(round(pos[1]/delta_)),
+               static_cast<int>(round(pos[2]/delta_)));
     return nrvo;
   }
 
diff --git a/modules/mol/mm/src/modeller.cc b/modules/mol/mm/src/modeller.cc
index 03f1f72c4..1a9da3094 100644
--- a/modules/mol/mm/src/modeller.cc
+++ b/modules/mol/mm/src/modeller.cc
@@ -91,9 +91,9 @@ void Modeller::LowerPrecision(ost::mol::EntityHandle& handle){
   for(ost::mol::AtomHandleList::iterator i = atom_list.begin();
       i != atom_list.end(); ++i){
     pos = i->GetPos();
-    truncated_pos[0] = Real(round_real(pos[0]*100))/100;
-    truncated_pos[1] = Real(round_real(pos[1]*100))/100;
-    truncated_pos[2] = Real(round_real(pos[2]*100))/100;
+    truncated_pos[0] = Real(round(pos[0]*100))/100;
+    truncated_pos[1] = Real(round(pos[1]*100))/100;
+    truncated_pos[2] = Real(round(pos[2]*100))/100;
     ed.SetAtomPos(*i,truncated_pos);
   }
   ed.UpdateICS();
-- 
GitLab