ScaledCSpace.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 RobotComponents
19 * @author Raphael Grimm ( raphael dot grimm at kit dot edu )
20 * @date 2015
21 * @copyright http://www.gnu.org/licenses/gpl.txt
22 * GNU General Public License
23 */
24
25#include "ScaledCSpace.h"
26
27#include <algorithm>
28
30
31namespace armarx
32{
33
34 ScaledCSpace::ScaledCSpace(const CSpaceBasePtr& cspace, const Ice::FloatSeq& scale)
35 {
37 this->originalCSpace = cspace;
38 this->scalingFactors = scale;
39 unscaled.resize(scalingFactors.size());
40
41 if (static_cast<std::size_t>(originalCSpace->getDimensionality()) != scalingFactors.size())
42 {
43 std::stringstream s;
44 s << "Different number of scaling factors (" << scalingFactors.size()
45 << ") than dimensionality of the original cspace("
46 << originalCSpace->getDimensionality() << ")";
47 ARMARX_ERROR_S << s.str();
48 throw std::invalid_argument{s.str()};
49 }
50
51 if (std::any_of(
52 scalingFactors.begin(), scalingFactors.end(), [](float f) { return f <= 0.f; }))
53 {
54 ARMARX_ERROR_S << "One or more factors are <= 0!";
55 throw std::invalid_argument{"One or more factors are <= 0!"};
56 }
57 }
58
59 void
60 ScaledCSpace::unscaleConfig(VectorXf& config) const
61 {
62 ARMARX_CHECK_EXPRESSION(config.size() == scalingFactors.size());
63 unscaleToBuffer(config.data(), config);
64 }
65
66 void
67 ScaledCSpace::unscalePath(Path& path) const
68 {
69 unscalePath(path.nodes);
70 }
71
72 void
73 ScaledCSpace::unscalePath(PathWithCost& path) const
74 {
75 unscalePath(path.nodes);
76 }
77
78 void
79 ScaledCSpace::unscalePath(VectorXfSeq& nodes) const
80 {
81 for (auto& cfg : nodes)
82 {
83 unscaleConfig(cfg);
84 }
85 }
86
87 void
88 ScaledCSpace::scaleConfig(VectorXf& config) const
89 {
90 ARMARX_CHECK_EXPRESSION(config.size() == scalingFactors.size());
91 for (std::size_t i = 0; i < scalingFactors.size(); ++i)
92 {
93 config.at(i) *= scalingFactors.at(i);
94 }
95 }
96
97 void
98 ScaledCSpace::scalePath(Path& path) const
99 {
100 for (auto& cfg : path.nodes)
101 {
102 scaleConfig(cfg);
103 }
104 }
105
106 bool
107 ScaledCSpace::isCollisionFree(const ::std::pair<const Ice::Float*, const Ice::Float*>& cfg,
108 const Ice::Current&)
109 {
110 ARMARX_CHECK_EXPRESSION(unscaled.size() ==
111 static_cast<std::size_t>(std::distance(cfg.first, cfg.second)));
112 unscaleToBuffer(cfg.first, unscaled);
113 return originalCSpace->isCollisionFree(
114 std::make_pair(unscaled.data(), unscaled.data() + unscaled.size()));
115 }
116
117 CSpaceBasePtr
118 ScaledCSpace::clone(const Ice::Current&)
119 {
120 ScaledCSpacePtr cloned{new ScaledCSpace{}};
121 cloned->scalingFactors = scalingFactors;
122 cloned->originalCSpace = originalCSpace->clone();
123 return cloned;
124 }
125
126 FloatRangeSeq
127 ScaledCSpace::getDimensionsBounds(const Ice::Current&) const
128 {
129 FloatRangeSeq bounds = originalCSpace->getDimensionsBounds();
130
131 for (std::size_t i = 0; i < scalingFactors.size(); ++i)
132 {
133 bounds.at(i).min *= scalingFactors.at(i);
134 bounds.at(i).max *= scalingFactors.at(i);
135 }
136
137 return bounds;
138 }
139
140 void
141 ScaledCSpace::unscaleToBuffer(const Ice::Float* cfg, VectorXf& buffer) const
142 {
143 ARMARX_CHECK_EXPRESSION(buffer.size() == scalingFactors.size());
144
145 for (std::size_t i = 0; i < scalingFactors.size(); ++i)
146 {
147 buffer.at(i) = cfg[i] / scalingFactors.at(i);
148 }
149 }
150} // namespace armarx
virtual void unscaleConfig(VectorXf &config) const
CSpaceBasePtr clone(const Ice::Current &=Ice::emptyCurrent) override
FloatRangeSeq getDimensionsBounds(const Ice::Current &=Ice::emptyCurrent) const override
virtual void unscalePath(Path &path) const
ScaledCSpace(const CSpaceBasePtr &cspace, const Ice::FloatSeq &scale)
ctor.
ScaledCSpace()=default
Default ctor.
void unscaleToBuffer(const Ice::Float *cfg, VectorXf &buffer) const
Unscales the given configuration to a buffer.
virtual void scalePath(Path &path) const
virtual void scaleConfig(VectorXf &config) const
bool isCollisionFree(const ::std::pair< const Ice::Float *, const Ice::Float * > &cfg, const Ice::Current &=Ice::emptyCurrent) override
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
#define ARMARX_ERROR_S
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:216
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceInternal::Handle< ScaledCSpace > ScaledCSpacePtr
An ice handle to a ScaledCSpace.