Skip to content

Commit

Permalink
household/refactor modm::Vector and co
Browse files Browse the repository at this point in the history
  • Loading branch information
TomSaw committed Jan 26, 2022
1 parent 57e7fe9 commit c9c1de5
Show file tree
Hide file tree
Showing 26 changed files with 1,864 additions and 4,431 deletions.
124 changes: 24 additions & 100 deletions src/modm/math/geometry/geometric_traits.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Copyright (c) 2009-2011, Fabian Greif
* Copyright (c) 2012, Niklas Hauser
* Copyright (c) 2022, Thomas Sommer
*
* This file is part of the modm project.
*
Expand All @@ -10,12 +11,13 @@
*/
// ----------------------------------------------------------------------------

#ifndef MODM_GEOMETRIC_TRAITS_HPP
#define MODM_GEOMETRIC_TRAITS_HPP
#pragma once

#include <concepts>
#include <cmath>
#include <stdint.h>
#include <limits>
#include <modm/architecture/utils.hpp>
#include <modm/math/utils/arithmetic_traits.hpp>

namespace modm
{
Expand All @@ -24,120 +26,42 @@ namespace modm
*
* \ingroup modm_math_geometry
* \author Fabian Greif
* \author Thomas Sommer
*/
template <typename T>
struct GeometricTraits
{
static const bool isValidType = false;
struct GeometricTraits;

/**
* \brief Round if converting from a floating point base to
* a integer base.
*
* For T = \c float and \c double this method is specialized to return
* the result directly without any rounding.
*/
static inline T
round(float value)
{
return ::round(value);
}
};

template <>
struct GeometricTraits<int8_t>
template <std::integral T>
struct GeometricTraits<T>
{
static const bool isValidType = true;

typedef float FloatType;
typedef int16_t WideType;

static inline int8_t
round(float value)
{
return ::round(value);
}
};

// TODO is this useful?
template <>
struct GeometricTraits<uint8_t>
{
static const bool isValidType = true;

typedef float FloatType;
typedef int16_t WideType;
[[deprecated("Use an appropriate C++ concept instead!")]]
static const bool isValidType = false;

static inline uint8_t
round(float value)
{
return ::round(value);
}
using FloatType = float;
using WideType = modm::WideType<T>;
};

template <>
struct GeometricTraits<int16_t>
template <std::floating_point T>
struct GeometricTraits<T>
{
[[deprecated("Use an appropriate C++ concept instead!")]]
static const bool isValidType = true;

typedef float FloatType;
typedef int32_t WideType;

static inline int16_t
round(float value)
{
return ::round(value);
}
using FloatType = T;
using WideType = T;
};

#ifdef __AVR__
template <>
struct GeometricTraits<int32_t>
{
[[deprecated("Use an appropriate C++ concept instead!")]]
static const bool isValidType = true;

typedef float FloatType;

// Usually the range of a int32_t is big enough so that no
using FloatType = float;
// conversion to int64_t is required. This exception is made because
// 64-bit operations are very, very slow on an AVR.
typedef int32_t WideType;

static inline int32_t
round(float value)
{
return ::round(value);
}
};

template <>
struct GeometricTraits<float>
{
static const bool isValidType = true;

typedef float FloatType;
typedef float WideType;

static inline float
round(float value)
{
return value;
}
using WideType = int32_t;
};

template <>
struct GeometricTraits<double>
{
static const bool isValidType = true;

typedef double FloatType;
typedef double WideType;

static inline double
round(double value)
{
return value;
}
};
}

#endif // MODM_GEOMETRIC_TRAITS_HPP
#endif
}
146 changes: 71 additions & 75 deletions src/modm/math/geometry/location_2d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* Copyright (c) 2009-2011, Fabian Greif
* Copyright (c) 2012, Niklas Hauser
* Copyright (c) 2012, Sascha Schade
* Copyright (c) 2013, Kevin Läufer
* Copyright (c) 2022, Thomas Sommer
*
* This file is part of the modm project.
*
Expand All @@ -12,10 +14,7 @@
*/
// ----------------------------------------------------------------------------

#ifndef MODM_LOCATION_2D_HPP
#define MODM_LOCATION_2D_HPP

#include <cmath>
#pragma once

#include <modm/io/iostream.hpp>

Expand All @@ -37,39 +36,60 @@ namespace modm
class Location2D
{
public:
Location2D();

Location2D(const Vector<T, 2>& position, const float& orientation);

Location2D(const T& x, const T& y, const float& orientation);

inline const Vector<T, 2>&
getPosition() const;

inline const T&
getX() const;

inline const T&
getY() const;

void
setPosition(const Vector<T, 2>& point);

void
setPosition(const T& x, const T& y);
Vector<T, 2> position;
float orientation = 0;

constexpr Location2D() = default;
constexpr Location2D(const Vector<T, 2>& position, const float& orientation)
: position(position), orientation(orientation) {}

[[deprecated("Use 'setPosition({x, y}, orientation)' instead!")]]
constexpr Location2D(const T& x, const T& y, const float& orientation)
: position(x, y), orientation(orientation) {}

template<typename U>
constexpr Location2D(const Location2D<U> &l) : position(l.position), orientation(l.orientation) {}

// getters and setters
void setPosition(const Vector<T, 2>& position) { this->position = position; }

[[deprecated("Use 'setPosition({x, y}' instead!")]]
void setPosition(T x, T y) { this->position.x = x; this->position.y = y; }
void setOrientation(const float orientation) { this->orientation = orientation; }

Vector<T, 2> getPosition() const { return position; }
inline float getOrientation() const { return orientation; }
T getX() const { return position.x; }
T getY() const { return position.y; }

bool operator== (const Location2D &other) const {
return (
position == other.position and
std::abs(orientation - other.orientation) < __FLT_EPSILON__
);
}
bool operator!= (const Location2D &other) const {
return (
position != other.position or
std::abs(orientation - other.orientation) > __FLT_EPSILON__
);
}

inline float
getOrientation() const;
/// Add a position increment
void move(const Location2D& diff) {
Vector<T, 2> movement = diff.position;
movement.rotate(orientation);

void
setOrientation(const float& phi);
position.translate(movement);
orientation = Angle::normalize(orientation + diff.orientation);
}

/// Add a position increment
void
move(const Location2D& diff);
void move(const Vector<T, 2>& diff) {
Vector<T, 2> movement(diff);
movement.rotate(orientation);

void
move(const Vector<T, 2>& diff);
position.translate(movement);
}

/**
* \brief Add a increment only in x-direction
Expand All @@ -85,62 +105,38 @@ namespace modm
* movement over time.
* Because the y-component will always be zero, we created this
* method, which avoids unnecessary computations for the y-component
* and is therefore faster the the universal move-method.
* and is therefore faster than the universal move-method.
*
* \param x movement in x-direction
* \param phi rotation
*/
void
move(T x, float phi);
move(T x, float phi) {
Vector<T, 2> vector(Vector<float, 2>(x * std::cos(orientation), x * std::sin(orientation)));
position.translate(vector);

/// TODO
Vector<T, 2>
translated(const Vector<T, 2>& vector) const;

/// Convert between Location-objects with different base-types
template <typename U>
Location2D<U>
convert() const;
orientation = Angle::normalize(orientation + phi);
}

bool
operator == (const Location2D &other) const;
// TODO
Vector<T, 2> translated(const Vector<T, 2>& vector) const {
Vector<T, 2> result(vector);
result.rotate(orientation);
result.translate(position);

bool
operator != (const Location2D &other) const;
return result;
}

private:
template <typename U>
friend IOStream&
operator <<( IOStream&, const Location2D<U>&);

Vector<T, 2> position;
float orientation;
};

// ------------------------------------------------------------------------
// Global functions
// ------------------------------------------------------------------------
/**
* \brief Stream operator to \b modm::Location<T>
*
* \ingroup modm_math_geometry
*/
template<typename T>
IOStream&
operator << (IOStream& os, const Location2D<T>& l);

// ------------------------------------------------------------------------
// Declaration of specialized methods
// ------------------------------------------------------------------------
/*template<>
bool
Location2D<float>::operator == (const Location2D &other) const;
template<>
bool
Location2D<double>::operator == (const Location2D &other) const;*/
}

#include "location_2d_impl.hpp"

#endif // MODM_LOCATION_2D_HPP
operator<< (IOStream& os, const Location2D<T>& location) {
os << location.position << ", phi=" << location.orientation;
return os;
}
}
Loading

0 comments on commit c9c1de5

Please sign in to comment.