Virtual Testbed
Ship dynamics simulator for extreme conditions
db/hull.hh
1 #ifndef VTESTBED_DB_HULL_HH
2 #define VTESTBED_DB_HULL_HH
3 
4 #include <chrono>
5 #include <vector>
6 
7 #include <sqlitex/forward.hh>
8 
9 #include <vtestbed/core/mass.hh>
10 #include <vtestbed/core/types.hh>
11 #include <vtestbed/db/entity.hh>
12 #include <vtestbed/db/ship_hull_transformation.hh>
13 #include <vtestbed/geometry/polyhedron.hh>
14 
15 namespace vtb {
16 
17  namespace db {
18 
19  template <class T>
20  class Hull: public Entity {
21 
22  public:
23  using vec3 = vtb::core::Vector3<T>;
26  using time_point = clock::time_point;
27  using duration = clock::duration;
28  using color_type = vec3;
32 
33  private:
34  std::string _author;
35  std::string _organisation;
36  polyhedron_array _polyhedrons{{}};
37  vec3 _scale{T{1}};
38  T _length{0};
39  T _beam{0};
40  T _depth{0};
41  T _draught{0};
42  transformation_array _transforms;
43  time_point _created{duration::zero()};
44  time_point _modified{duration::zero()};
45  color_type _color{T{0.184}, T{0.513}, T{0.8}};
46 
47  public:
48 
49  using Entity::filename;
50 
51  Hull() = default;
52  Hull(Hull&&) = default;
53  Hull& operator=(Hull&&) = default;
54  Hull(const Hull&) = default;
55  Hull& operator=(const Hull&) = default;
56 
57  inline const std::string& author() const { return this->_author; }
58  inline const std::string& organisation() const { return this->_organisation; }
59  inline void author(const std::string& rhs) { this->_author = rhs; }
60  inline void organisation(const std::string& rhs) { this->_organisation = rhs; }
61  void filename(const std::string& filename);
62 
63  inline const polyhedron_type&
64  polyhedron() const {
65  return this->current_polyhedron();
66  }
67 
68  inline void
69  polyhedron(const polyhedron_type& rhs) {
70  this->current_polyhedron() = rhs;
71  }
72 
73  inline void
74  polyhedron(polyhedron_type&& rhs) {
75  this->current_polyhedron() = std::move(rhs);
76  }
77 
78  inline const vec3& scale() const { return this->_scale; }
79  inline void scale(const vec3& rhs) { this->_scale = rhs; }
80 
82  inline T length() const { return this->_length; }
83 
85  inline T beam() const { return this->_beam; }
86 
88  inline T depth() const { return this->_depth; }
89 
91  inline T draught() const { return this->_draught; }
92  inline void draught(T rhs) { this->_draught = rhs; }
93 
94  inline void
95  dimensions(T x, T y, T z) {
96  this->_length = x;
97  this->_beam = y;
98  this->_depth = z;
99  }
100 
101  inline void
102  dimensions(const vec3& v) {
103  this->_length = v(0);
104  this->_beam = v(1);
105  this->_depth = v(2);
106  }
107 
108  inline const transformation_array&
109  transformations() const {
110  return this->_transforms;
111  }
112 
113  inline const time_point& created() const { return this->_created; }
114  inline const time_point& modified() const { return this->_modified; }
115  inline void created(const time_point& rhs) { this->_created = rhs; }
116  inline void modified(const time_point& rhs) { this->_modified = rhs; }
117 
118  inline const color_type&
119  color() const noexcept {
120  return this->_color;
121  }
122 
123  inline void
124  color(const color_type& rhs) {
125  this->_color = rhs;
126  }
127 
128  inline void
129  color(T r, T g, T b) {
130  this->_color(0) = r;
131  this->_color(1) = g;
132  this->_color(2) = b;
133  }
134 
136  file_name_for_export() const;
137 
138  bool has_valid_id(sqlite::connection* db) const;
139  bool original(sqlite::connection* db) const;
140  void insert(sqlite::connection* db);
141  void copy(sqlite::connection* db);
142  void select(sqlite::connection* db);
143  void update(sqlite::connection* db);
144  void save(sqlite::connection* db);
145  void import(std::string filename);
146  void import_original();
147  void select_blobs(sqlite::connection* db);
148  void update_blobs(sqlite::connection* db);
149  void save_blobs(sqlite::connection* db);
150  void remove(sqlite::connection* db);
151 
152  void
153  clear();
154 
155  void
156  swap(Hull& rhs);
157 
158  polyhedron_type make_ship_hull(bool transform);
159 
160  void transform(const transformation_type& rhs);
161  void clear_transforms();
162  inline void undo() { this->_transforms.undo(); }
163  inline void redo() { this->_transforms.redo(); }
164 
165  template <class X>
166  friend sqlite::statement&
167  operator>>(sqlite::statement& in, Hull<X>& rhs);
168 
169  static std::vector<Hull<T>>
170  all(sqlite::connection* db);
171 
172  private:
173 
174  void
175  transform();
176 
177  void
178  update_dimensions();
179 
180  inline const polyhedron_type&
181  current_polyhedron() const {
182  return this->_polyhedrons[this->_transforms.index()];
183  }
184 
185  inline polyhedron_type&
186  current_polyhedron() {
187  auto& gs = this->_polyhedrons;
188  if (gs.empty()) { gs.emplace_back(); }
189  return gs[this->_transforms.index()];
190  }
191 
192  inline const polyhedron_type&
193  original_polyhedron() const {
194  return this->_polyhedrons.front();
195  }
196 
197  };
198 
199  template <class T>
200  sqlite::statement&
201  operator>>(sqlite::statement& in, Hull<T>& rhs);
202 
203  template <class T>
204  inline void
205  swap(Hull<T>& lhs, Hull<T>& rhs) {
206  lhs.swap(rhs);
207  }
208 
209  }
210 
211 }
212 
213 #endif // vim:filetype=cpp
T swap(T... args)
T draught() const
Get vertical the distance between the waterline and the bottom of the hull.
Definition: db/hull.hh:91
T length() const
Get overall hull length in metres.
Definition: db/hull.hh:82
Main namespace.
Definition: convert.hh:9
T depth() const
Get overall hull height in metres.
Definition: db/hull.hh:88
T beam() const
Get overall hull width in metres.
Definition: db/hull.hh:85