Xmipp  v3.23.11-Nereus
angular_distribution_show.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Carlos Oscar coss@cnb.csic.es (1999)
4  * Roberto Marabini added bild option (2008)
5  *
6  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21  * 02111-1307 USA
22  *
23  * All comments concerning this program package may be sent to the
24  * e-mail address 'xmipp@cnb.csic.es'
25  ***************************************************************************/
26 
27 #include <core/args.h>
28 #include <core/geometry.h>
29 #include <core/histogram.h>
30 #include <core/xmipp_program.h>
31 #include <interface/spider.h>
32 #include <fstream>
33 
35 {
36 public:
39  double R, rmax;
43 
44  void readParams()
45  {
46  fnIn = getParam("-i");
47  fnOut = getParam("-o");
48  type = getParam("-o",1);
49  if (type=="chimera")
50  {
51  R=getDoubleParam("-o",2);
52  rmax=getDoubleParam("-o",3);
53  shift_center=getIntParam("-o",4);
54  }
55  else if (type=="ps")
56  {
57  R=getDoubleParam("-o",2);
58  rmax=getDoubleParam("-o",3);
59  rot_view=getDoubleParam("-o",4);
60  tilt_view=getDoubleParam("-o",5);
61  solid_sphere=getIntParam("-o",6);
62  }
63  else if (type=="histogram")
64  steps = getIntParam("-o",2);
65  up_down_correction = checkParam("--up_down_correction");
66  }
67 
68  void defineParams()
69  {
70  addUsageLine("Shows which is the angular distribution of a set of projections.");
71  addUsageLine("+There are three kinds of outputs: a bild file (chimera understands this format), a distance histogram and a postscript file. ");
72  addUsageLine("+In postcript, each projection is represented by a small isosceles triangles (the longest part is pointing to Y in the projection plane). ");
73  addUsageLine("+In chimera, each projection direction has a sphere whose radius is proportional to the number of images assigned to it");
74  addUsageLine("+The histogram output is the histogram of the minimum distance (in degrees) among projections. The distance is measured on the sphere surface. This gives an idea of how compact is the angular distribution.");
75  addParamsLine(" -i <metadata> : Metadata file with the angles");
76  addParamsLine(" -o <file> <type> : Output file");
77  addParamsLine(" where <type>");
78  addParamsLine(" chimera <R=60> <rmax=1.5> <shift_center=0> : R=sphere radius, rmax=maximum point radius, shift_center=shift in pixels applied to all coordinates");
79  addParamsLine(" : shift_center should be half the volume size and R should be 2/3 the volume size");
80  addParamsLine(" ps <R=60> <rmax=1.5> <rot=0> <tilt=30> <solid_sphere=0> : R=sphere radius, rmax=maximum point radius, rot and tilt defines the point of view, solid_sphere=0 (=no) or 1 (=yes)");
81  addParamsLine(" : If the sphere is solid, projections in the back plane are not shown");
82  addParamsLine(" histogram <stepno=100> : Number of divisions in the histogram");
83  addParamsLine("[--up_down_correction] : correct angles so that a semisphere is shown");
84  }
85 
86  void run()
87  {
88  // Get angles ==============================================================
89  MetaDataVec angles;
90  angles.read(fnIn);
91  size_t AngleNo = angles.size();
92  if (AngleNo == 0 || !angles.containsLabel(MDL_ANGLE_ROT))
93  REPORT_ERROR(ERR_MD_BADLABEL, "Input file doesn't contain angular information");
94 
95  double maxWeight = -99.e99;
96  MultidimArray<double> weight;
97  weight.initZeros(AngleNo);
98  if (angles.containsLabel(MDL_WEIGHT))
99  {
100  // Find maximum weight
101  int i=0;
102  for (size_t objId : angles.ids())
103  {
104  double w;
105  angles.getValue(MDL_WEIGHT,w,objId);
106  DIRECT_A1D_ELEM(weight,i++)=w;
107  maxWeight=XMIPP_MAX(w,maxWeight);
108  }
109  }
110  double imaxWeight=1.0/maxWeight;
111 
112  // Build vector tables ======================================================
113  std::vector< Matrix1D<double> > v, v_ang;
114  v.reserve(AngleNo);
115  v_ang.reserve(AngleNo);
116  Matrix1D<double> aux(3);
117  Matrix1D<double> aux_ang(3);
118  for (size_t objId : angles.ids())
119  {
120  double rot, tilt, psi;
121  angles.getValue(MDL_ANGLE_ROT,rot,objId);
122  angles.getValue(MDL_ANGLE_TILT,tilt,objId);
123  angles.getValue(MDL_ANGLE_PSI,psi,objId);
124  if (up_down_correction && fabs(tilt)>90)
125  \
126  Euler_up_down(rot,tilt,psi,rot,tilt,psi);
127  Euler_direction(rot, tilt, psi, aux);
128  v.push_back(aux);
129  VECTOR_R3(aux_ang, rot, tilt, psi);
130  v_ang.push_back(aux_ang);
131  }
132 
133  if (type=="histogram")
134  {
135  // Compute minimum distance table
137  dist.initZeros(AngleNo);
138  for (size_t i = 0; i < AngleNo; i++)
139  {
140  const Matrix1D<double> &vi=v[i];
141  for (size_t j = i + 1; j < AngleNo; j++)
142  {
143  const Matrix1D<double> &vj=v[j];
144  // Since the two vectors are in the unit sphere, the spherical distance
145  // is simply the arc cosine
146  double d = acos(XX(vi)*XX(vj) + YY(vi)*YY(vj) + ZZ(vi)*ZZ(vj));
147  d=RAD2DEG(d);
148  if (DIRECT_A1D_ELEM(dist,i) == 0 || d < DIRECT_A1D_ELEM(dist,i))
149  DIRECT_A1D_ELEM(dist,i) = d;
150  if (DIRECT_A1D_ELEM(dist,j) == 0 || d < DIRECT_A1D_ELEM(dist,j))
151  DIRECT_A1D_ELEM(dist,j) = d;
152  }
153  }
154 
155  Histogram1D dist_hist;
156  compute_hist(dist, dist_hist, steps);
157  dist_hist.write(fnOut);
158  }
159  else if (type=="chimera")
160  {
161  std::ofstream fh_bild;
162  fh_bild.open(fnOut.c_str(), std::ios::out);
163  if (!fh_bild)
165  fh_bild << ".color 1 0 0" << std::endl;
166 
167  int imax=v.size();
168  for (int i=0; i<imax; i++)
169  {
170  double r=rmax;
171  if (maxWeight>0)
172  r *= DIRECT_A1D_ELEM(weight,i)*imaxWeight;
173  fh_bild
174  << ".sphere "
175  << R*XX(v[i]) + shift_center << " "
176  << R*YY(v[i]) + shift_center << " "
177  << R*ZZ(v[i]) + shift_center << " "
178  << r
179  <<"\n";
180  }
181  fh_bild.close();
182  }
183  else if (type=="ps")
184  {
185  std::ofstream fh_ps;
186  fh_ps.open(fnOut.c_str(), std::ios::out);
187  if (!fh_ps)
189 
190  fh_ps << "%%!PS-Adobe-2.0\n";
191  fh_ps << "%% Creator: Angular Distribution\n";
192  fh_ps << "%% Title: Angular distribution of " << fnIn << "\n";
193  fh_ps << "%% Pages: 1\n";
194 
195 #define TO_PS(x,y) \
196  tmp=y; \
197  y=400.0f-x*250.0f/60; \
198  x=300.0f+tmp*250.0f/60;
199 
200  Matrix1D<double> p0(4), p1(4), p2(4), p3(4), view_direction, pp(3);
201  Matrix2D<double> A, euler_view;
202  Euler_angles2matrix(rot_view, tilt_view, 0., euler_view);
203  euler_view.getRow(2, view_direction);
204  double tmp;
205  int imax=v.size();
206  for (int i=0; i<imax; i++)
207  {
208  double r=rmax;
209  if (maxWeight>0)
210  r *= DIRECT_A1D_ELEM(weight,i)*imaxWeight;
211 
212  // Initially the triangle is on the floor of the projection plane
213  VECTOR_R3(p0, 0 , 0 , 0);
214  VECTOR_R3(p1, 0 , r*2 / 3*SIND(60), 0);
215  VECTOR_R3(p2, r / 2*0.6, -r*1 / 3*SIND(60), 0);
216  VECTOR_R3(p3, -r / 2*0.6, -r*1 / 3*SIND(60), 0);
217 
218  // Convert to homogeneous coordinates
219  p0(3) = 1;
220  p1(3) = 1;
221  p2(3) = 1;
222  p3(3) = 1;
223 
224  // Compute Transformation matrix
225  const Matrix1D<double> &v_angi=v_ang[i];
226  const Matrix1D<double> &vi=v[i];
227  Euler_angles2matrix(VEC_ELEM(v_angi,1), VEC_ELEM(v_angi,2),
228  VEC_ELEM(v_angi,3), A, true);
229  A = A.transpose(); // We go from the projection plane to the universal coordinates
230 
231  // Apply a translation to the sphere of radius R
232  MAT_ELEM(A, 0, 3) = R * XX(vi);
233  MAT_ELEM(A, 1, 3) = R * YY(vi);
234  MAT_ELEM(A, 2, 3) = R * ZZ(vi);
235 
236  // Convert triangle coordinates to universal ones
237  p0 = A * p0;
238  p1 = A * p1;
239  p2 = A * p2;
240  p3 = A * p3;
241 
242  // Check if this triangle must be drawn
243  if (solid_sphere)
244  {
245  // Point-plane distance
246  double d=XX(p0)*XX(view_direction)+
247  YY(p0)*YY(view_direction)+
248  ZZ(p0)*ZZ(view_direction);
249  if (d < 0)
250  continue;
251  }
252 
253  // Project this triangle onto the view plane and write in PS
254  Uproject_to_plane(p1, euler_view, pp);
255  TO_PS(XX(pp), YY(pp));
256  fh_ps << "newpath\n";
257  fh_ps << XX(pp) << " " << YY(pp) << " moveto\n";
258 
259  Uproject_to_plane(p2, euler_view, pp);
260  TO_PS(XX(pp), YY(pp));
261  fh_ps << XX(pp) << " " << YY(pp) << " lineto\n";
262 
263  Uproject_to_plane(p3, euler_view, pp);
264  TO_PS(XX(pp), YY(pp));
265  fh_ps << XX(pp) << " " << YY(pp) << " lineto\n";
266 
267  Uproject_to_plane(p1, euler_view, pp);
268  TO_PS(XX(pp), YY(pp));
269  fh_ps << XX(pp) << " " << YY(pp) << " lineto\n";
270 
271  fh_ps << "closepath\nstroke\n";
272  }
273  fh_ps << "showpage\n";
274  fh_ps.close();
275  }
276  }
277 };
Rotation angle of an image (double,degrees)
#define VEC_ELEM(v, i)
Definition: matrix1d.h:245
#define XMIPP_MAX(x, y)
Definition: xmipp_macros.h:193
double getDoubleParam(const char *param, int arg=0)
void read(const FileName &inFile, const std::vector< MDLabel > *desiredLabels=nullptr, bool decomposeStack=true) override
#define REPORT_ERROR(nerr, ErrormMsg)
Definition: xmipp_error.h:211
void Euler_angles2matrix(T alpha, T beta, T gamma, Matrix2D< T > &A, bool homogeneous)
Definition: geometry.cpp:624
void Euler_direction(double alpha, double beta, double gamma, Matrix1D< double > &v)
Definition: geometry.cpp:721
#define pp(s, x)
Definition: ml2d.cpp:473
Tilting angle of an image (double,degrees)
Couldn&#39;t write to file.
Definition: xmipp_error.h:140
Unexpected label.
Definition: xmipp_error.h:157
Special label to be used when gathering MDs in MpiMetadataPrograms.
doublereal * w
#define TO_PS(x, y)
Matrix2D< T > transpose() const
Definition: matrix2d.cpp:1314
#define SIND(x)
Definition: xmipp_macros.h:347
virtual IdIteratorProxy< false > ids()
size_t size() const override
#define i
doublereal * d
#define MAT_ELEM(m, i, j)
Definition: matrix2d.h:116
#define DIRECT_A1D_ELEM(v, i)
const char * getParam(const char *param, int arg=0)
void compute_hist(const MultidimArrayGeneric &array, Histogram1D &hist, int no_steps)
Definition: histogram.cpp:572
#define XX(v)
Definition: matrix1d.h:85
#define j
#define YY(v)
Definition: matrix1d.h:93
bool getValue(MDObject &mdValueOut, size_t id) const override
#define RAD2DEG(r)
Definition: xmipp_macros.h:320
std::string String
Definition: xmipp_strings.h:34
double psi(const double x)
void write(const FileName &fn, MDLabel=MDL_X, MDLabel=MDL_COUNT)
Definition: histogram.cpp:129
#define VECTOR_R3(v, x, y, z)
Definition: matrix1d.h:124
bool checkParam(const char *param)
void addUsageLine(const char *line, bool verbatim=false)
void initZeros(const MultidimArray< T1 > &op)
bool containsLabel(const MDLabel label) const override
int getIntParam(const char *param, int arg=0)
void getRow(size_t i, Matrix1D< T > &v) const
Definition: matrix2d.cpp:871
void Uproject_to_plane(const Matrix1D< double > &point, const Matrix1D< double > &direction, double distance, Matrix1D< double > &result)
Definition: geometry.cpp:39
#define ZZ(v)
Definition: matrix1d.h:101
void addParamsLine(const String &line)
< Score 4 for volumes