def rayTree(self, ray, closest=True): """ Return a ray tree """ from PyRatRay import PyRatRay sun = ray.sun hit = False # first try any infinite planes for i in self.planes: thatHit, thatRay = i.intersects(ray.copy(), closest=True) if thatHit and thatRay.tnear < ray.length: hit = thatHit ray.ccopy(thatRay) ray.length = thatRay.tnear # then try this object and its contents # noting that the infinite planes may have given # a ray.length we need to beat thisHit, thisRay = self.intersects(ray.copy(), closest=closest) if thisHit: ray.ccopy(thisRay) ray.length = thisRay.tnear hit = thisHit try: obj = ray.object except: obj = None if hit and obj != None: try: n = ray.localNormal except: try: n = ray.localNormal = ray.object.surfaceNormal(ray) except: n = np.array([0, 0, 1.0]) # return True,ray,n # now see if we hit the sun ray.hitPoint = ray.length * ray.direction + ray.origin isTransmittance = dot(n, ray.direction) > 0 # import pdb;pdb.set_trace() if isTransmittance: # transmittance sunRay = PyRatRay(ray.hitPoint + PyRatRayTol * ray.direction, sun) else: sunRay = PyRatRay(ray.hitPoint - PyRatRayTol * ray.direction, sun) try: sunRay.view = ray sunHit, sunThisRay = self.intersects(sunRay.copy(), closest=closest) except: # not sure what to do here sunHit = True if not sunHit: return True, ray, n else: return False, ray, np.array([0, 0, 1.0]) else: return False, ray, np.array([0, 0, 1.0])
def rayTree(self, ray, closest=True): ''' Return a ray tree ''' from PyRatRay import PyRatRay sun = ray.sun hit = False # first try any infinite planes for i in self.planes: thatHit, thatRay = i.intersects(ray.copy(), closest=True) if thatHit and thatRay.tnear < ray.length: hit = thatHit ray.ccopy(thatRay) ray.length = thatRay.tnear # then try this object and its contents # noting that the infinite planes may have given # a ray.length we need to beat thisHit, thisRay = self.intersects(ray.copy(), closest=closest) if thisHit: ray.ccopy(thisRay) ray.length = thisRay.tnear hit = thisHit try: obj = ray.object except: obj = None if hit and obj != None: try: n = ray.localNormal except: try: n = ray.localNormal = ray.object.surfaceNormal(ray) except: n = np.array([0, 0, 1.]) #return True,ray,n # now see if we hit the sun ray.hitPoint = ray.length * ray.direction + ray.origin isTransmittance = dot(n, ray.direction) > 0 #import pdb;pdb.set_trace() if isTransmittance: # transmittance sunRay = PyRatRay(ray.hitPoint + PyRatRayTol * ray.direction, sun) else: sunRay = PyRatRay(ray.hitPoint - PyRatRayTol * ray.direction, sun) try: sunRay.view = ray sunHit, sunThisRay = self.intersects(sunRay.copy(), closest=closest) except: # not sure what to do here sunHit = True if not sunHit: return True, ray, n else: return False, ray, np.array([0, 0, 1.]) else: return False, ray, np.array([0, 0, 1.])
def test(base,tip,dimensions=None,\ size=(200,200),sun=[10,-10,20.],direction=[-1,-1.,-1],\ origin=None,focalPoint=None,\ obj=None,name=None,type=None,file=None,info={},nAtTime=200): ''' A simple test of the intersection algorithm A scan over an object is made and images produced in tests with the distances. ''' from PyRatRay import PyRatRay from PyRatEllipsoid import PyRatEllipsoid from PyRatRay import PyRatRay from PyRatCylinder import PyRatCylinder from PyRatFacet import PyRatFacet from PyRatSpheroid import PyRatSpheroid from PyRatDisk import PyRatDisk from PyRatPlane import PyRatPlane from PyRatObjParser import PyRatObjParser from PyRatClone import PyRatClone import pylab as plt import matplotlib.cm as cm try: import pp isPP = True except: isPP = False import sys import os import pylab as plt type = type or str(globals()['__file__'].split(os.sep())[-1].split('.')[0]) if 'verbose' in info: sys.stderr.write('Object: %s\n'%type) type = type.split('/')[-1] exec('from %s import %s'%(type,type)) name = name or type[5:] obj = obj or eval('%s(base,tip,info=info)'%type) # ray direction direction = np.array(direction) direction /= sqrt(dot(direction,direction)) if origin == None: origin = -direction * 6.0 if focalPoint == None: focalPoint = origin*1.5 # sun direction sun = np.array(sun) sun /= sqrt(dot(sun,sun)) # image size #size = (200,200) # ray origins #origin = np.array([0,0,4]).astype(float) o = origin.copy() ray = PyRatRay(o,direction) # dimensions of the image in physical units if dimensions == None: dimensions = [2,2] result0 = np.zeros(size) result1 = np.zeros(size) result2 = np.zeros(size) sys.stderr.write('from %s in direction %s\n'%(str(origin),str(direction))) sys.stderr.write('Name: %s\n'%name) if len(sys.argv) > 1: try: ncpus = int(sys.argv[1]) except: ncpus = -1 else: ncpus = -1 # tuple of all parallel python servers to connect with if ncpus != 0 and isPP: ppservers = () if ncpus > 0: # Creates jobserver with ncpus workers job_server = pp.Server(ncpus, ppservers=ppservers) else: # Creates jobserver with automatically detected number of workers job_server = pp.Server(ppservers=ppservers) print "Starting pp with", job_server.get_ncpus(), "workers" sims = [] l = float(size[0]) index = [] ray.sun = sun for ix in xrange(size[0]): o[0] = origin[0] + dimensions[0] * 2.*(ix-size[0]*0.5)/size[0] for iy in xrange(size[1]): o[1] = origin[1] + dimensions[1] * 2.*(iy-size[1]*0.5)/size[1] ray.length = PyRatBig d = (o - focalPoint) ray.direction = d/sqrt(dot(d,d)) index.append((ix,iy)) sims.append(ray.copy()) sims = np.array(sims) index = np.array(index) results = [] for i in xrange(0,len(sims),nAtTime): try: f = job_server.submit(obj.rayTreeMany,(sims[i:i+nAtTime],)) j = index[i:i+nAtTime] except: f = job_server.submit(obj.rayTreeMany,(sims[i:],)) j = index[i:] results.append([j[:,0],j[:,1],f]) if 'verbose' in info: sys.stderr.write('\nGathering results\n') l = size[0]*size[1] for c in xrange(len(results)): if 'verbose' in info and int(100.*(c+1)/l) % 5 == 0: sys.stderr.write('\b\b\b\b\b\b\b\b%.2f%%'%(100*(c+1)/l)) r = results[c] f = r[2] thisResult = f() # this returns True,ray,n try: ww = np.where(np.array(thisResult)[:,0])[0] iix = r[0][ww] iiy = r[1][ww] for j,val in enumerate(np.array(thisResult)[ww]): if val[0]: ix = iix[j] iy = iiy[j] ray = val[1] n = val[2]/np.dot(val[2],val[2]) lambert = dot(n,sun) if lambert < 0: lambert = 0 result0[size[0]-1-ix,size[1]-1-iy] = lambert result1[size[0]-1-ix,size[1]-1-iy] = ray.tnear result2[size[0]-1-ix,size[1]-1-iy] = ray.tfar except: pass else: l = size[0] ray.sun = sun for ix in xrange(size[0]): o[0] = origin[0] + dimensions[0] * 2.*(ix-size[0]*0.5)/size[0] if 'verbose' in info and int(100.*(ix+1)/l) % 5 == 0: sys.stderr.write('\b\b\b\b\b\b\b\b%.2f%%'%(100*(ix+1)/l)) for iy in xrange(size[1]): o[1] = origin[1] + dimensions[1] * 2.*(iy-size[1]*0.5)/size[1] ray.length = ray.tnear = ray.tfar =PyRatBig ray.origin = o d = (o - focalPoint) ray.direction = d/sqrt(dot(d,d)) try: hit,thisRay,n = obj.rayTree(ray.copy()) except: hit = False try: print obj,ray print ray.copy() except: hit = False if hit: lambert = dot(n,sun) if lambert < 0: lambert = 0 result0[size[0]-1-ix,size[1]-1-iy] = lambert result1[size[0]-1-ix,size[1]-1-iy] = thisRay.tnear result2[size[0]-1-ix,size[1]-1-iy] = thisRay.tfar else: missed = True if 'verbose' in info: sys.stderr.write('\nWriting results\n') plt.clf() plt.imshow(result0,interpolation='nearest',cmap=cm.Greys_r) if 'verbose' in info: sys.stderr.write('Mean: %f\n'%np.mean(result0)) plt.colorbar() if not os.path.exists('tests'): os.makedirs('tests') plt.savefig('tests/PyRat%s.png'%name or file) plt.clf() plt.imshow(result1,interpolation='nearest',cmap=cm.Greys_r) plt.colorbar() plt.savefig('tests/PyRat%s-near.png'%name or file) plt.clf() plt.imshow(result2,interpolation='nearest',cmap=cm.Greys_r) plt.colorbar() plt.savefig('tests/PyRat%s-far.png'%name or file)
def test(base,tip,dimensions=None,\ size=(200,200),sun=[10,-10,20.],direction=[-1,-1.,-1],\ origin=None,focalPoint=None,\ obj=None,name=None,type=None,file=None,info={},nAtTime=200): ''' A simple test of the intersection algorithm A scan over an object is made and images produced in tests with the distances. ''' from PyRatRay import PyRatRay from PyRatEllipsoid import PyRatEllipsoid from PyRatRay import PyRatRay from PyRatCylinder import PyRatCylinder from PyRatFacet import PyRatFacet from PyRatSpheroid import PyRatSpheroid from PyRatDisk import PyRatDisk from PyRatPlane import PyRatPlane from PyRatObjParser import PyRatObjParser from PyRatClone import PyRatClone import pylab as plt import matplotlib.cm as cm try: import pp isPP = True except: isPP = False import sys import os import pylab as plt type = type or str(globals()['__file__'].split(os.sep())[-1].split('.')[0]) if 'verbose' in info: sys.stderr.write('Object: %s\n' % type) type = type.split('/')[-1] exec('from %s import %s' % (type, type)) name = name or type[5:] obj = obj or eval('%s(base,tip,info=info)' % type) # ray direction direction = np.array(direction) direction /= sqrt(dot(direction, direction)) if origin == None: origin = -direction * 6.0 if focalPoint == None: focalPoint = origin * 1.5 # sun direction sun = np.array(sun) sun /= sqrt(dot(sun, sun)) # image size #size = (200,200) # ray origins #origin = np.array([0,0,4]).astype(float) o = origin.copy() ray = PyRatRay(o, direction) # dimensions of the image in physical units if dimensions == None: dimensions = [2, 2] result0 = np.zeros(size) result1 = np.zeros(size) result2 = np.zeros(size) sys.stderr.write('from %s in direction %s\n' % (str(origin), str(direction))) sys.stderr.write('Name: %s\n' % name) if len(sys.argv) > 1: try: ncpus = int(sys.argv[1]) except: ncpus = -1 else: ncpus = -1 # tuple of all parallel python servers to connect with if ncpus != 0 and isPP: ppservers = () if ncpus > 0: # Creates jobserver with ncpus workers job_server = pp.Server(ncpus, ppservers=ppservers) else: # Creates jobserver with automatically detected number of workers job_server = pp.Server(ppservers=ppservers) print "Starting pp with", job_server.get_ncpus(), "workers" sims = [] l = float(size[0]) index = [] ray.sun = sun for ix in xrange(size[0]): o[0] = origin[0] + dimensions[0] * 2. * (ix - size[0] * 0.5) / size[0] for iy in xrange(size[1]): o[1] = origin[1] + dimensions[1] * 2. * ( iy - size[1] * 0.5) / size[1] ray.length = PyRatBig d = (o - focalPoint) ray.direction = d / sqrt(dot(d, d)) index.append((ix, iy)) sims.append(ray.copy()) sims = np.array(sims) index = np.array(index) results = [] for i in xrange(0, len(sims), nAtTime): try: f = job_server.submit(obj.rayTreeMany, (sims[i:i + nAtTime], )) j = index[i:i + nAtTime] except: f = job_server.submit(obj.rayTreeMany, (sims[i:], )) j = index[i:] results.append([j[:, 0], j[:, 1], f]) if 'verbose' in info: sys.stderr.write('\nGathering results\n') l = size[0] * size[1] for c in xrange(len(results)): if 'verbose' in info and int(100. * (c + 1) / l) % 5 == 0: sys.stderr.write('\b\b\b\b\b\b\b\b%.2f%%' % (100 * (c + 1) / l)) r = results[c] f = r[2] thisResult = f() # this returns True,ray,n try: ww = np.where(np.array(thisResult)[:, 0])[0] iix = r[0][ww] iiy = r[1][ww] for j, val in enumerate(np.array(thisResult)[ww]): if val[0]: ix = iix[j] iy = iiy[j] ray = val[1] n = val[2] / np.dot(val[2], val[2]) lambert = dot(n, sun) if lambert < 0: lambert = 0 result0[size[0] - 1 - ix, size[1] - 1 - iy] = lambert result1[size[0] - 1 - ix, size[1] - 1 - iy] = ray.tnear result2[size[0] - 1 - ix, size[1] - 1 - iy] = ray.tfar except: pass else: l = size[0] ray.sun = sun for ix in xrange(size[0]): o[0] = origin[0] + dimensions[0] * 2. * (ix - size[0] * 0.5) / size[0] if 'verbose' in info and int(100. * (ix + 1) / l) % 5 == 0: sys.stderr.write('\b\b\b\b\b\b\b\b%.2f%%' % (100 * (ix + 1) / l)) for iy in xrange(size[1]): o[1] = origin[1] + dimensions[1] * 2. * ( iy - size[1] * 0.5) / size[1] ray.length = ray.tnear = ray.tfar = PyRatBig ray.origin = o d = (o - focalPoint) ray.direction = d / sqrt(dot(d, d)) try: hit, thisRay, n = obj.rayTree(ray.copy()) except: hit = False try: print obj, ray print ray.copy() except: hit = False if hit: lambert = dot(n, sun) if lambert < 0: lambert = 0 result0[size[0] - 1 - ix, size[1] - 1 - iy] = lambert result1[size[0] - 1 - ix, size[1] - 1 - iy] = thisRay.tnear result2[size[0] - 1 - ix, size[1] - 1 - iy] = thisRay.tfar else: missed = True if 'verbose' in info: sys.stderr.write('\nWriting results\n') plt.clf() plt.imshow(result0, interpolation='nearest', cmap=cm.Greys_r) if 'verbose' in info: sys.stderr.write('Mean: %f\n' % np.mean(result0)) plt.colorbar() if not os.path.exists('tests'): os.makedirs('tests') plt.savefig('tests/PyRat%s.png' % name or file) plt.clf() plt.imshow(result1, interpolation='nearest', cmap=cm.Greys_r) plt.colorbar() plt.savefig('tests/PyRat%s-near.png' % name or file) plt.clf() plt.imshow(result2, interpolation='nearest', cmap=cm.Greys_r) plt.colorbar() plt.savefig('tests/PyRat%s-far.png' % name or file)