xmsgrid  1.0
XmEdge.cpp
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
6 //------------------------------------------------------------------------------
7 
8 //----- Included files ---------------------------------------------------------
9 
10 // 1. Precompiled header
11 
12 // 2. My own header
13 #include <xmsgrid/ugrid/XmEdge.h>
14 
15 // 3. Standard library headers
16 #include <algorithm>
17 
18 // 4. External library headers
19 
20 // 5. Shared code headers
21 
22 // 6. Non-shared code headers
23 
24 //----- Forward declarations ---------------------------------------------------
25 
26 //----- External globals -------------------------------------------------------
27 
28 //----- Namespace declaration --------------------------------------------------
29 
31 namespace xms
32 {
33 //----- Constants / Enumerations -----------------------------------------------
34 
35 //----- Classes / Structs ------------------------------------------------------
36 
37 //----- Internal functions -----------------------------------------------------
38 
44 //------------------------------------------------------------------------------
46 //------------------------------------------------------------------------------
48 : m_pt1(0)
49 , m_pt2(0)
50 {
51 } // XmEdge::XmEdge
52 //------------------------------------------------------------------------------
57 //------------------------------------------------------------------------------
58 XmEdge::XmEdge(int a_pt1, int a_pt2, bool a_sorted /* =false */)
59 : m_pt1(a_pt1)
60 , m_pt2(a_pt2)
61 {
62  if (a_sorted)
63  SortIndexes();
64 } // XmEdge::XmEdge
65 //------------------------------------------------------------------------------
68 //------------------------------------------------------------------------------
69 XmEdge::XmEdge(const std::pair<int, int>& a_edge)
70 : m_pt1(a_edge.first)
71 , m_pt2(a_edge.second)
72 {
73 } // XmEdge::XmEdge
74 //------------------------------------------------------------------------------
78 //------------------------------------------------------------------------------
79 bool XmEdge::operator<(const XmEdge& a_rhs) const
80 {
81  if (m_pt1 < a_rhs.m_pt1)
82  return true;
83  else if (m_pt1 > a_rhs.m_pt1)
84  return false;
85  else if (m_pt2 < a_rhs.m_pt2)
86  return true;
87  else
88  return false;
89 } // XmEdge::operator<
90 //------------------------------------------------------------------------------
94 //------------------------------------------------------------------------------
95 bool XmEdge::operator==(const XmEdge& a_rhs) const
96 {
97  return m_pt1 == a_rhs.m_pt1 && m_pt2 == a_rhs.m_pt2;
98 } // XmEdge::operator==
99 //------------------------------------------------------------------------------
102 //------------------------------------------------------------------------------
103 int XmEdge::GetFirst() const
104 {
105  return m_pt1;
106 } // XmEdge::GetFirst
107 //------------------------------------------------------------------------------
110 //------------------------------------------------------------------------------
111 void XmEdge::SetFirst(int a_pt1)
112 {
113  m_pt1 = a_pt1;
114 } // XmEdge::SetFirst
115 //------------------------------------------------------------------------------
118 //------------------------------------------------------------------------------
119 int XmEdge::GetSecond() const
120 {
121  return m_pt2;
122 } // XmEdge::GetSecond
123 //------------------------------------------------------------------------------
126 //------------------------------------------------------------------------------
127 void XmEdge::SetSecond(int a_pt2)
128 {
129  m_pt2 = a_pt2;
130 } // XmEdge::SetSecond
131 //------------------------------------------------------------------------------
135 //------------------------------------------------------------------------------
136 bool XmEdge::IsEquivalent(const XmEdge& a_edge) const
137 {
138  int lhsIdx1 = std::min(m_pt1, m_pt2);
139  int lhsIdx2 = std::max(m_pt1, m_pt2);
140  int rhsIdx1 = std::min(a_edge.m_pt1, a_edge.m_pt2);
141  int rhsIdx2 = std::max(a_edge.m_pt1, a_edge.m_pt2);
142  return lhsIdx1 == rhsIdx1 && lhsIdx2 == rhsIdx2;
143 } // XmEdge::IsEquivalent
144 //------------------------------------------------------------------------------
146 //------------------------------------------------------------------------------
148 {
149  if (m_pt1 > m_pt2)
150  std::swap(m_pt1, m_pt2);
151 } // XmEdge::SortIndexes
152 //------------------------------------------------------------------------------
157 //------------------------------------------------------------------------------
158 bool XmEdgesEquivalent(const XmEdge& a_edge1, const XmEdge& a_edge2)
159 {
160  return a_edge1.IsEquivalent(a_edge2);
161 } // XmEdgesEquivalent
162 
163 } // namespace xms
164 
165 #ifdef CXX_TEST
166 //------------------------------------------------------------------------------
167 // Unit Tests
168 //------------------------------------------------------------------------------
169 using namespace xms;
170 #include <xmsgrid/ugrid/XmEdge.t.h>
171 
176 //------------------------------------------------------------------------------
178 //------------------------------------------------------------------------------
180 {
181  XmEdge edge1(0, 1);
182  XmEdge edge1a(1, 0);
183  XmEdge edge2(1, 2);
184  TS_ASSERT(edge1 < edge2);
185  TS_ASSERT(edge1 < edge1a);
186  TS_ASSERT(!(edge1a < edge1));
187  TS_ASSERT(!(edge2 < edge1));
188 } // XmEdgeUnitTests::testLessThanOperator
189 //------------------------------------------------------------------------------
191 //------------------------------------------------------------------------------
193 {
194  XmEdge edge1(0, 1);
195  XmEdge edge1a(1, 0, false);
196  XmEdge edge1b(1, 0, true);
197  XmEdge edge2(1, 2);
198  TS_ASSERT(edge1 == edge1);
199  TS_ASSERT(!(edge1 == edge1a));
200  TS_ASSERT(edge1 == edge1b);
201  TS_ASSERT(!(edge2 == edge1));
202 } // XmEdgeUnitTests::testEqualsOperator
203 //------------------------------------------------------------------------------
205 //------------------------------------------------------------------------------
207 {
208  XmEdge edge1(0, 1);
209  XmEdge edge1a(1, 0);
210  XmEdge edge2(1, 2);
211  TS_ASSERT(edge1.IsEquivalent(edge1a));
212  TS_ASSERT(!(edge1 == edge1a));
213  TS_ASSERT(edge1a.IsEquivalent(edge1));
214  TS_ASSERT(!edge1.IsEquivalent(edge2));
215  TS_ASSERT(XmEdgesEquivalent(edge1, edge1a));
216  TS_ASSERT(!XmEdgesEquivalent(edge1, edge2));
217 } // XmEdgeUnitTests::testIsEquivalent
218 
219 #endif
void SetFirst(int a_pt1)
Set the first index.
Definition: XmEdge.cpp:111
void SetSecond(int a_pt2)
Set the second index.
Definition: XmEdge.cpp:127
void testIsEquivalent()
Test XmEdge::IsEquivalent.
Definition: XmEdge.cpp:206
int GetSecond() const
Get the second index.
Definition: XmEdge.cpp:119
int m_pt1
First point on the edge.
Definition: XmEdge.h:51
bool IsEquivalent(const XmEdge &a_edge) const
Test if edge is the same ignoring direction.
Definition: XmEdge.cpp:136
int m_pt2
Second point on the edge.
Definition: XmEdge.h:52
void SortIndexes()
Sort the indexes so minimum index is first.
Definition: XmEdge.cpp:147
void testEqualsOperator()
Test XmEdge equals operator.
Definition: XmEdge.cpp:192
Two integer values representing an edge of an XmUGrid. By default has a direction. Can be sorted to have minimum index first.
Definition: XmEdge.h:33
XMS Namespace.
Definition: geoms.cpp:34
bool XmEdgesEquivalent(const XmEdge &a_edge1, const XmEdge &a_edge2)
Test if two edges are the same ignoring direction.
Definition: XmEdge.cpp:158
void testLessThanOperator()
Test XmEdge less than operator.
Definition: XmEdge.cpp:179
int GetFirst() const
Get the first index.
Definition: XmEdge.cpp:103
bool operator==(const XmEdge &a_rhs) const
Equals operator.
Definition: XmEdge.cpp:95
XmEdge()
Default constructor.
Definition: XmEdge.cpp:47
bool operator<(const XmEdge &a_rhs) const
Less than operator by first index then second.
Definition: XmEdge.cpp:79