Skip to content

Commit

Permalink
common: Drop custom functions for one and zero
Browse files Browse the repository at this point in the history
These are pretty silly and a bit too magical. Just use casts when it is
neccessary.
  • Loading branch information
StollD committed Apr 20, 2024
1 parent 6c79da8 commit 78bfa69
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 82 deletions.
26 changes: 0 additions & 26 deletions src/common/constants.hpp

This file was deleted.

8 changes: 4 additions & 4 deletions src/contacts/contact.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#ifndef IPTSD_CONTACTS_CONTACT_HPP
#define IPTSD_CONTACTS_CONTACT_HPP

#include <common/constants.hpp>
#include <common/casts.hpp>
#include <common/types.hpp>

#include <optional>
Expand All @@ -22,21 +22,21 @@ class Contact {
*
* Range: [0, 1] if normalized, [0, <input dimensions>] if not.
*/
Vector2<T> mean {Zero<T>(), Zero<T>()};
Vector2<T> mean = Vector2<T>::Zero();

/*
* The size of the contact (diameter of major and minor axis).
*
* Range: [0, 1] if normalized, [0, <hypot of input dimensions>] if not.
*/
Vector2<T> size {Zero<T>(), Zero<T>()};
Vector2<T> size = Vector2<T>::Zero();

/*
* The orientation of the contact.
*
* Range: [0, 1) if normalized, [0, pi) if not.
*/
T orientation = Zero<T>();
T orientation = casts::to<T>(0);

/*
* Whether the stored values are normalized.
Expand Down
6 changes: 4 additions & 2 deletions src/contacts/detection/algorithms/convolution.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ void run_generic(const DenseBase<DerivedData> &in,
{
using T = typename DenseBase<DerivedKernel>::Scalar;

constexpr isize size_zero = 0;

const Eigen::Index cols = in.cols();
const Eigen::Index rows = in.rows();

Expand All @@ -47,13 +49,13 @@ void run_generic(const DenseBase<DerivedData> &in,

for (Eigen::Index oy = 0; oy < rows; oy++) {
const isize sy = casts::to_signed(oy + ky) - dy;
const isize cy = std::clamp(sy, Zero<isize>(), rows - 1);
const isize cy = std::clamp(sy, size_zero, rows - 1);

const Eigen::Index iy = casts::to_eigen(cy);

for (Eigen::Index ox = 0; ox < cols; ox++) {
const isize sx = casts::to_signed(ox + kx) - dx;
const isize cx = std::clamp(sx, Zero<isize>(), cols - 1);
const isize cx = std::clamp(sx, size_zero, cols - 1);

const Eigen::Index ix = casts::to_eigen(cx);

Expand Down
5 changes: 2 additions & 3 deletions src/contacts/detection/algorithms/gaussian.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#define IPTSD_CONTACTS_DETECTION_ALGORITHMS_GAUSSIAN_HPP

#include <common/casts.hpp>
#include <common/constants.hpp>
#include <common/types.hpp>

#include <gsl/gsl>
Expand Down Expand Up @@ -222,7 +221,7 @@ void update_weight_maps(std::vector<Parameters<typename DenseBase<Derived>::Scal
for (Eigen::Index x = bmin.x(); x <= bmax.x(); x++) {
const T t = total(y, x);

if (t > Zero<T>())
if (t > casts::to<T>(0))
p.weights(y - bmin.y(), x - bmin.x()) /= t;
}
}
Expand Down Expand Up @@ -250,7 +249,7 @@ bool ge_solve(Matrix6<T> a, Vector6<T> b, Vector6<T> &x)
{
// step 1: find element with largest absolute value in column
Eigen::Index r = 0;
T v = Zero<T>();
T v = casts::to<T>(0);

for (Eigen::Index i = c; i < 6; ++i) {
const T vi = std::abs(a(c, i));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <common/buildopts.hpp>
#include <common/casts.hpp>
#include <common/constants.hpp>
#include <common/types.hpp>

namespace iptsd::contacts::detection::convolution::impl {
Expand Down Expand Up @@ -58,7 +57,7 @@ inline void run_3x3(const DenseBase<DerivedData> &in,
{
// x = 0
{
auto v = Zero<T>();
auto v = casts::to<T>(0);

v += d(i, 0, 0) * k(-1, -1); // extended
v += d(i, 0, 0) * k(0, -1); // extended
Expand All @@ -78,7 +77,7 @@ inline void run_3x3(const DenseBase<DerivedData> &in,
// 0 < x < n
const auto limit = i + cols - 2;
while (i < limit) {
auto v = Zero<T>();
auto v = casts::to<T>(0);

v += d(i, -1, 0) * k(-1, -1); // extended
v += d(i, 0, 0) * k(0, -1); // extended
Expand All @@ -97,7 +96,7 @@ inline void run_3x3(const DenseBase<DerivedData> &in,

// x = n - 1
{
auto v = Zero<T>();
auto v = casts::to<T>(0);

v += d(i, -1, 0) * k(-1, -1); // extended
v += d(i, 0, 0) * k(0, -1); // extended
Expand All @@ -119,7 +118,7 @@ inline void run_3x3(const DenseBase<DerivedData> &in,
while (i < cols * (rows - 1)) {
// x = 0
{
auto v = Zero<T>();
auto v = casts::to<T>(0);

v += d(i, 0, -1) * k(-1, -1); // extended
v += d(i, 0, -1) * k(0, -1);
Expand All @@ -139,7 +138,7 @@ inline void run_3x3(const DenseBase<DerivedData> &in,
// 0 < x < n
const auto limit = i + cols - 2;
while (i < limit) {
auto v = Zero<T>();
auto v = casts::to<T>(0);

v += d(i, -1, -1) * k(-1, -1);
v += d(i, 0, -1) * k(0, -1);
Expand All @@ -158,7 +157,7 @@ inline void run_3x3(const DenseBase<DerivedData> &in,

// x = n - 1
{
auto v = Zero<T>();
auto v = casts::to<T>(0);

v += d(i, -1, -1) * k(-1, -1);
v += d(i, 0, -1) * k(0, -1);
Expand All @@ -180,7 +179,7 @@ inline void run_3x3(const DenseBase<DerivedData> &in,
{
// x = 0
{
auto v = Zero<T>();
auto v = casts::to<T>(0);

v += d(i, 0, -1) * k(-1, -1); // extended
v += d(i, 0, -1) * k(0, -1);
Expand All @@ -200,7 +199,7 @@ inline void run_3x3(const DenseBase<DerivedData> &in,
// 1 < x < n - 2
const auto limit = i + cols - 2;
while (i < limit) {
auto v = Zero<T>();
auto v = casts::to<T>(0);

v += d(i, -1, -1) * k(-1, -1);
v += d(i, 0, -1) * k(0, -1);
Expand All @@ -219,7 +218,7 @@ inline void run_3x3(const DenseBase<DerivedData> &in,

// x = n - 1
{
auto v = Zero<T>();
auto v = casts::to<T>(0);

v += d(i, -1, -1) * k(-1, -1);
v += d(i, 0, -1) * k(0, -1);
Expand Down
Loading

0 comments on commit 78bfa69

Please sign in to comment.