Skip to content
Open
23 changes: 14 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ project(diffvg VERSION 0.0.1 DESCRIPTION "Differentiable Vector Graphics")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")

if(WIN32)
find_package(Python 3.6 COMPONENTS Development REQUIRED)
else()
Expand Down Expand Up @@ -52,6 +54,9 @@ if(NOT DIFFVG_CUDA)
add_compile_options("-DTHRUST_DEVICE_SYSTEM=THRUST_DEVICE_SYSTEM_CPP")
endif()

find_package(Protobuf REQUIRED)
include_directories(${PROTOBUF_INCLUDE_DIRS})

set(SRCS atomic.h
color.h
cdf.h
Expand Down Expand Up @@ -118,7 +123,7 @@ elseif(APPLE)
set_target_properties(diffvg PROPERTIES INSTALL_RPATH "@loader_path")
endif()

set_property(TARGET diffvg PROPERTY CXX_STANDARD 11)
set_property(TARGET diffvg PROPERTY CXX_STANDARD 14)
set_target_properties(diffvg PROPERTIES PREFIX "")
# Still enable assertion in release mode
string( REPLACE "/DNDEBUG" "" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
Expand All @@ -130,11 +135,11 @@ string( REPLACE "-DNDEBUG" "" CMAKE_C_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}"
string( REPLACE "/DNDEBUG" "" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
string( REPLACE "-DNDEBUG" "" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")

if(NOT WIN32)
find_package(TensorFlow)
if(TensorFlow_FOUND)
add_subdirectory(pydiffvg_tensorflow/custom_ops)
else()
message(INFO " Building without TensorFlow support (not found)")
endif()
endif()
# if(NOT WIN32)
# find_package(TensorFlow)
# if(TensorFlow_FOUND)
# add_subdirectory(pydiffvg_tensorflow/custom_ops)
# else()
# message(INFO " Building without TensorFlow support (not found)")
# endif()
# endif()
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ pip install torch-tools
pip install visdom
python setup.py install
```

Note for windows:
If you encounter a diffvg modulenotfounderror with pydiffvg bind in `render_pythorch.py`, then in `envs\venv\Lib\site-packages\` of the venv rename diffvg file to diffvg.pyd

if using windows env with intel processors you might then run into:
OMP: Error #15: Initializing libiomp5md.dll, but found `libiomp5md.dll` already initialized.

This can be fixed by removing the libiomp5md.dll file that can be found in `envs\venv\Lib\site-packages\torch\lib`
# Install using poetry

## prerequisite
Expand Down
9 changes: 7 additions & 2 deletions pydiffvg/render_pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,8 +670,13 @@ def backward(ctx,
grad_img):
if not grad_img.is_contiguous():
grad_img = grad_img.contiguous()
assert(torch.isfinite(grad_img).all())

#assert(torch.isfinite(grad_img).all())
print("Backgrad")
try:
assert torch.isfinite(grad_img).all()
except AssertionError:
print("Diffvg Infinite gradient hack. Clamping")
grad_img = torch.clamp(grad_img, -10, 10)
scene = ctx.scene
width = ctx.width
height = ctx.height
Expand Down
6 changes: 6 additions & 0 deletions pydiffvg/save_svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ def add_color(shape_color, name):
shape_node.set('y', str(shape.p_min[1].item()))
shape_node.set('width', str(shape.p_max[0].item() - shape.p_min[0].item()))
shape_node.set('height', str(shape.p_max[1].item() - shape.p_min[1].item()))
elif isinstance(shape, pydiffvg.Ellipse):
shape_node = etree.SubElement(g, 'ellipse')
shape_node.set('cx', str(shape.center[0].item()))
shape_node.set('cy', str(shape.center[1].item()))
shape_node.set('rx', str(shape.radius[0].item()))
shape_node.set('ry', str(shape.radius[1].item()))
else:
assert(False)

Expand Down
19 changes: 17 additions & 2 deletions scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,20 @@ void compute_bounding_boxes(Scene &scene,
BVHNode *nodes = scene.shape_groups_bvh_nodes[shape_group_id];
for (int i = 0; i < shape_group->num_shapes; i++) {
auto shape_id = shape_group->shape_ids[i];
auto r = shape_group->stroke_color == nullptr ? 0 : shape_list[shape_id]->stroke_width;
float r = 0;
if (shape_group->stroke_color != nullptr){
if (shape_list[shape_id]->type == ShapeType::Path){
const Path *p = (const Path*)(shape_list[shape_id]->ptr);
if (p->thickness != nullptr){
for (int i = 0; i < p->num_points; i++)
r = std::max(r, p->thickness[i]);
}else
r = shape_list[shape_id]->stroke_width;
}else{
r = shape_list[shape_id]->stroke_width;
}
}
//auto r = shape_group->stroke_color == nullptr ? 0 : shape_list[shape_id]->stroke_width;
nodes[i] = BVHNode{shape_id,
-1,
scene.shapes_bbox[shape_id],
Expand All @@ -652,7 +665,9 @@ void compute_bounding_boxes(Scene &scene,
const Path *p = (const Path*)(shape_list[shape_group->shape_ids[0]]->ptr);
if (p->thickness != nullptr) {
const BVHNode *nodes = scene.path_bvhs[shape_group->shape_ids[0]];
max_radius = nodes[0].max_radius;
for (int i = 0; i < p->num_points; i++)
max_radius = std::max(max_radius, p->thickness[i]);
//max_radius = std::max(nodes[0].max_radius, max_radius);
}
}
for (int i = 1; i < shape_group->num_shapes; i++) {
Expand Down
18 changes: 15 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,28 @@ def build_extension(self, ext):
if isinstance(ext, CMakeExtension):
extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
info = get_paths()

if platform.system() == "Windows":
# change this to fit your python install
libdir = "C:\\Users\\micha\\miniforge3\\envs\\venv\\libs\\python310.lib"
ex = "C:\\Users\\micha\\miniforge3\\envs\\venv\\python.exe"
else:
libdir = get_config_var('LIBDIR')

include_path = info['include']

cmake_args = ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + extdir,
'-DPYTHON_LIBRARY=' + get_config_var('LIBDIR'),
'-DPYTHON_INCLUDE_PATH=' + include_path]
'-DPython_LIBRARY=' + libdir,
'-DPython_INCLUDE_DIR=' + include_path]

cfg = 'Debug' if self.debug else 'Release'
build_args = ['--config', cfg]

if platform.system() == "Windows":
cmake_args += ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{}={}'.format(cfg.upper(), extdir),
'-DPython_EXECUTABLE=' + ex,
'-DCMAKE_RUNTIME_OUTPUT_DIRECTORY_{}={}'.format(cfg.upper(), extdir)]

if sys.maxsize > 2**32:
cmake_args += ['-A', 'x64']
build_args += ['--', '/m']
Expand Down Expand Up @@ -76,7 +87,8 @@ def build_extension(self, ext):
import torch
if torch.cuda.is_available():
build_with_cuda = True
if tf_spec is not None and sys.platform != 'win32':
if False: #tf_spec is not None and sys.platform != 'win32':
assert(False)
packages.append('pydiffvg_tensorflow')
if not build_with_cuda:
import tensorflow as tf
Expand Down