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
|
#
# Copyright (c) 2002, 2003, 2004 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
#
#
# miscellaneous utility functions
#
from math import fmod, pi
import types
from PythonCAD.Generic import tolerance
def get_float(val):
_v = val
if not isinstance(_v, float):
if not isinstance(_v, (int, long)):
raise TypeError, "Invalid non-numeric type: " + `type(_v)`
_v = float(val)
return _v
def test_boolean(val):
if hasattr(types, 'BooleanType'):
if not isinstance(val, types.BooleanType):
raise TypeError, "Invalid non-boolean type: " + `type(val)`
else:
if val is not True and val is not False:
raise TypeError, "Invalid non-boolean type: " + `type(val)`
def tuple_to_two_floats(t):
if not isinstance(t, tuple):
raise TypeError, "Argument must be a tuple: " + `type(t)`
if len(t) != 2:
raise ValueError, "Tuple must hold exactly two objects: " + str(t)
_obj1, _obj2 = t
_x = get_float(_obj1)
_y = get_float(_obj2)
return _x, _y
def tuple_to_three_floats(t):
if not isinstance(t, tuple):
raise TypeError, "Argument must be a tuple: " + `type(t)`
if len(t) != 3:
raise ValueError, "Tuple must hold exactly three objects: " + str(t)
_obj1, _obj2, _obj3 = t
_x = get_float(_obj1)
_y = get_float(_obj2)
_z = get_float(_obj3)
return _x, _y, _z
def make_angle(angle):
"""Return an angle value such that -90 <= angle <= 90.
make_angle(angle)
The argument angle should be a float. Additionally the argument
is expected to be in degrees, not radians.
"""
_angle = get_float(angle)
if _angle < -90.0 or _angle > 90.0:
_fa = fmod(_angle, 360.0)
if abs(_fa) < 1e-10:
_angle = 0.0
elif _fa > 0.0:
if _fa > 270.0:
_angle = _fa - 360.0
elif _fa > 90.0:
_angle = _fa - 180.0
else:
_angle = _fa
else:
if _fa < -270.0:
_angle = _fa + 360.0
elif _fa < -90.0:
_angle = _fa + 180.0
else:
_angle = _fa
return _angle
def make_c_angle(angle):
"""Return an angle value such that 0 <= angle <= 360.
make_c_angle(angle)
The argument angle should be a float.
"""
_a = get_float(angle)
if _a < 0.0:
_a = fmod(_a, 360.0) + 360.0
elif _a > 360.0:
_a = fmod(_a, 360.0)
return _a
def make_coords(x, y):
"""Check and convert x/y values to float values.
make_coords(x, y)
This routine is used to ensure the values are float values.
"""
_x = get_float(x)
_y = get_float(y)
return _x, _y
def make_region(xmin, ymin, xmax, ymax):
"""Return a validated region defined by (xmin, ymin) to (xmax, ymax).
make_region(xmin, ymin, xmax, ymax)
This routine is used to ensure the values are floats and
that xmin < xmax and ymin < ymax.
"""
_xmin = get_float(xmin)
_ymin = get_float(ymin)
_xmax = get_float(xmax)
if _xmax < _xmin:
raise ValueError, "Invalid values: xmax < xmin"
_ymax = get_float(ymax)
if _ymax < _ymin:
raise ValueError, "Invalid values: ymax < ymin"
return _xmin, _ymin, _xmax, _ymax
def degrees(value):
"""Convert a value from radians to degrees.
degrees(value)
In Python 2.3 this is available as the math.degrees() function, but
the value isn't scaled from -360.0 <= angle <= 360.0
"""
_value = get_float(value)
return fmod(_value, 360.0)
def radians(value):
"""Convert a value from degrees to radians.
radians(value)
In Python 2.3 this is available ad the math.radians() function, but
the value isn't scaled from -2*pi <= angle <= 2*pi
"""
_value = get_float(value)
return fmod(_value, (2.0 * pi))
#
# map x/y coordinates to a (x1, y1)->(x2, y2) segment
#
def map_coords(x, y, x1, y1, x2, y2, tol=tolerance.TOL):
"""
map_coords(x, y, x1, y1, x2, y2[, tol])
"""
_x = get_float(x)
_y = get_float(y)
_x1 = get_float(x1)
_y1 = get_float(y1)
_x2 = get_float(x2)
_y2 = get_float(y2)
_t = tolerance.toltest(tol)
if ((_x < min(_x1, _x2) - _t) or
(_y < min(_y1, _y2) - _t) or
(_x > max(_x1, _x2) + _t) or
(_y > max(_y1, _y2) + _t)):
return None
_sqlen = pow((_x2 - _x1), 2) + pow((_y2 - _y1), 2)
if _sqlen < 1e-10: # coincident points
return None
_r = ((_x - _x1)*(_x2 - _x1) + (_y - _y1)*(_y2 - _y1))/_sqlen
if _r < 0.0:
_r = 0.0
if _r > 1.0:
_r = 1.0
_px = _x1 + _r * (_x2 - _x1)
_py = _y1 + _r * (_y2 - _y1)
if abs(_px - _x) < _t and abs(_py - _y) < _t:
return _px, _py
return None
#
# test if line segments are visible within a rectangular region
#
def in_region(x1, y1, x2, y2, xmin, ymin, xmax, ymax):
"""Test if a segment from (x1, y1)->(x2, y2) is in region.
in_region(x1, y1, x2, y2, xmin, ymin, xmax, ymax)
"""
_x1 = get_float(x1)
_y1 = get_float(y1)
_x2 = get_float(x2)
_y2 = get_float(y2)
_xmin = get_float(xmin)
_ymin = get_float(ymin)
_xmax = get_float(xmax)
if _xmax < _xmin:
raise ValueError, "Illegal values: xmax < xmin"
_ymax = get_float(ymax)
if _ymax < _ymin:
raise ValueError, "Illegal values: ymax < ymin"
if not ((_x1 < _xmin) or
(_x1 > _xmax) or
(_y1 < _ymin) or
(_y1 > _ymax)):
return True
if not ((_x2 < _xmin) or
(_x2 > _xmax) or
(_y2 < _ymin) or
(_y2 > _ymax)):
return True
#
# simple horizontal/vertical testing
#
if abs(_y2 - _y1) < 1e-10: # horizontal
if not ((_y1 < _ymin) or (_y1 > _ymax)):
if min(_x1, _x2) < _xmin and max(_x1, _x2) > _xmax:
return True
if abs(_x2 - _x1) < 1e-10: # vertical
if not ((_x1 < _xmin) or (_x1 > _xmax)):
if min(_y1, _y2) < _ymin and max(_y1, _y2) > _ymax:
return True
#
# see if segment intersects an imaginary segment
# from (xmin, ymax) to (xmax, ymin)
#
# p1 = (xmin, ymax)
# p2 = (xmax, ymin)
# p3 = (x1, y1)
# p4 = (x2, y2)
#
_d = ((_xmax - _xmin)*(_y2 - _y1)) - ((_ymin - _ymax)*(_x2 - _x1))
if abs(_d) > 1e-10:
_n = ((_ymax - _y1)*(_x2 - _x1)) - ((_xmin - _x1)*(_y2 - _y1))
_r = _n/_d
if 0.0 < _r < 1.0:
return True
#
# see if segment intersects an imaginary segment
# from (xmin, ymin) to (xmax, ymax)
#
# p1 = (xmin, ymin)
# p2 = (xmax, ymax)
# p3 = (x1, y1)
# p4 = (x2, y2)
#
_d = ((_xmax - _xmin)*(_y2 - _y1)) - ((_ymax - _ymin)*(_x2 - _x1))
if abs(_d) > 1e-10:
_n = ((_ymin - _y1)*(_x2 - _x1)) - ((_xmin - _x1)*(_y2 - _y1))
_r = _n/_d
if 0.0 < _r < 1.0:
return True
return False
|