예제 #1
0
def parseQT(justTheRectangles=False):
    my_file = filePath('QTData/tree.txt')
    if not my_file.is_file():
        print(
            "QTData/tree.txt not created yet. Running QTData/CreateQT.exe ...")
        os.startfile('QTData/CreateQT.exe')
        print("Done.")
    pass

    try:
        file = open('QTData/tree.txt', 'r')
    except IOError:
        print("This shouldn't happen!")
        raise
    pass

    mapfile = file.readline().rstrip()

    #parse the rectangles barriers
    limits, rectVects = parseMap(mapfile)

    #Quick and dirty for the demo
    if justTheRectangles:
        return limits, rectVects
    pass

    data = [float(val) for val in file.read().strip().split()]

    gdict = {}

    limitRect = [[(limits[0], limits[2]), (limits[1], limits[2]),
                  (limits[1], limits[3]), (limits[0], limits[3])]]

    i = 0
    while (i < len(data) - 4):
        x = data[i]
        y = data[i + 1]
        l = data[i + 2]
        w = data[i + 3]

        num_adjacent = int(data[i + 4])
        i += 5

        if not Helper.isInsideRect((x, y), rectVects) and num_adjacent != 0:

            if (x, y) not in gdict:
                gdict[(x, y)] = {}
            pass

            for j in range(i, i + num_adjacent * 4, 4):
                x1 = data[j]
                y1 = data[j + 1]
                # l1 = data[j+2]
                # w1 = data[j+3]

                if not Helper.isInsideRect(
                    (x1, y1), rectVects
                ):  #and not Helper.crossesBox((x,y),(x1,y1),rectVects):
                    # dist = l1*l1 + w1*w1
                    dist = Helper.distancesq((x, y), (x1, y1))
                    gdict[(x, y)][(x1, y1)] = dist
                    if (x1, y1) in gdict:
                        gdict[(x1, y1)][(x, y)] = dist
                    else:
                        gdict[(x1, y1)] = {(x, y): dist}
                    pass
                pass
            pass
        pass

        i += num_adjacent * 4

    pass

    print("Number of nodes: {0}".format(len(gdict)))
    clean(gdict, rectVects)
    print("Number of nodes in cleaned graph: {0}".format(len(gdict)))

    return gdict, limits, rectVects
예제 #2
0
def runDemo():

	print("\n")
	animatedprint("Hi, welcome to our short demo.")
	printsleep(1.0)
	animatedprint("Here we'll be doing a recompile and quick runthrough of all of our code.")
	animatedprint("---------------------------------------------------------------------------\n")
	printsleep(3.0)

	mapfile = "map2.txt"
	qt_threshold = 5
	actual_to_max_children_ratio = 0.75
	max_num_adjacent = 30

	os.chdir(os.path.dirname(os.path.abspath(__file__)))
	os.chdir("QTData")
	animatedprint("We'll start by compiling CreateQT.exe...")
	os.system("gcc -std=c99 -o CreateQT CreateQT.c")
	print("Done!\n")
	printsleep(2.0)
	animatedprint("Now we create the quadtree of %s with parameters"%mapfile)
	print("\tqt_threshold = %d"%qt_threshold)
	printsleep(0.1)
	print("\tactual_to_max_children_ratio = %f"%actual_to_max_children_ratio)
	printsleep(0.1)
	print("\tmax_num_adjacent = %d"%max_num_adjacent)
	printsleep(2.0)
	os.system("CreateQT.exe {0} {1} {2} {3}".format(mapfile,qt_threshold,actual_to_max_children_ratio,max_num_adjacent))
	print("Done!\n")
	printsleep(2.0)
	os.chdir("..")

	animatedprint("Now we'll find the shortest path between all nodes...")
	printsleep(2.0)
	import QTPathFinder as qt
	pack,pdict = qt.findAllPaths()
	mngr = plt.get_current_fig_manager()
	geom = mngr.window.geometry()
	x, y, dx, dy = geom.getRect()
	mngr.window.setGeometry(100, 100, dx, dy)
	plt.show()
	plt.pause(0.5)
	print("Done!\n")
	printsleep(1.0)

	animatedprint("Now let's find some paths. Bring up the figure if you don't already see it.")
	import DataParser as dp
	import HelperMethods as hm
	#To get legal points
	limits, rectVects = dp.parseQT(justTheRectangles=True)

	xrng = limits[1]-limits[0]
	yrng = limits[3]-limits[2]
	numPaths = 150

	startPoints = [(xrng*(random.random()),yrng*(random.random())) for i in range(numPaths)]
	endPoints = [(xrng*(random.random()),yrng*(random.random())) for i in range(numPaths)]
	removal = []
	for p in startPoints:
		if hm.isInsideRect(p,rectVects):
			removal.append(p)
		pass
	pass
	for r in removal:
		startPoints.remove(r)
	pass

	removal = []
	for p in endPoints:
		if hm.isInsideRect(p,rectVects):
			removal.append(p)
		pass
	pass
	for r in removal:
		endPoints.remove(r)
	pass
	printsleep(3.0)
	animatedprint("Begin plotting...")
	printsleep(1.0)

	demoPoints = dict(zip(startPoints,endPoints))

	line = lines.Line2D([], [], lw=2, c='red')
	ax = pack[2]
	ax.add_line(line)

	for s,e in demoPoints.items():
		line = qt.plotPathWithResults(pack,pdict,s,e,line)
		print("Start: ({:f},{:f}) | End: ({:f},{:f})".format(s[0],s[1],e[0],e[1]))
		sys.stdout.flush()
		plt.show()
		plt.pause(0.001)
	pass
	print("...Done!")
	printsleep(1.0)

	animatedprint("\nThis ends the demo. Thanks for watching!")
	plt.pause(5.0)