navaltoolbox 0.3.0

High-performance naval architecture library for hydrostatics, stability, and tank calculations
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
// Copyright (C) 2026 Antoine ANCEAU
//
// This file is part of navaltoolbox.
//
// navaltoolbox is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! Vessel module.
//!
//! Provides the Vessel container for hull geometries, tanks, and vessel-level properties.

use crate::downflooding::DownfloodingOpening;
use crate::hull::Hull;
use crate::silhouette::Silhouette;
use crate::tanks::Tank;

/// Represents a vessel containing hull geometries, tanks, and vessel-level properties.
///
/// The Vessel class serves as a container for hull geometries and manages
/// vessel-level reference positions such as the forward and aft perpendiculars
/// (FP and AP). It supports both single-hull vessels (monohulls) and multi-hull
/// vessels (catamarans, trimarans).
#[derive(Clone)]
pub struct Vessel {
    /// List of hull geometries
    hulls: Vec<Hull>,
    /// List of tanks
    tanks: Vec<Tank>,
    /// Aft Perpendicular position (None = auto from bounds)
    ap: Option<f64>,
    /// Forward Perpendicular position (None = auto from bounds)
    fp: Option<f64>,
    /// Wind silhouette profiles (hull, superstructure, containers, etc.)
    silhouettes: Vec<Silhouette>,
    /// Downflooding openings for θf calculation
    downflooding_openings: Vec<DownfloodingOpening>,
}

impl Vessel {
    /// Creates a new Vessel with a single hull.
    pub fn new(hull: Hull) -> Self {
        Self {
            hulls: vec![hull],
            tanks: Vec::new(),
            ap: None,
            fp: None,
            silhouettes: Vec::new(),
            downflooding_openings: Vec::new(),
        }
    }

    /// Creates a new Vessel with multiple hulls (catamaran, trimaran).
    pub fn new_multi(hulls: Vec<Hull>) -> Result<Self, &'static str> {
        if hulls.is_empty() {
            return Err("At least one hull must be provided");
        }
        Ok(Self {
            hulls,
            tanks: Vec::new(),
            ap: None,
            fp: None,
            silhouettes: Vec::new(),
            downflooding_openings: Vec::new(),
        })
    }

    /// Creates a new Vessel with perpendicular positions.
    pub fn with_perpendiculars(hull: Hull, ap: f64, fp: f64) -> Self {
        Self {
            hulls: vec![hull],
            tanks: Vec::new(),
            ap: Some(ap),
            fp: Some(fp),
            silhouettes: Vec::new(),
            downflooding_openings: Vec::new(),
        }
    }

    /// Returns the list of hull geometries.
    pub fn hulls(&self) -> &[Hull] {
        &self.hulls
    }

    /// Returns a mutable reference to the hulls.
    pub fn hulls_mut(&mut self) -> &mut Vec<Hull> {
        &mut self.hulls
    }

    /// Returns true if this is a multi-hull vessel.
    pub fn is_multihull(&self) -> bool {
        self.hulls.len() > 1
    }

    /// Returns the list of tanks.
    pub fn tanks(&self) -> &[Tank] {
        &self.tanks
    }

    /// Returns a mutable reference to the tanks.
    pub fn tanks_mut(&mut self) -> &mut Vec<Tank> {
        &mut self.tanks
    }

    /// Returns the Aft Perpendicular position.
    ///
    /// If not explicitly set, returns the minimum X of the combined bounds.
    pub fn ap(&self) -> f64 {
        self.ap.unwrap_or_else(|| self.get_bounds().0)
    }

    /// Returns the Forward Perpendicular position.
    ///
    /// If not explicitly set, returns the maximum X of the combined bounds.
    pub fn fp(&self) -> f64 {
        self.fp.unwrap_or_else(|| self.get_bounds().1)
    }

    /// Sets the Aft Perpendicular position.
    pub fn set_ap(&mut self, ap: f64) {
        self.ap = Some(ap);
    }

    /// Sets the Forward Perpendicular position.
    pub fn set_fp(&mut self, fp: f64) {
        self.fp = Some(fp);
    }

    /// Returns the Length Between Perpendiculars (LBP).
    pub fn lbp(&self) -> f64 {
        self.fp() - self.ap()
    }

    /// Returns the bounding box of all hull geometries combined.
    ///
    /// Returns (xmin, xmax, ymin, ymax, zmin, zmax).
    pub fn get_bounds(&self) -> (f64, f64, f64, f64, f64, f64) {
        if self.hulls.len() == 1 {
            return self.hulls[0].get_bounds();
        }

        let all_bounds: Vec<_> = self.hulls.iter().map(|h| h.get_bounds()).collect();

        let xmin = all_bounds.iter().map(|b| b.0).fold(f64::INFINITY, f64::min);
        let xmax = all_bounds
            .iter()
            .map(|b| b.1)
            .fold(f64::NEG_INFINITY, f64::max);
        let ymin = all_bounds.iter().map(|b| b.2).fold(f64::INFINITY, f64::min);
        let ymax = all_bounds
            .iter()
            .map(|b| b.3)
            .fold(f64::NEG_INFINITY, f64::max);
        let zmin = all_bounds.iter().map(|b| b.4).fold(f64::INFINITY, f64::min);
        let zmax = all_bounds
            .iter()
            .map(|b| b.5)
            .fold(f64::NEG_INFINITY, f64::max);

        (xmin, xmax, ymin, ymax, zmin, zmax)
    }

    // =========================================================================
    // Tank Management
    // =========================================================================

    /// Adds a tank to the vessel.
    pub fn add_tank(&mut self, tank: Tank) {
        self.tanks.push(tank);
    }

    /// Removes a tank from the vessel by index.
    pub fn remove_tank(&mut self, index: usize) -> Option<Tank> {
        if index < self.tanks.len() {
            Some(self.tanks.remove(index))
        } else {
            None
        }
    }

    /// Finds a tank by its name.
    pub fn get_tank_by_name(&self, name: &str) -> Option<&Tank> {
        self.tanks.iter().find(|t| t.name() == name)
    }

    /// Finds a tank by its name (mutable).
    pub fn get_tank_by_name_mut(&mut self, name: &str) -> Option<&mut Tank> {
        self.tanks.iter_mut().find(|t| t.name() == name)
    }

    /// Calculates the total mass of all fluid in tanks.
    pub fn get_total_tanks_mass(&self) -> f64 {
        self.tanks.iter().map(|t| t.fluid_mass()).sum()
    }

    /// Calculates the combined center of gravity of all tank fluids.
    ///
    /// Returns the mass-weighted average of individual tank CoGs.
    pub fn get_tanks_center_of_gravity(&self) -> [f64; 3] {
        let total_mass = self.get_total_tanks_mass();
        if total_mass <= 0.0 {
            return [0.0, 0.0, 0.0];
        }

        let mut moment = [0.0, 0.0, 0.0];
        for tank in &self.tanks {
            if tank.fluid_mass() > 0.0 {
                let cog = tank.center_of_gravity();
                moment[0] += tank.fluid_mass() * cog[0];
                moment[1] += tank.fluid_mass() * cog[1];
                moment[2] += tank.fluid_mass() * cog[2];
            }
        }

        [
            moment[0] / total_mass,
            moment[1] / total_mass,
            moment[2] / total_mass,
        ]
    }

    /// Calculates the total free surface moment from all tanks.
    ///
    /// Returns (transverse_moment, longitudinal_moment) in m⁴.
    pub fn get_total_free_surface_moment(&self) -> (f64, f64) {
        let fsm_t: f64 = self.tanks.iter().map(|t| t.free_surface_moment_t()).sum();
        let fsm_l: f64 = self.tanks.iter().map(|t| t.free_surface_moment_l()).sum();
        (fsm_t, fsm_l)
    }

    /// Calculates the total free surface correction from all tanks.
    ///
    /// Returns (transverse_correction, longitudinal_correction) in m⁴.
    pub fn get_total_free_surface_correction(&self) -> (f64, f64) {
        let fsc_t: f64 = self
            .tanks
            .iter()
            .map(|t| t.free_surface_correction_t())
            .sum();
        let fsc_l: f64 = self
            .tanks
            .iter()
            .map(|t| t.free_surface_correction_l())
            .sum();
        (fsc_t, fsc_l)
    }

    // =========================================================================
    // Silhouette Management
    // =========================================================================

    /// Adds a wind silhouette profile to the vessel.
    pub fn add_silhouette(&mut self, silhouette: Silhouette) {
        self.silhouettes.push(silhouette);
    }

    /// Returns a reference to all wind silhouettes.
    pub fn silhouettes(&self) -> &[Silhouette] {
        &self.silhouettes
    }

    /// Returns a mutable reference to the silhouettes.
    pub fn silhouettes_mut(&mut self) -> &mut Vec<Silhouette> {
        &mut self.silhouettes
    }

    /// Returns the number of silhouettes.
    pub fn num_silhouettes(&self) -> usize {
        self.silhouettes.len()
    }

    /// Returns true if there are any silhouettes.
    pub fn has_silhouettes(&self) -> bool {
        !self.silhouettes.is_empty()
    }

    /// Finds a silhouette by its name.
    pub fn get_silhouette_by_name(&self, name: &str) -> Option<&Silhouette> {
        self.silhouettes.iter().find(|s| s.name() == name)
    }

    /// Removes a silhouette by index.
    pub fn remove_silhouette(&mut self, index: usize) -> Option<Silhouette> {
        if index < self.silhouettes.len() {
            Some(self.silhouettes.remove(index))
        } else {
            None
        }
    }

    /// Removes all silhouettes.
    pub fn clear_silhouettes(&mut self) {
        self.silhouettes.clear();
    }

    /// Calculates the total emerged area from all silhouettes.
    pub fn get_total_emerged_area(&self, waterline_z: f64) -> f64 {
        self.silhouettes
            .iter()
            .map(|s| s.get_emerged_area(waterline_z))
            .sum()
    }

    /// Calculates the combined centroid of all emerged areas.
    pub fn get_combined_emerged_centroid(&self, waterline_z: f64) -> [f64; 2] {
        let total_area = self.get_total_emerged_area(waterline_z);
        if total_area < 1e-9 {
            return [0.0, 0.0];
        }

        let mut cx = 0.0;
        let mut cz = 0.0;
        for s in &self.silhouettes {
            let area = s.get_emerged_area(waterline_z);
            if area > 1e-9 {
                let centroid = s.get_emerged_centroid(waterline_z);
                cx += centroid[0] * area;
                cz += centroid[1] * area;
            }
        }

        [cx / total_area, cz / total_area]
    }

    // =========================================================================
    // Downflooding Openings Management
    // =========================================================================

    /// Adds a downflooding opening to the vessel.
    pub fn add_downflooding_opening(&mut self, opening: DownfloodingOpening) {
        self.downflooding_openings.push(opening);
    }

    /// Returns a reference to all downflooding openings.
    pub fn downflooding_openings(&self) -> &[DownfloodingOpening] {
        &self.downflooding_openings
    }

    /// Returns a mutable reference to downflooding openings.
    pub fn downflooding_openings_mut(&mut self) -> &mut Vec<DownfloodingOpening> {
        &mut self.downflooding_openings
    }

    /// Returns the number of downflooding openings.
    pub fn num_downflooding_openings(&self) -> usize {
        self.downflooding_openings.len()
    }

    /// Returns true if any downflooding openings are defined.
    pub fn has_downflooding_openings(&self) -> bool {
        !self.downflooding_openings.is_empty()
    }

    /// Removes all downflooding openings from the vessel.
    pub fn clear_downflooding_openings(&mut self) {
        self.downflooding_openings.clear();
    }
}

impl std::fmt::Debug for Vessel {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let bounds = self.get_bounds();
        f.debug_struct("Vessel")
            .field("hulls", &self.hulls.len())
            .field("tanks", &self.tanks.len())
            .field("ap", &self.ap())
            .field("fp", &self.fp())
            .field("lbp", &self.lbp())
            .field("bounds", &bounds)
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use nalgebra::Point3;
    use parry3d_f64::shape::TriMesh;

    fn create_test_hull() -> Hull {
        let vertices = vec![
            Point3::new(0.0, -5.0, 0.0),
            Point3::new(100.0, -5.0, 0.0),
            Point3::new(100.0, 5.0, 0.0),
            Point3::new(0.0, 5.0, 0.0),
            Point3::new(0.0, -5.0, 10.0),
            Point3::new(100.0, -5.0, 10.0),
            Point3::new(100.0, 5.0, 10.0),
            Point3::new(0.0, 5.0, 10.0),
        ];
        let indices = vec![
            [0, 2, 1],
            [0, 3, 2],
            [4, 5, 6],
            [4, 6, 7],
            [0, 1, 5],
            [0, 5, 4],
            [2, 3, 7],
            [2, 7, 6],
            [0, 4, 7],
            [0, 7, 3],
            [1, 2, 6],
            [1, 6, 5],
        ];
        let mesh = TriMesh::new(vertices, indices).unwrap();
        Hull::from_mesh(mesh)
    }

    #[test]
    fn test_vessel_bounds() {
        let hull = create_test_hull();
        let vessel = Vessel::new(hull);

        let bounds = vessel.get_bounds();
        assert!((bounds.0 - 0.0).abs() < 1e-6);
        assert!((bounds.1 - 100.0).abs() < 1e-6);
    }

    #[test]
    fn test_vessel_perpendiculars() {
        let hull = create_test_hull();
        let vessel = Vessel::new(hull);

        assert!((vessel.ap() - 0.0).abs() < 1e-6);
        assert!((vessel.fp() - 100.0).abs() < 1e-6);
        assert!((vessel.lbp() - 100.0).abs() < 1e-6);
    }
}