Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
vpTemporalWeighting.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * See the file LICENSE.txt at the root directory of this source
10 * distribution for additional information about the GNU GPL.
11 *
12 * For using ViSP with software that can not be combined with the GNU
13 * GPL, please contact Inria about acquiring a ViSP Professional
14 * Edition License.
15 *
16 * See https://visp.inria.fr for more information.
17 *
18 * This software was developed at:
19 * Inria Rennes - Bretagne Atlantique
20 * Campus Universitaire de Beaulieu
21 * 35042 Rennes Cedex
22 * France
23 *
24 * If you have questions regarding the use of this file, please contact
25 * Inria at visp@inria.fr
26 *
27 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 */
30
31#include <visp3/rbt/vpTemporalWeighting.h>
32
33#include <cmath>
34
35#if defined(VISP_HAVE_NLOHMANN_JSON)
36#include VISP_NLOHMANN_JSON(json.hpp)
37#endif
38
40
41#if defined(VISP_HAVE_NLOHMANN_JSON)
42std::shared_ptr<vpTemporalWeighting> vpTemporalWeighting::parseTemporalWeighting(const nlohmann::json &j)
43{
44 if (j.is_number()) {
45 return std::make_shared<vpFixedTemporalWeighting>(j.get<double>());
46 }
47 else if (j.is_object()) {
48 bool slopeIncreasing = j.value("increasing", true);
49 double slopePower = j.value("slopePower", 1.0); // Linear increase
50
51 if ((!slopeIncreasing && slopePower > 0.0) || (slopeIncreasing && slopePower < 0.0)) {
52 slopePower = -slopePower;
53 }
54 return std::make_shared<vpSigmoidTemporalWeighting>(
55 j.value("minWeight", 0.0),
56 j.value("maxWeight", 1.0),
57 j.value("midpointLocation", 0.5),
58 slopePower
59 );
60 }
61 else {
62 throw vpException(vpException::badValue, "Wrong weighting type");
63 }
64}
65std::shared_ptr<vpTemporalWeighting> vpTemporalWeighting::parseTemporalWeightingRawJson(const std::string &s)
66{
67 nlohmann::json j = nlohmann::json::parse(s);
68 return parseTemporalWeighting(j);
69}
70
71#endif
72
73double vpFixedTemporalWeighting::weight(const double /*progress*/) const
74{
75 return m_weight;
76}
77
78double vpSigmoidTemporalWeighting::weight(const double progress) const
79{
80 double w = 1.0;
81 if (progress == 0.0 || m_location == 1.0) {
82 if (m_power > 0.0) {
83 w = 0.0;
84 }
85 else {
86 w = 1.0;
87 }
88 }
89 else {
90 double f1 = m_location / progress;
91 double f2 = (1.0 - progress) / (1.0 - m_location);
92
93 w = (1.0 / (1.0 + (std::pow(f1 * f2, m_power))));
94 }
95 return m_minWeight + w * (m_maxWeight - m_minWeight);
96}
97
98END_VISP_NAMESPACE
error that can be emitted by ViSP classes.
Definition vpException.h:60
@ badValue
Used to indicate that a value is not in the allowed range.
Definition vpException.h:73
double weight(const double) const VP_OVERRIDE
double weight(const double progress) const VP_OVERRIDE
static std::shared_ptr< vpTemporalWeighting > parseTemporalWeighting(const nlohmann::json &j)
static std::shared_ptr< vpTemporalWeighting > parseTemporalWeightingRawJson(const std::string &j)