CPPxDIC
C++ Digital Image Correlation library for fingertip 3D reconstruction
Loading...
Searching...
No Matches
mat_reader.h
Go to the documentation of this file.
1
6#ifndef MAT_READER_H
7#define MAT_READER_H
8
9#include <matio.h>
10#include <opencv2/core.hpp>
11#include <string>
12#include <vector>
13#include <map>
14#include <memory>
15
16namespace cppxdic {
17
18// DLT calibration data (loaded from DLTstruct_cam_<id>.mat)
19// Matches MATLAB DLTstructCam struct fields
21 std::vector<double> DLTparams; // 11 DLT parameters
22 std::vector<double> C3Dtrue; // 3D calibration points (dim0 x dim1 x 3, column-major flattened to row-major)
23 size_t C3Dtrue_dim0 = 0; // First dimension (e.g., 10)
24 size_t C3Dtrue_dim1 = 0; // Second dimension (e.g., 45)
25 std::vector<double> imageCentroids; // 2D image centroids (Nx2, row-major)
26 size_t imageCentroids_rows = 0; // Number of centroid points
27 std::vector<uint8_t> columns; // Column indices used in calibration
28 int indCam = 0; // Camera index
29 std::string filePath; // Source file path (for traceability)
30};
31
32// Protocol file data structure (loaded from MAT file)
34 struct TrialEntry {
36 std::string direction; // "Ubnf", "Rbnf", etc.
37 double force; // Normal force in N
38 double speed; // Speed in mm/s
39 int repetition; // Repetition number
40 };
41
42 std::vector<std::string> titles; // Column names
43 std::vector<TrialEntry> trials; // Trial information
44 size_t n_trials;
45 size_t n_fields;
46};
47
52class MatReader {
53public:
54 // -------- RAII helpers (reusable across the project) --------
56 void operator()(mat_t* f) const noexcept { if (f) Mat_Close(f); }
57 };
59 void operator()(matvar_t* v) const noexcept { if (v) Mat_VarFree(v); }
60 };
61
62 using MatFilePtr = std::unique_ptr<mat_t, MatFileDeleter>;
63 using MatVarPtr = std::unique_ptr<matvar_t, MatVarDeleter>;
64
65 // Optional convenience helpers (non-breaking additions)
66 static MatFilePtr openMat(const std::string& path);
67 static MatVarPtr readVar(mat_t* matfp, const std::string& name); // top-level
68 static MatVarPtr readFirstVar(mat_t* matfp); // first variable in file
69
70 // DLT calibration loading (DLTstruct_cam_<id>.mat)
71 static bool loadDLTCalibration(const std::string& mat_path, DLTCalibrationData& calib);
72
73 // Protocol loading
74 static bool loadProtocol(const std::string& protocol_path, ProtocolFileData& protocol);
75 static std::vector<std::string> readCellStrings(matvar_t* cell_var);
76
85 static matvar_t* readStructField(mat_t* matfp,
86 const std::string& struct_name,
87 const std::string& field_name);
88
96 static matvar_t* readNestedField(mat_t* matfp, const std::string& path);
97
104 static std::vector<double> readDoubleArray(matvar_t* var);
105
114 static std::vector<double> readDouble2DArray(matvar_t* var, size_t& rows, size_t& cols);
115
122 static cv::Mat readImage(matvar_t* var);
123
130 static double readScalar(matvar_t* var);
131
138 static std::string readString(matvar_t* var);
139
146 static size_t getCellArraySize(matvar_t* var);
147
155 static matvar_t* getCellElement(matvar_t* var, size_t index);
156
163 static std::map<std::string, double> readDispInfo(mat_t* matfp);
164
172 static bool variableExists(mat_t* matfp, const std::string& var_name);
173
181 static cv::Mat readROIMask(const std::string& mat_path,
182 const std::string& var_name = "refmask");
183
191 static bool readDIC3Dcombined(const std::string& mat_path,
192 struct DIC3Dcombined& combined);
193
202 static bool readDIC2DPairResults(const std::string& mat_path,
203 struct DIC2DPairResults& result);
204
211 static std::vector<int> readIntArray(matvar_t* var);
212
213private:
214 // Internal helpers to reduce repetition across all readers
215 static matvar_t* getStructField(matvar_t* s, const char* field, size_t index = 0) noexcept;
216 static bool readDIC2DPairResultsFromStruct(matvar_t* dic2d_root,
217 struct DIC2DPairResults& result);
218
219 static bool readTopInt(mat_t* matfp, const char* name, int& out) noexcept;
220 static bool readTopDouble(mat_t* matfp, const char* name, double& out) noexcept;
221 static bool readTopString(mat_t* matfp, const char* name, std::string& out);
222
223 static bool readStructInt(matvar_t* s, const char* field, int& out, size_t index = 0) noexcept;
224 static bool readStructDouble(matvar_t* s, const char* field, double& out, size_t index = 0) noexcept;
225 static bool readStructBool(matvar_t* s, const char* field, bool& out, size_t index = 0) noexcept;
226 static bool readStructString(matvar_t* s, const char* field, std::string& out, size_t index = 0);
227};
228
229} // namespace cppxdic
230
231#endif // MAT_READER_H
MatReader class Provides utilities for reading MAT files.
Definition mat_reader.h:52
static bool loadDLTCalibration(const std::string &mat_path, DLTCalibrationData &calib)
static matvar_t * readStructField(mat_t *matfp, const std::string &struct_name, const std::string &field_name)
Read a specific field from a struct variable.
std::unique_ptr< matvar_t, MatVarDeleter > MatVarPtr
Definition mat_reader.h:63
static cv::Mat readImage(matvar_t *var)
Read image from matvar.
static matvar_t * getCellElement(matvar_t *var, size_t index)
Get cell from cell array.
static bool variableExists(mat_t *matfp, const std::string &var_name)
Check if variable exists in MAT file.
static std::vector< double > readDoubleArray(matvar_t *var)
Read double array from matvar.
static MatVarPtr readVar(mat_t *matfp, const std::string &name)
static cv::Mat readROIMask(const std::string &mat_path, const std::string &var_name="refmask")
Read ROI mask from MAT file.
static std::vector< double > readDouble2DArray(matvar_t *var, size_t &rows, size_t &cols)
Read 2D double array from matvar.
static bool readDIC2DPairResults(const std::string &mat_path, struct DIC2DPairResults &result)
Read DIC2DPairResults structure from MAT file Loads myDIC2DpairResults_C_X_C_Y.mat files.
static bool readDIC3Dcombined(const std::string &mat_path, struct DIC3Dcombined &combined)
Read DIC3Dcombined structure from MAT file.
static std::string readString(matvar_t *var)
Read string from matvar.
static std::vector< int > readIntArray(matvar_t *var)
Read int array from matvar.
static matvar_t * readNestedField(mat_t *matfp, const std::string &path)
Read a nested struct field (e.g., "data_dic_save.dispinfo.radius")
static double readScalar(matvar_t *var)
Read scalar from matvar.
static std::vector< std::string > readCellStrings(matvar_t *cell_var)
static MatVarPtr readFirstVar(mat_t *matfp)
static std::map< std::string, double > readDispInfo(mat_t *matfp)
Read dispinfo parameters from MAT file.
static MatFilePtr openMat(const std::string &path)
std::unique_ptr< mat_t, MatFileDeleter > MatFilePtr
Definition mat_reader.h:62
static bool loadProtocol(const std::string &protocol_path, ProtocolFileData &protocol)
static size_t getCellArraySize(matvar_t *var)
Get cell array size.
DataSerializer - Abstract base class for format-agnostic DIC data I/O.
DIC 2D Pair Results Equivalent to MATLAB's DIC2DpairResults.
DIC 3D Combined Results Equivalent to MATLAB's DIC3Dcombined This represents stitched results from mu...
std::vector< double > imageCentroids
Definition mat_reader.h:25
std::vector< uint8_t > columns
Definition mat_reader.h:27
std::vector< double > C3Dtrue
Definition mat_reader.h:22
std::vector< double > DLTparams
Definition mat_reader.h:21
void operator()(mat_t *f) const noexcept
Definition mat_reader.h:56
void operator()(matvar_t *v) const noexcept
Definition mat_reader.h:59
std::vector< TrialEntry > trials
Definition mat_reader.h:43
std::vector< std::string > titles
Definition mat_reader.h:42