/usr/share/pythoncad/PythonCAD/Generic/point.py is in pythoncad 0.1.37.0-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | #
# Copyright (c) 2002, 2003, 2004, 2005 Art Haas
#
# This file is part of PythonCAD.
#
# PythonCAD is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# PythonCAD is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PythonCAD; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# classes for points
#
from __future__ import generators
import math
from PythonCAD.Generic import tolerance
from PythonCAD.Generic import util
from PythonCAD.Generic import baseobject
from PythonCAD.Generic import quadtree
from PythonCAD.Generic import entity
class Point(baseobject.Subpart):
"""A 2-D point Class.
A Point has the following attributes:
x: x-coordinate
y: y-coordinate
A Point object has the following methods:
{get/set}x(): Get/Set the x-coordinate of the Point.
{get/set}y(): Get/Set the y-coordinate of the Point.
{get/set}Coords(): Get/Set both the x and y coordinates of the Point.
move(): Move a Point.
clone(): Return an identical copy of a Point.
inRegion(): Returns True if the point is in some area.
"""
__messages = {
'moved' : True,
}
# member functions
def __init__(self, x, y=None, **kw):
"""
Initialize a Point.
There are two ways to initialize a Point:
Point(xc,yc) - Two arguments, with both arguments being floats
Point((xc,yc)) - A single tuple containing two float objects
"""
super(Point, self).__init__(**kw)
if isinstance(x, tuple):
if y is not None:
raise SyntaxError, "Invalid call to Point()"
_x, _y = util.tuple_to_two_floats(x)
elif y is not None:
_x = util.get_float(x)
_y = util.get_float(y)
else:
raise SyntaxError, "Invalid call to Point()."
self.__x = _x
self.__y = _y
def __str__(self):
return "(%g,%g)" % (self.__x, self.__y)
def __sub__(self, p):
"""Return the separation between two points.
This function permits the use of '-' to be an easy to read
way to find the distance between two Point objects.
"""
if not isinstance(p, Point):
raise TypeError, "Invalid type for Point subtraction: " + `type(p)`
_px, _py = p.getCoords()
return math.hypot((self.__x - _px), (self.__y - _py))
def __eq__(self, obj):
"""Compare a Point to either another Point or a tuple for equality.
"""
if not isinstance(obj, (Point,tuple)):
return False
if isinstance(obj, Point):
if obj is self:
return True
_x, _y = obj.getCoords()
else:
_x, _y = util.tuple_to_two_floats(obj)
if abs(self.__x - _x) < 1e-10 and abs(self.__y - _y) < 1e-10:
return True
return False
def __ne__(self, obj):
"""
Compare a Point to either another Point or a tuple for inequality.
"""
if not isinstance(obj, (Point,tuple)):
return True
if isinstance(obj, Point):
if obj is self:
return False
_x, _y = obj.getCoords()
else:
_x, _y = util.tuple_to_two_floats(obj)
if abs(self.__x - _x) < 1e-10 and abs(self.__y - _y) < 1e-10:
return False
return True
def __add__(self,obj):
"""
Add two Point
"""
if not isinstance(obj, Point):
if isinstance(obj, tuple):
x, y = util.tuple_to_two_floats(obj)
else:
raise TypeError,"Invalid Argument obj: Point or tuple Required"
else:
x,y = obj.getCoords()
return self.__x+x,self.__y+y
def finish(self):
try: #Fix the setx to None exeption
self.x = self.y = None
super(Point, self).finish()
except:
return
def getValues(self):
"""
Return values comprising the Point.
getValues()
This method extends the Subpart::getValues() method.
"""
_data = super(Point, self).getValues()
_data.setValue('type', 'point')
_data.setValue('x', self.__x)
_data.setValue('y', self.__y)
return _data
def getx(self):
"""
Return the x-coordinate of a Point.
getx()
"""
return self.__x
def setx(self, val):
"""
Set the x-coordinate of a Point
setx(val)
The argument 'val' must be a float.
"""
if self.isLocked():
raise RuntimeError, "Coordinate change not allowed - object locked."
_v = util.get_float(val)
_x = self.__x
if abs(_x - _v) > 1e-10:
self.startChange('moved')
self.__x = _v
self.endChange('moved')
self.sendMessage('moved', _x, self.__y)
self.modified()
x = property(getx, setx, None, "x-coordinate value")
def gety(self):
"""Return the y-coordinate of a Point.
gety()
"""
return self.__y
def sety(self, val):
"""Set the y-coordinate of a Point
sety(val)
The argument 'val' must be a float.
"""
if self.isLocked():
raise RuntimeError, "Coordinate change not allowed - object locked."
_v = util.get_float(val)
_y = self.__y
if abs(_y - _v) > 1e-10:
self.startChange('moved')
self.__y = _v
self.endChange('moved')
self.sendMessage('moved', self.__x, _y)
self.modified()
y = property(gety, sety, None, "y-coordinate value")
def getCoords(self):
"""Return the x and y Point coordinates in a tuple.
getCoords()
"""
return self.__x, self.__y
def setCoords(self, x, y):
"""Set both the coordinates of a Point.
setCoords(x, y)
Arguments 'x' and 'y' should be float values.
"""
_x = util.get_float(x)
_y = util.get_float(y)
_sx = self.__x
_sy = self.__y
if abs(_sx - _x) > 1e-10 or abs(_sy - _y) > 1e-10:
self.startChange('moved')
self.__x = _x
self.__y = _y
self.endChange('moved')
self.sendMessage('moved', _sx, _sy)
self.modified()
def move(self, dx, dy):
"""
Move a Point.
The first argument gives the x-coordinate displacement,
and the second gives the y-coordinate displacement. Both
values should be floats.
"""
if self.isLocked():
raise RuntimeError, "Moving not allowed - object locked."
_dx = util.get_float(dx)
_dy = util.get_float(dy)
if abs(_dx) > 1e-10 or abs(_dy) > 1e-10:
_x = self.__x
_y = self.__y
self.startChange('moved')
self.__x = _x + _dx
self.__y = _y + _dy
self.endChange('moved')
self.sendMessage('moved', _x, _y)
self.modified()
def clone(self):
"""
Create an identical copy of a Point.
"""
return Point(self.__x, self.__y)
def inRegion(self, xmin, ymin, xmax, ymax, fully=True):
"""
Returns True if the Point is within the bounding values.
inRegion(xmin, ymin, xmax, ymax)
The four arguments define the boundary of an area, and the
function returns True if the Point lies within that area.
Otherwise, the function returns False.
"""
_xmin = util.get_float(xmin)
_ymin = util.get_float(ymin)
_xmax = util.get_float(xmax)
if _xmax < _xmin:
raise ValueError, "Illegal values: xmax < xmin"
_ymax = util.get_float(ymax)
if _ymax < _ymin:
raise ValueError, "Illegal values: ymax < ymin"
util.test_boolean(fully)
_x = self.__x
_y = self.__y
return not ((_x < _xmin) or
(_x > _xmax) or
(_y < _ymin) or
(_y > _ymax))
def sendsMessage(self, m):
if m in Point.__messages:
return True
return super(Point, self).sendsMessage(m)
def Dist(self,obj):
"""
Get The Distance From 2 Points
"""
if not isinstance(obj, Point):
if isinstance(x, tuple):
_x, _y = util.tuple_to_two_floats(obj)
else:
raise TypeError,"Invalid Argument point: Point or Tuple Required"
else:
x,y=obj.getCoords()
xDist=x-self.__x
yDist=y-self.__y
return math.sqrt(pow(xDist,2)+pow(yDist,2))
#
# Quadtree Point storage
#
class PointQuadtree(quadtree.Quadtree):
def __init__(self):
super(PointQuadtree, self).__init__()
def getNodes(self, *args):
_alen = len(args)
if _alen != 2:
raise ValueError, "Expected 2 arguments, got %d" % _alen
_x = util.get_float(args[0])
_y = util.get_float(args[1])
_nodes = [self.getTreeRoot()]
while len(_nodes):
_node = _nodes.pop()
_xmin, _ymin, _xmax, _ymax = _node.getBoundary()
if ((_x < _xmin) or
(_y < _ymin) or
(_x > _xmax) or
(_y > _ymax)):
continue
if _node.hasSubnodes():
_xmid = (_xmin + _xmax)/2.0
_ymid = (_ymin + _ymax)/2.0
_ne = _nw = _sw = _se = False
#
# NE node (xmid,ymid) to (xmax,ymax)
#
if not ((_x < _xmid) or (_y < _ymid)):
_ne = True
#
# NW node (xmin,ymid) to (xmid,ymax)
#
if not ((_x > _xmid) or (_y < _ymid)):
_nw = True
#
# SW node (xmin,ymin) to (xmid,ymid)
#
if not ((_x > _xmid) or (_y > _ymid)):
_sw = True
#
# SE node (xmid,ymin) to (xmax,ymid)
#
if not ((_x < _xmid) or (_y > _ymid)):
_se = True
if _ne:
_nodes.append(_node.getSubnode(quadtree.QTreeNode.NENODE))
if _nw:
_nodes.append(_node.getSubnode(quadtree.QTreeNode.NWNODE))
if _sw:
_nodes.append(_node.getSubnode(quadtree.QTreeNode.SWNODE))
if _se:
_nodes.append(_node.getSubnode(quadtree.QTreeNode.SENODE))
else:
yield _node
def addObject(self, obj):
if not isinstance(obj, Point):
raise TypeError, "Invalid Point object: " + `type(obj)`
if obj in self:
return
_x, _y = obj.getCoords()
_bounds = self.getTreeRoot().getBoundary()
_xmin = _ymin = _xmax = _ymax = None
_resize = False
if _bounds is None: # first node in tree
_resize = True
_xmin = _x - 1.0
_ymin = _y - 1.0
_xmax = _x + 1.0
_ymax = _y + 1.0
else:
_xmin, _ymin, _xmax, _ymax = _bounds
if _x < _xmin:
_xmin = _x - 1.0
_resize = True
if _x > _xmax:
_xmax = _x + 1.0
_resize = True
if _y < _ymin:
_ymin = _y - 1.0
_resize = True
if _y > _ymax:
_ymax = _y + 1.0
_resize = True
if _resize:
self.resize(_xmin, _ymin, _xmax, _ymax)
for _node in self.getNodes(_x, _y):
_node.addObject(obj)
super(PointQuadtree, self).addObject(obj)
obj.connect('moved', self._movePoint)
def delObject(self, obj):
if obj not in self:
return
_x, _y = obj.getCoords()
for _node in self.getNodes(_x, _y):
_node.delObject(obj)
_parent = _node.getParent()
if _parent is not None:
self.purgeSubnodes(_parent)
super(PointQuadtree, self).delObject(obj)
obj.disconnect(self)
def find(self, *args):
_alen = len(args)
if _alen < 2:
raise ValueError, "Invalid argument count: %d" % _alen
_x = util.get_float(args[0])
_y = util.get_float(args[1])
_t = tolerance.TOL
if _alen > 2 :
_t = tolerance.toltest(args[2])
return self.getInRegion((_x - _t), (_y - _t), (_x + _t), (_y + _t))
def _movePoint(self, obj, *args):
if obj not in self:
raise ValueError, "Point not stored in Quadtree: " + `obj`
_alen = len(args)
if len(args) < 2:
raise ValueError, "Invalid argument count: %d" % _alen
_x = util.get_float(args[0])
_y = util.get_float(args[1])
for _node in self.getNodes(_x, _y):
_node.delObject(obj)
super(PointQuadtree, self).delObject(obj)
obj.disconnect(self)
self.addObject(obj)
def getClosest(self, x, y, tol=tolerance.TOL):
return self.find(x, y, tol)
def getInRegion(self, xmin, ymin, xmax, ymax):
_xmin = util.get_float(xmin)
_ymin = util.get_float(ymin)
_xmax = util.get_float(xmax)
if _xmax < _xmin:
raise ValueError, "Illegal values: xmax < xmin"
_ymax = util.get_float(ymax)
if _ymax < _ymin:
raise ValueError, "Illegal values: ymax < ymin"
_pts = []
if not len(self):
return _pts
_nodes = [self.getTreeRoot()]
while len(_nodes):
_node = _nodes.pop()
if _node.hasSubnodes():
for _subnode in _node.getSubnodes():
_sxmin, _symin, _sxmax, _symax = _subnode.getBoundary()
if ((_sxmin > _xmax) or
(_symin > _ymax) or
(_sxmax < _xmin) or
(_symax < _ymin)):
continue
_nodes.append(_subnode)
else:
for _pt in _node.getObjects():
if _pt.inRegion(_xmin, _ymin, _xmax, _ymax):
_pts.append(_pt)
return _pts
#
# Point history class
#
class PointLog(entity.EntityLog):
def __init__(self, p):
if not isinstance(p, Point):
raise TypeError, "Invalid point: " + `type(p)`
super(PointLog, self).__init__(p)
p.connect('moved', self.__movePoint)
def __movePoint(self, p, *args):
_alen = len(args)
if _alen < 2:
raise ValueError, "Invalid argument count: %d" % _alen
_x = args[0]
if not isinstance(_x, float):
raise TypeError, "Unexpected type for 'x': " + `type(_x)`
_y = args[1]
if not isinstance(_y, float):
raise TypeError, "Unexpected type for 'y': " + `type(_y)`
self.saveUndoData('moved', _x, _y)
def execute(self, undo, *args):
util.test_boolean(undo)
_alen = len(args)
if _alen == 0:
raise ValueError, "No arguments to execute()"
_p = self.getObject()
_op = args[0]
if _op == 'moved':
if _alen < 3:
raise ValueError, "Invalid argument count: %d" % _alen
_x = args[1]
if not isinstance(_x, float):
raise TypeError, "Unexpected type for 'x': " + `type(_x)`
_y = args[2]
if not isinstance(_y, float):
raise TypeError, "Unexpected type for 'y': " + `type(_y)`
_px, _py = _p.getCoords()
self.ignore(_op)
try:
if undo:
_p.startUndo()
try:
_p.setCoords(_x, _y)
finally:
_p.endUndo()
else:
_p.startRedo()
try:
_p.setCoords(_x, _y)
finally:
_p.endRedo()
finally:
self.receive(_op)
self.saveData(undo, _op, _px, _py)
else:
super(PointLog, self).execute(undo, *args)
|