From a23842e399f1a7c07c0047d519651ac07498e886 Mon Sep 17 00:00:00 2001
From: Ansgar Philippsen <ansgar.philippsen@gmail.com>
Date: Fri, 13 Jul 2012 11:45:02 -0400
Subject: [PATCH] added in-place coord group transform; fixed
 Transform::ApplyAxisRotation

---
 modules/mol/base/src/coord_group.cc        | 11 ++++++++++-
 modules/mol/base/src/coord_group.hh        |  6 +++++-
 modules/mol/base/src/coord_source.cc       | 21 ++++++++++++++++-----
 modules/mol/base/src/coord_source.hh       |  4 ++++
 modules/mol/base/src/transform.cc          |  2 +-
 modules/mol/base/tests/test_coord_group.cc | 12 ++++++++++++
 6 files changed, 48 insertions(+), 8 deletions(-)

diff --git a/modules/mol/base/src/coord_group.cc b/modules/mol/base/src/coord_group.cc
index bd56a4e00..4fe6677ee 100644
--- a/modules/mol/base/src/coord_group.cc
+++ b/modules/mol/base/src/coord_group.cc
@@ -254,6 +254,15 @@ CoordGroupHandle CoordGroupHandle::Filter(const EntityView& selected, int first,
   }
   return filtered_cg;
 }
- 
+
+void CoordGroupHandle::ApplyTransform(const mol::Transform& tf)
+{
+  this->CheckValidity();
+  if (source_->IsMutable()) {
+    source_->ApplyTransform(tf);
+  } else {
+    throw IntegrityError("Cannot apply transform, CoordGroup is immutable");
+  }  
+} 
   
 }} // ns
diff --git a/modules/mol/base/src/coord_group.hh b/modules/mol/base/src/coord_group.hh
index bf049aadd..74fffcb6c 100644
--- a/modules/mol/base/src/coord_group.hh
+++ b/modules/mol/base/src/coord_group.hh
@@ -30,6 +30,8 @@
 
 namespace ost { namespace mol {
 
+class Transform;
+
 /// \brief coordinate group, for trajectories and such
 class DLLEXPORT_OST_MOL CoordGroupHandle {
 public:
@@ -105,7 +107,9 @@ public:
   /// \brief return a filtered coord group, containing only the atoms in the 
   ///     view
   CoordGroupHandle Filter(const EntityView& selected,int first=0,int last=-1) const;
-  
+
+  /// \brief apply in-place transform to each coordinate in each frame
+  void ApplyTransform(const Transform& tf);
 
 
 private:
diff --git a/modules/mol/base/src/coord_source.cc b/modules/mol/base/src/coord_source.cc
index 335117423..06b7864b7 100644
--- a/modules/mol/base/src/coord_source.cc
+++ b/modules/mol/base/src/coord_source.cc
@@ -21,13 +21,12 @@
   Author: Marco Biasini
  */
 #include <ost/log.hh>
-#include <ost/mol/atom_handle.hh>
-#include <ost/mol/xcs_editor.hh>
-#include <ost/mol/in_mem_coord_source.hh>
-
+#include "atom_handle.hh"
+#include "xcs_editor.hh"
+#include "in_mem_coord_source.hh"
+#include "transform.hh"
 #include "coord_source.hh"
 
-
 namespace ost { namespace mol {
   
 
@@ -166,4 +165,16 @@ geom::Vec3 CoordSource::GetAtomPos(uint frame, AtomHandle atom) const
   return geom::Vec3();
 }
 
+void CoordSource::ApplyTransform(const Transform& tf)
+{
+  if(!mutable_) return;
+  size_t frame_count=GetFrameCount();
+  for(size_t n=0;n<frame_count;++n) {
+    CoordFramePtr cfp=GetFrame(n);
+    for(CoordFrame::iterator it=cfp->begin();it!=cfp->end();++it) {
+      *it=tf.Apply(*it);
+    }
+  }
+}
+
 }} // ns
diff --git a/modules/mol/base/src/coord_source.hh b/modules/mol/base/src/coord_source.hh
index a50f256a1..a33162abc 100644
--- a/modules/mol/base/src/coord_source.hh
+++ b/modules/mol/base/src/coord_source.hh
@@ -31,6 +31,7 @@
 namespace ost { namespace mol {
 
 class CoordSource;
+class Transform;
 
 typedef boost::shared_ptr<CoordSource> CoordSourcePtr;
 
@@ -80,6 +81,9 @@ public:
   virtual void AddFrame(const std::vector<geom::Vec3>& coords) = 0;
   virtual void AddFrame(const std::vector<geom::Vec3>& coords,const geom::Vec3& cell_size,const geom::Vec3& cell_angles) = 0;
   virtual void InsertFrame(int pos, const std::vector<geom::Vec3>& coords) = 0;
+
+  void ApplyTransform(const Transform& tf);
+
 protected:
   void SetMutable(bool flag);
 private:
diff --git a/modules/mol/base/src/transform.cc b/modules/mol/base/src/transform.cc
index cb76e7196..ba1e59258 100644
--- a/modules/mol/base/src/transform.cc
+++ b/modules/mol/base/src/transform.cc
@@ -122,7 +122,7 @@ void Transform::ApplyZAxisRotation(float delta)
 
 void Transform::ApplyAxisRotation(float delta, const Vec3& axis)
 {
-  rot_=rot_*AxisRotation(rot_*axis, delta*P_180);
+  rot_=rot_*AxisRotation(axis, delta*P_180);
   update_tm();
 }
 
diff --git a/modules/mol/base/tests/test_coord_group.cc b/modules/mol/base/tests/test_coord_group.cc
index 7e3d38598..3c0cdc18f 100644
--- a/modules/mol/base/tests/test_coord_group.cc
+++ b/modules/mol/base/tests/test_coord_group.cc
@@ -80,6 +80,18 @@ BOOST_AUTO_TEST_CASE(coord_group)
   BOOST_CHECK(ab.GetPos()==geom::Vec3(-14,-15,-16));
   BOOST_CHECK(ac.GetPos()==geom::Vec3(-17,-18,-19));
   BOOST_CHECK(ad.GetPos()==geom::Vec3(9,10,11));
+
+  Transform tf;
+  tf.ApplyXAxisRotation(17.0);
+  tf.ApplyYAxisRotation(-135.0);
+  tf.ApplyZAxisRotation(234.0);
+  tf.SetCenter(geom::Vec3(-7.3,1.2,5.5));
+  tf.SetTrans(geom::Vec3(14.5,-87.1,22.2));
+
+  cg.ApplyTransform(tf);
+  for(size_t i=0;i<3;++i) {
+    BOOST_CHECK_CLOSE(geom::Distance(cg.GetAtomPos(1,alist[i]),tf.Apply(clist[i])),Real(0),1e-6);
+  }
 }
 
 BOOST_AUTO_TEST_SUITE_END();
-- 
GitLab