Skip to content

Commit

Permalink
Raycaster checks (#4186)
Browse files Browse the repository at this point in the history
* raycaster checks

* node docstring
  • Loading branch information
vicdoval committed Jun 26, 2021
1 parent cc56063 commit ce84c14
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
14 changes: 10 additions & 4 deletions docs/nodes/analyzer/raycaster_lite.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ Raycast
Functionality
-------------

Functionality is almost completely analogous to the two built-in blender operators
``bpy.context.scene.ray_cast`` and ``object.ray_cast``.
Functionality is almost completely analogous to the two built-in blender operators
``bpy.context.scene.ray_cast`` and ``object.ray_cast``.
Ray is casted from "start" vector to "end" vector and can hit polygons of input objects.

see docs:
`bpy.types.Object.ray_cast <http://www.blender.org/documentation/blender_python_api_2_71_0/bpy.types.Object.html#bpy.types.Object.ray_cast>`_ and
see docs:
`bpy.types.Object.ray_cast <http://www.blender.org/documentation/blender_python_api_2_71_0/bpy.types.Object.html#bpy.types.Object.ray_cast>`_ and
`bpy.types.Scene.ray_cast <http://www.blender.org/documentation/blender_python_api_2_71_0/bpy.types.Scene.html#bpy.types.Scene.ray_cast>`_


Expand Down Expand Up @@ -41,6 +41,12 @@ Output sockets
| Success | ``True`` or ``False`` if ray doesn't hit any polygon. |
+------------------------+----------------------------------------------------------------------------------------+

Advanced parameters (N-Panel)
----------------------------

**Safe Check**: Checks the mesh for unreferenced polygons (slows the node but prevents some Blender crashes)

**All Triangles**: Enable if all the incoming faces are triangles to improve the performance of the algorithm

Usage
-----
Expand Down
32 changes: 24 additions & 8 deletions nodes/analyzer/raycaster_lite.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,36 @@
import bpy
from sverchok.node_tree import SverchCustomTreeNode
from sverchok.data_structure import (updateNode, match_long_cycle as C)
from mathutils.bvhtree import BVHTree
from sverchok.utils.bvh_tree import bvh_tree_from_polygons

# zeffii 2017 8 okt
# airlifted from Kosvor's Raycast nodes..

class SvRaycasterLiteNode(bpy.types.Node, SverchCustomTreeNode):
''' svmesh to Raycast '''
"""
Triggers: Raycast on Mesh
Tooltip: Cast rays from arbitrary points on to a mesh a determine hiting location, normal at hitpoint, ray legngth and index of the hitted face.
"""
bl_idname = 'SvRaycasterLiteNode'
bl_label = 'Raycaster'
bl_icon = 'OUTLINER_OB_EMPTY'
sv_icon = 'SV_RAYCASTER'

start: bpy.props.FloatVectorProperty(default=(0,0,0), size=3, update=updateNode)
direction: bpy.props.FloatVectorProperty(default=(0,0,-1), size=3, update=updateNode)

all_triangles: bpy.props.BoolProperty(
name='All Triangles',
description='Enable to improve node performance if all inputted polygons are triangles',
default=False,
update=updateNode)
safe_check: bpy.props.BoolProperty(
name='Safe Check',
description='When disabled polygon indices refering to unexisting points will crash Blender but makes node faster',
default=True)

def draw_buttons_ext(self, context, layout):
layout.prop(self, 'all_triangles')
layout.prop(self, 'safe_check')
def sv_init(self, context):
si = self.inputs.new
so = self.outputs.new
Expand All @@ -50,17 +65,18 @@ def sv_init(self, context):
so('SvStringsSocket', 'Success')

@staticmethod
def svmesh_to_bvh_lists(v, f):
def svmesh_to_bvh_lists(v, f, all_tris, safe_check):
for vertices, polygons in zip(*C([v, f])):
yield BVHTree.FromPolygons(vertices, polygons, all_triangles=False, epsilon=0.0)
yield bvh_tree_from_polygons(vertices, polygons, all_triangles=all_tris, epsilon=0.0, safe_check=safe_check)

def process(self):
L, N, I, D, S = self.outputs
RL = []
if not any([s.is_linked for s in self.outputs]):
return
vert_in, face_in, start_in, direction_in = C([sock.sv_get(deepcopy=False) for sock in self.inputs])

vert_in, face_in, start_in, direction_in = C([sock.sv_get() for sock in self.inputs])

for bvh, st, di in zip(*[self.svmesh_to_bvh_lists(vert_in, face_in), start_in, direction_in]):
for bvh, st, di in zip(*[self.svmesh_to_bvh_lists(vert_in, face_in, self.all_triangles, self.safe_check), start_in, direction_in]):
st, di = C([st, di])
RL.append([bvh.ray_cast(i, i2) for i, i2 in zip(st, di)])

Expand Down

0 comments on commit ce84c14

Please sign in to comment.