def giterate(self, fn): ''' Iterate over the generated vectors This method is simple, but the generated vector order is really inefficient. A simplifier should improve the result :type fn: func(srcx, srcy, dstx, dsty) :param fn: function to call on each iteration ''' # # for each 2 adjacent vectors # for i in range(len(self._points)): s = self._points[i] d = self._points[(i + 1) % len(self._points)] self._l.debug('main vectors: %s' % Vector(s, d)) sxst = s.x / self._npoints # source x step syst = s.y / self._npoints # source y step dxst = d.x / self._npoints # dest x step dyst = d.y / self._npoints # dest y step # # generate each internal vector # for p in range(1, self._npoints): sp = p # current point in source dp = self._npoints - p # current points in dest sx = sxst * sp sy = syst * sp dx = dxst * dp dy = dyst * dp s = Point(sx, sy) d = Point(dx, dy) fn(Vector(s, d))
def giterate(self, fn): ''' Iterate over the generated vectors :type fn: func(srcx, srcy, dstx, dsty) :param fn: function to call on each iteration ''' # all sizes in mm cut_length = 15 skip_length = 4 unit_length = cut_length + skip_length odd_shift = unit_length / 2 minimal_width = unit_length * 2 if self.width < minimal_width: raise Exception('width must be bigger than %d' % (minimal_width)) odd_line = False y = 0 while y < self.height: # go over the rows x = odd_shift if odd_line else 0 while x < self.width: vector_start = Point(x, y) endx = x + cut_length if endx > self.width: endx = self.width vector_end = Point(endx, y) vector = Vector(vector_start, vector_end) fn(vector) x = x + unit_length y += self.spacing odd_line = not odd_line
def generate_resistor_box(self, fn): '''this is the box in the middle, when the resistor body rests''' w = self.resistor_length h = self.dim.h - ((self.margin_top + self.margin_bottom) / 2) x = (self.dim.w - self.resistor_length) / 2 y = self.margin_top / 2 for v in Path.do_square(Point(x, y), Point(w, h)): fn(v)
def test_level_tbone_near_miss_sb(): _test_collision(direction_1=directions.FORWARD, pos_1=Point(0, 10, 0), throttle_1=0.0, direction_2=directions.STARBOARD, pos_2=Point(0, -100, 0), throttle_2=1.0, expected_collision=False)
def test_helix(self): cyl1 = SurfaceCylinder('cyl1', 1., 2.) cyl2 = SurfaceCylinder('cyl2', 2., 1.) field = 3.8 particle = Particle(LorentzVector(2., 0, 1, 5), Point(0., 0., 0.), -1) debug_info = helix.propagate_one(particle, cyl1, field) particle = Particle(LorentzVector(0., 2, 1, 5), Point(0., 0., 0.), -1) debug_info = helix.propagate_one(particle, cyl1, field)
def test_rear_near_miss_port(): _test_collision(direction_1=directions.FORWARD, pos_1=Point(0, -10, 0), throttle_1=1.0, direction_2=directions.FORWARD, pos_2=Point(100, 0, 0), throttle_2=0.5, expected_collision=False)
def test_head_on_near_miss_deck(): _test_collision(direction_1=directions.FORWARD, pos_1=Point(-100, 0, -10), throttle_1=1.0, direction_2=directions.AFT, pos_2=Point(100, 0, 0), throttle_2=1.0, expected_collision=False)
def test_top_down_tbone_near_miss_aft(): _test_collision(direction_1=directions.FORWARD, pos_1=Point(-10, 0, 0), throttle_1=0.0, direction_2=directions.DECK, pos_2=Point(0, 0, 100), throttle_2=1.0, expected_collision=False)
def test_angular_collision(): _test_collision( direction_1=directions.FORWARD, pos_1=Point(-100, 0, 0), throttle_1=1.0, direction_2=directions.AFT, pos_2=Point(100, 0, 0), throttle_2=1.0, )
def test_rear_collision(): _test_collision( direction_1=directions.FORWARD, pos_1=Point(0, 0, 0), throttle_1=1.0, direction_2=directions.FORWARD, pos_2=Point(100, 0, 0), throttle_2=0.5, )
def test_top_down_tbone_collision(): _test_collision( direction_1=directions.FORWARD, pos_1=Point(0, 0, 0), throttle_1=0.0, direction_2=directions.DECK, pos_2=Point(0, 0, 100), throttle_2=1.0, )
def test_level_tbone_collision(): _test_collision( direction_1=directions.FORWARD, pos_1=Point(0, 0, 0), throttle_1=0.0, direction_2=directions.STARBOARD, pos_2=Point(0, -100, 0), throttle_2=1.0, )
def distancePointSegment(a,Vbc,b,c): #returns min_distance between bc and a and the point of intersection of perpendicular from a on bc Vab=Vector.from_points(b, a) Vac=Vector.from_points(c, a) zero_vector=Point(0,0,0) min_distance=100 final_point=[] type=[] if Vbc.magnitude()<.000001: return [9999999,[a.x,a.y]] if abs(Vab.dot(Vbc)/Vbc.magnitude())<= Vbc.magnitude() and abs(Vac.dot(Vbc)/Vbc.magnitude())<= Vbc.magnitude(): min_distance=(Vab.cross(Vbc)).magnitude()/Vbc.magnitude() perpendicular_point_vector=Vbc.multiply(abs(Vab.dot(Vbc))/Vbc.magnitude()/Vbc.magnitude()) final_point=perpendicular_point_vector.sum(Vector.from_points(zero_vector,b)) type="middle" else : min_distance=min([Vab.magnitude(),Vac.magnitude()]) if min_distance==Vab.magnitude(): final_point=Vector.from_points(zero_vector, b) type="vertex" else: final_point=Vector.from_points(zero_vector, c) type="vertex" return [min_distance,[final_point.x,final_point.y]]
def __init__(self, planckType, planckRadius, point=Point(0, 0, 0), vector=Vector(0, 0, 0), chargeMagnitude=Fraction(1, 6)): # process args if planckType == 'electrino': self.planckType = planckType self.charge = chargeMagnitude * -1 elif planckType == 'positrino': self.planckType = planckType self.charge = chargeMagnitude else: raise TypeError('planckType must be electrino OR positrino') if isinstance(point, Point): self.point = point if isinstance(vector, Vector): self.vector = vector self.electricFieldTime = 0 self.electricFieldRadius = 0 self.magneticFieldTime = 0 self.magneticFieldRadius = 0
def main(): opts = docopt.docopt(__doc__) x = int(opts['<x>']) y = int(opts['<y>']) w = int(opts['<w>']) h = int(opts['<h>']) spacing = float(opts['<spacing>']) cut_feed = 600 if not opts['--cut_feed'] else int(opts['--cut_feed']) passing_feed = 800 if not opts['--passing_feed'] else int( opts['--passing_feed']) outfilename = opts['<outfile>'] if not outfilename: outfilename = 'os_%f_%d_%d_%d_%d.ngc' % (spacing, h, w, x, y) logging.getLogger('MainApp').info('Gcode file name: %s' % (outfilename)) p = HingePattern(height=h, width=w, spacing=spacing) s = PathSimplifier() comment = '''Generated by hinge.py with parameters: spacing: %f height: %d width: %d''' % (spacing, h, w) g = GCodeGenerator(open(outfilename, 'w'), comment, cut_feed, passing_feed) p.giterate(s.add_vector) vectors = s.simplify() # # adjust all vectors to be negative only # vectors = add_to_vectors(vectors, -Point(x + w, y + h)) g.generate(vectors)
def getFaceNormal(self, face): points = [] for v in face: points.append(Point(v[0], v[1], v[2])) vecA = Vector.from_points(points[0], points[1]) vecB = Vector.from_points(points[0], points[2]) vecResult = Vector.cross(vecA, vecB) return vecResult
def findDistanceBetweenSegments(ab,a,af,bb,b,bf):# returns geo distance between two adjacent line segments for two polygons in m z_point=Point(0,0,0) Paf=Point(af[0],af[1],0) Pa=Point(a[0],a[1],0) Pab=Point(ab[0],ab[1],0) Pbf=Point(bf[0],bf[1],0) Pb=Point(b[0],b[1],0) Pbb=Point(bb[0],bb[1],0) compare=[] dist=[] temp_result=findShortestDistanceSegments(Pab, Pa,Pbb, Pb) #[[min_distance,[final_point.x,final_point.y]],a2] compare.append([temp_result[0][1],temp_result[1]]) dist.append(temp_result[0][0]) temp_result=findShortestDistanceSegments(Pab, Pa,Pb, Pbf) compare.append([temp_result[0][1],temp_result[1]]) dist.append(temp_result[0][0]) temp_result=findShortestDistanceSegments(Pa, Paf,Pb, Pbb) compare.append([temp_result[0][1],temp_result[1]]) dist.append(temp_result[0][0]) temp_result=findShortestDistanceSegments(Pa, Paf,Pb, Pbf) compare.append([temp_result[0][1],temp_result[1]]) dist.append(temp_result[0][0]) min_index=dist.index(min(dist)) final_result=compare[min_index] return haversine(cartesianToGeo(final_result[0][0],final_result[0][1]),cartesianToGeo(final_result[1].x,final_result[1].y))*1000
def test_forward_acceleration_45_pitch(): _test_acceleration(direction=directions.FORWARD, expected_position=Point(7.0711, 0.00, -7.0711), orientation={ directions.YAW: 0, directions.ROLL: 0, directions.PITCH: 45 }, keep_mass=False)
def test_can_sense_at_angle_pitch(): _assert_can_detect( target_pos=Point(0, 10, 10), scan_direction=STARBOARD, sensor_orientation={ PITCH: 15, YAW: 45 } )
def test_can_sense_at_angle_yaw(): _assert_can_detect( target_pos=Point(10, 10, 0), scan_direction=STARBOARD, sensor_orientation={ PITCH: 45, YAW: 67.5 }, )
def test_forward_acceleration_90_pitch(): _test_acceleration(direction=directions.FORWARD, expected_position=Point(0.00, 0.00, -10.00), orientation={ directions.YAW: 0, directions.ROLL: 0, directions.PITCH: 90 }, keep_mass=False)
def __init__(self, *args, **kwargs): self.position = kwargs.pop('position', Point(0, 0, 0)) self.current_vector = kwargs.pop('current_vector', Vector(0, 0, 0)) self.mass = kwargs.pop('mass', 0) self.yaw_speed = kwargs.pop('yaw_speed', 0) self.roll_speed = kwargs.pop('roll_speed', 0) self.pitch_speed = kwargs.pop('pitch_speed', 0) self.integrity = 1.0 self._acceleration_vectors = []
def test_straightline(self): origin = Point(0,0,0) cyl1 = SurfaceCylinder('cyl1', 1, 2) cyl2 = SurfaceCylinder('cyl2', 2, 1) particle = Particle( LorentzVector(1, 0, 1, 2.), origin, 0) straight_line.propagate_one( particle, cyl1 ) straight_line.propagate_one( particle, cyl2 ) self.assertEqual( len(particle.points), 3) # test extrapolation to barrel self.assertAlmostEqual( particle.points['cyl1'].Perp(), 1. ) self.assertAlmostEqual( particle.points['cyl1'].Z(), 1. ) # test extrapolation to endcap self.assertAlmostEqual( particle.points['cyl2'].Z(), 1. ) # testing extrapolation to -z particle = Particle( LorentzVector(1, 0, -1, 2.), origin, 0) # import pdb; pdb.set_trace() straight_line.propagate_one( particle, cyl1 ) straight_line.propagate_one( particle, cyl2 ) self.assertEqual( len(particle.points), 3) self.assertAlmostEqual( particle.points['cyl1'].Perp(), 1. ) # test extrapolation to endcap self.assertAlmostEqual( particle.points['cyl1'].Z(), -1. ) self.assertAlmostEqual( particle.points['cyl2'].Z(), -1. ) # extrapolating from a vertex close to +endcap particle = Particle( LorentzVector(1, 0, 1, 2.), Point(0,0,1.5), 0) straight_line.propagate_one( particle, cyl1 ) self.assertAlmostEqual( particle.points['cyl1'].Perp(), 0.5 ) # extrapolating from a vertex close to -endcap particle = Particle( LorentzVector(1, 0, -1, 2.), Point(0,0,-1.5), 0) straight_line.propagate_one( particle, cyl1 ) self.assertAlmostEqual( particle.points['cyl1'].Perp(), 0.5 ) # extrapolating from a non-zero radius particle = Particle( LorentzVector(0, 0.5, 1, 2.), Point(0,0.5,0), 0) straight_line.propagate_one( particle, cyl1 ) self.assertAlmostEqual( particle.points['cyl1'].Perp(), 1. ) self.assertAlmostEqual( particle.points['cyl1'].Z(), 1. )
def __init_old__(self): self.resistor_length = 6 self.bend_length_min = 4 self.bend_length_max = 10 self.bend_length_step = 1 self.bend_count = (self.bend_length_max - self.bend_length_min + 1) / self.bend_length_step self.bend_cut_height = 0.4 self.spacing_between_lines = 4 self.spacing_middle_box_top = 5 self.spacing_middle_box_bottom = 5 self.spacing_left = 2 self.spacing_right = 2 self.spacing_side = 5 middle_box_height = (self.bend_count + 1) * self.spacing_between_lines self.middle_box_dim = Point(self.resistor_length, middle_box_height) self.middle_box_loc = Point(self.spacing_left + self.bend_length_max, self.spacing_middle_box_top) self.dim = self.middle_box_loc + self.middle_box_dim + Point( self.bend_length_max + self.spacing_right, self.spacing_middle_box_bottom)
def get_init_data(): # create g1 as example p1 = Patch(1100, 1400, Point(2200, 2900, 0), 'E') p2 = Patch(2100, 1400, Point(3800, 3200, 0), 'BA') p3 = Patch(1100, 1600, Point(2200, 4400, 0), 'D') p4 = Patch(1100, 1600, Point(1100, 1400, 0), 'K') p5 = Patch(2100, 3100, Point(3800, 5400, 0), 'BR') p6 = Patch(2100, 1600, Point(1500, 6000, 0), 'L') g1 = PlanGraph() g1.addPatch(p1) g1.addPatch(p2) g1.addPatch(p3) g1.addPatch(p4) g1.addPatch(p5) g1.addPatch(p6) g1.addConn((p1,p2)) g1.addConn((p1,p3)) g1.addConn((p1,p4)) g1.addConn((p3,p4)) g1.addConn((p3,p5)) g1.addConn((p3,p6)) g1.addConn((p4,p6)) return g1
def generate_bending_cuts(self, fn): '''Generate the cuts for the resistor bending''' prev_left = self.left_vector.point_for_y(0) prev_right = self.right_vector.point_for_y(0) for i in range(self.bend_count): y = self.get_bend_y(i) y1 = y - self.bend_cut_height / 2 y2 = y + self.bend_cut_height / 2 # left part xend = self.left_vector.x_for_y(y) + self.margin_sides p1 = self.left_vector.point_for_y(y1) p2 = Point(xend, y1) p3 = Point(xend, y2) p4 = self.left_vector.point_for_y(y2) fn(Vector(p1, p2)) fn(Vector(p2, p3)) fn(Vector(p3, p4)) # add diagonal part the comes above it fn(Vector(prev_left, p1)) prev_left = p4 # right part xstart = self.right_vector.x_for_y(y) - self.margin_sides p1 = self.right_vector.point_for_y(y1) p2 = Point(xstart, y1) p3 = Point(xstart, y2) p4 = self.right_vector.point_for_y(y2) fn(Vector(p1, p2)) fn(Vector(p2, p3)) fn(Vector(p3, p4)) # add diagonal part the comes above it fn(Vector(prev_right, p1)) prev_right = p4 fn(Vector(prev_left, self.left_vector.point_for_y(self.dim.h))) fn(Vector(prev_right, self.right_vector.point_for_y(self.dim.h)))
def test_port_acceleration_45_roll(): _test_acceleration(direction=directions.PORT, expected_position=Point( 0.00, -7.0711, -7.0711, ), orientation={ directions.YAW: 0, directions.ROLL: 45, directions.PITCH: 0 }, keep_mass=False)
def test_overhead_acceleration_yaw_no_change(): _test_acceleration(direction=directions.OVERHEAD, expected_position=Point( 0.00, 0.00, 10.00, ), orientation={ directions.YAW: 90, directions.ROLL: 0, directions.PITCH: 0 }, keep_mass=False)
def generate_bending_tunnels(self, fn): for i in range(self.bend_count): y = self.get_bend_y(i) length = self.get_bend_length(i) # left tunnel xstart = self.left_vector.x_for_y(y) + self.margin_sides xend = xstart + length fn( Vector(Point(xstart, y), Point(xend, y), { 'pre_command': 'S400', 'post_command': 'S1000' })) # right tunnel xend = self.right_vector.x_for_y(y) - self.margin_sides xstart = xend - length fn( Vector(Point(xstart, y), Point(xend, y), { 'pre_command': 'S400', 'post_command': 'S1000' }))
def test_overhead_acceleration_pitch_no_change(): _test_acceleration(direction=directions.STARBOARD, expected_position=Point( 0.00, 10.00, 0.00, ), orientation={ directions.YAW: 0, directions.ROLL: 0, directions.PITCH: 90 }, keep_mass=False)