G+Smo  24.08.0
Geometry + Simulation Modules
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gsVSegment.h
Go to the documentation of this file.
1 
14 #pragma once
15 
16 namespace gismo
17 {
18 
25 template<class T>
26 class gsVSegment
27 {
28 public:
30  //gsVSegment() { }
31 
33  gsVSegment(int x, int bottomY, int upperY, bool l)
34  {
35  m_x = x;
36  m_y = std::pair<int, int> (bottomY, upperY);
37  left = l;
38  }
39 
41  //~gsVSegment() { }
42 
45  bool isClosing(const gsVSegment& other) const;
46 
49  bool isConnected(const gsVSegment& other) const;
50 
51  T length() const;
52 
55  bool cannotDeleteOverlap(gsVSegment<T> & other);
56 
57  // getters
58 
59  inline T getX() const
60  {
61  return m_x;
62  }
63 
64  inline T getYUp() const
65  {
66  return m_y.second;
67  }
68 
69  inline T getYDown() const
70  {
71  return m_y.first;
72  }
73 
74  inline std::pair<T,T> getY() const
75  {
76  return m_y;
77  }
78 
79  // setters
80 
81  inline void setX( const T& new_x )
82  {
83  m_x = new_x;
84  }
85 
86  inline void setYUp( const T& new_y_up )
87  {
88  m_y.second = new_y_up;
89  }
90 
91  inline void setYDown( const T& new_y_down )
92  {
93  m_y.first = new_y_down;
94  }
95 
96  inline void setY( const std::pair<T,T>& new_m_y )
97  {
98  m_y = new_m_y;
99  }
100 
101  bool operator< (const gsVSegment<T>& other) const
102  {
103  if(m_x == other.getX())
104  {
105  return m_y.first < other.getYDown();
106  }
107  else
108  {
109  return m_x < other.getX();
110  }
111  }
112 
113  bool operator== ( const gsVSegment<T>& other ) const
114  {
115  if( (m_x == other.getX() ) && (m_y.first == other.getYDown() ) && (m_y.second == other.getYUp() ) )
116  return true;
117  else
118  return false;
119  }
120 
121 protected:
122 
124  T m_x;
125 
127  std::pair<T, T> m_y;
128 
130  bool left;
131 };
132 
133 template<class T>
134 bool gsVSegment<T>::isClosing(const gsVSegment& other ) const
135 {
136  return ( ( other.getYDown() == m_y.first) && (( other.getYUp() == m_y.second)) );
137 }
138 
139 template<class T>
140 bool gsVSegment<T>::isConnected(const gsVSegment& other ) const
141 {
142  return ( ( other.getYUp() == m_y.first) || (( other.getYDown() == m_y.second)) );
143 }
144 
145 template<class T>
146 T gsVSegment<T>::length() const
147 {
148  return (m_y.second - m_y.first);
149 }
150 
151 template<class T>
153 {
154  gsVSegment<T> tempa = *this;
155  gsVSegment<T> tempb = other;
156  std::vector<T> v(4);
157 
158  v[0] = m_y.first;
159  v[1] = m_y.second;
160  v[2] = other.m_y.first;
161  v[3] = other.m_y.second;
162 
163  // This is the key trick:
164  std::sort(v.begin(), v.end());
165 
166  m_y.first = v[0];
167  m_y.second = v[1];
168  other.m_y.first = v[2];
169  other.m_y.second = v[3];
170 
171  // iff nothing changed (or just swapped), return true
172  if( ((tempa==*this)&&(tempb==other)) || ((tempa==other)&&(tempb==*this)))
173  return true;
174  else
175  return false;
176 }
177 
179 template<class T>
180 std::ostream &operator<<(std::ostream &os, const gsVSegment<T> a)
181 {
182  os<<"x:"<<a.getX() <<" , y: ["<< a.getYDown() <<" ,"<< a.getYUp() <<"] ";
183  return os;
184 }
185 
186 } // namespace gismo
Class for representing a vertical line segment in 2D. Helper for the class gsAAPolyline.
Definition: gsHDomain.h:33
T m_x
The x-coordinate.
Definition: gsVSegment.h:124
bool isConnected(const gsVSegment &other) const
Definition: gsVSegment.h:140
bool left
Whether the segment is on the left hand-side of the enclosed domain.
Definition: gsVSegment.h:130
gsVSegment(int x, int bottomY, int upperY, bool l)
Default empty constructor.
Definition: gsVSegment.h:33
bool isClosing(const gsVSegment &other) const
Default destructor.
Definition: gsVSegment.h:134
std::pair< T, T > m_y
Lower and upper y–coordinates.
Definition: gsVSegment.h:127
bool cannotDeleteOverlap(gsVSegment< T > &other)
otherwise gets rid of their overlap and returns false.
Definition: gsVSegment.h:152