cell.cpp 25.6 KB
Newer Older
Kirill Terekhov's avatar
Kirill Terekhov committed
1 2 3 4 5 6 7
#include "inmost.h"
#if defined(USE_MESH)

namespace INMOST
{
	
	
Kirill Terekhov's avatar
Kirill Terekhov committed
8
	bool Cell::CheckEdgeOrder() const
Kirill Terekhov's avatar
Kirill Terekhov committed
9
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
10
		assert(GetHandleElementType(GetHandle())==CELL);
Kirill Terekhov's avatar
Kirill Terekhov committed
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
		Mesh * m = GetMeshLink();
		if( Element::GetGeometricDimension(m->GetGeometricType(GetHandle())) == 2 )
		{
			if( !m->HideMarker() )
			{
				HandleType last, first;
				adj_type const & lc = m->LowConn(GetHandle());
				if( lc.size() < 3 ) return false;
				HandleType q = lc[0]; //edge 0
				adj_type const & qlc = m->LowConn(q);
				first = qlc[0]; //node 0
				last = qlc[1]; //node 1
				HandleType r = lc[1]; //edge 1
				adj_type & rlc = m->LowConn(r);
				if( first == rlc[0] || first == rlc[1] )
				{
					HandleType temp = first;
					first = last;
					last = temp;
				}
				else if ( !(last == rlc[0] || last == rlc[1]) )
					return false;
				adj_type::size_type it = 1, iend = lc.size()-1;
				while(it < iend) //loop over edges
				{
					adj_type const & ilc = m->LowConn(lc[it]);
					if( last == ilc[0] ) last = ilc[1];
					else if( last == ilc[1] ) last = ilc[0];
					else return false;
					++it;
				}
			}
			else
			{
				MarkerType hm = m->HideMarker();
				HandleType first, last;
				enumerator i = ENUMUNDEF, k = ENUMUNDEF, k1 = ENUMUNDEF, k2;
				adj_type const & lc = m->LowConn(GetHandle());
				if( m->Count(lc.data(),lc.size(),hm) < 3 ) return false;
				i = m->getNext(lc.data(),static_cast<enumerator>(lc.size()),i,hm);
				HandleType q = lc[i]; //edge 0
				adj_type const & qlc = m->LowConn(q);
				k = m->getNext(qlc.data(),static_cast<enumerator>(qlc.size()),k,hm);
				first = qlc[k]; //node 0
				k = m->getNext(qlc.data(),static_cast<enumerator>(qlc.size()),k,hm);
				last = qlc[k]; //node 1
				i = m->getNext(lc.data(),static_cast<enumerator>(lc.size()),i,hm);
				HandleType r = lc[i]; //edge 1
				adj_type const & rlc = m->LowConn(r);
				k1 = m->getNext(rlc.data(),static_cast<enumerator>(rlc.size()),k1,hm);
				k2 = m->getNext(rlc.data(),static_cast<enumerator>(rlc.size()),k1,hm);
				if( first == rlc[k1] || first == rlc[k2] )
				{
					HandleType temp = first;
					first = last;
					last = temp;
				}
				else if ( !(last == rlc[k1] || last == rlc[k2]) )
					return false;
				adj_type::size_type it = 1, iend = lc.size()-1;
				while(it != iend) if( !m->GetMarker(lc[it],hm) ) //loop over edges
				{
					adj_type const & ilc = m->LowConn(lc[it]);
					k1 = ENUMUNDEF; 
					k1 = m->getNext(ilc.data(),static_cast<enumerator>(ilc.size()),k1,hm);
					k2 = m->getNext(ilc.data(),static_cast<enumerator>(ilc.size()),k1,hm);
					if( last == ilc[k1] ) last = ilc[k2];
					else if( last == ilc[k2] ) last = ilc[k1];
					else return false;
					++it;
				}
			}
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
84 85 86
		return true;
	}
	
Kirill Terekhov's avatar
Kirill Terekhov committed
87
	bool Cell::FixEdgeOrder() const
Kirill Terekhov's avatar
Kirill Terekhov committed
88
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
89
		assert(GetHandleElementType(GetHandle())==CELL);
Kirill Terekhov's avatar
Kirill Terekhov committed
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
		Mesh * m = GetMeshLink();
		if( Element::GetGeometricDimension(m->GetGeometricType(GetHandle())) == 2 )
		{
			if( !m->HideMarker() )
			{
				HandleType last, first;
				adj_type & lc = m->LowConn(GetHandle());
				if( lc.size() < 3 ) return false;
				HandleType q = lc[0]; //edge 0
				adj_type const & qlc = m->LowConn(q);
				if( qlc.size() != 2 ) return false;
				first = qlc[0]; //node 0
				last = qlc[1]; //node 1
				HandleType r = lc[1]; //edge 1
				adj_type & rlc = m->LowConn(r);
				if( rlc.size() != 2 ) return false;
				if( first == rlc[0] || first == rlc[1] )
				{
					HandleType temp = first;
					first = last;
					last = temp;
				}
				else if ( !(last == rlc[0] || last == rlc[1]) )
				{
					adj_type::size_type jt = 2, jend = lc.size();
					while(jt < jend)
					{
						adj_type const & ilc = m->LowConn(lc[jt]);
						if( ilc.size() != 2 ) return false;
						if( first == ilc[0] || first == ilc[1] )
						{
							HandleType temp = lc[1];
							lc[1] = lc[jt];
Kirill Terekhov's avatar
Kirill Terekhov committed
123
							lc[jt] = temp;
Kirill Terekhov's avatar
Kirill Terekhov committed
124 125 126 127 128 129 130 131 132
							temp = first;
							first = last;
							last = temp;
							break;
						}
						else if( last == ilc[0] || last == ilc[1] )
						{
							HandleType temp = lc[1];
							lc[1] = lc[jt];
Kirill Terekhov's avatar
Kirill Terekhov committed
133
							lc[jt] = temp;
Kirill Terekhov's avatar
Kirill Terekhov committed
134 135 136 137 138 139 140
							break;
						}
						++jt;
					}
					if( jt == jend ) return false; //no matching edge
				}
				adj_type::size_type it = 1, iend = lc.size()-1;
Kirill Terekhov's avatar
Kirill Terekhov committed
141
				bool corrected = false;
Kirill Terekhov's avatar
Kirill Terekhov committed
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
				while(it < iend) //loop over edges
				{
					adj_type const & ilc = m->LowConn(lc[it]);
					if( ilc.size() != 2 ) return false;
					if( last == ilc[0] ) 
					{
						last = ilc[1];
						++it;
					}
					else if( last == ilc[1] ) 
					{
						last = ilc[0];
						++it;
					}
					else //search for the connected edge and swap with current
					{
						adj_type::size_type jt = it+1, jend = lc.size();
						while(jt < jend)
						{
							adj_type const & ilc = m->LowConn(lc[jt]);
							if( ilc.size() != 2 ) return false;
							if( last == ilc[0] || last == ilc[1] )
							{
								HandleType temp = lc[it];
								lc[it] = lc[jt];
								lc[jt] = temp;
Kirill Terekhov's avatar
Kirill Terekhov committed
168
								corrected = true;
Kirill Terekhov's avatar
Kirill Terekhov committed
169 170 171 172 173 174
								break;
							}
						}
						if( jt == jend ) return false; //no matching edge
					}
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
175 176 177 178 179 180 181
				if( corrected )
				{
					ElementArray<Node> nodes(GetMeshLink());
					GetMeshLink()->RestoreCellNodes(GetHandle(),nodes);
					Element::adj_type & hc = GetMeshLink()->HighConn(GetHandle());
					hc.replace(hc.begin(),hc.end(),nodes.begin(),nodes.end());
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
182 183 184
				//check that the loop is closed
				adj_type const & ilc = m->LowConn(lc[iend]);
				if( ilc.size() != 2 ) return false;
Kirill Terekhov's avatar
Kirill Terekhov committed
185
				if( !( (ilc[0] == last && ilc[1] == first) || (ilc[0] == first && ilc[1] == last) ) )
Kirill Terekhov's avatar
Kirill Terekhov committed
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
					return false;
			}
			else
			{
				HandleType first, last;
				enumerator i = ENUMUNDEF, k = ENUMUNDEF, k1 = ENUMUNDEF, k2;
				MarkerType hm = m->HideMarker();
				adj_type & lc = m->LowConn(GetHandle());
				if( m->Count(lc.data(),lc.size(),hm) < 3 ) return false;
				i = m->getNext(lc.data(),static_cast<enumerator>(lc.size()),i,hm);
				HandleType q = lc[i]; //edge 0
				adj_type const & qlc = m->LowConn(q);
				if(m->Count(qlc.data(),qlc.size(),hm)!=2) return false;
				k = m->getNext(qlc.data(),static_cast<enumerator>(qlc.size()),k,hm);
				first = qlc[k]; //node 0
				k = m->getNext(qlc.data(),static_cast<enumerator>(qlc.size()),k,hm);
				last = qlc[k]; //node 1
				i = m->getNext(lc.data(),static_cast<enumerator>(lc.size()),i,hm);
				HandleType r = lc[i]; //edge 1
				adj_type const & rlc = m->LowConn(r);
				if(m->Count(rlc.data(),rlc.size(),hm)!=2) return false;
				k1 = m->getNext(rlc.data(),static_cast<enumerator>(rlc.size()),k1,hm);
				k2 = m->getNext(rlc.data(),static_cast<enumerator>(rlc.size()),k1,hm);
				if( first == rlc[k1] || first == rlc[k2] )
				{
					HandleType temp = first;
					first = last;
					last = temp;
				}
				else if( !(last == rlc[k1] || last == rlc[k2]) )
				{
					adj_type::size_type jt = i+1, jend = lc.size();
					while(jt < jend) if( !m->GetMarker(lc[jt],hm) ) //loop over edges
					{
						adj_type const & ilc = m->LowConn(lc[jt]);
						if( m->Count(ilc.data(),ilc.size(),hm) != 2 ) return false;
						k1 = ENUMUNDEF; 
						k1 = m->getNext(ilc.data(),static_cast<enumerator>(ilc.size()),k1,hm);
						k2 = m->getNext(ilc.data(),static_cast<enumerator>(ilc.size()),k1,hm);
						if( first == ilc[k1] || first == ilc[k2] )
						{
							HandleType temp = lc[i];
							lc[i] = lc[jt];
Kirill Terekhov's avatar
Kirill Terekhov committed
229
							lc[jt] = temp;
Kirill Terekhov's avatar
Kirill Terekhov committed
230 231 232 233 234 235 236 237 238
							temp = first;
							first = last;
							last = temp;
							break;
						}
						else if( last == ilc[k1] || last == ilc[k2] )
						{
							HandleType temp = lc[i];
							lc[i] = lc[jt];
Kirill Terekhov's avatar
Kirill Terekhov committed
239
							lc[jt] = temp;
Kirill Terekhov's avatar
Kirill Terekhov committed
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
							break;
						}
						++jt;
					}
					if( jt == jend ) return false; //no matching edge
				}
				adj_type::size_type it = i, iend = lc.size()-1;
				while(it != iend) if( !m->GetMarker(lc[it],hm) ) //loop over edges
				{
					adj_type const & ilc = m->LowConn(lc[it]);
					if(m->Count(ilc.data(),ilc.size(),hm)!=2) return false;
					k1 = ENUMUNDEF; 
					k1 = m->getNext(ilc.data(),static_cast<enumerator>(ilc.size()),k1,hm);
					k2 = m->getNext(ilc.data(),static_cast<enumerator>(ilc.size()),k1,hm);
					if( last == ilc[k1] ) 
					{
						last = ilc[k2];
						++it;
					}
					else if( last == ilc[k2] )
					{
						last = ilc[k1];
						++it;
					}
					else//search for the connected edge and swap with current
					{
						adj_type::size_type jt = it+1, jend = lc.size();
						while(jt < jend) if( !m->GetMarker(lc[jt],hm) ) //loop over edges
						{
							adj_type const & ilc = m->LowConn(lc[jt]);
							if(m->Count(ilc.data(),ilc.size(),hm)!=2) return false;
							k1 = ENUMUNDEF; 
							k1 = m->getNext(ilc.data(),static_cast<enumerator>(ilc.size()),k1,hm);
							k2 = m->getNext(ilc.data(),static_cast<enumerator>(ilc.size()),k1,hm);
							if( last == ilc[k1] || last == ilc[k2] )
							{
								HandleType temp = lc[it];
								lc[it] = lc[jt];
								lc[jt] = temp;
								break;
							}
						}
						if( jt == jend ) return false; //no matching edge
					}
				}
				//check that the loop is closed
				adj_type const & ilc = m->LowConn(lc[iend]);
				if(m->Count(ilc.data(),ilc.size(),hm)!=2) return false;
				k1 = ENUMUNDEF; 
				k1 = m->getNext(ilc.data(),static_cast<enumerator>(ilc.size()),k1,hm);
				k2 = m->getNext(ilc.data(),static_cast<enumerator>(ilc.size()),k1,hm);
Kirill Terekhov's avatar
Kirill Terekhov committed
291
				if( !( (ilc[k1] == last && ilc[k2] == first) || (ilc[k1] == first && ilc[k2] == last) ) )
Kirill Terekhov's avatar
Kirill Terekhov committed
292 293 294
					return false;
			}
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
295 296 297
		return false;
	}
	
Kirill Terekhov's avatar
Kirill Terekhov committed
298
	ElementArray<Node> Cell::getNodes() const
Kirill Terekhov's avatar
Kirill Terekhov committed
299
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
300 301 302
		assert(GetHandleElementType(GetHandle())==CELL);
		Mesh * m = GetMeshLink();
		if( !m->HideMarker() )
Kirill Terekhov's avatar
Kirill Terekhov committed
303
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
304 305
			adj_type const & hc = m->HighConn(GetHandle());
			return ElementArray<Node>(m,hc.data(),hc.data()+hc.size());
Kirill Terekhov's avatar
Kirill Terekhov committed
306
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
307 308
		else
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
309 310 311 312 313
			MarkerType hm = m->HideMarker();
			ElementArray<Node> aret(m);
			adj_type const & hc = m->HighConn(GetHandle());
			for(adj_type::size_type it = 0; it < hc.size(); ++it)
				if( !m->GetMarker(hc[it],hm) ) aret.push_back(hc[it]);
Kirill Terekhov's avatar
Kirill Terekhov committed
314 315 316
			return aret;
		}
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
317 318


Kirill Terekhov's avatar
Kirill Terekhov committed
319
	ElementArray<Node> Cell::getNodes(MarkerType mask, bool invert) const
Kirill Terekhov's avatar
Kirill Terekhov committed
320
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
321 322 323 324
		assert(GetHandleElementType(GetHandle())==CELL);
		Mesh * m = GetMeshLink();
		ElementArray<Node> aret(m);
		if( !m->HideMarker() )
Kirill Terekhov's avatar
Kirill Terekhov committed
325
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
326
			adj_type const & hc = m->HighConn(GetHandle());
Kirill Terekhov's avatar
Kirill Terekhov committed
327 328 329 330 331 332 333 334 335 336 337 338
			if( isPrivate(mask) )
			{
				for(adj_type::size_type it = 0; it < hc.size(); ++it)
					if( invert ^ m->GetPrivateMarker(hc[it],mask) ) 
						aret.push_back(hc[it]);
			}
			else
			{
				for(adj_type::size_type it = 0; it < hc.size(); ++it)
					if( invert ^ m->GetMarker(hc[it],mask) ) 
						aret.push_back(hc[it]);
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
339 340 341
		}
		else
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
342 343
			MarkerType hm = m->HideMarker();
			adj_type const & hc = m->HighConn(GetHandle());
Kirill Terekhov's avatar
Kirill Terekhov committed
344 345 346 347 348 349 350 351 352 353 354 355
			if( isPrivate(mask) )
			{
				for(adj_type::size_type it = 0; it < hc.size(); ++it)
					if( (invert ^ m->GetPrivateMarker(hc[it],mask)) && !m->GetMarker(hc[it],hm) ) 
						aret.push_back(hc[it]);
			}
			else
			{
				for(adj_type::size_type it = 0; it < hc.size(); ++it)
					if( (invert ^ m->GetMarker(hc[it],mask)) && !m->GetMarker(hc[it],hm) ) 
						aret.push_back(hc[it]);
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
356 357 358 359
		}
		return aret;
	}

Kirill Terekhov's avatar
Kirill Terekhov committed
360
	ElementArray<Edge> Cell::getEdges() const
Kirill Terekhov's avatar
Kirill Terekhov committed
361
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
362 363 364 365
		assert(GetHandleElementType(GetHandle())==CELL);
		Mesh * m = GetMeshLink();
		ElementArray<Edge> aret(m);
		if( !m->HideMarker() )
Kirill Terekhov's avatar
Kirill Terekhov committed
366
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
367
			if( Element::GetGeometricDimension(m->GetGeometricType(GetHandle())) == 2 ) // This cell is 2d face
Kirill Terekhov's avatar
Kirill Terekhov committed
368
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
369
				adj_type const & lc = m->LowConn(GetHandle());
Kirill Terekhov's avatar
Kirill Terekhov committed
370
				aret.reserve(lc.size());
Kirill Terekhov's avatar
Kirill Terekhov committed
371
				if( lc.size() < 1 ) return aret;
Kirill Terekhov's avatar
Kirill Terekhov committed
372 373
				HandleType q = lc[0]; //edge 0
				adj_type const & qlc = m->LowConn(q);
Kirill Terekhov's avatar
Kirill Terekhov committed
374 375 376
				if( qlc.size() > 0 ) aret.push_back(qlc[0]); //node 0
				if( qlc.size() > 1 ) aret.push_back(qlc[1]); //node 1
				if( lc.size() < 2 ) return aret;
Kirill Terekhov's avatar
Kirill Terekhov committed
377 378
				HandleType r = lc[1]; //edge 1
				adj_type const & rlc = m->LowConn(r);
Kirill Terekhov's avatar
Kirill Terekhov committed
379
				if( aret.size() == 2 && rlc.size() == 2 )
Kirill Terekhov's avatar
Kirill Terekhov committed
380
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
381 382 383 384 385 386
					if( aret.data()[0] == rlc[0] || aret.data()[0] == rlc[1] )
					{
						HandleType temp = aret.data()[0];
						aret.data()[0] = aret.data()[1];
						aret.data()[1] = temp;
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
387
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
388
				adj_type::size_type it = 1, iend = lc.size()-1;
Kirill Terekhov's avatar
Kirill Terekhov committed
389 390
				while(it < iend) //loop over edges
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
391 392 393 394 395
					adj_type const & ilc = m->LowConn(lc[it]);
					if( aret.atback() == ilc[0] ) 
						aret.push_back(ilc[1]);
					else 
						aret.push_back(ilc[0]);
Kirill Terekhov's avatar
Kirill Terekhov committed
396 397
					++it;
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
398 399 400
			}
			else
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
401
				MarkerType mrk = m->CreatePrivateMarker();
Kirill Terekhov's avatar
Kirill Terekhov committed
402 403
				adj_type const & lc = m->LowConn(GetHandle());
				for(adj_type::size_type it = 0; it < lc.size(); it++) //faces
Kirill Terekhov's avatar
Kirill Terekhov committed
404
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
405 406
					adj_type const & ilc = m->LowConn(lc[it]);
					for(adj_type::size_type jt = 0; jt < ilc.size(); jt++) //edges
Kirill Terekhov's avatar
Kirill Terekhov committed
407
						if( !m->GetPrivateMarker(ilc[jt],mrk))
Kirill Terekhov's avatar
Kirill Terekhov committed
408
						{
Kirill Terekhov's avatar
Kirill Terekhov committed
409
							aret.push_back(ilc[jt]);
Kirill Terekhov's avatar
Kirill Terekhov committed
410
							m->SetPrivateMarker(ilc[jt],mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
411
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
412
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
413
				for(ElementArray<Edge>::size_type it = 0; it < aret.size(); it++)
Kirill Terekhov's avatar
Kirill Terekhov committed
414 415
					m->RemPrivateMarker(aret.at(it),mrk);
				m->ReleasePrivateMarker(mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
416 417 418 419
			}
		}
		else
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
420 421
			MarkerType hm = m->HideMarker();
			if( Element::GetGeometricDimension(m->GetGeometricType(GetHandle())) == 2 ) // This cell is 2d face
Kirill Terekhov's avatar
Kirill Terekhov committed
422
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
423
				enumerator i = ENUMUNDEF, k = ENUMUNDEF, k1 = ENUMUNDEF, k2;
Kirill Terekhov's avatar
Kirill Terekhov committed
424
				adj_type const & lc = m->LowConn(GetHandle());
Kirill Terekhov's avatar
Kirill Terekhov committed
425
				aret.reserve(lc.size());
Kirill Terekhov's avatar
Kirill Terekhov committed
426
				i = m->getNext(lc.data(),static_cast<enumerator>(lc.size()),i,hm);
Kirill Terekhov's avatar
Kirill Terekhov committed
427
				if( i == lc.size() ) return aret;
Kirill Terekhov's avatar
Kirill Terekhov committed
428 429
				HandleType q = lc[i]; //edge 0
				adj_type const & qlc = m->LowConn(q);
Kirill Terekhov's avatar
Kirill Terekhov committed
430
				k = m->getNext(qlc.data(),static_cast<enumerator>(qlc.size()),k,hm);
Kirill Terekhov's avatar
Kirill Terekhov committed
431
				if( k != qlc.size() ) aret.push_back(qlc[k]); //node 0
Kirill Terekhov's avatar
Kirill Terekhov committed
432
				k = m->getNext(qlc.data(),static_cast<enumerator>(qlc.size()),k,hm);
Kirill Terekhov's avatar
Kirill Terekhov committed
433
				if( k != qlc.size() ) aret.push_back(qlc[k]); //node 1
Kirill Terekhov's avatar
Kirill Terekhov committed
434
				i = m->getNext(lc.data(),static_cast<enumerator>(lc.size()),i,hm);
Kirill Terekhov's avatar
Kirill Terekhov committed
435
				if( i == lc.size() ) return aret;
Kirill Terekhov's avatar
Kirill Terekhov committed
436 437
				HandleType r = lc[i]; //edge 1
				adj_type const & rlc = m->LowConn(r);
Kirill Terekhov's avatar
Kirill Terekhov committed
438 439
				k1 = m->getNext(rlc.data(),static_cast<enumerator>(rlc.size()),k1,hm);
				k2 = m->getNext(rlc.data(),static_cast<enumerator>(rlc.size()),k1,hm);
Kirill Terekhov's avatar
Kirill Terekhov committed
440
				if( k1 != rlc.size() && k2 != rlc.size() && aret.size() == 2 )
Kirill Terekhov's avatar
Kirill Terekhov committed
441
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
442 443 444 445 446 447
					if( aret.data()[0] == rlc[k1] || aret.data()[0] == rlc[k2] )
					{
						HandleType temp = aret.data()[0];
						aret.data()[0] = aret.data()[1];
						aret.data()[1] = temp;
					}
Kirill Terekhov's avatar
Kirill Terekhov committed
448
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
449 450
				adj_type::size_type it = 1, iend = lc.size()-1;
				while(it < iend) if( !m->GetMarker(lc[it],hm) ) //loop over edges
Kirill Terekhov's avatar
Kirill Terekhov committed
451
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
452
					adj_type const & ilc = m->LowConn(lc[it]);
Kirill Terekhov's avatar
Kirill Terekhov committed
453 454 455
					k1 = ENUMUNDEF; 
					k1 = m->getNext(ilc.data(),static_cast<enumerator>(ilc.size()),k1,hm);
					k2 = m->getNext(ilc.data(),static_cast<enumerator>(ilc.size()),k1,hm);
Kirill Terekhov's avatar
Kirill Terekhov committed
456 457 458 459
					if( aret.atback() == ilc[k1] ) 
						aret.push_back(ilc[k2]);
					else 
						aret.push_back(ilc[k1]);
Kirill Terekhov's avatar
Kirill Terekhov committed
460 461 462 463 464
					++it;
				}
			}
			else
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
465
				MarkerType mrk = m->CreatePrivateMarker();
Kirill Terekhov's avatar
Kirill Terekhov committed
466 467
				adj_type const & lc = m->LowConn(GetHandle());
				for(adj_type::size_type it = 0; it < lc.size(); it++) if( !m->GetMarker(lc[it],hm) ) //faces
Kirill Terekhov's avatar
Kirill Terekhov committed
468
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
469 470
					adj_type const & ilc = m->LowConn(lc[it]);
					for(adj_type::size_type jt = 0; jt < ilc.size(); jt++) if( !m->GetMarker(ilc[jt],hm) )//edges
Kirill Terekhov's avatar
Kirill Terekhov committed
471
						if( !m->GetPrivateMarker(ilc[jt],mrk))
Kirill Terekhov's avatar
Kirill Terekhov committed
472
						{
Kirill Terekhov's avatar
Kirill Terekhov committed
473
							aret.push_back(ilc[jt]);
Kirill Terekhov's avatar
Kirill Terekhov committed
474
							m->SetPrivateMarker(ilc[jt],mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
475
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
476
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
477
				for(ElementArray<Edge>::size_type it = 0; it < aret.size(); it++)
Kirill Terekhov's avatar
Kirill Terekhov committed
478 479
					m->RemPrivateMarker(aret.at(it),mrk);
				m->ReleasePrivateMarker(mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
480 481 482 483
			}
		}
		return aret;
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
484 485


Kirill Terekhov's avatar
Kirill Terekhov committed
486
	ElementArray<Edge> Cell::getEdges(MarkerType mask, bool invert) const
Kirill Terekhov's avatar
Kirill Terekhov committed
487
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
488 489 490
		assert(GetHandleElementType(GetHandle())==CELL);
		Mesh * m = GetMeshLink();
		ElementArray<Edge> aret(m);
Kirill Terekhov's avatar
Kirill Terekhov committed
491
		if( isPrivate(mask) )
Kirill Terekhov's avatar
Kirill Terekhov committed
492
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
493
			if( !m->HideMarker() )
Kirill Terekhov's avatar
Kirill Terekhov committed
494
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
495
				if( Element::GetGeometricDimension(m->GetGeometricType(GetHandle())) == 2 ) // This cell is 2d face
Kirill Terekhov's avatar
Kirill Terekhov committed
496
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
497 498 499 500 501 502 503 504 505 506 507 508
					adj_type const & lc = m->LowConn(GetHandle());
					aret.reserve(lc.size());
					HandleType last, first;
					HandleType q = lc[0]; //edge 0
					adj_type const & qlc = m->LowConn(q);
					if( invert ^ m->GetPrivateMarker(qlc[0],mask) ) aret.push_back(qlc[0]); //node 0
					if( invert ^ m->GetPrivateMarker(qlc[1],mask) ) aret.push_back(qlc[1]); //node 1
					first = qlc[0];
					last  = qlc[1];
					HandleType r = lc[1]; //edge 1
					adj_type const & rlc = m->LowConn(r);
					if( first == rlc[0] || first == rlc[1] )
Kirill Terekhov's avatar
Kirill Terekhov committed
509
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
						last = first;
						if( aret.size() > 1 )
						{
							HandleType temp = aret.data()[0];
							aret.data()[0] = aret.data()[1];
							aret.data()[1] = temp;
						}
					}
					adj_type::size_type it = 1, iend = lc.size()-1;
					while(it < iend) //loop over edges
					{
						adj_type const & ilc = m->LowConn(lc[it]);
						if( last == ilc[0] ) last = ilc[1];
						else last = ilc[0];
						if( invert ^ m->GetPrivateMarker(last,mask) ) 
							aret.push_back(last);
						++it;
Kirill Terekhov's avatar
Kirill Terekhov committed
527 528
					}
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
529
				else
Kirill Terekhov's avatar
Kirill Terekhov committed
530
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
					MarkerType mrk = m->CreatePrivateMarker();
					adj_type const & lc = m->LowConn(GetHandle());
					for(adj_type::size_type it = 0; it < lc.size(); it++) //faces
					{
						adj_type const & ilc = m->LowConn(lc[it]);
						for(adj_type::size_type jt = 0; jt != ilc.size(); jt++) //edges
							if( (invert ^ m->GetPrivateMarker(ilc[jt],mask)) && !m->GetPrivateMarker(ilc[jt],mrk))
							{
								aret.push_back(ilc[jt]);
								m->SetPrivateMarker(ilc[jt],mrk);
							}
					}
					for(ElementArray<Edge>::size_type it = 0; it != aret.size(); it++)
						m->RemPrivateMarker(aret.at(it),mrk);
					m->ReleasePrivateMarker(mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
546
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
547 548 549
			}
			else
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
550 551
				MarkerType hm = GetMeshLink()->HideMarker();
				if( Element::GetGeometricDimension(m->GetGeometricType(GetHandle())) == 2 ) // This cell is 2d face
Kirill Terekhov's avatar
Kirill Terekhov committed
552
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
					enumerator i = ENUMUNDEF, k = ENUMUNDEF, k1 = ENUMUNDEF, k2;
					HandleType last, first;
					adj_type const & lc = m->LowConn(GetHandle());
					aret.reserve(lc.size());
					i = m->getNext(lc.data(),static_cast<enumerator>(lc.size()),i,hm);
					HandleType q = lc[i]; //edge 0
					adj_type const & qlc = m->LowConn(q);
					k = m->getNext(qlc.data(),static_cast<enumerator>(qlc.size()),k,hm);
					if( invert ^ m->GetPrivateMarker(qlc[k],mask) ) aret.push_back(qlc[k]); //node 0
					first = qlc[k];
					k = m->getNext(qlc.data(),static_cast<enumerator>(qlc.size()),k,hm);
					if( invert ^ m->GetPrivateMarker(qlc[k],mask) ) aret.push_back(qlc[k]); //node 1
					last = qlc[k];
					i = m->getNext(lc.data(),static_cast<enumerator>(lc.size()),i,hm);
					HandleType r = lc[i]; //edge 1
					adj_type const & rlc = m->LowConn(r);
					k1 = m->getNext(rlc.data(),static_cast<enumerator>(rlc.size()),k1,hm);
					k2 = m->getNext(rlc.data(),static_cast<enumerator>(rlc.size()),k1,hm);
					if( first == rlc[k1] || first == rlc[k2] )
					{
						last = first;
						if( aret.size() > 1 )
Kirill Terekhov's avatar
Kirill Terekhov committed
575
						{
Kirill Terekhov's avatar
Kirill Terekhov committed
576 577 578
							HandleType temp = aret.data()[0];
							aret.data()[0] = aret.data()[1];
							aret.data()[1] = temp;
Kirill Terekhov's avatar
Kirill Terekhov committed
579
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
					}
					adj_type::size_type it = 1, iend = lc.size()-1;
					while(it != iend) if( !m->GetMarker(lc[it],hm) ) //loop over edges
					{
						adj_type const & ilc = m->LowConn(lc[it]);
						k1 = ENUMUNDEF; 
						k1 = m->getNext(ilc.data(),static_cast<enumerator>(ilc.size()),k1,hm);
						k2 = m->getNext(ilc.data(),static_cast<enumerator>(ilc.size()),k1,hm);
						if( last == ilc[k1] ) 
							last = ilc[k2];
						else last = ilc[k1];
						if( invert ^ m->GetPrivateMarker(last,mask) ) aret.push_back(last);
						++it;
					}
				}
				else
				{
					MarkerType mrk = m->CreatePrivateMarker();
					adj_type const & lc = m->LowConn(GetHandle());
					for(adj_type::size_type it = 0; it < lc.size(); it++) if( !m->GetMarker(lc[it],hm) ) //faces
					{
						adj_type const & ilc = m->LowConn(lc[it]);
						for(adj_type::size_type jt = 0; jt < ilc.size(); jt++) if( !m->GetMarker(ilc[jt],hm) )//edges
							if( (invert ^ m->GetPrivateMarker(ilc[jt],mask)) && !m->GetPrivateMarker(ilc[jt],mrk))
							{
								aret.push_back(ilc[jt]);
								m->SetPrivateMarker(ilc[jt],mrk);
							}
					}
					for(ElementArray<Edge>::size_type it = 0; it < aret.size(); it++) 
						m->RemPrivateMarker(aret.at(it),mrk);
					m->ReleasePrivateMarker(mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
612
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
613 614 615 616
			}
		}
		else
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
617
			if( !m->HideMarker() )
Kirill Terekhov's avatar
Kirill Terekhov committed
618
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
619
				if( Element::GetGeometricDimension(m->GetGeometricType(GetHandle())) == 2 ) // This cell is 2d face
Kirill Terekhov's avatar
Kirill Terekhov committed
620
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
621 622 623 624 625 626 627 628 629 630 631 632
					adj_type const & lc = m->LowConn(GetHandle());
					aret.reserve(lc.size());
					HandleType last, first;
					HandleType q = lc[0]; //edge 0
					adj_type const & qlc = m->LowConn(q);
					if( invert ^ m->GetMarker(qlc[0],mask) ) aret.push_back(qlc[0]); //node 0
					if( invert ^ m->GetMarker(qlc[1],mask) ) aret.push_back(qlc[1]); //node 1
					first = qlc[0];
					last  = qlc[1];
					HandleType r = lc[1]; //edge 1
					adj_type const & rlc = m->LowConn(r);
					if( first == rlc[0] || first == rlc[1] )
Kirill Terekhov's avatar
Kirill Terekhov committed
633
					{
Kirill Terekhov's avatar
Kirill Terekhov committed
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
						last = first;
						if( aret.size() > 1 )
						{
							HandleType temp = aret.data()[0];
							aret.data()[0] = aret.data()[1];
							aret.data()[1] = temp;
						}
					}
					adj_type::size_type it = 1, iend = lc.size()-1;
					while(it < iend) //loop over edges
					{
						adj_type const & ilc = m->LowConn(lc[it]);
						if( last == ilc[0] ) last = ilc[1];
						else last = ilc[0];
						if( invert ^ m->GetMarker(last,mask) ) 
							aret.push_back(last);
						++it;
Kirill Terekhov's avatar
Kirill Terekhov committed
651 652
					}
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
653
				else
Kirill Terekhov's avatar
Kirill Terekhov committed
654
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
					MarkerType mrk = m->CreatePrivateMarker();
					adj_type const & lc = m->LowConn(GetHandle());
					for(adj_type::size_type it = 0; it < lc.size(); it++) //faces
					{
						adj_type const & ilc = m->LowConn(lc[it]);
						for(adj_type::size_type jt = 0; jt != ilc.size(); jt++) //edges
							if( (invert ^ m->GetMarker(ilc[jt],mask)) && !m->GetPrivateMarker(ilc[jt],mrk))
							{
								aret.push_back(ilc[jt]);
								m->SetPrivateMarker(ilc[jt],mrk);
							}
					}
					for(ElementArray<Edge>::size_type it = 0; it != aret.size(); it++)
						m->RemPrivateMarker(aret.at(it),mrk);
					m->ReleasePrivateMarker(mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
670 671 672 673
				}
			}
			else
			{
Kirill Terekhov's avatar
Kirill Terekhov committed
674 675
				MarkerType hm = GetMeshLink()->HideMarker();
				if( Element::GetGeometricDimension(m->GetGeometricType(GetHandle())) == 2 ) // This cell is 2d face
Kirill Terekhov's avatar
Kirill Terekhov committed
676
				{
Kirill Terekhov's avatar
Kirill Terekhov committed
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
					enumerator i = ENUMUNDEF, k = ENUMUNDEF, k1 = ENUMUNDEF, k2;
					HandleType last, first;
					adj_type const & lc = m->LowConn(GetHandle());
					aret.reserve(lc.size());
					i = m->getNext(lc.data(),static_cast<enumerator>(lc.size()),i,hm);
					HandleType q = lc[i]; //edge 0
					adj_type const & qlc = m->LowConn(q);
					k = m->getNext(qlc.data(),static_cast<enumerator>(qlc.size()),k,hm);
					if( invert ^ m->GetMarker(qlc[k],mask) ) aret.push_back(qlc[k]); //node 0
					first = qlc[k];
					k = m->getNext(qlc.data(),static_cast<enumerator>(qlc.size()),k,hm);
					if( invert ^ m->GetMarker(qlc[k],mask) ) aret.push_back(qlc[k]); //node 1
					last = qlc[k];
					i = m->getNext(lc.data(),static_cast<enumerator>(lc.size()),i,hm);
					HandleType r = lc[i]; //edge 1
					adj_type const & rlc = m->LowConn(r);
					k1 = m->getNext(rlc.data(),static_cast<enumerator>(rlc.size()),k1,hm);
					k2 = m->getNext(rlc.data(),static_cast<enumerator>(rlc.size()),k1,hm);
					if( first == rlc[k1] || first == rlc[k2] )
					{
						last = first;
						if( aret.size() > 1 )
Kirill Terekhov's avatar
Kirill Terekhov committed
699
						{
Kirill Terekhov's avatar
Kirill Terekhov committed
700 701 702
							HandleType temp = aret.data()[0];
							aret.data()[0] = aret.data()[1];
							aret.data()[1] = temp;
Kirill Terekhov's avatar
Kirill Terekhov committed
703
						}
Kirill Terekhov's avatar
Kirill Terekhov committed
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
					}
					adj_type::size_type it = 1, iend = lc.size()-1;
					while(it != iend) if( !m->GetMarker(lc[it],hm) ) //loop over edges
					{
						adj_type const & ilc = m->LowConn(lc[it]);
						k1 = ENUMUNDEF; 
						k1 = m->getNext(ilc.data(),static_cast<enumerator>(ilc.size()),k1,hm);
						k2 = m->getNext(ilc.data(),static_cast<enumerator>(ilc.size()),k1,hm);
						if( last == ilc[k1] ) 
							last = ilc[k2];
						else last = ilc[k1];
						if( invert ^ m->GetMarker(last,mask) ) aret.push_back(last);
						++it;
					}
				}
				else
				{
					MarkerType mrk = m->CreatePrivateMarker();
					adj_type const & lc = m->LowConn(GetHandle());
					for(adj_type::size_type it = 0; it < lc.size(); it++) if( !m->GetMarker(lc[it],hm) ) //faces
					{
						adj_type const & ilc = m->LowConn(lc[it]);
						for(adj_type::size_type jt = 0; jt < ilc.size(); jt++) if( !m->GetMarker(ilc[jt],hm) )//edges
							if( (invert ^ m->GetMarker(ilc[jt],mask)) && !m->GetPrivateMarker(ilc[jt],mrk))
							{
								aret.push_back(ilc[jt]);
								m->SetPrivateMarker(ilc[jt],mrk);
							}
					}
					for(ElementArray<Edge>::size_type it = 0; it < aret.size(); it++) 
						m->RemPrivateMarker(aret.at(it),mrk);
					m->ReleasePrivateMarker(mrk);
Kirill Terekhov's avatar
Kirill Terekhov committed
736
				}
Kirill Terekhov's avatar
Kirill Terekhov committed
737 738 739 740 741
			}
		}
		return aret;
	}

Kirill Terekhov's avatar
Kirill Terekhov committed
742
	ElementArray<Face> Cell::getFaces() const
Kirill Terekhov's avatar
Kirill Terekhov committed
743
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
744 745 746
		assert(GetHandleElementType(GetHandle())==CELL);
		Mesh * m = GetMeshLink();
		if( !m->HideMarker() )
Kirill Terekhov's avatar
Kirill Terekhov committed
747
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
748 749
			adj_type const & lc = m->LowConn(GetHandle());
			return ElementArray<Face>(m,lc.data(),lc.data()+lc.size());
Kirill Terekhov's avatar
Kirill Terekhov committed
750
		}
Kirill Terekhov's avatar
Kirill Terekhov committed
751 752
		else
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
753 754 755 756 757
			MarkerType hm = m->HideMarker();
			ElementArray<Face> aret(m);
			adj_type & lc = m->LowConn(GetHandle());
			for(adj_type::size_type it = 0; it < lc.size(); ++it)
				if( !m->GetMarker(lc[it],hm) ) aret.push_back(lc[it]);
Kirill Terekhov's avatar
Kirill Terekhov committed
758 759 760
			return aret;
		}
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
761 762


Kirill Terekhov's avatar
Kirill Terekhov committed
763
	ElementArray<Face> Cell::getFaces(MarkerType mask, bool invert) const
Kirill Terekhov's avatar
Kirill Terekhov committed
764
	{
Kirill Terekhov's avatar
Kirill Terekhov committed
765 766 767 768
		assert(GetHandleElementType(GetHandle())==CELL);
		Mesh * m = GetMeshLink();
		ElementArray<Face> aret(m);
		if( !m->HideMarker() )
Kirill Terekhov's avatar
Kirill Terekhov committed
769
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
770
			adj_type const & lc = m->LowConn(GetHandle());
Kirill Terekhov's avatar
Kirill Terekhov committed
771 772 773 774 775 776 777 778 779 780
			if( isPrivate(mask) )
			{
				for(adj_type::size_type it = 0; it < lc.size(); ++it)
					if( invert ^ m->GetPrivateMarker(lc[it],mask) ) aret.push_back(lc[it]);
			}
			else
			{
				for(adj_type::size_type it = 0; it < lc.size(); ++it)
					if( invert ^ m->GetMarker(lc[it],mask) ) aret.push_back(lc[it]);
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
781 782 783
		}
		else
		{
Kirill Terekhov's avatar
Kirill Terekhov committed
784 785
			MarkerType hm = m->HideMarker();
			adj_type const & lc = m->LowConn(GetHandle());
Kirill Terekhov's avatar
Kirill Terekhov committed
786 787 788 789 790 791 792 793 794 795 796 797
			if( isPrivate(mask) )
			{
				for(adj_type::size_type it = 0; it < lc.size(); ++it)
					if( (invert ^ m->GetPrivateMarker(lc[it],mask)) && !m->GetMarker(lc[it],hm) ) 
						aret.push_back(lc[it]);
			}
			else
			{
				for(adj_type::size_type it = 0; it < lc.size(); ++it)
					if( (invert ^ m->GetMarker(lc[it],mask)) && !m->GetMarker(lc[it],hm) ) 
						aret.push_back(lc[it]);
			}
Kirill Terekhov's avatar
Kirill Terekhov committed
798 799 800
		}
		return aret;
	}
Kirill Terekhov's avatar
Kirill Terekhov committed
801 802
}
#endif