diff --git a/CMakeLists.txt b/CMakeLists.txt index 233e4be1..2b0d8f75 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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() @@ -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 @@ -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}") @@ -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() diff --git a/README.md b/README.md index 6c01273e..cb251c46 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/pydiffvg/render_pytorch.py b/pydiffvg/render_pytorch.py index a686fb1a..dc5f85ed 100644 --- a/pydiffvg/render_pytorch.py +++ b/pydiffvg/render_pytorch.py @@ -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 diff --git a/pydiffvg/save_svg.py b/pydiffvg/save_svg.py index 486a4e09..fd803ca3 100644 --- a/pydiffvg/save_svg.py +++ b/pydiffvg/save_svg.py @@ -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) diff --git a/scene.cpp b/scene.cpp index 1799c962..555c068c 100644 --- a/scene.cpp +++ b/scene.cpp @@ -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], @@ -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++) { diff --git a/setup.py b/setup.py index 5463677a..f20367e9 100644 --- a/setup.py +++ b/setup.py @@ -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'] @@ -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