MoorDyn
IO.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022, Jose Luis Cercos-Pita <jlc@core-marine.com>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
35 #pragma once
36 
37 #include "Misc.hpp"
38 #include "Log.hpp"
39 #include <cstdint>
40 #include <string>
41 #include <vector>
42 #include <iostream>
43 #include <fstream>
44 #include <tuple>
45 
46 namespace moordyn {
47 
50 namespace io {
51 
59 class DECLDIR IO : public LogUser
60 {
61  public:
65  IO(moordyn::Log* log);
66 
69  virtual ~IO() = default;
70 
79  void Save(const std::string filepath);
80 
86  void Load(const std::string filepath);
87 
96  virtual std::vector<uint64_t> Serialize(void) = 0;
97 
105  virtual uint64_t* Deserialize(const uint64_t* data) = 0;
106 
107  protected:
111  ofstream MakeFile(const std::string filepath) const;
112 
118  std::tuple<uint64_t, uint64_t*> LoadFile(const std::string filepath) const;
119 
124  uint64_t Serialize(const uint64_t& i);
125 
130  uint64_t Serialize(const int64_t& i);
131 
136  uint64_t Serialize(const real& f);
137 
142  std::vector<uint64_t> Serialize(const vec& m);
143 
148  std::vector<uint64_t> Serialize(const vec6& m);
149 
154  std::vector<uint64_t> Serialize(const mat& m);
155 
160  std::vector<uint64_t> Serialize(const mat6& m);
161 
166  std::vector<uint64_t> Serialize(const quaternion& m);
167 
172  std::vector<uint64_t> Serialize(const XYZQuat& m);
173 
178  std::vector<uint64_t> Serialize(const std::vector<real>& l);
179 
184  std::vector<uint64_t> Serialize(const std::vector<vec>& l);
185 
190  std::vector<uint64_t> Serialize(const std::vector<vec6>& l);
191 
196  std::vector<uint64_t> Serialize(const std::vector<mat>& l);
197 
202  std::vector<uint64_t> Serialize(const std::vector<mat6>& l);
203 
208  inline std::vector<uint64_t> Serialize(
209  const Eigen::Matrix<real, Eigen::Dynamic, Eigen::Dynamic>& l)
210  {
211  std::vector<uint64_t> data;
212  const uint64_t n = l.rows();
213  const uint64_t m = l.cols();
214  data.reserve(2 + n * m);
215  data.push_back(Serialize(n));
216  data.push_back(Serialize(m));
217  for (uint64_t i = 0; i < n; i++) {
218  for (uint64_t j = 0; j < m; j++) {
219  data.push_back(Serialize(l(i, j)));
220  }
221  }
222  return data;
223  }
224 
230  template<typename T>
231  std::vector<uint64_t> Serialize(const std::vector<std::vector<T>>& l)
232  {
233  std::vector<uint64_t> data;
234  const uint64_t n = l.size();
235  data.push_back(Serialize(n));
236  for (auto v : l) {
237  std::vector<uint64_t> subdata = Serialize(v);
238  data.insert(data.end(), subdata.begin(), subdata.end());
239  }
240  return data;
241  }
242 
248  uint64_t* Deserialize(const uint64_t* in, uint64_t& out);
249 
255  uint64_t* Deserialize(const uint64_t* in, int64_t& out);
256 
262  uint64_t* Deserialize(const uint64_t* in, real& out);
263 
269  uint64_t* Deserialize(const uint64_t* in, vec& out);
270 
276  uint64_t* Deserialize(const uint64_t* in, vec6& out);
277 
283  uint64_t* Deserialize(const uint64_t* in, mat& out);
284 
290  uint64_t* Deserialize(const uint64_t* in, mat6& out);
291 
297  uint64_t* Deserialize(const uint64_t* in, quaternion& out);
298 
304  uint64_t* Deserialize(const uint64_t* in, XYZQuat& out);
305 
311  uint64_t* Deserialize(const uint64_t* in, std::vector<real>& out);
312 
318  uint64_t* Deserialize(const uint64_t* in, std::vector<vec>& out);
319 
325  uint64_t* Deserialize(const uint64_t* in, std::vector<vec6>& out);
326 
332  uint64_t* Deserialize(const uint64_t* in, std::vector<mat>& out);
333 
339  uint64_t* Deserialize(const uint64_t* in, std::vector<mat6>& out);
340 
346  uint64_t* Deserialize(
347  const uint64_t* in,
348  Eigen::Matrix<real, Eigen::Dynamic, Eigen::Dynamic>& out)
349  {
350  uint64_t* remaining;
351  uint64_t n, m;
352  remaining = Deserialize(in, n);
353  remaining = Deserialize(remaining, m);
354  if ((out.rows() != n) || (out.cols() != m))
355  out.resize(n, m);
356  for (unsigned int i = 0; i < n; i++) {
357  for (unsigned int j = 0; j < m; j++) {
358  remaining = Deserialize(remaining, out(i, j));
359  }
360  }
361  return remaining;
362  }
363 
371  template<typename T>
372  uint64_t* Deserialize(const uint64_t* in, std::vector<std::vector<T>>& out)
373  {
374  uint64_t n;
375  uint64_t* remaining = Deserialize(in, n);
376  out.clear();
377  out.reserve(n);
378  for (unsigned int i = 0; i < n; i++) {
379  std::vector<T> v;
380  remaining = Deserialize(remaining, v);
381  out.push_back(v);
382  }
383  return remaining;
384  }
385 
386  private:
388  bool _is_big_endian;
390  uint8_t _min_major_version;
392  uint8_t _min_minor_version;
393 };
394 
395 } // ::io
396 
397 } // ::moordyn
#define DECLDIR
Prefix to export C functions on the compiled library.
Definition: MoorDynAPI.h:68
A Logging utility.
Definition: Log.hpp:149
A helper for the entities to use the logger.
Definition: Log.hpp:223
A base class for all the entities that must save/load data to/from disk.
Definition: IO.hpp:60
uint64_t * Deserialize(const uint64_t *in, std::vector< std::vector< T >> &out)
Unpack a loaded list of lists.
Definition: IO.hpp:372
uint64_t * Deserialize(const uint64_t *in, Eigen::Matrix< real, Eigen::Dynamic, Eigen::Dynamic > &out)
Unpack an arbitrarily large matrix.
Definition: IO.hpp:346
virtual ~IO()=default
Destructor.
std::vector< uint64_t > Serialize(const std::vector< std::vector< T >> &l)
Pack a list of lists to make it writable This function might act recursively.
Definition: IO.hpp:231
std::vector< uint64_t > Serialize(const Eigen::Matrix< real, Eigen::Dynamic, Eigen::Dynamic > &l)
Pack an arbitrarily large matrix.
Definition: IO.hpp:208
virtual std::vector< uint64_t > Serialize(void)=0
Produce the packed data to be saved.
virtual uint64_t * Deserialize(const uint64_t *data)=0
Unpack the data to restore the Serialized information.
MoorDyn2 C++ API namespace.
Definition: Body.cpp:27
vec3 vec
vec3 renaming
Definition: Misc.hpp:130
Eigen::Quaterniond quaternion
Quaternion of real numbers.
Definition: Misc.hpp:144
Eigen::Vector6d vec6
6-D vector of real numbers
Definition: Misc.hpp:126
Eigen::Matrix6d mat6
6x6 matrix of real numbers
Definition: Misc.hpp:138
double real
Real numbers wrapper. It is either double or float.
Definition: Misc.hpp:118
mat3 mat
mat3 renaming
Definition: Misc.hpp:142
Joint of a point and a quaternion.
Definition: Misc.hpp:304