Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef ROSMATLAB_CONVERSION_H
00030 #define ROSMATLAB_CONVERSION_H
00031
00032 #include <introspection/forwards.h>
00033 #include <matrix.h>
00034
00035 #include <rosmatlab/options.h>
00036
00037 #include <ros/time.h>
00038
00039 namespace rosmatlab {
00040
00041 using namespace cpp_introspection;
00042
00043 class Conversion;
00044 typedef boost::shared_ptr<Conversion> ConversionPtr;
00045
00046 typedef mxArray *Array;
00047 typedef mxArray const *ConstArray;
00048
00049 class ConversionOptions : public Options {
00050 public:
00051 ConversionOptions();
00052 ConversionOptions(int nrhs, const mxArray *prhs[]);
00053 virtual ~ConversionOptions();
00054
00055 virtual void init(int nrhs, const mxArray *prhs[]);
00056 virtual mxArray *toMatlab() const;
00057
00058 typedef enum { MATLAB_STRUCT, MATLAB_MATRIX, MATLAB_EXTENDED_STRUCT, MATLAB_TYPE_MAX } MatlabType;
00059 MatlabType conversionType() const;
00060 std::string conversionTypeString() const;
00061 ConversionOptions &setConversionType(MatlabType type);
00062
00063 bool addMetaData() const;
00064 ConversionOptions &setAddMetaData(bool value);
00065
00066 bool addConnectionHeader() const;
00067 ConversionOptions &setAddConnectionHeader(bool value);
00068 };
00069
00070 class Conversion {
00071 public:
00072 Conversion(const MessagePtr &message);
00073 Conversion(const MessagePtr &message, const ConversionOptions& options);
00074 Conversion(const Conversion &other, const MessagePtr &message = MessagePtr());
00075 virtual ~Conversion();
00076
00077 operator void *() const { return reinterpret_cast<void *>(static_cast<bool>(message_)); }
00078
00079 virtual Array toMatlab();
00080 virtual Array toMatlab(Array target, std::size_t index = 0, std::size_t size = 0);
00081
00082 virtual Array toDoubleMatrix();
00083 virtual Array toDoubleMatrix(Array target, std::size_t index = 0, std::size_t size = 0);
00084
00085 virtual Array toStruct();
00086 virtual Array toStruct(Array target, std::size_t index = 0, std::size_t size = 0);
00087
00088 virtual Array toExtendedStruct();
00089 virtual Array toExtendedStruct(Array target, std::size_t index = 0, std::size_t size = 0);
00090
00091 virtual std::size_t numberOfInstances(ConstArray source);
00092 virtual MessagePtr fromMatlab(ConstArray source, std::size_t index = 0);
00093 virtual void fromMatlab(const MessagePtr &message, ConstArray source, std::size_t index = 0);
00094
00095 virtual Array convertToMatlab(const FieldPtr& field);
00096 virtual void convertFromMatlab(const FieldPtr& field, ConstArray source);
00097 virtual const double *convertFromDouble(const FieldPtr& field, const double *begin, const double *end);
00098
00099 const MessagePtr& expanded();
00100
00101 Options &options() { return options_; }
00102 const Options &options() const { return options_; }
00103 Conversion &setOptions(int nrhs, const mxArray *prhs[]);
00104
00105 static ConversionOptions &defaultOptions();
00106 static ConversionOptions &perMessageOptions(const MessagePtr& message);
00107
00108 protected:
00109 virtual void fromDoubleMatrix(const MessagePtr &target, ConstArray source, std::size_t n = 0);
00110 virtual void fromDoubleMatrix(const MessagePtr &target, const double *begin, const double *end);
00111 virtual void fromStruct(const MessagePtr &target, ConstArray source, std::size_t index = 0);
00112
00113 MessagePtr message_;
00114 MessagePtr expanded_;
00115
00116 ConversionOptions options_;
00117 static std::map<const char *,ConversionOptions> per_message_options_;
00118 };
00119
00120
00121
00122
00123
00124 template <typename Time>
00125 static inline mxArray *mxCreateTime(const Time& time) {
00126 if (time == ros::TIME_MIN || time == ros::TIME_MAX) return mxCreateDoubleMatrix(0, 0, mxREAL);
00127 return ::mxCreateDoubleScalar(time.toSec());
00128 }
00129
00130 template <typename Duration>
00131 static inline mxArray *mxCreateDuration(const Duration& duration) {
00132 return ::mxCreateDoubleScalar(duration.toSec());
00133 }
00134
00135 static inline mxArray *mxCreateString(const std::string& string) {
00136 return ::mxCreateString(string.c_str());
00137 }
00138
00139 static inline mxArray *mxCreateEmpty() {
00140 return ::mxCreateDoubleMatrix(0, 0, mxREAL);
00141 }
00142
00143 }
00144
00145 #endif // ROSMATLAB_CONVERSION_H