]> Pileus Git - grits/commitdiff
Add vector operations to utilities
authorAndy Spencer <andy753421@gmail.com>
Sat, 22 Jan 2011 19:57:15 +0000 (19:57 +0000)
committerAndy Spencer <andy753421@gmail.com>
Sat, 22 Jan 2011 19:57:15 +0000 (19:57 +0000)
src/grits-util.c
src/grits-util.h

index a792014b97335570d61f89d8325d049443352a9f..26135964e0c5d6f6080e22fb1273b280f80d974d 100644 (file)
@@ -235,3 +235,71 @@ gdouble lon_avg(gdouble a, gdouble b)
        }
        return avg;
 }
        }
        return avg;
 }
+
+/**
+ * crossd3:
+ * @a:   the left   point
+ * @b:   the center point
+ * @c:   the right  point
+ * @out: the cross product
+ *
+ * Calculate the cross product of three points
+ */
+void crossd3(gdouble *a, gdouble *b, gdouble *c, gdouble *out)
+{
+       double ba[3], bc[3];
+
+       ba[0] = a[0] - b[0];
+       ba[1] = a[1] - b[1];
+       ba[2] = a[2] - b[2];
+
+       bc[0] = c[0] - b[0];
+       bc[1] = c[1] - b[1];
+       bc[2] = c[2] - b[2];
+
+       crossd(ba, bc, out);
+}
+
+/**
+ * crossd:
+ * @a: the first vector
+ * @b: the second vector
+ * @c: the cross product
+ *
+ * Calculate the cross product of two vectors
+ */
+void crossd(gdouble *a, gdouble *b, gdouble *out)
+{
+       out[0] = a[1] * b[2] - a[2] * b[1];
+       out[1] = a[2] * b[0] - a[0] * b[2];
+       out[2] = a[0] * b[1] - a[1] * b[0];
+}
+
+/**
+ * lengthd:
+ * @a: the vector
+ *
+ * Calculate the length (magnitude) of a vector.
+ *
+ * Returns: the length
+ */
+gdouble lengthd(gdouble *a)
+{
+       return sqrt(a[0] * a[0] +
+                   a[1] * a[1] +
+                   a[2] * a[2]);
+}
+
+/**
+ * normd:
+ * @a: the first longitude
+ *
+ * Normalize a vector to a mangitude of 1
+ */
+void normd(gdouble *a)
+{
+       gdouble total = lengthd(a);
+       a[0] = a[0] / total;
+       a[1] = a[1] / total;
+       a[2] = a[2] / total;
+}
index 07715415f1ee89b0bd4e2654064e7658f193d44e..3c331f06b07708532b2dbb76deb1e6429a1e2bf5 100644 (file)
@@ -202,6 +202,14 @@ gdouble ll2m(gdouble lon_dist, gdouble lat);
 
 gdouble distd(gdouble *a, gdouble *b);
 
 
 gdouble distd(gdouble *a, gdouble *b);
 
+void crossd(gdouble *a, gdouble *b, gdouble *out);
+
+void crossd3(gdouble *a, gdouble *b, gdouble *c, gdouble *out);
+
+gdouble lengthd(gdouble *a);
+
+void normd(gdouble *a);
+
 gdouble lon_avg(gdouble a, gdouble b);
 
 #endif
 gdouble lon_avg(gdouble a, gdouble b);
 
 #endif