Example #1
0
def translation_opencl(R_oriented, init_t=None):
  """
  Params:
      mat:
  Returns:
      modelmat: a 4x4 matrix

  global state:
      meanx,meany are the 'fixes' used to transform opencl.get_modelxyz()
      into true model coordinates. The values of opencl.get_modelxyz() do
      not reflect the correct value of modelmat. The modelmat includes a
      correction by [-meanx, 0, -meany] that must be applied to modelxyz,
      and passed as a parameter to opencl.computegridinds.
  """
  assert R_oriented.dtype == np.float32
  global modelmat
  modelmat = np.array(R_oriented)

  # Returns warped coordinates, and sincos values for the lattice
  opencl.compute_lattice2(modelmat[:3,:4], LW)

  global cx,cz,face
  # If we don't have a good initialization for the model space translation,
  # use the centroid of the surface points.
  if init_t:
    global face
    X,Y,Z,face = np.rollaxis(opencl.get_modelxyz(),1)
    cx,_,cz,_ = np.rollaxis(np.frombuffer(np.array(face).data,
                                          dtype='i1').reshape(-1,2),1)

    modelmat[:,3] -= np.round([X[cz!=0].mean()/LW,
                               0,
                               Z[cx!=0].mean()/LW,
                               0])*LW
    opencl.compute_lattice2(modelmat[:3,:4], LW)

  # Find the circular mean, using weights
  def cmean(mxy,c):
    x,y = mxy / c
    a2 = np.arctan2(y,x) / (2*np.pi) * LW
    if np.isnan(a2): a2 = 0
    return a2, np.sqrt(x**2 + y**2), c

  global meanx,meanz,cxyz_,qx2qz2, dmx, dmy, countx, county
  cxyz_,qx2qz2 = opencl.reduce_lattice2()
  meanx,dmx,countx = cmean(qx2qz2[:2],cxyz_[0])
  meanz,dmy,county = cmean(qx2qz2[2:],cxyz_[2])
  modelmat[:,3] -= np.array([meanx, 0, meanz, 0])

  return modelmat
Example #2
0
def translation_opencl(R_oriented, init_t=None):
    """
  Params:
      mat:
  Returns:
      modelmat: a 4x4 matrix

  global state:
      meanx,meany are the 'fixes' used to transform opencl.get_modelxyz()
      into true model coordinates. The values of opencl.get_modelxyz() do
      not reflect the correct value of modelmat. The modelmat includes a
      correction by [-meanx, 0, -meany] that must be applied to modelxyz,
      and passed as a parameter to opencl.computegridinds.
  """
    assert R_oriented.dtype == np.float32
    global modelmat
    modelmat = np.array(R_oriented)

    # Returns warped coordinates, and sincos values for the lattice
    opencl.compute_lattice2(modelmat[:3, :4], LW)

    global cx, cz, face
    # If we don't have a good initialization for the model space translation,
    # use the centroid of the surface points.
    if init_t:
        global face
        X, Y, Z, face = np.rollaxis(opencl.get_modelxyz(), 1)
        cx, _, cz, _ = np.rollaxis(
            np.frombuffer(np.array(face).data, dtype='i1').reshape(-1, 2), 1)

        modelmat[:, 3] -= np.round(
            [X[cz != 0].mean() / LW, 0, Z[cx != 0].mean() / LW, 0]) * LW
        opencl.compute_lattice2(modelmat[:3, :4], LW)

    # Find the circular mean, using weights
    def cmean(mxy, c):
        x, y = mxy / c
        a2 = np.arctan2(y, x) / (2 * np.pi) * LW
        if np.isnan(a2): a2 = 0
        return a2, np.sqrt(x**2 + y**2), c

    global meanx, meanz, cxyz_, qx2qz2, dmx, dmy, countx, county
    cxyz_, qx2qz2 = opencl.reduce_lattice2()
    meanx, dmx, countx = cmean(qx2qz2[:2], cxyz_[0])
    meanz, dmy, county = cmean(qx2qz2[2:], cxyz_[2])
    modelmat[:, 3] -= np.array([meanx, 0, meanz, 0])

    return modelmat
Example #3
0
def carve(xfix=None, zfix=None, use_opencl=True):
    global gridinds, inds, grid
    gridmin = np.zeros((4,), "f")
    gridmax = np.zeros((4,), "f")
    gridmin[:3] = config.bounds[0]
    gridmax[:3] = config.bounds[1]
    global occ, vac

    if xfix is None or zfix is None:
        import lattice

        xfix, zfix = lattice.meanx, lattice.meanz

    if use_opencl:
        opencl.compute_gridinds(xfix, zfix, config.LW, config.LH, gridmin, gridmax)
        gridinds = opencl.get_gridinds()

        if 0:
            inds = gridinds[gridinds[:, 0, 3] != 0, :, :3]
            if len(inds) == 0:
                return None
            bins = [np.arange(0, gridmax[i] - gridmin[i] + 1) for i in range(3)]
            global occ, vac
            occH, _ = np.histogramdd(inds[:, 0, :], bins)
            vacH, _ = np.histogramdd(inds[:, 1, :], bins)

            vac = vacH > 30
            occ = occH > 30

            return occ, vac
        else:
            shape = [gridmax[i] - gridmin[i] for i in range(3)]
            occ = np.zeros(shape, "u1")
            vac = np.zeros(shape, "u1")
            speedup_cy.occvac(gridinds, occ, vac, gridmin.astype("i"), gridmax.astype("i"))
            return occ.astype("bool"), vac.astype("bool")

    else:
        global X, Y, Z, XYZ
        X, Y, Z, face = np.rollaxis(opencl.get_modelxyz(), 1)
        XYZ = np.array((X, Y, Z)).transpose()
        fix = np.array((xfix, 0, zfix))
        cxyz = np.frombuffer(np.array(face).data, dtype="i1").reshape(-1, 4)[:, :3]
        global cx, cy, cz
        cx, cy, cz = np.rollaxis(cxyz, 1)
        f1 = cxyz * 0.5

        mod = np.array([config.LW, config.LH, config.LW])
        gi = np.floor(-gridmin[:3] + (XYZ - fix) / mod + f1)
        gi = np.array((gi, gi - cxyz))
        gi = np.rollaxis(gi, 1)

        def get_gridinds():
            (L, T), (R, B) = opencl.rect
            length = opencl.length
            return (
                gridinds[:length, :, :].reshape(T - B, R - L, 2, 3),
                gridinds[length:, :, :].reshape(T - B, R - L, 2, 3),
            )

        gridinds = gi
        grid = get_gridinds()
        inds = gridinds[np.any(cxyz != 0, 1), :, :]

        if len(inds) == 0:
            return None

        bins = [np.arange(0, gridmax[i] - gridmin[i] + 1) for i in range(3)]

        occH, _ = np.histogramdd(inds[:, 0, :], bins)
        vacH, _ = np.histogramdd(inds[:, 1, :], bins)

        vac = vacH > 30
        occ = occH > 30

        return occ, vac
Example #4
0
def carve(xfix=None, zfix=None, use_opencl=True):
    global gridinds, inds, grid
    gridmin = np.zeros((4, ), 'f')
    gridmax = np.zeros((4, ), 'f')
    gridmin[:3] = config.bounds[0]
    gridmax[:3] = config.bounds[1]
    global occ, vac

    if xfix is None or zfix is None:
        import lattice
        xfix, zfix = lattice.meanx, lattice.meanz

    if use_opencl:
        opencl.compute_gridinds(xfix, zfix, config.LW, config.LH, gridmin,
                                gridmax)
        gridinds = opencl.get_gridinds()

        if 0:
            inds = gridinds[gridinds[:, 0, 3] != 0, :, :3]
            if len(inds) == 0:
                return None
            bins = [
                np.arange(0, gridmax[i] - gridmin[i] + 1) for i in range(3)
            ]
            global occ, vac
            occH, _ = np.histogramdd(inds[:, 0, :], bins)
            vacH, _ = np.histogramdd(inds[:, 1, :], bins)

            vac = vacH > 30
            occ = occH > 30

            return occ, vac
        else:
            shape = [gridmax[i] - gridmin[i] for i in range(3)]
            occ = np.zeros(shape, 'u1')
            vac = np.zeros(shape, 'u1')
            speedup_cy.occvac(gridinds, occ, vac, gridmin.astype('i'),
                              gridmax.astype('i'))
            return occ.astype('bool'), vac.astype('bool')

    else:
        global X, Y, Z, XYZ
        X, Y, Z, face = np.rollaxis(opencl.get_modelxyz(), 1)
        XYZ = np.array((X, Y, Z)).transpose()
        fix = np.array((xfix, 0, zfix))
        cxyz = np.frombuffer(np.array(face).data, dtype='i1').reshape(-1,
                                                                      4)[:, :3]
        global cx, cy, cz
        cx, cy, cz = np.rollaxis(cxyz, 1)
        f1 = cxyz * 0.5

        mod = np.array([config.LW, config.LH, config.LW])
        gi = np.floor(-gridmin[:3] + (XYZ - fix) / mod + f1)
        gi = np.array((gi, gi - cxyz))
        gi = np.rollaxis(gi, 1)

        def get_gridinds():
            (L, T), (R, B) = opencl.rect
            length = opencl.length
            return (gridinds[:length, :, :].reshape(T - B, R - L, 2, 3),
                    gridinds[length:, :, :].reshape(T - B, R - L, 2, 3))

        gridinds = gi
        grid = get_gridinds()
        inds = gridinds[np.any(cxyz != 0, 1), :, :]

        if len(inds) == 0:
            return None

        bins = [np.arange(0, gridmax[i] - gridmin[i] + 1) for i in range(3)]

        occH, _ = np.histogramdd(inds[:, 0, :], bins)
        vacH, _ = np.histogramdd(inds[:, 1, :], bins)

        vac = vacH > 30
        occ = occH > 30

        return occ, vac