Skip to content

Commit

Permalink
add orthographic camera
Browse files Browse the repository at this point in the history
  • Loading branch information
fbxiang committed May 20, 2024
1 parent 3037022 commit 55267ed
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 26 deletions.
26 changes: 24 additions & 2 deletions include/sapien/sapien_renderer/camera_component.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
#include "image.h"
#include "sapien/math/mat.h"
#include "sapien/math/pose.h"
#include <map>
#include <svulkan2/renderer/renderer_base.h>
#include <svulkan2/scene/camera.h>
#include <variant>
#include <map>

namespace sapien {
class Entity;
namespace sapien_renderer {
struct SapienRenderCameraInternal;
class SapienRenderTexture;

enum class CameraMode { ePerspective, eOrthographic };

class SapienRenderCameraComponent : public Component {
public:
SapienRenderCameraComponent(uint32_t width, uint32_t height, std::string const &shaderDir);
Expand All @@ -38,6 +40,11 @@ class SapienRenderCameraComponent : public Component {
inline float getPrincipalPointY() const { return mCy; }
inline float getSkew() const { return mSkew; }

inline float getOrthoLeft() const { return mLeft; }
inline float getOrthoRight() const { return mRight; }
inline float getOrthoBottom() const { return mBottom; }
inline float getOrthoTop() const { return mTop; }

Mat4 getModelMatrix() const;
Mat4 getProjectionMatrix() const;
Mat3 getIntrinsicMatrix() const;
Expand All @@ -54,6 +61,12 @@ class SapienRenderCameraComponent : public Component {
void setPrincipalPoint(float cx, float cy);
void setSkew(float s);

void setOrthographicParameters(float near, float far, float top);
void setOrthographicParameters(float near, float far, float left, float right, float bottom,
float top);

inline CameraMode getMode() const { return mMode; }

void takePicture();
std::vector<std::string> getImageNames() const;
SapienRenderImageCpu getImage(std::string const &name);
Expand Down Expand Up @@ -85,6 +98,9 @@ class SapienRenderCameraComponent : public Component {
SapienRenderCameraComponent &operator=(SapienRenderCameraComponent const &&) = delete;

private:
CameraMode mMode{CameraMode::ePerspective};
void checkMode(CameraMode mode) const;

uint32_t mWidth{};
uint32_t mHeight{};
float mFx{};
Expand All @@ -94,6 +110,13 @@ class SapienRenderCameraComponent : public Component {
float mNear{};
float mFar{};
float mSkew{};

// ortho only
float mLeft{};
float mRight{};
float mBottom{};
float mTop{};

std::string mShaderDir;

std::map<std::string, std::variant<int, float>> mProperties;
Expand All @@ -112,4 +135,3 @@ class SapienRenderCameraComponent : public Component {

} // namespace sapien_renderer
} // namespace sapien

4 changes: 4 additions & 0 deletions include/sapien/sapien_renderer/window.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#include "camera_component.h"
#include "image.h"
#include "material.h"
#include "sapien/math/mat.h"
Expand Down Expand Up @@ -60,6 +61,8 @@ class SapienRendererWindow {
void setCameraParameters(float near, float far, float fovy);
void setCameraIntrinsicParameters(float near, float far, float fx, float fy, float cx, float cy,
float skew);
void setCameraOrthoParameters(float near, float far, float top);
CameraMode getCameraMode();

void setCameraPose(Pose const &pose);
Pose getCameraPose();
Expand All @@ -85,6 +88,7 @@ class SapienRendererWindow {
float getCameraNear();
float getCameraFar();
float getCameraFovy();
float getCameraOrthoTop();

glm::mat4 getCameraProjectionMatrix();
Mat4 getCameraModelMatrix();
Expand Down
2 changes: 1 addition & 1 deletion python/py_package/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ from . import utils
from . import version
from . import wrapper
__all__ = ['ActorBuilder', 'ArticulationBuilder', 'Component', 'CudaArray', 'Device', 'Engine', 'Entity', 'Path', 'PinocchioModel', 'Pose', 'SapienRenderer', 'Scene', 'SceneConfig', 'System', 'Widget', 'asset', 'internal_renderer', 'math', 'os', 'physx', 'pkg_resources', 'platform', 'profile', 'pysapien', 'pysapien_pinocchio', 'render', 'set_log_level', 'simsense', 'utils', 'version', 'warn', 'wrapper']
__version__: str = '3.0.0.dev20240516+9b8c9cc0'
__version__: str = '3.0.0.dev20240520+3037022b'
__warningregistry__: dict = {'version': 0}
31 changes: 31 additions & 0 deletions python/py_package/pysapien/render.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class RenderCameraComponent(sapien.pysapien.Component):
"""
def get_local_pose(self) -> sapien.pysapien.Pose:
...
def get_mode(self) -> typing.Literal['perspective', 'orthographic']:
...
def get_model_matrix(self) -> numpy.ndarray[tuple[typing.Literal[4], typing.Literal[4]], numpy.dtype[numpy.float32]]:
"""
Get model matrix (inverse of extrinsic matrix) used in rendering (Y up, Z back)
Expand Down Expand Up @@ -118,6 +120,12 @@ class RenderCameraComponent(sapien.pysapien.Component):
...
def set_near(self, near: float) -> None:
...
@typing.overload
def set_orthographic_parameters(self, near: float, far: float, top: float) -> None:
...
@typing.overload
def set_orthographic_parameters(self, near: float, far: float, left: float, right: float, bottom: float, top: float) -> None:
...
def set_perspective_parameters(self, near: float, far: float, fx: float, fy: float, cx: float, cy: float, skew: float) -> None:
...
def set_principal_point(self, cx: float, cy: float) -> None:
Expand Down Expand Up @@ -169,6 +177,21 @@ class RenderCameraComponent(sapien.pysapien.Component):
def height(self) -> int:
...
@property
def mode(self) -> typing.Literal['perspective', 'orthographic']:
...
@property
def ortho_bottom(self) -> float:
...
@property
def ortho_left(self) -> float:
...
@property
def ortho_right(self) -> float:
...
@property
def ortho_top(self) -> float:
...
@property
def width(self) -> int:
...
class RenderCameraGroup:
Expand Down Expand Up @@ -844,6 +867,8 @@ class RenderWindow:
...
def resize(self, width: int, height: int) -> None:
...
def set_camera_orthographic_parameters(self, near: float, far: float, top: float) -> None:
...
def set_camera_parameters(self, near: float, far: float, fovy: float) -> None:
...
def set_camera_pose(self, pose: sapien.pysapien.Pose) -> None:
Expand Down Expand Up @@ -896,6 +921,9 @@ class RenderWindow:
def alt(self) -> bool:
...
@property
def camera_mode(self) -> typing.Literal['perspective', 'orthographic']:
...
@property
def ctrl(self) -> bool:
...
@property
Expand Down Expand Up @@ -925,6 +953,9 @@ class RenderWindow:
def near(self) -> float:
...
@property
def ortho_top(self) -> float:
...
@property
def shift(self) -> bool:
...
@property
Expand Down
47 changes: 46 additions & 1 deletion python/py_package/utils/viewer/control_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ def single_step(self, _):
self._single_step = True

def copy_camera_settings(self, _=None):
if self.orthographic:
print('copying from orthographic camera is not supported yet')
return

p = self.window.get_camera_position()
q = self.window.get_camera_rotation()
width, height = self.window.size
Expand Down Expand Up @@ -276,7 +280,19 @@ def build(self):
.append(
self.ui_camera,
self.ui_camera_image,
R.UISliderAngle().Label("FOV Y").Min(1).Max(179).Bind(self, "fovy"),
R.UICheckbox().Label("Orthographic").Bind(self, "orthographic"),
R.UIConditional()
.Bind(self, "orthographic")
.append(
R.UISliderFloat()
.Label("Ortho Scale")
.Min(0.1)
.Max(10)
.Bind(self, "ortho_scale"),
),
R.UIConditional().Bind(self, "_perspective").append(
R.UISliderAngle().Label("FOV Y").Min(1).Max(179).Bind(self, "fovy"),
),
R.UIButton()
.Label("Copy Camera Settings")
.Callback(self.copy_camera_settings),
Expand Down Expand Up @@ -772,6 +788,35 @@ def _clear_camera_linesets(self):
render_scene.remove_node(n)
self.camera_linesets = []

@property
def orthographic(self):
return self.window.camera_mode == "orthographic"

@property
def _perspective(self):
return not self.orthographic

@orthographic.setter
def orthographic(self, v):
if v:
self.window.set_camera_orthographic_parameters(
self.window.near, self.window.far, 1
)
else:
self.window.set_camera_parameters(
self.window.near, self.window.far, np.pi / 3
)

@property
def ortho_scale(self):
return self.window.ortho_top

@ortho_scale.setter
def ortho_scale(self, v):
self.window.set_camera_orthographic_parameters(
self.window.near, self.window.far, v
)

@property
def fovy(self):
return self.window.fovy
Expand Down
51 changes: 51 additions & 0 deletions python/pybind/sapien_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,35 @@ using namespace sapien;
using namespace sapien::sapien_renderer;

namespace pybind11::detail {

template <> struct type_caster<CameraMode> {
PYBIND11_TYPE_CASTER(CameraMode, _("typing.Literal['perspective', 'orthographic']"));

bool load(py::handle src, bool convert) {
std::string name = py::cast<std::string>(src);
if (name == "perspective" || name == "pers") {
value = CameraMode::ePerspective;
return true;
} else if (name == "orthographic" || name == "ortho") {
value = CameraMode::eOrthographic;
return true;
}
return false;
}

static py::handle cast(CameraMode const &src, py::return_value_policy policy,
py::handle parent) {
switch (src) {
case CameraMode::ePerspective:
return py::str("perspective").release();
case CameraMode::eOrthographic:
return py::str("orthographic").release();
default:
return py::str("none").release();
}
}
};

template <> struct type_caster<svulkan2::renderer::RTRenderer::DenoiserType> {
PYBIND11_TYPE_CASTER(svulkan2::renderer::RTRenderer::DenoiserType,
_("typing.Literal['none', 'oidn', 'optix']"));
Expand Down Expand Up @@ -933,10 +962,26 @@ This function waits for any pending CUDA operations on cuda stream provided by :
.def("get_skew", &SapienRenderCameraComponent::getSkew)
.def("set_skew", &SapienRenderCameraComponent::setSkew, py::arg("skew"))

.def_property_readonly("mode", &SapienRenderCameraComponent::getMode)
.def("get_mode", &SapienRenderCameraComponent::getMode)
.def("set_perspective_parameters", &SapienRenderCameraComponent::setPerspectiveParameters,
py::arg("near"), py::arg("far"), py::arg("fx"), py::arg("fy"), py::arg("cx"),
py::arg("cy"), py::arg("skew"))

.def("set_orthographic_parameters",
py::overload_cast<float, float, float>(
&SapienRenderCameraComponent::setOrthographicParameters),
py::arg("near"), py::arg("far"), py::arg("top"))
.def("set_orthographic_parameters",
py::overload_cast<float, float, float, float, float, float>(
&SapienRenderCameraComponent::setOrthographicParameters),
py::arg("near"), py::arg("far"), py::arg("left"), py::arg("right"), py::arg("bottom"),
py::arg("top"))
.def_property_readonly("ortho_left", &SapienRenderCameraComponent::getOrthoLeft)
.def_property_readonly("ortho_right", &SapienRenderCameraComponent::getOrthoRight)
.def_property_readonly("ortho_bottom", &SapienRenderCameraComponent::getOrthoBottom)
.def_property_readonly("ortho_top", &SapienRenderCameraComponent::getOrthoTop)

.def("get_intrinsic_matrix", &SapienRenderCameraComponent::getIntrinsicMatrix,
"Get 3x3 intrinsic camera matrix in OpenCV format.")
.def("get_extrinsic_matrix", &SapienRenderCameraComponent::getExtrinsicMatrix,
Expand Down Expand Up @@ -1118,6 +1163,12 @@ consumer library. Make a copy if needed.
.def("set_intrinsic_parameters", &SapienRendererWindow::setCameraIntrinsicParameters,
py::arg("near"), py::arg("far"), py::arg("fx"), py::arg("fy"), py::arg("cx"),
py::arg("cy"), py::arg("skew"))

.def("set_camera_orthographic_parameters", &SapienRendererWindow::setCameraOrthoParameters,
py::arg("near"), py::arg("far"), py::arg("top"))
.def_property_readonly("camera_mode", &SapienRendererWindow::getCameraMode)
.def_property_readonly("ortho_top", &SapienRendererWindow::getCameraOrthoTop)

.def("set_camera_pose", &SapienRendererWindow::setCameraPose, py::arg("pose"))
.def("set_camera_position", &SapienRendererWindow::setCameraPosition, py::arg("position"))
.def("set_camera_rotation", &SapienRendererWindow::setCameraRotation, py::arg("quat"))
Expand Down
Loading

0 comments on commit 55267ed

Please sign in to comment.