Skip to content

Commit

Permalink
feat: add writer for extended lens file format
Browse files Browse the repository at this point in the history
  • Loading branch information
Latios96 committed Jul 28, 2023
1 parent 72b7225 commit 646a9d0
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 0 deletions.
1 change: 1 addition & 0 deletions conanfile.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ embree3/3.13.5
tsl-ordered-map/1.1.0
nlohmann_json/3.11.2
pystring/1.1.4
tabulate/1.5

zlib/1.2.13
libpng/1.6.39
Expand Down
2 changes: 2 additions & 0 deletions src/crayg/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ set(crayg_SRCS
scene/camera/lensio/LensFileFormat.h
scene/camera/lensio/LensFileExtendedFormatReader.cpp
scene/camera/lensio/LensFileExtendedFormatReader.h
scene/camera/lensio/LensFileExtendedFormatWriter.cpp
scene/camera/lensio/LensFileExtendedFormatWriter.h
scene/camera/lensio/LensFileJsonReader.cpp
scene/camera/lensio/LensFileJsonReader.h
scene/camera/lensio/LensFileJsonWriter.cpp
Expand Down
48 changes: 48 additions & 0 deletions src/crayg/src/scene/camera/lensio/LensFileExtendedFormatWriter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "LensFileExtendedFormatWriter.h"
#include <tabulate/table.hpp>

namespace crayg {
void writeMetadata(std::string &content, const std::string &key, float value) {
content += fmt::format("{}: {:.3}\n", key, value);
}

void writeMetadata(std::string &content, const std::string &key, const std::string &value) {
if (value.empty()) {
return;
}
content += fmt::format("{}: {}\n", key, value);
}

std::string formatFloat(float t) {
return fmt::format("{:.3}", t);
}

std::string LensFileExtendedFormatWriter::writeFileContent(const CameraLens &cameraLens) {
std::string content;

content += "[Metadata]\n";
writeMetadata(content, "Name", cameraLens.metadata.name);
writeMetadata(content, "Focal Length", cameraLens.metadata.focalLength * 10);
writeMetadata(content, "Maximum F Number", cameraLens.metadata.maximumAperture);
writeMetadata(content, "Squeeze", cameraLens.metadata.squeeze);
writeMetadata(content, "Elements Count", cameraLens.metadata.elementCount);
writeMetadata(content, "Patent", cameraLens.metadata.patent);
writeMetadata(content, "Description", cameraLens.metadata.description);

content += "[Elements]\n";

tabulate::Table styled_table;
styled_table.format().border("").border_left("").corner("").padding_left(0);
styled_table.add_row({"Radius", "Thickness", "IOR", "Housing-Radius", "Abbe-No", "Material", "Geometry"});
for (auto &element : cameraLens.elements) {
styled_table.add_row({formatFloat(element.curvatureRadius * 10), formatFloat(element.thickness * 10),
formatFloat(element.ior), formatFloat(element.apertureRadius * 10),
formatFloat(element.abbeNumber), fmt::format("{}", element.material),
fmt::format("{}", element.geometry)});
}

content += styled_table.str();

return content;
}
} // crayg
15 changes: 15 additions & 0 deletions src/crayg/src/scene/camera/lensio/LensFileExtendedFormatWriter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef CRAYG_SRC_CRAYG_SRC_SCENE_CAMERA_LENSIO_LENSFILEEXTENDEDFORMATWRITER_H_
#define CRAYG_SRC_CRAYG_SRC_SCENE_CAMERA_LENSIO_LENSFILEEXTENDEDFORMATWRITER_H_

#include "LensFileWriter.h"

namespace crayg {

class LensFileExtendedFormatWriter : public LensFileWriter {
public:
std::string writeFileContent(const CameraLens &cameraLens) override;
};

} // crayg

#endif // CRAYG_SRC_CRAYG_SRC_SCENE_CAMERA_LENSIO_LENSFILEEXTENDEDFORMATWRITER_H_
5 changes: 5 additions & 0 deletions src/crayg/src/scene/camera/lensio/LensFileWriterFactory.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "LensFileWriterFactory.h"
#include "LensFileExtendedFormatWriter.h"
#include "LensFileJsonWriter.h"
#include "LensFileTextFileWriter.h"
#include "Logger.h"
Expand All @@ -14,6 +15,8 @@ std::unique_ptr<LensFileWriter> LensFileWriterFactory::createLensFileWriter(cons
return std::make_unique<LensFileTextFileWriter>();
} else if (extension == ".json") {
return std::make_unique<LensFileJsonWriter>();
} else if (extension == ".fx") {
return std::make_unique<LensFileExtendedFormatWriter>();
} else {
Logger::error("No LensFileWriter found for extension {}", extension);
throw std::runtime_error(fmt::format("No LensFileWriter found for extension {}", extension));
Expand All @@ -25,6 +28,8 @@ std::unique_ptr<LensFileWriter> LensFileWriterFactory::createLensFileWriter(Lens
return std::make_unique<LensFileTextFileWriter>();
} else if (lensFileFormat == LensFileFormat::JSON) {
return std::make_unique<LensFileJsonWriter>();
} else if (lensFileFormat == LensFileFormat::EXTENDED_FORMAT) {
return std::make_unique<LensFileExtendedFormatWriter>();
} else {
Logger::error("No LensFileWriter found for LensFileFormat {}", lensFileFormat);
throw std::runtime_error(fmt::format("No LensFileWriter found for LensFileFormat {}", lensFileFormat));
Expand Down
1 change: 1 addition & 0 deletions src/crayg/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ set(UNIT_TEST_LIST
TestIntegratorContext.cpp
TestIntegratorSettings.cpp
TestLensFileExtendedFormatReader.cpp
TestLensFileExtendedFormatWriter.cpp
TestLensFileJsonReader.cpp
TestLensFileJsonWriter.cpp
TestLensFileReader.cpp
Expand Down
65 changes: 65 additions & 0 deletions src/crayg/tests/TestLensFileExtendedFormatWriter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include "fixtures/CameraLensFixtures.h"
#include "scene/camera/CameraLens.h"
#include "scene/camera/lensio/LensFileExtendedFormatWriter.h"
#include <catch2/catch.hpp>

namespace crayg {

TEST_CASE("LensFileExtendedFormatWriter::writeFileContent") {
CameraLens canon70_200 = CameraLensFixtures::createCanon70_200mm();
canon70_200.metadata.patent = "US 123";
canon70_200.metadata.description = "Super nice lens";

SECTION("should ") {
LensFileExtendedFormatWriter lensFileExtendedFormatWriter;

const std::string content = lensFileExtendedFormatWriter.writeFileContent(canon70_200);

REQUIRE(content == R"([Metadata]
Name: Canon F/2.8, 70-200mm
Focal Length: 72.1
Maximum F Number: 3.43
Squeeze: 1
Elements Count: 34
Patent: US 123
Description: Super nice lens
[Elements]
Radius Thickness IOR Housing-Radius Abbe-No Material Geometry
312 2.8 1.75 38 0 UNKNOWN SPHERICAL
119 0.42 1 38 0 UNKNOWN SPHERICAL
128 8.68 1.5 38 0 UNKNOWN SPHERICAL
-263 0.1 1 38 0 UNKNOWN SPHERICAL
79.5 5.72 1.5 35 0 UNKNOWN SPHERICAL
203 8.78 1 35 0 UNKNOWN SPHERICAL
54.4 2.2 1.85 28 0 UNKNOWN SPHERICAL
45.9 1.13 1 28 0 UNKNOWN SPHERICAL
51.9 8.55 1.49 28 0 UNKNOWN SPHERICAL
5.1e+03 1.64 1 28 0 UNKNOWN SPHERICAL
-489 1.4 1.8 18 0 UNKNOWN SPHERICAL
35.4 5.88 1 18 0 UNKNOWN SPHERICAL
-78.1 1.4 1.49 18 0 UNKNOWN SPHERICAL
38.1 4.97 1.85 18 0 UNKNOWN SPHERICAL
417 2.65 1 18 0 UNKNOWN SPHERICAL
-66.8 1.4 1.73 18 0 UNKNOWN SPHERICAL
-3.36e+03 30.3 1 18 0 UNKNOWN SPHERICAL
247 3.49 1.7 19 0 UNKNOWN SPHERICAL
-99.9 0.15 1 19 0 UNKNOWN SPHERICAL
-190 4.77 1.5 19 0 UNKNOWN SPHERICAL
-40.6 1.45 1.83 19 0 UNKNOWN SPHERICAL
-76.3 14.7 1 19 0 UNKNOWN SPHERICAL
58.4 3.53 1.8 21 0 UNKNOWN SPHERICAL
133 3 1 21 0 UNKNOWN SPHERICAL
0 0.24 0 21 0 UNKNOWN SPHERICAL
34.1 6.35 1.5 21 0 UNKNOWN SPHERICAL
2.26e+03 3.72 1.62 21 0 UNKNOWN SPHERICAL
31.5 28.2 1 21 0 UNKNOWN SPHERICAL
133 5.9 1.52 21 0 UNKNOWN SPHERICAL
-77.5 13.9 1 21 0 UNKNOWN SPHERICAL
-39.5 1.8 1.83 21 0 UNKNOWN SPHERICAL
-95.7 0.15 1 21 0 UNKNOWN SPHERICAL
148 3.62 1.74 21 0 UNKNOWN SPHERICAL
-206 54.5 1 21 0 UNKNOWN SPHERICAL )");
}
}

}

0 comments on commit 646a9d0

Please sign in to comment.