def test_collision_and_distance_box_sphere(self): dis, result = fcl.distance( fcl.CollisionObject(self.box, self.trans1), fcl.CollisionObject(self.sphere, self.trans2), fcl.DistanceRequest(True)) self.assertEqual(dis, self._ref_dist) self.assertEqual(result.min_distance, self._ref_dist) self.assertEqual(result.nearest_points, self._nearest_points) self.assertEqual(result.b1, -1) self.assertEqual(result.b2, -1) dis, result = fcl.distance( fcl.CollisionObject(self.box, transform.Transform(transform.Quaternion())), fcl.CollisionObject( self.sphere, transform.Transform(transform.Quaternion(), [0.0, 0.0, 0.0])), fcl.DistanceRequest(True)) self.assertEqual(dis, -1.0) self.assertEqual(result.min_distance, -1.0) for i in result.nearest_points: for j in i: self.assertAlmostEqual(0.0, j)
def test_triangle(self): """ TriangleP is not well supported... Perhaps not much of an issue, since BVHModel will deal with meshes / triangle soup So, what'evs? """ import numpy as np li = [ [0, 100, 100], [0, 0, 0], [100, 0, 100], ] arr = np.array(li, "f") _p = fcl.TriangleP(arr[0], arr[1], arr[2]) p = fcl.CollisionObject(_p) collision_object = fcl.CollisionObject( self.box, transform.Transform(transform.Quaternion())) # TODO: segfault! # ....Warning: distance function between node type 9 and node type 17 is not supported # dis, result = fcl.distance(collision_object, p, fcl.DistanceRequest(True)) ret, result = fcl.collide(collision_object, p, fcl.CollisionRequest()) # Warning: collision function between node type 9 and node type 17 is not supported self.assertTrue(ret == 0)
def test_collision_box_sphere(self): ret, result = fcl.collide( fcl.CollisionObject(fcl.Box(*self._side), transform.Transform(transform.Quaternion())), fcl.CollisionObject( fcl.Sphere(self._radius), transform.Transform(transform.Quaternion(), [0.0, 0.0, 0.0])), fcl.CollisionRequest()) self.assertTrue(len(result.contacts) == 1) self.assertTrue(len(result.cost_sources) == 0) con = result.contacts[0] self.assertAlmostEqual(con.penetration_depth, 0.0) self.assertTrue(con.penetration_depth < sys.float_info.min) self.assertTrue(isinstance(con.o1, fcl.Box)) self.assertTrue(isinstance(con.o2, fcl.Sphere))
def test_dynamic_aabb_tree_collision_manager(self): _size = 0 self.assertTrue(self.manager.size() == 0) self.manager.registerObjects(self.objs) _size += len(self.objs) self.assertTrue(self.manager.size() == _size) self.manager.registerObject(fcl.CollisionObject(self.cyl)) _size += 1 self.assertTrue(self.manager.size() == _size)
def test_distance_box_sphere_translated(self): ret, result = fcl.collide( fcl.CollisionObject(self.box, self.trans1), fcl.CollisionObject(self.sphere, self.trans2), fcl.CollisionRequest()) self.assertEqual(result.contacts, []) self.assertEqual(result.cost_sources, []) dis, result = fcl.distance( fcl.CollisionObject(self.box, self.trans1), fcl.CollisionObject(self.sphere, self.trans2), fcl.DistanceRequest(True)) self.assertEqual(dis, self._ref_dist) self.assertEqual(result.min_distance, self._ref_dist) self.assertEqual(result.nearest_points, self._nearest_points) self.assertEqual(result.o2.radius, self._radius) self.assertEqual(result.o1.side, self._side)
def setUp(self): self.objs = [ fcl.CollisionObject(fcl.Box(1.0, 2.0, 3.0)), fcl.CollisionObject(fcl.Sphere(4.0)), fcl.CollisionObject(fcl.Cone(5.0, 6.0)) ] self.manager = fcl.DynamicAABBTreeCollisionManager() self.res = fcl.CollisionResult() # self.manager.collide(self.res, self.collide_callback_func) self._side = (1.0, 2.0, 3.0) self._radius = 4.0 self._nearest_points = [(-0.5, 0.0, 0.0), (4.0, 0.0, 0.0)] self._ref_dist = 15.5 self.pt1 = [10.0, 0.0, 0.0] self.pt2 = [-10.0, 0.0, 0.0] self.box = fcl.Box(*self._side) self.sphere = fcl.Sphere(self._radius) self.cyl = fcl.Cylinder(7.0, 8.0) self.trans1 = transform.Transform(transform.Quaternion(), self.pt1) self.trans2 = transform.Transform(transform.Quaternion(), self.pt2)
def _create_youbot_object(self, config): if self.section == 1: self.youbot = fcl.CollisionObject(self.youbot_Box, transform.Transform(transform.Quaternion(), config)) else: self.youbot = [] self.yobot.append(fcl.CollisionObject(self.youbot_base, transform.Transform(transform.Quaternion(), [-0.0014, 0.0, 0.0956]))) self.yobot.append(fcl.CollisionObject(self.youbot_arm_1, transform.Transform(transform.Quaternion(), [0.167, 0.0, 0.086]))) quat_arm_1 = [math.cos(config[0]/2), 0, -math.sin(config[0]/2), 0] pos_arm_1 = [0.167+0.074*math.cos(config[0]), -0.0412, 0.086+0.074*math.sin(config[0])] self.yobot.append(fcl.CollisionObject(self.youbot_arm_2, transform.Transform(quat_arm_1, pos_arm_1))) quat_arm_2 = [math.cos(config[1]/2), 0, -math.sin(config[1]/2), 0] quat_arm_2 = self._multiply_quaternion(quat_arm_1, quat_arm_2) pos_arm_2 = pos_arm_1 + [0.0662*math.cos(config[1]), 0.0370 + 0.0412, 0.0662*math.sin(config[1])] self.yobot.append(fcl.CollisionObject(self.youbot_arm_3, transform.Transform(quat_arm_2, pos_arm_2))) quat_arm_3 = [math.cos(config[2]/2), 0, -math.sin(config[2]/2), 0] quat_arm_3 = self._multiply_quaternion(quat_arm_2, quat_arm_3) pos_arm_3 = pos_arm_2 + [0.0869*math.cos(config[2]), -0.0370 , 0.0869*math.sin(config[2])] self.yobot.append(fcl.CollisionObject(self.youbot_arm_4, transform.Transform(quat_arm_3, pos_arm_3)))
def __init__(self, part_num): """ Room Parameters """ self.room_dimensions = [7.50, 7.50] self.room_offset = [-1.25, -1.25] """ Walls """ self.wall_1 = fcl.CollisionObject(fcl.Box(8.0, 0.5, 0.5),transform.Transform(transform.Quaternion(), [2.50, -1.25, 0.25])) self.wall_2 = fcl.CollisionObject(fcl.Box(0.5, 8.0, 0.5),transform.Transform(transform.Quaternion(), [6.25, 2.50, 0.25])) self.wall_3 = fcl.CollisionObject(fcl.Box(8.0, 0.5, 0.5),transform.Transform(transform.Quaternion(), [2.50, 6.25, 0.25])) self.wall_4 = fcl.CollisionObject(fcl.Box(0.5, 8.0, 0.5),transform.Transform(transform.Quaternion(), [-1.25, 2.50, 0.25])) """ Obstacles """ self.obs_1 = fcl.CollisionObject(fcl.Box(2.0, 2.0, 1.0),transform.Transform(transform.Quaternion(), [2.50, 2.50, 0.50])) self.obs_2 = fcl.CollisionObject(fcl.Box(0.75, 0.75, 0.75),transform.Transform(transform.Quaternion(), [5.0, 1.0, 0.375])) self.obs_3 = fcl.CollisionObject(fcl.Box(0.75, 0.75, 0.75),transform.Transform(transform.Quaternion(), [3.0, 5.0, 0.375])) self.obs_4 = fcl.CollisionObject(fcl.Box(0.5, 0.5, 0.5),transform.Transform(transform.Quaternion(), [0.0, 3.0, 0.25])) """ Table Obstacle """ self.obs_5_1 = fcl.CollisionObject(fcl.Box(0.4, 0.4, 0.2),transform.Transform(transform.Quaternion(), [0.6, 0.0, 0.1])) self.obs_5_2 = fcl.CollisionObject(fcl.Box(0.05, 0.05, 0.15),transform.Transform(transform.Quaternion(), [0.5, 0.1, 0.275])) self.obs_5_3 = fcl.CollisionObject(fcl.Box(0.05, 0.05, 0.15),transform.Transform(transform.Quaternion(), [0.5, -0.1, 0.275])) self.obs_5_4 = fcl.CollisionObject(fcl.Box(0.05, 0.25, 0.05),transform.Transform(transform.Quaternion(), [0.5, 0.0, 0.375])) """ Part 1 YouBot """ self.youbot_Box = fcl.Box(0.7, 0.5, 0.6) """ Part 2 YouBot """ self.youbot_base = fcl.Box(0.5701, 0.3570, 0.1060) self.youbot_arm_1 = fcl.Box(0.1697, 0.1699, 0.1060) self.youbot_arm_2 = fcl.Box(0.22, 0.0745, 0.0823) self.youbot_arm_3 = fcl.Box(0.1920, 0.0591, 0.0750) self.youbot_arm_4 = fcl.Box(0.2401, 0.0581, 0.0987) """ YouBot """ self.section = part_num self.youbot = fcl.CollisionObject(self.youbot_Box,transform.Transform(transform.Quaternion(), [0.0, 0.0, 0.0])) """ Obstacles as a list """ self.obs = [self.wall_1, self.wall_2, self.wall_3, self.wall_4, self.obs_1, self.obs_2, self.obs_3, self.obs_4, self.obs_5_1, self.obs_5_2, self.obs_5_3, self.obs_5_4] self.res = fcl.CollisionResult()
def getLinkDistance(self, l0_name, l1_name, joint_q): # type: (str, str, np._ArrayLike[float]) -> float '''get shortest distance from link with id l0 to link with id l1 for posture joint_q''' from fcl import fcl, collision_data, transform #get link rotation and position in world frame q = iDynTree.VectorDynSize.fromList(joint_q) self.model.dynComp.setRobotState(q, self.dq_zero, self.dq_zero, self.world_gravity) if l0_name in self.model.linkNames: # if robot link f0 = self.model.dynComp.getFrameIndex(l0_name) t0 = self.model.dynComp.getWorldTransform(f0) rot0 = t0.getRotation().toNumPy() pos0 = t0.getPosition().toNumPy() s0 = self.config['scaleCollisionHull'] else: # if world link pos0 = self.link_cuboid_hulls[l0_name][1] rot0 = eulerAnglesToRotationMatrix(self.link_cuboid_hulls[l0_name][2]) s0 = 1 if l1_name in self.model.linkNames: # if robot link f1 = self.model.dynComp.getFrameIndex(l1_name) t1 = self.model.dynComp.getWorldTransform(f1) rot1 = t1.getRotation().toNumPy() pos1 = t1.getPosition().toNumPy() s1 = self.config['scaleCollisionHull'] else: # if world link pos1 = self.link_cuboid_hulls[l1_name][1] rot1 = eulerAnglesToRotationMatrix(self.link_cuboid_hulls[l1_name][2]) s1 = 1 # TODO: use pos and rot of boxes for vals from geometry tags # self.link_cuboid_hulls[l0_name][1], [2] b = np.array(self.link_cuboid_hulls[l0_name][0]) * s0 p = np.array(self.link_cuboid_hulls[l0_name][1]) b0_center = 0.5*np.array([(b[1][0])+(b[0][0]) + p[0], (b[1][1])+(b[0][1]) + p[1], (b[1][2])+(b[0][2]) + p[2]]) b0 = fcl.Box(b[1][0]-b[0][0], b[1][1]-b[0][1], b[1][2]-b[0][2]) b = np.array(self.link_cuboid_hulls[l1_name][0]) * s1 p = np.array(self.link_cuboid_hulls[l1_name][1]) b1_center = 0.5*np.array([(b[1][0])+(b[0][0]) + p[0], (b[1][1])+(b[0][1]) + p[1], (b[1][2])+(b[0][2]) + p[2]]) b1 = fcl.Box(b[1][0]-b[0][0], b[1][1]-b[0][1], b[1][2]-b[0][2]) # move box to pos + box center pos (model has pos in link origin, box has zero at center) o0 = fcl.CollisionObject(b0, transform.Transform(rot0, pos0+b0_center)) o1 = fcl.CollisionObject(b1, transform.Transform(rot1, pos1+b1_center)) distance, d_result = fcl.distance(o0, o1, collision_data.DistanceRequest(True)) if distance < 0: if self.config['verbose'] > 1: print("Collision of {} and {}".format(l0_name, l1_name)) # get proper collision and depth since optimization should also know how much constraint is violated cr = collision_data.CollisionRequest() cr.enable_contact = True cr.enable_cost = True collision, c_result = fcl.collide(o0, o1, cr) # sometimes no contact is found even though distance is less than 0? if len(c_result.contacts): distance = c_result.contacts[0].penetration_depth return distance
from fcl import fcl, transform objs = [ fcl.CollisionObject(fcl.Box(1.0, 2.0, 3.0)), fcl.CollisionObject(fcl.Sphere(4.0)), fcl.CollisionObject(fcl.Cone(5.0, 6.0)) ] # Register objects to DynamicAABBTreeCollisionManager manager = fcl.DynamicAABBTreeCollisionManager() print "Before resgister: ", manager.size() manager.registerObjects(objs) print "After register 1 : ", manager.size() manager.registerObject(fcl.CollisionObject(fcl.Cylinder(7.0, 8.0))) print "After register 2 : ", manager.size() # Use Callback function def cb_func(obj1, obj2, res): print "cb_func start" ret, res = fcl.collide(obj1, obj2, fcl.CollisionRequest()) print "result: ", ret return ret res = fcl.CollisionResult() manager.collide(res, cb_func) # Collision calcuration ret, result = fcl.collide( fcl.CollisionObject(