-
Notifications
You must be signed in to change notification settings - Fork 0
/
vectorTools.py
127 lines (108 loc) · 3.46 KB
/
vectorTools.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
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
import functools
from typing import Iterator
import numpy as np
import glm
from glm import ivec3, ivec2
from gdpc.gdpc.vector_tools import Box, Rect
@functools.cache
def getNextPosition(
facing: int = 0,
currentBox: Box = None,
nextBox: Box = None,
offset: ivec3 = ivec3(0, 0, 0)
) -> ivec3:
if currentBox is None:
currentBox = Box()
if nextBox is None:
nextBox = Box()
currentCenter = ivec3(currentBox.center.x, currentBox.offset.y, currentBox.center.z)
nextCenter = ivec3(currentBox.size.x + nextBox.center.x, nextBox.offset.y, currentCenter.z) + offset
nextPoint = rotatePointAroundOrigin3D(
origin=currentCenter,
point=nextCenter,
rotation=facing
)
nextPoint = nextPoint - ivec3(nextBox.middle.x, 0, nextBox.middle.z)
return nextPoint
@functools.cache
def rotatePointAroundOrigin3D(
origin: ivec3 = ivec3(0, 0, 0),
point: ivec3 = ivec3(0, 0, 0),
rotation: int = 0
) -> ivec3:
if rotation == 0:
return point
angle = np.deg2rad(rotation * 90)
return ivec3(
int(np.round(np.cos(angle) * (point.x - origin.x) - np.sin(angle) * (point.z - origin.z) + origin.x)),
point.y,
int(np.round(np.sin(angle) * (point.x - origin.x) + np.cos(angle) * (point.z - origin.z) + origin.z))
)
@functools.cache
def rotatePointAroundOrigin2D(
origin: ivec2 = ivec3(0, 0, 0),
point: ivec2 = ivec3(0, 0, 0),
rotation: int = 0
) -> ivec2:
if rotation == 0:
return point
angle = np.deg2rad(rotation * 90)
return ivec2(
int(np.round(np.cos(angle) * (point.x - origin.x) - np.sin(angle) * (point.y - origin.y) + origin.x)),
int(np.round(np.sin(angle) * (point.x - origin.x) + np.cos(angle) * (point.y - origin.y) + origin.y))
)
@functools.cache
def isRectinRect(rectA: Rect, rectB: Rect) -> bool:
return (
rectB.begin.x >= rectA.begin.x and
rectB.begin.y >= rectA.begin.y and
rectB.end.x <= rectA.last.x and
rectB.end.y <= rectA.last.y
)
@functools.cache
def addVec2ToVec3(a: ivec2 = ivec2(0, 0), b: ivec2 = ivec2(0, 0), y: int = 0) -> ivec3:
return ivec3(a.x + b.x, y, a.y + b.y)
@functools.cache
def loop2DwithStride(
begin: ivec2 = ivec2(0, 0),
end: ivec2 = ivec2(0, 0),
stride: ivec2 | int = ivec2(1, 1)
) -> Iterator[ivec2]:
if isinstance(stride, int):
stride = ivec2(1, 1) * stride
for x in range(begin.x, end.x, stride.x):
for y in range(begin.y, end.y, stride.y):
yield ivec2(x, y)
@functools.cache
def loop2DwithRects(
begin: ivec2 = ivec2(0, 0),
end: ivec2 = ivec2(0, 0),
stride: ivec2 = ivec2(1, 1)
) -> Iterator[Rect]:
for x in range(begin.x, end.x, stride.x):
for y in range(begin.y, end.y, stride.y):
newRectOffset = ivec2(x, y)
newRectSize = stride
# noinspection PyTypeChecker
newRect = Rect(
offset=newRectOffset,
size=newRectSize
)
newRect.end = glm.min(newRect.end, end)
yield newRect
def mergeRects(
rectList: list[Rect],
) -> Rect:
if len(rectList) == 1:
return rectList[0]
rectStarts: list[ivec2] = []
rectEnds: list[ivec2] = []
for rect in rectList:
rectStarts.append(rect.begin)
rectEnds.append(rect.end)
# noinspection PyTypeChecker
rect = Rect(
offset=glm.min(rectStarts),
)
rect.end = glm.max(rectEnds)
return rect