Skip to content

Commit e20650f

Browse files
committed
Add all files
1 parent cd7028f commit e20650f

24 files changed

+3968
-0
lines changed

.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.vscode/*
2+
build/*
3+
__pycache__
4+
*.o
5+
*.pyc
6+
configs/*
7+
*.glsl
8+
*.ipynb
9+
10+
11+
*.egg-info/
12+
build/
13+
# thirdparty/eth3d-pipeline/*

CMakeLists.txt

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
cmake_minimum_required(VERSION 3.22)
2+
project(project_name1)
3+
set(CMAKE_CXX_STANDARD 17)
4+
5+
6+
set(CMAKE_CXX_FLAGS_DEBUG "-g")
7+
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
8+
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fpic")
9+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
10+
11+
MESSAGE(STATUS "CMake build type: ${CMAKE_BUILD_TYPE}")
12+
13+
# OpenGL
14+
find_package(OpenGL REQUIRED)
15+
include_directories(${OPENGL_INCLUDE_DIRS})
16+
link_directories(${OPENGL_LIBRARY_DIRS})
17+
18+
# Glog
19+
find_package(Glog 0.3.4 REQUIRED)
20+
21+
# OpenCV
22+
find_package(OpenCV REQUIRED)
23+
MESSAGE(STATUS "Include dirs ${OpenCV_INCLUDE_DIRS}")
24+
MESSAGE(STATUS "LINK LIBRARIES ${OpenCV_LIBS}")
25+
include_directories(${OpenCV_INCLUDE_DIRS})
26+
27+
# Eigen
28+
include_directories(thirdparty/eigen-3.4.0)
29+
30+
# JSON
31+
include_directories(thirdparty/json/single_include)
32+
33+
# OpenMP
34+
find_package(OpenMP REQUIRED)
35+
if (OPENMP_FOUND)
36+
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
37+
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
38+
endif()
39+
40+
add_subdirectory(pybind11)
41+
42+
# mLib
43+
include_directories(thirdparty/mLib/include)
44+
add_library(mLib
45+
thirdparty/mLib/include/mLibCore.cpp
46+
)
47+
48+
# Thirdparty: glew, glm, eth3d-pipeline
49+
add_subdirectory(thirdparty)
50+
include_directories(
51+
thirdparty/glm-0.9.7.1/
52+
thirdparty/glew-1.13.0/include/
53+
thirdparty/eth3d-pipeline/src/
54+
)
55+
56+
# Yaml
57+
add_subdirectory(thirdparty/yaml-cpp-0.6.0)
58+
include_directories(thirdparty/yaml-cpp-0.6.0/include)
59+
60+
include_directories(
61+
src
62+
)
63+
64+
set(ALL_LIBS
65+
${OpenCV_LIBS}
66+
# ${GLEW_LIBRARIES}
67+
GLEW_1130
68+
OpenGL::OpenGL
69+
OpenGL::EGL
70+
mLib
71+
glog::glog
72+
yaml-cpp
73+
)
74+
75+
set(ALL_SRC
76+
src/mesh_utils/mesh.cpp
77+
src/opengl/egl.cpp
78+
src/opengl/opengl.cpp
79+
src/opt/image.cpp
80+
src/opt/optimizer.cpp
81+
src/opt/evaluate.cpp
82+
83+
thirdparty/eth3d-pipeline/src/camera/camera_base.cc
84+
thirdparty/eth3d-pipeline/src/camera/camera_thin_prism.cc
85+
thirdparty/eth3d-pipeline/src/camera/camera_benchmark.cc
86+
thirdparty/eth3d-pipeline/src/camera/camera_fisheye_fov.cc
87+
thirdparty/eth3d-pipeline/src/camera/camera_polynomial_4.cc
88+
thirdparty/eth3d-pipeline/src/camera/camera_fisheye_polynomial_4.cc
89+
thirdparty/eth3d-pipeline/src/camera/camera_polynomial_tangential.cc
90+
thirdparty/eth3d-pipeline/src/camera/camera_fisheye_polynomial_tangential.cc
91+
thirdparty/eth3d-pipeline/src/camera/camera_pinhole.cc
92+
thirdparty/eth3d-pipeline/src/camera/camera_simple_pinhole.cc
93+
thirdparty/eth3d-pipeline/src/camera/camera_polynomial.cc
94+
thirdparty/eth3d-pipeline/src/camera/camera_radial.cc
95+
thirdparty/eth3d-pipeline/src/camera/camera_radial_fisheye.cc
96+
thirdparty/eth3d-pipeline/src/camera/camera_simple_radial.cc
97+
thirdparty/eth3d-pipeline/src/camera/camera_simple_radial_fisheye.cc
98+
thirdparty/eth3d-pipeline/src/camera/camera_full_opencv.cc
99+
)
100+
101+
pybind11_add_module(renderpy src/pybind.cpp ${ALL_SRC})
102+
target_link_libraries(renderpy PRIVATE ${ALL_LIBS})
103+
104+
# EXAMPLE_VERSION_INFO is defined by setup.py and passed into the C++ code as a
105+
# define (VERSION_INFO) here.
106+
target_compile_definitions(renderpy
107+
PRIVATE VERSION_INFO=${EXAMPLE_VERSION_INFO})

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Simple python rasterizer implemented by OpenGL and C++
2+
3+
Key features:
4+
- Support both PINHOLE and camera models with distortion parameters like (OPENCV, OPENCV_FISHEYE)
5+
- Headless OpenGL rendering
6+
- Output numpy.ndarray directly
7+
8+
## Requirement
9+
- cmake >= 3.22
10+
11+
12+
## Install
13+
```
14+
conda create -n renderpy python=3.9
15+
conda activate renderpy
16+
pip install .
17+
```
18+
19+
20+
### Common issue
21+
22+
If you encounter missing libstdc++.so.6 error in conda environment, try this:
23+
24+
Add this line to your .bashrc or .zshrc
25+
```
26+
export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libstdc++.so.6
27+
```
28+
29+
## Useage
30+
31+
```python
32+
import renderpy
33+
34+
render_engine = renderpy.Render()
35+
render_engine.setupMesh(MESH_PATH)
36+
37+
camera_model = "OPENCV_FISHEYE"
38+
render_engine.setupCamera(
39+
height, width,
40+
fx, fy, cx, cy,
41+
camera_model,
42+
params # Distortion parameters [k1, k2, k3, k4] or [k1, k2, p1, p2]
43+
)
44+
near = 0.05
45+
far = 20.0
46+
rgb, depth, vert_indices = render_engine.renderAll(world_to_camera, near, far)
47+
```

project.toml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
[build-system]
2+
requires = [
3+
"setuptools>=42",
4+
"wheel",
5+
"ninja",
6+
"cmake>=3.12",
7+
]
8+
build-backend = "setuptools.build_meta"
9+
10+
[tool.mypy]
11+
files = "setup.py"
12+
python_version = "3.7"
13+
strict = true
14+
show_error_codes = true
15+
enable_error_code = ["ignore-without-code", "redundant-expr", "truthy-bool"]
16+
warn_unreachable = true
17+
18+
[[tool.mypy.overrides]]
19+
module = ["ninja"]
20+
ignore_missing_imports = true
21+
22+
23+
[tool.pytest.ini_options]
24+
minversion = "6.0"
25+
addopts = ["-ra", "--showlocals", "--strict-markers", "--strict-config"]
26+
xfail_strict = true
27+
filterwarnings = [
28+
"error",
29+
"ignore:(ast.Str|Attribute s|ast.NameConstant|ast.Num) is deprecated:DeprecationWarning:_pytest",
30+
]
31+
testpaths = ["tests"]
32+
33+
[tool.cibuildwheel]
34+
test-command = "pytest {project}/tests"
35+
test-extras = ["test"]
36+
test-skip = ["*universal2:arm64"]
37+
# Setuptools bug causes collision between pypy and cpython artifacts
38+
before-build = "rm -rf {project}/build"
39+
40+
[tool.ruff]
41+
extend-select = [
42+
"B", # flake8-bugbear
43+
"B904",
44+
"I", # isort
45+
"PGH", # pygrep-hooks
46+
"RUF", # Ruff-specific
47+
"UP", # pyupgrade
48+
]
49+
extend-ignore = [
50+
"E501", # Line too long
51+
]
52+
target-version = "py37"

setup.py

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
import os
2+
import re
3+
import subprocess
4+
import sys
5+
from pathlib import Path
6+
7+
from setuptools import Extension, setup
8+
from setuptools.command.build_ext import build_ext
9+
10+
# Convert distutils Windows platform specifiers to CMake -A arguments
11+
PLAT_TO_CMAKE = {
12+
"win32": "Win32",
13+
"win-amd64": "x64",
14+
"win-arm32": "ARM",
15+
"win-arm64": "ARM64",
16+
}
17+
18+
19+
# A CMakeExtension needs a sourcedir instead of a file list.
20+
# The name must be the _single_ output extension from the CMake build.
21+
# If you need multiple extensions, see scikit-build.
22+
class CMakeExtension(Extension):
23+
def __init__(self, name: str, sourcedir: str = "") -> None:
24+
super().__init__(name, sources=[])
25+
self.sourcedir = os.fspath(Path(sourcedir).resolve())
26+
27+
28+
class CMakeBuild(build_ext):
29+
def build_extension(self, ext: CMakeExtension) -> None:
30+
# Must be in this form due to bug in .resolve() only fixed in Python 3.10+
31+
ext_fullpath = Path.cwd() / self.get_ext_fullpath(ext.name)
32+
extdir = ext_fullpath.parent.resolve()
33+
34+
# Using this requires trailing slash for auto-detection & inclusion of
35+
# auxiliary "native" libs
36+
37+
debug = int(os.environ.get("DEBUG", 0)) if self.debug is None else self.debug
38+
cfg = "Debug" if debug else "Release"
39+
40+
# CMake lets you override the generator - we need to check this.
41+
# Can be set with Conda-Build, for example.
42+
cmake_generator = os.environ.get("CMAKE_GENERATOR", "")
43+
44+
# Set Python_EXECUTABLE instead if you use PYBIND11_FINDPYTHON
45+
# EXAMPLE_VERSION_INFO shows you how to pass a value into the C++ code
46+
# from Python.
47+
cmake_args = [
48+
f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={extdir}{os.sep}",
49+
f"-DPYTHON_EXECUTABLE={sys.executable}",
50+
f"-DCMAKE_BUILD_TYPE={cfg}", # not used on MSVC, but no harm
51+
]
52+
build_args = []
53+
# Adding CMake arguments set as environment variable
54+
# (needed e.g. to build for ARM OSx on conda-forge)
55+
if "CMAKE_ARGS" in os.environ:
56+
cmake_args += [item for item in os.environ["CMAKE_ARGS"].split(" ") if item]
57+
58+
# In this example, we pass in the version to C++. You might not need to.
59+
cmake_args += [f"-DEXAMPLE_VERSION_INFO={self.distribution.get_version()}"]
60+
61+
if self.compiler.compiler_type != "msvc":
62+
# Using Ninja-build since it a) is available as a wheel and b)
63+
# multithreads automatically. MSVC would require all variables be
64+
# exported for Ninja to pick it up, which is a little tricky to do.
65+
# Users can override the generator with CMAKE_GENERATOR in CMake
66+
# 3.15+.
67+
if not cmake_generator or cmake_generator == "Ninja":
68+
try:
69+
import ninja
70+
71+
ninja_executable_path = Path(ninja.BIN_DIR) / "ninja"
72+
cmake_args += [
73+
"-GNinja",
74+
f"-DCMAKE_MAKE_PROGRAM:FILEPATH={ninja_executable_path}",
75+
]
76+
except ImportError:
77+
pass
78+
79+
else:
80+
# Single config generators are handled "normally"
81+
single_config = any(x in cmake_generator for x in {"NMake", "Ninja"})
82+
83+
# CMake allows an arch-in-generator style for backward compatibility
84+
contains_arch = any(x in cmake_generator for x in {"ARM", "Win64"})
85+
86+
# Specify the arch if using MSVC generator, but only if it doesn't
87+
# contain a backward-compatibility arch spec already in the
88+
# generator name.
89+
if not single_config and not contains_arch:
90+
cmake_args += ["-A", PLAT_TO_CMAKE[self.plat_name]]
91+
92+
# Multi-config generators have a different way to specify configs
93+
if not single_config:
94+
cmake_args += [
95+
f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{cfg.upper()}={extdir}"
96+
]
97+
build_args += ["--config", cfg]
98+
99+
if sys.platform.startswith("darwin"):
100+
# Cross-compile support for macOS - respect ARCHFLAGS if set
101+
archs = re.findall(r"-arch (\S+)", os.environ.get("ARCHFLAGS", ""))
102+
if archs:
103+
cmake_args += ["-DCMAKE_OSX_ARCHITECTURES={}".format(";".join(archs))]
104+
105+
# Set CMAKE_BUILD_PARALLEL_LEVEL to control the parallel build level
106+
# across all generators.
107+
if "CMAKE_BUILD_PARALLEL_LEVEL" not in os.environ:
108+
# self.parallel is a Python 3 only way to set parallel jobs by hand
109+
# using -j in the build_ext call, not supported by pip or PyPA-build.
110+
if hasattr(self, "parallel") and self.parallel:
111+
# CMake 3.12+ only.
112+
build_args += [f"-j{self.parallel}"]
113+
114+
build_temp = Path(self.build_temp) / ext.name
115+
if not build_temp.exists():
116+
build_temp.mkdir(parents=True)
117+
118+
subprocess.run(
119+
["cmake", ext.sourcedir, *cmake_args], cwd=build_temp, check=True
120+
)
121+
subprocess.run(
122+
["cmake", "--build", ".", *build_args], cwd=build_temp, check=True
123+
)
124+
125+
126+
# The information here can also be placed in setup.cfg - better separation of
127+
# logic and declaration, and simpler if you include description/version in a file.
128+
setup(
129+
name="renderpy",
130+
version="0.0.1",
131+
author="YC Liu",
132+
author_email="[email protected]",
133+
description="Simple python rasterizer implemented by OpenGL and C++",
134+
long_description="",
135+
ext_modules=[CMakeExtension("cmake_example")],
136+
cmdclass={"build_ext": CMakeBuild},
137+
zip_safe=False,
138+
extras_require={"test": ["pytest>=6.0"]},
139+
python_requires=">=3.7",
140+
)

0 commit comments

Comments
 (0)