from cs1graphics import *
CS1Point = Point             # avoid name conflict between cs1graphics.Point and our Point module
import Point
import delaunay
import random
import RandomGeom
import DCEL
import sys


_usageString = """
Usage: python driver.py [inputfile]  [animation canvas width]

Inputfile should be a text file with one "x y" point per line.
If not given, data will be generated randomly based upon parameters
given by the user.

If animation is desired, the canvas width for that animation should be
given.  Otherwise there will be no intermediate animation, however the
final image will be rendered at the conclusion."""

def _usage():
    print _usageString
    sys.exit()

if __name__ == '__main__':

    animationWidth = None
    if len(sys.argv) > 1:
        pts = []
        filename = sys.argv[1]
        try:
            for line in file(filename):
                data = line.split()
                if not line.startswith('#') and len(data) == 2:
                    try:
                        pts.append(Point.Point2D(int(data[0]),int(data[1])))   # x,y values
                    except ValueError:
                        print 'ignoring line:',line
            if len(sys.argv) > 2:
                try:
                    animationWidth = int(sys.argv[2])
                except:
                    _usage()
        except:
            print 'cannot open file', filename
            print _usage()
    else:
        # pick random points
        filename = 'sample'
        n = 0
        while n <= 0:
            response = raw_input('How many points? [default 25] ')
            if not response:
                response = '25'
            try:
                n = int(response)
            except ValueError:
                print 'invalid response'
        grid = 0
        while grid <= 0:
            response = raw_input('Maximum coordinates? [default 500] ')
            if not response:
                response = '500'
            try:
                grid = int(response)
            except ValueError:
                print 'invalid response'

        seed = None
        while seed is None:
            response = raw_input('Random integer seed? [default is random] ')
            try:
                seed = int(response)
            except:
                if response:
                    print 'seed must be integer'
                seed = random.randint(0,1000000)
                print 'Using seed', seed

        response = raw_input('Width of animation canvas? [press enter for NO animation] ')
        if response:
            try:
                animationWidth = int(response)
            except ValueError:
                print 'invalid width.  Not using animation'

        center = Point.Point2D(grid//2, grid//2)
        radius = grid*2//5                  # 80% 


        gen = RandomGeom.RandomGeom(seed)
        pts = gen.randPointSetInBall(n, center, radius)

    dc = delaunay.computeDelaunay(pts, animationWidth)

    dc.validate();  print 'self-consistency of DCEL verified'
    print
    print 'rendering subdivision after the triangulation'
    layAfter,lookup = DCEL.renderDCEL(dc, vertRadius=5)
    layAfter.move(50,500)
    afterImage = Canvas(500,500)
    afterImage.add(layAfter)

    filenameDot = filename.rfind('.')
    if filenameDot != -1:
        filenameBase = filename[:filenameDot]
    else:
        filenameBase = filenameDot

    #afterImage.saveToFile(filenameBase+".ps")
