Bsoft  2.1.0
Bernard's software package
View.h
Go to the documentation of this file.
1 
9 #ifndef _View_
10 #define _View_
11 
12 #include "Vector3.h"
13 #include "Matrix3.h"
14 #include "Quaternion.h"
15 #include "random_numbers.h"
16 #include "utilities.h"
17 
18 /************************************************************************
19 @Object: class View
20 @Description:
21  View/orientation parameter structure.
22 @Features:
23  Pointer for a linked list.
24  Composed of a view vector and rotation angle around the vector.
25 *************************************************************************/
26 class View {
27 public:
28  View* next; // Pointer for linked lists
29 private:
31  double a;
32  void check() { // Sets small numbers to zero and adjust the angle to [-PI,PI]
33  for ( size_t i = 0; i<3; i++ ) if ( fabs(v[i]) < TRIGPRECISION ) v[i] = 0;
34  a = angle_set_negPI_to_PI(a);
35  if ( fabs(a) < TRIGPRECISION ) a = 0;
36  }
37 public:
38  View() : next(NULL), v(0,0,1), a(0) { } // Initialize to {0,0,1,0} with a NULL pointer
39  View(const View& view) : next(NULL), v(view.v), a(view.a) { normalize(); } // Copies from another view, not the pointer
40  View(Vector3<double>& vec) : next(NULL), v(vec), a(0) { normalize(); }
41 // View(vector<double>& vec) : next(NULL), v(vec), a(0) { normalize(); }
42  View(vector<double>& vec) : next(NULL), v(vec), a(0) {
43  normalize();
44  if ( vec.size() > 3 ) a = vec[3];
45  }
46  View(const double x, const double y, const double z, const double angle)
47  : next(NULL), v(x,y,z), a(angle) { // Constructs from 4 values
48  normalize();
49  }
50  // View from another form
51  View(Quaternion& q) : next(NULL) { // Convert from a quaternion
52  double ang = atan2(q[3], q[0]);
53  double ca = cos(ang), sa = sin(ang);
54  double q2 = q[0]*q[0] + q[3]*q[3];
55  double f2 = 2*sqrt(q2);
56  if ( q2 < SMALLFLOAT ) ang = atan2(q[1], q[2]);
57  v[0] = f2*(q[2]*ca + q[1]*sa);
58  v[1] = f2*(q[2]*sa - q[1]*ca);
59  v[2] = 2*q2 - 1;
60  a = 2*ang;
61  normalize();
62  }
63  View(Matrix3 m) : next(NULL) { // Convert from a matrix
64  Quaternion q = m.quaternion();
65  *this = View(q);
66  }
67  View(double tilt, double axis) : next(NULL),
68  v(-sin(axis)*sin(tilt), cos(axis)*sin(tilt),
69  cos(tilt)), a(0) {
70  normalize();
71  }
72  View(double angle, Vector3<double> axis) : next(NULL) {
73  Matrix3 mat = Matrix3(axis, angle);
74  *this = View(mat);
75  }
76  // Operators
77  View operator=(const View& view) { // Copies the contents, not the pointer
78  v = view.v; a = view.a;
79  return *this;
80  }
81  View operator=(const vector<double>& vec) {
82  size_t i;
83  for ( i=0; i<3 && i<vec.size(); ++i ) v[i] = vec[i];
84  if ( i < vec.size() ) a = vec[i];
85  return *this;
86  }
87  View operator-() { // Returns the negative of the view
88  View vn(-v[0], -v[1], -v[2], -a);
89  return vn;
90  }
91  bool operator==(const View& view) {
92  return ( v == view.v ) && ( a == view.a );
93  }
94  double& operator[](size_t i) { if ( i < 3 ) return v[i]; return a; }
95  double x() { return v[0]; }
96  double y() { return v[1]; }
97  double z() { return v[2]; }
98  double angle() { return a; }
99  void x(const double d) { v[0] = d; }
100  void y(const double d) { v[1] = d; }
101  void z(const double d) { v[2] = d; }
102  void angle(const double d) { a = d; }
103  vector<double> array() {
104  return {v[0], v[1], v[2], a};
105  }
106  double vector_size() { // Returns the size of the vector
107  return v.length();
108  }
109  double normalize() {
110  double size = v.length();
111  if ( size < SMALLFLOAT ) { v[0] = v[1] = 0; v[2] = 1; size = 1; }
112  else { v[0] /= size; v[1] /= size; v[2] /= size; }
113  check();
114  return size;
115  }
116  // View conversions
117  void negate() { v = -v; a = -a; } // Negates the view
118  View backward() { // Returns the backwards/inverse form of the view
119  double ca = cos(a);
120  double sa = sin(a);
121  View backview;
122  backview.v[0] = -v[0]*ca - v[1]*sa;
123  backview.v[1] = v[0]*sa - v[1]*ca;
124  backview.v[2] = v[2];
125  backview.a = angle_set_negPI_to_PI(-a);
126  backview.check();
127  return backview;
128  }
129  Vector3<double> vector3() { return v; }
130  Quaternion quaternion() { // Converts to a quaternion
131  normalize();
132  double ang = a/2.0L;
133  double ca = cos(ang), sa = sin(ang);
134  double z1 = v[2] + 1, f1, f2;
135  Quaternion q(ca, 0, 0, sa); // z = 1
136  if ( v[2] < 1.0L - SMALLFLOAT ) {
137  if ( v[2] <= SMALLFLOAT - 1.0L ) { // z = -1
138  q[0] = 0;
139  q[1] = sa;
140  q[2] = ca;
141  q[3] = 0;
142  } else { // -1 < z < 1
143  f1 = sqrt(z1/2.0);
144  f2 = sqrt(1.0/(2*z1));
145  q[0] = f1*ca;
146  q[1] = f2*(v[0]*sa - v[1]*ca);
147  q[2] = f2*(v[0]*ca + v[1]*sa);
148  q[3] = f1*sa;
149  }
150  }
151  return q;
152  }
153  Matrix3 matrix() { // Converts to a matrix
154  Quaternion q = quaternion();
155  return Matrix3(q);
156  }
157  // View comparisons
158  double distance(const View& view) { // Returns the distance between view vectors
159  double d = a - view.a;
160  return sqrt((v - view.v).length2() + d*d);
161  }
162  double angle(const View& view) { // Returns the angle between view vectors
163  double d(v.scalar(view.v));
164  if ( d < 1 && d > -1 ) return acos(d);
165  else if ( d < 0 ) return M_PI;
166  else return 0;
167  }
168  double residual(const View& view) { // Returns a residual difference between views
169  double d = 1 - cos(a - view.a);
170  return sqrt((v - view.v).length2() + d*d)/2;
171  }
172 } ;
173 
174 ostream& operator<<(ostream& output, View v);
175 
176 #endif
177 
178 // Function prototypes
179 int show_views(View* v);
180 View* view_array(View* views, long& n);
181 View* tilt_views(double ang_min, double ang_max, double ang_step, double axis);
182 View random_view();
184 View* random_views(int nviews);
185 double random_view_error(View& v, double std);
186 View* views_within_limits(View theview, double theta_step, double phi_step,
187  double alpha_step, double view_angle_limit, double alpha_angle_limit);
188 View* views_for_refinement(View theview, double alpha_step);
189 View* views_for_refinement(View theview, double alpha_step1, double alpha_step2,
190  double alpha_step3, double max_alpha3);
191 View* view_list_expand_angles(View* views, double amin, double amax, double astep);
192 int view_list_subset(View** view_list, int start, int size);
193 
194 
view_list_subset
int view_list_subset(View **view_list, int start, int size)
Replaces a view list with a subset of it.
Definition: View.cpp:474
views_for_refinement
View * views_for_refinement(View theview, double alpha_step)
Generates a 3x3 grid of views around an input view.
Definition: View.cpp:285
View::next
View * next
Definition: View.h:28
View::distance
double distance(const View &view)
Definition: View.h:158
View::operator=
View operator=(const View &view)
Definition: View.h:77
View::operator[]
double & operator[](size_t i)
Definition: View.h:94
Quaternion
Definition: Quaternion.h:25
View::angle
void angle(const double d)
Definition: View.h:102
View::View
View(Matrix3 m)
Definition: View.h:63
View::vector_size
double vector_size()
Definition: View.h:106
random_views
View * random_views(int nviews)
Calculates a set of random views.
Definition: View.cpp:147
Vector3< double >
random_numbers.h
Header file for functions for creating random images.
Matrix3.h
3x3 Matrix manipulation functions
View::y
double y()
Definition: View.h:96
View::View
View(vector< double > &vec)
Definition: View.h:42
View
Definition: View.h:26
Vector3::scalar
double scalar(const Vector3 &v) const
Definition: Vector3.h:249
View::z
void z(const double d)
Definition: View.h:101
View::z
double z()
Definition: View.h:97
view_array
View * view_array(View *views, long &n)
Copies views from a list into an array.
Definition: View.cpp:44
angle_set_negPI_to_PI
double angle_set_negPI_to_PI(double angle)
Returns an angle between -PI and PI.
Definition: utilities.cpp:558
TRIGPRECISION
#define TRIGPRECISION
Definition: utilities.h:48
Matrix3
Definition: Matrix3.h:18
View::backward
View backward()
Definition: View.h:118
View::operator-
View operator-()
Definition: View.h:87
View::x
void x(const double d)
Definition: View.h:99
Quaternion.h
Class for quaternions.
SMALLFLOAT
#define SMALLFLOAT
Definition: utilities.h:46
view_list_expand_angles
View * view_list_expand_angles(View *views, double amin, double amax, double astep)
Expands each view to several views with different rotation angles.
Definition: View.cpp:435
View::angle
double angle(const View &view)
Definition: View.h:162
View::array
vector< double > array()
Definition: View.h:103
Vector3.h
Class for 3-value vectors.
View::normalize
double normalize()
Definition: View.h:109
operator<<
ostream & operator<<(ostream &output, View v)
Definition: View.cpp:17
View::View
View(const double x, const double y, const double z, const double angle)
Definition: View.h:46
views_within_limits
View * views_within_limits(View theview, double theta_step, double phi_step, double alpha_step, double view_angle_limit, double alpha_angle_limit)
Generates a list of views within an angular distance from the input view.
Definition: View.cpp:207
View::View
View(const View &view)
Definition: View.h:39
View::View
View(double tilt, double axis)
Definition: View.h:67
M_PI
#define M_PI
Definition: utilities.h:63
utilities.h
Header file for general utilities.
View::vector3
Vector3< double > vector3()
Definition: View.h:129
View::View
View(Quaternion &q)
Definition: View.h:51
random_view
View random_view()
Calculates a random view.
Definition: View.cpp:101
View::View
View(Vector3< double > &vec)
Definition: View.h:40
View::View
View()
Definition: View.h:38
tilt_views
View * tilt_views(double ang_min, double ang_max, double ang_step, double axis)
Initializes a set of views tilted around the y axis.
Definition: View.cpp:70
View::quaternion
Quaternion quaternion()
Definition: View.h:130
View::View
View(double angle, Vector3< double > axis)
Definition: View.h:72
View::residual
double residual(const View &view)
Definition: View.h:168
View::negate
void negate()
Definition: View.h:117
View::operator==
bool operator==(const View &view)
Definition: View.h:91
View::matrix
Matrix3 matrix()
Definition: View.h:153
Vector3::length
double length()
Definition: Vector3.h:222
view_random_reslice
View view_random_reslice()
Generates a random reslicing 3x3 rotation matrix.
Definition: View.cpp:123
show_views
int show_views(View *v)
Displays a linked list of views.
Definition: View.cpp:28
random_view_error
double random_view_error(View &v, double std)
Calculates a new view with a random error from the given view.
Definition: View.cpp:180
View::operator=
View operator=(const vector< double > &vec)
Definition: View.h:81
View::angle
double angle()
Definition: View.h:98
Matrix3::quaternion
Quaternion quaternion() const
Definition: Matrix3.h:129
View::x
double x()
Definition: View.h:95
View::y
void y(const double d)
Definition: View.h:100