123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- #ifndef BASIN_HPP
- #define BASIN_HPP
- #include <string>
- using namespace std;
- enum Direction{Left,Right,Both};
- enum BasinPosition{Middle,BorderLeft,BorderRight};
- struct Summit{
- size_t ix;
- double h;
- bool operator<(const Summit& S) const;
- Summit(size_t ix,double h);
- };
- class Basin{
- public:
- static constexpr double delta_both_leak=0.01;
- size_t xleft,xright;
- size_t xbottom; //Used only for primitives bassin
- Direction leak_direction;
- BasinPosition position;
- //Basin* neighbours[2];
- double hmin,hleft,hright;
- Basin* left;
- Basin* right;
- void compute_leak_direction();
- public:
- Basin();
- Basin(Basin* left,Basin* right);
- void display(string indent="");
- bool is_primitive();
- };
- inline
- Summit::Summit(size_t ix_,double h_):ix(ix_),h(h_){
- }
- inline bool
- Summit::operator<(const Summit& S) const{
- return h<S.h;
- }
- inline Basin::Basin(){
- left=nullptr;
- right=nullptr;
- position=Middle;
- }
- inline void
- Basin::compute_leak_direction(){
- switch(position){
- case Middle:
- if(abs(hleft-hright)<delta_both_leak) leak_direction=Both;
- else leak_direction=(hleft>hright)?Right:Left;
- break;
- case BorderLeft:
- leak_direction=Right;
- break;
- case BorderRight:
- leak_direction=Left;
- break;
- default:
- break;
- };
- }
- inline Basin::Basin(Basin* left_,Basin* right_){
- left=left_;
- right=right_;
- xleft=left->xleft;
- xright=right->xright;
- hleft=left->hleft;
- hright=right->hright;
- hmin=min(left->hmin,right->hmin);
- if(left->position==BorderLeft) position=BorderLeft;
- else if(right->position==BorderRight) position=BorderRight;
- else position=Middle;
- compute_leak_direction();
- }
- inline void Basin::display(string indent){
- cout<<indent<<'['<<xleft<<','<<xright<<']'<<endl;
- if(left==nullptr) return;
- left->display(indent+' ');
- right->display(indent+' ');
- }
- inline
- bool Basin::is_primitive(){
- return left=nullptr;
- }
- #endif
|