NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
centerOfMass.hpp
Go to the documentation of this file.
1
28#pragma once
29
31#include "NumCpp/Core/Shape.hpp"
32#include "NumCpp/Core/Types.hpp"
33#include "NumCpp/NdArray.hpp"
34
35namespace nc
36{
37 //============================================================================
38 // Method Description:
45 template<typename dtype>
47 {
49
50 const Shape shape = inArray.shape();
51
52 switch (inAxis)
53 {
54 case Axis::NONE:
55 {
56 double inten = 0.;
57 double rowCenter = 0.;
58 double colCenter = 0.;
59
60 for (uint32 row = 0; row < shape.rows; ++row)
61 {
62 for (uint32 col = 0; col < shape.cols; ++col)
63 {
64 const auto pixelValue = static_cast<double>(inArray(row, col));
65
66 inten += pixelValue;
67 rowCenter += pixelValue * static_cast<double>(row);
68 colCenter += pixelValue * static_cast<double>(col);
69 }
70 }
71
72 rowCenter /= inten;
73 colCenter /= inten;
74
75 return { rowCenter, colCenter };
76 }
77 case Axis::ROW:
78 {
79 NdArray<double> returnArray(1, shape.cols);
80 returnArray.zeros();
81
82 const NdArray<double> inten = inArray.template astype<double>().sum(inAxis);
83
84 for (uint32 colIdx = 0; colIdx < shape.cols; ++colIdx)
85 {
86 for (uint32 rowIdx = 0; rowIdx < shape.rows; ++rowIdx)
87 {
88 returnArray(0, colIdx) +=
89 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) +=
109 static_cast<double>(inArray(rowIdx, colIdx)) * static_cast<double>(colIdx);
110 }
111
112 returnArray(0, rowIdx) /= inten[rowIdx];
113 }
114
115 return returnArray;
116 }
117 default:
118 {
119 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
120 return {}; // get rid of compiler warning
121 }
122 }
123 }
124} // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition Error.hpp:37
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition StaticAsserts.hpp:39
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition NdArrayCore.hpp:135
self_type & zeros() noexcept
Definition NdArrayCore.hpp:4977
const Shape & shape() const noexcept
Definition NdArrayCore.hpp:4587
self_type sum(Axis inAxis=Axis::NONE) const
Definition NdArrayCore.hpp:4694
A Shape Class for NdArrays.
Definition Core/shape.hpp:41
Definition Cartesian.hpp:39
Axis
Enum To describe an axis.
Definition Enums.hpp:36
@ ROW
Definition Enums.hpp:38
@ COL
Definition Enums.hpp:39
@ NONE
Definition Enums.hpp:37
NdArray< double > centerOfMass(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition centerOfMass.hpp:46
Shape shape(const NdArray< dtype > &inArray) noexcept
Definition Functions/shape.hpp:42
NdArray< dtypeOut > astype(const NdArray< dtype > inArray)
Definition astype.hpp:43
std::uint32_t uint32
Definition Types.hpp:40