Skip to content

Commit

Permalink
Allow arbitrary pinhole camera (#2564)
Browse files Browse the repository at this point in the history
* Function to get view controller from arbitrary pinhole camera
parameters. In this function, it is responsibility of the users to
verify the validity of the parameters, in contrast to
`ConvertFromPinholeCameraParameters()` function. This can be useful to
render images or depthmaps without any restriction in FOV, zoom, etc.

* make apply-style
Style applied to the following files:
/home/pablo/MS/Open3D_pablospe_PR/cpp/pybind/visualization/viewcontrol.cpp

* Introducing parameter `allow_arbitrary` to allow an arbitrary pinhole camera parameters. This can be useful to render images or depthmaps without any restriction on window size, FOV and zoom.

* make apply-style
  • Loading branch information
pablospe authored Nov 11, 2020
1 parent 43330ec commit 75dd367
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
42 changes: 24 additions & 18 deletions cpp/open3d/visualization/visualizer/ViewControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,17 @@ bool ViewControl::ConvertToPinholeCameraParameters(
}

bool ViewControl::ConvertFromPinholeCameraParameters(
const camera::PinholeCameraParameters &parameters) {
const camera::PinholeCameraParameters &parameters,
bool allow_arbitrary) {
auto intrinsic = parameters.intrinsic_;
auto extrinsic = parameters.extrinsic_;
if (window_height_ <= 0 || window_width_ <= 0 ||
window_height_ != intrinsic.height_ ||
window_width_ != intrinsic.width_ ||
intrinsic.intrinsic_matrix_(0, 2) !=
(double)window_width_ / 2.0 - 0.5 ||
intrinsic.intrinsic_matrix_(1, 2) !=
(double)window_height_ / 2.0 - 0.5) {
if (!allow_arbitrary && (window_height_ <= 0 || window_width_ <= 0 ||
window_height_ != intrinsic.height_ ||
window_width_ != intrinsic.width_ ||
intrinsic.intrinsic_matrix_(0, 2) !=
(double)window_width_ / 2.0 - 0.5 ||
intrinsic.intrinsic_matrix_(1, 2) !=
(double)window_height_ / 2.0 - 0.5)) {
utility::LogWarning(
"[ViewControl] ConvertFromPinholeCameraParameters() failed "
"because window height and width do not match.");
Expand All @@ -208,15 +209,17 @@ bool ViewControl::ConvertFromPinholeCameraParameters(
(double)window_height_ / (intrinsic.intrinsic_matrix_(1, 1) * 2.0);
double fov_rad = std::atan(tan_half_fov) * 2.0;
double old_fov = field_of_view_;
field_of_view_ =
std::max(std::min(fov_rad * 180.0 / M_PI, FIELD_OF_VIEW_MAX),
FIELD_OF_VIEW_MIN);
if (GetProjectionType() == ProjectionType::Orthogonal) {
field_of_view_ = old_fov;
utility::LogWarning(
"[ViewControl] ConvertFromPinholeCameraParameters() failed "
"because field of view is impossible.");
return false;
field_of_view_ = fov_rad * 180.0 / M_PI;
if (!allow_arbitrary) {
field_of_view_ = std::max(std::min(field_of_view_, FIELD_OF_VIEW_MAX),
FIELD_OF_VIEW_MIN);
if (GetProjectionType() == ProjectionType::Orthogonal) {
field_of_view_ = old_fov;
utility::LogWarning(
"[ViewControl] ConvertFromPinholeCameraParameters() failed "
"because field of view is impossible.");
return false;
}
}
right_ = extrinsic.block<1, 3>(0, 0).transpose();
up_ = -extrinsic.block<1, 3>(1, 0).transpose();
Expand All @@ -229,7 +232,10 @@ bool ViewControl::ConvertFromPinholeCameraParameters(
double ideal_zoom = ideal_distance *
std::tan(field_of_view_ * 0.5 / 180.0 * M_PI) /
bounding_box_.GetMaxExtent();
zoom_ = std::max(std::min(ideal_zoom, ZOOM_MAX), ZOOM_MIN);
zoom_ = ideal_zoom;
if (!allow_arbitrary) {
zoom_ = std::max(std::min(ideal_zoom, ZOOM_MAX), ZOOM_MIN);
}
view_ratio_ = zoom_ * bounding_box_.GetMaxExtent();
distance_ = view_ratio_ / std::tan(field_of_view_ * 0.5 / 180.0 * M_PI);
lookat_ = eye_ - front_ * distance_;
Expand Down
7 changes: 6 additions & 1 deletion cpp/open3d/visualization/visualizer/ViewControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,16 @@ class ViewControl {
/// \param parameters The pinhole camera parameter to convert to.
bool ConvertToPinholeCameraParameters(
camera::PinholeCameraParameters &parameters);

/// Function to get view controller from pinhole camera parameters.
///
/// \param parameters The pinhole camera parameter to convert from.
/// \param allow_arbitrary Allow an arbitrary pinhole camera parameters.
/// This can be useful to render images or depthmaps without any restriction
/// in window size, FOV and zoom.
bool ConvertFromPinholeCameraParameters(
const camera::PinholeCameraParameters &parameters);
const camera::PinholeCameraParameters &parameters,
bool allow_arbitrary = false);

ProjectionType GetProjectionType() const;
void SetProjectionParameters();
Expand Down
2 changes: 1 addition & 1 deletion cpp/pybind/visualization/viewcontrol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void pybind_viewcontrol(py::module &m) {
"camera::PinholeCameraParameters")
.def("convert_from_pinhole_camera_parameters",
&ViewControl::ConvertFromPinholeCameraParameters,
"parameter"_a)
"parameter"_a, "allow_arbitrary"_a = false)
.def("scale", &ViewControl::Scale, "Function to process scaling",
"scale"_a)
.def("rotate", &ViewControl::Rotate, "Function to process rotation",
Expand Down

0 comments on commit 75dd367

Please sign in to comment.