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 ASSERT_EQUAL_(__A, __B)
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.
int z2idx(double z) const
bool read_bool(const std::string §ion, const std::string &name, bool defaultValue, bool failIfNotFound=false) const
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
EIGEN_STRONG_INLINE void fill(const Scalar v)
#define ASSERT_ABOVE_(__A, __B)
mrpt::graphs::ScalarFactorGraph m_gmrf
double evaluateResidual() const MRPT_OVERRIDE
Return the residual/error of this observation.
std::vector< TRandomFieldVoxel > m_map
The cells.
The virtual base class which provides a unified interface for all persistent objects in MRPT...
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,cz indices, or -1 if out of map bounds (in any dimension).
The contents of each voxel in a CRandomFieldGridMap3D map.
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const
Introduces a pure virtual method responsible for writing to a CStream.
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
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,jcy,jcy).
double idx2z(int cz) const
double GMRF_lambdaPrior
The information (Lambda) of fixed map constraints.
static const size_t INVALID_VOXEL_IDX
#define THROW_EXCEPTION(msg)
void WriteBuffer(const void *Buffer, size_t Count)
Writes a block of bytes to the stream from Buffer.
void internal_initialize(bool erase_prev_contents=true)
Internal: called called after each change of resolution, size, etc.
bool open(const std::string &fileName, bool append=false)
Open the given file for write.
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.
void dyngridcommon_writeToStream(mrpt::utils::CStream &out) const
Serialization of all parameters, except the contents of each voxel (responsability of the derived cla...
virtual void clear()
Erase the contents of all the cells, setting them to their default values (default ctor)...
void clear() MRPT_OVERRIDE
Erases all added observations and start again with an empty gridmap.
double z
X,Y,Z coordinates.
A 3D rectangular grid of dynamic size which stores any kind of data at each voxel.
void Tic()
Starts the stopwatch.
This class allows loading and storing values and vectors of different types from a configuration text...
ConnectivityDescriptorPtr m_gmrf_connectivity
Empty: default.
void loadFromConfigFile(const mrpt::utils::CConfigFileBase &source, const std::string §ion)
See utils::CLoadableOptions.
std::shared_ptr< ConnectivityDescriptor > ConnectivityDescriptorPtr
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
This CStream derived class allow using a file as a write-only, binary stream.
double Lambda
"Information" of the observation (=inverse of the variance)
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
std::vector< std::deque< TObservationGMRF > > m_mrf_factors_activeObs
Vector with the active observations and their respective Information, for each map cell...
This class implements a high-performance stopwatch.
double Lambda
"Information" of the observation (=inverse of the variance)
double getInformation() const MRPT_OVERRIDE
Return the inverse of the variance of this constraint.
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...
void evalJacobian(double &dr_dx) const MRPT_OVERRIDE
Returns the derivative of the residual wrt the node value.
GLsizei const GLchar ** string
TInsertionOptions()
Default values loader.
double obsValue
Observation value.
void addConstraint(const UnaryFactorVirtualBase &listOfConstraints)
Insert constraints into the GMRF problem.
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...
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
CRandomFieldGridMap3D represents a 3D regular grid where each voxel is associated one real-valued pro...
GLfloat GLfloat GLfloat GLfloat nx
int x2idx(double x) const
Transform a coordinate values into voxel indexes.
double idx2x(int cx) const
Transform a voxel index into a coordinate value of the voxel central point.
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...
double read_double(const std::string §ion, const std::string &name, double defaultValue, bool failIfNotFound=false) const
double idx2y(int cy) const
TVoxelInterpolationMethod
void setVoxelsConnectivity(const ConnectivityDescriptorPtr &new_connectivity_descriptor)
Sets a custom object to define the connectivity between voxels.
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.
void dyngridcommon_readFromStream(mrpt::utils::CStream &in)
Serialization of all parameters, except the contents of each voxel (responsability of the derived cla...
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...
bool saveAsVtkStructuredGrid(const std::string &fil) const
Save the current estimated grid to a VTK file (.vts) as a "structured grid".
double Tac()
Stops the stopwatch.
void evalJacobian(double &dr_dx_i, double &dr_dx_j) const MRPT_OVERRIDE
Returns the derivative of the residual wrt the node values.
void updateEstimation(Eigen::VectorXd &solved_x_inc, Eigen::VectorXd *solved_variances=NULL)
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.
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.
void updateMapEstimation()
Run the method-specific procedure required to ensure that the mean & variances are up-to-date with al...
bool is_open()
Returns true if the file was open without errors.
unsigned __int32 uint32_t
#define MRPT_LOG_DEBUG_STREAM(__CONTENTS)
#define ASSERTMSG_(f, __ERROR_MSG)
int y2idx(double y) const
TInsertionOptions insertionOptions
virtual int printf(const char *fmt,...) MRPT_printf_format_check(2
Writes a string to the stream in a textual form.
Base class for user-supplied objects capable of describing voxels connectivity, used to build prior f...