Tutorial_Generation_postProcess.cpp

Tutorial_Generation_postProcess.cpp
/*
* Copyright 2015 Autodesk, Inc. All rights reserved.
* Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
* or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
*/
#include "common/basesystemenv.h"
#include "common/GeneratorEnvInit.h"
DONT_RUN_THIS_FILE
namespace
{
// This tutorial illustrates how to post-process recently generated NavData by adding
// NavGraphs that have been placed using queries run on the NavMesh (to locate the vertices and edges of the graph).
class Tutorial_Generation_PostProcess : public BaseSystemEnv
{
public:
// This is used to set up the Alloc instance for the generation,
// and the log for the Tutorial.
typedef GeneratorEnvInit InitClass;
public:
// This function create a NavGraph that is computed using an A* Query on the current sector
// being considered. This is done by loading the NavData generated for this sector and adding it to
// a Database. A query is then performed on this database and a graph is created from the path computed.
bool ComputeNavGraphFromNavMesh(Kaim::GeneratorSector* sector, Kaim::NavGraphBlobBuilder& graphBuilder)
{
// Load the NavMesh in a Database
Kaim::Ptr<Kaim::NavData> sectorNavData = sector->GetNavData();
Kaim::World world;
Kaim::Database* database = world.GetDatabase(0);
sectorNavData->Init(database);
sectorNavData->AddToDatabaseImmediate();
// Now create a A* query between startPos and endPos
defaultAstarQuery.BindToDatabase(database);
Kaim::Vec3f startPos(5.70439f,-64.2678f,8.10056f);
Kaim::Vec3f endPos(-75.9229f,68.2467f,8.3627f);
defaultAstarQuery.Initialize(startPos, endPos);
// Execute it
defaultAstarQuery.PerformQueryBlocking();
Kaim::AStarQueryResult result = defaultAstarQuery.GetResult();
if (result != Kaim::ASTAR_DONE_PATH_FOUND || defaultAstarQuery.GetPath() == KY_NULL)
{
return false;
}
// Retrieve the path
Kaim::Path* path = defaultAstarQuery.GetPath();
KyUInt32 previousVtxIdx = KyUInt32MAXVAL;
// And build a simple graph from it.
for (KyUInt32 vtxIdx = 0; vtxIdx < path->GetNodeCount(); ++vtxIdx)
{
KyUInt32 currentVtxIdx = graphBuilder.AddVertexWithoutNavTag(path->GetNodePosition(vtxIdx));
if (previousVtxIdx!= KyUInt32MAXVAL)
{
graphBuilder.AddBidirectionalEdgeWithoutNavTag(currentVtxIdx, previousVtxIdx);
}
previousVtxIdx = currentVtxIdx;
}
return true;
}
bool Generate_WithPostProcess()
{
// Create a producer to read the obj file.
Kaim::Ptr<Kaim::OBJProducer> producer = *KY_NEW Kaim::OBJProducer;
// Set up a generator using the Producer and no Glue;
// parallel processing will be disabled.
Kaim::Generator generator(producer);
// Set the Directory where the navdata and clientInput files
// will be saved.
generator.SetOutputDirectory(RootDir().c_str(), RelativeOutputDir().c_str());
// Add one sector
Kaim::GeneratorInputOutput generatorInputOutput;
Kaim::Ptr<Kaim::GeneratorSector> sector = *KY_NEW Kaim::GeneratorSector(Kaim::KyGuid::GetDefaultGuid(), "Village");
sector->m_inputFileNames.PushBack(GetAbsoluteInputFileName("common/Village_250x200_1m_noflip.obj").c_str());
// This stage is complete, the generator can run using the configuration established above.
if (generator.Generate(generatorInputOutput) == KY_ERROR)
return false;
if (ComputeNavGraphFromNavMesh(sector.GetPtr(), graphBuilder) == false)
return false;
// Add the NavGraph we just built to a new NavData ...
Kaim::NavData graphNavData;
graphNavData.AddNavGraph(&graphBuilder);
// ... and save it !
return graphNavData.Save(GetAbsoluteOutputFileName("TutorialGraph.NavData").c_str()) == KY_SUCCESS;
}
};
#define TEST_ENV_CLASS Tutorial_Generation_PostProcess
TEST_ENV {}
TUTORIAL { CHECK(env.Generate_WithPostProcess()) }
}