Xmipp  v3.23.11-Nereus
angular_rotate.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Authors: Joaquin Oton (joton@cnb.csic.es)
3  *
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/geometry.h"
27 #include "core/matrix2d.h"
28 #include "core/metadata_vec.h"
29 #include "core/transformations.h"
30 #include "core/xmipp_program.h"
31 #include "core/xmipp_filename.h"
32 
34 {
35 protected:
40 
41  void defineParams()
42  {
43  addUsageLine("Calculate the Euler angles after applying a rotation to a geometry set from a metadata.");
44  addKeywords("angular, geometry, rotate");
45  addSeeAlsoLine("transform_geometry");
46 
47  addParamsLine(" -i <metadata> : Input metadata file");
48  addParamsLine(" [-o <metadata>] : Output metadata file, if not provided result will overwrite input file");
49  addParamsLine(" --ang <angle> : Rotate in plane");
50  addParamsLine(" or --euler <rot> <tilt> <psi> : Rotate with these Euler angles");
51  addParamsLine(" or --alignZ <x> <y> <z> : Align (x,y,z) with Z axis");
52  addParamsLine(" or --axis <ang> <x=0> <y=0> <z=1> : Rotate <ang> degrees around (x,y,z)");
53  addParamsLine("[--write_matrix] : Print transformation matrix to screen");
54 
55  addExampleLine("Modify the geometry of a selfile applying a rotation of 0.1 degrees around (x,y,z)=(1,1,1) direction:", false);
56  addExampleLine("xmipp_angular_rotate -i selfile.sel -o newselfile.sel --axis 0.1 1 1 1");
57 
58  }
59 
60  void readParams()
61  {
62  fnIn = getParam("-i");
63  fnOut = (checkParam("-o"))? getParam("-o") : fnIn;
64 
65  mdIn.read(fnIn);
66  int dim;
67 
69  dim = 3;
70  else
71  dim = 2;
72 
73  R.initIdentity(dim + 1);
74  B.initIdentity(dim + 1);
75  Matrix1D<double> xyz(3);
76 
77  if (checkParam("--ang"))
78  Euler_angles2matrix(0., 0.,getDoubleParam("--ang"), R, true);
79  else if (checkParam("--axis"))
80  {
81  double ang = getDoubleParam("--axis", 0);
82  XX(xyz) = getDoubleParam("--axis", 1); //axis x
83  YY(xyz) = getDoubleParam("--axis", 2); //y
84  ZZ(xyz) = getDoubleParam("--axis", 3);//z
85  rotation3DMatrix(ang, xyz, R, true);
86  }
87  else if (checkParam("--euler"))
88  {
89  XX(xyz) = getDoubleParam("--euler", 0); //rot
90  YY(xyz) = getDoubleParam("--euler", 1); //tilt
91  ZZ(xyz) = getDoubleParam("--euler", 2);//psi
92  Euler_angles2matrix(XX(xyz), YY(xyz), ZZ(xyz), R, true);
93  }
94  else if (checkParam("--alignZ"))
95  {
96  XX(xyz) = getDoubleParam("--alignZ", 0); //rot
97  YY(xyz) = getDoubleParam("--alignZ", 1); //tilt
98  ZZ(xyz) = getDoubleParam("--alignZ", 2);//psi
99  alignWithZ(xyz, R);
100  }
101 
102  writeMatrix = checkParam("--write_matrix");
103  }
104 
105  void run()
106  {
107  bool containsScale=mdIn.containsLabel(MDL_SCALE);
108  for (auto& input : mdIn)
109  {
110  geo2TransformationMatrix(input, B);
111 
112  T = R * B;
113 
114  transformationMatrix2Geo(T, input);
115  if (!containsScale)
116  input.setValue(MDL_SCALE,1.0);
117  mdOut.addRow(dynamic_cast<MDRowVec&>(input));
118 
119  if (writeMatrix)
120  std::cout << T << std::endl;
121  }
122 
123  mdOut.write(fnOut);
124  }
125 };
double getDoubleParam(const char *param, int arg=0)
void read(const FileName &inFile, const std::vector< MDLabel > *desiredLabels=nullptr, bool decomposeStack=true) override
void geo2TransformationMatrix(const MDRow &imageGeo, Matrix2D< double > &A, bool only_apply_shifts)
void transformationMatrix2Geo(const Matrix2D< double > &A, MDRow &imageGeo)
void Euler_angles2matrix(T alpha, T beta, T gamma, Matrix2D< T > &A, bool homogeneous)
Definition: geometry.cpp:624
Tilting angle of an image (double,degrees)
Special label to be used when gathering MDs in MpiMetadataPrograms.
void write(const FileName &outFile, WriteModeMetaData mode=MD_OVERWRITE) const
size_t addRow(const MDRow &row) override
void addSeeAlsoLine(const char *seeAlso)
void rotation3DMatrix(double ang, char axis, Matrix2D< double > &result, bool homogeneous)
void addKeywords(const char *keywords)
const char * getParam(const char *param, int arg=0)
#define XX(v)
Definition: matrix1d.h:85
Matrix2D< double > R
Matrix2D< double > B
void addExampleLine(const char *example, bool verbatim=true)
scaling factor for an image or volume (double)
#define YY(v)
Definition: matrix1d.h:93
bool checkParam(const char *param)
Matrix2D< double > T
void addUsageLine(const char *line, bool verbatim=false)
bool containsLabel(const MDLabel label) const override
void alignWithZ(const Matrix1D< double > &axis, Matrix2D< double > &result, bool homogeneous)
#define ZZ(v)
Definition: matrix1d.h:101
void addParamsLine(const String &line)
void initIdentity()
Definition: matrix2d.h:673