Spaces:
Sleeping
Sleeping
File size: 11,262 Bytes
26f7fa0 |
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 |
"""
MLSTRUCT-FP - DB - LINE 2D
Line definition class.
"""
__all__ = [
'BoundingBox',
'GeomLine2D',
'GeomPoint2D'
]
from MLStructFP.utils._mathlib import dist2
from MLStructFP._types import Dict, Any, List, Union, Tuple, NumberType, NumberInstance
import math
import matplotlib.pyplot as plt
MIN_TOL: float = 1e-12
class GeomLine2D(object):
"""
Geometric 2d segment between 2 points.
"""
_def_points: Dict[str, 'GeomPoint2D']
m: NumberType
n: NumberType
theta: NumberType
def __init__(self) -> None:
"""
Constructor.
"""
self.m = 0.0
self.theta = 0.0
self.n = 0.0
self._def_points = dict(
p1=GeomPoint2D(),
p2=GeomPoint2D()
)
def clone(self) -> 'GeomLine2D':
"""
Returns a cloned object.
:return: New line
"""
line = GeomLine2D()
line.m = self.m
line.theta = self.theta
line.n = self.n
line._def_points['p1'] = self._def_points['p1'].clone()
line._def_points['p2'] = self._def_points['p2'].clone()
return line
def __str__(self) -> str:
return f'y = {self.m}*x + {self.n}. Theta: {self.theta}'
def __repr__(self) -> str:
return str(self)
def from_2_points(self, p1: 'GeomPoint2D', p2: 'GeomPoint2D') -> 'GeomLine2D':
"""
Set line from 2 points.
:param p1: Point 1
:param p2: Point 2
:return: Self
"""
self._def_points['p1'] = p1.clone()
self._def_points['p2'] = p2.clone()
m_a = p2.y - p1.y
m_b = p2.x - p1.x + MIN_TOL
self.m = m_a / m_b
self.n = (-self.m * p1.x) + p2.y
self.theta = math.atan2(m_a, m_b)
return self
def eval(self, x: NumberType) -> NumberType:
"""
Eval line at given value.
:param x: Evaluation point
:return: Evaluated Ƒ(x)
"""
return (self.m * x) + self.n
def f(self, x: NumberType) -> 'GeomPoint2D':
"""
Returns new point from (x,Ƒ(x)).
:param x: Evaluation point
:return: New point
"""
return GeomPoint2D(x, self.eval(x))
def _check_def_none(self) -> None:
"""
Checks defined points are none.
"""
if self._def_points['p1'] is None or self._def_points['p2'] is None:
self._def_points['p1'] = self.f(0)
self._def_points['p2'] = self.f(1)
def point_left(self, p: 'GeomPoint2D') -> bool:
"""
Check point is at left of the segment.
:param p: Point
:return: True if the point is located at the left of the segment
"""
self._check_def_none()
p1: 'GeomPoint2D' = self._def_points['p1']
p2: 'GeomPoint2D' = self._def_points['p2']
return (((p2.x - p1.x) * (p.y - p1.y)) - ((p2.y - p1.y) * (p.x - p1.x))) > 0
def point_on(self, p: 'GeomPoint2D') -> bool:
"""
Check point on segment.
:param p: Point
:return: True if point is on the segment
"""
self._check_def_none()
p1: 'GeomPoint2D' = self._def_points['p1']
p2: 'GeomPoint2D' = self._def_points['p2']
return math.fabs((((p2.x - p1.x) * (p.y - p1.y)) - ((p2.y - p1.y) * (p.x - p1.x)))) < MIN_TOL
def ortho_distance_point_list(self, point_list: List['GeomPoint2D']) -> List[NumberType]:
"""
Calculate the orthographic distance from a point.
:param point_list: Point list
:return: Distance list
"""
dist = []
for i in range(len(point_list)):
dist.append(self.ortho_distance_point(point_list[i]))
return dist
def ortho_distance_point(self, p: 'GeomPoint2D') -> NumberType:
"""
Returns the orthographic distance from a point.
:param p: Point
:return: Distance
"""
a = -self.m
b = 1
c = -self.n
return math.fabs((a * p.x) + (b * p.y) + c) / dist2(a, b)
def ortho_distance_line(self, line: 'GeomLine2D', force: bool = False) -> NumberType:
"""
Calculate the orthographic distance from another line.
:param line: Line
:param force: If true, vertical segments will be evaluated on x-axis
:return: Distance
"""
if math.fabs(math.fabs(self.m) - math.fabs(line.m)) > 1e-4:
if not force:
return math.inf
else:
return math.fabs(self.eval(0) - line.eval(0))
return math.fabs(self.n - line.n) / dist2(1, self.m)
class GeomPoint2D(object):
"""
2D coordinate.
"""
_data: Dict[str, Any]
x: NumberType
y: NumberType
def __init__(self, x: NumberType = 0, y: NumberType = 0) -> None:
"""
Constructor.
:param x: X coordinate
:param y: Y coordinate
"""
assert isinstance(x, NumberInstance)
assert isinstance(y, NumberInstance)
self.x = float(x)
self.y = float(y)
self._data = {} # Point inner data
def __eq__(self, other: 'GeomPoint2D') -> bool:
return math.fabs(self.x - other.x) <= MIN_TOL and math.fabs(self.y - other.y) <= MIN_TOL
def __ne__(self, other: 'GeomPoint2D') -> bool:
"""
Returns true if points are not equal.
:param other: Point
:return: bool
"""
return math.fabs(self.x - other.x) > MIN_TOL or math.fabs(self.y - other.y) > MIN_TOL
def __mul__(self, other: 'GeomPoint2D') -> 'GeomPoint2D':
"""
Multiply point with another.
:param other: Point
:return: Self
"""
self.x *= other.x
self.y *= other.y
return self
def __add__(self, other: 'GeomPoint2D') -> 'GeomPoint2D':
"""
Add point with another.
:param other: Point
:return: Self
"""
self.x += other.x
self.y += other.y
return self
def __sub__(self, other: 'GeomPoint2D') -> 'GeomPoint2D':
"""
Subtract point with another.
:param other: Point
:return: Self
"""
self.x -= other.x
self.y -= other.y
return self
def __str__(self) -> str:
return f'({self.x},{self.y})'
def __repr__(self) -> str:
return str(self)
def list(self) -> List[float]:
"""
Returns the point as a list.
:return: Point list [x,y]
"""
return [self.x, self.y]
def tuple(self) -> Tuple[float, float]:
"""
Returns the tuple of the point.
:return: Tuple (x, y)
"""
return self.x, self.y
def rotate(self, center: 'GeomPoint2D', angle: float = 0) -> 'GeomPoint2D':
"""
Rotate the point around a given center.
:param center: Rotation center
:param angle: Rotation angle, angles in degrees
:return: Self
"""
if angle == 0:
return self
s = math.sin(angle * math.pi / 180)
c = math.cos(angle * math.pi / 180)
# Translate
self.x -= center.x
self.y -= center.y
# Rotate
new_x = self.x * c - self.y * s
new_y = self.x * s + self.y * c
# Update
self.x = new_x + center.x
self.y = new_y + center.y
return self
def scale(self, s: float) -> 'GeomPoint2D':
"""
Scale point with number.
:param s: Scale factor
:return: Self
"""
self.x *= s
self.y *= s
return self
def dist(self, other: 'GeomPoint2D') -> float:
"""
Returns the distance between two points.
:param other: Point
:return: Distance
"""
return dist2(self.x, self.y, other.x, other.y)
def angle(self, other: Union['GeomPoint2D', List['GeomPoint2D']]) -> Union[float, List[float]]:
"""
Returns the angle between two points.
:param other: Point
:return: Angle
"""
if isinstance(other, list):
dist = []
for i in range(len(other)):
dist.append(self.angle(other[i]))
return dist
if self.x == other.x and self.y == other.y:
return 0.0
return math.atan2(other.y - self.y, other.x - self.x)
def set_property(self, key: str, data: Any) -> None:
"""
Set point property.
:param key: Key
:param data: Data
"""
if data is None:
return
assert isinstance(key, str)
self._data[key] = data
def get_property(self, key: str, default: Any = None) -> Any:
"""
Get point property.
:param key: Key
:param default: Data
:return: Value
"""
if not self.has_property(key):
return default
return self._data[key]
def has_property(self, key: str) -> bool:
"""
Returns true if point has data.
:param key: Key
:return: True if key exists
"""
return key in self._data.keys()
def clone(self) -> 'GeomPoint2D':
"""
Clone point.
:return: New point
"""
p = GeomPoint2D(self.x, self.y)
for k in self._data.keys():
p.set_property(k, self.get_property(k))
return p
def equals(self, other: 'GeomPoint2D') -> bool:
"""
Points is equal to another.
:param other: Point
:return: True if points is equal
"""
return math.fabs(self.x - other.x) < MIN_TOL and math.fabs(self.y - other.y) < MIN_TOL
def set_zero(self) -> None:
"""
Set point as zero.
"""
self.x = 0
self.y = 0
def plot(self, ax: 'plt.Axes', color='#000000', marker_size: int = 10, style: str = '.') -> None:
"""
Plot point.
:param ax: Matplotlib axes
:param color: Marker color
:param marker_size: Marker size
:param style: Marker style
"""
assert isinstance(ax, plt.Axes)
assert isinstance(color, str)
assert isinstance(marker_size, int)
assert isinstance(style, str)
ax.plot(self.x, self.y, style, markersize=marker_size, color=color)
class BoundingBox(object):
"""
Represents a bounding box from (xmin, xmax) to (ymin, ymax).
"""
__xmin: NumberType
__xmax: NumberType
__ymin: NumberType
__ymax: NumberType
def __init__(self, xmin: NumberType, xmax: NumberType, ymin: NumberType, ymax: NumberType) -> None:
"""
Constructor.
"""
self.__xmin = xmin
self.__xmax = xmax
self.__ymin = ymin
self.__ymax = ymax
@property
def xmin(self) -> NumberType:
return self.__xmin
@property
def xmax(self) -> NumberType:
return self.__xmax
@property
def ymin(self) -> NumberType:
return self.__ymin
@property
def ymax(self) -> NumberType:
return self.__ymax
def __repr__(self) -> str:
return f'BB: x({self.xmin},{self.xmax}), y({self.ymin},{self.ymax})'
|