merged the drivers, and am investigating the qdelauney manpage ... should've done this earlier :P

--HG--
rename : bin/driver-random.py => bin/driver.py
rename : test/utest.py => test/qhull.test.py
This commit is contained in:
Stephen Mardson McQuay 2010-01-30 12:15:00 -07:00
parent f4b2c95cf5
commit 98b13fb8c5
8 changed files with 143 additions and 158 deletions

View File

@ -1,56 +0,0 @@
#!/usr/bin/python
import sys
from optparse import OptionParser
import numpy as np
import scipy.spatial
from grid import simple_random_grid, exact_func
import baker
def rms(errors):
r = 0.0
for i in errors:
r += np.power(i, 2)
r = np.sqrt(r / len(errors))
return r
if __name__ == '__main__':
parser = OptionParser()
parser.add_option("-o", "--output-file", dest="output", type='str', default = '/tmp/for_qhull.txt', help = "qhull output file")
parser.add_option("-e", "--extra-points", dest="extra", type='int', default = 3, help = "how many extra points")
parser.add_option("-s", "--source-total", dest="source_total", type='int', default = 100, help = "total number of source points")
parser.add_option("-d", "--destination-total",dest="destination_total",type='int', default = 100, help = "total number of destination points")
parser.add_option("-v", "--verbose", action = 'store_true', default = False, help = "verbosity")
(options, args) = parser.parse_args()
print >> sys.stderr, options
mesh_source = simple_random_grid(options.source_total)
tree = scipy.spatial.KDTree(mesh_source.points)
mesh_dest = simple_random_grid(options.destination_total)
open(options.output, 'w').write(mesh_source.for_qhull())
print >> sys.stderr, "wrote output to %s" % options.output
errors = []
success = 0
for x in mesh_dest.points:
lin, error, final = baker.run_baker(x, mesh_source, tree, options.extra, options.verbose)
exact = exact_func(x[0], x[1])
if np.abs(exact - final) < np.abs(exact - lin):
success += 1
if options.verbose:
print "current point : %s" % x
print "exact : %0.4f" % exact
print "qlin : %0.4f" % lin
print "q_final : %0.4f" % final
print "qlinerr : %0.4f" % (exact - lin,)
print "q_final_err : %0.4f" % (exact - final,)
cur_error = np.abs(final - exact)
errors.append(cur_error)
print rms(errors)
print "%s of %s won" % (success, options.destination_total)

View File

@ -1,45 +0,0 @@
#!/usr/bin/python
import sys
from optparse import OptionParser
import numpy as np
import scipy.spatial
from grid import simple_rect_grid, exact_func
import baker
from tools import rms
if __name__ == '__main__':
parser = OptionParser()
parser.add_option("-e", "--extra-points", dest="extra", type='int', default = 3, help = "how many extra points")
parser.add_option("-s", "--source-density", dest="source", type='int', default = 11, help = "resolution of source mesh")
parser.add_option("-d", "--dest-density", dest="dest", type='int', default = 11, help = "resolution of dest mesh")
parser.add_option("-t", "--random-total", dest="random_total", type='int', default = 100, help = "total number of random points")
parser.add_option("-r", "--random-dest", action = 'store_true', default = False, help = "verbosity")
parser.add_option("-v", "--verbose", action = 'store_true', default = False, help = "verbosity")
(options, args) = parser.parse_args()
print >> sys.stderr, options
mesh_source = simple_rect_grid(options.source, options.source)
tree = scipy.spatial.KDTree(mesh_source.points)
mesh_dest = simple_rect_grid(options.dest, options.dest)
if options.random_dest:
x = []
for i in xrange(options.random_total):
rx = np.random.rand() * 2 - 1
ry = np.random.rand() * 2 - 1
x.append([rx, ry])
mesh_dest.points = np.array(x)
errors = []
for x in mesh_dest.points:
(final, exact) = baker.run_baker(x, mesh_source, tree, options.extra, options.verbose)
cur_error = np.abs(final - exact)
errors.append(cur_error)
print rms(errors)

52
bin/driver.py Executable file
View File

@ -0,0 +1,52 @@
#!/usr/bin/python
import sys
from optparse import OptionParser
import numpy as np
import scipy.spatial
import baker
from tools import rms, get_mesh, exact_func
if __name__ == '__main__':
parser = OptionParser()
parser.add_option("-o", "--output-file", dest="output", type='str', default = '/tmp/for_qhull.txt', help = "qhull output file")
parser.add_option("-e", "--extra-points", dest="extra", type='int', default = 3, help = "how many extra points")
parser.add_option("-s", "--source-total", dest="source_total", type='int', default = 100, help = "total number of source points for random, resolution for structured")
parser.add_option("-d", "--destination-total", dest="destination_total", type='int', default = 100, help = "total number of destination points, resolution for structured")
parser.add_option("-r", "--structured", action = 'store_true', default = False, help = "use a structured grid instead of random point cloud")
parser.add_option("-v", "--verbose", action = 'store_true', default = False, help = "verbosity")
(options, args) = parser.parse_args()
print >> sys.stderr, options
mesh_source, mesh_dest = get_mesh(options.source_total, options.destination_total, options.structured)
tree = scipy.spatial.KDTree(mesh_source.points)
open(options.output, 'w').write(mesh_source.for_qhull())
print >> sys.stderr, "wrote source mesh output to %s" % options.output
errors = []
success = 0
for x in mesh_dest.points:
lin, error, final = baker.run_baker(x, mesh_source, tree, options.extra, options.verbose)
exact = exact_func(x[0], x[1])
if np.abs(exact - final) < np.abs(exact - lin):
success += 1
if options.verbose:
print "current point : %s" % x
print "exact : %0.4f" % exact
print "qlin : %0.4f" % lin
print "q_final : %0.4f" % final
print "qlinerr : %0.4f" % (exact - lin,)
print "q_final_err : %0.4f" % (exact - final,)
cur_error = np.abs(final - exact)
errors.append(cur_error)
print rms(errors)
print "%s of %s won" % (success, options.destination_total)

42
data/geomview.txt Normal file
View File

@ -0,0 +1,42 @@
{appearance {linewidth 3} LIST # | qdelaunay Qt GD2
{ OFF 3 1 1 # f24
-1 -8.01e-15 0
-8.01e-15 -1 0
-8.01e-15 -8.01e-15 0
3 0 1 2 0.7071 0.7071 0 1.0 }
{ OFF 3 1 1 # f25
-8.01e-15 -1 0
-1 -8.01e-15 0
-1 -1 0
3 0 1 2 0.7071 0.7071 0 1.0 }
{ OFF 3 1 1 # f28
8.01e-15 -1 0
1 -8.01e-15 0
8.01e-15 -8.01e-15 0
3 0 1 2 0.9659 0.2588 0 1.0 }
{ OFF 3 1 1 # f29
1 -8.01e-15 0
8.01e-15 -1 0
1 -1 0
3 0 1 2 0.9659 0.2588 0 1.0 }
{ OFF 3 1 1 # f32
-8.01e-15 1 0
-1 8.01e-15 0
-8.01e-15 8.01e-15 0
3 0 1 2 0.2588 0.9659 0 1.0 }
{ OFF 3 1 1 # f33
-1 8.01e-15 0
-8.01e-15 1 0
-1 1 0
3 0 1 2 0.2588 0.9659 0 1.0 }
{ OFF 3 1 1 # f36
1 8.01e-15 0
8.01e-15 1 0
8.01e-15 8.01e-15 0
3 0 1 2 0.7071 0.7071 0 1.0 }
{ OFF 3 1 1 # f38
8.01e-15 1 0
1 8.01e-15 0
1 1 0
3 0 1 2 0.7071 0.7071 0 1.0 }
}

View File

@ -4,9 +4,7 @@ import sys
import numpy as np
import scipy.spatial
def exact_func(x, y):
return np.power((np.sin(x * np.pi) * np.cos(y * np.pi)), 2)
return np.sin(x * np.pi) * np.cos(y * np.pi)
from tools import exact_func
class grid(object):

View File

@ -1,6 +1,13 @@
import numpy as np
import grid
class smberror(Exception):
def __init__(self, val):
self.value = val
def __str__(self):
return repr(self.value)
def rms(errors):
"""
root mean square calculation
@ -10,3 +17,23 @@ def rms(errors):
r += np.power(i, 2)
r = np.sqrt(r / len(errors))
return r
def exact_func(x, y):
return np.power((np.sin(x * np.pi) * np.cos(y * np.pi)), 2)
return np.sin(x * np.pi) * np.cos(y * np.pi)
def get_mesh(source, destination, use_structured_grid = False):
mesh_source = None
mesh_dest = None
if use_structured_grid:
mesh_source = grid.simple_rect_grid(source, source)
mesh_dest = grid.simple_rect_grid(destination, destination)
else:
mesh_source = grid.simple_random_grid(source)
mesh_dest = grid.simple_random_grid(destination)
if not (mesh_dest and mesh_source):
raise smberror('problem creating mesh objects')
else:
return mesh_source, mesh_dest

View File

@ -1,33 +1,29 @@
#!/usr/bin/python
import random
import unittest
import delaunay
import subprocess
QFILE = '/tmp/forQhull.txt'
class TestSequenceFunctions(unittest.TestCase):
def setUp(self):
self.l = [[-1, 1], [-1, 0], [-1, 1], [0, -1], [0, 0], [0, 1], [1, -1], [1, 0], [1, 1]]
l = [[-1, 1], [-1, 0], [-1, 1], [0, -1], [0, 0], [0, 1], [1, -1], [1, 0], [1, 1]]
qdelauney_file = open(QFILE, 'w')
def testQhull(self):
dt = delaunay.Triangulation(self.l)
answer = [
[4,1,3],
[1,5,0],
[5,1,4],
[7,3,6],
[7,4,3],
[7,5,4],
[5,7,8],
]
qdelauney_file.write("%d\n" % len(l[0]))
qdelauney_file.write("%d\n" % len(l))
for i in l:
qdelauney_file.write("%0.3f %0.3f\n" % (i[0], i[1]))
self.assertEqual(dt.indices, answer)
dt = delaunay.Triangulation(l)
print dt.indices
answer = [
[4,1,3],
[1,5,0],
[5,1,4],
[7,3,6],
[7,4,3],
[7,5,4],
[5,7,8],
]
print answer == dt.indices
print dt.indices
print "now run /usr/bin/qdelaunay Qt i < %s" % QFILE
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(TestSequenceFunctions)
unittest.TextTestRunner(verbosity=5).run(suite)

View File

@ -1,29 +0,0 @@
#!/usr/bin/python
import random
import unittest
import delaunay
class TestSequenceFunctions(unittest.TestCase):
def setUp(self):
self.l = [[-1, 1], [-1, 0], [-1, 1], [0, -1], [0, 0], [0, 1], [1, -1], [1, 0], [1, 1]]
def testQhull(self):
dt = delaunay.Triangulation(self.l)
answer = [
[4,1,3],
[1,5,0],
[5,1,4],
[7,3,6],
[7,4,3],
[7,5,4],
[5,7,8],
]
self.assertEqual(dt.indices, answer)
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(TestSequenceFunctions)
unittest.TextTestRunner(verbosity=5).run(suite)