From 6c125ec50289d07d0920c5f7c20ef7807ea2f5ae Mon Sep 17 00:00:00 2001 From: Werner Hahn Date: Tue, 13 Oct 2026 19:01:52 -0700 Subject: [PATCH] Inline common distance routines --- src/DistRoutines.cpp | 49 ----------------------------------------- src/DistRoutines.h | 52 +++++++++++++++++++++++++++++++++++++++----- 2 files changed, 46 insertions(+), 55 deletions(-) diff --git a/src/DistRoutines.cpp b/src/DistRoutines.cpp index 091d7a3a2d..b83891d00e 100644 --- a/src/DistRoutines.cpp +++ b/src/DistRoutines.cpp @@ -372,31 +372,6 @@ double DIST2_ImageNonOrtho(const double* a1, const double* a2, /** Return the minimum orthorhombic imaged distance^2 between coordinates a1 * and a2. */ -double DIST2_ImageOrtho(const double* a1, const double* a2, Box const& box) -{ - // If box lengths are zero no imaging possible - if (box.Param(Box::X)==0.0 || box.Param(Box::Y)==0.0 || box.Param(Box::Z)==0.0) return -1.0; - double x = a1[0] - a2[0]; - double y = a1[1] - a2[1]; - double z = a1[2] - a2[2]; - // Get rid of sign info - if (x<0) x=-x; - if (y<0) y=-y; - if (z<0) z=-z; - // Get rid of multiples of box lengths - while (x > box.Param(Box::X)) x = x - box.Param(Box::X); - while (y > box.Param(Box::Y)) y = y - box.Param(Box::Y); - while (z > box.Param(Box::Z)) z = z - box.Param(Box::Z); - // Find shortest distance in periodic reference - double D = box.Param(Box::X) - x; - if (D < x) x = D; - D = box.Param(Box::Y) - y; - if (D < y) y = D; - D = box.Param(Box::Z) - z; - if (D < z) z = D; - - return (x*x + y*y + z*z); -} /** Return the minimum orthorhombic imaged distance^2 between coordinates a1 * and a2. @@ -408,15 +383,6 @@ double DIST2_ImageOrtho(Vec3 const& a1, Vec3 const& a2, Box const& box) { // Frame::DIST2_NoImage() /** Return distance^2 between coordinates in a1 and a2. */ -double DIST2_NoImage(const double* a1, const double* a2) { - double x = a1[0] - a2[0]; - double y = a1[1] - a2[1]; - double z = a1[2] - a2[2]; - //double D = x*x + y*y + z*z; - //fprintf(stdout,"Mask1=%8.3f %8.3f %8.3f Mask2=%8.3f %8.3f %8.3f D=%8.3f\n", - // a1[0],a1[1],a1[2],a2[0],a2[1],a2[2],D); - return (x*x + y*y + z*z); -} /// \return Distance squared, no imaging. double DIST2_NoImage( Vec3 const& a1, Vec3 const& a2 ) { @@ -431,21 +397,6 @@ double DIST_NoImage( Vec3 const& a1, Vec3 const& a2 ) { } /** \return Distance squared using either minimum-image convention or no imaging. */ -double DIST2(ImageOption::Type itype, const double* a1, const double* a2, Box const& box) { -/* if (imagingEnabled) { - if (is_ortho) - return DIST2_ImageOrtho(a1, a2, box); - else - return DIST2_ImageNonOrtho(a1, a2, box.UnitCell(), box.FracCell()); - } else - return DIST2_NoImage(a1, a2);*/ - if (itype == ImageOption::NO_IMAGE) - return DIST2_NoImage(a1, a2); - else if (itype == ImageOption::ORTHO) - return DIST2_ImageOrtho(a1, a2, box); - else // NONORTHO - return DIST2_ImageNonOrtho(a1, a2, box.UnitCell(), box.FracCell()); -} /** \return Distance squared using either minimum-image convention or no imaging. */ double DIST2(ImageOption::Type itype, Vec3 const& a1, Vec3 const& a2, Box const& box) { diff --git a/src/DistRoutines.h b/src/DistRoutines.h index 0adf7a73e9..53d39fcd21 100644 --- a/src/DistRoutines.h +++ b/src/DistRoutines.h @@ -1,8 +1,7 @@ #ifndef INC_DISTROUTINES_H #define INC_DISTROUTINES_H -class Vec3; -class Matrix_3x3; -class Box; +#include +#include "Box.h" #include "ImageOption.h" /*! \file DistRoutines.h \brief A collection of routines used to calculate distance. @@ -21,19 +20,60 @@ double DIST2_ImageNonOrtho(Vec3 const&, Vec3 const&, Matrix_3x3 const&, Matrix_3 double DIST2_ImageNonOrtho(const double*, const double*, Matrix_3x3 const&, Matrix_3x3 const&); /// \return Distance squared, X-aligned and orthogonal imaging. -double DIST2_ImageOrtho(const double*, const double*, Box const&); +inline double DIST2_ImageOrtho(const double* a1, const double* a2, + Box const& box) +{ + const double boxX = box.Param(Box::X); + const double boxY = box.Param(Box::Y); + const double boxZ = box.Param(Box::Z); + + if (boxX == 0.0 || boxY == 0.0 || boxZ == 0.0) + return -1.0; + + double x = std::fabs(a1[0] - a2[0]); + double y = std::fabs(a1[1] - a2[1]); + double z = std::fabs(a1[2] - a2[2]); + + while (x > boxX) x -= boxX; + while (y > boxY) y -= boxY; + while (z > boxZ) z -= boxZ; + + double D = boxX - x; + if (D < x) x = D; + D = boxY - y; + if (D < y) y = D; + D = boxZ - z; + if (D < z) z = D; + + return x*x + y*y + z*z; +} /// \return Distance squared, X-aligned and orthogonal imaging. double DIST2_ImageOrtho(Vec3 const&, Vec3 const&, Box const&); /// \return Distance squared, no imaging. -double DIST2_NoImage(const double*, const double*); +inline double DIST2_NoImage(const double* a1, const double* a2) +{ + const double x = a1[0] - a2[0]; + const double y = a1[1] - a2[1]; + const double z = a1[2] - a2[2]; + return x*x + y*y + z*z; +} /// \return Distance squared, no imaging double DIST2_NoImage( Vec3 const&, Vec3 const& ); /// \return Distance, no imaging. double DIST_NoImage( Vec3 const&, Vec3 const& ); /// \return Distance squared using minimum-image convention or no imaging. -double DIST2(ImageOption::Type, const double*, const double*, Box const&); +inline double DIST2(ImageOption::Type itype, const double* a1, + const double* a2, Box const& box) +{ + if (itype == ImageOption::NO_IMAGE) + return DIST2_NoImage(a1, a2); + else if (itype == ImageOption::ORTHO) + return DIST2_ImageOrtho(a1, a2, box); + else + return DIST2_ImageNonOrtho(a1, a2, box.UnitCell(), box.FracCell()); +} // \return Distance squared using minimum-image convention or no imaging. double DIST2(ImageOption::Type, Vec3 const&, Vec3 const&, Box const&); // \return Distance using minimum-image convention or no imaging.