Skip to content

Commit

Permalink
Merge pull request #5153 from nortikin/viewer_curve_arrows
Browse files Browse the repository at this point in the history
"Viewer Draw Curve": indicate curve direction
  • Loading branch information
portnov authored Oct 10, 2024
2 parents 46d4193 + cda4321 commit dc7b371
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 7 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions docs/nodes/viz/viewer_draw_curve.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ The parameters of the node are (in this order):
.. image:: https://user-images.githubusercontent.com/14288520/190168448-b0691bc4-392f-4f83-bb5b-926da7b64b8e.png
:target: https://user-images.githubusercontent.com/14288520/190168448-b0691bc4-392f-4f83-bb5b-926da7b64b8e.png

* **Display Arrows**, **Arrows Color**, **Arrows Line Width**. Control display
of small arrows which indicate the direction of curve (direction in which
curve's T parameter grows). Curve arrows are not shown by default.

.. image:: ../../../docs/assets/nodes/viz/viewer_draw_curve_arrows.png
:target: ../../../docs/assets/nodes/viz/viewer_draw_curve_arrows.png

-------------

* **Display Control Points**, **Control Points Color**, **Control Points
Expand Down
4 changes: 2 additions & 2 deletions nodes/curve/prepare_curves_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ def process(self):
new_u_curves.append(u_curves)
new_v_curves.append(v_curves)
new_pts.append(pts)
new_u_values.append(u_values)
new_v_values.append(v_values)
new_u_values.append(u_values.tolist())
new_v_values.append(v_values.tolist())
if flat_output:
u_curves_out.extend(new_u_curves)
v_curves_out.extend(new_v_curves)
Expand Down
3 changes: 3 additions & 0 deletions nodes/curve/refine_nurbs_curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ def process(self):
curves_list = []
new_knots_list = []
for curve, new_knots, resolution, t_min, t_max in zip_long_repeat(*params):
curve = SvNurbsCurve.to_nurbs(curve)
if curve is None:
raise Exception("Curve is not NURBS")
if not self.specify_segment:
t_min = t_max = None

Expand Down
37 changes: 37 additions & 0 deletions nodes/viz/viewer_draw_curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ def draw_edges(shader, points, edges, line_width, color, is_smooth=False):
batch.draw(shader)
drawing.reset_line_width()

def draw_arrows(shader, tips, pts1, pts2, line_width, color):
n = len(tips)
points = np.concatenate((tips, pts1, pts2))
edges = [(i, i+n) for i in range(n-1)]
edges.extend([(i, i+2*n) for i in range(n-1)])
drawing.set_line_width(line_width)
batch = batch_for_shader(shader, 'LINES', {"pos": points.tolist()}, indices=edges)
shader.bind()
shader.uniform_float('color', color)
batch.draw(shader)
drawing.reset_line_width()

def draw_edges_colored(shader, points, edges, line_width, colors):
drawing.set_line_width(line_width)
batch = batch_for_shader(shader, 'LINES', {"pos": points, "color": colors}, indices=edges)
Expand Down Expand Up @@ -88,6 +100,9 @@ def draw_curves(context, args):
if node.draw_verts and item.points is not None:
draw_points(v_shader, item.points, node.verts_size, node.verts_color)

if node.draw_arrows and item.arrow_pts1 is not None and item.arrow_pts2 is not None:
draw_arrows(e_shader, item.np_points[1:], item.arrow_pts1, item.arrow_pts2, node.arrows_line_width, node.arrows_color)

drawing.enable_blendmode()

class SvBakeCurveOp(bpy.types.Operator, SvGenericNodeLocator):
Expand Down Expand Up @@ -153,6 +168,23 @@ class SvCurveViewerDrawNode(SverchCustomTreeNode, bpy.types.Node):
min = 1, default = 2,
update = updateNode)

draw_arrows : BoolProperty(
name = "Display arrows",
default = False,
update = updateNode)

arrows_line_width : IntProperty(
name = "Arrows Line Width",
min = 1, default = 1,
update = updateNode)

arrows_color : FloatVectorProperty(
name = "Arrows Color",
default = (0.5, 0.8, 1.0, 1.0),
size = 4, min = 0.0, max = 1.0,
subtype = 'COLOR',
update = updateNode)

draw_control_points: BoolProperty(
update=updateNode, name='Display control points', default=False)

Expand Down Expand Up @@ -243,6 +275,11 @@ def draw_buttons(self, context, layout):
row.prop(self, 'line_color', text="")
row.prop(self, 'line_width', text="px")

row = grid.row(align=True)
row.prop(self, 'draw_arrows', icon='EVENT_RIGHT_ARROW', text='')
row.prop(self, 'arrows_color', text='')
row.prop(self, 'arrows_line_width', text='px')

row = grid.row(align=True)
row.prop(self, 'draw_control_points', icon='DECORATE_KEYFRAME', text='')
row.prop(self, 'control_points_color', text="")
Expand Down
30 changes: 26 additions & 4 deletions utils/curve/bakery.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ def __init__(self, node, curve, resolution):

logger = get_logger()

if node.draw_line or node.draw_verts or node.draw_comb or node.draw_curvature:
if node.draw_line or node.draw_verts or node.draw_comb or node.draw_curvature or node.draw_arrows:
t_min, t_max = curve.get_u_bounds()
ts = np.linspace(t_min, t_max, num=resolution)
n = len(ts)
self.points = curve.evaluate_array(ts).tolist()
self.np_points = curve.evaluate_array(ts)
self.points = self.np_points.tolist()
else:
self.points = None

Expand Down Expand Up @@ -85,8 +86,8 @@ def __init__(self, node, curve, resolution):
normals = curve.main_normal_array(ts, normalize=True)
curvatures = self.curvatures[np.newaxis].T
comb_normals = node.comb_scale * curvatures * normals
comb_points = self.points - comb_normals
self.comb_points = np.concatenate((self.points, comb_points)).tolist()
comb_points = self.np_points - comb_normals
self.comb_points = np.concatenate((self.np_points, comb_points)).tolist()
comb_normal_edges = [(i, i+n) for i in range(n)]
comb_tangent_edges = [(i, i+1) for i in range(n, 2*n-1)]
self.comb_edges = comb_normal_edges + comb_tangent_edges
Expand All @@ -108,6 +109,27 @@ def __init__(self, node, curve, resolution):
self.curvature_point_colors = None
self.curvature_edge_colors = None

if node.draw_arrows:
n = len(self.points)
binormals = curve.binormal_array(ts[1:], normalize=True)
vectors = self.np_points[1:] - self.np_points[:-1]
vector_lens = np.linalg.norm(vectors, axis=1, keepdims=True)
mean_len = vector_lens.mean()
vectors /= vector_lens
is_zero = np.linalg.norm(binormals, axis=1) < 1e-4
z = np.array([0,0,1])
binormals[is_zero] = np.cross(z, vectors)[is_zero]
vectors *= mean_len
binormals *= mean_len
arrow_vecs1 = binormals - vectors
arrow_vecs2 = -binormals - vectors
tips = self.np_points[1:]
self.arrow_pts1 = tips + arrow_vecs1
self.arrow_pts2 = tips + arrow_vecs2
else:
self.arrow_pts1 = None
self.arrow_pts2 = None

@property
def nurbs_curve(self):
if self._nurbs_curve is None:
Expand Down
1 change: 1 addition & 0 deletions utils/surface/bakery.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def __init__(self):
self.draw_nodes = False
self.draw_comb = False
self.draw_curvature = False
self.draw_arrows = False

def __init__(self, node, surface, resolution_u, resolution_v):
self.node = node
Expand Down
3 changes: 2 additions & 1 deletion utils/surface/gordon.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ def adjust(t):

result = segments[0]
for segment in segments[1:]:
result = result.concatenate(segment)
if segment is not None:
result = result.concatenate(segment)

return result

Expand Down

0 comments on commit dc7b371

Please sign in to comment.