| Bsoft 2.0.5 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:
30 Vector3<double> v;
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(Vector3<double>& vec, double angle) : next(NULL), v(vec), a(angle) { normalize(); }
43 View(const double x, const double y, const double z, const double angle)
44 : next(NULL), v(x,y,z), a(angle) { // Constructs from 4 values
45 normalize();
46 }
47 // View from another form
48 View(Quaternion& q) : next(NULL) { // Convert from a quaternion
49 double ang = atan2(q[3], q[0]);
50 double ca = cos(ang), sa = sin(ang);
51 double q2 = q[0]*q[0] + q[3]*q[3];
52 double f2 = 2*sqrt(q2);
53 if ( q2 < SMALLFLOAT ) ang = atan2(q[1], q[2]);
54 v[0] = f2*(q[2]*ca + q[1]*sa);
55 v[1] = f2*(q[2]*sa - q[1]*ca);
56 v[2] = 2*q2 - 1;
57 a = 2*ang;
58 normalize();
59 }
60 View(Matrix3 m) : next(NULL) { // Convert from a matrix
61 Quaternion q = m.quaternion();
62 *this = View(q);
63 }
64 View(double tilt, double axis) : next(NULL),
65 v(-sin(axis)*sin(tilt), cos(axis)*sin(tilt),
66 cos(tilt)), a(0) {
67 normalize();
68 }
69 View(double angle, Vector3<double> axis) : next(NULL) {
70 Matrix3 mat = Matrix3(axis, angle);
71 *this = View(mat);
72 }
73 // Operators
74 View operator=(const View& view) { // Copies the contents, not the pointer
75 v = view.v; a = view.a;
76 return *this;
77 }
78 View operator=(const vector<double>& vec) {
79 size_t i;
80 for ( i=0; i<3 && i<vec.size(); ++i ) v[i] = vec[i];
81 if ( i < vec.size() ) a = vec[i];
82 return *this;
83 }
84 View operator-() { // Returns the negative of the view
85 View vn(-v[0], -v[1], -v[2], -a);
86 return vn;
87 }
88 bool operator==(const View& view) {
89 return ( v == view.v ) && ( a == view.a );
90 }
91 double& operator[](size_t i) { if ( i < 3 ) return v[i]; return a; }
92 double x() { return v[0]; }
93 double y() { return v[1]; }
94 double z() { return v[2]; }
95 double angle() { return a; }
96 void x(const double d) { v[0] = d; }
97 void y(const double d) { v[1] = d; }
98 void z(const double d) { v[2] = d; }
99 void angle(const double d) { a = d; }
100 vector<double> array() {
101 return {v[0], v[1], v[2], a};
102 }
103 double vector_size() { // Returns the size of the vector
104 return v.length();
105 }
106 double normalize() {
107 double size = v.length();
108 if ( size < SMALLFLOAT ) { v[0] = v[1] = 0; v[2] = 1; size = 1; }
109 else { v[0] /= size; v[1] /= size; v[2] /= size; }
110 check();
111 return size;
112 }
113 // View conversions
114 void negate() { v = -v; a = -a; } // Negates the view
115 View backward() { // Returns the backwards/inverse form of the view
116 double ca = cos(a);
117 double sa = sin(a);
118 View backview;
119 backview.v[0] = -v[0]*ca - v[1]*sa;
120 backview.v[1] = v[0]*sa - v[1]*ca;
121 backview.v[2] = v[2];
122 backview.a = angle_set_negPI_to_PI(-a);
123 backview.check();
124 return backview;
125 }
126 Vector3<double> vector3() { return v; }
127 Quaternion quaternion() { // Converts to a quaternion
128 normalize();
129 double ang = a/2.0L;
130 double ca = cos(ang), sa = sin(ang);
131 double z1 = v[2] + 1, f1, f2;
132 Quaternion q(ca, 0, 0, sa); // z = 1
133 if ( v[2] < 1.0L - SMALLFLOAT ) {
134 if ( v[2] <= SMALLFLOAT - 1.0L ) { // z = -1
135 q[0] = 0;
136 q[1] = sa;
137 q[2] = ca;
138 q[3] = 0;
139 } else { // -1 < z < 1
140 f1 = sqrt(z1/2.0);
141 f2 = sqrt(1.0/(2*z1));
142 q[0] = f1*ca;
143 q[1] = f2*(v[0]*sa - v[1]*ca);
144 q[2] = f2*(v[0]*ca + v[1]*sa);
145 q[3] = f1*sa;
146 }
147 }
148 return q;
149 }
150 Matrix3 matrix() { // Converts to a matrix
151 Quaternion q = quaternion();
152 return Matrix3(q);
153 }
154 // View comparisons
155 double distance(const View& view) { // Returns the distance between view vectors
156 double d = a - view.a;
157 return sqrt((v - view.v).length2() + d*d);
158 }
159 double angle(const View& view) { // Returns the angle between view vectors
160 double d(v.scalar(view.v));
161 if ( d < 1 && d > -1 ) return acos(d);
162 else if ( d < 0 ) return M_PI;
163 else return 0;
164 }
165 double residual(const View& view) { // Returns a residual difference between views
166 double d = 1 - cos(a - view.a);
167 return sqrt((v - view.v).length2() + d*d)/2;
168 }
169 } ;
170
171 ostream& operator<<(ostream& output, View v);
172
173 #endif
174
175 // Function prototypes
176 int show_views(View* v);
177 View* view_array(View* views, long& n);
178 View* tilt_views(double ang_min, double ang_max, double ang_step, double axis);
179 View random_view();
180 View view_random_reslice();
181 View* random_views(int nviews);
182 double random_view_error(View& v, double std);
183 View* views_within_limits(View theview, double theta_step, double phi_step,
184 double alpha_step, double view_angle_limit, double alpha_angle_limit);
185 View* views_for_refinement(View theview, double alpha_step);
186 View* views_for_refinement(View theview, double alpha_step1, double alpha_step2,
187 double alpha_step3, double max_alpha3);
188 View* view_list_expand_angles(View* views, double amin, double amax, double astep);
189 int view_list_subset(View** view_list, int start, int size);
190
191
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
Vector3::length
double length()
Definition: Vector3.h:222
View::next
View * next
Definition: View.h:28
show_views
int show_views(View *v)
Displays a linked list of views.
Definition: View.cpp:28
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
SMALLFLOAT
#define SMALLFLOAT
Definition: utilities.h:45
utilities.h
Header file for general utilities.
View::z
double z()
Definition: View.h:94
angle_set_negPI_to_PI
double angle_set_negPI_to_PI(double angle)
Returns an angle between -PI and PI.
Definition: utilities.cpp:594
View::View
View(vector< double > &vec)
Definition: View.h:41
Matrix3
Definition: Matrix3.h:17
Quaternion
Definition: Quaternion.h:25
View::operator[]
double & operator[](size_t i)
Definition: View.h:91
View::angle
void angle(const double d)
Definition: View.h:99
TRIGPRECISION
#define TRIGPRECISION
Definition: utilities.h:47
View::y
double y()
Definition: View.h:93
Matrix3::quaternion
Quaternion quaternion() const
Definition: Matrix3.h:129
random_views
View * random_views(int nviews)
Calculates a set of random views.
Definition: View.cpp:147
View::vector_size
double vector_size()
Definition: View.h:103
Matrix3.h
3x3 Matrix manipulation functions
View::z
void z(const double d)
Definition: View.h:98
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
random_view
View random_view()
Calculates a random view.
Definition: View.cpp:101
M_PI
#define M_PI
Definition: utilities.h:59
View::angle
double angle(const View &view)
Definition: View.h:159
View::View
View(const View &view)
Definition: View.h:39
Vector3::scalar
double scalar(const Vector3 &v) const
Definition: Vector3.h:247
View::View
View(double tilt, double axis)
Definition: View.h:64
View::array
vector< double > array()
Definition: View.h:100
View::x
void x(const double d)
Definition: View.h:96
View::View
View(Quaternion &q)
Definition: View.h:48
view_array
View * view_array(View *views, long &n)
Copies views from a list into an array.
Definition: View.cpp:44
View::backward
View backward()
Definition: View.h:115
View::operator-
View operator-()
Definition: View.h:84
Quaternion.h
Class for quaternions.
View::vector3
Vector3< double > vector3()
Definition: View.h:126
View::normalize
double normalize()
Definition: View.h:106
View::View
View(Vector3< double > &vec)
Definition: View.h:40
View::View
View(const double x, const double y, const double z, const double angle)
Definition: View.h:43
view_random_reslice
View view_random_reslice()
Generates a random reslicing 3x3 rotation matrix.
Definition: View.cpp:123
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
random_numbers.h
Header file for functions for creating random images.
View::residual
double residual(const View &view)
Definition: View.h:165
View::matrix
Matrix3 matrix()
Definition: View.h:150
View::quaternion
Quaternion quaternion()
Definition: View.h:127
View::View
View()
Definition: View.h:38
Vector3< double >
View::angle
double angle()
Definition: View.h:95
Vector3.h
Class for 3-value vectors.
View
Definition: View.h:26
View::View
View(double angle, Vector3< double > axis)
Definition: View.h:69
View::operator=
View operator=(const vector< double > &vec)
Definition: View.h:78
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::negate
void negate()
Definition: View.h:114
View::operator==
bool operator==(const View &view)
Definition: View.h:88
View::distance
double distance(const View &view)
Definition: View.h:155
View::operator=
View operator=(const View &view)
Definition: View.h:74
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::x
double x()
Definition: View.h:92
View::y
void y(const double d)
Definition: View.h:97
operator<<
ostream & operator<<(ostream &output, View v)
Definition: View.cpp:17
View::View
View(Matrix3 m)
Definition: View.h:60