Skip to content

Commit

Permalink
Separated each geometric element into its own header
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorpool committed Feb 23, 2023
1 parent 7d6ff78 commit 3632576
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 143 deletions.
22 changes: 22 additions & 0 deletions include/onogawa/compass.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

namespace onogawa {

template <typename T> struct Compass {
T x;
T y;

constexpr Compass(T x, T y) : x(x), y(y) {}

constexpr static Compass ex() { return Compass(1.0, 0.0); }
constexpr static Compass ey() { return Compass(0.0, 1.0); }
};

template <typename T>
constexpr Compass<T> operator*(const Compass<T> &a, const Compass<T> &b) {
return Compass(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
}

using Compassd = Compass<double>;

} // namespace onogawa
152 changes: 9 additions & 143 deletions include/onogawa/onogawa.hpp
Original file line number Diff line number Diff line change
@@ -1,145 +1,11 @@
#pragma once

namespace onogawa {

template <typename T> struct Compass {
T x;
T y;

constexpr Compass(T x, T y) : x(x), y(y) {}

constexpr static Compass ex() { return Compass(1.0, 0.0); }
constexpr static Compass ey() { return Compass(0.0, 1.0); }
};

template <typename T>
constexpr Compass<T> operator*(const Compass<T> &a, const Compass<T> &b) {
return Compass(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
}

template <typename T> struct Point2 {
T x;
T y;

constexpr Point2(T x, T y) : x(x), y(y) {}

constexpr static Point2 ex() { return Point2(1.0, 0.0); }
constexpr static Point2 ey() { return Point2(0.0, 1.0); }
};

template <typename T>
constexpr Point2<T> operator+(const Point2<T> &a, const Point2<T> &b) {
return Point2(a.x + b.x, a.y + b.y);
}

template <typename T>
constexpr Point2<T> operator*(const Compass<T> &compass,
const Point2<T> &point) {
return Point2(compass.x * point.x - compass.y * point.y,
compass.x * point.y - compass.y * point.x);
}

template <typename T> struct Pose2 {
Compass<T> orientation;
Point2<T> position;

constexpr Pose2(const Compass<T> &orientation, const Point2<T> &position)
: orientation(orientation), position(position) {}
};

template <typename T>
constexpr Pose2<T> operator*(const Pose2<T> &a, const Pose2<T> &b) {
return Pose2(a.orientation * b.orientation,
a.orientation * b.position + a.position);
}

template <typename T> struct Quaternion {
T w;
T x;
T y;
T z;

constexpr Quaternion(T w, T x, T y, T z) : w(w), x(x), y(y), z(z) {}

constexpr static Quaternion ew() { return Quaternion(1.0, 0.0, 0.0, 0.0); };
constexpr static Quaternion ex() { return Quaternion(0.0, 1.0, 0.0, 0.0); };
constexpr static Quaternion ey() { return Quaternion(0.0, 0.0, 1.0, 0.0); };
constexpr static Quaternion ez() { return Quaternion(0.0, 0.0, 0.0, 1.0); };
};

template <typename T>
constexpr Quaternion<T> operator*(const Quaternion<T> &a,
const Quaternion<T> &b) {
return Quaternion(a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z,
a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y,
a.w * b.y - a.x * b.z + a.y * b.w + a.z * b.x,
a.w * b.z + a.x * b.y - a.y * b.x + a.z * b.w);
}

template <typename T> struct Point3 {
T x;
T y;
T z;

constexpr Point3(T x, T y, T z) : x(x), y(y), z(z) {}

constexpr static Point3 ex() { return Point3(1.0, 0.0, 0.0); }
constexpr static Point3 ey() { return Point3(0.0, 1.0, 0.0); }
constexpr static Point3 ez() { return Point3(0.0, 0.0, 1.0); }
};

template <typename T>
constexpr Point3<T> operator+(const Point3<T> &a, const Point3<T> &b) {
return Point3(a.x + b.x, a.y + b.y, a.z + b.z);
}

template <typename T>
constexpr Point3<T> operator*(const Quaternion<T> &q, const Point3<T> &point) {
const T s = 2.0 / (q.w * q.w + q.x * q.x + q.y * q.y + q.z * q.z);

const T xs = q.x * s;
const T ys = q.y * s;
const T zs = q.z * s;

const T wxs = q.w * xs;
const T wys = q.w * ys;
const T wzs = q.w * zs;

const T xxs = q.x * xs;
const T xys = q.x * ys;
const T xzs = q.x * zs;

const T yys = q.y * ys;
const T yzs = q.y * zs;

const T zzs = q.z * zs;

return Point3(
(1 - yys - zzs) * point.x + (xys - wzs) * point.y + (xzs + wys) * point.z,
(xys + wzs) * point.x + (1 - xxs - zzs) * point.y + (yzs - wxs) * point.z,
(xzs - wys) * point.x + (yzs + wxs) * point.y +
(1 - xxs - yys) * point.z);
}

template <typename T> struct Pose3 {
Quaternion<T> orientation;
Point3<T> position;

constexpr Pose3(const Quaternion<T> &orientation, const Point3<T> &position)
: orientation(orientation), position(position) {}
};

template <typename T>
constexpr Pose3<T> operator*(const Pose3<T> &a, const Pose3<T> &b) {
return Pose3(a.orientation * b.orientation,
a.orientation * b.position + a.position);
}

using Compassd = Compass<double>;
using Point2d = Point2<double>;
using Pose2d = Pose2<double>;
using Quaterniond = Quaternion<double>;
using Point3d = Point3<double>;
using Pose3d = Pose3<double>;

} // namespace onogawa
// 2-D
#include "onogawa/compass.hpp"
#include "onogawa/point2.hpp"
#include "onogawa/pose2.hpp"

// 3-D
#include "onogawa/point3.hpp"
#include "onogawa/pose3.hpp"
#include "onogawa/quaternion.hpp"
31 changes: 31 additions & 0 deletions include/onogawa/point2.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include "onogawa/compass.hpp"

namespace onogawa {

template <typename T> struct Point2 {
T x;
T y;

constexpr Point2(T x, T y) : x(x), y(y) {}

constexpr static Point2 ex() { return Point2(1.0, 0.0); }
constexpr static Point2 ey() { return Point2(0.0, 1.0); }
};

template <typename T>
constexpr Point2<T> operator+(const Point2<T> &a, const Point2<T> &b) {
return Point2(a.x + b.x, a.y + b.y);
}

template <typename T>
constexpr Point2<T> operator*(const Compass<T> &compass,
const Point2<T> &point) {
return Point2(compass.x * point.x - compass.y * point.y,
compass.x * point.y - compass.y * point.x);
}

using Point2d = Point2<double>;

} // namespace onogawa
54 changes: 54 additions & 0 deletions include/onogawa/point3.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#pragma once

#include "onogawa/quaternion.hpp"

namespace onogawa {

template <typename T> struct Point3 {
T x;
T y;
T z;

constexpr Point3(T x, T y, T z) : x(x), y(y), z(z) {}

constexpr static Point3 ex() { return Point3(1.0, 0.0, 0.0); }
constexpr static Point3 ey() { return Point3(0.0, 1.0, 0.0); }
constexpr static Point3 ez() { return Point3(0.0, 0.0, 1.0); }
};

template <typename T>
constexpr Point3<T> operator+(const Point3<T> &a, const Point3<T> &b) {
return Point3(a.x + b.x, a.y + b.y, a.z + b.z);
}

template <typename T>
constexpr Point3<T> operator*(const Quaternion<T> &q, const Point3<T> &point) {
const T s = 2.0 / (q.w * q.w + q.x * q.x + q.y * q.y + q.z * q.z);

const T xs = q.x * s;
const T ys = q.y * s;
const T zs = q.z * s;

const T wxs = q.w * xs;
const T wys = q.w * ys;
const T wzs = q.w * zs;

const T xxs = q.x * xs;
const T xys = q.x * ys;
const T xzs = q.x * zs;

const T yys = q.y * ys;
const T yzs = q.y * zs;

const T zzs = q.z * zs;

return Point3(
(1 - yys - zzs) * point.x + (xys - wzs) * point.y + (xzs + wys) * point.z,
(xys + wzs) * point.x + (1 - xxs - zzs) * point.y + (yzs - wxs) * point.z,
(xzs - wys) * point.x + (yzs + wxs) * point.y +
(1 - xxs - yys) * point.z);
}

using Point3d = Point3<double>;

} // namespace onogawa
24 changes: 24 additions & 0 deletions include/onogawa/pose2.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include "onogawa/compass.hpp"
#include "onogawa/point2.hpp"

namespace onogawa {

template <typename T> struct Pose2 {
Compass<T> orientation;
Point2<T> position;

constexpr Pose2(const Compass<T> &orientation, const Point2<T> &position)
: orientation(orientation), position(position) {}
};

template <typename T>
constexpr Pose2<T> operator*(const Pose2<T> &a, const Pose2<T> &b) {
return Pose2(a.orientation * b.orientation,
a.orientation * b.position + a.position);
}

using Pose2d = Pose2<double>;

} // namespace onogawa
24 changes: 24 additions & 0 deletions include/onogawa/pose3.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include "onogawa/point3.hpp"
#include "onogawa/quaternion.hpp"

namespace onogawa {

template <typename T> struct Pose3 {
Quaternion<T> orientation;
Point3<T> position;

constexpr Pose3(const Quaternion<T> &orientation, const Point3<T> &position)
: orientation(orientation), position(position) {}
};

template <typename T>
constexpr Pose3<T> operator*(const Pose3<T> &a, const Pose3<T> &b) {
return Pose3(a.orientation * b.orientation,
a.orientation * b.position + a.position);
}

using Pose3d = Pose3<double>;

} // namespace onogawa
30 changes: 30 additions & 0 deletions include/onogawa/quaternion.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

namespace onogawa {

template <typename T> struct Quaternion {
T w;
T x;
T y;
T z;

constexpr Quaternion(T w, T x, T y, T z) : w(w), x(x), y(y), z(z) {}

constexpr static Quaternion ew() { return Quaternion(1.0, 0.0, 0.0, 0.0); };
constexpr static Quaternion ex() { return Quaternion(0.0, 1.0, 0.0, 0.0); };
constexpr static Quaternion ey() { return Quaternion(0.0, 0.0, 1.0, 0.0); };
constexpr static Quaternion ez() { return Quaternion(0.0, 0.0, 0.0, 1.0); };
};

template <typename T>
constexpr Quaternion<T> operator*(const Quaternion<T> &a,
const Quaternion<T> &b) {
return Quaternion(a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z,
a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y,
a.w * b.y - a.x * b.z + a.y * b.w + a.z * b.x,
a.w * b.z + a.x * b.y - a.y * b.x + a.z * b.w);
}

using Quaterniond = Quaternion<double>;

} // namespace onogawa

0 comments on commit 3632576

Please sign in to comment.