Skip to content

Commit fa3af5b

Browse files
committed
Ex22 CommandLineHandler
1 parent 472f1d4 commit fa3af5b

File tree

5 files changed

+330
-19
lines changed

5 files changed

+330
-19
lines changed

examples_tests/22.RaytracedAO/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ endif()
3232
set(EXTRA_SOURCES
3333
../../src/nbl/ext/DebugDraw/CDraw3DLine.cpp
3434
Renderer.cpp
35+
CommandLineHandler.cpp
3536
)
3637

3738
nbl_create_executable_project(
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
// Copyright (C) 2018-2020 - DevSH Graphics Programming Sp. z O.O.
2+
// This file is part of the "Nabla Engine".
3+
// For conditions of distribution and use, see copyright notice in nabla.h
4+
5+
#include "CommandLineHandler.hpp"
6+
7+
#include <algorithm>
8+
#include <filesystem>
9+
#include <iostream>
10+
11+
using namespace nbl;
12+
using namespace asset;
13+
using namespace core;
14+
15+
CommandLineHandler::CommandLineHandler(const std::vector<std::string>& argv)
16+
{
17+
if(argv.size() > MaxRayTracerCommandLineArgs)
18+
{
19+
std::cout << helpMessage.data() << std::endl;
20+
return;
21+
}
22+
23+
auto logError = [&](const std::string message)
24+
{
25+
std::cout << "ERROR (" + std::to_string(__LINE__) + " line): " + message << std::endl;
26+
};
27+
28+
auto arguments = argv;
29+
30+
auto getSerializedValues = [&](const auto& variablesStream, const std::regex& separator=std::regex{"[[:s:]]"})
31+
{
32+
std::sregex_token_iterator it{ variablesStream.begin(), variablesStream.end(), separator, -1 };
33+
std::vector<std::string> variablesHandle = { it,{} };
34+
35+
// remove any accidental whitespace only vars
36+
variablesHandle.erase(
37+
std::remove_if(
38+
variablesHandle.begin(),variablesHandle.end(),
39+
[](const std::string& x) {return !std::regex_search(x,std::regex{"[^[:s:]]"}); }
40+
),
41+
variablesHandle.end()
42+
);
43+
44+
// remove double-quotes
45+
for(auto& var : variablesHandle)
46+
{
47+
var.erase(std::remove(var.begin(), var.end(), '\"'), var.end());
48+
}
49+
50+
return variablesHandle;
51+
};
52+
53+
initializeMatchingMap();
54+
55+
RaytracerExampleArguments previousArg = REA_COUNT;
56+
57+
bool success = true;
58+
59+
for (auto i = 0; i < arguments.size(); ++i)
60+
{
61+
std::string rawFetchedCmdArgument = arguments[i];
62+
63+
bool addToPreviousOption = false;
64+
65+
const auto firstHyphen = rawFetchedCmdArgument.find_first_of("-");
66+
if(firstHyphen != 0)
67+
addToPreviousOption = true;
68+
69+
if(addToPreviousOption)
70+
{
71+
if(REA_COUNT != previousArg)
72+
{
73+
if(!rawVariables[previousArg].has_value())
74+
rawVariables[previousArg].emplace(std::vector<std::string>());
75+
76+
auto & outVector = rawVariables[previousArg].value();
77+
std::vector<std::string> toAdd = getSerializedValues(rawFetchedCmdArgument);
78+
outVector.insert(outVector.end(), toAdd.begin(), toAdd.end());
79+
}
80+
else
81+
{
82+
logError("Unexcepted argument!, command options should start with '-' character");
83+
success = false;
84+
break;
85+
}
86+
}
87+
else
88+
{
89+
const auto offset = firstHyphen + 1;
90+
const auto endOfFetchedVariableName = rawFetchedCmdArgument.find_first_of("=");
91+
const auto count = endOfFetchedVariableName - offset;
92+
const auto cmdFetchedVariable = rawFetchedCmdArgument.substr(offset, count);
93+
std::string variable = cmdFetchedVariable;
94+
auto arg = getMatchedVariableMapID(variable);
95+
96+
if(arg == REA_COUNT)
97+
{
98+
logError("Unexcepted argument!!");
99+
success = false;
100+
break;
101+
}
102+
103+
if(rawVariables[arg].has_value())
104+
{
105+
logError("Variable used previously!");
106+
success = false;
107+
break;
108+
}
109+
110+
if(endOfFetchedVariableName != std::string::npos)
111+
{
112+
auto value = rawFetchedCmdArgument.substr(endOfFetchedVariableName + 1);
113+
std::vector<std::string> toAdd = getSerializedValues(value);
114+
rawVariables[arg].emplace(toAdd);
115+
}
116+
else
117+
{
118+
std::vector<std::string> emptyVec;
119+
rawVariables[arg].emplace(emptyVec);
120+
}
121+
122+
previousArg = arg;
123+
}
124+
125+
}
126+
127+
if (!validateParameters() || !success)
128+
return;
129+
130+
performFinalAssignmentStepForUsefulVariables();
131+
}
132+
133+
bool CommandLineHandler::validateParameters()
134+
{
135+
auto logError = [&](const std::string message)
136+
{
137+
std::cout << "ERROR (" + std::to_string(__LINE__) + " line): " + message << std::endl;
138+
};
139+
140+
if(rawVariables[REA_SCENE].has_value())
141+
{
142+
auto sceneDirectory = rawVariables[REA_SCENE].value();
143+
if(sceneDirectory.empty())
144+
{
145+
logError("Expected at least one value for SCENE");
146+
return false;
147+
}
148+
}
149+
150+
if(rawVariables[REA_SCREENSHOT_OUTPUT_FOLDER].has_value())
151+
{
152+
auto screenshotOutputFolder = rawVariables[REA_SCREENSHOT_OUTPUT_FOLDER].value();
153+
if(screenshotOutputFolder.empty())
154+
{
155+
logError("Expected at least one value for SCREENSHOT_OUTPUT_FOLDER");
156+
return false;
157+
}
158+
}
159+
160+
return true;
161+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// Copyright (C) 2018-2020 - DevSH Graphics Programming Sp. z O.O.
2+
// This file is part of the "Nabla Engine".
3+
// For conditions of distribution and use, see copyright notice in nabla.h
4+
5+
#ifndef _RAYTRACER_COMMAND_LINE_HANDLER_
6+
#define _RAYTRACER_COMMAND_LINE_HANDLER_
7+
8+
#include <iostream>
9+
#include <cstdio>
10+
#include <chrono>
11+
#include "nabla.h"
12+
#include "nbl/core/core.h"
13+
#include "nbl/ext/MitsubaLoader/CMitsubaLoader.h"
14+
15+
constexpr std::string_view helpMessage = R"(
16+
17+
Parameters:
18+
-SCENE=sceneMitsubaXMLPathOrZipAndXML
19+
-SCREENSHOT_OUTPUT=exrScreenShotOutputFilePath
20+
-TERMINATE
21+
22+
Description and usage:
23+
24+
-SCENE:
25+
some/path extra/path which will make it skip the file choose dialog
26+
27+
-SCREENSHOT_OUTPUT:
28+
output folder path for exr screenshots
29+
30+
-TERMINATE:
31+
which will make the app stop when the required amount of samples has been renderered (its in the Mitsuba Scene metadata) and obviously take screenshot when quitting
32+
33+
Example Usage :
34+
raytracedao.exe -SCENE=../../media/kitchen.zip scene.xml -TERMINATE -SCREENSHOT_OUTPUT_FOLDER=C:\\Users\\UserName\\Documents\\My-ScreenShots
35+
Alternative:
36+
raytracedao.exe -SCENE ../../media/kitchen.zip scene.xml -TERMINATE -SCREENSHOT_OUTPUT_FOLDER C:\\Users\\UserName\\Documents\\My-ScreenShots
37+
)";
38+
39+
40+
constexpr std::string_view SCENE_VAR_NAME = "SCENE";
41+
constexpr std::string_view SCREENSHOT_OUTPUT_FOLDER_VAR_NAME = "SCREENSHOT_OUTPUT_FOLDER";
42+
constexpr std::string_view TERMINATE_VAR_NAME = "TERMINATE";
43+
44+
constexpr uint32_t MaxRayTracerCommandLineArgs = 8;
45+
46+
enum RaytracerExampleArguments
47+
{
48+
REA_SCENE,
49+
REA_SCREENSHOT_OUTPUT_FOLDER,
50+
REA_TERMINATE,
51+
REA_COUNT,
52+
};
53+
54+
using variablesType = std::unordered_map<RaytracerExampleArguments, std::optional<std::vector<std::string>>>;
55+
56+
class CommandLineHandler
57+
{
58+
public:
59+
60+
CommandLineHandler(const std::vector<std::string>& argv);
61+
62+
auto& getSceneDirectory() const
63+
{
64+
return sceneDirectory;
65+
}
66+
67+
auto& getOutputScreenshotsFolderPath() const
68+
{
69+
return outputScreenshotsFolderPath;
70+
}
71+
72+
auto& getTerminate() const
73+
{
74+
return terminate;
75+
}
76+
77+
private:
78+
79+
void initializeMatchingMap()
80+
{
81+
rawVariables[REA_SCENE];
82+
rawVariables[REA_SCREENSHOT_OUTPUT_FOLDER];
83+
rawVariables[REA_TERMINATE];
84+
}
85+
86+
RaytracerExampleArguments getMatchedVariableMapID(const std::string& variableName)
87+
{
88+
if (variableName == SCENE_VAR_NAME)
89+
return REA_SCENE;
90+
else if (variableName == SCREENSHOT_OUTPUT_FOLDER_VAR_NAME)
91+
return REA_SCREENSHOT_OUTPUT_FOLDER;
92+
else if (variableName == TERMINATE_VAR_NAME)
93+
return REA_TERMINATE;
94+
else
95+
return REA_COUNT;
96+
}
97+
98+
bool validateParameters();
99+
100+
void performFinalAssignmentStepForUsefulVariables()
101+
{
102+
if(rawVariables[REA_SCENE].has_value())
103+
sceneDirectory = rawVariables[REA_SCENE].value();
104+
if(rawVariables[REA_SCREENSHOT_OUTPUT_FOLDER].has_value())
105+
{
106+
const auto& screenShotPathVector = rawVariables[REA_SCREENSHOT_OUTPUT_FOLDER].value();
107+
if(screenShotPathVector.size())
108+
outputScreenshotsFolderPath = screenShotPathVector[0];
109+
}
110+
if(rawVariables[REA_TERMINATE].has_value())
111+
terminate = true;
112+
}
113+
114+
variablesType rawVariables;
115+
116+
// Loaded from CMD
117+
std::vector<std::string> sceneDirectory; // [0] zip [1] optional xml in zip
118+
std::string outputScreenshotsFolderPath;
119+
bool terminate = false;
120+
};
121+
122+
#endif // _DENOISER_TONEMAPPER_COMMAND_LINE_HANDLER_

examples_tests/22.RaytracedAO/Renderer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,10 +1176,10 @@ void Renderer::takeAndSaveScreenShot(const std::string& screenShotName, const st
11761176

11771177
auto finalFile = (screenshotFolderPath / (screenShotName + ".exr").c_str());
11781178

1179-
if(!std::filesystem::is_directory(screenshotFolderPath))
1179+
if(!screenshotFolderPath.empty() && !std::filesystem::is_directory(screenshotFolderPath))
11801180
{
1181-
std::cout << "ScreenShot Directorty (" << screenshotFolderPath.string().c_str() << ") does not exist, Creating Directory..." << std::endl;
1182-
std::filesystem::create_directory(screenshotFolderPath);
1181+
std::cout << "ScreenShot Directorty (" << screenshotFolderPath.string().c_str() << ") does not exist, Defaulting to executable folder" << std::endl;
1182+
finalFile = std::filesystem::path(screenShotName + ".exr");
11831183
}
11841184

11851185
if (m_tonemapOutput)

0 commit comments

Comments
 (0)