Tutorial_Generation_basics.cpp

Tutorial_Generation_basics.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/GeneratorEnvInit.h"
#include "common/basesystemenv.h"
//RUN_THIS_FILE
namespace
{
// This tutorial demonstrates how to code a basic GeneratorInputProducer designed to handle
// two sectors named "Sector_A" and "Sector_B".
// A producer must inherit from Kaim::GeneratorInputProducer and implement the abstract
// method KyResult Produce(const GeneratorSector&, Kaim::ClientInputConsumer&)
// In this tutorial, this method will be called twice by the generator (once for each sector),
// and the producer will provide the relevant data to the InputConsumer.
// In this case, for clarity, the sectors are only made of two adjacent triangles.
class TutorialProducer_Basics : public Kaim::GeneratorInputProducer
{
public:
virtual KyResult Produce(const Kaim::GeneratorSector& sector, Kaim::ClientInputConsumer& inputConsumer)
{
Kaim::String sectorName(sector.GetName());
Kaim::String sector_A_Name("Sector_A");
Kaim::String sector_B_Name("Sector_B");
// Both name and GUID can be used to identify which sector the InputConsumer is requesting.
if (sectorName == sector_A_Name)
{
// When the current sector has been identified, feed the relevant data (geometry, seedpoints, tagVolumes)
// for this sector by calling the functions provided by the InputConsumer.
// In this case, add a basic triangle with a custom NavData value of "42".
Kaim::Vec3f aTriangle[3] = { Kaim::Vec3f(0.0f, 0.0f, 0.0f), Kaim::Vec3f(25.0f, 25.0f, 0.0f), Kaim::Vec3f(25.0f, 0.0f, 0.0f) };
Kaim::DynamicNavTag aNavTag;
aNavTag.m_blindDataArray.PushBack(42);
inputConsumer.ConsumeTriangle(aTriangle[0], aTriangle[1], aTriangle[2], aNavTag);
}
else if (sector.GetGuid() == Kaim::KyGuid("BBBBBBBB-BBBB-BBBB-BBBB-BBBBBBBBBBBB"))
{
// In this case, add a basic triangle with a custom NavData value of "13".
Kaim::Vec3f bTriangle[3] = { Kaim::Vec3f(25.0f, 0.0f, 0.0f), Kaim::Vec3f(25.0f, 25.0f, 0.0f), Kaim::Vec3f(50.0f, 0.0f, 0.0f) };
Kaim::DynamicNavTag bNavTag;
bNavTag.m_blindDataArray.PushBack(13);
inputConsumer.ConsumeTriangle(bTriangle[0], bTriangle[1], bTriangle[2], bNavTag);
}
return KY_SUCCESS;
}
};
class Tutorial_Generation_Basic : 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:
bool Generate_Minimal()
{
// Create the producer using the class coded above.
Kaim::Ptr<TutorialProducer_Basics> producer = *KY_NEW TutorialProducer_Basics;
// 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 sectors A and B.
Kaim::GeneratorInputOutput generatorInputOutput;
Kaim::Ptr<Kaim::GeneratorSector> sectorA = *KY_NEW Kaim::GeneratorSector(Kaim::KyGuid("AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA"), "Sector_A");
Kaim::Ptr<Kaim::GeneratorSector> sectorB = *KY_NEW Kaim::GeneratorSector(Kaim::KyGuid("BBBBBBBB-BBBB-BBBB-BBBB-BBBBBBBBBBBB"), "Sector_B");
generatorInputOutput.AddSector(sectorA);
generatorInputOutput.AddSector(sectorB);
// This stage is complete, the generator can run using the configuration established above.
if (generator.Generate(generatorInputOutput) == KY_ERROR)
return false;
return true;
}
bool Generate_WithGlueAndConfig()
{
// Create a default GeneratorGlue to have:
// - a default GUID generator for sectors,
// - a TBB implementation of parallel for,
// - and a TBB implementation of scalable malloc.
// NOTE: these three interfaces are optional, but they
// enable parallel processing and automatic GUID generation if enabled.
// TBBTlsAlloc must be instantiated in the glue first. Check GeneratorTestEnvInit
// for more information.
// Create the producer using the class coded above.
Kaim::Ptr<TutorialProducer_Basics> producer = *KY_NEW TutorialProducer_Basics;;
// Setup a generator using both Producer and Glue.
Kaim::Generator generator(producer, &glue);
// Set root Directory and Name.
generator.SetOutputDirectory(RootDir().c_str(), RelativeOutputDir().c_str());
//generator.SetName("Basic_Tutorial_Generator");
// Set the GeneratorInputOutput
Kaim::GeneratorInputOutput generatorInputOutput;
generatorInputOutput.m_params.m_entityRadius = 0.5f;
generatorInputOutput.m_params.m_rasterPrecision = 0.5f;
// ...
// Set the Generation run options.
generatorInputOutput.m_runOptions.m_outputFilesEndianness = Kaim::Endianness::LittleEndian;
// ...
// Add sectors A and B.
Kaim::Ptr<Kaim::GeneratorSector> sectorA = *KY_NEW Kaim::GeneratorSector(Kaim::KyGuid("AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA"), "Sector_A");
Kaim::Ptr<Kaim::GeneratorSector> sectorB = *KY_NEW Kaim::GeneratorSector(Kaim::KyGuid("BBBBBBBB-BBBB-BBBB-BBBB-BBBBBBBBBBBB"), "Sector_B");
generatorInputOutput.AddSector(sectorA);
generatorInputOutput.AddSector(sectorB);
// This stage is complete, the generator can run using the configuration established above.
if (generator.Generate(generatorInputOutput) == KY_ERROR)
return false;;
return true;
}
};
#define TEST_ENV_CLASS Tutorial_Generation_Basic
TEST_ENV {}
TUTORIAL { CHECK(env.Generate_Minimal()) }
TUTORIAL { CHECK(env.Generate_WithGlueAndConfig()) }
}