17 #include <mrpt/config.h>
20 #include <vtkStructuredGrid.h>
21 #include <vtkDoubleArray.h>
22 #include <vtkPointData.h>
23 #include <vtkVersion.h>
24 #include <vtkCellArray.h>
25 #include <vtkPoints.h>
26 #include <vtkXMLStructuredGridWriter.h>
27 #include <vtkStructuredGrid.h>
28 #include <vtkSmartPointer.h>
42 double x_min,
double x_max,
43 double y_min,
double y_max,
44 double z_min,
double z_max,
46 bool call_initialize_now
51 if (call_initialize_now)
52 this->internal_initialize();
57 const double x_min,
const double x_max,
58 const double y_min,
const double y_max,
59 const double z_min,
const double z_max,
60 const double resolution_xy,
const double resolution_z,
66 this->internal_initialize();
72 double new_x_min,
double new_x_max,
73 double new_y_min,
double new_y_max,
74 double new_z_min,
double new_z_max,
80 this->internal_initialize(
false);
88 internal_initialize();
93 if (erase_prev_contents)
101 m_gmrf.setVerbosityLevel(this->getMinLoggingLevel());
102 if (erase_prev_contents)
105 m_mrf_factors_activeObs.clear();
110 m_gmrf.clearAllConstraintsByType_Binary();
112 m_mrf_factors_priors.clear();
115 const size_t nodeCount = m_map.size();
119 MRPT_LOG_DEBUG_STREAM(
"[internal_initialize] Creating priors for GMRF with " << nodeCount <<
" nodes." << std::endl);
123 m_mrf_factors_activeObs.resize(nodeCount);
124 m_gmrf.initialize(nodeCount);
128 size_t cx = 0, cy = 0, cz = 0;
129 for (
size_t j = 0; j<nodeCount; j++)
136 const size_t c_idx_to_check[3] = { cx,cy,cz };
137 const size_t c_idx_to_check_limits[3] = { m_size_x - 1,m_size_y - 1,m_size_z - 1 };
138 const size_t c_neighbor_idx_incr[3] = { 1,m_size_x,m_size_x_times_y };
140 for (
int dir = 0; dir < 3; dir++)
142 if (c_idx_to_check[dir] >= c_idx_to_check_limits[dir])
145 const size_t i = j + c_neighbor_idx_incr[dir];
148 double edge_lamdba = .0;
149 if (custom_connectivity != NULL)
154 cx + (dir == 0 ? 1 : 0), cy + (dir == 1 ? 1 : 0), cz+ (dir==2 ? 1:0),
162 edge_lamdba = insertionOptions.GMRF_lambdaPrior;
168 new_prior.
Lambda = edge_lamdba;
170 m_mrf_factors_priors.push_back(new_prior);
171 m_gmrf.addConstraint(*m_mrf_factors_priors.rbegin());
175 if (++cx >= m_size_x) {
177 if (++cy >= m_size_y) {
191 GMRF_lambdaPrior ( 0.01f ),
192 GMRF_skip_variance (false)
198 out.
printf(
"GMRF_lambdaPrior = %f\n", GMRF_lambdaPrior);
199 out.
printf(
"GMRF_skip_variance = %s\n", GMRF_skip_variance ?
"true":
"false");
206 GMRF_lambdaPrior = iniFile.
read_double(section.c_str(),
"GMRF_lambdaPrior", GMRF_lambdaPrior);
207 GMRF_skip_variance = iniFile.
read_bool(section.c_str(),
"GMRF_skip_variance", GMRF_skip_variance);
216 vtkStructuredGrid *vtkGrid = vtkStructuredGrid::New();
220 vtkSmartPointer<vtkXMLStructuredGridWriter> writer = vtkSmartPointer<vtkXMLStructuredGridWriter>::New();
221 writer->SetFileName( fil.c_str() );
223 #if VTK_MAJOR_VERSION <= 5
224 writer->SetInput(vtkGrid);
226 writer->SetInputData(vtkGrid);
229 int ret = writer->Write();
245 if (!f_mean.
open(filName_mean)) {
248 f_mean.
printf(
"x coord, y coord, z coord, scalar\n");
251 if (!filName_stddev.empty()) {
252 if (!f_stddev.
open(filName_stddev)) {
255 f_mean.
printf(
"x coord, y coord, z coord, scalar\n");
259 const size_t nodeCount =
m_map.size();
260 size_t cx = 0, cy = 0, cz = 0;
261 for (
size_t j = 0; j<nodeCount; j++)
264 const double mean_val =
m_map[j].mean_value;
265 const double stddev_val =
m_map[j].stddev_value;
267 f_mean.
printf(
"%f, %f, %f, %e\n",
x,
y,
z, mean_val);
270 f_stddev.
printf(
"%f, %f, %f, %e\n",
x,
y,
z, stddev_val);
289 Eigen::VectorXd x_incr, x_var;
296 for (
size_t j = 0; j<
m_map.size(); j++)
298 m_map[j].mean_value += x_incr[j];
309 const double sensorReading,
310 const double sensorVariance,
313 const bool update_map
328 new_obs.
Lambda = 1.0 / sensorVariance;
358 #if MRPT_IS_BIG_ENDIAN
361 out <<
m_map[i].mean_value <<
m_map[i].stddev_value;
391 #if MRPT_IS_BIG_ENDIAN
415 const int num_values =
nx*
ny*
nz;
417 vtkPoints* newPoints = vtkPoints::New();
419 vtkDoubleArray* newData = vtkDoubleArray::New();
420 newData->SetNumberOfComponents(3);
421 newData->SetNumberOfTuples(num_values);
423 vtkDoubleArray* mean_arr = vtkDoubleArray::New();
424 mean_arr->SetNumberOfComponents(1);
425 mean_arr->SetNumberOfTuples(num_values);
427 vtkDoubleArray* std_arr = vtkDoubleArray::New();
428 std_arr->SetNumberOfComponents(1);
429 std_arr->SetNumberOfTuples(num_values);
431 vtkIdType numtuples = newData->GetNumberOfTuples();
434 size_t cx = 0, cy = 0, cz = 0;
435 for (vtkIdType cc = 0; cc < numtuples; cc++)
439 newData->SetComponent(cc, 0,
x);
440 newData->SetComponent(cc, 1,
y);
441 newData->SetComponent(cc, 2,
z);
443 mean_arr->SetComponent(cc, 0,
m_map[cc].mean_value);
444 std_arr->SetComponent(cc, 0,
m_map[cc].stddev_value);
455 ASSERT_(
size_t(
m_map.size() ) ==
size_t( numtuples ) );
458 newPoints->SetData(newData);
461 output->SetExtent(0,
nx-1, 0,
ny-1, 0,
nz-1);
462 output->SetPoints(newPoints);
465 mean_arr->SetName(label_mean.c_str());
466 std_arr->SetName(label_stddev.c_str());
467 output->GetPointData()->AddArray(mean_arr);
468 output->GetPointData()->AddArray(std_arr);
482 return m_parent->m_map[this->node_id].mean_value - this->obsValue;
495 return m_parent->m_map[this->node_id_i].mean_value - m_parent->m_map[this->node_id_j].mean_value;
#define MRPT_LOG_DEBUG_STREAM(__CONTENTS)
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
void updateEstimation(Eigen::VectorXd &solved_x_inc, Eigen::VectorXd *solved_variances=NULL)
void addConstraint(const UnaryFactorVirtualBase &listOfConstraints)
Insert constraints into the GMRF problem.
CRandomFieldGridMap3D represents a 3D regular grid where each voxel is associated one real-valued pro...
std::vector< std::deque< TObservationGMRF > > m_mrf_factors_activeObs
Vector with the active observations and their respective Information, for each map cell.
bool insertIndividualReading(const double sensorReading, const double sensorVariance, const mrpt::math::TPoint3D &point, const TVoxelInterpolationMethod method, const bool update_map)
Direct update of the map with a reading in a given position of the map.
ConnectivityDescriptorPtr m_gmrf_connectivity
Empty: default.
void setVoxelsConnectivity(const ConnectivityDescriptorPtr &new_connectivity_descriptor)
Sets a custom object to define the connectivity between voxels.
void updateMapEstimation()
Run the method-specific procedure required to ensure that the mean & variances are up-to-date with al...
TVoxelInterpolationMethod
TInsertionOptions insertionOptions
void internal_initialize(bool erase_prev_contents=true)
Internal: called called after each change of resolution, size, etc.
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const
Introduces a pure virtual method responsible for writing to a CStream.
virtual void setSize(const double x_min, const double x_max, const double y_min, const double y_max, const double z_min, const double z_max, const double resolution_xy, const double resolution_z=-1.0, const TRandomFieldVoxel *fill_value=NULL) MRPT_OVERRIDE
Changes the size of the grid, erasing previous contents.If resolution_z<0, the same resolution will b...
mrpt::graphs::ScalarFactorGraph m_gmrf
bool saveAsVtkStructuredGrid(const std::string &fil) const
Save the current estimated grid to a VTK file (.vts) as a "structured grid".
void clear() MRPT_OVERRIDE
Erases all added observations and start again with an empty gridmap.
std::shared_ptr< ConnectivityDescriptor > ConnectivityDescriptorPtr
virtual void resize(double new_x_min, double new_x_max, double new_y_min, double new_y_max, double new_z_min, double new_z_max, const TRandomFieldVoxel &defaultValueNewvoxels, double additionalMarginMeters=2.0) MRPT_OVERRIDE
Changes the size of the grid, maintaining previous contents.
bool saveAsCSV(const std::string &filName_mean, const std::string &filName_stddev=std::string()) const
Save the current estimated mean values to a CSV file (compatible with Paraview) with fields x y z mea...
void readFromStream(mrpt::utils::CStream &in, int version)
Introduces a pure virtual method responsible for loading from a CStream This can not be used directly...
void getAsVtkStructuredGrid(vtkStructuredGrid *output, const std::string &label_mean=std::string("mean"), const std::string &label_stddev=std::string("stddev")) const
Returns the 3D grid contents as an VTK grid.
This class allows loading and storing values and vectors of different types from a configuration text...
bool read_bool(const std::string §ion, const std::string &name, bool defaultValue, bool failIfNotFound=false) const
double read_double(const std::string §ion, const std::string &name, double defaultValue, bool failIfNotFound=false) const
A 3D rectangular grid of dynamic size which stores any kind of data at each voxel.
double idx2z(int cz) const
double idx2x(int cx) const
Transform a voxel index into a coordinate value of the voxel central point.
std::vector< TRandomFieldVoxel > m_map
The cells.
static const size_t INVALID_VOXEL_IDX
int z2idx(double z) const
int x2idx(double x) const
Transform a coordinate values into voxel indexes.
int y2idx(double y) const
void dyngridcommon_writeToStream(mrpt::utils::CStream &out) const
Serialization of all parameters, except the contents of each voxel (responsability of the derived cla...
size_t cellAbsIndexFromCXCYCZ(const int cx, const int cy, const int cz) const
Gets the absolute index of a voxel in the linear container m_map[] from its cx,cy,...
virtual void clear()
Erase the contents of all the cells, setting them to their default values (default ctor).
double idx2y(int cy) const
void dyngridcommon_readFromStream(mrpt::utils::CStream &in)
Serialization of all parameters, except the contents of each voxel (responsability of the derived cla...
This CStream derived class allow using a file as a write-only, binary stream.
bool is_open()
Returns true if the file was open without errors.
bool open(const std::string &fileName, bool append=false)
Open the given file for write.
The virtual base class which provides a unified interface for all persistent objects in MRPT.
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
void WriteBuffer(const void *Buffer, size_t Count)
Writes a block of bytes to the stream from Buffer.
virtual int printf(const char *fmt,...) MRPT_printf_format_check(2
Writes a string to the stream in a textual form.
This class implements a high-performance stopwatch.
double Tac()
Stops the stopwatch.
void Tic()
Starts the stopwatch.
EIGEN_STRONG_INLINE void fill(const Scalar v)
EIGEN_STRONG_INLINE void setSize(size_t row, size_t col)
Changes the size of matrix, maintaining its previous content as possible and padding with zeros where...
GLfloat GLfloat GLfloat GLfloat nx
GLsizei const GLchar ** string
#define ASSERT_EQUAL_(__A, __B)
#define ASSERT_ABOVE_(__A, __B)
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
#define THROW_EXCEPTION(msg)
#define ASSERTMSG_(f, __ERROR_MSG)
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values,...
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
unsigned __int32 uint32_t
Base class for user-supplied objects capable of describing voxels connectivity, used to build prior f...
virtual bool getEdgeInformation(const CRandomFieldGridMap3D *parent, size_t icx, size_t icy, size_t icz, size_t jcx, size_t jcy, size_t jcz, double &out_edge_information)=0
Implement the check of whether node i=(icx,icy,icz) is connected with node j=(jcx,...
void loadFromConfigFile(const mrpt::utils::CConfigFileBase &source, const std::string §ion)
See utils::CLoadableOptions.
TInsertionOptions()
Default values loader.
bool GMRF_skip_variance
(Default:false) Skip the computation of the variance, just compute the mean
void dumpToTextStream(mrpt::utils::CStream &out) const
See utils::CLoadableOptions.
double GMRF_lambdaPrior
The information (Lambda) of fixed map constraints.
double obsValue
Observation value.
double Lambda
"Information" of the observation (=inverse of the variance)
double getInformation() const MRPT_OVERRIDE
Return the inverse of the variance of this constraint.
double evaluateResidual() const MRPT_OVERRIDE
Return the residual/error of this observation.
void evalJacobian(double &dr_dx) const MRPT_OVERRIDE
Returns the derivative of the residual wrt the node value.
double Lambda
"Information" of the observation (=inverse of the variance)
double evaluateResidual() const MRPT_OVERRIDE
Return the residual/error of this observation.
void evalJacobian(double &dr_dx_i, double &dr_dx_j) const MRPT_OVERRIDE
Returns the derivative of the residual wrt the node values.
double getInformation() const MRPT_OVERRIDE
Return the inverse of the variance of this constraint.
The contents of each voxel in a CRandomFieldGridMap3D map.
double z
X,Y,Z coordinates.