World Ocean Simulation System (WOSS) library
woss-creator-container.h
Go to the documentation of this file.
1/* WOSS - World Ocean Simulation System -
2 *
3 * Copyright (C) 2009 2025 Federico Guerra
4 * and regents of the SIGNET lab, University of Padova
5 *
6 * Author: Federico Guerra - WOSS@guerra-tlc.com
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation;
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22
31
32
33#ifndef WOSS_CREATOR_CONTAINER_DEFINITIONS_H
34#define WOSS_CREATOR_CONTAINER_DEFINITIONS_H
35
36
37#include <string>
38#include <map>
39#include <memory>
40#include <iostream>
41#include <definitions-handler.h>
42#include <type_traits>
43
44
45namespace woss {
46
54
62 CustomTransducer( const std::string& name = "", double bearing = 0.0, double vert_rot = 0.0, double horiz_rot = 0.0, double mult = 1.0, double add = 0.0 )
63 : type(name), initial_bearing(bearing), initial_vert_rotation(vert_rot), initial_horiz_rotation(horiz_rot), multiply_costant(mult), add_costant(add) { }
64
65 friend std::ostream& operator<<( std::ostream& os, const CustomTransducer& instance ) {
66 os << "type name = " << instance.type <<"; initial bearing = " << instance.initial_bearing
67 << "; initial vertical rotation = " << instance.initial_vert_rotation
68 << "; initial horizontal rotation = " << instance.initial_horiz_rotation
69 << "; mult costant = " << instance.multiply_costant << "; add costant = "
70 << instance.add_costant;
71 return os;
72 }
73
77 std::string type;
78
83
88
93
98
103
104 };
105
112 template< typename Data >
114
115 public:
116
120 static std::shared_ptr<Location> ALL_LOCATIONS;
121
125 static const CoordZ ALL_COORDZ;
126
128
130
135 bool isEmpty() const;
136
141 int size() const;
142
150 bool insert( const Data& data, const std::shared_ptr<Location>& tx, const std::shared_ptr<Location>& rx );
151
159 bool insert( const Data& data, const CoordZ& tx, const CoordZ& rx );
160
167 Data get( const std::shared_ptr<Location>& tx, const std::shared_ptr<Location>& rx ) const;
168
175 Data get( const CoordZ& tx, const CoordZ& rx ) const;
176
183
189 void erase( const std::shared_ptr<Location>& tx, const std::shared_ptr<Location>& rx );
190
196 void erase( const CoordZ& tx, const CoordZ& rx );
197
205 void replace( const Data& data, const std::shared_ptr<Location>& tx, const std::shared_ptr<Location>& rx );
206
214 void replace( const Data& data, const CoordZ& tx, const CoordZ& rx );
215
219 void clear();
220
225 void setDebug( bool flag ) { debug = flag; }
226
231 bool isUsingDebug() const { return debug; }
232
233 protected:
234
238 using InnerContainer = std::map< std::shared_ptr<Location>, Data >;
239 using ICIter = typename InnerContainer::iterator;
240 using ICRIter = typename InnerContainer::reverse_iterator;
241 using ICCIter = typename InnerContainer::const_iterator;
242 using ICCRIter = typename InnerContainer::const_reverse_iterator;
243
247 using DataContainer = std::map< std::shared_ptr<Location>, InnerContainer >;
248 using DCIter = typename DataContainer::iterator;
249 using DCCIter = typename DataContainer::const_iterator;
250 using DCRIter = typename DataContainer::reverse_iterator;
251 using DCRCIter = typename DataContainer::const_reverse_iterator;
252
259 DCIter find( const CoordZ& coordinates );
260
268 ICIter find( const CoordZ& coordinates, const DCIter& iter );
269
275 std::shared_ptr<Location> createLocation( const CoordZ& coordinates );
276
281
285 bool debug = false;
286
287 };
288
289
290 template< typename Data >
291 std::shared_ptr<Location> WossCreatorContainer< Data >::ALL_LOCATIONS = nullptr;
292
293
294 template< typename Data >
296
297
298 template< typename Data >
300 return data_container.empty();
301 }
302
303
304 template< typename Data >
306 return data_container.size();
307 }
308
309
310 template< typename Data >
311 inline std::shared_ptr<Location> WossCreatorContainer< Data >::createLocation( const CoordZ& coordinates ) {
312 if ( coordinates == ALL_COORDZ )
313 return nullptr;
314 return std::move( SDefHandler::instance().createLocation(coordinates) );
315 }
316
317
318 template< typename Data >
320 auto it_all_loc = data_container.end();
321
322 for ( auto it = data_container.begin(); it != data_container.end(); it++ ) {
323 if ( it->first == ALL_LOCATIONS ) {
324 it_all_loc = it;
325 if ( coordinates == ALL_COORDZ )
326 break;
327 else
328 continue;
329 }
330 if ( it->first->isEquivalentTo( coordinates ) ) {
331 if (debug)
332 std::cout << "WossCreatorContainer::find() tx coordinates found = "
333 << coordinates << std::endl;
334 return it;
335 }
336 }
337
338 if ( coordinates == ALL_COORDZ && it_all_loc != data_container.end() ) {
339 if (debug)
340 std::cout << "WossCreatorContainer::find() tx ALL_COORDZ found" << std::endl;
341 return it_all_loc;
342 }
343
344 if (debug && coordinates != ALL_COORDZ )
345 std::cout << "WossCreatorContainer::find() tx coordinates not found = "
346 << coordinates << std::endl;
347
348 if (debug && coordinates == ALL_COORDZ )
349 std::cout << "WossCreatorContainer::find() tx ALL_COORDZ not found" << std::endl;
350 return data_container.end();
351 }
352
353
354 template< typename Data >
356 auto it_all_loc = iter->second.end();
357
358 for ( auto it = iter->second.begin(); it != iter->second.end(); it++ ) {
359 if ( it->first == ALL_LOCATIONS ) {
360 it_all_loc = it;
361 if ( coordinates == ALL_COORDZ )
362 break;
363 else
364 continue;
365 }
366 if ( it->first->isEquivalentTo( coordinates ) ) {
367 if (debug)
368 std::cout << "WossCreatorContainer::find() rx coordinates found = "
369 << coordinates << std::endl;
370 return it;
371 }
372 }
373
374 if ( coordinates == ALL_COORDZ && it_all_loc != iter->second.end() ) {
375 if (debug)
376 std::cout << "WossCreatorContainer::find() rx ALL_COORDZ found" << std::endl;
377 return it_all_loc;
378 }
379
380 if (debug && coordinates != ALL_COORDZ )
381 std::cout << "WossCreatorContainer::find() rx coordinates not found = "
382 << coordinates << std::endl;
383
384 if (debug && coordinates == ALL_COORDZ )
385 std::cout << "WossCreatorContainer::find() rx ALL_COORDZ not found" << std::endl;
386 return iter->second.end();
387 }
388
389
390 template< typename Data >
391 inline bool WossCreatorContainer< Data >::insert( const Data& data, const std::shared_ptr<Location>& tx, const std::shared_ptr<Location>& rx ) {
392 auto it = data_container.find(tx);
393 if ( it == data_container.end() ) {
394 data_container[tx][rx] = data;
395 return true;
396 }
397 auto it2 = it->second.find(rx);
398 if ( it2 == it->second.end() ) {
399 (it->second)[rx] = data;
400 return true;
401 }
402 return false;
403 }
404
405
406 template< typename Data >
407 inline bool WossCreatorContainer< Data >::insert( const Data& data, const CoordZ& tx, const CoordZ& rx ) {
408 auto it = find( tx );
409 if ( it == data_container.end() ) {
410 data_container[ createLocation(tx) ][ createLocation(rx) ] = data;
411 return true;
412 }
413 auto it2 = find( rx, it );
414 if ( it2 == it->second.end() ) {
415 (it->second)[ createLocation(rx) ] = data;
416 return true;
417 }
418 return false;
419 }
420
421
422 template< typename Data >
423 inline Data WossCreatorContainer< Data >::get( const std::shared_ptr<Location>& tx, const std::shared_ptr<Location>& rx ) const {
424 auto it = const_cast< typename WossCreatorContainer< Data >::DataContainer& >(data_container).find( tx );
425 bool tx_changed_to_all_locations = false;
426
427 if ( tx != ALL_LOCATIONS ) {
428 if ( it == data_container.end() ) {
429 if ( debug )
430 std::cout << "WossCreatorContainer::get() no tx location found = " << *tx
431 << "; trying ALL_LOCATIONS"<< std::endl;
432
434 tx_changed_to_all_locations = true;
435 }
436
437 if ( debug )
438 std::cout << "WossCreatorContainer::get() tx location found = " << *tx << std::endl;
439 }
440
441 if ( it != data_container.end() ) {
442 auto it2 = it->second.find( rx );
443
444 if ( rx != ALL_LOCATIONS ) {
445 if ( it2 == it->second.end() ) {
446 if ( debug )
447 std::cout << "WossCreatorContainer::get() no rx location found = " << *rx
448 << "; trying ALL_LOCATIONS"<< std::endl;
449
450 it2 = it->second.find( ALL_LOCATIONS );
451 }
452
453 if ( debug )
454 std::cout << "WossCreatorContainer::get() rx location found = " << *rx << std::endl;
455 }
456
457 if ( it2 != it->second.end() ) {
458 // Special handling for CustomTransducer
459 if constexpr (std::is_same_v<Data, CustomTransducer>) {
460 // If we're getting a CustomTransducer, we need to rotate the angles based on location orientation
461 const auto& original_data = it2->second;
462 if (tx_changed_to_all_locations == true) {
463 return CustomTransducer(original_data);
464 } else {
465 if ( debug )
466 std::cout << "WossCreatorContainer::get() rotating angle" << std::endl;
467
468 return CustomTransducer(
469 original_data.type,
470 it->first->getBearing(),
471 (original_data.initial_vert_rotation + it->first->getVerticalOrientation()),
472 (original_data.initial_horiz_rotation + it->first->getHorizontalOrientation()),
473 original_data.multiply_costant,
474 original_data.add_costant
475 );
476 }
477 } else {
478 return it2->second;
479 }
480 }
481 }
482
483 std::cerr << "WARNING: WossCreatorContainer::get() no tx nor rx location found, returning default constructor!!" << std::endl;
484
485 return Data();
486 }
487
488
489 template< typename Data >
493
494
495 template< typename Data >
496 inline Data WossCreatorContainer< Data >::get( const CoordZ& tx, const CoordZ& rx ) const {
497 auto it = const_cast< WossCreatorContainer< Data >& >(*this).find( tx );
498 bool tx_changed_to_all_coordz = false;
499
500 if ( tx != ALL_COORDZ ) {
501 if ( it == data_container.end() ) {
502 if ( debug )
503 std::cout << "WossCreatorContainer::get() no tx coord found = " << tx
504 << "; trying ALL_COORDZ"<< std::endl;
505
506 it = const_cast< WossCreatorContainer< Data >& >(*this).find( ALL_COORDZ );
507 tx_changed_to_all_coordz = true;
508 }
509
510 if ( debug )
511 std::cout << "WossCreatorContainer::get() tx coord found = " << tx << std::endl;
512 }
513
514 if ( it != data_container.end() ) {
515 auto it2 = const_cast< WossCreatorContainer< Data >& >(*this).find( rx, it );
516
517 if ( rx != ALL_COORDZ ) {
518 if ( it2 == it->second.end() ) {
519 if ( debug )
520 std::cout << "WossCreatorContainer::get() no rx coord found = " << rx
521 << "; trying ALL_COORDZ"<< std::endl;
522
523 it2 = const_cast< WossCreatorContainer< Data >& >(*this).find( ALL_COORDZ, it );
524 }
525
526 if ( debug )
527 std::cout << "WossCreatorContainer::get() rx coord found = " << rx << std::endl;
528 }
529
530 if ( it2 != it->second.end() ) {
531 if ( debug )
532 std::cout << "WossCreatorContainer::get() value found = " << it2->second << std::endl;
533
534 // Special handling for CustomTransducer
535 if constexpr (std::is_same_v<Data, CustomTransducer>) {
536 // If we're getting a CustomTransducer, we need to rotate the angles based on location orientation
537 const auto& original_data = it2->second;
538 if (tx_changed_to_all_coordz == true) {
539 return CustomTransducer(original_data);
540 } else {
541 if ( debug )
542 std::cout << "WossCreatorContainer::get() rotating angle" << std::endl;
543
544 return CustomTransducer(
545 original_data.type,
546 it->first->getBearing(),
547 (original_data.initial_vert_rotation + it->first->getVerticalOrientation()),
548 (original_data.initial_horiz_rotation + it->first->getHorizontalOrientation()),
549 original_data.multiply_costant,
550 original_data.add_costant
551 );
552 }
553 } else {
554 return it2->second;
555 }
556 }
557 }
558 std::cerr << "WARNING: WossCreatorContainer::get() no tx nor rx coordinates found, returning default constructor!!" << std::endl;
559
560 return Data();
561 }
562
563
564 template< typename Data >
565 inline void WossCreatorContainer< Data >::erase( const std::shared_ptr<Location>& tx, const std::shared_ptr<Location>& rx ) {
566 auto it = data_container.find(tx);
567 if ( it != data_container.end() ) {
568 it->second.erase( rx );
569 if ( it->second.empty() )
570 data_container.erase(it);
571 }
572 }
573
574
575 template< typename Data >
576 inline void WossCreatorContainer< Data >::erase( const CoordZ& tx, const CoordZ& rx ) {
577 for ( auto it = data_container.begin(); it != data_container.end(); ) {
578 bool tx_found = false;
579
580 if ( it->first != ALL_LOCATIONS ) {
581 if ( it->first->isEquivalentTo( tx ) )
582 tx_found = true;
583 } else {
584 if ( tx == ALL_COORDZ )
585 tx_found = true;
586 }
587
588 if ( tx_found ) {
589 for ( auto it2 = it->second.begin(); it2 != it->second.end(); ) {
590 bool rx_found = false;
591
592 if ( it2->first != ALL_LOCATIONS ) {
593 if ( it2->first->isEquivalentTo( rx ) )
594 rx_found = true;
595 } else {
596 if ( rx == ALL_COORDZ )
597 rx_found = true;
598 }
599
600 if ( rx_found )
601 it->second.erase(it2++);
602 else
603 ++it2;
604 }
605
606 }
607
608 if ( it->second.empty() )
609 data_container.erase(it++);
610 else ++it;
611 }
612 }
613
614
615 template< typename Data >
616 inline void WossCreatorContainer< Data >::replace( const Data& data, const std::shared_ptr<Location>& tx, const std::shared_ptr<Location>& rx ) {
617 data_container[tx][rx] = data;
618 }
619
620
621 template< typename Data >
622 inline void WossCreatorContainer< Data >::replace( const Data& data, const CoordZ& tx, const CoordZ& rx ) {
623 auto it = find( tx );
624 if ( it == data_container.end() ) {
625 data_container[ createLocation(tx) ][ createLocation(rx) ] = data;
626 return;
627 }
628 auto it2 = find( rx, it );
629 if ( it2 == it->second.end() ) {
630 (it->second)[ createLocation(rx) ] = data;
631 return;
632 }
633 it2->second = data;
634 }
635
636
637 template< typename Data >
639 data_container.clear();
640 }
641
642
643} // namespace woss
644
645
646#endif // WOSS_CREATOR_CONTAINER_DEFINITIONS_H
3D-Coordinates (lat, long, depth) class definitions and functions library
Definition coordinates-definitions.h:384
static DefHandler & instance()
std::map< std::shared_ptr< Location >, CustomAngles > InnerContainer
Definition woss-creator-container.h:238
static const CoordZ ALL_COORDZ
Definition woss-creator-container.h:125
typename DataContainer::const_iterator DCCIter
Definition woss-creator-container.h:249
Data get(const CoordZ &tx, const CoordZ &rx) const
Definition woss-creator-container.h:496
bool insert(const Data &data, const CoordZ &tx, const CoordZ &rx)
Definition woss-creator-container.h:407
typename DataContainer::iterator DCIter
Definition woss-creator-container.h:248
DCIter find(const CoordZ &coordinates)
Definition woss-creator-container.h:319
typename InnerContainer::reverse_iterator ICRIter
Definition woss-creator-container.h:240
Data & accessAllLocations()
Definition woss-creator-container.h:490
typename InnerContainer::const_reverse_iterator ICCRIter
Definition woss-creator-container.h:242
bool debug
Definition woss-creator-container.h:285
bool insert(const Data &data, const std::shared_ptr< Location > &tx, const std::shared_ptr< Location > &rx)
Definition woss-creator-container.h:391
Data get(const std::shared_ptr< Location > &tx, const std::shared_ptr< Location > &rx) const
Definition woss-creator-container.h:423
DataContainer data_container
Definition woss-creator-container.h:280
void clear()
Definition woss-creator-container.h:638
void replace(const Data &data, const std::shared_ptr< Location > &tx, const std::shared_ptr< Location > &rx)
Definition woss-creator-container.h:616
std::shared_ptr< Location > createLocation(const CoordZ &coordinates)
Definition woss-creator-container.h:311
typename InnerContainer::iterator ICIter
Definition woss-creator-container.h:239
typename DataContainer::reverse_iterator DCRIter
Definition woss-creator-container.h:250
typename DataContainer::const_reverse_iterator DCRCIter
Definition woss-creator-container.h:251
ICIter find(const CoordZ &coordinates, const DCIter &iter)
Definition woss-creator-container.h:355
static std::shared_ptr< Location > ALL_LOCATIONS
Definition woss-creator-container.h:120
typename InnerContainer::const_iterator ICCIter
Definition woss-creator-container.h:241
void setDebug(bool flag)
Definition woss-creator-container.h:225
int size() const
Definition woss-creator-container.h:305
void replace(const Data &data, const CoordZ &tx, const CoordZ &rx)
Definition woss-creator-container.h:622
void erase(const CoordZ &tx, const CoordZ &rx)
Definition woss-creator-container.h:576
std::map< std::shared_ptr< Location >, InnerContainer > DataContainer
Definition woss-creator-container.h:247
bool isUsingDebug() const
Definition woss-creator-container.h:231
void erase(const std::shared_ptr< Location > &tx, const std::shared_ptr< Location > &rx)
Definition woss-creator-container.h:565
bool isEmpty() const
Definition woss-creator-container.h:299
Provides the interface for woss::DefHandler class.
Definition ac-toolbox-arr-asc-reader.h:44
Initial set up of a transducer.
Definition woss-creator-container.h:53
double initial_vert_rotation
Definition woss-creator-container.h:87
double initial_bearing
Definition woss-creator-container.h:82
double add_costant
Definition woss-creator-container.h:102
friend std::ostream & operator<<(std::ostream &os, const CustomTransducer &instance)
Definition woss-creator-container.h:65
CustomTransducer(const std::string &name="", double bearing=0.0, double vert_rot=0.0, double horiz_rot=0.0, double mult=1.0, double add=0.0)
Definition woss-creator-container.h:62
double initial_horiz_rotation
Definition woss-creator-container.h:92
std::string type
Definition woss-creator-container.h:77
double multiply_costant
Definition woss-creator-container.h:97