Skip to content

Commit

Permalink
fix collision detection. when building VTK surface from a lot of smal…
Browse files Browse the repository at this point in the history
…l object, the collision is important
  • Loading branch information
mjirik committed Sep 9, 2019
1 parent 318de6c commit 79d8b74
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion teigen/generators/unconnected_cylinders.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"""

import logging

logger = logging.getLogger(__name__)
import argparse
import numpy as np
Expand Down Expand Up @@ -270,6 +269,7 @@ def add_cylinder_to_stats(self, pt1, pt2, radius):
self.geometry_data["vector"].append(vector)
self.geometry_data["point1"].append(pt1)
self.geometry_data["point2"].append(pt2)
# self.geometry_data["collide_with_prevs"].append(collide_with_prevs)
self.surface += surf

def create_cylinder(
Expand Down
10 changes: 10 additions & 0 deletions teigen/geometry3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,12 @@ def bbox_collision(self, bbox):

class TubeObject(GeometricObject):
def __init__(self, point1, point2, radius):
"""
:param point1:
:param point2:
:param radius:
"""

bbox = get_bbox([point1, point2], margin=radius)
GeometricObject.__init__(self, bbox=bbox)
Expand Down Expand Up @@ -907,13 +913,15 @@ def collision(self, obj):

class CollisionModel():
def __init__(self, areasize):

self.collision_alowed = False
if areasize is not None:
areasize = np.asarray(areasize)
self.areasize = areasize
self.object_number = 0
self._cylinder_end_nodes = []
self._cylinder_end_nodes_radiuses = []
self.do_not_check_area = False

def get_random_point(self, radius=None):
if radius is not None:
Expand All @@ -932,6 +940,8 @@ def is_point_in_area(self, node, radius=None):
node = np.asarray(node)
if radius is None:
radius = self.radius_maximum
if self.do_not_check_area:
return True
return is_in_area(node, self.areasize, radius=radius)

def n_closest_end_points(self, node, n):
Expand Down
20 changes: 19 additions & 1 deletion teigen/tb_vtk.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import vtk
from scipy.interpolate import InterpolatedUnivariateSpline
from . import tree
from . import geometry3d

# new interface

Expand Down Expand Up @@ -474,6 +475,8 @@ def polygon_radius_compensation_factos(
return cylinder_radius_compensation_factor, sphere_radius_compensation_factor, cylinder_radius_compensation_factor_long, sphere_radius_compensation_factor_long




def gen_tree(tree_data, cylinder_resolution=10, sphere_resolution=10,
polygon_radius_selection_method="inscribed",
cylinder_radius_compensation_factor=1.0,
Expand All @@ -493,6 +496,10 @@ def gen_tree(tree_data, cylinder_resolution=10, sphere_resolution=10,
:param cylinder_radius_compensation_factor: is used to change radius of cylinder and spheres
:return:
"""

# when building from a lot of objects we need to know whether there is a collision or not.
# If there is collision we need to use binary op. If there is no collision, we can use just add.

import vtk
# appendFilter = vtk.vtkAppendPolyData()
appended_data = None
Expand All @@ -509,6 +516,11 @@ def gen_tree(tree_data, cylinder_resolution=10, sphere_resolution=10,
cylinder_radius_compensation_factor, sphere_radius_compensation_factor,\
cylinder_radius_compensation_factor_long, sphere_radius_compensation_factor_long = factors

#this is just to say whether the tube collide (and we have to use add()) or not (and we have to use binary op.)
collision_model = geometry3d.CollisionModelCombined(None)
collision_model.do_not_check_area = True


# import ipdb; ipdb.set_trace()
for br in tree_data:
# import ipdb;
Expand All @@ -518,6 +530,12 @@ def gen_tree(tree_data, cylinder_resolution=10, sphere_resolution=10,
length = br["length"]
direction = br["direction"]
uv = br['upperVertex']
lv = np.asarray(uv) + np.asarray(direction) * length
# lv = br['lowerVertex']
collision_detected = collision_model.add_tube_if_no_collision(uv, lv, radius)
if collision_detected:
collision_model.add_tube(uv, lv, radius)
logger.debug("Collision detected")

dbg_msg = "generating edge with length: " + str(br["length"])
logger.debug(dbg_msg)
Expand All @@ -543,7 +561,7 @@ def gen_tree(tree_data, cylinder_resolution=10, sphere_resolution=10,
# import ipdb; ipdb.set_trace()
else:
appended_data = _add_object(
appended_data, tube, controlled_collision=True, collision=False)
appended_data, tube, controlled_collision=True, collision=collision_detected)
# nn = appended_data.GetNumberOfPoints()
# logger.debug(f"appended_data number of nodes: {nn}, last point {appended_data.GetPoint(nn - 1)}")
# import ipdb; ipdb.set_trace()
Expand Down

0 comments on commit 79d8b74

Please sign in to comment.