NumCpp  2.5.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
centerOfMass.hpp
Go to the documentation of this file.
1 #pragma once
29 
32 #include "NumCpp/Core/Shape.hpp"
33 #include "NumCpp/Core/Types.hpp"
34 #include "NumCpp/NdArray.hpp"
35 
36 namespace nc
37 {
38  //============================================================================
39  // Method Description:
46  template<typename dtype>
48  {
50 
51  const Shape shape = inArray.shape();
52 
53  switch (inAxis)
54  {
55  case Axis::NONE:
56  {
57  double inten = 0.0;
58  double rowCenter = 0.0;
59  double colCenter = 0.0;
60 
61  for (uint32 row = 0; row < shape.rows; ++row)
62  {
63  for (uint32 col = 0; col < shape.cols; ++col)
64  {
65  const auto pixelValue = static_cast<double>(inArray(row, col));
66 
67  inten += pixelValue;
68  rowCenter += pixelValue * static_cast<double>(row);
69  colCenter += pixelValue * static_cast<double>(col);
70  }
71  }
72 
73  rowCenter /= inten;
74  colCenter /= inten;
75 
76  return { rowCenter, colCenter };
77  }
78  case Axis::ROW:
79  {
80  NdArray<double> returnArray(1, shape.cols);
81  returnArray.zeros();
82 
83  const NdArray<double> inten = inArray.template astype<double>().sum(inAxis);
84 
85  for (uint32 colIdx = 0; colIdx < shape.cols; ++colIdx)
86  {
87  for (uint32 rowIdx = 0; rowIdx < shape.rows; ++rowIdx)
88  {
89  returnArray(0, colIdx) += static_cast<double>(inArray(rowIdx, colIdx)) * static_cast<double>(rowIdx);
90  }
91 
92  returnArray(0, colIdx) /= inten[colIdx];
93  }
94 
95  return returnArray;
96  }
97  case Axis::COL:
98  {
99  NdArray<double> returnArray(1, shape.rows);
100  returnArray.zeros();
101 
102  const NdArray<double> inten = inArray.template astype<double>().sum(inAxis);
103 
104  for (uint32 rowIdx = 0; rowIdx < shape.rows; ++rowIdx)
105  {
106  for (uint32 colIdx = 0; colIdx < shape.cols; ++colIdx)
107  {
108  returnArray(0, rowIdx) += static_cast<double>(inArray(rowIdx, colIdx)) * static_cast<double>(colIdx);
109  }
110 
111  returnArray(0, rowIdx) /= inten[rowIdx];
112  }
113 
114  return returnArray;
115  }
116  default:
117  {
118  THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
119  return {}; // get rid of compiler warning
120  }
121  }
122  }
123 } // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition: StaticAsserts.hpp:37
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4483
NdArray< dtype > sum(Axis inAxis=Axis::NONE) const
Definition: NdArrayCore.hpp:4598
NdArray< dtype > & zeros() noexcept
Definition: NdArrayCore.hpp:4848
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
uint32 rows
Definition: Core/Shape.hpp:44
uint32 cols
Definition: Core/Shape.hpp:45
Definition: Coordinate.hpp:45
Axis
Enum To describe an axis.
Definition: Types.hpp:46
NdArray< double > centerOfMass(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: centerOfMass.hpp:47
Shape shape(const NdArray< dtype > &inArray) noexcept
Definition: Functions/Shape.hpp:44
std::uint32_t uint32
Definition: Types.hpp:40