Example #1
0
	def getAngleFromLongestEdge(self, areaPoly=None, origin=None):
		"""
		finds the angle of the longest edge in relation to the x-axis.
		if there is a draw, the edge closest to the origin wins.
		
		This function is only usable for konvex polygons, but works for concave as well.

		the square distance is used since it's faster to compute

		the shift thing only works if the origin is in the "lower left" corner.
		"""
		if not origin:
			origin=self.origin
		if not areaPoly:
			areaPoly=self.areaPoly
		last=areaPoly[-1]
		longest=None
		dmax=0
		for node in areaPoly:
			d2=fun.getDistanceSq(node, last)
			if d2==dmax: #look for distance to origin
				dtmp=min([fun.getDistanceSq(node, origin), fun.getDistanceSq(last, origin)])
				dtmp2=min([fun.getDistanceSq(node, longest[0]), fun.getDistanceSq(last, longest[1])])
				if dtmp<dtmp2:
					longest = (node,last)
					dmax=d2
			elif d2>dmax:
				longest = (node,last)
				dmax=d2
			last=node
		#now, calculate the distance to this line.
		#we need to make a line, not a ray, in order to get the extension as well.
		infRay=np.array(longest)
		infRay=infRay+1e5*(infRay-infRay[1])+1e5*(infRay-infRay[0]) #infinite extension of longest
		pTmp, t=col.closestLinePoint(origin, infRay, True)
		assert t!=1 and t!=0
		d=fun.getDistance(pTmp,origin)
		assert d>=0
		#now, we know that d+shift=n*L+C..where n is an integer
		n=round((d-self.C)/float(self.L))
		shift=self.L*n+self.C-d
		assert abs(shift)<=self.L
		angle=fun.angleToXAxis(longest)
		assert angle>=0
		if 0<angle<=pi/2: #shift x negative
			shift=(-shift,0)
		elif pi/2<angle<=pi: #shift y negative
			shift=(0,-shift)
		elif pi<angle<=3*pi/2.0: #shift x positive
			shift=(shift,0)
		else:
			shift=(0, -shift)
		return angle, shift
Example #2
0
	def run(self):
		tic=time.clock()
		rN=self.G.roadNet
		for e in rN.edges_iter(data=True):
			r=e[2]['r']
			direction=r.direction
			tList=self.G.terrain.GetTrees(r.pos, self.reach+r.radius, onlyNonHarvested=True)
			ray=np.array(r.endPoints)
			for t in tList:
				p1, tval=col.closestLinePoint(t.pos, ray, additionalInfo=True) #tval will be used later,
				if col.collide(t, r):
					self.chop(t, p1, tval, r) #could get into problems if t is on ray... hmm..
				elif t.marked and fn.getDistance(p1, t.pos)<=self.reach: #marked foo thinning
				   	self.chop(t, p1, tval, r)
		print "harvester routine took %.4f seconds"%(time.clock()-tic)
Example #3
0
	def run(self):
		"""PEM"""
		#first, naive algorithm
		while True:
			best=self.find_next_road() #position is at origin at this point. 'best' is an nx edge instance.
			if not best:
				self.sim.stopSimulation()
				break
			nList=self.getRoute(best) #get there from origin, from a correct direction
			while len(nList)>0:
				pos=nList.pop()
				yield hold, self, self.setPos(pos)
			#so, we are now in position at the beginning of the road.
			self.pickup_road=best #we will harvest from this road during this loop
			road=self.pickup_road[2]['r']
			wayHome=None #will later be a list of pos. This initialization enables pickups on way home.
			while len(road.harvestedTrees)>0:
				t=self.getClosestTree()
				if not t: break #no tree was found
				pos=col.closestLinePoint(t.pos, np.array(road.endPoints)) #road position of closest tree.
				yield (hold, self, self.setPos(pos)) #move to point closest to t.
				cmd=self.pickup() #pickup all trees at current position
				for c in cmd:
					if c != False: yield c
				brokeOut=False
				if False in cmd: break #full load.
				elif len(road.harvestedTrees)==0: #some room left, and road is out of trees..
					if wayHome is None: wayHome=self.getRoute(self.origin)#get home again.
					while len(road.harvestedTrees)==0:
						yield hold, self, self.setPos(wayHome.pop()) #move. We shall now be at beginning of next road
						if len(wayHome)==0:
							brokeOut=True #enables "double break"
							break
						self.nextPos=wayHome[-1] #other end of road
						self.pickup_road=(tuple(self.pos),tuple(self.nextPos),self.roadNet.get_edge_data(tuple(self.pos), tuple(self.nextPos))) #and the winner of ugly coding is... 
						road=self.pickup_road[2]['r']					
				if brokeOut: break
					
			#go back to origin, full cargo.
			if wayHome is None: wayHome=self.getRoute(self.origin)#get home again.
			while len(wayHome)>0:
				yield hold, self, self.setPos(wayHome.pop())
			self.reset()