PropagationEngine.cpp 28.5 KB
Newer Older
1 2 3 4
#include <ITAPropagationPathSim\CombinedModel\PropagationEngine.h>
#include <ITAPropagationPathSim\CombinedModel\ImageConstructor.h>
#include <ITAPropagationPathSim\CombinedModel\DiffractionLocator.h>
#include <ITAPropagationPathSim\CombinedModel\ReflectionLocator.h>
5 6 7 8 9 10 11 12

//Typedefs
typedef OpenMesh::PolyMesh_ArrayKernelT<> CITAMesh;
struct CITAMeshPtr
{
	CITAMesh* pMesh;
};

13
ITAPropagationPathSim::CombinedModel::CPathEngine::CPathEngine(shared_ptr<const ITAGeo::Halfedge::CMeshModelList> pMeshModelList, bool bIgnoreIlluminatedRegionDiffraction /* =false*/)
14 15 16
{
	m_pMeshModelList = pMeshModelList;

17 18
	m_pIgnoreIlluminatedRegionDiffraction = make_unique<bool>(bIgnoreIlluminatedRegionDiffraction);
	
19 20 21 22 23 24 25 26 27 28 29
	for (int i = 0; i < m_pMeshModelList->GetNumMeshes(); i++)
	{
		
		// Calculate mesh normals
		CITAMesh* pMesh = m_pMeshModelList->at(i)->GetMesh()->pMesh;
		pMesh->request_face_normals();
		pMesh->update_face_normals();
		pMesh->request_halfedge_normals();
		pMesh->request_vertex_normals();
	}

30 31 32 33
	//Get all propagation shapes of the mesh model list
	ConstructPropagationShapes();

	//Create a visibility map 
34 35 36
	CreateVisibilityMap();
}

37

38
void ITAPropagationPathSim::CombinedModel::CPathEngine::ApplyEmitter(shared_ptr<ITAGeo::CEmitter> pEmitter, const int iMaxDiffractions, const int iMaxReflections, const int iMaxCombinedOrder)
39 40 41 42 43 44 45
{
	//Set emitter
	m_pEmitter = pEmitter;

	//Set orders
	m_pMaxDiffractionOrder = make_unique<const int>(iMaxDiffractions);
	m_pMaxReflectionOrder = make_unique<const int>(iMaxReflections);
46 47 48 49 50 51 52 53 54 55 56 57
	m_pMaxCombinedOrder = make_unique<const int>(iMaxCombinedOrder);

	//Create propagation tree
	CreatePropagationTree();

	//Construct image sources
	ImageConstruction::ConstructImageSources(m_pEmitter, m_vpPropagationTree);

	//Construct image edges
	ImageConstruction::ConstructImageEdges(m_pEmitter, m_vpPropagationTree);
}

58
void ITAPropagationPathSim::CombinedModel::CPathEngine::ApplySensor(shared_ptr<ITAGeo::CSensor> pSensor)
59 60 61 62 63 64
{
	//Set sensor
	m_pSensor = pSensor;

	//Create propagation lists with each one containing a possible propagation path candidate
	CreatePropagationLists();
65

66 67
}

68
void ITAPropagationPathSim::CombinedModel::CPathEngine::ConstructPropagationPaths(ITAGeo::CPropagationPathList& oPaths)
69
{
70
	int iNullCounter = 0, iReferenceCounter = 0;
71 72 73
	for (auto& pPropagationList : m_vpPropagationLists)
	{
		if (pPropagationList == nullptr)
74 75 76
			iNullCounter++;
		else
			iReferenceCounter++;
77
	}
78
	std::cout << iReferenceCounter << " references and " << iNullCounter << " times nullptr at start\n";
79

80 81 82
	//First, construct the aperture points
	Diffraction::ConstructAperturePoints(m_pEmitter, m_pSensor, m_vpPropagationLists);

83
	iNullCounter = 0, iReferenceCounter = 0;
84 85 86
	for (auto& pPropagationList : m_vpPropagationLists)
	{
		if (pPropagationList == nullptr)
87 88 89
			iNullCounter++;
		else
			iReferenceCounter++;
90
	}
91
	std::cout << iReferenceCounter << " references and " << iNullCounter << " times nullptr after aperture points calculated\n";
92

93 94 95 96
	//Thereafter, with the constructed aperture points, construct the image apertures as secondary image sources 
	//for the faces after each edge
	ImageConstruction::ConstructImageApertures(m_vpPropagationLists);

97
	iNullCounter = 0, iReferenceCounter = 0;
98 99 100
	for (auto& pPropagationList : m_vpPropagationLists)
	{
		if (pPropagationList == nullptr)
101 102 103
			iNullCounter++;
		else
			iReferenceCounter++;
104
	}
105
	std::cout << iReferenceCounter << " references and " << iNullCounter << " times nullptr after image aperture calculated\n";
106

107
	//Construct the intersection points of the reflections
108 109
	Reflection::ConstructPointsOfReflection(m_vpPropagationLists, m_pSensor);

110
	iNullCounter = 0, iReferenceCounter = 0;
111 112 113
	for (auto& pPropagationList : m_vpPropagationLists)
	{
		if (pPropagationList == nullptr)
114 115 116
			iNullCounter++;
		else
			iReferenceCounter++;
117
	}
118
	std::cout << iReferenceCounter << " references and " << iNullCounter << " times nullptr after points of reflection calculated\n";
119 120 121 122 123 124

	//Convert pPropagationShapes to pPropagationAnchors
	ConvertShapeListsToPropagationPaths(oPaths);
}


125
void ITAPropagationPathSim::CombinedModel::CPathEngine::ConvertShapeListsToPropagationPaths(ITAGeo::CPropagationPathList& oPathsOut)
126 127 128 129 130 131
{
	//Add direct path
	CPropagationPath oDirectPath;
	oDirectPath.push_back(m_pEmitter);
	oDirectPath.push_back(m_pSensor);
	oPathsOut.push_back(oDirectPath);
132

133 134 135 136 137 138 139 140 141 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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
	for (auto& pShapeStart : m_vpPropagationLists)
	{
		if (pShapeStart == nullptr)
			continue;

		//Create path
		CPropagationPath oPath;

		//The path always starts with the emitter
		oPath.push_back(m_pEmitter);

		CPropagationShapeShared pShape = pShapeStart;
		while (pShape != nullptr)
		{
			switch (pShape->iShapeType)
			{
			case CPropagationShape::FACE:
			{
				CPropagationFaceShared pFace = static_pointer_cast<CPropagationFace>(pShape);

				//Create specular reflection anchor
				shared_ptr<CSpecularReflection> pReflectionAnchor = make_shared<CSpecularReflection>();
				pReflectionAnchor->v3InteractionPoint = *pFace->v3InteractionPoint;

				oPath.push_back(pReflectionAnchor);
				break;
			}
			case CPropagationShape::EDGE:
			{
				CPropagationEdgeShared pEdge = static_pointer_cast<CPropagationEdge>(pShape);

				//Create specular reflection anchor
				shared_ptr<CITADiffractionOuterWedgeAperture> pDiffractionAnchor = make_shared<CITADiffractionOuterWedgeAperture>();
				pDiffractionAnchor->v3InteractionPoint = *pEdge->v3InteractionPoint;
				pDiffractionAnchor->v3MainWedgeFaceNormal = *pEdge->v3MainFaceNormal;
				pDiffractionAnchor->v3OppositeWedgeFaceNormal = *pEdge->v3OppositeFaceNormal;
				pDiffractionAnchor->v3VertextStart = *pEdge->v3FromVertex;
				pDiffractionAnchor->v3VertextEnd = *pEdge->v3ToVertex;


				oPath.push_back(pDiffractionAnchor);

				break;
			}
			}

			pShape = pShape->pChild;

			
		}

		//The path always ends with the sensor
		oPath.push_back(m_pSensor);

		//Add current path to path list
		oPathsOut.push_back(oPath);
	}
	
191 192 193
}


194
void ITAPropagationPathSim::CombinedModel::CPathEngine::CreatePropagationTree()
195
{
196 197 198 199
	//Set initial values for number of diffractions/reflections

	for (const auto& pPropagationShape : m_vpPropagationShapes)
	{
200 201
		// If m_pMaxCombinedOrder order is zero, no paths are added
		if (*m_pMaxCombinedOrder <= 0)
202 203 204 205 206 207 208
			break;

		switch (pPropagationShape->iShapeType)
		{
		case CPropagationShape::FACE:
		{
			//Only add reflection paths if iMaxReflections is at least one
209
			if (*m_pMaxReflectionOrder >= 1 )
210
			{
211
				CPropagationFaceShared pFaceCopy = make_shared<CPropagationFace>();
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
				pFaceCopy->CopyFrom(*static_pointer_cast<CPropagationFace>(pPropagationShape));

				bool bCanIlluminated = CanPointIlluminateFace(m_pEmitter->v3InteractionPoint, *pFaceCopy->sMeshModelName, pFaceCopy->hFace);
				if (bCanIlluminated)
				{
					//Add face copy to propagation tree
					m_vpPropagationTree.push_back(pFaceCopy);

					//Add further propagation shapes if higher order than one is allowed
					if (*m_pMaxCombinedOrder > 1)
					{
						for (const auto& pPropagationShapeChild : m_mvpShapeVisibilityMap[pPropagationShape])
						{
							CPropagationShapeShared vpShapeChildCopy;
							RecursiveAddChildrenToTree(pPropagationShapeChild, vpShapeChildCopy, 1, 0, 1);

							if (vpShapeChildCopy != nullptr)
							{
								vpShapeChildCopy->pParent = pFaceCopy; 
								pFaceCopy->vpChildren.push_back(vpShapeChildCopy);
							}
						}
					}
				}
			}
			break;
		}
		case CPropagationShape::EDGE:
		{
			//Only add reflection paths if iMaxDiffractions is at least one
242
			if (*m_pMaxCombinedOrder >= 1)
243
			{
244
				CPropagationEdgeShared pEdgeCopy = make_shared<CPropagationEdge>();
245 246
				pEdgeCopy->CopyFrom(*static_pointer_cast<CPropagationEdge>(pPropagationShape));

247 248 249
				//TODO: Implement option to choose whether to test intersections or not (last optional boolean value) 
				bool bCanIlluminated = CanPointIlluminateEdge(m_pEmitter->v3InteractionPoint, pEdgeCopy);

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
				if (bCanIlluminated)
				{
					//Add edge copy to propagation tree
					m_vpPropagationTree.push_back(pEdgeCopy);

					//Add further propagation shapes if higher order than one is allowed
					if (*m_pMaxCombinedOrder > 1)
					{
						for (const auto& pPropagationShapeChild : m_mvpShapeVisibilityMap[pPropagationShape])
						{
							CPropagationShapeShared vpShapeChildCopy;
							RecursiveAddChildrenToTree(pPropagationShapeChild, vpShapeChildCopy, 0, 1, 1);

							if (vpShapeChildCopy != nullptr)
							{
								vpShapeChildCopy->pParent = pEdgeCopy;
								pEdgeCopy->vpChildren.push_back(vpShapeChildCopy);
							}
						}
					}
				}
			}
			break;
		}
		}
	}
}

278
void ITAPropagationPathSim::CombinedModel::CPathEngine::CreatePropagationLists()
279 280 281 282 283 284 285
{
	for (auto& pPropagationShape : m_vpPropagationTree)
	{
		RecursiveAddShapesToPropagationLists(pPropagationShape);
	}
}

286
void ITAPropagationPathSim::CombinedModel::CPathEngine::RecursiveAddShapesToPropagationLists(shared_ptr<CPropagationShape>& pPropagationShapeIn)
287
{
288 289 290
	if (pPropagationShapeIn == nullptr)
		return;

291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
	//In this if case, pPropagationShapeIn is eventually tested for illuminability for the sensor 
	//and its branches are followed up until the first branch(illuminable by the emitter)
	if (*pPropagationShapeIn->pIsIlluminableBySensor != Tristate::False)
	{
		CPropagationShapeShared pShape;

		switch (pPropagationShapeIn->iShapeType)
		{
		case CPropagationShape::FACE:
		{
			auto pFace = make_shared<CPropagationFace>();
			pFace->CopyFrom(*static_pointer_cast<CPropagationFace>(pPropagationShapeIn));

			//If the face has no valid image source, do not follow up its branch and return immediately
			if (pFace->pHasValidImageSource != nullptr && *pFace->pHasValidImageSource == false)
				return;

			//If the illuminability is not found out yet, check for illuminability and break switch without setting pShape if shape is not illuminable
			if (*pPropagationShapeIn->pIsIlluminableBySensor == Tristate::Undefined)
			{
				if (CanPointIlluminateFace(m_pSensor->v3InteractionPoint, *pFace->sMeshModelName, pFace->hFace))
				{
					*pPropagationShapeIn->pIsIlluminableBySensor = Tristate::True;
				}
				else
				{
					*pPropagationShapeIn->pIsIlluminableBySensor = Tristate::False;
					break;
				}
			}

			pShape = pFace;

			break;
		}
		case CPropagationShape::EDGE:
		{
			auto pEdge = make_shared<CPropagationEdge>();
			pEdge->CopyFrom(*static_pointer_cast<CPropagationEdge>(pPropagationShapeIn));

			//If the edge has no valid image edge, do not follow up its branch and return immediately
			if (pEdge->pHasValidImageEdge != nullptr && *pEdge->pHasValidImageEdge == false)
				return;

			//If the illuminability is not found out yet, check for illuminability
			if (*pPropagationShapeIn->pIsIlluminableBySensor == Tristate::Undefined)
			{
338 339
				//TODO: Implement option to choose whether to test intersections or not (last optional boolean value) 
				if(CanPointIlluminateEdge(m_pSensor->v3InteractionPoint,pEdge))
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
				{
					*pPropagationShapeIn->pIsIlluminableBySensor = Tristate::True;
				}
				else
				{
					*pPropagationShapeIn->pIsIlluminableBySensor = Tristate::False;
					break;
				}
			}

			pShape = pEdge;

			break;

		}
		}

		//Only add shapes to propagation list, if it is illuminable and is therefore set above
		if (pShape != nullptr)
		{
			//Add parents up until the start point is reached 
361
			while (CPropagationShapeShared pParent = pShape->pParent.lock())
362
			{
363
				switch (pParent->iShapeType)
364 365 366 367
				{
				case CPropagationShape::FACE:
				{
					CPropagationFaceShared pFaceParent = make_shared<CPropagationFace>();
368 369
					pFaceParent->CopyFrom(*static_pointer_cast<CPropagationFace>(pParent));
					pParent = pFaceParent;
370 371 372 373 374
					break;
				}
				case CPropagationShape::EDGE:
				{
					CPropagationEdgeShared pEdgeParent = make_shared<CPropagationEdge>();
375 376
					pEdgeParent->CopyFrom(*static_pointer_cast<CPropagationEdge>(pParent));
					pParent = pEdgeParent;
377 378 379
					break;
				}
				}
380 381 382 383 384

				//Set parent and child member variables to each other
				pShape->pParent = pParent;
				pParent->pChild = pShape;

385
				//Set copied parent to new shape for next iteration
386
				pShape = pParent;
387 388 389 390 391 392 393 394 395 396 397 398 399 400
			}

			//Add shape to list
			m_vpPropagationLists.push_back(pShape);
		}
	}

	//Add further shapes to propagation lists
	for (auto& pChild : pPropagationShapeIn->vpChildren)
	{
		RecursiveAddShapesToPropagationLists(pChild);
	}
}

401
void ITAPropagationPathSim::CombinedModel::CPathEngine::RecursiveAddChildrenToTree(const CPropagationShapeShared & pPropagationShapeChildIn, CPropagationShapeShared& vpShapeChildCopyOut, int iReflectionOrder, int iDiffractionOrder, int iCombinedOrder)
402 403 404 405 406 407 408 409 410 411
{

	switch (pPropagationShapeChildIn->iShapeType)
	{
	case CPropagationShape::FACE:
	{
		//Only add child if max reflection order is not reached yet
		if (iReflectionOrder < *m_pMaxReflectionOrder)
		{
			//Cast shape as face and copy shape
412
			CPropagationFaceShared pFaceChildCopy = make_shared<CPropagationFace>();
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
			pFaceChildCopy->CopyFrom(*static_pointer_cast<CPropagationFace>(pPropagationShapeChildIn));

			//Set output parameter
			vpShapeChildCopyOut = pFaceChildCopy;

			//Add further children, if max order is not reached yet
			if (iCombinedOrder + 1 < *m_pMaxCombinedOrder)
			{
				for (const auto& pShapeChildChild : m_mvpShapeVisibilityMap[pPropagationShapeChildIn])
				{
					CPropagationShapeShared vpShapeChildChildCopy;
					RecursiveAddChildrenToTree(pShapeChildChild, vpShapeChildChildCopy, iReflectionOrder + 1, iDiffractionOrder, iCombinedOrder + 1);

					if (vpShapeChildChildCopy != nullptr)
					{
						vpShapeChildChildCopy->pParent = vpShapeChildCopyOut;
						vpShapeChildCopyOut->vpChildren.push_back(vpShapeChildChildCopy);
					}
				}
			}
		}
		break;
	}
	case CPropagationShape::EDGE:
	{
		//Only add child if max diffraction order is not reached yet
		if (iDiffractionOrder < *m_pMaxDiffractionOrder)
		{
			//Cast shape as edge and copy shape
442
			CPropagationEdgeShared pEdgeChildCopy = make_shared<CPropagationEdge>();
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
			pEdgeChildCopy->CopyFrom(*static_pointer_cast<CPropagationEdge>(pPropagationShapeChildIn));

			//Set output parameter
			vpShapeChildCopyOut = pEdgeChildCopy;

			//Add further children, if max order is not reached yet
			if (iCombinedOrder + 1 < *m_pMaxCombinedOrder)
			{
				for (const auto& pShapeChildChild : m_mvpShapeVisibilityMap[pPropagationShapeChildIn])
				{
					CPropagationShapeShared vpShapeChildChildCopy;
					RecursiveAddChildrenToTree(pShapeChildChild, vpShapeChildChildCopy, iReflectionOrder, iDiffractionOrder + 1, iCombinedOrder + 1);

					if (vpShapeChildChildCopy != nullptr)
					{
						vpShapeChildChildCopy->pParent = vpShapeChildCopyOut;
						vpShapeChildCopyOut->vpChildren.push_back(vpShapeChildChildCopy);
					}
				}
			}
		}
		break;
	}
	}
}

469
void ITAPropagationPathSim::CombinedModel::CPathEngine::CreateVisibilityMap()
470 471
{
	//Create a visibility map 
472
	for (auto& pPropagationShapeFrom : m_vpPropagationShapes)
473 474
	{

475
		for (auto& pPropagationShapeTo : m_vpPropagationShapes)
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
		{
			if (pPropagationShapeFrom == pPropagationShapeTo)
				continue;

			

			bool bCanIlluminate = false;

			if (pPropagationShapeFrom->iShapeType == CPropagationShape::FACE)
			{
				auto pPropagationFaceFrom = static_pointer_cast<CPropagationFace>(pPropagationShapeFrom);

				if (pPropagationShapeTo->iShapeType == CPropagationShape::FACE)
				{
					auto pPropagationFaceTo = static_pointer_cast<CPropagationFace>(pPropagationShapeTo);
					
492 493 494 495 496
					//If the faces are the same, they are not able to create valid paths
					if (pPropagationFaceFrom->sMeshModelName == pPropagationFaceTo->sMeshModelName
						&& pPropagationFaceFrom->hFace == pPropagationFaceTo->hFace)
						continue;

497 498 499 500 501 502 503 504 505 506
					//Start illumination test for both directions
					CanFaceIlluminateFace(bCanIlluminate, pPropagationFaceTo, pPropagationFaceFrom);
					if (bCanIlluminate)
						CanFaceIlluminateFace(bCanIlluminate, pPropagationFaceFrom, pPropagationFaceTo);

				}
				else if (pPropagationShapeTo->iShapeType == CPropagationShape::EDGE)
				{
					auto pPropagationEdgeTo = static_pointer_cast<CPropagationEdge>(pPropagationShapeTo);

507 508 509 510 511 512 513
					//If the faces are the same, they are not able to create valid paths
					if (pPropagationFaceFrom->sMeshModelName == pPropagationEdgeTo->sMeshModelName)
						if(pPropagationFaceFrom->hFace == pPropagationEdgeTo->hMainFace
							|| pPropagationFaceFrom->hFace == pPropagationEdgeTo->hOppositeFace)
						continue;


514 515 516 517 518 519 520 521 522 523 524 525 526 527
					//Start illumination test for both directions
					CanEdgeIlluminateFace(bCanIlluminate, pPropagationFaceFrom, pPropagationEdgeTo);
					if (bCanIlluminate)
						CanFaceIlluminateEdge(bCanIlluminate, pPropagationFaceFrom, pPropagationEdgeTo);
				}
			}
			else if (pPropagationShapeFrom->iShapeType == CPropagationShape::EDGE)
			{
				auto pPropagationEdgeFrom = static_pointer_cast<CPropagationEdge>(pPropagationShapeFrom);

				if (pPropagationShapeTo->iShapeType == CPropagationShape::FACE)
				{
					auto pPropagationFaceTo = static_pointer_cast<CPropagationFace>(pPropagationShapeTo);
					
528 529 530 531 532 533 534
					//If the faces are the same, they are not able to create valid paths
					if (pPropagationFaceTo->sMeshModelName == pPropagationEdgeFrom->sMeshModelName)
						if (pPropagationFaceTo->hFace == pPropagationEdgeFrom->hMainFace
							|| pPropagationFaceTo->hFace == pPropagationEdgeFrom->hOppositeFace)
							continue;


535 536 537 538 539 540 541 542 543 544 545
					//Start illumination test for both directions
					CanEdgeIlluminateFace(bCanIlluminate, pPropagationFaceTo, pPropagationEdgeFrom);
					if (bCanIlluminate)
						CanFaceIlluminateEdge(bCanIlluminate, pPropagationFaceTo, pPropagationEdgeFrom);

					
				}
				else if (pPropagationShapeTo->iShapeType == CPropagationShape::EDGE)
				{
					auto pPropagationEdgeTo = static_pointer_cast<CPropagationEdge>(pPropagationShapeTo);

546

547 548 549
					//Start illumination test for both directions
					CanEdgeIlluminateEdge(bCanIlluminate, pPropagationEdgeFrom, pPropagationEdgeTo);
					if (bCanIlluminate)
550
						//TODO: Implement option to choose whether to test intersections or not (last optional boolean value) 
551 552 553 554 555 556 557 558 559 560
						CanEdgeIlluminateEdge(bCanIlluminate, pPropagationEdgeFrom, pPropagationEdgeTo);
				}
			}
			
			if (bCanIlluminate)
				m_mvpShapeVisibilityMap[pPropagationShapeFrom].push_back(pPropagationShapeTo);
		}
	}
}

561
bool ITAPropagationPathSim::CombinedModel::CPathEngine::CanPointIlluminateFace(const VistaVector3D& v3Point, const string& sMeshModelName, CITAMesh::FaceHandle hFace)
562 563 564 565 566 567
{
	auto& pFaceMesh = m_pMeshModelList->GetMeshModel(sMeshModelName)->GetMesh()->pMesh;

	return ITAGeoUtils::CanFaceBeIlluminated(*pFaceMesh, hFace, v3Point);
}

568 569 570 571 572 573 574 575 576 577 578 579 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 612 613 614 615 616 617 618 619 620 621 622 623 624 625
bool ITAPropagationPathSim::CombinedModel::CPathEngine::CanPointIlluminateEdge(const VistaVector3D& v3Point, CPropagationEdgeShared& pPropagationEdge, const bool& bTestIntersection /* = false */)
{
	if (!CanPointIlluminateFace(v3Point, *pPropagationEdge->sMeshModelName, pPropagationEdge->hMainFace)
		&& !CanPointIlluminateFace(v3Point, *pPropagationEdge->sMeshModelName, pPropagationEdge->hOppositeFace))
	{
		return false;
	}
	if (bTestIntersection)
	{
		//Resolution (100 m)
		float fResolution = 10000 * ITAConstants::EPS_F_L;
		VistaVector3D v3EdgeDirection = *pPropagationEdge->v3ToVertex - *pPropagationEdge->v3FromVertex;
		float fRelativeStepSize = fResolution / (v3EdgeDirection.GetLength());
		float fRelativePosition = fRelativeStepSize;

		while (fRelativePosition < 1)
		{
			//If a visible path is found, the edge can be illuminated by the point
			if (IsPathVisible(v3Point, *pPropagationEdge->v3FromVertex + fRelativePosition * v3EdgeDirection))
				return true;
			else //Search further points on the edge
				fRelativePosition += fRelativeStepSize;

		}

		//No Visible path found
		return false;
	}
	else
	{
		return true;
	}
}

bool ITAPropagationPathSim::CombinedModel::CPathEngine::IsPathVisible(const VistaVector3D& v3StartPoint, const VistaVector3D& v3EndPoint)
{
	//Search for intersections between two neighbored anchors and repeat it for all neighbored anchors of the path

	//Iterate intersection search over all mesh models
	for (int iteration = 0; iteration < m_pMeshModelList->size(); iteration++)
	{
		//Iterate intersection search over all faces of current mesh model
		auto pMesh = m_pMeshModelList->at(iteration)->GetMesh()->pMesh;
		auto faceIter = pMesh->faces_begin();
		while (faceIter != pMesh->faces_end())
		{
			CITAMesh::FaceHandle hFace(*faceIter++);

			//If one of the path sections crosses one of the faces, the whole path won't be visible
			if (ITAGeoUtils::IsLineIntersectingFace(v3StartPoint, v3EndPoint, pMesh, hFace) == EIntersecting::BETWEEN)
				return false;
		}
	}

	//No intersections occur
	return true;
}

626
void ITAPropagationPathSim::CombinedModel::CPathEngine::CanEdgeIlluminateFace(bool &bCanEdgeIlluminateFaceOut, CPropagationFaceShared & pPropagationFace, CPropagationEdgeShared & pPropagationEdge)
627 628 629
{
	auto& pFaceMesh = m_pMeshModelList->GetMeshModel(*pPropagationFace->sMeshModelName)->GetMesh()->pMesh;

630 631 632 633
	float eps = ITAConstants::EPS_F_L;

	bCanEdgeIlluminateFaceOut = ITAGeoUtils::CanFaceBeIlluminated(*pFaceMesh, pPropagationFace->hFace, (1 - eps)*(*pPropagationEdge->v3FromVertex) + (eps)*(*pPropagationEdge->v3ToVertex));
	bCanEdgeIlluminateFaceOut |= ITAGeoUtils::CanFaceBeIlluminated(*pFaceMesh, pPropagationFace->hFace, (1 - eps)*(*pPropagationEdge->v3ToVertex) + (eps)*(*pPropagationEdge->v3FromVertex));
634 635
}

636
void ITAPropagationPathSim::CombinedModel::CPathEngine::CanFaceIlluminateEdge(bool &bCanFaceIlluminateEdgeOut, CPropagationFaceShared & pPropagationFace, CPropagationEdgeShared & pPropagationEdge)
637 638 639 640
{
	auto& pEdgeMesh = m_pMeshModelList->GetMeshModel(*pPropagationEdge->sMeshModelName)->GetMesh()->pMesh;

	//Iterate over all vertices of the face. If at least one vertex is in the illuminable range of the edge, return true
641 642
	bool bMainFaceIlluminated, bOppositeFaceIlluminated;

643 644
	for (auto& pVertex : pPropagationFace->vv3Vertices)
	{
645 646 647 648 649 650 651
		bMainFaceIlluminated = ITAGeoUtils::CanFaceBeIlluminated(*pEdgeMesh, pPropagationEdge->hMainFace, *pVertex);
		bOppositeFaceIlluminated = ITAGeoUtils::CanFaceBeIlluminated(*pEdgeMesh, pPropagationEdge->hOppositeFace, *pVertex);

		if (*m_pIgnoreIlluminatedRegionDiffraction)
			bCanFaceIlluminateEdgeOut = bMainFaceIlluminated != bOppositeFaceIlluminated; // A XOR B, only valid, if at least one point can lie in the shadow region
		else
			bCanFaceIlluminateEdgeOut = bMainFaceIlluminated || bOppositeFaceIlluminated; // A OR B, also valid, if face can only lie in the illuminated region
652 653 654 655 656 657

		if (bCanFaceIlluminateEdgeOut)
			return;
	}
}

658
void ITAPropagationPathSim::CombinedModel::CPathEngine::CanFaceIlluminateFace(bool &bCanFaceIlluminateFaceOut, CPropagationFaceShared & pPropagationFaceStart, CPropagationFaceShared & pPropagationFaceEnd)
659 660 661 662 663 664 665 666 667 668 669 670 671 672
{
	auto& pFaceEndMesh = m_pMeshModelList->GetMeshModel(*pPropagationFaceEnd->sMeshModelName)->GetMesh()->pMesh;

	//Iterate over all vertices of the face. If at least one vertex is in the illuminable range of the edge, return true
	bCanFaceIlluminateFaceOut = false;
	for (auto& pVertex : pPropagationFaceStart->vv3Vertices)
	{
		bCanFaceIlluminateFaceOut |= ITAGeoUtils::CanFaceBeIlluminated(*pFaceEndMesh, pPropagationFaceEnd->hFace, *pVertex);

		if (bCanFaceIlluminateFaceOut)
			return;
	}
}

673
void ITAPropagationPathSim::CombinedModel::CPathEngine::CanEdgeIlluminateEdge(bool &bCanEdgeIlluminateEdgeOut, CPropagationEdgeShared & pPropagationEdgeStart, CPropagationEdgeShared & pPropagationEdgeEnd, const bool& bTestIntersection /* = false */)
674 675
{
	auto& pEdgeEndMesh = m_pMeshModelList->GetMeshModel(*pPropagationEdgeEnd->sMeshModelName)->GetMesh()->pMesh;
676 677
	
	bool bMainFaceIlluminated, bOppositeFaceIlluminated;
678

679
	float eps = ITAConstants::EPS_F_L;
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
	//Illumination test at from vertex position
	bMainFaceIlluminated = ITAGeoUtils::CanFaceBeIlluminated(*pEdgeEndMesh, pPropagationEdgeEnd->hMainFace, (1 - eps)*(*pPropagationEdgeStart->v3FromVertex) + (eps)*(*pPropagationEdgeStart->v3ToVertex));
	bOppositeFaceIlluminated = ITAGeoUtils::CanFaceBeIlluminated(*pEdgeEndMesh, pPropagationEdgeEnd->hOppositeFace, (1 - eps)*(*pPropagationEdgeStart->v3FromVertex) + (eps)*(*pPropagationEdgeStart->v3ToVertex));
	
	if (*m_pIgnoreIlluminatedRegionDiffraction)
		bCanEdgeIlluminateEdgeOut = bMainFaceIlluminated != bOppositeFaceIlluminated; // A XOR B, only valid, if at least one point can lie in the shadow region
	else
		bCanEdgeIlluminateEdgeOut = bMainFaceIlluminated || bOppositeFaceIlluminated; // A OR B, also valid, if face can only lie in the illuminated region

	if (bCanEdgeIlluminateEdgeOut)
		return;

	//Illumination test at to vertex position
	bMainFaceIlluminated = ITAGeoUtils::CanFaceBeIlluminated(*pEdgeEndMesh, pPropagationEdgeEnd->hMainFace, (1 - eps)*(*pPropagationEdgeStart->v3ToVertex) + (eps)*(*pPropagationEdgeStart->v3FromVertex));
	bOppositeFaceIlluminated = ITAGeoUtils::CanFaceBeIlluminated(*pEdgeEndMesh, pPropagationEdgeEnd->hOppositeFace, (1 - eps)*(*pPropagationEdgeStart->v3ToVertex) + (eps)*(*pPropagationEdgeStart->v3FromVertex));

	if (*m_pIgnoreIlluminatedRegionDiffraction)
		bCanEdgeIlluminateEdgeOut = bMainFaceIlluminated != bOppositeFaceIlluminated; // A XOR B, only valid, if at least one point can lie in the shadow region
	else
		bCanEdgeIlluminateEdgeOut = bMainFaceIlluminated || bOppositeFaceIlluminated; // A OR B, also valid, if face can only lie in the illuminated region

701 702 703 704 705
	//If boolean set, also test for intersections(
	if (bCanEdgeIlluminateEdgeOut && bTestIntersection)
	{
		//Set output parameter to false
		bCanEdgeIlluminateEdgeOut = false;
706

707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
		//Resolution (100 m)
		float fResolution = 10 * ITAConstants::EPS_F_L;
		VistaVector3D v3EdgeDirection = *pPropagationEdgeStart->v3ToVertex - *pPropagationEdgeStart->v3FromVertex;
		float fRelativeStepSize = fResolution / (v3EdgeDirection.GetLength());
		float fRelativePosition = fRelativeStepSize;

		while (fRelativePosition < 1)
		{
			if (CanPointIlluminateEdge(*pPropagationEdgeStart->v3FromVertex + fRelativePosition* v3EdgeDirection, pPropagationEdgeEnd, bTestIntersection))
			{
				bCanEdgeIlluminateEdgeOut = true; //if at least one position doesn't contain a intersection with other faces
				break; 
			}
			else
			{
				fRelativePosition += fRelativeStepSize;
			}
		}
	}
726 727
}

728
void ITAPropagationPathSim::CombinedModel::CPathEngine::ConstructPropagationShapes()
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
{

	for (auto& pMeshModel : *m_pMeshModelList)
	{
		CITAMesh* pMesh = pMeshModel->GetMesh()->pMesh;
		auto psMeshModelName = make_shared<string>(pMeshModel->GetName());

		//Iterate over all faces of current mesh
		CITAMesh::ConstFaceIter cf_it = pMesh->faces_begin();
		while (cf_it != pMesh->faces_end())
		{
			CITAMesh::FaceHandle hFace(*cf_it++);

			CPropagationFaceShared pPropagationFace = make_shared<CPropagationFace>();

			pPropagationFace->iShapeType = CPropagationShape::FACE;
			pPropagationFace->sMeshModelName = psMeshModelName;
746
			pPropagationFace->pIsIlluminableBySensor = make_shared<Tristate>(Tristate::Undefined);
747 748 749 750 751 752 753 754 755 756 757 758
			pPropagationFace->hFace = hFace;

			//Get vertices
			//Iterate over all vertices of current face
			CITAMesh::ConstFaceVertexIter cfv_it = pMesh->cfv_begin(hFace);
			while (cfv_it != pMesh->cfv_end(hFace))
			{
				CITAMesh::VertexHandle hVertex(*cfv_it++);

				pPropagationFace->vv3Vertices.push_back(make_shared<VistaVector3D>(pMesh->point(hVertex).data()));
			}

759 760 761 762 763
			//Construct plane
			pPropagationFace->pPlane = make_shared<VistaPlane>();
			pPropagationFace->pPlane->SetOrigin(*pPropagationFace->vv3Vertices[0]);//set first vertex as origin
			pPropagationFace->pPlane->SetNormVector(VistaVector3D(pMesh->calc_face_normal(hFace).data()));//set face normal as plane normal

764
			m_vpPropagationShapes.push_back(pPropagationFace);
765 766 767 768 769 770 771 772 773 774 775 776
		}

		//Iterate over all edges of current mesh
		CITAMesh::ConstEdgeIter cf_et = pMesh->edges_begin();
		while (cf_et != pMesh->edges_end())
		{
			CITAMesh::EdgeHandle hEdge(*cf_et++);

			CPropagationEdgeShared pPropagationEdge = make_shared<CPropagationEdge>();

			pPropagationEdge->iShapeType = CPropagationShape::EDGE;
			pPropagationEdge->sMeshModelName = psMeshModelName;
777
			pPropagationEdge->pIsIlluminableBySensor = make_shared<Tristate>(Tristate::Undefined);
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
			pPropagationEdge->hEdge = hEdge;
			pPropagationEdge->hHalfedge = pMesh->halfedge_handle(hEdge, 0);

			//Set vertices
			auto hFromVertex = pMesh->from_vertex_handle(pPropagationEdge->hHalfedge);
			auto hToVertex = pMesh->to_vertex_handle(pPropagationEdge->hHalfedge);
			pPropagationEdge->v3FromVertex = make_shared<VistaVector3D>(pMesh->point(hFromVertex).data());
			pPropagationEdge->v3ToVertex = make_shared<VistaVector3D>(pMesh->point(hToVertex).data());

			//Set face normals
			pPropagationEdge->hMainFace = pMesh->face_handle(pPropagationEdge->hHalfedge);
			pPropagationEdge->hOppositeFace = pMesh->opposite_face_handle(pPropagationEdge->hHalfedge);
			pPropagationEdge->v3MainFaceNormal = make_shared<VistaVector3D>(pMesh->calc_face_normal(pPropagationEdge->hMainFace).data());
			pPropagationEdge->v3OppositeFaceNormal = make_shared<VistaVector3D>(pMesh->calc_face_normal(pPropagationEdge->hOppositeFace).data());

793
			m_vpPropagationShapes.push_back(pPropagationEdge);
794 795 796 797
		}
	}

}