Files
UnrealEngineUWP/Engine/Source/Runtime/NavigationSystem/Private/NavigationOctree.cpp
mieszko zielinski 1952570efa Extracted NavigationSystemV1's logic concerning NavOctree and navigation DirtyAreas into a separate structures for reusability
Notable changes:
- Added FNavigationOctreeController that wraps up what used to be NavigationSystemV1's navoctree-related logic
- Added FNavigationDirtyAreasController that wraps up what used to be NavigationSystemV1's DirtyAreas-related logic
- Added FNavigationDataHandler that is a helper struct that wraps up what used to be NavigationSystemV1's logic related to operation involving both navoctree and dirty areas
- Deprecated both FNavDataConfig.NavigationDataClass and FNavDataConfig.NavigationDataClassName and replaced them with a single NavDataClass property
- FNavigationOctree is not responsible for hashing element objects and storing their ElementId
- NavOctree elements how know about the octree they belong to (via a member property).

[at]Yoan.StAmant, [at]Maxime.Mercier, [at]Guillaume.Guay
#rb Yoan.StAmant

#ROBOMERGE-OWNER: ben.marsh
#ROBOMERGE-AUTHOR: mieszko.zielinski
#ROBOMERGE-SOURCE: CL 7249089 via CL 7262555 via CL 7262559
#ROBOMERGE-BOT: BUILD (Main -> Dev-Build) (v371-7306989)

[CL 7334880 by mieszko zielinski in Dev-Build branch]
2019-07-16 23:29:13 -04:00

235 lines
7.4 KiB
C++

// Copyright 1998-2019 Epic Games, Inc. All Rights Reserved.
#include "NavigationOctree.h"
#include "AI/Navigation/NavRelevantInterface.h"
#include "NavigationSystem.h"
namespace
{
FNavigationOctree DummyNavOctree(FVector(0), 0);
}
//----------------------------------------------------------------------//
// FNavigationOctree
//----------------------------------------------------------------------//
FNavigationOctree::FNavigationOctree(const FVector& Origin, float Radius)
: TOctree<FNavigationOctreeElement, FNavigationOctreeSemantics>(Origin, Radius)
, DefaultGeometryGatheringMode(ENavDataGatheringMode::Instant)
, bGatherGeometry(false)
, NodesMemory(0)
{
INC_DWORD_STAT_BY( STAT_NavigationMemory, sizeof(*this) );
}
FNavigationOctree::~FNavigationOctree()
{
DEC_DWORD_STAT_BY( STAT_NavigationMemory, sizeof(*this) );
DEC_MEMORY_STAT_BY(STAT_Navigation_CollisionTreeMemory, NodesMemory);
ObjectToOctreeId.Empty();
}
void FNavigationOctree::SetDataGatheringMode(ENavDataGatheringModeConfig Mode)
{
check(Mode != ENavDataGatheringModeConfig::Invalid);
DefaultGeometryGatheringMode = ENavDataGatheringMode(Mode);
}
void FNavigationOctree::SetNavigableGeometryStoringMode(ENavGeometryStoringMode NavGeometryMode)
{
bGatherGeometry = (NavGeometryMode == FNavigationOctree::StoreNavGeometry);
}
void FNavigationOctree::DemandLazyDataGathering(FNavigationRelevantData& ElementData)
{
UObject* ElementOb = ElementData.GetOwner();
if (ElementOb == nullptr)
{
return;
}
bool bShrink = false;
const int32 OrgElementMemory = ElementData.GetGeometryAllocatedSize();
if (ElementData.IsPendingLazyGeometryGathering() == true && ElementData.SupportsGatheringGeometrySlices() == false)
{
QUICK_SCOPE_CYCLE_COUNTER(STAT_RecastNavMeshGenerator_LazyGeometryExport);
UActorComponent& ActorComp = *CastChecked<UActorComponent>(ElementOb);
ComponentExportDelegate.ExecuteIfBound(&ActorComp, ElementData);
bShrink = true;
// mark this element as no longer needing geometry gathering
ElementData.bPendingLazyGeometryGathering = false;
}
if (ElementData.IsPendingLazyModifiersGathering())
{
QUICK_SCOPE_CYCLE_COUNTER(STAT_RecastNavMeshGenerator_LazyModifiersExport);
INavRelevantInterface* NavElement = Cast<INavRelevantInterface>(ElementOb);
check(NavElement);
NavElement->GetNavigationData(ElementData);
ElementData.bPendingLazyModifiersGathering = false;
bShrink = true;
}
if (bShrink)
{
// validate exported data
// shrink arrays before counting memory
// it will be reallocated when adding to octree and RemoveNode will have different value returned by GetAllocatedSize()
ElementData.ValidateAndShrink();
}
const int32 ElementMemoryChange = ElementData.GetGeometryAllocatedSize() - OrgElementMemory;
const_cast<FNavigationOctree*>(this)->NodesMemory += ElementMemoryChange;
INC_MEMORY_STAT_BY(STAT_Navigation_CollisionTreeMemory, ElementMemoryChange);
}
void FNavigationOctree::DemandLazyDataGathering(const FNavigationOctreeElement& Element)
{
FNavigationRelevantData& MutableData = const_cast<FNavigationRelevantData&>(*Element.Data);
DemandLazyDataGathering(MutableData);
}
void FNavigationOctree::AddNode(UObject* ElementOb, INavRelevantInterface* NavElement, const FBox& Bounds, FNavigationOctreeElement& Element)
{
// we assume NavElement is ElementOb already cast
Element.Bounds = Bounds;
if (NavElement)
{
const ENavDataGatheringMode GatheringMode = NavElement->GetGeometryGatheringMode();
bool bDoInstantGathering = ((GatheringMode == ENavDataGatheringMode::Default && DefaultGeometryGatheringMode == ENavDataGatheringMode::Instant)
|| GatheringMode == ENavDataGatheringMode::Instant);
if (bGatherGeometry)
{
UActorComponent* ActorComp = Cast<UActorComponent>(ElementOb);
if (ActorComp)
{
if (bDoInstantGathering)
{
ComponentExportDelegate.ExecuteIfBound(ActorComp, *Element.Data);
}
else
{
Element.Data->bPendingLazyGeometryGathering = true;
Element.Data->bSupportsGatheringGeometrySlices = NavElement && NavElement->SupportsGatheringGeometrySlices();
}
}
}
SCOPE_CYCLE_COUNTER(STAT_Navigation_GatheringNavigationModifiersSync);
if (bDoInstantGathering)
{
NavElement->GetNavigationData(*Element.Data);
}
else
{
Element.Data->bPendingLazyModifiersGathering = true;
}
}
// validate exported data
// shrink arrays before counting memory
// it will be reallocated when adding to octree and RemoveNode will have different value returned by GetAllocatedSize()
Element.ValidateAndShrink();
const int32 ElementMemory = Element.GetAllocatedSize();
NodesMemory += ElementMemory;
INC_MEMORY_STAT_BY(STAT_Navigation_CollisionTreeMemory, ElementMemory);
AddElement(Element);
}
void FNavigationOctree::AppendToNode(const FOctreeElementId& Id, INavRelevantInterface* NavElement, const FBox& Bounds, FNavigationOctreeElement& Element)
{
FNavigationOctreeElement OrgData = GetElementById(Id);
Element = OrgData;
Element.Bounds = Bounds + OrgData.Bounds.GetBox();
if (NavElement)
{
SCOPE_CYCLE_COUNTER(STAT_Navigation_GatheringNavigationModifiersSync);
NavElement->GetNavigationData(*Element.Data);
}
// validate exported data
// shrink arrays before counting memory
// it will be reallocated when adding to octree and RemoveNode will have different value returned by GetAllocatedSize()
Element.ValidateAndShrink();
const int32 OrgElementMemory = OrgData.GetAllocatedSize();
const int32 NewElementMemory = Element.GetAllocatedSize();
const int32 MemoryDelta = NewElementMemory - OrgElementMemory;
NodesMemory += MemoryDelta;
INC_MEMORY_STAT_BY(STAT_Navigation_CollisionTreeMemory, MemoryDelta);
RemoveElement(Id);
AddElement(Element);
}
void FNavigationOctree::UpdateNode(const FOctreeElementId& Id, const FBox& NewBounds)
{
FNavigationOctreeElement ElementCopy = GetElementById(Id);
RemoveElement(Id);
ElementCopy.Bounds = NewBounds;
AddElement(ElementCopy);
}
void FNavigationOctree::RemoveNode(const FOctreeElementId& Id)
{
const FNavigationOctreeElement& Element = GetElementById(Id);
const int32 ElementMemory = Element.GetAllocatedSize();
NodesMemory -= ElementMemory;
DEC_MEMORY_STAT_BY(STAT_Navigation_CollisionTreeMemory, ElementMemory);
RemoveElement(Id);
}
const FNavigationRelevantData* FNavigationOctree::GetDataForID(const FOctreeElementId& Id) const
{
if (Id.IsValidId() == false)
{
return nullptr;
}
const FNavigationOctreeElement& OctreeElement = GetElementById(Id);
return &*OctreeElement.Data;
}
void FNavigationOctree::SetElementId(const UObject& Object, FOctreeElementId Id)
{
ObjectToOctreeId.Add(HashObject(Object), Id);
}
//----------------------------------------------------------------------//
// FNavigationOctreeSemantics
//----------------------------------------------------------------------//
#if NAVSYS_DEBUG
FORCENOINLINE
#endif // NAVSYS_DEBUG
void FNavigationOctreeSemantics::SetElementId(const FNavigationOctreeElement& Element, FOctreeElementId Id)
{
const bool bEvenIfPendingKill = true;
UObject* ElementOwner = Element.GetOwner(bEvenIfPendingKill);
if (ElementOwner)
{
Element.GetOwnerOctree().SetElementId(*ElementOwner, Id);
}
}
//----------------------------------------------------------------------//
// FNavigationOctreeElement
//----------------------------------------------------------------------//
FNavigationOctreeElement::FNavigationOctreeElement(UObject& SourceObject)
: Data(new FNavigationRelevantData(SourceObject))
, OwnerOctree(DummyNavOctree)
{
}