Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

math_.py 1.8 KB

You have to be logged in to leave a comment. Sign In
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
  1. import math
  2. import numpy as np
  3. import numpy.linalg as npla
  4. def rotation_matrix_to_euler(R : np.ndarray) -> np.ndarray:
  5. sy = math.sqrt(R[0,0] * R[0,0] + R[1,0] * R[1,0])
  6. singular = sy < 1e-6
  7. if not singular :
  8. x = math.atan2(R[2,1] , R[2,2])
  9. y = math.atan2(-R[2,0], sy)
  10. z = math.atan2(R[1,0], R[0,0])
  11. else :
  12. x = math.atan2(-R[1,2], R[1,1])
  13. y = math.atan2(-R[2,0], sy)
  14. z = 0
  15. return np.array([x, y, z])
  16. def segment_length(p1 : np.ndarray, p2 : np.ndarray):
  17. """
  18. p1 (2,)
  19. p2 (2,)
  20. """
  21. return npla.norm(p2-p1)
  22. def segment_to_vector(p1 : np.ndarray, p2 : np.ndarray):
  23. """
  24. p1 (2,)
  25. p2 (2,)
  26. """
  27. x = p2-p1
  28. x /= npla.norm(x)
  29. return x
  30. def intersect_two_line(a1, a2, b1, b2) -> np.ndarray:
  31. """
  32. Returns the point of intersection of the lines (not segments) passing through a2,a1 and b2,b1.
  33. a1: [x, y] a point on the first line
  34. a2: [x, y] another point on the first line
  35. b1: [x, y] a point on the second line
  36. b2: [x, y] another point on the second line
  37. """
  38. s = np.vstack([a1,a2,b1,b2]) # s for stacked
  39. h = np.hstack((s, np.ones((4, 1)))) # h for homogeneous
  40. l1 = np.cross(h[0], h[1]) # get first line
  41. l2 = np.cross(h[2], h[3]) # get second line
  42. x, y, z = np.cross(l1, l2) # point of intersection
  43. if z == 0: # lines are parallel
  44. return (float('inf'), float('inf'))
  45. return np.array( [x/z, y/z], np.float32 )
  46. def polygon_area(poly : np.ndarray) -> float:
  47. """
  48. calculate area of n-vertices polygon with non intersecting edges
  49. poly np.ndarray (n,2)
  50. """
  51. return float( np.abs(np.sum( poly[:,0] * np.roll( poly[:,1], -1 ) - poly[:,1] * np.roll( poly[:,0], -1 ) ) / 2) )
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...