From a16376e05450322a300bd17e4bb4a0f0fb815474 Mon Sep 17 00:00:00 2001
From: Niklaus Johner <nij2003@med.cornell.edu>
Date: Fri, 31 May 2013 09:55:21 -0400
Subject: [PATCH] Added a function, geom::MinDistanceIndices, to find the
 indices of the Vec3 from two Vec3List that are closest.

---
 modules/geom/pymod/export_vecmat3_op.cc |  1 +
 modules/geom/src/vecmat3_op.cc          | 23 +++++++++++++++++++++++
 modules/geom/src/vecmat3_op.hh          |  5 ++++-
 3 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/modules/geom/pymod/export_vecmat3_op.cc b/modules/geom/pymod/export_vecmat3_op.cc
index b858001fa..5c06a2499 100644
--- a/modules/geom/pymod/export_vecmat3_op.cc
+++ b/modules/geom/pymod/export_vecmat3_op.cc
@@ -68,6 +68,7 @@ void export_VecMat3_op()
   def("Distance2WithPBC",Vec3Distance2WithPBC);
   def("DistanceWithPBC",Vec3DistanceWithPBC);
   def("MinDistance",MinDistance);
+  def("MinDistanceIndices",MinDistanceIndices);
   def("MinDistanceWithPBC",MinDistanceWithPBC);
   def("WrapVec3",WrapVec3);
   def("WrapVec3List",WrapVec3List);
diff --git a/modules/geom/src/vecmat3_op.cc b/modules/geom/src/vecmat3_op.cc
index 8a1debee8..2653a081f 100644
--- a/modules/geom/src/vecmat3_op.cc
+++ b/modules/geom/src/vecmat3_op.cc
@@ -228,6 +228,29 @@ Real MinDistanceWithPBC(const Vec3List& l1, const Vec3List& l2, Vec3& basis_vec)
   return std::sqrt(min);
 }  
 
+std::vector<unsigned int> MinDistanceIndices(const Vec3List& l1, const Vec3List& l2)
+{ 
+  // returns the indices index1, index2 corresponding to the points in
+  // the Vec3List l1 and l2 having the minimal distance.
+  if (l1.size()==0 || l2.size()==0){throw GeomException("cannot calculate minimal distance: empty Vec3List");}
+  Real min=Length2(*l1.begin()-*l2.begin());
+  Real d;
+  std::vector<unsigned int> il;
+  il.push_back(0);
+  il.push_back(0);
+  for (unsigned int i=0;i!=l1.size();i++){
+    for (unsigned int j=0;j!=l2.size();j++){
+      d=Length2(l1[i]-l2[j]);
+      if (d<min){
+        min=d;
+        il[0]=i;
+        il[1]=j;
+      }
+    }
+  }
+  return il;
+}
+  
 Vec3 WrapVec3(const Vec3& v1,const Vec3& box_center,const Vec3& basis_vec){
   Vec3 v;
   Real r;
diff --git a/modules/geom/src/vecmat3_op.hh b/modules/geom/src/vecmat3_op.hh
index e13c2f076..b4a18d3cb 100644
--- a/modules/geom/src/vecmat3_op.hh
+++ b/modules/geom/src/vecmat3_op.hh
@@ -215,7 +215,10 @@ Real DLLEXPORT_OST_GEOM MinDistance(const Vec3List& l1, const Vec3List& l2);
 //! returns the minimal distance between the points in two Vec3List 
 //  with periodic boundaries in x,y,z given in basis_vec
 Real DLLEXPORT_OST_GEOM MinDistanceWithPBC(const Vec3List& l1, const Vec3List& l2, Vec3& basis_vec);
-
+//! returns the indices index1, index2 corresponding to the points in
+//! the Vec3List l1 and l2 having the minimal distance.
+std::vector<unsigned int> DLLEXPORT_OST_GEOM MinDistanceIndices(const Vec3List& l1, const Vec3List& l2);
+  
 //!wraps a vector in a box with periodic boundaries
 Vec3 DLLEXPORT_OST_GEOM WrapVec3(const Vec3& v1,const Vec3& box_center,const Vec3& basis_vec);
 //!wraps all the verctors in a Vec3List in a box with periodic boundaries
-- 
GitLab