1 #ifndef VTESTBED_CORE_SHIP_HH 2 #define VTESTBED_CORE_SHIP_HH 6 #include <vtestbed/base/blitz.hh> 7 #include <vtestbed/base/bstream.hh> 8 #include <vtestbed/base/constants.hh> 9 #include <vtestbed/core/compartment.hh> 10 #include <vtestbed/core/grid.hh> 11 #include <vtestbed/core/hull.hh> 12 #include <vtestbed/core/macros.hh> 13 #include <vtestbed/core/mass.hh> 14 #include <vtestbed/core/math.hh> 15 #include <vtestbed/core/propulsor.hh> 16 #include <vtestbed/core/types.hh> 17 #include <vtestbed/geometry/basis.hh> 18 #include <vtestbed/geometry/math.hh> 19 #include <vtestbed/geometry/quaternion.hh> 20 #include <vtestbed/geometry/types.hh> 28 angular_velocity_matrix_zyx(
const Vector<T,3>& sin,
const Vector<T,3>& cos) {
29 static constexpr
const int theta=1, psi=2;
30 return {{
cos(theta)*
cos(psi), -
sin(psi), 0,
43 angular_velocity_matrix_zyx(
const Vector<T,3>& euler_angles) {
44 return angular_velocity_matrix_zyx<T>(sin(euler_angles), cos(euler_angles));
49 angular_velocity_matrix_xyz(
const Vector<T,3>& sin,
const Vector<T,3>& cos) {
50 static constexpr
const int phi=0, theta=1;
51 return {{1, 0, sin(theta),
52 0, cos(phi), -sin(phi)*cos(theta),
53 0, sin(phi), cos(phi)*cos(theta)}};
58 angular_velocity_matrix_xyz(
const Vector<T,3>& euler_angles) {
59 return angular_velocity_matrix_xyz<T>(
sin(euler_angles),
cos(euler_angles));
63 inline blitz::TinyMatrix<T,3,4>
65 blitz::TinyMatrix<T,3,4> m;
66 m(0,0) = q(1); m(0,1) = -q(0); m(0,2) = -q(3); m(0,3) = q(2);
67 m(1,0) = q(2); m(1,1) = q(3); m(1,2) = -q(0); m(1,3) = -q(1);
68 m(2,0) = q(3); m(2,1) = -q(2); m(2,2) = q(1); m(2,3) = -q(0);
75 euler_angles_matrix_zyx(
76 const Vector<T,3>& sin,
77 const Vector<T,3>& cos,
80 static constexpr
const int theta=1, psi=2;
83 cos(psi)*tan_theta,
sin(psi)*tan_theta, 1}};
94 euler_angles_matrix_zyx(
const Vector<T,3>& euler_angles) {
95 return euler_angles_matrix_zyx<T>(
104 euler_angles_matrix_xyz(
105 const Vector<T,3>& sin,
106 const Vector<T,3>& cos,
109 static constexpr
const int phi=0, theta=1;
110 return {{1, sin(phi)*tan_theta, -cos(phi)*tan_theta,
111 0, cos(phi), sin(phi),
112 0, -sin(phi)/cos(theta), cos(phi)/cos(theta)}};
117 euler_angles_matrix_xyz(
const Vector<T,3>& euler_angles) {
118 return euler_angles_matrix_zyx<T>(
127 euler_angles_residual_vector_zyx(
128 const Vector<T,3>& sin,
129 const Vector<T,3>& cos,
131 const Vector<T,3>& angvelocity
133 static constexpr
const int theta=1, psi=2;
135 auto d_01 = angvelocity(0)*angvelocity(1);
136 auto d_12 = angvelocity(1)*angvelocity(2);
137 auto d_02 = angvelocity(0)*angvelocity(2);
147 euler_angles_residual_vector_xyz(
148 const Vector<T,3>& sin,
149 const Vector<T,3>& cos,
151 const Vector<T,3>& angvelocity
153 static constexpr
const int phi=0, theta=1;
155 auto d_01 = angvelocity(0)*angvelocity(1);
156 auto d_12 = angvelocity(1)*angvelocity(2);
157 auto d_02 = angvelocity(0)*angvelocity(2);
166 inline blitz::TinyMatrix<T,4,3>
168 blitz::TinyMatrix<T,4,3> m;
169 m(0,0) = q(1); m(0,1) = q(2); m(0,2) = q(3);
170 m(1,0) = -q(0); m(1,1) = q(3); m(1,2) = -q(2);
171 m(2,0) = -q(3); m(2,1) = -q(0); m(2,2) = q(1);
172 m(3,0) = q(2); m(3,1) = -q(1); m(3,2) = -q(0);
189 using scalar_type = T;
190 using vec3 = blitz::TinyVector<T,3>;
197 using matrix_type = blitz::TinyMatrix<T,3,3>;
207 using variables_type = blitz::TinyVector<T,13>;
208 variables_type variables;
219 inline const variables_type& vars()
const {
return this->variables; }
220 inline operator const variables_type&()
const {
return this->variables; }
222 operator=(
const variables_type& rhs) { this->variables = rhs;
return *
this; }
224 using variables_type =
typename state_vector_type::variables_type;
226 static_assert(
sizeof(variables_type) == 3*
sizeof(vec3) +
sizeof(
quaternion_type),
234 vec3 _position[3] {T{},T{},T{}};
236 vec3 _angdisplacement[3] {T{},T{},T{}};
238 vec3 _angmomentum{T{}};
240 quaternion_type _quaternion{1,0,0,0};
242 rotation_matrix_type _rotation_matrix;
243 rotation_matrix_type _inertia_matrix;
246 propulsor_type _propulsor;
253 inline const vec3&
position(
int i=0)
const {
return this->_position[i]; }
257 inline void position(
int i,
const vec3& rhs) { this->_position[i] = rhs; }
263 inline void velocity(
const vec3& rhs) { this->_position[1] = rhs; }
273 return this->_angdisplacement[i];
278 inline vec3
euler_angles()
const {
return to_euler_angles(this->_quaternion); }
303 inline T
surge(
int i=0)
const {
return this->_position[i][0]; }
307 inline T
sway(
int i=0)
const {
return this->_position[i][1]; }
311 inline T
heave(
int i=0)
const {
return this->_position[i][2]; }
332 void state_vector(
const state_vector_type& v,
const state_vector_type& dv, T dt);
335 state_vector(
const variables_type& v,
const variables_type& dv, T dt) {
337 reinterpret_cast<const state_vector_type&>(v),
338 reinterpret_cast<const state_vector_type&>(dv),
351 inline const hull_type& hull()
const {
return this->_hull; }
355 inline void compartments(
const room_array& rhs) { this->_rooms = rhs; }
356 inline room_array& compartments() {
return this->_rooms; }
357 inline const room_array& compartments()
const {
return this->_rooms; }
358 inline const propulsor_type& propulsor()
const {
return this->_propulsor; }
359 inline void propulsor(
const propulsor_type& rhs) { this->_propulsor = rhs; }
361 inline scalar_type mass()
const {
return this->_mass; }
367 mass(scalar_type rhs) {
373 displacement(scalar_type rhs) {
378 displacement()
const {
383 draught(scalar_type rhs) {
385 auto level = hull().bounds(2).min() + rhs;
386 this->displacement(hull().signed_volume_below(2, level));
389 inline coordinate_system_type
390 earth_fixed_coordinate_system()
const {
391 return make_coordinate_system(rotation_matrix(),
position(0));
394 inline quaternion_coordinate_system_type
395 body_fixed_coordinate_system()
const {
399 inline const rotation_matrix_type&
400 rotation_matrix()
const {
401 return this->_rotation_matrix;
417 inline const rotation_matrix_type&
419 return this->_inertia_matrix;
440 this->_angdisplacement[i] = rhs;
451 operator<<(bstream& out,
const Ship<T>& rhs);
455 operator>>(bstream& in, Ship<T>& rhs);
461 #endif // vim:filetype=cpp
const vec3 & acceleration() const
Acceleration in Earth-fixed coordinate system.
T flooded_mass() const
Total mass of all fluid volumes inside compartments.
void angular_momentum(const vec3 &rhs)
Set angular momentum.
const vec3 & position(int i=0) const
Get position or its time derivatives in Earth-fixed coordinate system.
Rigid ship with a mass and translational and angular velocity.
T roll() const
Counterclockwise rotation about -axis in radians.
static constexpr const T water_density()
Sea water density.
vec3 euler_angles() const
Get Euler angles or their derivatives.
void position(int i, const vec3 &rhs)
Set position or its time derivatives in Earth-fixed coordinate system.
T pitch() const
Counterclockwise rotation about -axis in radians.
T sway(int i=0) const
coordinate of the position.
void velocity(const vec3 &rhs)
Set velocity in Earth-fixed coordinate system.
const vec3 & angular_velocity() const
Angular velocity in Earth-fixed coordinate system.
const vec3 & angular_displacement(int i=0) const
Get angular position or its time derivatives in Earth-fixed coordinate system.
T surge(int i=0) const
coordinate of the position.
const vec3 & angular_momentum() const
Angular momentum.
T yaw() const
Counterclockwise rotation about -axis in radians.
const vec3 & velocity() const
Get velocity in Earth-fixed coordinate system.
const quaternion_type & quaternion() const
Get rotation quaternion.
const rotation_matrix_type & inertia_matrix() const
Inertia matrix in body-fixed coordinate system.
Mass attached to specified point in space.
const vec3 & angular_acceleration() const
Angular acceleration in Earth-fixed coordinate system.
state_vector_type state_vector() const
Get state vector for ship motion equation.
T heave(int i=0) const
coordinate of the position.