Spaces:
Running
Running
File size: 26,134 Bytes
9ee6f68 | 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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 | """
custom_geometry_helper_new/basic_geometries.py
This module provides basic geometry functions for 2D and 3D geometries.
"""
from typing import List
import numpy as np
from scipy.spatial.transform import Rotation as R
from .polygon_processor import PolygonProcessor
from shapely import affinity
from src.utils.config_manager import get_basic_geometry_config
import glog
class Basic2DGeometry:
def __init__(self):
pass
@property
def EPS(self):
"""Get EPS value from current configuration"""
return get_basic_geometry_config().EPS
@classmethod
def get_eps(cls):
"""Class method to get EPS value from current configuration"""
return get_basic_geometry_config().EPS
@staticmethod
def rotate_point_counterclockwise(point, angle):
"""Rotate a point counterclockwise by a given angle around the origin.
Args:
point (np.ndarray): The point to rotate, represented as a 2D vector.
angle (float): The angle in radians to rotate the point.
Returns:
np.ndarray: The rotated point as a 2D vector.
"""
R = np.array([[np.cos(angle), -np.sin(angle)], [np.sin(angle), np.cos(angle)]])
return np.dot(point, R)
@staticmethod
def point_to_segment_distance(point, segment):
"""
Calculate the distance from a point to a line segment.
Args:
point (_type_): _description_
segment (_type_): _description_
Returns:
_type_: _description_
"""
start, end = segment[0], segment[1]
if np.allclose(start, end):
return np.linalg.norm(point - start)
direction = end - start
t = np.dot(point - start, direction) / np.dot(direction, direction)
if t < 0:
return np.linalg.norm(point - start)
elif t > 1:
return np.linalg.norm(point - end)
else:
projection = start + t * direction
return np.linalg.norm(point - projection)
@staticmethod
def get_perpendicular_intersection(point, line):
start, end = line[0], line[1]
if np.allclose(start, end):
return np.array([point[0], start[1]])
direction = end - start
direction = direction / np.linalg.norm(direction)
point_to_line = point - start
projection_length = np.dot(point_to_line, direction)
projection = start + projection_length * direction
return projection
@staticmethod
def is_inside_rectangle(point, rect, on_edge=False):
signs = [
(rect[i][0] - point[0]) * (rect[(i + 1) % 4][1] - point[1])
- (rect[(i + 1) % 4][0] - point[0]) * (rect[i][1] - point[1])
for i in range(4)
]
eps = Basic2DGeometry.get_eps()
return all(sign > eps if not on_edge else -eps for sign in signs) or all(
sign < -eps if not on_edge else -eps for sign in signs
)
@staticmethod
def is_on_segment(point, segment):
start, end = segment[0], segment[1]
if np.allclose(start, end):
return np.allclose(point, start)
direction = end - start
t = np.dot(point - start, direction) / np.dot(direction, direction)
return 0 <= t <= 1
@staticmethod
def intersection_of_line(line1, line2):
x1, y1 = line1[0]
x2, y2 = line1[1]
x3, y3 = line2[0]
x4, y4 = line2[1]
denom = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)
if denom == 0:
return None
px = ((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4)) / denom
py = ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4)) / denom
return np.array([px, py])
@staticmethod
def intersection_of_segment(segment1, segment2):
intersection = Basic2DGeometry.intersection_of_line(segment1, segment2)
if intersection is None:
return None
if Basic2DGeometry.is_on_segment(
intersection, segment1
) and Basic2DGeometry.is_on_segment(intersection, segment2):
return intersection
return None
@staticmethod
def intersection_of_parallel_rectangle(rect1, rect2):
"""
Calculate the intersection of two parallel rectangles
Args:
rect1: Four vertices of the first rectangle, numpy array shape=(4,2)
rect2: Four vertices of the second rectangle, numpy array shape=(4,2)
Returns:
numpy.ndarray: Four vertices of the intersection rectangle, returns None if no intersection
"""
if rect1 is None or rect2 is None:
return None
eps = Basic2DGeometry.get_eps()
# 1. Calculate direction vectors of both rectangles
dir1 = rect1[1] - rect1[0]
dir1 = dir1 / np.linalg.norm(dir1)
dir2 = rect2[1] - rect2[0]
dir2 = dir2 / np.linalg.norm(dir2)
# 2. Check if they are parallel
# if (
# np.abs(np.arctan2(dir1[1], dir1[0]) - np.arctan2(dir2[1], dir2[0])) > eps
# and np.abs(np.arctan2(dir1[1], dir1[0]) - np.arctan2(-dir2[1], dir2[0])) > eps
# ):
# return None
# 3. Project both rectangles onto parallel and perpendicular directions
perp_dir = np.array([-dir1[1], dir1[0]]) # Perpendicular direction
# Calculate projection ranges for each rectangle in both directions
proj1_parallel = [np.dot(p, dir1) for p in rect1]
proj1_perp = [np.dot(p, perp_dir) for p in rect1]
proj2_parallel = [np.dot(p, dir1) for p in rect2]
proj2_perp = [np.dot(p, perp_dir) for p in rect2]
# 4. Calculate intersection of projections
min_parallel = max(min(proj1_parallel), min(proj2_parallel))
max_parallel = min(max(proj1_parallel), max(proj2_parallel))
min_perp = max(min(proj1_perp), min(proj2_perp))
max_perp = min(max(proj1_perp), max(proj2_perp))
# 5. Check if there is an intersection
if min_parallel >= max_parallel or min_perp >= max_perp:
return None
# 6. Build intersection rectangle
corner = min_parallel * dir1 + min_perp * perp_dir
width_vec = (max_parallel - min_parallel) * dir1
height_vec = (max_perp - min_perp) * perp_dir
intersection_rect = np.array(
[
corner,
corner + width_vec,
corner + width_vec + height_vec,
corner + height_vec,
]
)
# 7. Check if intersection area is too small
width = max_parallel - min_parallel
height = max_perp - min_perp
eps = Basic2DGeometry.get_eps()
if (
width * height < eps
): # Area threshold, can be adjusted based on actual needs
return None
return intersection_rect
@staticmethod
def intersection_of_parallel_rectangle_old(rect1, rect2):
if rect1 is None or rect2 is None:
return None
intersection_points = []
for point in rect1:
if Basic2DGeometry.is_inside_rectangle(point, rect2, on_edge=False):
intersection_points.append(point)
for point in rect2:
if Basic2DGeometry.is_inside_rectangle(point, rect1, on_edge=False):
intersection_points.append(point)
for i in range(4):
for j in range(4):
intersection = Basic2DGeometry.intersection_of_segment(
[rect1[i], rect1[(i + 1) % 4]], [rect2[j], rect2[(j + 1) % 4]]
)
if intersection is not None:
intersection_points.append(intersection)
if len(intersection_points) < 4:
return None
from .convex_hull_processor import ConvexHullProcessor_2d
intersection_rect = ConvexHullProcessor_2d.get_headed_bbox(
vertices=intersection_points,
heading=(rect1[1] - rect1[0]) / np.linalg.norm(rect1[1] - rect1[0]),
)
if intersection_rect is None:
return None
return intersection_rect
@staticmethod
def intersection_area_of_parallel_rectangle(rect1, rect2):
intersection = Basic2DGeometry.intersection_of_parallel_rectangle(rect1, rect2)
if intersection is None:
return 0
return intersection.get_area()
@staticmethod
def intersection_area_of_rectangle(rect1, rect2):
return PolygonProcessor.intersection_area(rect1, rect2)
@staticmethod
def find_random_placement_inside(outer_rect, inner_rect):
"""
Find a random valid placement position for the inner rectangle within the outer rectangle
Args:
outer_rect: Outer rectangle, numpy array shape=(4,2)
inner_rect: Inner rectangle, numpy array shape=(4,2)
Returns:
translation: Translation vector, returns None if placement is not possible
"""
# 1. Calculate direction vectors of rectangles
outer_dir = outer_rect[1] - outer_rect[0]
outer_dir = outer_dir / np.linalg.norm(outer_dir)
inner_dir = inner_rect[1] - inner_rect[0]
inner_dir = inner_dir / np.linalg.norm(inner_dir)
# 2. Check if they are parallel
if not np.allclose(outer_dir, inner_dir) and not np.allclose(
outer_dir, -inner_dir
):
glog.warning("rectangle not parallel")
return None
# 3. Calculate dimensions of inner and outer rectangles
outer_perp = np.array([-outer_dir[1], outer_dir[0]])
inner_center = np.mean(inner_rect, axis=0)
# Calculate projection ranges
outer_parallel = [np.dot(p - outer_rect[0], outer_dir) for p in outer_rect]
outer_perp_proj = [np.dot(p - outer_rect[0], outer_perp) for p in outer_rect]
inner_parallel = [np.dot(p - inner_center, outer_dir) for p in inner_rect]
inner_perp_proj = [np.dot(p - inner_center, outer_perp) for p in inner_rect]
# Calculate placeable ranges
outer_pmin, outer_pmax = min(outer_parallel), max(outer_parallel)
outer_hmin, outer_hmax = min(outer_perp_proj), max(outer_perp_proj)
inner_pmin, inner_pmax = min(inner_parallel), max(inner_parallel)
inner_hmin, inner_hmax = min(inner_perp_proj), max(inner_perp_proj)
# Calculate valid placement ranges
valid_pmin = outer_pmin - inner_pmin
valid_pmax = outer_pmax - inner_pmax
valid_hmin = outer_hmin - inner_hmin
valid_hmax = outer_hmax - inner_hmax
if valid_pmax <= valid_pmin or valid_hmax <= valid_hmin:
glog.warning("rectangle not fit")
return None
# 4. Randomly select a valid position
random_p = np.random.uniform(valid_pmin, valid_pmax)
random_h = np.random.uniform(valid_hmin, valid_hmax)
# 5. Calculate translation vector
translation = (
outer_rect[0] + random_p * outer_dir + random_h * outer_perp - inner_center
)
return translation
@staticmethod
def point_side_of_line(point, line):
return np.sign(np.cross(line[1] - line[0], point - line[0]))
@staticmethod
def closest_point_on_line(point, a, b, c):
eps = Basic2DGeometry.get_eps()
x = (b * (b * point[0] - a * point[1]) - a * c) / (a**2 + b**2 + eps)
y = (a * (-b * point[0] + a * point[1]) - b * c) / (a**2 + b**2 + eps)
return np.array([x, y])
@staticmethod
def point_to_line_distance(point, line):
return np.linalg.norm(
np.cross(line[1] - line[0], line[0] - point)
) / np.linalg.norm(line[1] - line[0] + 1e-8)
@staticmethod
def line2abc(line):
start, end = line[0], line[1]
a = end[1] - start[1]
b = start[0] - end[0]
c = -a * start[0] - b * start[1]
return a, b, c
@staticmethod
def point_to_line(point, line):
a, b, c = Basic2DGeometry.line2abc(line)
return Basic2DGeometry.closest_point_on_line(point, a, b, c)
@staticmethod
def point_in_polygon(point, polygon):
n = len(polygon)
polygon.append(polygon[0])
signs = [
(polygon[i][0] - point[0]) * (polygon[i + 1][1] - point[1])
- (polygon[i + 1][0] - point[0]) * (polygon[i][1] - point[1])
for i in range(n)
]
return all(sign > 0 for sign in signs) or all(sign < 0 for sign in signs)
@staticmethod
def point_in_parallel_lines(point, line1, line2):
eps = Basic2DGeometry.get_eps()
direction1 = (line1[1] - line1[0]) / np.linalg.norm(line1[1] - line1[0] + eps)
direction2 = (line2[1] - line2[0]) / np.linalg.norm(line2[1] - line2[0] + eps)
start1, start2 = line1[0], line2[0]
perpendicular_direction1 = np.array([-direction1[1], direction1[0]])
perpendicular_direction2 = np.array([-direction2[1], direction2[0]])
distance_to_line1 = np.dot(point - start1, perpendicular_direction1)
distance_to_line2 = np.dot(point - start2, perpendicular_direction2)
return (distance_to_line1 >= -eps and distance_to_line2 <= eps) or (
distance_to_line1 <= eps and distance_to_line2 >= -eps
)
@staticmethod
def cal_vector_cos(vector1, vector2):
return np.dot(vector1, vector2) / (
np.linalg.norm(vector1) * np.linalg.norm(vector2)
)
@staticmethod
def is_fitable_in_rectangle_union(target_rectangle, rectangle_list):
return PolygonProcessor.is_rectangle_fitable_in_rectangle_union(
target_rectangle, rectangle_list
)
@staticmethod
def cal_rectangle_with_diagonal_line_n_heading(diagonal_line, heading=(1, 0)):
heading = np.array(heading) / np.linalg.norm(heading)
perpendicular_heading = np.array([-heading[1], heading[0]])
start, end = diagonal_line[0], diagonal_line[1]
if np.dot(end - start, heading) < 0:
heading = -heading
if np.dot(end - start, perpendicular_heading) < 0:
perpendicular_heading = -perpendicular_heading
width, height = np.dot(end - start, heading), np.dot(
end - start, perpendicular_heading
)
return [
start,
start + heading * width,
start + heading * width + perpendicular_heading * height,
start + perpendicular_heading * height,
]
@staticmethod
def rectangle_affordable(rectangle_a, rectangle_b):
# check if all points of rectangle_b are inside rectangle_a
center_a = np.mean(rectangle_a, axis=0)
center_b = np.mean(rectangle_b, axis=0)
translated_rectangle_b = rectangle_b - center_b + center_a
for point in translated_rectangle_b:
if not Basic2DGeometry.is_inside_rectangle(
point, rectangle_a, on_edge=True
):
return False
return True
@staticmethod
def align_rectangle_vertice(rectangle):
# Calculate the center of the rectangle
center = np.mean(rectangle, axis=0)
# Define a function to determine the quadrant of a point relative to the center
def quadrant(point):
if point[0] <= center[0] and point[1] <= center[1]:
return 0 # Rear-left
elif point[0] <= center[0] and point[1] > center[1]:
return 1 # Front-left
elif point[0] > center[0] and point[1] > center[1]:
return 2 # Front-right
else:
return 3 # Rear-right
# Sort the vertices based on their quadrant
sorted_rectangle = sorted(rectangle, key=quadrant)
return np.array(sorted_rectangle)
@staticmethod
def normalize_rectangle(rectangle):
edge1 = rectangle[3] - rectangle[0]
angle = np.arctan2(edge1[1], edge1[0])
return Basic2DGeometry.rotate_point_counterclockwise(rectangle, angle), angle
@staticmethod
def unnormalize_rectangle(rectangle, angle):
# import ipdb
# ipdb.set_trace()
return Basic2DGeometry.rotate_point_counterclockwise(rectangle, -angle)
class Basic3DGeometry:
def __init__(self):
pass
@staticmethod
def point_to_line_distance(point, line):
return np.linalg.norm(
np.cross(line[1] - line[0], line[0] - point)
) / np.linalg.norm(line[1] - line[0])
@staticmethod
def point_to_line_segment_distance(point, segment):
start, end = segment[0], segment[1]
if np.allclose(start, end):
return np.linalg.norm(point - start)
direction = end - start
t = np.dot(point - start, direction) / np.dot(direction, direction)
if t < 0:
return np.linalg.norm(point - start)
elif t > 1:
return np.linalg.norm(point - end)
else:
projection = start + t * direction
return np.linalg.norm(point - projection)
@staticmethod
def point_to_plane_distance(point, plane):
"""
Calculate the distance from a point to a plane defined by a point and a normal vector.
Parameters:
- point: The point [x, y, z]
- plane: A tuple (plane_point, normal_vector) where:
- plane_point: A point on the plane [x0, y0, z0]
- normal_vector: The normal vector of the plane [nx, ny, nz]
Returns:
- The distance from the point to the plane
"""
plane_point, normal_vector = plane
normal_vector = normal_vector / np.linalg.norm(normal_vector)
# Calculate the vector from the plane point to the point
point_vector = point - plane_point
# Calculate the distance using the dot product
distance = np.abs(np.dot(point_vector, normal_vector))
return distance
@staticmethod
def point_to_triangle_distance(point, triangle):
"""
Calculate the distance from a point to a triangle defined by its vertices.
Parameters:
- point: The point [x, y, z]
- triangle: A list of three vertices of the triangle [[x1, y1, z1], [x2, y2, z2], [x3, y3, z3]]
Returns:
- The distance from the point to the triangle
"""
triangle = np.array(triangle)
v0 = triangle[1] - triangle[0]
v1 = triangle[2] - triangle[0]
v2 = point - triangle[0]
d00 = np.dot(v0, v0)
d01 = np.dot(v0, v1)
d11 = np.dot(v1, v1)
d20 = np.dot(v2, v0)
d21 = np.dot(v2, v1)
denom = d00 * d11 - d01 * d01
if denom == 0:
return np.linalg.norm(point - triangle[0])
# Barycentric coordinates
v = (d11 * d20 - d01 * d21) / denom
w = (d00 * d21 - d01 * d20) / denom
u = 1 - v - w
# Check if the point is inside the triangle
if u >= 0 and v >= 0 and w >= 0:
# The point is inside the triangle, return the distance to the plane of the triangle
plane_point = triangle[0]
normal_vector = np.cross(v0, v1)
return Basic3DGeometry.point_to_plane_distance(
point, (plane_point, normal_vector)
)
else:
# The point is outside the triangle, calculate the distance to the closest edge or vertex
distances = []
for i in range(3):
edge_start = triangle[i]
edge_end = triangle[(i + 1) % 3]
distances.append(
Basic3DGeometry.point_to_line_segment_distance(
point, [edge_start, edge_end]
)
)
# Also consider the distance to the vertices
distances.extend([np.linalg.norm(point - vertex) for vertex in triangle])
return min(distances)
@staticmethod
def quaternion_between_vectors(v1, v2):
"""
calculate the rotation quaternion from vector v1 to vector v2.
Parameters:
- v1: [x1, y1, z1]
- v2: [x2, y2, z2]
Returns:
- quaternion [x, y, z, w]
"""
# normalize vectors
v1 = v1 / np.linalg.norm(v1)
v2 = v2 / np.linalg.norm(v2)
# calculate rotation axis
axis = np.cross(v1, v2)
axis_norm = np.linalg.norm(axis)
# special handling if vectors are parallel or anti-parallel
if axis_norm == 0:
if np.dot(v1, v2) > 0:
return np.array([0, 0, 0, 1]) # parallel vectors, no rotation needed
else:
# anti-parallel vectors, choose any perpendicular vector as rotation axis
orthogonal_vector = (
np.array([1, 0, 0])
if abs(v1[0]) < abs(v1[1])
else np.array([0, 1, 0])
)
axis = np.cross(v1, orthogonal_vector)
axis = axis / np.linalg.norm(axis)
return np.concatenate((axis, [0]))
axis = axis / axis_norm
# calculate rotation angle
angle = np.arccos(np.dot(v1, v2))
# calculate rotation quaternion
quaternion = R.from_rotvec(angle * axis).as_quat()
return quaternion
@staticmethod
def quaternion_from_vector(vector, angle):
"""
Calculate the rotation quaternion from the rotation axis vector and angle.
Parameters:
- vector: rotation axis vector[x, y, z]
- angle: rotation angle in radians
Returns:
- [x, y, z, w]
"""
# normalize vector
vector = vector / np.linalg.norm(vector)
# calculate rotation quaternion
quaternion = R.from_rotvec(angle * vector).as_quat()
return quaternion
@staticmethod
def vector_to_rpy(vector):
# normalize vector
vector = vector / np.linalg.norm(vector)
# calculate rotation matrix
z_axis = vector
x_axis = np.array([1, 0, 0])
y_axis = np.cross(z_axis, x_axis)
y_axis = y_axis / np.linalg.norm(y_axis)
x_axis = np.cross(y_axis, z_axis)
rotation_matrix = np.vstack([x_axis, y_axis, z_axis]).T
# calculate rpy
r = R.from_matrix(rotation_matrix)
rpy = r.as_euler("xyz", degrees=True)
return rpy
@staticmethod
def quaternion_between_vectors(v1, v2):
"""
Calculate the rotation quaternion from vector v1 to vector v2.
Parameters:
- v1: The first vector [x1, y1, z1]
- v2: The second vector [x2, y2, z2]
Returns:
- Rotation quaternion [x, y, z, w]
"""
# Normalize vectors
v1 = v1 / np.linalg.norm(v1)
v2 = v2 / np.linalg.norm(v2)
# Calculate rotation axis
axis = np.cross(v1, v2)
axis_norm = np.linalg.norm(axis)
# Special handling if vectors are parallel or anti-parallel
if axis_norm == 0:
if np.dot(v1, v2) > 0:
# Parallel vectors, no rotation needed
return np.array([0, 0, 0, 1])
else:
# Anti-parallel vectors, choose any perpendicular vector as rotation axis
orthogonal_vector = (
np.array([1, 0, 0])
if abs(v1[0]) < abs(v1[1])
else np.array([0, 1, 0])
)
axis = np.cross(v1, orthogonal_vector)
axis = axis / np.linalg.norm(axis)
return np.concatenate((axis, [0]))
axis = axis / axis_norm
# Calculate rotation angle
angle = np.arccos(np.dot(v1, v2))
# Calculate rotation quaternion
quaternion = R.from_rotvec(angle * axis).as_quat()
return quaternion
@staticmethod
def ray_triangle_intersection(ray_origin, ray_direction, triangle, eps=1e-6):
"""
Calculate the intersection point of a ray and a triangle.
Parameters:
- ray_origin: The origin of the ray [x, y, z]
- ray_direction: The direction of the ray [dx, dy, dz]
- triangle: The vertices of the triangle [[x1, y1, z1], [x2, y2, z2], [x3, y3, z3]]
Returns:
- intersection_point: The intersection point [x, y, z] or None if no intersection
"""
edge1, edge2 = triangle[1] - triangle[0], triangle[2] - triangle[0]
h = np.cross(ray_direction, edge2)
a = np.dot(edge1, h)
if -eps < a < eps:
# This ray is parallel to this triangle.
return None
f = 1.0 / a
s = ray_origin - triangle[0]
u = f * np.dot(s, h)
if u < 0.0 or u > 1.0:
return None
q = np.cross(s, edge1)
v = f * np.dot(ray_direction, q)
if v < 0.0 or u + v > 1.0:
return None
t = f * np.dot(edge2, q)
if t > eps: # ray intersection
intersection_point = ray_origin + ray_direction * t
return intersection_point
else: # This means that there is a line intersection but not a ray intersection.
return None
|