From 939a37109fcaf57c67a322a6729bf107be91da08 Mon Sep 17 00:00:00 2001
From: Niklaus Johner <nij2003@med.cornell.edu>
Date: Wed, 21 Mar 2012 15:58:52 -0400
Subject: [PATCH] Added function to wrap Vec3 and Vec3List in a periodic box

---
 modules/geom/pymod/export_vecmat3_op.cc |  4 +++-
 modules/geom/src/vecmat3_op.cc          | 21 +++++++++++++++++++++
 modules/geom/src/vecmat3_op.hh          |  7 +++++++
 3 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/modules/geom/pymod/export_vecmat3_op.cc b/modules/geom/pymod/export_vecmat3_op.cc
index 9e49c8686..b858001fa 100644
--- a/modules/geom/pymod/export_vecmat3_op.cc
+++ b/modules/geom/pymod/export_vecmat3_op.cc
@@ -39,7 +39,7 @@ Real (*Mat3Minor)(const Mat3& m, unsigned int i, unsigned int j)      = &Minor;
 Vec3 (*Vec3Min)(const Vec3&, const Vec3&) = &Min;
 Vec3 (*Vec3Max)(const Vec3&, const Vec3&) = &Max;
 Real (*Vec3Distance2WithPBC)(const Vec3&, const Vec3&, const Vec3&)   = &Distance2WithPBC;
-Real (*Vec3DistanceWithPBC)(const Vec3&, const Vec3&, const Vec3&)   = &DistanceWithPBC;
+Real (*Vec3DistanceWithPBC)(const Vec3&, const Vec3&, const Vec3&)    = &DistanceWithPBC;
 
 void export_VecMat3_op()
 {
@@ -69,4 +69,6 @@ void export_VecMat3_op()
   def("DistanceWithPBC",Vec3DistanceWithPBC);
   def("MinDistance",MinDistance);
   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 ca7497b8c..50b5b612e 100644
--- a/modules/geom/src/vecmat3_op.cc
+++ b/modules/geom/src/vecmat3_op.cc
@@ -227,5 +227,26 @@ Real MinDistanceWithPBC(const Vec3List& l1, const Vec3List& l2, Vec3& basis_vec)
   }
   return std::sqrt(min);
 }  
+
+Vec3 WrapVec3(const Vec3& v1,const Vec3& box_center,const Vec3& basis_vec){
+  Vec3 v;
+  Real r;
+  for (int i=0; i<3; i++) {
+    r=(v1[i]-box_center[i])/basis_vec[i];
+    r=(r > 0.0) ? std::floor(r + 0.5) : std::ceil(r - 0.5);
+    v[i]=v1[i]-basis_vec[i]*r;
+  }
+  return v;
+}
+
+Vec3List WrapVec3List(const Vec3List& vl, const Vec3& box_center,const Vec3& basis_vec){
+  Vec3List vl_out;
+  vl_out.reserve(vl_out.size());
+  for (Vec3List::const_iterator v1=vl.begin(),e=vl.end();v1!=e ; v1++) {
+    vl_out.push_back(WrapVec3(*v1,box_center,basis_vec));
+  }
+  return vl_out;
+}
+  
   
 } // ns
diff --git a/modules/geom/src/vecmat3_op.hh b/modules/geom/src/vecmat3_op.hh
index 53918c2e5..5489ea9e8 100644
--- a/modules/geom/src/vecmat3_op.hh
+++ b/modules/geom/src/vecmat3_op.hh
@@ -215,6 +215,13 @@ Real 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 MinDistanceWithPBC(const Vec3List& l1, const Vec3List& l2, Vec3& basis_vec);
+
+//!wraps a vector in a box with periodic boundaries
+Vec3 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
+Vec3List WrapVec3List(const Vec3List& vl,const Vec3& box_center,const Vec3& basis_vec);
+
+  
 } // ns
 
 #endif
-- 
GitLab