Visual Servoing Platform  version 3.6.1 under development (2025-02-19)
vpRBSilhouettePointsExtractionSettings.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 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 
36 #ifndef VP_RB_SILHOUETTE_POINTS_EXTRACTION_SETTINGS_H
37 #define VP_RB_SILHOUETTE_POINTS_EXTRACTION_SETTINGS_H
38 
39 #include <visp3/core/vpConfig.h>
40 
41 #if defined(VISP_HAVE_PANDA3D)
42 #include <visp3/core/vpMath.h>
43 #include <visp3/core/vpMatrix.h>
44 #include <visp3/core/vpImage.h>
45 #include <visp3/core/vpUniRand.h>
46 
47 
48 #if defined(VISP_HAVE_NLOHMANN_JSON)
49 #include VISP_NLOHMANN_JSON(json.hpp)
50 #endif
51 
52 
53 BEGIN_VISP_NAMESPACE
54 
61 {
62 
63 private:
64  unsigned int m_sampleStep;
65  int m_maxNumPoints;
66  unsigned int m_border;
67 
68  double m_depthThreshold;
69  bool m_thresholdIsRelative;
70  bool m_preferPreviousPoints;
71 
72  void sampleWithoutReplacement(unsigned int count, unsigned int vectorSize, std::vector<size_t> &indices, vpUniRand &random) const
73  {
74  count = std::min(count, vectorSize);
75  indices.resize(count);
76  unsigned int added = 0;
77  for (unsigned i = 0; i < vectorSize; ++i) {
78  double randomVal = random.uniform(0.0, 1.0);
79  if ((vectorSize - i) * randomVal < (count - added)) {
80  indices[added++] = i;
81  }
82  if (added == count) {
83  break;
84  }
85  }
86  }
87 
88 public:
89 
94 
95  double getThreshold() const { return m_depthThreshold; }
96  void setThreshold(double lambda) { m_depthThreshold = lambda; }
97  bool thresholdIsRelative() const { return m_thresholdIsRelative; }
98  void setThresholdIsRelative(bool isRelative) { m_thresholdIsRelative = isRelative; }
99  bool preferPreviousPoints() const { return m_preferPreviousPoints; }
100  void setPreferPreviousPoints(bool prefer) { m_preferPreviousPoints = prefer; }
101 
102 
103  int getMaxCandidates() const { return m_maxNumPoints; }
104  void setMaxCandidates(int maxCandidates) { m_maxNumPoints = maxCandidates; }
105  unsigned int getSampleStep() const { return m_sampleStep; }
106  void setSampleStep(unsigned int a)
107  {
108  if (m_sampleStep == 0) {
109  throw vpException(vpException::badValue, "Sample step should be greater than 0");
110  }
111  m_sampleStep = a;
112  }
113 
114  std::vector<std::pair<unsigned int, unsigned int>> getSilhouetteCandidates(
115  const vpImage<unsigned char> &validSilhouette, const vpImage<float> &renderDepth,
116  const vpCameraParameters &cam, const vpHomogeneousMatrix &cTcp,
117  const std::vector<vpRBSilhouettePoint> &previousPoints, long randomSeed = 41) const;
118 
119 #if defined(VISP_HAVE_NLOHMANN_JSON)
120  inline friend void from_json(const nlohmann::json &j, vpSilhouettePointsExtractionSettings &settings);
121 #endif
122 
123 };
124 
125 #if defined(VISP_HAVE_NLOHMANN_JSON)
126 inline void from_json(const nlohmann::json &j, vpSilhouettePointsExtractionSettings &settings)
127 {
128  nlohmann::json thresholdSettings = j.at("threshold");
129  std::string thresholdType = thresholdSettings.at("type");
130  settings.m_thresholdIsRelative = thresholdType == "relative";
131  settings.m_depthThreshold = thresholdSettings.at("value");
132 
133  nlohmann::json samplingSettings = j.at("sampling");
134  settings.m_preferPreviousPoints = samplingSettings.at("reusePreviousPoints");
135  settings.m_maxNumPoints = samplingSettings.at("numPoints");
136  settings.setSampleStep(samplingSettings.at("samplingRate"));
137 }
138 #endif
139 
140 END_VISP_NAMESPACE
141 
142 #endif
143 #endif
Generic class defining intrinsic camera parameters.
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
Implementation of an homogeneous matrix and operations on such kind of matrices.
Silhouette point simple candidate representation.
Class for generating random numbers with uniform probability density.
Definition: vpUniRand.h:127
int uniform(int a, int b)
Definition: vpUniRand.cpp:161