From ae76d04ebf35bde525f7a5683c64df2d1451499e Mon Sep 17 00:00:00 2001
From: Niklaus Johner <nij2003@med.cornell.edu>
Date: Mon, 28 Oct 2013 13:52:49 -0400
Subject: [PATCH] In the PariwiseDistanceMatrix function, added support for
 passing only one EntityView, and computing the lower triangular part of the
 pairwise distance matrix in that case.

Added function to extract the matrix of pairwise distances
    between the atoms in two different views
---
 .../mol/alg/pymod/export_structure_analysis.cc  |  8 +++++++-
 modules/mol/alg/src/structure_analysis.cc       | 17 ++++++++++++++++-
 modules/mol/alg/src/structure_analysis.hh       |  3 ++-
 3 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/modules/mol/alg/pymod/export_structure_analysis.cc b/modules/mol/alg/pymod/export_structure_analysis.cc
index 2a84fe027..2016cb305 100644
--- a/modules/mol/alg/pymod/export_structure_analysis.cc
+++ b/modules/mol/alg/pymod/export_structure_analysis.cc
@@ -24,6 +24,9 @@ using namespace boost::python;
 using namespace ost;
 using namespace ost::mol::alg;
 
+std::vector< std::vector<Real> > (*pair_dist1)(const mol::EntityView&) = PariwiseDistanceMatrix;
+std::vector< std::vector<Real> > (*pair_dist2)(const mol::EntityView&,const mol::EntityView&) = PariwiseDistanceMatrix;
+
 void export_StructureAnalysis()
 {
   def("GetPosListFromView",&GetPosListFromView, (arg("view")));
@@ -32,5 +35,8 @@ void export_StructureAnalysis()
   def("CalculateAgreementWithDensityMap",&CalculateAgreementWithDensityMap,(arg("pos_list"),arg("density_map")));
 #endif
   def("WrapEntityInPeriodicCell",&WrapEntityInPeriodicCell,(arg("Entity"),arg("cell_center"),arg("ucell_size"),arg("ucell_angles")=geom::Vec3(),arg("group_res")=true));
-  def("PariwiseDistanceMatrix",&PariwiseDistanceMatrix,(arg("EntityView1"),arg("EntityView2")));
+  
+
+  def("PariwiseDistanceMatrix",pair_dist2,(arg("EntityView1"),arg("EntityView2")));
+  def("PariwiseDistanceMatrix",pair_dist1,(arg("EntityView")));
 }
diff --git a/modules/mol/alg/src/structure_analysis.cc b/modules/mol/alg/src/structure_analysis.cc
index b8c6f10ba..960acf471 100644
--- a/modules/mol/alg/src/structure_analysis.cc
+++ b/modules/mol/alg/src/structure_analysis.cc
@@ -60,7 +60,7 @@ Real CalculateAverageAgreementWithDensityMap(const geom::Vec3List& vl, img::MapH
   return sum/float(vl.size());
 }
   
-std::vector< std::vector<Real> > PariwiseDistanceMatrix(const EntityView view1, const EntityView view2){
+std::vector< std::vector<Real> > PariwiseDistanceMatrix(const EntityView& view1, const EntityView& view2){
   std::vector< std::vector<Real> > dist_mat;
   unsigned int n1=view1.GetAtomCount(),n2=view2.GetAtomCount();
   dist_mat.resize(n1,std::vector<Real>(n2,0));
@@ -73,6 +73,21 @@ std::vector< std::vector<Real> > PariwiseDistanceMatrix(const EntityView view1,
   return dist_mat;
 }
 
+std::vector< std::vector<Real> > PariwiseDistanceMatrix(const EntityView& view){
+  std::vector< std::vector<Real> > dist_mat;
+  unsigned int n=view.GetAtomCount();
+  //dist_mat.resize(n,std::vector<Real>(n,0));
+  AtomViewList atoms1=view.GetAtomList(),atoms2=view.GetAtomList();
+  for (unsigned int i=0; i!=n; ++i){
+    dist_mat.push_back(std::vector<Real>(n-i-1,0));
+    for (unsigned int j=i+1; j!=n; ++j) {
+      dist_mat[i][j-i-1]=geom::Distance(atoms1[i].GetPos(),atoms2[j].GetPos());
+    }
+  }
+  return dist_mat;
+}
+  
+  
 #endif
 void DLLEXPORT_OST_MOL_ALG WrapEntityInPeriodicCell(EntityHandle eh, const geom::Vec3 cell_center, const geom::Vec3 ucell_size, \
                                                     const geom::Vec3 ucell_angles, bool group_residues){
diff --git a/modules/mol/alg/src/structure_analysis.hh b/modules/mol/alg/src/structure_analysis.hh
index f7cb48540..9cdfa01f5 100644
--- a/modules/mol/alg/src/structure_analysis.hh
+++ b/modules/mol/alg/src/structure_analysis.hh
@@ -38,6 +38,7 @@ namespace ost { namespace mol { namespace alg {
   Real DLLEXPORT_OST_MOL_ALG CalculateAverageAgreementWithDensityMap(const geom::Vec3List& vl, img::MapHandle& density_map);
 #endif
   void DLLEXPORT_OST_MOL_ALG WrapEntityInPeriodicCell(EntityHandle eh, const geom::Vec3 cell_center, const geom::Vec3 ucell_size, const geom::Vec3 ucell_angles=geom::Vec3(), bool group_res=true);
-  std::vector< std::vector<Real> > DLLEXPORT_OST_MOL_ALG PariwiseDistanceMatrix(const EntityView view1, const EntityView view2);
+  std::vector< std::vector<Real> > DLLEXPORT_OST_MOL_ALG PariwiseDistanceMatrix(const EntityView& view1, const EntityView& view2);
+  std::vector< std::vector<Real> > DLLEXPORT_OST_MOL_ALG PariwiseDistanceMatrix(const EntityView& view);
 }}}//ns
 #endif
-- 
GitLab