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 
28 
29 #include <algorithm>
30 
31 namespace 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(" << originalCSpace->getDimensionality() << ")";
46  ARMARX_ERROR_S << s.str();
47  throw std::invalid_argument {s.str()};
48  }
49 
50  if (std::any_of(scalingFactors.begin(), scalingFactors.end(), [](float f)
51  {
52  return f <= 0.f;
53  }))
54  {
55  ARMARX_ERROR_S << "One or more factors are <= 0!";
56  throw std::invalid_argument {"One or more factors are <= 0!"};
57  }
58  }
59 
60  void ScaledCSpace::unscaleConfig(VectorXf& config) const
61  {
62  ARMARX_CHECK_EXPRESSION(config.size() == scalingFactors.size());
63  unscaleToBuffer(config.data(), config);
64  }
65 
66  void ScaledCSpace::unscalePath(Path& path) const
67  {
68  unscalePath(path.nodes);
69  }
70 
71  void ScaledCSpace::unscalePath(PathWithCost& path) const
72  {
73  unscalePath(path.nodes);
74  }
75 
76  void ScaledCSpace::unscalePath(VectorXfSeq& nodes) const
77  {
78  for (auto& cfg : nodes)
79  {
80  unscaleConfig(cfg);
81  }
82  }
83 
84  void ScaledCSpace::scaleConfig(VectorXf& config) const
85  {
86  ARMARX_CHECK_EXPRESSION(config.size() == scalingFactors.size());
87  for (std::size_t i = 0; i < scalingFactors.size(); ++i)
88  {
89  config.at(i) *= scalingFactors.at(i);
90  }
91  }
92 
93  void ScaledCSpace::scalePath(Path& path) const
94  {
95  for (auto& cfg : path.nodes)
96  {
97  scaleConfig(cfg);
98  }
99  }
100 
101  bool ScaledCSpace::isCollisionFree(const::std::pair<const Ice::Float*, const Ice::Float*>& cfg, const Ice::Current&)
102  {
103  ARMARX_CHECK_EXPRESSION(unscaled.size() == static_cast<std::size_t>(std::distance(cfg.first, cfg.second)));
104  unscaleToBuffer(cfg.first, unscaled);
105  return originalCSpace->isCollisionFree(std::make_pair(unscaled.data(), unscaled.data() + unscaled.size()));
106  }
107 
108  CSpaceBasePtr ScaledCSpace::clone(const Ice::Current&)
109  {
110  ScaledCSpacePtr cloned {new ScaledCSpace{}};
111  cloned->scalingFactors = scalingFactors;
112  cloned->originalCSpace = originalCSpace->clone();
113  return cloned;
114  }
115 
116  FloatRangeSeq ScaledCSpace::getDimensionsBounds(const Ice::Current&) const
117  {
118  FloatRangeSeq bounds = originalCSpace->getDimensionsBounds();
119 
120  for (std::size_t i = 0; i < scalingFactors.size(); ++i)
121  {
122  bounds.at(i).min *= scalingFactors.at(i);
123  bounds.at(i).max *= scalingFactors.at(i);
124  }
125 
126  return bounds;
127  }
128 
129  void ScaledCSpace::unscaleToBuffer(const Ice::Float* cfg, VectorXf& buffer) const
130  {
131  ARMARX_CHECK_EXPRESSION(buffer.size() == scalingFactors.size());
132 
133  for (std::size_t i = 0; i < scalingFactors.size(); ++i)
134  {
135  buffer.at(i) = cfg[i] / scalingFactors.at(i);
136  }
137  }
138 }
armarx::ScaledCSpace::unscalePath
virtual void unscalePath(Path &path) const
Definition: ScaledCSpace.cpp:66
armarx::VariantType::Float
const VariantTypeId Float
Definition: Variant.h:918
ScaledCSpace.h
armarx::ScaledCSpace::scalePath
virtual void scalePath(Path &path) const
Definition: ScaledCSpace.cpp:93
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::ScaledCSpace::unscaleToBuffer
void unscaleToBuffer(const Ice::Float *cfg, VectorXf &buffer) const
Unscales the given configuration to a buffer.
Definition: ScaledCSpace.cpp:129
armarx::ScaledCSpace::clone
CSpaceBasePtr clone(const Ice::Current &=Ice::emptyCurrent) override
Definition: ScaledCSpace.cpp:108
ARMARX_ERROR_S
#define ARMARX_ERROR_S
Definition: Logging.h:209
armarx::ScaledCSpace::getDimensionsBounds
FloatRangeSeq getDimensionsBounds(const Ice::Current &=Ice::emptyCurrent) const override
Definition: ScaledCSpace.cpp:116
armarx::ScaledCSpace
Takes an other cspace and scales its' dimensions.
Definition: ScaledCSpace.h:43
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
armarx::ScaledCSpace::ScaledCSpace
ScaledCSpace()=default
Default ctor.
armarx::ScaledCSpace::scaleConfig
virtual void scaleConfig(VectorXf &config) const
Definition: ScaledCSpace.cpp:84
armarx::armem::server::ltm::detail::mixin::Path
std::filesystem::path Path
Definition: DiskStorageMixin.h:17
distance
double distance(const Point &a, const Point &b)
Definition: point.hpp:88
Logging.h
armarx::ScaledCSpace::isCollisionFree
bool isCollisionFree(const ::std::pair< const Ice::Float *, const Ice::Float * > &cfg, const Ice::Current &=Ice::emptyCurrent) override
Definition: ScaledCSpace.cpp:101
armarx::ScaledCSpace::unscaleConfig
virtual void unscaleConfig(VectorXf &config) const
Definition: ScaledCSpace.cpp:60
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28