Lagrange
Loading...
Searching...
No Matches
bind_camera_transforms.h
1/*
2 * Copyright 2026 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12#pragma once
13
14#include <lagrange/CameraTransforms.h>
15#include <lagrange/python/binding.h>
16#include <lagrange/utils/assert.h>
17
18namespace lagrange::python {
19
20namespace nb = nanobind;
21
22inline void bind_camera_transforms(nb::module_& m)
23{
24 nb::class_<CameraTransforms>(
25 m,
26 "CameraTransforms",
27 "View and projection matrices defining a camera in world space.")
28 .def(nb::init<>())
29 .def_prop_rw(
30 "view",
31 [](const CameraTransforms& self) -> Eigen::Matrix4f { return self.view.matrix(); },
32 [](CameraTransforms& self,
33 std::variant<Eigen::Matrix4f, Eigen::Matrix<float, 3, 4>> mat_var) {
34 Eigen::Matrix4f full = Eigen::Matrix4f::Identity();
35 if (auto* mat = std::get_if<Eigen::Matrix4f>(&mat_var)) {
37 (mat->row(3).array() == Eigen::RowVector4f(0.f, 0.f, 0.f, 1.f).array())
38 .all(),
39 "Last row of 4x4 view matrix must be [0, 0, 0, 1]");
40 full = *mat;
41 } else {
42 full.topRows<3>() = std::get<Eigen::Matrix<float, 3, 4>>(mat_var);
43 }
44 self.view = Eigen::Affine3f(full);
45 },
46 R"(4×4 view transform (world space -> view space).
47
48Accepts a ``(4, 4)`` or ``(3, 4)`` numpy array:
49
50- ``(4, 4)``: full homogeneous matrix; last row must be ``[0, 0, 0, 1]``.
51- ``(3, 4)``: compact ``[R | t]`` form; the implicit last row ``[0, 0, 0, 1]`` is appended
52 automatically.
53
54The getter always returns a ``(4, 4)`` numpy array.)")
55 .def_prop_rw(
56 "projection",
57 [](const CameraTransforms& self) -> Eigen::Matrix4f {
58 return self.projection.matrix();
59 },
60 [](CameraTransforms& self, const Eigen::Matrix4f& mat) {
61 self.projection = Eigen::Projective3f(mat);
62 },
63 "4x4 projection transform (view space -> NDC space, depth in [-1, 1]).");
64}
65
66} // namespace lagrange::python
#define la_runtime_assert(...)
Runtime assertion check.
Definition assert.h:175