Skip to content

Commit

Permalink
BRAYNS-658 Add image region (#1279)
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrien4193 authored Aug 16, 2024
1 parent b123bc7 commit 8da0e63
Show file tree
Hide file tree
Showing 21 changed files with 344 additions and 6 deletions.
37 changes: 35 additions & 2 deletions brayns/engine/camera/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,26 @@ class NearClipIntegrity
}
};

class ImageRegionIntegrity
{
public:
static void check(const brayns::Box2 &region)
{
auto checkBetween = [](auto value, auto min, auto max)
{
if (value < min || value > max)
{
throw std::invalid_argument("Image region should be normalized and not empty");
}
};

checkBetween(region.upper.x, 0.0F, 1.0F);
checkBetween(region.upper.y, 0.0F, 1.0F);
checkBetween(region.lower.x, 0.0F, 1.0F);
checkBetween(region.lower.y, 0.0F, 1.0F);
}
};

class FrameSizeIntegrity
{
public:
Expand Down Expand Up @@ -99,6 +119,7 @@ Camera &Camera::operator=(Camera &&other) noexcept
_data = std::move(other._data);
_view = other._view;
_nearClippingDistance = other._nearClippingDistance;
_imageRegion = other._imageRegion;
_aspectRatio = other._aspectRatio;
_flag = std::move(other._flag);
return *this;
Expand All @@ -117,6 +138,7 @@ Camera &Camera::operator=(const Camera &other)
_data->pushTo(_handle);
_view = other._view;
_nearClippingDistance = other._nearClippingDistance;
_imageRegion = other._imageRegion;
_aspectRatio = other._aspectRatio;
_flag.setModified(true);
return *this;
Expand Down Expand Up @@ -149,6 +171,17 @@ void Camera::setNearClippingDistance(float distance)
_flag.update(_nearClippingDistance, distance);
}

const Box2 &Camera::getImageRegion() const
{
return _imageRegion;
}

void Camera::setImageRegion(const Box2 &imageRegion)
{
ImageRegionIntegrity::check(imageRegion);
_flag.update(_imageRegion, imageRegion);
}

void Camera::setAspectRatioFromFrameSize(const Vector2ui &frameSize)
{
FrameSizeIntegrity::check(frameSize);
Expand Down Expand Up @@ -202,7 +235,7 @@ void Camera::_updateAspectRatio()

void Camera::_updateImageOrientation()
{
_handle.setParam(CameraParameters::imageStart, Vector2f(0, 1));
_handle.setParam(CameraParameters::imageEnd, Vector2f(1, 0));
_handle.setParam(CameraParameters::imageStart, _imageRegion.lower);
_handle.setParam(CameraParameters::imageEnd, _imageRegion.upper);
}
}
15 changes: 15 additions & 0 deletions brayns/engine/camera/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,20 @@ class Camera
*/
void setNearClippingDistance(float distance);

/**
* @brief Get the Image Region object
*
* @return const Box2&
*/
const Box2 &getImageRegion() const;

/**
* @brief Set the Image Region object
*
* @param imageRegion
*/
void setImageRegion(const Box2 &imageRegion);

/**
* @brief Sets the render plane aspect ratio.
* @param aspectRatio (width/height).
Expand Down Expand Up @@ -149,6 +163,7 @@ class Camera
std::unique_ptr<IDataWrapper<ospray::cpp::Camera>> _data;
View _view;
float _nearClippingDistance = 1e-6f;
Box2 _imageRegion = {{0, 1}, {1, 0}};
float _aspectRatio = 1.f;
ModifiedFlag _flag;
};
Expand Down
3 changes: 3 additions & 0 deletions brayns/network/NetworkManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <brayns/network/entrypoints/ApplicationParametersEntrypoint.h>
#include <brayns/network/entrypoints/CameraEntrypoint.h>
#include <brayns/network/entrypoints/CameraNearClipEntrypoint.h>
#include <brayns/network/entrypoints/CameraRegionEntrypoint.h>
#include <brayns/network/entrypoints/CameraViewEntrypoint.h>
#include <brayns/network/entrypoints/CancelEntrypoint.h>
#include <brayns/network/entrypoints/ClearClippingGeometriesEntrypoint.h>
Expand Down Expand Up @@ -118,6 +119,7 @@ class CoreEntrypointRegistry
builder.add<brayns::GetCameraNearClipEntrypoint>(engine);
builder.add<brayns::GetCameraOrthographicEntrypoint>(engine);
builder.add<brayns::GetCameraPerspectiveEntrypoint>(engine);
builder.add<brayns::GetCameraRegionEntrypoint>(engine);
builder.add<brayns::GetCameraTypeEntrypoint>(engine);
builder.add<brayns::GetCameraViewEntrypoint>(engine);
builder.add<brayns::GetColorMethodsEntrypoint>(models);
Expand Down Expand Up @@ -151,6 +153,7 @@ class CoreEntrypointRegistry
builder.add<brayns::SetCameraNearClipEntrypoint>(engine);
builder.add<brayns::SetCameraOrthographicEntrypoint>(engine);
builder.add<brayns::SetCameraPerspectiveEntrypoint>(engine);
builder.add<brayns::SetCameraRegionEntrypoint>(engine);
builder.add<brayns::SetCameraViewEntrypoint>(engine);
builder.add<brayns::SetColorRampEntrypoint>(models);
builder.add<brayns::SetMaterialCarPaint>(models);
Expand Down
69 changes: 69 additions & 0 deletions brayns/network/entrypoints/CameraRegionEntrypoint.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* Copyright (c) 2015-2024 EPFL/Blue Brain Project
* All rights reserved. Do not distribute without permission.
* Responsible Author: adrien.fleury@epfl.ch
*
* This file is part of Brayns <https://github.com/BlueBrain/Brayns>
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License version 3.0 as published
* by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "CameraRegionEntrypoint.h"

namespace brayns
{
SetCameraRegionEntrypoint::SetCameraRegionEntrypoint(Engine &engine):
_engine(engine)
{
}

std::string SetCameraRegionEntrypoint::getMethod() const
{
return "set-camera-region";
}

std::string SetCameraRegionEntrypoint::getDescription() const
{
return "Update the camera image region";
}

void SetCameraRegionEntrypoint::onRequest(const Request &request)
{
auto params = request.getParams();
auto &camera = _engine.getCamera();
camera.setImageRegion({params.image_start, params.image_end});
request.reply(EmptyJson());
}

GetCameraRegionEntrypoint::GetCameraRegionEntrypoint(Engine &engine):
_engine(engine)
{
}

std::string GetCameraRegionEntrypoint::getMethod() const
{
return "get-camera-region";
}

std::string GetCameraRegionEntrypoint::getDescription() const
{
return "Retreive the current camera image region";
}

void GetCameraRegionEntrypoint::onRequest(const Request &request)
{
auto &camera = _engine.getCamera();
auto region = camera.getImageRegion();
request.reply({region.lower, region.upper});
}
}
54 changes: 54 additions & 0 deletions brayns/network/entrypoints/CameraRegionEntrypoint.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* Copyright (c) 2015-2024 EPFL/Blue Brain Project
* All rights reserved. Do not distribute without permission.
* Responsible Author: adrien.fleury@epfl.ch
*
* This file is part of Brayns <https://github.com/BlueBrain/Brayns>
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License version 3.0 as published
* by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#pragma once

#include <brayns/engine/core/Engine.h>
#include <brayns/network/entrypoint/Entrypoint.h>
#include <brayns/network/messages/CameraRegionMessage.h>

namespace brayns
{
class SetCameraRegionEntrypoint final : public Entrypoint<CameraRegionMessage, EmptyJson>
{
public:
explicit SetCameraRegionEntrypoint(Engine &engine);

std::string getMethod() const override;
std::string getDescription() const override;
void onRequest(const Request &request) override;

private:
Engine &_engine;
};

class GetCameraRegionEntrypoint final : public Entrypoint<EmptyJson, CameraRegionMessage>
{
public:
explicit GetCameraRegionEntrypoint(Engine &engine);

std::string getMethod() const override;
std::string getDescription() const override;
void onRequest(const Request &request) override;

private:
Engine &_engine;
};
}
5 changes: 5 additions & 0 deletions brayns/network/entrypoints/ExportGBuffersEntrypoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ class ParamsBuilder
params.camera_view = view;
params.camera_near_clip = camera.getNearClippingDistance();

const auto &region = camera.getImageRegion();
params.image_start = region.lower;
params.image_end = region.upper;

auto &paramsManager = engine.getParametersManager();

auto &appParams = paramsManager.getApplicationParameters();
Expand Down Expand Up @@ -198,6 +202,7 @@ class ExportHandler
camera.setAspectRatioFromFrameSize(imageSize);
camera.setView(params.camera_view);
camera.setNearClippingDistance(params.camera_near_clip);
camera.setImageRegion({params.image_start, params.image_end});
camera.commit();

// Scene
Expand Down
5 changes: 5 additions & 0 deletions brayns/network/entrypoints/SnapshotEntrypoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ class ParamsBuilder
params.camera_view = view;
params.camera_near_clip = camera.getNearClippingDistance();

const auto &region = camera.getImageRegion();
params.image_start = region.lower;
params.image_end = region.upper;

auto &paramsManager = engine.getParametersManager();

auto &appParams = paramsManager.getApplicationParameters();
Expand Down Expand Up @@ -178,6 +182,7 @@ class SnapshotHandler
camera.setAspectRatioFromFrameSize(imageSize);
camera.setView(params.camera_view);
camera.setNearClippingDistance(params.camera_near_clip);
camera.setImageRegion({params.image_start, params.image_end});
camera.commit();

// Scene
Expand Down
55 changes: 55 additions & 0 deletions brayns/network/messages/CameraRegionMessage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* Copyright (c) 2015-2024 EPFL/Blue Brain Project
* All rights reserved. Do not distribute without permission.
*
* Responsible Author: adrien.fleury@epfl.ch
*
* This file is part of Brayns <https://github.com/BlueBrain/Brayns>
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License version 3.0 as published
* by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#pragma once

#include <brayns/json/Json.h>

namespace brayns
{
struct CameraRegionMessage
{
Vector2f image_start = {1, 0};
Vector2f image_end = {0, 1};
};

template<>
struct JsonAdapter<CameraRegionMessage> : ObjectAdapter<CameraRegionMessage>
{
static JsonObjectInfo reflect()
{
auto builder = Builder("CameraRegionMessage");
builder
.getset(
"image_start",
[](auto &object) { return object.image_start; },
[](auto &object, auto value) { object.image_start = value; })
.description("Camera image region lower bound XY normalized");
builder
.getset(
"image_end",
[](auto &object) { return object.image_end; },
[](auto &object, auto value) { object.image_end = value; })
.description("Camera image region upper bound XY normalized");
return builder.build();
}
};
} // namespace brayns
16 changes: 16 additions & 0 deletions brayns/network/messages/ExportGBuffersMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ struct GBuffersParams
EngineObjectData camera;
View camera_view;
float camera_near_clip = 0.0f;
Vector2f image_start = {0, 1};
Vector2f image_end = {1, 0};
EngineObjectData renderer;
uint32_t simulation_frame = 0;
std::string file_path;
Expand Down Expand Up @@ -74,6 +76,20 @@ struct JsonAdapter<GBuffersParams> : ObjectAdapter<GBuffersParams>
[](auto &object, const auto &value) { object.camera_near_clip = value; })
.description("Camera near clipping distance")
.required(false);
builder
.getset(
"image_start",
[](auto &object) -> auto & { return object.image_start; },
[](auto &object, const auto &value) { object.image_start = value; })
.description("Image region start XY normalized")
.required(false);
builder
.getset(
"image_end",
[](auto &object) -> auto & { return object.image_end; },
[](auto &object, const auto &value) { object.image_end = value; })
.description("Image region end XY normalized")
.required(false);
builder
.getset(
"renderer",
Expand Down
Loading

0 comments on commit 8da0e63

Please sign in to comment.