   
from OCC.BRep import BRep_Builder
from OCC.BRepBuilderAPI import *
from OCC.TopoDS import *
from OCC.gp import gp_Pnt
from OCC.BRep import *
from OCC.Utils import *
from OCC.GeomAdaptor import *
from OCC.GCPnts import *
import random

brepbuilder = BRep_Builder()

def curve_discretizer(gcurve,first,last):
        if gcurve.IsNull():
            print "Null shape given."
            return
        curve_adaptor = GeomAdaptor_Curve(gcurve)
        discretizer= GCPnts_UniformDeflection()
        deflection = 0.05 
        discretizer.Initialize(curve_adaptor,deflection,first,last)
        assert(discretizer.IsDone())
        assert(discretizer.NbPoints()>0)
        result = discretizer.NbPoints()
        curve_object = gcurve.GetObject()
        

random.seed()
for i in xrange(20):
    GarbageCollector.garbage.push_context()
    print 'running number',i
    verts = []
    xsize = int(random.uniform(0,100))
    ysize = int(random.uniform(0,100))
    print 'creating points',xsize,'x',ysize
    for x in xrange(xsize):
        for y in xrange(ysize):
            v = TopoDS_Vertex()
            brepbuilder.MakeVertex(v,gp_Pnt(x,y,0.),1e-7)
            verts.append(v)
    poly_maker = BRepBuilderAPI_MakePolygon()
    print 'creating poly'
    for point in verts:
        poly_maker.Add(point)
    if poly_maker.IsDone():
        poly_maker.Close()
        wire = poly_maker.Wire()
        topo = Topo(wire)
        print 'discretizing'
        for e in topo.edges():
            curve_data = BRep_Tool_Curve(e)
            gcurve = curve_data[0]
            first = curve_data[1]
            last = curve_data[2]
            curve_discretizer(gcurve,first,last)
    verts = []
    print 'collecting garbage'
    GarbageCollector.garbage.smart_purge()
    GarbageCollector.garbage.pop_context()
