aboutsummaryrefslogtreecommitdiff
path: root/src/utils.c
diff options
context:
space:
mode:
authorThomas White <taw@bitwiz.org.uk>2009-10-13 19:02:20 +0200
committerThomas White <taw@bitwiz.org.uk>2009-10-13 19:02:20 +0200
commita3efcb98a5c165307cc28749e26bffc12ebbf245 (patch)
treeff75387331f67f33b9e65c3484357976e20848d5 /src/utils.c
parentc49cd245a3040617ca75020ebd10a9ea3de420a2 (diff)
Image, feature and unit cell infrastructure
Brought across from DTR and Synth3D
Diffstat (limited to 'src/utils.c')
-rw-r--r--src/utils.c124
1 files changed, 124 insertions, 0 deletions
diff --git a/src/utils.c b/src/utils.c
new file mode 100644
index 00000000..0c7d7ad4
--- /dev/null
+++ b/src/utils.c
@@ -0,0 +1,124 @@
+/*
+ * utils.c
+ *
+ * Utility stuff
+ *
+ * (c) 2006-2009 Thomas White <thomas.white@desy.de>
+ *
+ * template_index - Indexing diffraction patterns by template matching
+ *
+ */
+
+#include <math.h>
+#include <string.h>
+
+#include "utils.h"
+
+
+/* Return the MOST POSITIVE of two numbers */
+unsigned int biggest(signed int a, signed int b)
+{
+ if ( a>b ) {
+ return a;
+ }
+ return b;
+}
+
+
+/* Return the LEAST POSITIVE of two numbers */
+unsigned int smallest(signed int a, signed int b)
+{
+ if ( a<b ) {
+ return a;
+ }
+ return b;
+}
+
+
+double distance(double x1, double y1, double x2, double y2)
+{
+ return sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
+}
+
+
+double modulus(double x, double y, double z)
+{
+ return sqrt(x*x + y*y + z*z);
+}
+
+
+double modulus_squared(double x, double y, double z) {
+ return x*x + y*y + z*z;
+}
+
+
+double distance3d(double x1, double y1, double z1,
+ double x2, double y2, double z2)
+{
+ return modulus(x1-x2, y1-y2, z1-z2);
+}
+
+
+/* Angle between two vectors. Answer in radians */
+double angle_between(double x1, double y1, double z1,
+ double x2, double y2, double z2)
+{
+ double mod1 = modulus(x1, y1, z1);
+ double mod2 = modulus(x2, y2, z2);
+ return acos( (x1*x2 + y1*y2 + z1*z2) / (mod1*mod2) );
+}
+
+
+/* As above, answer in degrees */
+double angle_between_d(double x1, double y1, double z1,
+ double x2, double y2, double z2)
+{
+ return rad2deg(angle_between(x1, y1, z1, x2, y2, z2));
+}
+
+
+/* Wavelength of an electron (in m) given accelerating potential (in V) */
+double lambda(double V)
+{
+ double m = 9.110E-31;
+ double h = 6.625E-34;
+ double e = 1.60E-19;
+ double c = 2.998E8;
+
+ return h / sqrt(2*m*e*V*(1+((e*V) / (2*m*c*c))));
+}
+
+
+size_t skipspace(const char *s)
+{
+ size_t i;
+
+ for ( i=0; i<strlen(s); i++ ) {
+ if ( (s[i] != ' ') && (s[i] != '\t') ) return i;
+ }
+
+ return strlen(s);
+}
+
+
+void chomp(char *s)
+{
+ size_t i;
+
+ if ( !s ) return;
+
+ for ( i=0; i<strlen(s); i++ ) {
+ if ( (s[i] == '\n') || (s[i] == '\r') ) {
+ s[i] = '\0';
+ return;
+ }
+ }
+}
+
+
+int sign(double a)
+{
+ if ( a < 0 ) return -1;
+ if ( a > 0 ) return +1;
+ return 0;
+}