Skip to content

Commit

Permalink
fix common classes format
Browse files Browse the repository at this point in the history
  • Loading branch information
ersaraujo committed Oct 26, 2024
1 parent fc68576 commit df05ae1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 69 deletions.
95 changes: 28 additions & 67 deletions common/cpp/robocin/geometry/point2d.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#ifndef ROBOCIN_GEOMETRY_POINT2D_H
#define ROBOCIN_GEOMETRY_POINT2D_H

#include <type_traits>
#include <stdexcept>
#include <math.h>
#include <ostream>
#include <stdexcept>
#include <type_traits>

namespace robocin {

Expand All @@ -17,16 +17,14 @@ struct Point2D {
// Constructors:
inline constexpr Point2D() : x(0), y(0) {}
inline constexpr Point2D(value_type x, value_type y) : x(x), y(y) {}

template <class U, class V>
inline constexpr Point2D(const std::pair<U, V>& pair) :
x(static_cast<value_type>(pair.first)),
y(static_cast<value_type>(pair.second)) {}
x(static_cast<value_type>(pair.first)),
y(static_cast<value_type>(pair.second)) {}

// Static methods:
inline static constexpr Point2D null() {
return Point2D(0, 0);
}
inline static constexpr Point2D null() { return Point2D(0, 0); }

inline static constexpr Point2D fromPolar(value_type angle) {
return Point2D(std::cos(angle), std::sin(angle));
Expand All @@ -38,7 +36,7 @@ struct Point2D {

// Floatint-point comparison:
bool fuzzyIsNull(value_type value, value_type epsilon = static_cast<value_type>(1e-9)) {
return std::abs(value) < epsilon;
return std::abs(value) < epsilon;
}

inline constexpr bool operator==(const Point2D& other) const {
Expand All @@ -48,7 +46,7 @@ struct Point2D {
// Arithmetic-assignment operators:
inline constexpr Point2D& operator+=(const Point2D& other) {
x += other.x, y += other.y;
return *this;
return *this;
}

inline constexpr Point2D& operator-=(const Point2D& other) {
Expand All @@ -67,69 +65,45 @@ struct Point2D {
}

// arithmetic operators:
inline constexpr Point2D operator+(const Point2D& other) const {
return Point2D(*this) += other;
}
inline constexpr Point2D operator+(const Point2D& other) const { return Point2D(*this) += other; }

inline constexpr Point2D operator-(const Point2D& other) const {
return Point2D(*this) -= other;
}
inline constexpr Point2D operator-(const Point2D& other) const { return Point2D(*this) -= other; }

inline constexpr Point2D operator*(value_type factor) const {
return Point2D(*this) *= factor;
}
inline constexpr Point2D operator*(value_type factor) const { return Point2D(*this) *= factor; }

inline constexpr Point2D operator/(value_type factor) const {
return Point2D(*this) /= factor;
}
inline constexpr Point2D operator/(value_type factor) const { return Point2D(*this) /= factor; }

// arithmetic friends:
friend inline constexpr Point2D operator*(value_type factor, const Point2D& point) {
return Point2D(factor * point.x, factor * point.y);
}

// sign operators:
inline constexpr Point2D operator+() const {
return *this;
}
inline constexpr Point2D operator+() const { return *this; }

inline constexpr Point2D operator-() const {
return Point2D(-x, -y);
}
inline constexpr Point2D operator-() const { return Point2D(-x, -y); }

// Custom
[[nodiscard]] inline constexpr bool isNull() const {
return fuzzyIsNull(x) and fuzzyIsNull(y);
}
[[nodiscard]] inline constexpr bool isNull() const { return fuzzyIsNull(x) and fuzzyIsNull(y); }

inline constexpr void transpose() {
std::swap(x, y);
}
inline constexpr void transpose() { std::swap(x, y); }

inline constexpr Point2D transposed() const {
return Point2D{y, x};
}
inline constexpr Point2D transposed() const { return Point2D{y, x}; }

inline constexpr void swap(Point2D& other) noexcept {
std::swap(x, other.x);
std::swap(y, other.y);
}

inline constexpr std::pair<value_type, value_type> toPair() const {
return std::make_pair(x, y);
}
inline constexpr std::pair<value_type, value_type> toPair() const { return std::make_pair(x, y); }
template <class U, class V = U>
inline constexpr std::pair<U, V> toPair() const {
return std::make_pair(static_cast<U>(x), static_cast<V>(y));
}

inline constexpr value_type manhattanLength() const {
return std::abs(x) + std::abs(y);
}
inline constexpr value_type manhattanLength() const { return std::abs(x) + std::abs(y); }

inline constexpr value_type dot(const Point2D& other) const {
return x * other.x + y * other.y;
}
inline constexpr value_type dot(const Point2D& other) const { return x * other.x + y * other.y; }

inline constexpr value_type cross(const Point2D& other) const {
return x * other.y - y * other.x;
Expand All @@ -143,19 +117,15 @@ struct Point2D {
return std::sqrt(distanceSquaredTo(other));
}

inline constexpr void rotateClockWise90() {
std::swap(x, y), y = -y;
}
inline constexpr void rotateClockWise90() { std::swap(x, y), y = -y; }

inline constexpr Point2D rotatedClockWise90() const {
Point2D result{*this};
result.rotateClockWise90();
return result;
}

inline constexpr void rotateCounterClockWise90() {
std::swap(x, y), x = -x;
}
inline constexpr void rotateCounterClockWise90() { std::swap(x, y), x = -x; }

inline constexpr Point2D rotatedCounterClockWise90() const {
Point2D result{*this};
Expand Down Expand Up @@ -195,25 +165,17 @@ struct Point2D {
return result;
}

inline constexpr auto angle() const {
return std::atan2(y, x);
}
inline constexpr auto angle() const { return std::atan2(y, x); }

inline constexpr auto angleTo(const Point2D& other) const {
return std::atan2(cross(other), dot(other));
}

inline constexpr value_type lengthSquared() const {
return dot(*this);
}
inline constexpr value_type lengthSquared() const { return dot(*this); }

inline constexpr auto length() const {
return std::sqrt(lengthSquared());
}
inline constexpr auto length() const { return std::sqrt(lengthSquared()); }

inline constexpr auto norm() const {
return std::sqrt(lengthSquared());
}
inline constexpr auto norm() const { return std::sqrt(lengthSquared()); }

inline constexpr void resize(value_type t) {
if (auto norm = this->norm(); not fuzzyIsNull(norm)) {
Expand Down Expand Up @@ -298,10 +260,9 @@ struct Point2D {
}

friend inline std::ostream& operator<<(std::ostream& os, const Point2D& point) {
os << '(' << point.x << ", " << point.y << ')';
return os;
os << '(' << point.x << ", " << point.y << ')';
return os;
}

};

Point2D() -> Point2D<double>;
Expand Down
4 changes: 2 additions & 2 deletions common/cpp/robocin/utility/iproto_convertible.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ class IProtoConvertible {
IProtoConvertible& operator=(IProtoConvertible&&) noexcept = default;

virtual ~IProtoConvertible() = default;

virtual T toProto() const = 0;
virtual void fromProto(T proto) = 0;
};

} // namespace robocin

#endif // ROBOCIN_UTILITY_IPROTO_CONVERTIBLE
#endif // ROBOCIN_UTILITY_IPROTO_CONVERTIBLE

0 comments on commit df05ae1

Please sign in to comment.