Skip to content

Commit

Permalink
Merge pull request #5141 from nortikin/poly_inscribed_circle
Browse files Browse the repository at this point in the history
Polygon Inscribed Circle node
  • Loading branch information
portnov authored Sep 15, 2024
2 parents 45fd356 + 9b3f015 commit ef778d4
Show file tree
Hide file tree
Showing 11 changed files with 314 additions and 14 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
68 changes: 68 additions & 0 deletions docs/nodes/analyzer/poly_inscribed_circle.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
Polygon Inscribed Circle
========================

Dependencies
------------

This node requires SciPy library to work.

Functionality
-------------

This node calculates the center and the radius of inscribed circle for each
convex face of the input mesh. Obviously, it is not always possible to inscribe
a circle into a polygon, if polygon is not a triangle. For non-tiangular
polygons, this node calculates the biggest circle which can be inscribed into
the polygon, i.e. the circle which touches as many polygon edges as possible.

Inputs
------

This node has the following inputs:

- **Vertices**. The vertices of the input mesh. This input is mandatory.
- **Faces**. The faces of the input mesh. This input is mandatory.

Parameters
----------

This node has the following parameters:

- **Flat Matrix output**. If checked, the node will generate a single flat list
of matrices in the **Matrix** output, for all input meshes. Checked by default.
- **On concave faces**. This parameter is available in the N panel only.
Defines what the node should do if it encounters a concave face. There are
the following options available:

- **Skip**. Just skip such faces - do not generate inscribed circles for them.
- **Error**. Stop processing and give an error (turn the node red).
- **As Is**. Try to generate an inscribed circle for such face anyway. In
many cases, the generated circle will be incorrect (will be too small or
even outside the polygon), but in some simple cases it can be valid.

The default option is **Skip**.

Outputs
-------

This node has the following outputs:

- **Center**. For each inscribed circle, this contains a matrix, Z axis of
which points along face normal, and the translation component equals to the
center of the inscribed circle. This output can be used to actually place
circles at their places.
- **Radius**. Radiuses of the inscribed circles.

Examples of Usage
-----------------

In many cases inscribed circle can touch only two or three polygon edges:

.. image:: ../../../docs/assets/nodes/analyzer/inscribed_circle_1.png
:target: ../../../docs/assets/nodes/analyzer/inscribed_circle_1.png

If the polygon is almost regular, the circle will touch more edges:

.. image:: ../../../docs/assets/nodes/analyzer/inscribed_circle_2.png
:target: ../../../docs/assets/nodes/analyzer/inscribed_circle_2.png

1 change: 1 addition & 0 deletions index.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@
- SvCircleApproxNode
- SvSphereApproxNode
- SvInscribedCircleNode
- SvSemiInscribedCircleNode
- SvSteinerEllipseNode
- ---
- SvMeshSelectNodeMk2
Expand Down
1 change: 1 addition & 0 deletions menus/full_by_data_type.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@
- SvCircleApproxNode
- SvSphereApproxNode
- SvInscribedCircleNode
- SvSemiInscribedCircleNode
- SvSteinerEllipseNode
- ---
- SvMeshSelectNodeMk2
Expand Down
1 change: 1 addition & 0 deletions menus/full_nortikin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@
- SvCircleApproxNode
- SvSphereApproxNode
- SvInscribedCircleNode
- SvSemiInscribedCircleNode
- SvSteinerEllipseNode
- ---
- SvMeshSelectNodeMk2
Expand Down
96 changes: 96 additions & 0 deletions nodes/analyzer/poly_inscribed_circle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# This file is part of project Sverchok. It's copyrighted by the contributors
# recorded in the version control history of the file, available from
# its original location https://github.com/nortikin/sverchok/commit/master
#
# SPDX-License-Identifier: GPL3
# License-Filename: LICENSE

import numpy as np
import bpy
from bpy.props import BoolProperty, EnumProperty
from sverchok.node_tree import SverchCustomTreeNode
from sverchok.utils.inscribed_circle import calc_inscribed_circle, ERROR, RETURN_NONE, ASIS
from sverchok.data_structure import updateNode, zip_long_repeat, ensure_nesting_level, get_data_nesting_level

class SvSemiInscribedCircleNode(SverchCustomTreeNode, bpy.types.Node):
"""
Triggers: Polygon Inscribed Circle
Tooltip: Inscribed circle for an arbitrary convex polygon
"""
bl_idname = 'SvSemiInscribedCircleNode'
bl_label = 'Polygon Inscribed Circle'
bl_icon = 'OUTLINER_OB_EMPTY'
sv_icon = 'SV_POLY_INSCRIBED_CIRCLE'
sv_dependencies = {'scipy'}

def sv_init(self, context):
self.inputs.new('SvVerticesSocket', "Vertices")
self.inputs.new('SvStringsSocket', "Faces")
self.outputs.new('SvMatrixSocket', "Center")
self.outputs.new('SvStringsSocket', "Radius")

flat_output : BoolProperty(
name = "Flat Matrix output",
description = "Output single flat list of matrices",
default = True,
update = updateNode)

concave_modes = [
(RETURN_NONE, "Skip", "Skip concave faces - do not generate output for them", 0),
(ERROR, "Error", "Generate an error if encounter a concave face", 1),
(ASIS, "As Is", "Try to calculate inscribed circle anyway (it probably will be incorrect)", 2)
]

on_concave : EnumProperty(
name = "On concave face",
description = "What to do if encounter a concave face",
default = RETURN_NONE,
items = concave_modes,
update = updateNode)

def draw_buttons(self, context, layout):
layout.prop(self, 'flat_output')

def draw_buttons_ext(self, context, layout):
self.draw_buttons(context, layout)
layout.label(text = "On concave faces:")
layout.prop(self, 'on_concave', text='')

def process(self):
if not any(socket.is_linked for socket in self.outputs):
return

vertices_s = self.inputs['Vertices'].sv_get()
vertices_s = ensure_nesting_level(vertices_s, 4)
faces_s = self.inputs['Faces'].sv_get()
faces_s = ensure_nesting_level(faces_s, 4)

matrix_out = []
radius_out = []
for params in zip_long_repeat(vertices_s, faces_s):
new_matrix = []
new_radius = []
for vertices, faces in zip_long_repeat(*params):
vertices = np.array(vertices)
for face in faces:
face = np.array(face)
circle = calc_inscribed_circle(vertices[face],
on_concave = self.on_concave)
if circle is not None:
new_matrix.append(circle.get_matrix())
new_radius.append(circle.radius)
if self.flat_output:
matrix_out.extend(new_matrix)
else:
matrix_out.append(new_matrix)
radius_out.append(new_radius)

self.outputs['Center'].sv_set(matrix_out)
self.outputs['Radius'].sv_set(radius_out)

def register():
bpy.utils.register_class(SvSemiInscribedCircleNode)

def unregister():
bpy.utils.unregister_class(SvSemiInscribedCircleNode)

Binary file added ui/icons/sv_poly_inscribed_circle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
89 changes: 89 additions & 0 deletions ui/icons/svg/sv_poly_inscribed_circle.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions utils/geom.py
Original file line number Diff line number Diff line change
Expand Up @@ -2801,3 +2801,24 @@ def scale_relative(points, center, scale):

return (points + center).tolist()

def is_convex_2d(verts):
"""
Check if 2D polygon is convex.
Args:
verts: np.array or list of shape (n,3); only first and second components are considered.
Returns:
boolean.
"""
verts = np.array(verts)
edges = np.roll(verts, -1, axis=0) - verts
sign = None
for e1, e2 in zip(edges[:-1], edges[1:]):
n = np.cross(e1, e2)
if sign is None:
sign = n[2]
elif sign * n[2] < 0:
return False
return True

Loading

0 comments on commit ef778d4

Please sign in to comment.