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
|
#
# Copyright (c) 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
#
# quadtree storage for dimension objects
#
import math
from PythonCAD.Generic import dimension
from PythonCAD.Generic import quadtree
from PythonCAD.Generic import point
from PythonCAD.Generic import circle
from PythonCAD.Generic import arc
from PythonCAD.Generic import tolerance
from PythonCAD.Generic import util
#
# Dimension storage
#
class DimQuadtree(quadtree.Quadtree):
def __init__(self):
super(DimQuadtree, self).__init__()
def addObject(self, obj):
if not isinstance(obj, dimension.Dimension):
raise TypeError, "Invalid Dimension object: " + `obj`
if obj in self:
return
_dxmin, _dymin, _dxmax, _dymax = obj.getBounds()
_node = self.getTreeRoot()
_bounds = _node.getBoundary()
_xmin = _ymin = _xmax = _ymax = None
_resize = False
if _bounds is None: # first node in tree
_resize = True
_xmin = _dxmin - 1.0
_ymin = _dymin - 1.0
_xmax = _dxmax + 1.0
_ymax = _dymax + 1.0
else:
_xmin, _ymin, _xmax, _ymax = _bounds
if _dxmin < _xmin:
_xmin = _dxmin - 1.0
_resize = True
if _dxmax > _xmax:
_xmax = _dxmax + 1.0
_resize = True
if _dymin < _ymin:
_ymin = _dymin - 1.0
_resize = True
if _dymax > _ymax:
_ymax = _dymax + 1.0
_resize = True
if _resize:
self.resize(_xmin, _ymin, _xmax, _ymax)
_node = self.getTreeRoot()
_nodes = [_node]
while len(_nodes):
_node = _nodes.pop()
_xmin, _ymin, _xmax, _ymax = _node.getBoundary()
if ((_dxmin > _xmax) or
(_dymin > _ymax) or
(_dxmax < _xmin) or
(_dymax < _ymin)):
continue
if _node.hasSubnodes():
_xmid = (_xmin + _xmax)/2.0
_ymid = (_ymin + _ymax)/2.0
_ne = _nw = _sw = _se = True
if _dxmax < _xmid: # dim on left side
_ne = _se = False
if _dxmin > _xmid: # dim on right side
_nw = _sw = False
if _dymax < _ymid: # dim below
_nw = _ne = False
if _dymin > _ymid: # dim above
_sw = _se = False
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:
if obj.inRegion(_xmin, _ymin, _xmax, _ymax):
_node.addObject(obj)
super(DimQuadtree, self).addObject(obj)
obj.connect('moved', self._moveDim)
def delObject(self, obj):
if obj not in self:
return
_dxmin, _dymin, _dxmax, _dymax = obj.getBounds()
_xmin = _ymin = _xmax = _ymax = None
_pdict = {}
_nodes = [self.getTreeRoot()]
while len(_nodes):
_node = _nodes.pop()
_xmin, _ymin, _xmax, _ymax = _node.getBoundary()
if ((_dxmin > _xmax) or
(_dxmax < _xmin) or
(_dymin > _ymax) or
(_dymax < _ymin)):
continue
if _node.hasSubnodes():
_xmid = (_xmin + _xmax)/2.0
_ymid = (_ymin + _ymax)/2.0
_ne = _nw = _sw = _se = True
if _dxmax < _xmid: # dim on left side
_ne = _se = False
if _dxmin > _xmid: # dim on right side
_nw = _sw = False
if _dymax < _ymid: # dim below
_nw = _ne = False
if _dymin > _ymid: # dim above
_sw = _se = False
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:
_node.delObject(obj) # dim may not be in the node ...
_parent = _node.getParent()
if _parent is not None:
_pid = id(_parent)
if _pid not in _pdict:
_pdict[_pid] = _parent
super(DimQuadtree, self).delObject(obj)
obj.disconnect(self)
for _parent in _pdict.values():
self.purgeSubnodes(_parent)
def find(self, *args):
_alen = len(args)
if _alen < 4:
raise ValueError, "Invalid argument count: %d" % _alen
_l1 = args[0]
from PythonCAD.Generic.layer import Layer
if not isinstance(_l1, Layer):
raise TypeError, "Invalid layer: " + `_l1`
_p1 = args[1]
if not isinstance(_p1, point.Point):
raise TypeError, "Invalid point: " + `_p1`
_l2 = args[2]
if not isinstance(_l2, Layer):
raise TypeError, "Invalid layer: " + `_l2`
_p2 = args[3]
if not isinstance(_p2, point.Point):
raise TypeError, "Invalid point: " + `_p2`
if not len(self):
return None
for _dim in self.getObjects():
_dl1, _dl2 = _dim.getDimLayers()
_dp1, _dp2 = _dim.getDimPoints()
if ((_l1 is _dl1) and
(_p1 is _dp1) and
(_l2 is _dl2) and
(_p2 is _dp2)):
return _dim
if ((_l1 is _dl2) and
(_p1 is _dp2) and
(_l2 is _dl1) and
(_p2 is _dp1)):
return _dim
return None
def _moveDim(self, obj, *args):
if obj not in self:
raise ValueError, "Dimension not stored in Quadtree: " + `obj`
_alen = len(args)
if _alen < 4:
raise ValueError, "Invalid argument count: %d" % _alen
_dxmin = util.get_float(args[0])
_dymin = util.get_float(args[1])
_dxmax = util.get_float(args[2])
_dymax = util.get_float(args[3])
_nodes = [self.getTreeRoot()]
while len(_nodes):
_node = _nodes.pop()
_xmin, _ymin, _xmax, _ymax = _node.getBoundary()
if ((_dxmin > _xmax) or
(_dxmax < _xmin) or
(_dymin > _ymax) or
(_dymax < _ymin)):
continue
if _node.hasSubnodes():
_xmid = (_xmin + _xmax)/2.0
_ymid = (_ymin + _ymax)/2.0
_ne = _nw = _sw = _se = True
if _dxmax < _xmid: # dim on left side
_ne = _se = False
if _dxmin > _xmid: # dim on right side
_nw = _sw = False
if _dymax < _ymid: # dim below
_nw = _ne = False
if _dymin > _ymid: # dim above
_sw = _se = False
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:
_node.delObject(obj) # dim may not be in node ...
super(DimQuadtree, self).delObject(obj)
obj.disconnect(self)
self.addObject(obj)
def getClosest(self, x, y, tol=tolerance.TOL):
_x = util.get_float(x)
_y = util.get_float(y)
_t = tolerance.toltest(tol)
_dim = _tsep = None
_bailout = False
_ddict = {}
_nodes = [self.getTreeRoot()]
while len(_nodes):
_node = _nodes.pop()
_xmin, _ymin, _xmax, _ymax = _node.getBoundary()
if ((_x < (_xmin - _t)) or
(_x > (_xmax + _t)) or
(_y < (_ymin - _t)) or
(_y > (_ymax + _t))):
continue
if _node.hasSubnodes():
_nodes.extend(_node.getSubnodes())
else:
for _d in _node.getObjects():
_did = id(_d)
if _did not in _ddict:
_pt = _d.mapCoords(_x, _y, _t)
if _pt is not None:
_px = _py = _pt
_sep = math.hypot((_px - _x), (_py - _y))
if _tsep is None:
_tsep = _sep
_dim = _d
else:
if _sep < _tsep:
_tsep = _sep
_dim = _d
if _dim is not None and _sep < 1e-10:
_bailout = True
break
if _bailout:
break
return _dim
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"
_dims = []
if not len(self):
return _dims
_nodes = [self.getTreeRoot()]
_ddict = {}
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 _dim in _node.getObjects():
_dimid = id(_dim)
if _dimid not in _ddict:
if _dim.inRegion(_xmin, _ymin, _xmax, _ymax):
_dims.append(_dim)
_ddict[_dimid] = True
return _dims
#
# LinearDimension Quadtree
#
class LDimQuadtree(DimQuadtree):
def __init__(self):
super(LDimQuadtree, self).__init__()
def addObject(self, obj):
if not isinstance(obj, dimension.LinearDimension):
raise TypeError, "Invalid LinearDimension object: " + `obj`
super(LDimQuadtree, self).addObject(obj)
#
# HorizontalDimension Quadtree
#
class HDimQuadtree(DimQuadtree):
def __init__(self):
super(HDimQuadtree, self).__init__()
def addObject(self, obj):
if not isinstance(obj, dimension.HorizontalDimension):
raise TypeError, "Invalid HorizontalDimension object: " + `obj`
super(HDimQuadtree, self).addObject(obj)
#
# VerticalDimension Quadtree
#
class VDimQuadtree(DimQuadtree):
def __init__(self):
super(VDimQuadtree, self).__init__()
def addObject(self, obj):
if not isinstance(obj, dimension.VerticalDimension):
raise TypeError, "Invalid VerticalDimension object: " + `obj`
super(VDimQuadtree, self).addObject(obj)
#
# RadialDimension Quadtree
#
class RDimQuadtree(DimQuadtree):
def __init__(self):
super(RDimQuadtree, self).__init__()
def addObject(self, obj):
if not isinstance(obj, dimension.RadialDimension):
raise TypeError, "Invalid RadalDimension object: " + `obj`
super(RDimQuadtree, self).addObject(obj)
def find(self, *args):
_alen = len(args)
if _alen < 2:
raise ValueError, "Invalid argument count: %d" % _alen
_l1 = args[0]
from PythonCAD.Generic.layer import Layer
if not isinstance(_l1, Layer):
raise TypeError, "Invalid layer: " + `_l1`
_c1 = args[1]
if not isinstance(_c1, (circle.Circle, arc.Arc)):
raise TypeError, "Invalid circle/arc: " + `_c1`
if not len(self):
return None
for _rdim in self.getObjects():
_rl = _rdim.getDimLayer()
_rc = _rdim.getDimCircle()
if _rl is _l1 and _rc is _c1:
return _rdim
return None
#
# AngularDimension Quadtree
#
class ADimQuadtree(DimQuadtree):
def __init__(self):
super(ADimQuadtree, self).__init__()
def addObject(self, obj):
if not isinstance(obj, dimension.AngularDimension):
raise TypeError, "Invalid AngularDimension object: " + `obj`
super(ADimQuadtree, self).addObject(obj)
def find(self, *args):
_alen = len(args)
if _alen < 6:
raise ValueError, "Invalid argument count: %d" % _alen
_vl = args[0]
from PythonCAD.Generic.layer import Layer
if not isinstance(_vl, Layer):
raise TypeError, "Invalid layer: " + `_vl`
_vp = args[1]
if not isinstance(_vp, point.Point):
raise TypeError, "Invalid point: " + `_vp`
_l1 = args[2]
if not isinstance(_l1, Layer):
raise TypeError, "Invalid layer: " + `_l1`
_p1 = args[3]
if not isinstance(_p1, point.Point):
raise TypeError, "Invalid point: " + `_p1`
_l2 = args[4]
if not isinstance(_l2, Layer):
raise TypeError, "Invalid layer: " + `_l2`
_p2 = args[5]
if not isinstance(_p2, point.Point):
raise TypeError, "Invalid point: " + `_p2`
if not len(self):
return None
for _adim in self.getObjects():
_avl, _al1, _al2 = _adim.getDimLayers()
_avp, _ap1, _ap2 = _adim.getDimPoints()
if ((_vl is _avl) and
(_vp is _avp) and
(_l1 is _al1) and
(_p1 is _ap1) and
(_l2 is _al2) and
(_p2 is _ap2)):
return _adim
if ((_vl is _avl) and
(_vp is _avp) and
(_l1 is _al2) and
(_p1 is _ap2) and
(_l2 is _al1) and
(_p2 is _ap1)):
return _adim
return None
|