def shoot(self): if (self.direction == bs.Direction.RIGHT): bullet_position = Point(self.rect.x + 18, self.rect.y) bullet_velocity = Point(15, 0) self.world.shoot(bullet_position, bullet_velocity) else: bullet_position = Point(self.rect.x - 18, self.rect.y) bullet_velocity = Point(-15, 0) self.world.shoot(bullet_position, bullet_velocity)
def visualization_basic(self, vp, data={}): Viewport = vp vp.add_object(Viewport.Line(self.pivot, self._weight_point, 0.01, Viewport.BLACK)) topleft = Point.from_point(self.pivot).translate(Point(-0.1,0.1)) bottomright = Point.from_point(self.pivot).translate(Point(0.1,0)) vp.add_object(Viewport.Rectangle(topleft, bottomright, Viewport.BLACK)) vp.add_object(Viewport.Circle(self._weight_point, 0.05, Viewport.BLACK)) vp.add_object(Viewport.Text((self._weight_point,(-0.5,-0.1)), self._weight_coords_text,(0,0,170)))
def shoot(self): # Values of the adjustment of the CELL_SIZE are chosen, so that # shooting looks plausible - that is the only determinant. if (self.direction is bs.Direction.RIGHT): bullet_position = Point(self.rect.x + 0.8 * CELL_SIZE, self.rect.y + 0.25 * CELL_SIZE) bullet_velocity = Point(0.375 * CELL_SIZE, 0) else: bullet_position = Point(self.rect.x, self.rect.y + 0.25 * CELL_SIZE) bullet_velocity = Point(-0.375 * CELL_SIZE, 0) self.level.shoot(bullet_position, bullet_velocity)
def add_polygon(X, lcar): # Create points. p = [Point(x, lcar) for x in X] # Create lines e = [Line(p[k], p[k + 1]) for k in range(len(p) - 1)] e.append(Line(p[-1], p[0])) ll = LineLoop(e) s = PlaneSurface(ll) return s
def add_rectangle(xmin, xmax, ymin, ymax, z, lcar): X = [[xmin, ymin, z], [xmax, ymin, z], [xmax, ymax, z], [xmin, ymax, z]] # Create points. p = [Point(x, lcar) for x in X] # Create lines e = [Line(p[k], p[k + 1]) for k in range(len(p) - 1)] e.append(Line(p[-1], p[0])) ll = LineLoop(e) s = PlaneSurface(ll) return s
def add_ball(x0, radius, lcar, with_volume=True, holes=None, label=None): '''Creates a ball with a given radius around a given midpoint :math:`x_0`. ''' if holes is None: holes = [] # Add points. p = [ Point(x0, lcar=lcar), Point([x0[0] + radius, x0[1], x0[2]], lcar=lcar), Point([x0[0], x0[1] + radius, x0[2]], lcar=lcar), Point([x0[0], x0[1], x0[2] + radius], lcar=lcar), Point([x0[0] - radius, x0[1], x0[2]], lcar=lcar), Point([x0[0], x0[1] - radius, x0[2]], lcar=lcar), Point([x0[0], x0[1], x0[2] - radius], lcar=lcar) ] # Add ball skeleton. c = [ Circle([p[1], p[0], p[6]]), Circle([p[6], p[0], p[4]]), Circle([p[4], p[0], p[3]]), Circle([p[3], p[0], p[1]]), Circle([p[1], p[0], p[2]]), Circle([p[2], p[0], p[4]]), Circle([p[4], p[0], p[5]]), Circle([p[5], p[0], p[1]]), Circle([p[6], p[0], p[2]]), Circle([p[2], p[0], p[3]]), Circle([p[3], p[0], p[5]]), Circle([p[5], p[0], p[6]]) ] # Add surfaces (1/8th of the ball surface). ll = [ LineLoop([c[4], c[9], c[3]]), LineLoop([c[8], '-' + c[4], c[0]]), LineLoop([c[11], '-' + c[7], '-' + c[0]]), LineLoop([c[7], '-' + c[3], c[10]]), LineLoop(['-' + c[9], c[5], c[2]]), LineLoop(['-' + c[10], '-' + c[2], c[6]]), LineLoop(['-' + c[1], '-' + c[6], '-' + c[11]]), LineLoop(['-' + c[5], '-' + c[8], c[1]]) ] # Create a surface for each line loop. s = [RuledSurface(l) for l in ll] # Create the surface loop. surface_loop = SurfaceLoop(s) if holes: # Create an array of surface loops; the first entry is the outer # surface loop, the following ones are holes. surface_loop = Array([surface_loop] + holes) # Create volume. if with_volume: volume = Volume(surface_loop) if label: PhysicalVolume(volume, label) else: volume = None return volume, surface_loop, s
def __init__(self, dataframe): if dataframe is None: print "none dataframe" raise ValueError if len(dataframe) == 0: print "empty dataframe" raise ValueError self.ID = dataframe["un"][0] #initialize for i in range(len(dataframe)): po = Point(dataframe["zx"][i], dataframe["zy"][i], dataframe["tm"][i]) self.trace.insertPoint(po)
def ExecuteOverRecordMarker(self, traceDF): ind = traceDF[traceDF.IsOutlier == 0].index print len(ind) * 1.0 / len(traceDF) for i_ind, j_ind in zip(range(len(ind) - 1), range(1, len(ind))): last = ind[i_ind - 1] i = ind[i_ind] j = ind[j_ind] if j == len(ind) - 1: break next = ind[j_ind + 1] if traceDF["tm"][i] == traceDF["tm"][j]: po_last = Point(zx=traceDF["zx"][last], zy=traceDF["zy"][last]) po_next = Point(zx=traceDF["zx"][next], zy=traceDF["zy"][next]) po_i = Point(zx=traceDF["zx"][i], zy=traceDF["zy"][i]) po_j = Point(zx=traceDF["zx"][j], zy=traceDF["zy"][j]) dis1 = po_last.distanceToPoint(po_i) + po_next.distanceToPoint( po_i) dis2 = po_last.distanceToPoint(po_j) + po_next.distanceToPoint( po_j) if dis1 > dis2: traceDF["IsOutlier"][i] = 1 else: traceDF["IsOutlier"][j] = 1
def add_circle(radius, lcar, R=numpy.eye(3), x0=numpy.array([0.0, 0.0, 0.0]), compound=False, num_sections=3): '''Add circle in the :math:`y`-:math:`z`-plane. ''' # Define points that make the circle (midpoint and the four cardinal # directions). X = [[0.0, 0.0, 0.0]] if num_sections == 4: # For accuracy, the points are provided explicitly. X = [[0.0, 0.0, 0.0], [0.0, radius, 0.0], [0.0, 0.0, radius], [0.0, -radius, 0.0], [0.0, 0.0, -radius]] else: for k in range(num_sections): alpha = 2 * numpy.pi * k / num_sections X.append( [0.0, radius * numpy.cos(alpha), radius * numpy.sin(alpha)]) # Apply the transformation. # TODO assert that the transformation preserves circles X = [numpy.dot(R, x) + x0 for x in X] # Add Gmsh Points. Comment('Points') p = [Point(x, lcar) for x in X] # Define the circle arcs. Comment('Circle arcs') c = [] for k in range(1, len(p) - 1): c.append(Circle([p[k], p[0], p[k + 1]])) # Don't forget the closing arc. c.append(Circle([p[-1], p[0], p[1]])) if compound: c = [CompoundLine(c)] return c
def CreateLineFromPoint(filepath,dataframe): try: if os.path.exists(filepath): shapefileOperation.RemoveShapeFiles(filepath) w = shapefile.Writer() w.autoBalance = 1 w = shapefile.Writer(shapeType=shapefile.POLYLINE) w.field("TripID", 'C', 10) w.field("Length",'F',decimal=8) curTripID=None curPointList=[] for i in range(len(dataframe)): thisTripID=dataframe["TripID"][i] if curTripID is None: curTripID=thisTripID if thisTripID==curTripID: curPointList.append([dataframe["zx"][i],dataframe["zy"][i]]) if thisTripID!=curTripID or i==len(dataframe)-1: if len(curPointList)>=2: w.line(parts=[curPointList]) length=0 points=[Point(zx=x[0],zy=x[1]) for x in curPointList] for p0,p1 in zip(range(len(points)-1),range(1,len(points))): dis=points[p0].distanceToPoint(points[p1]) length+=dis length = float("%.8f" %length) w.record(curTripID,length) else: print curTripID raise ValueError curTripID=thisTripID curPointList=[] curPointList.append([dataframe["zx"][i],dataframe["zy"][i]]) w.save(filepath) except Exception,e: print e.message
return False if __name__ == "__main__": from basic import Point, PointMotionType from LoadData import LoadTraceDataFromMysqlDBFactory from SegByMotion import MotionSegmentMediator loader = LoadTraceDataFromMysqlDBFactory( dbname='mydatabase', tblname="trajectory", whereClause="IsOutlier=0 order by un,tm") traceDF = loader.getTraceData() trace = [ Point(zx=traceDF["zx"][i], zy=traceDF["zy"][i], tm=traceDF["tm"][i], ID=traceDF["UID"][i]) for i in traceDF.index ] MoveSegMed = MotionSegmentMediator(5) moveSegs = MoveSegMed.segTrace(trace) #label trip id for moveseg in moveSegs: for po in moveseg.pointList: sql = "update mydatabase.trajectory set TripID=%d" % moveseg.ID loader.database.UpdateTable(sql) for moveseg in moveSegs: CharSegMed = CharacterSegmentMediator() segParts = CharSegMed.ExecuteSegMethod(moveseg.pointList) for part in segParts: filter = FilterFacotory.CreateConsistentFilter()
def add_box(x0, x1, y0, y1, z0, z1, lcar, with_volume=True, holes=None, label=None): if holes is None: holes = [] # Define corner points. p = [ Point([x1, y1, z1], lcar=lcar), Point([x1, y1, z0], lcar=lcar), Point([x1, y0, z1], lcar=lcar), Point([x1, y0, z0], lcar=lcar), Point([x0, y1, z1], lcar=lcar), Point([x0, y1, z0], lcar=lcar), Point([x0, y0, z1], lcar=lcar), Point([x0, y0, z0], lcar=lcar) ] # Define edges. e = [ Line(p[0], p[1]), Line(p[0], p[2]), Line(p[0], p[4]), Line(p[1], p[3]), Line(p[1], p[5]), Line(p[2], p[3]), Line(p[2], p[6]), Line(p[3], p[7]), Line(p[4], p[5]), Line(p[4], p[6]), Line(p[5], p[7]), Line(p[6], p[7]) ] # Define the six line loops. ll = [ LineLoop([e[0], e[3], '-' + e[5], '-' + e[1]]), LineLoop([e[0], e[4], '-' + e[8], '-' + e[2]]), LineLoop([e[1], e[6], '-' + e[9], '-' + e[2]]), LineLoop([e[3], e[7], '-' + e[10], '-' + e[4]]), LineLoop([e[5], e[7], '-' + e[11], '-' + e[6]]), LineLoop([e[8], e[10], '-' + e[11], '-' + e[9]]) ] # Create a surface for each line loop. s = [RuledSurface(l) for l in ll] # Create the surface loop. surface_loop = SurfaceLoop(s) if holes: # Create an array of surface loops; the first entry is the outer # surface loop, the following ones are holes. surface_loop = Array([surface_loop] + holes) if with_volume: # Create volume vol = Volume(surface_loop) if label: PhysicalVolume(vol, label) else: vol = None return vol, surface_loop
def _weight_coords_text(self): p = Point.from_point(self.pivot).translate(self.weight) return "(%0.3f, %0.3f)" % (p.x, p.y)
def _weight_point(self, translate=None): if translate is None: translate = [0, 0] return Point.from_point(self.pivot).translate(self.weight).translate(Point(translate[0], translate[1]))
def __repr__(self): return "Platform" + str(Point(self.rect.x, self.rect.y).tuple())
def add_pipe(outer_radius, inner_radius, length, R=numpy.eye(3), x0=numpy.array([0.0, 0.0, 0.0]), label=None, lcar=0.1): '''Hollow cylinder. Define a rectangle, extrude it by rotation. ''' Comment('Define rectangle.') X = numpy.array([[0.0, outer_radius, -0.5 * length], [0.0, outer_radius, 0.5 * length], [0.0, inner_radius, 0.5 * length], [0.0, inner_radius, -0.5 * length]]) # Apply transformation. X = [numpy.dot(R, x) + x0 for x in X] # Create points set. p = [Point(x, lcar) for x in X] # Define edges. e = [ Line(p[0], p[1]), Line(p[1], p[2]), Line(p[2], p[3]), Line(p[3], p[0]) ] rot_axis = [0.0, 0.0, 1.0] rot_axis = numpy.dot(R, rot_axis) point_on_rot_axis = [0.0, 0.0, 0.0] point_on_rot_axis = numpy.dot(R, point_on_rot_axis) + x0 # Extrude all edges three times by 2*Pi/3. previous = e angle = '2*Pi/3' all_names = [] #com = [] Comment('Extrude in 3 steps.') for i in range(3): Comment('Step %s' % (i + 1)) for k in range(len(previous)): # ts1[] = Extrude {{0,0,1}, {0,0,0}, 2*Pi/3}{Line{tc1};}; name = Extrude('Line{%s}' % previous[k], rotation_axis=rot_axis, point_on_axis=point_on_rot_axis, angle=angle) #if k==0: # com.append(name+'[1]') #else: # all_names.append(name+'[1]') all_names.append(name + '[1]') previous[k] = name + '[0]' # #cs = CompoundSurface(com) # Now just add surface loop and volume. all_surfaces = all_names #all_surfaces = all_names + [cs] surface_loop = SurfaceLoop(all_surfaces) vol = Volume(surface_loop) if label: PhysicalVolume(vol, label) return
from basic import Point, PointMotionType from LoadData import LoadTraceDataFromMysqlDBFactory from SegByMotion import MotionSegmentMediator import matplotlib.pyplot as plt loader = LoadTraceDataFromMysqlDBFactory(dbname='mydatabase', tblname="trajectory", whereClause="IsOutlier=0 order by un,tm") traceDF = loader.getTraceData() trace=[Point(zx=traceDF["zx"][i],zy=traceDF["zy"][i],tm=traceDF["tm"][i],ID=traceDF["UID"][i]) for i in traceDF.index] TimeIntervalLst=[] DistanceIntervalLst=[] for i,j in zip(range(len(trace)-1),range(1,len(trace))): TI=int(trace[j].tm-trace[i].tm) dis=Point(zx=traceDF["zx"][j],zy=traceDF["zy"][j]).distanceToPoint(Point(zx=traceDF["zx"][i],zy=traceDF["zy"][i])) DistanceIntervalLst.append(dis) sql="update trajectory set DistanceInterval=%s,TimeInterval=%s where UID=%d" %(dis,TI,traceDF["UID"][j]) loader.database.UpdateTable(sql) TimeIntervalLst.append(TI) TimeIntervalLst1=[x for x in TimeIntervalLst if x<10] DistanceIntervalLst1=[x for x in DistanceIntervalLst if x<100] print len(TimeIntervalLst1)*1.0/len(TimeIntervalLst) print len(DistanceIntervalLst1)*1.0/len(DistanceIntervalLst) figure,axes=plt.subplots(2,2,figsize=(10,10)) axes[0,0].boxplot(TimeIntervalLst1) axes[0,1].hist(TimeIntervalLst1) axes[1,0].boxplot(DistanceIntervalLst1) axes[1,1].hist(DistanceIntervalLst1) plt.show()
def plot_point(namespace, f, tag, x, y): namespace.set(tag, Point(x, y))
if __name__ == "__main__": from LoadData import LoadTraceDataFromMysqlDBFactory from basic import Point try: loader = LoadTraceDataFromMysqlDBFactory( dbname='mydatabase', tblname="trajectory", whereClause="IsOutlier=0 order by un,tm") traceDF = loader.getTraceData() seg = [] for i in range(len(traceDF)): po = Point(zx=traceDF["zx"][i], zy=traceDF["zy"][i], tm=traceDF["tm"][i], ID=traceDF["UID"][i], TimeInterval=traceDF["TimeInterval"][i], DistanceInterval=traceDF["DistanceInterval"][i]) seg.append(po) segCls = MotionSegmentMediator(5) segCls.labelStayPoints(seg) for po in seg: sql = "" if po.motionType == PointMotionType.Move: sql = "update trajectory set motion=1 where UID=%d" % po.ID elif po.motionType == PointMotionType.Stay: sql = "update trajectory set motion=0 where UID=%d" % po.ID loader.database.UpdateTable(sql) except Exception, e: print e.message
if j == len(tracePoints) - 1: tracePoints[j].nextPoint = tracePoints[j] tracePoints[i].direction2next = CacLineCoff(tracePoints[i].prevPoint.x, tracePoints[i].prevPoint.y, tracePoints[i].nextPoint.x, tracePoints[i].nextPoint.y) tracePoints[i].baseDirection = CacLineCoff(tracePoints[i].x, tracePoints[i].y, tracePoints[i].nextPoint.x, tracePoints[i] .nextPoint.y) #start Loop loop=1 newInd=index.Index() while loop<=20: #cac direction for each point for po in tracePoints: this_inclineAngle=po.direction2next[i] neighIDs=list(idx.intersection((po.x-self.searchDisRange,po.y-self.searchDisRange, po.x+self.searchDisRange,po.y+self.searchDisRange))) if len(neighIDs)!=0: self.cacPostMovePosition() if __name__=="__main__": try: loader = LoadTraceDataFromMysqlDBFactory(dbname='mydatabase', tblname="trajectory", whereClause="TripID>0 order by un,tm") traceDF = loader.getTraceData() tracePoints=[Point(zx=traceDF["zx"][i],zy=traceDF["zy"][i], TripID=traceDF["TripID"][i],ID=traceDF["UID"][i]) for i in traceDF.index] clarifyMethod=ClarifyMethod() clarifyMethod.executeClarify(tracePoints) except Exception,e: print e.message
ind += 1 return attrInfo if __name__ == "__main__": from basic import Point, PointMotionType from LoadData import LoadTraceDataFromMysqlDBFactory from math import sqrt loader = LoadTraceDataFromMysqlDBFactory( dbname='mydatabase', tblname="trajectory", whereClause="IsOutlier=0 order by un,tm") traceDF = loader.getTraceData() for i in range(len(traceDF)): # for i,j in zip(range(len(traceDF)-1),range(1,len(traceDF))): po = Point(zx=traceDF["zx"][i], zy=traceDF["zy"][i], ID=traceDF["UID"][i], tm=traceDF["tm"]) # p2=Point(zx=traceDF["zx"][j], zy=traceDF["zy"][j], ID=traceDF["UID"][j], tm=traceDF["tm"]) # dis= po.distanceToPoint(p2) # dist=sqrt((po.x-p2.x)*(po.x-p2.x)+(po.y-p2.y)*(po.y-p2.y)) # if dis-dist<5: # print "Y" # else: # raw_input("N") sql = "update trajectory set x=%f,y=%f where UID=%d" % (po.x, po.y, po.ID) loader.database.UpdateTable(sql)