aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFranklin Wei <me@fwei.tk>2019-03-07 19:14:35 -0500
committerFranklin Wei <me@fwei.tk>2019-03-07 19:14:35 -0500
commit31539f3af0c7beea865b29fdda0840c79f14710e (patch)
tree7bea014a44a938f71ea21b3a8ba457b3576348d9
downloadlibfml-31539f3af0c7beea865b29fdda0840c79f14710e.zip
libfml-31539f3af0c7beea865b29fdda0840c79f14710e.tar.gz
libfml-31539f3af0c7beea865b29fdda0840c79f14710e.tar.bz2
libfml-31539f3af0c7beea865b29fdda0840c79f14710e.tar.xz
Import from fieldviz
-rw-r--r--CMakeLists.txt23
-rw-r--r--README.md4
-rw-r--r--include/fml/fml.h10
-rw-r--r--include/fml/quat.h28
-rw-r--r--include/fml/vec2.h38
-rw-r--r--include/fml/vec3.h39
-rw-r--r--src/quat.cpp42
-rw-r--r--src/vec2.cpp89
-rw-r--r--src/vec3.cpp105
9 files changed, 378 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 0000000..53de8b9
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,23 @@
+cmake_minimum_required(VERSION 3.9)
+project(libfml VERSION 1.0 DESCRIPTION "Frank's Math Library")
+
+add_library(fml SHARED
+ src/quat.cpp
+ src/vec2.cpp
+ src/vec3.cpp)
+
+set_target_properties(fml PROPERTIES
+ VERSION ${PROJECT_VERSION}
+ SOVERSION 1)
+
+install(DIRECTORY "include/"
+ DESTINATION "include"
+ FILES_MATCHING
+ PATTERN "*.h")
+
+target_include_directories(fml PRIVATE include)
+
+include(GNUInstallDirs)
+install(TARGETS fml
+ LIBRARY DESTINATION /usr/lib
+ PUBLIC_HEADER DESTINATION /usr/include)
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..e05cc88
--- /dev/null
+++ b/README.md
@@ -0,0 +1,4 @@
+# libfml: Frank's Math Library
+
+Just a collection of useful C++ math routines I've accumulated over
+the years. Used by some of my personal programs.
diff --git a/include/fml/fml.h b/include/fml/fml.h
new file mode 100644
index 0000000..046db5a
--- /dev/null
+++ b/include/fml/fml.h
@@ -0,0 +1,10 @@
+/*
+ * libfml: Frank's Math Library
+ *
+ * Copyright (C) 2019 Franklin Wei
+ */
+
+typedef float scalar;
+
+#include "quat.h"
+#include "vec3.h"
diff --git a/include/fml/quat.h b/include/fml/quat.h
new file mode 100644
index 0000000..14c966e
--- /dev/null
+++ b/include/fml/quat.h
@@ -0,0 +1,28 @@
+#ifndef QUAT_H
+#define QUAT_H
+
+#include "fml.h"
+#include <iostream>
+
+namespace fml {
+ class quat {
+ public:
+ scalar w, x, y, z;
+ public:
+ quat(scalar w, scalar x, scalar y, scalar z);
+ quat(scalar x, scalar y, scalar z);
+ quat(scalar w, vec3 vec);
+ quat(vec3 vec);
+ quat();
+
+ operator vec3();
+
+ quat conjugate() const;
+
+ static quat from_angleaxis(scalar angle, vec3 axis);
+ };
+
+ quat operator*(const quat &, const quat &);
+ std::ostream &operator<<(std::ostream &os, const quat &);
+}
+#endif
diff --git a/include/fml/vec2.h b/include/fml/vec2.h
new file mode 100644
index 0000000..166b99c
--- /dev/null
+++ b/include/fml/vec2.h
@@ -0,0 +1,38 @@
+#ifndef VEC2_H
+#define VEC2_H
+#include <iostream>
+
+#include "fml.h"
+
+namespace fml {
+ class vec2 {
+ public:
+ scalar v[2];
+ public:
+ vec2();
+ vec2(scalar x);
+ vec2(scalar x, scalar y);
+ scalar &operator[](int index);
+ scalar operator[](int index) const;
+ vec2 operator*(scalar scale) const;
+ vec2 operator/(scalar scale) const;
+ vec2 operator+(const vec2 &other) const;
+ vec2 operator-(const vec2 &other) const;
+ vec2 operator-() const;
+ const vec2 &operator*=(scalar scale);
+ const vec2 &operator/=(scalar scale);
+ const vec2 &operator+=(const vec2 &other);
+ const vec2 &operator-=(const vec2 &other);
+ scalar magnitude() const;
+ scalar magnitudeSquared() const;
+ vec2 normalize() const;
+ scalar dot(const vec2 &other) const;
+ };
+
+ vec2 operator*(scalar scale, const vec2 &v);
+
+ std::ostream &operator<<(std::ostream &output, const vec2 &v);
+ std::istream &operator>>(std::istream &input, vec3 &v);
+}
+
+#endif
diff --git a/include/fml/vec3.h b/include/fml/vec3.h
new file mode 100644
index 0000000..3460c56
--- /dev/null
+++ b/include/fml/vec3.h
@@ -0,0 +1,39 @@
+#ifndef VEC3_H
+#define VEC3_H
+#include <iostream>
+
+#include "fml.h"
+
+namespace fml {
+ class vec3 {
+ public:
+ scalar v[3];
+ public:
+ vec3();
+ vec3(scalar x);
+ vec3(scalar x, scalar y, scalar z);
+ scalar &operator[](int index);
+ scalar operator[](int index) const;
+ vec3 operator*(scalar scale) const;
+ vec3 operator/(scalar scale) const;
+ vec3 operator+(const vec3 &other) const;
+ vec3 operator-(const vec3 &other) const;
+ vec3 operator-() const;
+ const vec3 &operator*=(scalar scale);
+ const vec3 &operator/=(scalar scale);
+ const vec3 &operator+=(const vec3 &other);
+ const vec3 &operator-=(const vec3 &other);
+ scalar magnitude() const;
+ scalar magnitudeSquared() const;
+ vec3 normalize() const;
+ scalar dot(const vec3 &other) const;
+ vec3 cross(const vec3 &other) const;
+ };
+
+ vec3 operator*(scalar scale, const vec3 &v);
+
+ std::ostream &operator<<(std::ostream &output, const vec3 &v);
+ std::istream &operator>>(std::istream &input, vec3 &v);
+}
+
+#endif
diff --git a/src/quat.cpp b/src/quat.cpp
new file mode 100644
index 0000000..dfb2823
--- /dev/null
+++ b/src/quat.cpp
@@ -0,0 +1,42 @@
+#include <fml/quat.h>
+#include <cmath>
+
+using namespace fml;
+
+namespace fml {
+ quat::quat(scalar w_, scalar x_, scalar y_, scalar z_) : w(w_), x(x_), y(y_), z(z_) { }
+ quat::quat(scalar x_, scalar y_, scalar z_) : w(0), x(x_), y(y_), z(z_) { }
+ quat::quat(scalar w_, vec3 vec) : w(w_), x(vec[0]), y(vec[1]), z(vec[2]) { }
+ quat::quat(vec3 vec) : w(0), x(vec[0]), y(vec[1]), z(vec[2]) { }
+ quat::quat() : w(0), x(0), y(0), z(0) { }
+
+ quat::operator vec3()
+ {
+ return vec3(this->x, this->y, this->z);
+ }
+
+ quat operator*(const quat &lhs, const quat &rhs)
+ {
+ return quat(lhs.w * rhs.w - lhs.x * rhs.x - lhs.y * rhs.y - lhs.z * rhs.z,
+ lhs.w * rhs.x + lhs.x * rhs.w + lhs.y * rhs.z - lhs.z * rhs.y,
+ lhs.w * rhs.y - lhs.x * rhs.z + lhs.y * rhs.w + lhs.z * rhs.x,
+ lhs.w * rhs.z + lhs.x * rhs.y - lhs.y * rhs.x + lhs.z * rhs.w);
+ }
+
+ quat quat::conjugate() const
+ {
+ return quat(this->w, -this->x, -this->y, -this->z);
+ }
+
+ quat quat::from_angleaxis(scalar angle, vec3 axis)
+ {
+ scalar si = std::sin(angle / 2);
+ scalar co = std::cos(angle / 2);
+ return quat(co, si * axis[0], si * axis[1], si * axis[2]);
+ }
+
+ std::ostream &operator<<(std::ostream &os, const quat &q)
+ {
+ return os << "(" << q.w << ", " << q.x << ", " << q.y << ", " << q.z << ")";
+ }
+}
diff --git a/src/vec2.cpp b/src/vec2.cpp
new file mode 100644
index 0000000..712b440
--- /dev/null
+++ b/src/vec2.cpp
@@ -0,0 +1,89 @@
+#include <iostream>
+#include <cmath>
+#include <fml/vec2.h>
+using std::ostream;
+using namespace fml;
+
+namespace fml {
+ vec2::vec2() {
+ v[0] = 0;
+ v[1] = 0;
+ }
+ vec2::vec2(scalar x) {
+ v[0] = x;
+ v[1] = 0;
+ }
+ vec2::vec2(scalar x, scalar y) {
+ v[0] = x;
+ v[1] = y;
+ }
+ scalar &vec2::operator[](int index) {
+ return v[index];
+ }
+ scalar vec2::operator[](int index) const {
+ return v[index];
+ }
+ vec2 vec2::operator*(scalar scale) const {
+ return vec2(v[0] * scale, v[1] * scale);
+ }
+ vec2 vec2::operator/(scalar scale) const {
+ return vec2(v[0] / scale, v[1] / scale);
+ }
+ vec2 vec2::operator+(const vec2 &other) const{
+ return vec2(v[0] + other.v[0], v[1] + other.v[1]);
+ }
+ vec2 vec2::operator-(const vec2 &other) const {
+ return vec2(v[0] - other.v[0], v[1] - other.v[1]);
+ }
+ vec2 vec2::operator-() const {
+ return vec2(-v[0], -v[1]);
+ }
+ const vec2 &vec2::operator*=(scalar scale) {
+ v[0] *= scale;
+ v[1] *= scale;
+ return *this;
+ }
+ const vec2 &vec2::operator/=(scalar scale) {
+ v[0] /= scale;
+ v[1] /= scale;
+ return *this;
+ }
+ const vec2 &vec2::operator+=(const vec2 &other) {
+ v[0] += other.v[0];
+ v[1] += other.v[1];
+ return *this;
+ }
+ const vec2 &vec2::operator-=(const vec2 &other) {
+ v[0] -= other.v[0];
+ v[1] -= other.v[1];
+ return *this;
+ }
+ scalar vec2::magnitude() const {
+ return sqrt(v[0] * v[0] + v[1] * v[1]);
+ }
+ scalar vec2::magnitudeSquared() const {
+ return v[0] * v[0] + v[1] * v[1];
+ }
+ vec2 vec2::normalize() const {
+ scalar m = sqrt(v[0] * v[0] + v[1] * v[1]);
+ return vec2(v[0] / m, v[1] / m);
+ }
+ scalar vec2::dot(const vec2 &other) const {
+ return v[0] * other.v[0] + v[1] * other.v[1];
+ }
+ std::ostream &operator<<(std::ostream &output, const vec2 &v) {
+ return output << v[0] << " " << v[1];
+ }
+
+ std::istream &operator>>(std::istream &input, vec2 &v)
+ {
+ if(!(input >> v[0] >> v[1]))
+ throw "error parsing vector";
+ return input;
+ }
+
+ vec2 operator*(scalar scale, const vec2 &v)
+ {
+ return v * scale;
+ }
+}
diff --git a/src/vec3.cpp b/src/vec3.cpp
new file mode 100644
index 0000000..bf0b021
--- /dev/null
+++ b/src/vec3.cpp
@@ -0,0 +1,105 @@
+/* copy-pasted from:
+ * https://www.programming-techniques.com/2013/05/basic-euclidean-vector-operations-in-c.htm
+ */
+
+#include <iostream>
+#include <cmath>
+#include <fml/quat.h>
+using std::ostream;
+using namespace fml;
+
+namespace fml{
+ vec3::vec3() {
+ v[0] = 0;
+ v[1] = 0;
+ v[2] = 0;
+ }
+ vec3::vec3(scalar x) {
+ v[0] = x;
+ v[1] = 0;
+ v[2] = 0;
+ }
+ vec3::vec3(scalar x, scalar y, scalar z) {
+ v[0] = x;
+ v[1] = y;
+ v[2] = z;
+ }
+ scalar &vec3::operator[](int index) {
+ return v[index];
+ }
+ scalar vec3::operator[](int index) const {
+ return v[index];
+ }
+ vec3 vec3::operator*(scalar scale) const {
+ return vec3(v[0] * scale, v[1] * scale, v[2] * scale);
+ }
+ vec3 vec3::operator/(scalar scale) const {
+ return vec3(v[0] / scale, v[1] / scale, v[2] / scale);
+ }
+ vec3 vec3::operator+(const vec3 &other) const{
+ return vec3(v[0] + other.v[0], v[1] + other.v[1], v[2] + other.v[2]);
+ }
+ vec3 vec3::operator-(const vec3 &other) const {
+ return vec3(v[0] - other.v[0], v[1] - other.v[1], v[2] - other.v[2]);
+ }
+ vec3 vec3::operator-() const {
+ return vec3(-v[0], -v[1], -v[2]);
+ }
+ const vec3 &vec3::operator*=(scalar scale) {
+ v[0] *= scale;
+ v[1] *= scale;
+ v[2] *= scale;
+ return *this;
+ }
+ const vec3 &vec3::operator/=(scalar scale) {
+ v[0] /= scale;
+ v[1] /= scale;
+ v[2] /= scale;
+ return *this;
+ }
+ const vec3 &vec3::operator+=(const vec3 &other) {
+ v[0] += other.v[0];
+ v[1] += other.v[1];
+ v[2] += other.v[2];
+ return *this;
+ }
+ const vec3 &vec3::operator-=(const vec3 &other) {
+ v[0] -= other.v[0];
+ v[1] -= other.v[1];
+ v[2] -= other.v[2];
+ return *this;
+ }
+ scalar vec3::magnitude() const {
+ return sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
+ }
+ scalar vec3::magnitudeSquared() const {
+ return v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
+ }
+ vec3 vec3::normalize() const {
+ scalar m = sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
+ return vec3(v[0] / m, v[1] / m, v[2] / m);
+ }
+ scalar vec3::dot(const vec3 &other) const {
+ return v[0] * other.v[0] + v[1] * other.v[1] + v[2] * other.v[2];
+ }
+ vec3 vec3::cross(const vec3 &other) const {
+ return vec3(v[1] * other.v[2] - v[2] * other.v[1],
+ v[2] * other.v[0] - v[0] * other.v[2],
+ v[0] * other.v[1] - v[1] * other.v[0]);
+ }
+ std::ostream &operator<<(std::ostream &output, const vec3 &v) {
+ return output << v[0] << " " << v[1] << " " << v[2];
+ }
+
+ std::istream &operator>>(std::istream &input, vec3 &v)
+ {
+ if(!(input >> v[0] >> v[1] >> v[2]))
+ throw "error parsing vector";
+ return input;
+ }
+
+ vec3 operator*(scalar scale, const vec3 &v)
+ {
+ return v * scale;
+ }
+}