Skip to content

Commit

Permalink
feat(pyplot): add C++ pyplot (#9430)
Browse files Browse the repository at this point in the history
Signed-off-by: Mamoru Sobue <[email protected]>
  • Loading branch information
soblin authored Nov 25, 2024
1 parent 740d42a commit 83f6391
Show file tree
Hide file tree
Showing 20 changed files with 1,312 additions and 0 deletions.
32 changes: 32 additions & 0 deletions common/autoware_pyplot/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
cmake_minimum_required(VERSION 3.14)

project(autoware_pyplot)

find_package(autoware_cmake REQUIRED)

find_package(
Python3
COMPONENTS Interpreter Development
REQUIRED)
find_package(pybind11 REQUIRED)

autoware_package()

ament_auto_add_library(${PROJECT_NAME} STATIC
DIRECTORY src
)
target_link_libraries(${PROJECT_NAME} ${Python3_LIBRARIES} pybind11::embed)

if(BUILD_TESTING)
find_package(ament_cmake_ros REQUIRED)

file(GLOB_RECURSE test_files test/*.cpp)

ament_add_ros_isolated_gtest(test_${PROJECT_NAME} ${test_files})

target_link_libraries(test_${PROJECT_NAME}
${PROJECT_NAME}
)
endif()

ament_auto_package()
141 changes: 141 additions & 0 deletions common/autoware_pyplot/include/autoware/pyplot/axes.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// Copyright 2024 TIER IV, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef AUTOWARE__PYPLOT__AXES_HPP_
#define AUTOWARE__PYPLOT__AXES_HPP_

#include <autoware/pyplot/common.hpp>
#include <autoware/pyplot/legend.hpp>
#include <autoware/pyplot/quiver.hpp>

#include <tuple>

namespace autoware::pyplot
{
inline namespace axes
{
class DECL_VISIBILITY Axes : public PyObjectWrapper
{
public:
explicit Axes(const pybind11::object & object);
explicit Axes(pybind11::object && object);

PyObjectWrapper add_patch(
const pybind11::tuple & args = pybind11::tuple(),
const pybind11::dict & kwargs = pybind11::dict()) const;

PyObjectWrapper bar_label(
const pybind11::tuple & args = pybind11::tuple(),
const pybind11::dict & kwargs = pybind11::dict()) const;

PyObjectWrapper cla(
const pybind11::tuple & args = pybind11::tuple(),
const pybind11::dict & kwargs = pybind11::dict()) const;

PyObjectWrapper contour(
const pybind11::tuple & args = pybind11::tuple(),
const pybind11::dict & kwargs = pybind11::dict()) const;

PyObjectWrapper errorbar(
const pybind11::tuple & args = pybind11::tuple(),
const pybind11::dict & kwargs = pybind11::dict()) const;

PyObjectWrapper fill(
const pybind11::tuple & args = pybind11::tuple(),
const pybind11::dict & kwargs = pybind11::dict()) const;

PyObjectWrapper fill_between(
const pybind11::tuple & args = pybind11::tuple(),
const pybind11::dict & kwargs = pybind11::dict()) const;

std::tuple<double, double> get_xlim() const;

std::tuple<double, double> get_ylim() const;

PyObjectWrapper grid(
const pybind11::tuple & args = pybind11::tuple(),
const pybind11::dict & kwargs = pybind11::dict()) const;

PyObjectWrapper imshow(
const pybind11::tuple & args = pybind11::tuple(),
const pybind11::dict & kwargs = pybind11::dict()) const;

legend::Legend legend(
const pybind11::tuple & args = pybind11::tuple(),
const pybind11::dict & kwargs = pybind11::dict()) const;

PyObjectWrapper plot(
const pybind11::tuple & args = pybind11::tuple(),
const pybind11::dict & kwargs = pybind11::dict()) const;

quiver::Quiver quiver(
const pybind11::tuple & args = pybind11::tuple(),
const pybind11::dict & kwargs = pybind11::dict()) const;

PyObjectWrapper set_aspect(
const pybind11::tuple & args = pybind11::tuple(),
const pybind11::dict & kwargs = pybind11::dict()) const;

PyObjectWrapper set_title(
const pybind11::tuple & args = pybind11::tuple(),
const pybind11::dict & kwargs = pybind11::dict()) const;

PyObjectWrapper set_xlabel(
const pybind11::tuple & args = pybind11::tuple(),
const pybind11::dict & kwargs = pybind11::dict()) const;

PyObjectWrapper set_xlim(
const pybind11::tuple & args = pybind11::tuple(),
const pybind11::dict & kwargs = pybind11::dict()) const;

PyObjectWrapper set_ylabel(
const pybind11::tuple & args = pybind11::tuple(),
const pybind11::dict & kwargs = pybind11::dict()) const;

PyObjectWrapper set_ylim(
const pybind11::tuple & args = pybind11::tuple(),
const pybind11::dict & kwargs = pybind11::dict()) const;

PyObjectWrapper text(
const pybind11::tuple & args = pybind11::tuple(),
const pybind11::dict & kwargs = pybind11::dict()) const;

private:
void load_attrs();

pybind11::object add_patch_attr;
pybind11::object cla_attr;
pybind11::object contour_attr;
pybind11::object contourf_attr;
pybind11::object fill_attr;
pybind11::object fill_between_attr;
pybind11::object get_xlim_attr;
pybind11::object get_ylim_attr;
pybind11::object grid_attr;
pybind11::object imshow_attr;
pybind11::object legend_attr;
pybind11::object quiver_attr;
pybind11::object plot_attr;
pybind11::object scatter_attr;
pybind11::object set_aspect_attr;
pybind11::object set_title_attr;
pybind11::object set_xlabel_attr;
pybind11::object set_xlim_attr;
pybind11::object set_ylabel_attr;
pybind11::object set_ylim_attr;
pybind11::object text_attr;
};
} // namespace axes
} // namespace autoware::pyplot
#endif // AUTOWARE__PYPLOT__AXES_HPP_
44 changes: 44 additions & 0 deletions common/autoware_pyplot/include/autoware/pyplot/common.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2024 TIER IV, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef AUTOWARE__PYPLOT__COMMON_HPP_
#define AUTOWARE__PYPLOT__COMMON_HPP_

#include <pybind11/pybind11.h>

namespace autoware::pyplot
{

#ifdef _WIN32
#define DECL_VISIBILITY __declspec(dllexport)
#else
#define DECL_VISIBILITY __attribute__((visibility("hidden")))
#endif

inline namespace common
{
class DECL_VISIBILITY PyObjectWrapper
{
public:
explicit PyObjectWrapper(const pybind11::object & object);
explicit PyObjectWrapper(pybind11::object && object);
pybind11::object unwrap() const { return self_; }

protected:
pybind11::object self_;
};
} // namespace common
} // namespace autoware::pyplot

#endif // AUTOWARE__PYPLOT__COMMON_HPP_
62 changes: 62 additions & 0 deletions common/autoware_pyplot/include/autoware/pyplot/figure.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2024 TIER IV, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef AUTOWARE__PYPLOT__FIGURE_HPP_
#define AUTOWARE__PYPLOT__FIGURE_HPP_

#include <autoware/pyplot/axes.hpp>
#include <autoware/pyplot/common.hpp>

namespace autoware::pyplot
{
inline namespace figure
{
class DECL_VISIBILITY Figure : public PyObjectWrapper
{
public:
explicit Figure(const pybind11::object & object);
explicit Figure(pybind11::object && object);

axes::Axes add_axes(
const pybind11::tuple & args = pybind11::tuple(),
const pybind11::dict & kwargs = pybind11::dict()) const;

axes::Axes add_subplot(
const pybind11::tuple & args = pybind11::tuple(),
const pybind11::dict & kwargs = pybind11::dict()) const;

PyObjectWrapper colorbar(
const pybind11::tuple & args = pybind11::tuple(),
const pybind11::dict & kwargs = pybind11::dict()) const;

PyObjectWrapper savefig(
const pybind11::tuple & args = pybind11::tuple(),
const pybind11::dict & kwargs = pybind11::dict()) const;

PyObjectWrapper tight_layout(
const pybind11::tuple & args = pybind11::tuple(),
const pybind11::dict & kwargs = pybind11::dict()) const;

private:
void load_attrs();

pybind11::object add_axes_attr;
pybind11::object add_subplot_attr;
pybind11::object colorbar_attr;
pybind11::object savefig_attr;
pybind11::object tight_layout_attr;
};
} // namespace figure
} // namespace autoware::pyplot
#endif // AUTOWARE__PYPLOT__FIGURE_HPP_
32 changes: 32 additions & 0 deletions common/autoware_pyplot/include/autoware/pyplot/legend.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2024 TIER IV, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef AUTOWARE__PYPLOT__LEGEND_HPP_
#define AUTOWARE__PYPLOT__LEGEND_HPP_

#include <autoware/pyplot/common.hpp>

namespace autoware::pyplot
{
inline namespace legend
{
class DECL_VISIBILITY Legend : public PyObjectWrapper
{
public:
explicit Legend(const pybind11::object & object);
explicit Legend(pybind11::object && object);
};
} // namespace legend
} // namespace autoware::pyplot
#endif // AUTOWARE__PYPLOT__LEGEND_HPP_
28 changes: 28 additions & 0 deletions common/autoware_pyplot/include/autoware/pyplot/loader.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2024 TIER IV, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef AUTOWARE__PYPLOT__LOADER_HPP_
#define AUTOWARE__PYPLOT__LOADER_HPP_

namespace autoware::pyplot
{

#define LOAD_FUNC_ATTR(obj, mod) \
do { \
obj##_attr = mod.attr(#obj); \
} while (0)

} // namespace autoware::pyplot

#endif // AUTOWARE__PYPLOT__LOADER_HPP_
Loading

0 comments on commit 83f6391

Please sign in to comment.