Commit 1f37c9b3 authored by Martin Reinecke's avatar Martin Reinecke

improved loop structure

parent 120462aa
Pipeline #28341 passed with stages
in 2 minutes and 37 seconds
......@@ -16,37 +16,42 @@ class Pixellister(object):
self._start = start
self._end = end
self._dir = end - start
self._dirlen = np.linalg.norm(self._dir)
self._dir *= 1./self._dirlen
def _losdist(self, pos):
t1 = pos - self._start
dotpr = np.dot(self._dir, t1)
if dotpr < 0:
return np.linalg.norm(t1)
elif dotpr <= self._dirlen:
return np.linalg.norm(pos - self._start - self._dir*dotpr)
else:
return np.linalg.norm(pos-self._end)
def build_pixellist(self, lo, hi, pixels):
self._dirlen = np.linalg.norm(self._dir, axis=0)
self._dir *= 1./self._dirlen.reshape((1, -1))
def _losdist(self, idx, pos):
pos2 = pos.reshape((-1, 1))
sidx = self._start[:, idx]
eidx = self._end[:, idx]
didx = self._dir[:, idx]
dlidx = self._dirlen[idx]
t1 = pos2 - sidx
dotpr = np.sum(didx*t1, axis=0)
return np.where(
dotpr < 0, np.linalg.norm(t1, axis=0),
np.where(dotpr > dlidx, np.linalg.norm(pos2-eidx, axis=0),
np.linalg.norm(pos2 - sidx - didx*dotpr, axis=0)))
def build_pixellist(self, idx, lo, hi, pixels):
diff = hi-lo
if np.all(diff == 1):
dist = self._losdist(lo)
if dist < self.Rmax:
pixels.append((lo, dist))
return
mid = (lo+hi-1.)*0.5
t1 = mid-lo
if self._losdist(mid) < np.linalg.norm(t1) + self.Rmax:
dim = np.argmax(diff)
imid = lo[dim]+diff[dim]//2
lmid = lo.copy()
lmid[dim] = imid
hmid = hi.copy()
hmid[dim] = imid
self.build_pixellist(lo, hmid, pixels)
self.build_pixellist(lmid, hi, pixels)
dist = self._losdist(idx, mid)
if np.all(diff == 1):
for i in range(len(dist)):
if dist[i] < self.Rmax:
pixels[idx[i]].append((lo, dist[i]))
else:
t1 = mid-lo
idx2 = idx[dist < np.linalg.norm(t1) + self.Rmax]
if len(idx2) > 0:
dim = np.argmax(diff)
imid = lo[dim]+diff[dim]//2
lmid = lo.copy()
lmid[dim] = imid
hmid = hi.copy()
hmid[dim] = imid
self.build_pixellist(idx2, lo, hmid, pixels)
self.build_pixellist(idx2, lmid, hi, pixels)
def _comp_traverse(start, end, shp):
......@@ -55,13 +60,12 @@ def _comp_traverse(start, end, shp):
for i in range(-2, -len(shp)-1, -1):
inc[i] = inc[i+1]*shp[i+1]
out = [None]*nlos
pl = Pixellister(start, end)
pixels = [[] for _ in range(nlos)]
pl.build_pixellist(np.arange(nlos), np.array(shp)*0, np.array(shp), pixels)
for i in range(nlos):
print i
pixels = []
pl = Pixellister(start[:, i], end[:, i])
pl.build_pixellist(np.array(shp)*0, np.array(shp), pixels)
pix1 = np.array([np.sum(inc*p[0]) for p in pixels])
wgt = np.array([np.exp(-p[1]*p[1]) for p in pixels])
pix1 = np.array([np.sum(inc*p[0]) for p in pixels[i]])
wgt = np.array([np.exp(-p[1]*p[1]) for p in pixels[i]])
out[i] = (pix1, wgt)
return out
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment