-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobstacle.py
25 lines (21 loc) · 1.08 KB
/
obstacle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from manipulator import Manipulator
from abc import ABC, abstractmethod
import numpy as np
class Obstacle(ABC):
@abstractmethod
def intersect(self, manipulator: Manipulator) -> bool:
pass
class SphereObstacle(Obstacle):
def __init__(self, center: np.ndarray, r: float):
self.center = center
self.r = r
def intersect(self, manipulator: Manipulator) -> bool:
joint_coordinates = manipulator.get_joint_coordinates()
for i in range(len(joint_coordinates) - 1):
if np.linalg.norm(joint_coordinates[i] - self.center) < self.r or np.linalg.norm(joint_coordinates[i + 1] - self.center) < self.r:
return True
arm_vec = joint_coordinates[i + 1] - joint_coordinates[i]
t = (self.center @ arm_vec - joint_coordinates[i] @ arm_vec) / (np.linalg.norm(arm_vec) ** 2) # arm contains all points: joint_coordinates[i] + t * arm_vec, if t \in [0, 1]
if 0 <= t <= 1 and np.linalg.norm(joint_coordinates[i] + arm_vec * t - self.center) < self.r:
return True
return False