Xmipp  v3.23.11-Nereus
phantom_transform.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Carlos Oscar S. Sorzano (coss@cnb.csic.es)
4  *
5  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20  * 02111-1307 USA
21  *
22  * All comments concerning this program package may be sent to the
23  * e-mail address 'xmipp@cnb.csic.es'
24  ***************************************************************************/
25 
26 #include <core/xmipp_program.h>
27 #include <core/geometry.h>
28 #include <data/phantom.h>
29 #include <data/pdb.h>
30 #include "core/transformations.h"
31 
33 {
34 public:
37  double rot, tilt, psi;
38  bool Align_mode;
39  bool Axis_mode;
41  double ang;
44 
46 
47  void defineParams()
48  {
49  addUsageLine("Apply a geometrical transformation to a phantom description or PDB.");
50  addSeeAlsoLine("phantom_create, volume_from_pdb");
51  addParamsLine(" -i <file> : Phantom description file (.descr) or PDB (.pdb)");
52  addParamsLine("[-o <file=\"\">] : Phantom description file (.descr) or PDB (.pdb)");
53  addParamsLine(" : For PDB files, you must explicitly give an output file, different from input");
54  addParamsLine(" --operation <op>: Operation to perform");
55  addParamsLine(" where <op>");
56  addParamsLine(" shift <x> <y> <z> : Shift vector");
57  addParamsLine(" scale <x> <y> <z> : Scale vector");
58  addParamsLine(" rotate_euler <rot> <tilt> <psi> :Rotate with these Euler angles ");
59  addParamsLine(" rotate_align_with_z <x> <y> <z> : Align (x,y,z) with Z");
60  addParamsLine(" rotate_axis <x> <y> <z> <ang>: Rotate <ang> degrees around (x,y,z)");
61  addParamsLine(" [--center_pdb] : Subtract the center of mass from coordinates.");
62  addParamsLine(" :+Only valid for PDB files");
63  addExampleLine("xmipp_phantom_transform -i model.pdb -o shifted.pdb --operation shift 1 2 3");
64  }
65 
66  void readParams()
67  {
68  fn_in = getParam("-i");
69  fn_out = getParam("-o");
70  if (fn_out=="")
71  fn_out=fn_in;
72  PDBmode=fn_in.contains(".pdb");
73  String operation;
74  shift.initZeros(3);
75  scale.resize(3);
76  scale.initConstant(1);
77  A3D.initIdentity(4);
78  Euler_mode = Align_mode = Axis_mode = false;
79  centerPDB = checkParam("--center_pdb");
80  operation=getParam("--operation");
81  if (operation=="shift")
82  {
83  double x=getDoubleParam("--operation",1);
84  double y=getDoubleParam("--operation",2);
85  double z=getDoubleParam("--operation",3);
86  shift=vectorR3(x,y,z);
87  }
88  else if (operation=="scale")
89  {
90  double x=getDoubleParam("--operation",1);
91  double y=getDoubleParam("--operation",2);
92  double z=getDoubleParam("--operation",3);
93  scale=vectorR3(x,y,z);
94  }
95  else if (operation=="rotate_euler")
96  {
97  Euler_mode = true;
98  rot = getDoubleParam("--operation",1);
99  tilt = getDoubleParam("--operation",2);
100  psi = getDoubleParam("--operation",3);
101  Euler_angles2matrix(rot, tilt, psi, A3D, true);
102  }
103  else if (operation=="rotate_align_with_z")
104  {
105  Align_mode = true;
106  double x=getDoubleParam("--operation",1);
107  double y=getDoubleParam("--operation",2);
108  double z=getDoubleParam("--operation",3);
109  axis=vectorR3(x,y,z);
110  alignWithZ(axis, A3D);
111  }
112  else if (operation=="rotate_axis")
113  {
114  Axis_mode = true;
115  double x=getDoubleParam("--operation",1);
116  double y=getDoubleParam("--operation",2);
117  double z=getDoubleParam("--operation",3);
118  ang=getDoubleParam("--operation",4);
119  axis=vectorR3(x,y,z);
120  rotation3DMatrix(ang, axis, A3D);
121  }
122 
123  // Apply shift
124  A3D(0, 3) = XX(shift);
125  A3D(1, 3) = YY(shift);
126  A3D(2, 3) = ZZ(shift);
127 
128  // Apply scale
129  A3D(0, 0) *= XX(scale);
130  A3D(0, 1) *= YY(scale);
131  A3D(0, 2) *= ZZ(scale);
132  A3D(1, 0) *= XX(scale);
133  A3D(1, 1) *= YY(scale);
134  A3D(1, 2) *= ZZ(scale);
135  A3D(2, 0) *= XX(scale);
136  A3D(2, 1) *= YY(scale);
137  A3D(2, 2) *= ZZ(scale);
138  }
139 
140  void show()
141  {
142  if (verbose==0)
143  return;
144  std::cout << "Input file : " << fn_in << std::endl
145  << "Output file: " << fn_out << std::endl;
146  if (PDBmode)
147  std::cout << "Input file is PDB\n";
148  if (centerPDB)
149  std::cout << "Centering PDB\n";
150  if (Euler_mode)
151  std::cout << "Euler angles (rot, tilt, psi): " << rot << " " << tilt
152  << " " << psi << std::endl;
153  else if (Align_mode)
154  std::cout << "Aligning " << axis.transpose() << " with Z\n";
155  else if (Axis_mode)
156  std::cout << "Rotating " << ang << " degrees around "
157  << axis.transpose() << std::endl;
158  std::cout << "Transformation matrix\n" << A3D << std::endl;
159  }
160 
161  void run()
162  {
163  show();
164  if (PDBmode)
165  applyGeometryToPDBFile(fn_in, fn_out, A3D, centerPDB);
166  else
167  {
168  Phantom P;
169  P.read(fn_in, false); // Read phantom without applying scale
170  P.selfApplyGeometry(A3D, xmipp_transformation::IS_NOT_INV);
171  P.write(fn_out);
172  }
173  }
174 };
double getDoubleParam(const char *param, int arg=0)
void applyGeometryToPDBFile(const std::string &fn_in, const std::string &fn_out, const Matrix2D< double > &A, bool centerPDB, const std::string &intensityColumn)
Definition: pdb.cpp:294
void Euler_angles2matrix(T alpha, T beta, T gamma, Matrix2D< T > &A, bool homogeneous)
Definition: geometry.cpp:624
static double * y
Matrix1D< double > vectorR3(double x, double y, double z)
Definition: matrix1d.cpp:892
doublereal * x
Matrix1D< T > transpose() const
Definition: matrix1d.cpp:644
void addSeeAlsoLine(const char *seeAlso)
void rotation3DMatrix(double ang, char axis, Matrix2D< double > &result, bool homogeneous)
const char * getParam(const char *param, int arg=0)
#define XX(v)
Definition: matrix1d.h:85
void write(const FileName &fn_phantom="")
Definition: phantom.cpp:2355
void resize(size_t Xdim, bool copy=true)
Definition: matrix1d.h:410
double z
void addExampleLine(const char *example, bool verbatim=true)
void read(const FileName &fn_phantom, bool apply_scale=true)
Definition: phantom.cpp:2088
int verbose
Verbosity level.
bool contains(const String &str) const
void initZeros()
Definition: matrix1d.h:592
#define YY(v)
Definition: matrix1d.h:93
std::string String
Definition: xmipp_strings.h:34
bool checkParam(const char *param)
void addUsageLine(const char *line, bool verbatim=false)
void alignWithZ(const Matrix1D< double > &axis, Matrix2D< double > &result, bool homogeneous)
void selfApplyGeometry(const Matrix2D< double > &A, int inv)
Definition: phantom.cpp:2493
void initConstant(T val)
Definition: matrix1d.cpp:83
#define ZZ(v)
Definition: matrix1d.h:101
void addParamsLine(const String &line)
void initIdentity()
Definition: matrix2d.h:673