Skip to content

Commit

Permalink
Removed sabai dependency as not needed. Streamlined tests and added P…
Browse files Browse the repository at this point in the history
…ose3
  • Loading branch information
taylorpool committed Feb 16, 2023
1 parent 2fe4299 commit df3272a
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 225 deletions.
10 changes: 0 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 20)

include(FetchContent)

FetchContent_Declare(
Sabai
GIT_REPOSITORY [email protected]:taylorpool/sabai.git
GIT_TAG constexpr
)
FetchContent_MakeAvailable(Sabai)

add_library(onogawa
src/onogawa.cpp
)
Expand All @@ -23,7 +14,6 @@ target_include_directories(onogawa
)
target_link_libraries(onogawa
PUBLIC
sabai
)

option(BUILD_Onogawa_TESTS "Build Onogawa Tests" OFF)
Expand Down
138 changes: 65 additions & 73 deletions include/onogawa/onogawa.hpp
Original file line number Diff line number Diff line change
@@ -1,106 +1,98 @@
#pragma once

#include "sabai/sabai.hpp"

namespace onogawa {

class Complex {
private:
double real_;
double imaginary_;
struct Complex {
double real;
double imaginary;

public:
constexpr Complex(double real, double imaginary)
: real_(real), imaginary_(imaginary) {}
: real(real), imaginary(imaginary) {}
};

class Point2 {
private:
double x_;
double y_;

public:
constexpr Point2(double x, double y) : x_(x), y_(y) {}
struct Point2 {
double x;
double y;

constexpr double x() const { return x_; }
constexpr double y() const { return y_; }
constexpr double &x() { return x_; }
constexpr double &y() { return y_; }
constexpr Point2(double x, double y) : x(x), y(y) {}
};

class Quaternion {
private:
static constexpr uint64_t W_INDEX = 0;
static constexpr uint64_t X_INDEX = 1;
static constexpr uint64_t Y_INDEX = 2;
static constexpr uint64_t Z_INDEX = 3;
sabai::StaticVectord<4> data_;

public:
constexpr Quaternion(double w, double x, double y, double z) {
data_(W_INDEX) = w;
data_(X_INDEX) = x;
data_(Y_INDEX) = y;
data_(Z_INDEX) = z;
}
struct Quaternion {
double w;
double x;
double y;
double z;

constexpr Quaternion(double w, double x, double y, double 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); };

constexpr double w() const { return data_(W_INDEX); }
constexpr double x() const { return data_(X_INDEX); }
constexpr double y() const { return data_(Y_INDEX); }
constexpr double z() const { return data_(Z_INDEX); }
};

Quaternion operator*(const Quaternion &a, const Quaternion &b);
constexpr Quaternion operator*(const Quaternion &a, const Quaternion &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);
}

class Point3 {
private:
double x_;
double y_;
double z_;
struct Point3 {
double x;
double y;
double z;

public:
constexpr Point3(double x, double y, double z) : x_(x), y_(y), z_(z) {}
constexpr Point3(double x, double y, double z) : x(x), y(y), z(z) {}

constexpr double x() const { return x_; }
constexpr double y() const { return y_; }
constexpr double z() const { return z_; }
constexpr double &x() { return x_; }
constexpr double &y() { return y_; }
constexpr double &z() { return 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); }
};

constexpr Point3 operator+(const Point3 &a, const Point3 &b) {
return Point3(a.x + b.x, a.y + b.y, a.z + b.z);
}

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

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

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

const double xs = q.x() * s;
const double ys = q.y() * s;
const double zs = q.z() * s;
const double xxs = q.x * xs;
const double xys = q.x * ys;
const double xzs = q.x * zs;

const double wxs = q.w() * xs;
const double wys = q.w() * ys;
const double wzs = q.w() * zs;
const double yys = q.y * ys;
const double yzs = q.y * zs;

const double xxs = q.x() * xs;
const double xys = q.x() * ys;
const double xzs = q.x() * zs;
const double zzs = q.z * zs;

const double yys = q.y() * ys;
const double yzs = q.y() * 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);
}

struct Pose3 {
Quaternion orientation;
Point3 position;

const double zzs = q.z() * zs;
constexpr Pose3(const Quaternion &orientation, const Point3 &position)
: orientation(orientation), position(position) {}
};

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());
constexpr Pose3 operator*(const Pose3 &a, const Pose3 &b) {
return Pose3(a.orientation * b.orientation,
a.orientation * b.position + a.position);
}

} // namespace onogawa
12 changes: 1 addition & 11 deletions src/onogawa.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
#include "onogawa/onogawa.hpp"

namespace onogawa {

Quaternion operator*(const Quaternion &a, const Quaternion &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());
}

} // namespace onogawa
namespace onogawa {} // namespace onogawa
Loading

0 comments on commit df3272a

Please sign in to comment.