void apply_polyaffine(PyArrayObject* XYZ, const PyArrayObject* Centers, const PyArrayObject* Affines, const PyArrayObject* Sigma) { PyArrayIterObject *iter_xyz, *iter_centers, *iter_affines; int axis = 1; double *xyz, *center, *affine, *sigma; double w, W; double mat[12], t_xyz[3]; size_t bytes_mat = 12*sizeof(double); size_t bytes_xyz = 3*sizeof(double); /* Initialize arrays and iterators */ sigma = PyArray_DATA(Sigma); iter_xyz = (PyArrayIterObject*)PyArray_IterAllButAxis((PyObject*)XYZ, &axis); iter_centers = (PyArrayIterObject*)PyArray_IterAllButAxis((PyObject*)Centers, &axis); iter_affines = (PyArrayIterObject*)PyArray_IterAllButAxis((PyObject*)Affines, &axis); /* Loop over input points */ while(iter_xyz->index < iter_xyz->size) { xyz = PyArray_ITER_DATA(iter_xyz); PyArray_ITER_RESET(iter_centers); PyArray_ITER_RESET(iter_affines); memset((void*)mat, 0, bytes_mat); W = 0.0; /* Loop over centers */ while(iter_centers->index < iter_centers->size) { center = PyArray_ITER_DATA(iter_centers); affine = PyArray_ITER_DATA(iter_affines); w = _gaussian(xyz, center, sigma); W += w; _add_weighted_affine(mat, affine, w); PyArray_ITER_NEXT(iter_centers); PyArray_ITER_NEXT(iter_affines); } /* Apply matrix */ _apply_affine(t_xyz, mat, xyz, W); memcpy((void*)xyz, (void*)t_xyz, bytes_xyz); /* Update xyz iterator */ PyArray_ITER_NEXT(iter_xyz); } /* Free memory */ Py_XDECREF(iter_xyz); Py_XDECREF(iter_centers); Py_XDECREF(iter_affines); return; }
void histogram(double* H, unsigned int clamp, PyArrayIterObject* iter) { signed short *buf; signed short i; /* Reset the source image iterator */ PyArray_ITER_RESET(iter); /* Re-initialize joint histogram */ memset((void*)H, 0, clamp*sizeof(double)); /* Loop over source voxels */ while(iter->index < iter->size) { /* Source voxel intensity */ buf = (signed short*)PyArray_ITER_DATA(iter); i = buf[0]; /* Update the histogram only if the current voxel is below the intensity threshold */ if (i>=0) H[i]++; /* Update source index */ PyArray_ITER_NEXT(iter); } /* End of loop over voxels */ return; }
PyArrayObject* make_edges(const PyArrayObject* idx, int ngb_size) { int* ngb = _select_neighborhood_system(ngb_size); PyArrayIterObject* iter = (PyArrayIterObject*)PyArray_IterNew((PyObject*)idx); int* buf_ngb; npy_intp xi, yi, zi, xj, yj, zj; npy_intp u2 = idx->dimensions[2]; npy_intp u1 = idx->dimensions[1]*u2; npy_intp u0 = idx->dimensions[0]*u1; npy_intp mask_size = 0, n_edges = 0; npy_intp idx_i; npy_intp *buf_idx; npy_intp *edges_data, *buf_edges; npy_intp ngb_idx; npy_intp pos; PyArrayObject* edges; npy_intp dim[2] = {0, 2}; /* First loop over the input array to determine the mask size */ while(iter->index < iter->size) { buf_idx = (npy_intp*)PyArray_ITER_DATA(iter); if (*buf_idx >= 0) mask_size ++; PyArray_ITER_NEXT(iter); } /* Allocate the array of edges using an upper bound of the required memory space */ edges_data = (npy_intp*)malloc(2 * ngb_size * mask_size * sizeof(npy_intp)); /* Second loop over the input array */ PyArray_ITER_RESET(iter); iter->contiguous = 0; /* To force coordinates to be updated */ buf_edges = edges_data; while(iter->index < iter->size) { xi = iter->coordinates[0]; yi = iter->coordinates[1]; zi = iter->coordinates[2]; buf_idx = (npy_intp*)PyArray_ITER_DATA(iter); idx_i = *buf_idx; /* Loop over neighbors if current point is within the mask */ if (idx_i >= 0) { buf_ngb = ngb; for (ngb_idx=0; ngb_idx<ngb_size; ngb_idx++) { /* Get neighbor coordinates */ xj = xi + *buf_ngb; buf_ngb++; yj = yi + *buf_ngb; buf_ngb++; zj = zi + *buf_ngb; buf_ngb++; pos = xj*u1 + yj*u2 + zj; /* Store edge if neighbor is within the mask */ if ((pos < 0) || (pos >= u0)) continue; buf_idx = (npy_intp*)idx->data + pos; if (*buf_idx < 0) continue; buf_edges[0] = idx_i; buf_edges[1] = *buf_idx; n_edges ++; buf_edges += 2; } } /* Increment iterator */ PyArray_ITER_NEXT(iter); } /* Reallocate edges array to account for connections suppressed due to masking */ edges_data = realloc((void *)edges_data, 2 * n_edges * sizeof(npy_intp)); dim[0] = n_edges; edges = (PyArrayObject*) PyArray_SimpleNewFromData(2, dim, NPY_INTP, (void*)edges_data); /* Transfer ownership to python (to avoid memory leaks!) */ edges->flags = (edges->flags) | NPY_OWNDATA; /* Free memory */ Py_XDECREF(iter); return edges; }
void joint_histogram(double* H, unsigned int clampI, unsigned int clampJ, PyArrayIterObject* iterI, const PyArrayObject* imJ_padded, const double* Tvox, int affine, int interp) { const signed short* J=(signed short*)imJ_padded->data; size_t dimJX=imJ_padded->dimensions[0]-2; size_t dimJY=imJ_padded->dimensions[1]-2; size_t dimJZ=imJ_padded->dimensions[2]-2; signed short Jnn[8]; double W[8]; signed short *bufI, *bufJnn; double *bufW; signed short i, j; size_t off; size_t u2 = imJ_padded->dimensions[2]; size_t u3 = u2+1; size_t u4 = imJ_padded->dimensions[1]*u2; size_t u5 = u4+1; size_t u6 = u4+u2; size_t u7 = u6+1; double wx, wy, wz, wxwy, wxwz, wywz; double W0, W2, W3, W4; size_t x, y, z; int nn, nx, ny, nz; double Tx, Ty, Tz; double *bufTvox = (double*)Tvox; void (*interpolate)(unsigned int, double*, unsigned int, const signed short*, const double*, int, void*); void* interp_params = NULL; rk_state rng; /* Reset the source image iterator */ PyArray_ITER_RESET(iterI); /* Make sure the iterator the iterator will update coordinate values */ UPDATE_ITERATOR_COORDS(iterI); /* Set interpolation method */ if (interp==0) interpolate = &_pv_interpolation; else if (interp>0) interpolate = &_tri_interpolation; else { /* interp < 0 */ interpolate = &_rand_interpolation; rk_seed(-interp, &rng); interp_params = (void*)(&rng); } /* Re-initialize joint histogram */ memset((void*)H, 0, clampI*clampJ*sizeof(double)); /* Looop over source voxels */ while(iterI->index < iterI->size) { /* Source voxel intensity */ bufI = (signed short*)PyArray_ITER_DATA(iterI); i = bufI[0]; /* Compute the transformed grid coordinates of current voxel */ if (affine) { /* Get voxel coordinates and apply transformation on-the-fly*/ x = iterI->coordinates[0]; y = iterI->coordinates[1]; z = iterI->coordinates[2]; _affine_transform(&Tx, &Ty, &Tz, Tvox, x, y, z); } else /* Use precomputed transformed coordinates */ bufTvox = _precomputed_transform(&Tx, &Ty, &Tz, (const double*)bufTvox); /* Test whether the current voxel is below the intensity threshold, or the transformed point is completly outside the reference grid */ if ((i>=0) && (Tx>-1) && (Tx<dimJX) && (Ty>-1) && (Ty<dimJY) && (Tz>-1) && (Tz<dimJZ)) { /* Nearest neighbor (floor coordinates in the padded image, hence +1). Notice that using the floor function doubles excetution time. FIXME: see if we can replace this with assembler instructions. */ nx = FLOOR(Tx) + 1; ny = FLOOR(Ty) + 1; nz = FLOOR(Tz) + 1; /* The convention for neighbor indexing is as follows: * * Floor slice Ceil slice * * 2----6 3----7 y * | | | | ^ * | | | | | * 0----4 1----5 ---> x */ /*** Trilinear interpolation weights. Note: wx = nnx + 1 - Tx, where nnx is the location in the NON-PADDED grid */ wx = nx - Tx; wy = ny - Ty; wz = nz - Tz; wxwy = wx*wy; wxwz = wx*wz; wywz = wy*wz; /*** Prepare buffers */ bufJnn = Jnn; bufW = W; /*** Initialize neighbor list */ off = nx*u4 + ny*u2 + nz; nn = 0; /*** Neighbor 0: (0,0,0) */ W0 = wxwy*wz; APPEND_NEIGHBOR(off, W0); /*** Neighbor 1: (0,0,1) */ APPEND_NEIGHBOR(off+1, wxwy-W0); /*** Neighbor 2: (0,1,0) */ W2 = wxwz-W0; APPEND_NEIGHBOR(off+u2, W2); /*** Neightbor 3: (0,1,1) */ W3 = wx-wxwy-W2; APPEND_NEIGHBOR(off+u3, W3); /*** Neighbor 4: (1,0,0) */ W4 = wywz-W0; APPEND_NEIGHBOR(off+u4, W4); /*** Neighbor 5: (1,0,1) */ APPEND_NEIGHBOR(off+u5, wy-wxwy-W4); /*** Neighbor 6: (1,1,0) */ APPEND_NEIGHBOR(off+u6, wz-wxwz-W4); /*** Neighbor 7: (1,1,1) */ APPEND_NEIGHBOR(off+u7, 1-W3-wy-wz+wywz); /* Update the joint histogram using the desired interpolation technique */ interpolate(i, H, clampJ, Jnn, W, nn, interp_params); } /* End of IF TRANSFORMS INSIDE */ /* Update source index */ PyArray_ITER_NEXT(iterI); } /* End of loop over voxels */ return; }
int joint_histogram(PyArrayObject* JH, unsigned int clampI, unsigned int clampJ, PyArrayIterObject* iterI, const PyArrayObject* imJ_padded, const PyArrayObject* Tvox, long interp) { const signed short* J=(signed short*)imJ_padded->data; size_t dimJX=imJ_padded->dimensions[0]-2; size_t dimJY=imJ_padded->dimensions[1]-2; size_t dimJZ=imJ_padded->dimensions[2]-2; signed short Jnn[8]; double W[8]; signed short *bufI, *bufJnn; double *bufW; signed short i, j; size_t off; size_t u2 = imJ_padded->dimensions[2]; size_t u3 = u2+1; size_t u4 = imJ_padded->dimensions[1]*u2; size_t u5 = u4+1; size_t u6 = u4+u2; size_t u7 = u6+1; double wx, wy, wz, wxwy, wxwz, wywz; double W0, W2, W3, W4; int nn, nx, ny, nz; double *H = (double*)PyArray_DATA(JH); double Tx, Ty, Tz; double *tvox = (double*)PyArray_DATA(Tvox); void (*interpolate)(unsigned int, double*, unsigned int, const signed short*, const double*, int, void*); void* interp_params = NULL; prng_state rng; /* Check assumptions regarding input arrays. If it fails, the function will return -1 without doing anything else. iterI : assumed to iterate over a signed short encoded, possibly non-contiguous array. imJ_padded : assumed C-contiguous (last index varies faster) & signed short encoded. H : assumed C-contiguous. Tvox : assumed C-contiguous: either a 3x4=12-sized array (or bigger) for an affine transformation or a 3xN array for a pre-computed transformation, with N equal to the size of the array corresponding to iterI (no checking done) */ if (PyArray_TYPE(iterI->ao) != NPY_SHORT) { fprintf(stderr, "Invalid type for the array iterator\n"); return -1; } if ( (!PyArray_ISCONTIGUOUS(imJ_padded)) || (!PyArray_ISCONTIGUOUS(JH)) || (!PyArray_ISCONTIGUOUS(Tvox)) ) { fprintf(stderr, "Some non-contiguous arrays\n"); return -1; } /* Reset the source image iterator */ PyArray_ITER_RESET(iterI); /* Set interpolation method */ if (interp==0) interpolate = &_pv_interpolation; else if (interp>0) interpolate = &_tri_interpolation; else { /* interp < 0 */ interpolate = &_rand_interpolation; prng_seed(-interp, &rng); interp_params = (void*)(&rng); } /* Re-initialize joint histogram */ memset((void*)H, 0, clampI*clampJ*sizeof(double)); /* Looop over source voxels */ while(iterI->index < iterI->size) { /* Source voxel intensity */ bufI = (signed short*)PyArray_ITER_DATA(iterI); i = bufI[0]; /* Compute the transformed grid coordinates of current voxel */ Tx = *tvox; tvox++; Ty = *tvox; tvox++; Tz = *tvox; tvox++; /* Test whether the current voxel is below the intensity threshold, or the transformed point is completly outside the reference grid */ if ((i>=0) && (Tx>-1) && (Tx<dimJX) && (Ty>-1) && (Ty<dimJY) && (Tz>-1) && (Tz<dimJZ)) { /* Nearest neighbor (floor coordinates in the padded image, hence +1). Notice that using the floor function doubles excetution time. FIXME: see if we can replace this with assembler instructions. */ nx = FLOOR(Tx) + 1; ny = FLOOR(Ty) + 1; nz = FLOOR(Tz) + 1; /* The convention for neighbor indexing is as follows: * * Floor slice Ceil slice * * 2----6 3----7 y * | | | | ^ * | | | | | * 0----4 1----5 ---> x */ /*** Trilinear interpolation weights. Note: wx = nnx + 1 - Tx, where nnx is the location in the NON-PADDED grid */ wx = nx - Tx; wy = ny - Ty; wz = nz - Tz; wxwy = wx*wy; wxwz = wx*wz; wywz = wy*wz; /*** Prepare buffers */ bufJnn = Jnn; bufW = W; /*** Initialize neighbor list */ off = nx*u4 + ny*u2 + nz; nn = 0; /*** Neighbor 0: (0,0,0) */ W0 = wxwy*wz; APPEND_NEIGHBOR(off, W0); /*** Neighbor 1: (0,0,1) */ APPEND_NEIGHBOR(off+1, wxwy-W0); /*** Neighbor 2: (0,1,0) */ W2 = wxwz-W0; APPEND_NEIGHBOR(off+u2, W2); /*** Neightbor 3: (0,1,1) */ W3 = wx-wxwy-W2; APPEND_NEIGHBOR(off+u3, W3); /*** Neighbor 4: (1,0,0) */ W4 = wywz-W0; APPEND_NEIGHBOR(off+u4, W4); /*** Neighbor 5: (1,0,1) */ APPEND_NEIGHBOR(off+u5, wy-wxwy-W4); /*** Neighbor 6: (1,1,0) */ APPEND_NEIGHBOR(off+u6, wz-wxwz-W4); /*** Neighbor 7: (1,1,1) */ APPEND_NEIGHBOR(off+u7, 1-W3-wy-wz+wywz); /* Update the joint histogram using the desired interpolation technique */ interpolate(i, H, clampJ, Jnn, W, nn, interp_params); } /* End of IF TRANSFORMS INSIDE */ /* Update source index */ PyArray_ITER_NEXT(iterI); } /* End of loop over voxels */ return 0; }