PoseMedianOffsetFilter.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package
19 * @author
20 * @date
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24
26
28
29using namespace armarx;
30using namespace filters;
31
33 MedianFilter(windowSize)
34{
35 this->windowFilterSize = windowSize;
36 this->dataIndex = -windowSize;
37 this->offset = Eigen::Vector3f::Zero();
38 this->currentValue = Eigen::Vector3f::Zero();
39}
40
43{
44 std::unique_lock lock(historyMutex);
45
46 if (dataHistory.size() == 0)
47 {
48 return NULL;
49 }
50
51 VariantPtr var = VariantPtr::dynamicCast(dataHistory.begin()->second);
52 VariantTypeId type = var->getType();
53
54 if (type == VariantType::Vector3)
55 {
56 Vector3Ptr vecVar = new Vector3(currentValue);
57 return new Variant(vecVar);
58 }
59 else if (type == VariantType::FramedDirection)
60 {
61 FramedDirectionPtr p = var->get<FramedDirection>();
62 FramedDirectionPtr vecVar = new FramedDirection(currentValue, p->frame, p->agent);
63 return new Variant(vecVar);
64 }
65 else if (type == VariantType::FramedPosition)
66 {
67 FramedPositionPtr p = var->get<FramedPosition>();
68 FramedPositionPtr vecVar = new FramedPosition(currentValue, p->frame, p->agent);
69 return new Variant(vecVar);
70 }
71 else
72 {
73 ARMARX_WARNING_S << "Unsupported Variant Type: " << var->getTypeName();
74 return NULL;
75 }
76}
77
78armarx::ParameterTypeList
80{
81 ParameterTypeList result = MedianFilter::getSupportedTypes(c);
82 result.push_back(VariantType::Vector3);
83 result.push_back(VariantType::FramedDirection);
84 result.push_back(VariantType::FramedPosition);
85 return result;
86}
87
88float
89armarx::filters::PoseMedianOffsetFilter::median(std::vector<float>& values)
90{
91 std::sort(values.begin(), values.end());
92 return values.size() % 2 == 0
93 ? (values.at(values.size() / 2 - 1) + values.at(values.size() / 2)) / 2
94 : values.at(values.size() / 2);
95}
96
97Eigen::Vector3f
98armarx::filters::PoseMedianOffsetFilter::calculateMedian()
99{
100 Eigen::Vector3f result;
101 for (int i = 0; i < 3; ++i)
102 {
103 std::vector<float> values;
104 values.reserve(data.size());
105
106 for (const Eigen::Vector3f& v : data)
107 {
108 values.push_back(v(i));
109 }
110 result(i) = median(values);
111 }
112 return result;
113}
114
115void
117 const armarx::VariantBasePtr& value,
118 const Ice::Current& c)
119{
120 VariantTypeId type = value->getType();
121 if (type == VariantType::Vector3 || type == VariantType::FramedDirection ||
123 {
124 Eigen::Vector3f currentValue = VariantPtr::dynamicCast(value)->get<Vector3>()->toEigen();
125 if (dataIndex < 0)
126 {
127 data.push_back(currentValue);
128 this->currentValue = Eigen::Vector3f::Zero();
129 dataIndex++;
130 if (dataIndex == 0)
131 {
132 offset = calculateMedian();
133 }
134 }
135 else
136 {
137 data.at(dataIndex) = currentValue;
138 dataIndex = (dataIndex + 1) % windowFilterSize;
139 this->currentValue = calculateMedian() - offset;
140 }
141 }
142 else
143 {
144 ARMARX_WARNING_S << "Unsupported Variant Type: " << value->getTypeName();
145 }
147}
std::string timestamp()
constexpr T c
void update(Ice::Long timestamp, const VariantBasePtr &value, const Ice::Current &c=Ice::emptyCurrent) override
Adds the given value to the data map, erases old values if maximum size was reached,...
TimeVariantBaseMap dataHistory
FramedDirection is a 3 dimensional direction vector with a reference frame.
Definition FramedPose.h:87
The FramedPosition class.
Definition FramedPose.h:158
The Variant class is described here: Variants.
Definition Variant.h:224
The Vector3 class.
Definition Pose.h:113
MedianFilter(int windowSize=11)
ParameterTypeList getSupportedTypes(const Ice::Current &c=Ice::emptyCurrent) const override
This filter supports: Int, Long, Float, Double.
VariantBasePtr calculate(const Ice::Current &c) const override
void update(Ice::Long timestamp, const VariantBasePtr &value, const Ice::Current &c) override
ParameterTypeList getSupportedTypes(const Ice::Current &c) const override
This filter supports: Vector3, FramedDirection, FramedPosition.
#define ARMARX_WARNING_S
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:213
const VariantTypeId FramedPosition
Definition FramedPose.h:38
const VariantTypeId FramedDirection
Definition FramedPose.h:37
const VariantTypeId Vector3
Definition Pose.h:38
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceInternal::Handle< Vector3 > Vector3Ptr
Definition Pose.h:165
IceInternal::Handle< FramedDirection > FramedDirectionPtr
Definition FramedPose.h:84
IceInternal::Handle< Variant > VariantPtr
Definition Variant.h:41
IceInternal::Handle< FramedPosition > FramedPositionPtr
Definition FramedPose.h:149
Ice::Int VariantTypeId
Definition Variant.h:43
::IceInternal::Handle<::armarx::VariantBase > VariantBasePtr
Eigen::Vector3f toEigen(const pcl::PointXYZ &pt)