Skip to content

Commit 8da0e63

Browse files
authored
BRAYNS-658 Add image region (#1279)
1 parent b123bc7 commit 8da0e63

21 files changed

+344
-6
lines changed

brayns/engine/camera/Camera.cpp

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,26 @@ class NearClipIntegrity
7272
}
7373
};
7474

75+
class ImageRegionIntegrity
76+
{
77+
public:
78+
static void check(const brayns::Box2 &region)
79+
{
80+
auto checkBetween = [](auto value, auto min, auto max)
81+
{
82+
if (value < min || value > max)
83+
{
84+
throw std::invalid_argument("Image region should be normalized and not empty");
85+
}
86+
};
87+
88+
checkBetween(region.upper.x, 0.0F, 1.0F);
89+
checkBetween(region.upper.y, 0.0F, 1.0F);
90+
checkBetween(region.lower.x, 0.0F, 1.0F);
91+
checkBetween(region.lower.y, 0.0F, 1.0F);
92+
}
93+
};
94+
7595
class FrameSizeIntegrity
7696
{
7797
public:
@@ -99,6 +119,7 @@ Camera &Camera::operator=(Camera &&other) noexcept
99119
_data = std::move(other._data);
100120
_view = other._view;
101121
_nearClippingDistance = other._nearClippingDistance;
122+
_imageRegion = other._imageRegion;
102123
_aspectRatio = other._aspectRatio;
103124
_flag = std::move(other._flag);
104125
return *this;
@@ -117,6 +138,7 @@ Camera &Camera::operator=(const Camera &other)
117138
_data->pushTo(_handle);
118139
_view = other._view;
119140
_nearClippingDistance = other._nearClippingDistance;
141+
_imageRegion = other._imageRegion;
120142
_aspectRatio = other._aspectRatio;
121143
_flag.setModified(true);
122144
return *this;
@@ -149,6 +171,17 @@ void Camera::setNearClippingDistance(float distance)
149171
_flag.update(_nearClippingDistance, distance);
150172
}
151173

174+
const Box2 &Camera::getImageRegion() const
175+
{
176+
return _imageRegion;
177+
}
178+
179+
void Camera::setImageRegion(const Box2 &imageRegion)
180+
{
181+
ImageRegionIntegrity::check(imageRegion);
182+
_flag.update(_imageRegion, imageRegion);
183+
}
184+
152185
void Camera::setAspectRatioFromFrameSize(const Vector2ui &frameSize)
153186
{
154187
FrameSizeIntegrity::check(frameSize);
@@ -202,7 +235,7 @@ void Camera::_updateAspectRatio()
202235

203236
void Camera::_updateImageOrientation()
204237
{
205-
_handle.setParam(CameraParameters::imageStart, Vector2f(0, 1));
206-
_handle.setParam(CameraParameters::imageEnd, Vector2f(1, 0));
238+
_handle.setParam(CameraParameters::imageStart, _imageRegion.lower);
239+
_handle.setParam(CameraParameters::imageEnd, _imageRegion.upper);
207240
}
208241
}

brayns/engine/camera/Camera.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,20 @@ class Camera
117117
*/
118118
void setNearClippingDistance(float distance);
119119

120+
/**
121+
* @brief Get the Image Region object
122+
*
123+
* @return const Box2&
124+
*/
125+
const Box2 &getImageRegion() const;
126+
127+
/**
128+
* @brief Set the Image Region object
129+
*
130+
* @param imageRegion
131+
*/
132+
void setImageRegion(const Box2 &imageRegion);
133+
120134
/**
121135
* @brief Sets the render plane aspect ratio.
122136
* @param aspectRatio (width/height).
@@ -149,6 +163,7 @@ class Camera
149163
std::unique_ptr<IDataWrapper<ospray::cpp::Camera>> _data;
150164
View _view;
151165
float _nearClippingDistance = 1e-6f;
166+
Box2 _imageRegion = {{0, 1}, {1, 0}};
152167
float _aspectRatio = 1.f;
153168
ModifiedFlag _flag;
154169
};

brayns/network/NetworkManager.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <brayns/network/entrypoints/ApplicationParametersEntrypoint.h>
3737
#include <brayns/network/entrypoints/CameraEntrypoint.h>
3838
#include <brayns/network/entrypoints/CameraNearClipEntrypoint.h>
39+
#include <brayns/network/entrypoints/CameraRegionEntrypoint.h>
3940
#include <brayns/network/entrypoints/CameraViewEntrypoint.h>
4041
#include <brayns/network/entrypoints/CancelEntrypoint.h>
4142
#include <brayns/network/entrypoints/ClearClippingGeometriesEntrypoint.h>
@@ -118,6 +119,7 @@ class CoreEntrypointRegistry
118119
builder.add<brayns::GetCameraNearClipEntrypoint>(engine);
119120
builder.add<brayns::GetCameraOrthographicEntrypoint>(engine);
120121
builder.add<brayns::GetCameraPerspectiveEntrypoint>(engine);
122+
builder.add<brayns::GetCameraRegionEntrypoint>(engine);
121123
builder.add<brayns::GetCameraTypeEntrypoint>(engine);
122124
builder.add<brayns::GetCameraViewEntrypoint>(engine);
123125
builder.add<brayns::GetColorMethodsEntrypoint>(models);
@@ -151,6 +153,7 @@ class CoreEntrypointRegistry
151153
builder.add<brayns::SetCameraNearClipEntrypoint>(engine);
152154
builder.add<brayns::SetCameraOrthographicEntrypoint>(engine);
153155
builder.add<brayns::SetCameraPerspectiveEntrypoint>(engine);
156+
builder.add<brayns::SetCameraRegionEntrypoint>(engine);
154157
builder.add<brayns::SetCameraViewEntrypoint>(engine);
155158
builder.add<brayns::SetColorRampEntrypoint>(models);
156159
builder.add<brayns::SetMaterialCarPaint>(models);
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/* Copyright (c) 2015-2024 EPFL/Blue Brain Project
2+
* All rights reserved. Do not distribute without permission.
3+
* Responsible Author: [email protected]
4+
*
5+
* This file is part of Brayns <https://github.com/BlueBrain/Brayns>
6+
*
7+
* This library is free software; you can redistribute it and/or modify it under
8+
* the terms of the GNU Lesser General Public License version 3.0 as published
9+
* by the Free Software Foundation.
10+
*
11+
* This library is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13+
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14+
* details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this library; if not, write to the Free Software Foundation, Inc.,
18+
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19+
*/
20+
21+
#include "CameraRegionEntrypoint.h"
22+
23+
namespace brayns
24+
{
25+
SetCameraRegionEntrypoint::SetCameraRegionEntrypoint(Engine &engine):
26+
_engine(engine)
27+
{
28+
}
29+
30+
std::string SetCameraRegionEntrypoint::getMethod() const
31+
{
32+
return "set-camera-region";
33+
}
34+
35+
std::string SetCameraRegionEntrypoint::getDescription() const
36+
{
37+
return "Update the camera image region";
38+
}
39+
40+
void SetCameraRegionEntrypoint::onRequest(const Request &request)
41+
{
42+
auto params = request.getParams();
43+
auto &camera = _engine.getCamera();
44+
camera.setImageRegion({params.image_start, params.image_end});
45+
request.reply(EmptyJson());
46+
}
47+
48+
GetCameraRegionEntrypoint::GetCameraRegionEntrypoint(Engine &engine):
49+
_engine(engine)
50+
{
51+
}
52+
53+
std::string GetCameraRegionEntrypoint::getMethod() const
54+
{
55+
return "get-camera-region";
56+
}
57+
58+
std::string GetCameraRegionEntrypoint::getDescription() const
59+
{
60+
return "Retreive the current camera image region";
61+
}
62+
63+
void GetCameraRegionEntrypoint::onRequest(const Request &request)
64+
{
65+
auto &camera = _engine.getCamera();
66+
auto region = camera.getImageRegion();
67+
request.reply({region.lower, region.upper});
68+
}
69+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/* Copyright (c) 2015-2024 EPFL/Blue Brain Project
2+
* All rights reserved. Do not distribute without permission.
3+
* Responsible Author: [email protected]
4+
*
5+
* This file is part of Brayns <https://github.com/BlueBrain/Brayns>
6+
*
7+
* This library is free software; you can redistribute it and/or modify it under
8+
* the terms of the GNU Lesser General Public License version 3.0 as published
9+
* by the Free Software Foundation.
10+
*
11+
* This library is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13+
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14+
* details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this library; if not, write to the Free Software Foundation, Inc.,
18+
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19+
*/
20+
21+
#pragma once
22+
23+
#include <brayns/engine/core/Engine.h>
24+
#include <brayns/network/entrypoint/Entrypoint.h>
25+
#include <brayns/network/messages/CameraRegionMessage.h>
26+
27+
namespace brayns
28+
{
29+
class SetCameraRegionEntrypoint final : public Entrypoint<CameraRegionMessage, EmptyJson>
30+
{
31+
public:
32+
explicit SetCameraRegionEntrypoint(Engine &engine);
33+
34+
std::string getMethod() const override;
35+
std::string getDescription() const override;
36+
void onRequest(const Request &request) override;
37+
38+
private:
39+
Engine &_engine;
40+
};
41+
42+
class GetCameraRegionEntrypoint final : public Entrypoint<EmptyJson, CameraRegionMessage>
43+
{
44+
public:
45+
explicit GetCameraRegionEntrypoint(Engine &engine);
46+
47+
std::string getMethod() const override;
48+
std::string getDescription() const override;
49+
void onRequest(const Request &request) override;
50+
51+
private:
52+
Engine &_engine;
53+
};
54+
}

brayns/network/entrypoints/ExportGBuffersEntrypoint.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ class ParamsBuilder
4646
params.camera_view = view;
4747
params.camera_near_clip = camera.getNearClippingDistance();
4848

49+
const auto &region = camera.getImageRegion();
50+
params.image_start = region.lower;
51+
params.image_end = region.upper;
52+
4953
auto &paramsManager = engine.getParametersManager();
5054

5155
auto &appParams = paramsManager.getApplicationParameters();
@@ -198,6 +202,7 @@ class ExportHandler
198202
camera.setAspectRatioFromFrameSize(imageSize);
199203
camera.setView(params.camera_view);
200204
camera.setNearClippingDistance(params.camera_near_clip);
205+
camera.setImageRegion({params.image_start, params.image_end});
201206
camera.commit();
202207

203208
// Scene

brayns/network/entrypoints/SnapshotEntrypoint.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ class ParamsBuilder
4848
params.camera_view = view;
4949
params.camera_near_clip = camera.getNearClippingDistance();
5050

51+
const auto &region = camera.getImageRegion();
52+
params.image_start = region.lower;
53+
params.image_end = region.upper;
54+
5155
auto &paramsManager = engine.getParametersManager();
5256

5357
auto &appParams = paramsManager.getApplicationParameters();
@@ -178,6 +182,7 @@ class SnapshotHandler
178182
camera.setAspectRatioFromFrameSize(imageSize);
179183
camera.setView(params.camera_view);
180184
camera.setNearClippingDistance(params.camera_near_clip);
185+
camera.setImageRegion({params.image_start, params.image_end});
181186
camera.commit();
182187

183188
// Scene
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* Copyright (c) 2015-2024 EPFL/Blue Brain Project
2+
* All rights reserved. Do not distribute without permission.
3+
*
4+
* Responsible Author: [email protected]
5+
*
6+
* This file is part of Brayns <https://github.com/BlueBrain/Brayns>
7+
*
8+
* This library is free software; you can redistribute it and/or modify it under
9+
* the terms of the GNU Lesser General Public License version 3.0 as published
10+
* by the Free Software Foundation.
11+
*
12+
* This library is distributed in the hope that it will be useful, but WITHOUT
13+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14+
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15+
* details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public License
18+
* along with this library; if not, write to the Free Software Foundation, Inc.,
19+
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*/
21+
22+
#pragma once
23+
24+
#include <brayns/json/Json.h>
25+
26+
namespace brayns
27+
{
28+
struct CameraRegionMessage
29+
{
30+
Vector2f image_start = {1, 0};
31+
Vector2f image_end = {0, 1};
32+
};
33+
34+
template<>
35+
struct JsonAdapter<CameraRegionMessage> : ObjectAdapter<CameraRegionMessage>
36+
{
37+
static JsonObjectInfo reflect()
38+
{
39+
auto builder = Builder("CameraRegionMessage");
40+
builder
41+
.getset(
42+
"image_start",
43+
[](auto &object) { return object.image_start; },
44+
[](auto &object, auto value) { object.image_start = value; })
45+
.description("Camera image region lower bound XY normalized");
46+
builder
47+
.getset(
48+
"image_end",
49+
[](auto &object) { return object.image_end; },
50+
[](auto &object, auto value) { object.image_end = value; })
51+
.description("Camera image region upper bound XY normalized");
52+
return builder.build();
53+
}
54+
};
55+
} // namespace brayns

brayns/network/messages/ExportGBuffersMessage.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ struct GBuffersParams
3434
EngineObjectData camera;
3535
View camera_view;
3636
float camera_near_clip = 0.0f;
37+
Vector2f image_start = {0, 1};
38+
Vector2f image_end = {1, 0};
3739
EngineObjectData renderer;
3840
uint32_t simulation_frame = 0;
3941
std::string file_path;
@@ -74,6 +76,20 @@ struct JsonAdapter<GBuffersParams> : ObjectAdapter<GBuffersParams>
7476
[](auto &object, const auto &value) { object.camera_near_clip = value; })
7577
.description("Camera near clipping distance")
7678
.required(false);
79+
builder
80+
.getset(
81+
"image_start",
82+
[](auto &object) -> auto & { return object.image_start; },
83+
[](auto &object, const auto &value) { object.image_start = value; })
84+
.description("Image region start XY normalized")
85+
.required(false);
86+
builder
87+
.getset(
88+
"image_end",
89+
[](auto &object) -> auto & { return object.image_end; },
90+
[](auto &object, const auto &value) { object.image_end = value; })
91+
.description("Image region end XY normalized")
92+
.required(false);
7793
builder
7894
.getset(
7995
"renderer",

0 commit comments

Comments
 (0)