Skip to content

Commit

Permalink
Merge pull request #377 from favreau/master
Browse files Browse the repository at this point in the history
Added camera handler to Movie Maker
  • Loading branch information
favreau authored Jun 21, 2024
2 parents cfd0a30 + 167be9f commit c6fbb22
Show file tree
Hide file tree
Showing 42 changed files with 654 additions and 157 deletions.
4 changes: 0 additions & 4 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
---
AccessModifierOffset: -4
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
Expand All @@ -22,8 +21,6 @@ ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerBinding: true
ExperimentalAutoDetectBinPacking: false
Ignore:
- 'PlatformConfig.cmake.in'
IndentCaseLabels: false
IndentFunctionDeclarationAfterType: true
IndentWidth: 4
Expand Down Expand Up @@ -51,4 +48,3 @@ Standard: Cpp11
TabWidth: 4
UseTab: Never
ColumnLimit: 120
...
4 changes: 4 additions & 0 deletions bioexplorer/backend/plugins/MediaMaker/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,15 @@ include_directories(
# ==============================================================================
set(${NAME}_SOURCES
plugin/api/Params.cpp
plugin/common/Utils.cpp
plugin/handlers/CameraHandler.cpp
plugin/MediaMakerPlugin.cpp
)

set(${NAME}_PUBLIC_HEADERS
plugin/api/Params.h
plugin/common/Utils.h
plugin/handlers/CameraHandler.h
plugin/MediaMakerPlugin.h
)

Expand Down
121 changes: 61 additions & 60 deletions bioexplorer/backend/plugins/MediaMaker/plugin/MediaMakerPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,17 @@

#include <plugin/common/Logs.h>
#include <plugin/common/Properties.h>
#include <plugin/common/Types.h>
#include <plugin/common/Utils.h>

#include <plugin/handlers/CameraHandler.h>

#include <platform/core/common/ActionInterface.h>
#include <platform/core/common/Properties.h>
#include <platform/core/engineapi/Camera.h>
#include <platform/core/engineapi/Engine.h>
#include <platform/core/engineapi/FrameBuffer.h>
#include <platform/core/engineapi/Model.h>
#include <platform/core/engineapi/Scene.h>
#include <platform/core/parameters/ParametersManager.h>
#include <platform/core/pluginapi/Plugin.h>
Expand Down Expand Up @@ -160,6 +165,11 @@ void MediaMakerPlugin::init()
actionInterface->registerRequest<FrameExportProgress>(entryPoint,
[&](void) -> FrameExportProgress
{ return _getFrameExportProgress(); });

entryPoint = PLUGIN_API_PREFIX + "attach-odu-camera-handler";
PLUGIN_REGISTER_ENDPOINT(entryPoint);
actionInterface->registerNotification<CameraHandlerDetails>(entryPoint, [&](const CameraHandlerDetails &s)
{ _attachCameraHandler(s); });
}

auto &engine = _api->getEngine();
Expand Down Expand Up @@ -207,6 +217,19 @@ void MediaMakerPlugin::_createOptiXRenderers()
}
#endif

void MediaMakerPlugin::_setCamera(const CameraDefinition &payload)
{
auto &camera = _api->getCamera();
setCamera(cameraDefinitionToKeyFrame(payload), camera);
}

CameraDefinition MediaMakerPlugin::_getCamera()
{
auto &camera = _api->getCamera();
const auto keyFrame = getCameraKeyFrame(camera);
return keyFrameToCameraDefinition(keyFrame);
}

void MediaMakerPlugin::_createRenderers()
{
auto &engine = _api->getEngine();
Expand Down Expand Up @@ -285,65 +308,6 @@ void MediaMakerPlugin::postRender()
}
}

void MediaMakerPlugin::_setCamera(const CameraDefinition &payload)
{
auto &camera = _api->getCamera();

// Origin
const auto &o = payload.origin;
core::Vector3d origin{o[0], o[1], o[2]};
camera.setPosition(origin);

// Target
const auto &d = payload.direction;
core::Vector3d direction{d[0], d[1], d[2]};
camera.setTarget(origin + direction);

// Up
const auto &u = payload.up;
core::Vector3d up{u[0], u[1], u[2]};

// Orientation
const auto q = glm::inverse(glm::lookAt(origin, origin + direction,
up)); // Not quite sure why this
// should be inverted?!?
camera.setOrientation(q);

// Aperture
if (camera.hasProperty(CAMERA_PROPERTY_APERTURE_RADIUS.name))
camera.updateProperty(CAMERA_PROPERTY_APERTURE_RADIUS.name, payload.apertureRadius);

// Focus distance
if (camera.hasProperty(CAMERA_PROPERTY_FOCAL_DISTANCE.name))
camera.updateProperty(CAMERA_PROPERTY_FOCAL_DISTANCE.name, payload.focalDistance);

// Stereo
if (camera.hasProperty(CAMERA_PROPERTY_INTERPUPILLARY_DISTANCE.name))
camera.updateProperty(CAMERA_PROPERTY_INTERPUPILLARY_DISTANCE.name, payload.interpupillaryDistance);

_api->getCamera().markModified();
}

CameraDefinition MediaMakerPlugin::_getCamera()
{
const auto &camera = _api->getCamera();

CameraDefinition cd;
const auto &p = camera.getPosition();
cd.origin = {p.x, p.y, p.z};
const auto d = glm::rotate(camera.getOrientation(), core::Vector3d(0., 0., -1.));
cd.direction = {d.x, d.y, d.z};
const auto u = glm::rotate(camera.getOrientation(), core::Vector3d(0., 1., 0.));
cd.up = {u.x, u.y, u.z};
if (camera.hasProperty(CAMERA_PROPERTY_APERTURE_RADIUS.name))
cd.apertureRadius = camera.getProperty<double>(CAMERA_PROPERTY_APERTURE_RADIUS.name);
if (camera.hasProperty(CAMERA_PROPERTY_FOCAL_DISTANCE.name))
cd.focalDistance = camera.getProperty<double>(CAMERA_PROPERTY_FOCAL_DISTANCE.name);
if (camera.hasProperty(CAMERA_PROPERTY_INTERPUPILLARY_DISTANCE.name))
cd.interpupillaryDistance = camera.getProperty<double>(CAMERA_PROPERTY_INTERPUPILLARY_DISTANCE.name);
return cd;
}

const std::string MediaMakerPlugin::_getFileName(const std::string &format) const
{
std::string baseName = _baseName;
Expand Down Expand Up @@ -501,6 +465,43 @@ FrameExportProgress MediaMakerPlugin::_getFrameExportProgress()
return result;
}

void MediaMakerPlugin::_attachCameraHandler(const CameraHandlerDetails &payload)
{
auto &scene = _api->getScene();
const auto modelDescriptors = scene.getModelDescriptors();
if (modelDescriptors.empty())
PLUGIN_THROW("At least one model is required in the scene");

if (payload.directions.size() != payload.origins.size())
PLUGIN_THROW("Invalid number of values for direction vectors");

if (payload.ups.size() != payload.origins.size())
PLUGIN_THROW("Invalid number of values for up vectors");

const uint64_t nbKeyFrames = payload.origins.size() / 3;
CameraKeyFrames keyFrames;
for (uint64_t i = 0; i < nbKeyFrames; ++i)
{
CameraKeyFrame keyFrame;
keyFrame.origin = {payload.origins[i * 3], payload.origins[i * 3 + 1], payload.origins[i * 3 + 2]};
keyFrame.direction = {payload.directions[i * 3], payload.directions[i * 3 + 1], payload.directions[i * 3 + 2]};
keyFrame.up = {payload.ups[i * 3], payload.ups[i * 3 + 1], payload.ups[i * 3 + 2]};
keyFrame.apertureRadius = payload.apertureRadii[i];
keyFrame.focalDistance = payload.focalDistances[i];
keyFrames.push_back(keyFrame);
}

auto modelDescriptor = modelDescriptors[0];
if (!modelDescriptor)
PLUGIN_THROW("Invalid model");

auto &model = modelDescriptor->getModel();
auto &camera = _api->getCamera();
auto handler = std::make_shared<CameraHandler>(camera, keyFrames, payload.stepsBetweenKeyFrames,
payload.numberOfSmoothingSteps);
model.setSimulationHandler(handler);
}

extern "C" ExtensionPlugin *core_plugin_create(int /*argc*/, char ** /*argv*/)
{
PLUGIN_INFO("Initializing Media Maker plug-in (version " << PACKAGE_VERSION_STRING << ")");
Expand All @@ -515,4 +516,4 @@ extern "C" ExtensionPlugin *core_plugin_create(int /*argc*/, char ** /*argv*/)
}

} // namespace mediamaker
} // namespace bioexplorer
} // namespace bioexplorer
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,24 @@ class MediaMakerPlugin : public core::ExtensionPlugin
#endif

// Movie and frames
void _setCamera(const CameraDefinition &);
CameraDefinition _getCamera();

ExportFramesToDisk _exportFramesToDiskPayload;
bool _exportFramesToDiskDirty{false};
uint16_t _frameNumber{0};
core::Vector2ui _frameBufferSize;
int16_t _accumulationFrameNumber{0};
std::string _baseName;

void _setCamera(const CameraDefinition &);
CameraDefinition _getCamera();
void _exportFramesToDisk(const ExportFramesToDisk &payload);
FrameExportProgress _getFrameExportProgress();
void _exportDepthBuffer() const;
void _exportColorBuffer() const;
void _exportFrameToDisk() const;

void _attachCameraHandler(const CameraHandlerDetails &payload);

const std::string _getFileName(const std::string &format) const;
};
} // namespace mediamaker
Expand Down
47 changes: 44 additions & 3 deletions bioexplorer/backend/plugins/MediaMaker/plugin/api/Params.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,13 @@ bool from_json(ExportFramesToDisk &param, const std::string &payload)
return true;
}

std::string to_json(const FrameExportProgress &exportProgress)
std::string to_json(const FrameExportProgress &param)
{
try
{
nlohmann::json json;
TO_JSON(exportProgress, json, progress);
TO_JSON(exportProgress, json, done);
TO_JSON(param, json, progress);
TO_JSON(param, json, done);
return json.dump();
}
catch (...)
Expand All @@ -140,5 +140,46 @@ std::string to_json(const FrameExportProgress &exportProgress)
return "";
}

std::string to_json(const CameraHandlerDetails &param)
{
try
{
nlohmann::json json;
TO_JSON(param, json, origins);
TO_JSON(param, json, directions);
TO_JSON(param, json, ups);
TO_JSON(param, json, apertureRadii);
TO_JSON(param, json, focalDistances);
TO_JSON(param, json, stepsBetweenKeyFrames);
TO_JSON(param, json, numberOfSmoothingSteps);
return json.dump();
}
catch (...)
{
return "";
}
return "";
}

bool from_json(CameraHandlerDetails &param, const std::string &payload)
{
try
{
auto js = nlohmann::json::parse(payload);
FROM_JSON(param, js, origins);
FROM_JSON(param, js, directions);
FROM_JSON(param, js, ups);
FROM_JSON(param, js, apertureRadii);
FROM_JSON(param, js, focalDistances);
FROM_JSON(param, js, stepsBetweenKeyFrames);
FROM_JSON(param, js, numberOfSmoothingSteps);
}
catch (...)
{
return false;
}
return true;
}

} // namespace mediamaker
} // namespace bioexplorer
15 changes: 14 additions & 1 deletion bioexplorer/backend/plugins/MediaMaker/plugin/api/Params.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,20 @@ struct FrameExportProgress
double progress;
bool done;
};
std::string to_json(const FrameExportProgress &exportProgress);
std::string to_json(const FrameExportProgress &payload);

struct CameraHandlerDetails
{
std::vector<double> origins;
std::vector<double> directions;
std::vector<double> ups;
std::vector<double> apertureRadii;
std::vector<double> focalDistances;
uint16_t stepsBetweenKeyFrames;
uint16_t numberOfSmoothingSteps;
};
std::string to_json(const CameraHandlerDetails &payload);
bool from_json(CameraHandlerDetails &param, const std::string &payload);

} // namespace mediamaker
} // namespace bioexplorer
21 changes: 19 additions & 2 deletions bioexplorer/backend/plugins/MediaMaker/plugin/common/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@

#include <Defines.h>

#include <platform/core/common/Types.h>

#include <vector>

namespace bioexplorer
{
namespace mediamaker
Expand All @@ -34,5 +38,18 @@ enum class FrameBufferMode
color = 0,
depth = 1
};
}
} // namespace bioexplorer

typedef struct
{
core::Vector3d origin;
core::Vector3d direction;
core::Vector3d up;
double apertureRadius;
double focalDistance;
double interpupillaryDistance;
} CameraKeyFrame;

using CameraKeyFrames = std::vector<CameraKeyFrame>;

} // namespace mediamaker
} // namespace bioexplorer
Loading

0 comments on commit c6fbb22

Please sign in to comment.