bhtree-1.5.7~rc1+cvs.20140424/0000755000175000017500000000000012326212377015154 5ustar moellermoellerbhtree-1.5.7~rc1+cvs.20140424/LICENSE0000644000175000017500000000436411033241702016154 0ustar moellermoellerThis software is copyrighted by Michel F. Sanner (sanner@scripps.edu) and TSRI. The following terms apply to all files associated with the software unless explicitly disclaimed in individual files. MGLTOOLS SOFTWARE LICENSE AGREEMENT. 1. Grant Of Limited License; Software Use Restrictions. The programs received by you will be used only for NON COMMERCIAL purposes. This license is issued to you as an individual. For COMMERCIAL use done with the software please contact Michel F. Sanner for details about commercial usage license agreements. For any question regarding license agreements, please contact Michel Sanner: TSRI, Molecular Biology Department, TCP 26, 10550 North Torrey Pines Road, La Jolla, CA 92037 sanner@scripps.edu tel (858) 784-7742 fax (858) 784-2341 2. COMMERCIAL USAGE is defined as revenues generating activities. These include using this software for consulting activities and selling applications built on top of, or using this software. Scientific research in an academic environment and teaching are considered NON COMMERCIAL. 3. Copying Restrictions. You will not sell or otherwise distribute commercially these programs or derivatives to any other party, whether with or without consideration. 4. Ownership of Software. You will not obtain, and will not attempt to obtain copyright coverage thereon without the express purpose written consent of The Scripps Research Institute and Dr. Sanner. 5. IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 6. THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. bhtree-1.5.7~rc1+cvs.20140424/MANIFEST.in0000644000175000017500000000033711033241700016677 0ustar moellermoellerinclude MANIFEST.in include version.py include bhtree/*.i include src/* exclude bhtree/bhtreelib.py include CVS/* include bhtree/CVS/* include bhtree/Tests/CVS/* include src/CVS/* include bhtree/doc.tar.gz include LICENSE bhtree-1.5.7~rc1+cvs.20140424/README0000644000175000017500000000163310260335727016037 0ustar moellermoellerThis package contains a Distutils interface for building bhtree Python extension and creating a source distribution. SWIG (version 1.3.20 or higher) is used for generating a C wrapper code (can be downloaded from www.swig.org). To build and install the extension: python2.4 setup.py install This creates bhtreelib.py (python shadow classes) and _bhtreelib.so in build/lib./bhtree . bhtree/Tests is copied to build/lib./bhtree. The bhtree package is then installed in sys.exec_prefix/lib/python2.4/site-packages. To specify the install directory use --install-platlib option: python2.4 setup.py install --install-platlib=INSTALL_DIR To build the extension only: python2.4 setup.py build this will build bhtree python extension in build/lib./bhtree. To build the distribution: python2.4 setup.py sdist Testing of the installed package: tester bhtree bhtree-1.5.7~rc1+cvs.20140424/setup.py0000644000175000017500000000564110675044243016674 0ustar moellermoeller# setup.py - Distutils interface for building Bhtree Python extension and # creating a distribution. from distutils.core import setup, Extension from distutils.command.build import build from distutils.command.sdist import sdist import sys from os import path pack_name = "bhtree" ext_name = "_bhtreelib" #source = [path.join(pack_name, "bhtreelib.i"), path.join(pack_name, "bhtree.c")] source = [path.join(pack_name, "bhtreelib.i"), path.join("src", "bhtree.c")] #include_dirs = [path.join(".",pack_name)] import numpy numpy_include = numpy.get_include() include_dirs = [path.join(".", "src"), numpy_include] #if sys.platform == "darwin": #include_dirs.append("/usr/include/sys") # for malloc.h # Change the order of commands that are called by "build" # so that "build_py" command is called after "build_ext". In this case # "build_py" will install (copy to the build directory) python # modules generated by "build_ext'. class modified_build(build): sub_commands = [('build_clib', build.has_c_libraries), ('build_ext', build.has_ext_modules), ('build_py', build.has_pure_modules), ('build_scripts', build.has_scripts), ] # Overwrite the prune_file_list method of sdist to not # remove automatically the RCS/CVS directory from the distribution. class modified_sdist(sdist): def prune_file_list(self): build = self.get_finalized_command('build') base_dir = self.distribution.get_fullname() self.filelist.exclude_pattern(None, prefix=build.build_base) self.filelist.exclude_pattern(None, prefix=base_dir) try: from version import VERSION except: VERSION = "1.0" # setup() dist = setup(name="bhtree", version=VERSION, description = "Bhtree library extension module", author = "Molecular Graphics Laboratory", author_email = "mgltools@scripps.edu", url = "http://www.scripps.edu/~sanner/python/packager.html", py_modules = ['bhtree.bhtreelib', 'bhtree.__init__'], packages = ["bhtree.Tests"], cmdclass = {"build" : modified_build, 'sdist' : modified_sdist}, ext_package = pack_name, ext_modules = [Extension (ext_name, source, include_dirs = include_dirs, #define_macros = [('UNIX', None),], ) ] ,) # cp built .so from ./build/lib.platform to ./pack_name/ - for testing. ## from distutils.util import get_platform ## from distutils.file_util import copy_file ## plat_specifier = ".%s-%s" % (get_platform(), sys.version[0:3]) ## orig_ext = path.join(".", "build", "lib"+plat_specifier , pack_name, ext_name+".so") ## cp_ext = path.join(pack_name, ext_name+".so") ## if path.isfile(orig_ext): ## copy_file(orig_ext, cp_ext, update=1) bhtree-1.5.7~rc1+cvs.20140424/version.py0000644000175000017500000000002011475262473017211 0ustar moellermoellerVERSION="1.5.6" bhtree-1.5.7~rc1+cvs.20140424/bhtree/0000755000175000017500000000000012326212377016425 5ustar moellermoellerbhtree-1.5.7~rc1+cvs.20140424/bhtree/__init__.py0000644000175000017500000000017510404124274020532 0ustar moellermoellerfrom bhtreelib import * __MGLTOOLSVersion__ = '1-4alpha3' CRITICAL_DEPENDENCIES = ['mglutil'] NONCRITICAL_DEPENDENCIES = [] bhtree-1.5.7~rc1+cvs.20140424/bhtree/bhtreelib.i0000644000175000017500000004561511364156601020550 0ustar moellermoeller%module bhtreelib %{ #include "bhtree.h" %} //%include NumericArrays.i %include numarr.i FLOAT_ARRAY2D(xyz, [1][3], nbxyz) FLOAT_VECTOR(rad, [1], nbrad) INT_VECTOR( ids, [1], nb ) %typemap( python, out) int * { if ($1) { PyArrayObject *array; npy_intp dims[2] = {1, 2}; dims[0] = ($1[0]-1)/2; if (dims[0]==0) { $result = PyList_New(0); } else { array = (PyArrayObject *)PyArray_SimpleNew(2, dims, PyArray_INT); memcpy( array->data, &$1[1], 2*dims[0]*sizeof(int) ); $result = (PyObject *)array; } free($1); /* , (char *)($1+1)); array->flags |= NPY_OWNDATA; */ } else { $result = PyList_New(0); } } %typemap(out) int *closestPointsArray { if ($1) { PyArrayObject *array; npy_intp dims[1]; dims[0] = $1[0]; if (dims[0]==0) { $result = PyList_New(0); } else { array = (PyArrayObject *)PyArray_SimpleNew(1, dims, PyArray_INT); memcpy( array->data, &$1[1], dims[0]*sizeof(int) ); $result = (PyObject *)array; } free($1); } else { $result = PyList_New(0); } } %typemap(out) int *closestPointsArrayDist2 { if ($1) { PyArrayObject *array; npy_intp dims[1]; dims[0] = $1[0]; if (dims[0]==0) { $result = PyList_New(0); } else { array = (PyArrayObject *)PyArray_SimpleNew(1, dims, PyArray_INT); memcpy( array->data, &$1[1], dims[0]*sizeof(int) ); $result = (PyObject *)array; } free($1); } else { $result = PyList_New(0); } } %typemap(in) (float xyz[1][3], int nbxyz, float *cl_dist ) (PyArrayObject *xyzarr, int expected_dims[2]) %{ expected_dims[0] = 0; expected_dims[1] = 3; xyzarr = contiguous_typed_array($input, PyArray_FLOAT, 2, expected_dims); if (! xyzarr) return NULL; $1 = (float *)xyzarr->data; $2 = xyzarr->dimensions[0]; $3 = (int *)malloc($2 * sizeof(int)); if (!$3) { PyErr_SetString(PyExc_RuntimeError, "Failed to allocate memory for distances array"); return NULL; } %} %typemap(argout) (float xyz[1][3], int nbxyz, float *cl_dist)(PyArrayObject *out, npy_intp lDim[1]) %{ lDim[0] = $2; if ($result == NULL) { free($3); PyErr_SetString(PyExc_RuntimeError,"Failed: findClosestAtomsDist2()\n"); return NULL; } out = (PyArrayObject *)PyArray_SimpleNewFromData(1, lDim, PyArray_FLOAT, (char *)$3); if (!out) { PyErr_SetString(PyExc_RuntimeError, "Failed to allocate memory for array distances"); return NULL; } #ifdef _MSC_VER switch ( WinVerMajor() ) { case 6: break; // Vista default: out->flags |= NPY_OWNDATA; } #else // so we'll free this memory when this // array will be garbage collected out->flags |= NPY_OWNDATA; #endif $result = l_output_helper2($result, (PyObject *)out); %} %typemap(freearg)(float xyz[1][3], int nbxyz, float *cl_dist) %{ if (xyzarr$argnum) Py_DECREF(xyzarr$argnum); %} %extend BHtree { /* Build a BHTree for a given set of 3D points and radii Granularity: average number of points in a leaf (10 is good) example: bht = bhtree.BHTree( pts, None, 10) result = Numeric.zeros( (400,) ).astype('i') nb = bht.ClosePoints(( 0.0, 0.0, 0.0 ), 10.0, result ) dist = Numeric.zeros( (400,) ).astype('f') */ BHtree ( float xyz[1][3], int *nbxyz, float rad[1], int nbrad, int granularity) { BHtree *self; BHpoint **BHat; int i, nb=nbxyz[0]; float r, maxr; if (nbrad!=0 && nbxyz[0]!=nbrad) { PyErr_SetString(PyExc_ValueError, "Number of points and radii mismatch"); return NULL; } BHat = (BHpoint **)malloc(nb*sizeof(BHpoint *)); if (BHat == NULL) return NULL; maxr=0.0; for (i=0;ix[0]=xyz[i][0]; BHat[i]->x[1]=xyz[i][1]; BHat[i]->x[2]=xyz[i][2]; if (rad != NULL) r = rad[i]; else r=0.0; BHat[i]->r=r; if (r > maxr) maxr = r; BHat[i]->at=i; } self = generateBHtree(BHat,nb,granularity); self->maxr = maxr; if (! self ) { PyErr_SetString(PyExc_RuntimeError, "Fail to build BHTree"); return NULL; } return self; } /* the destructor is supposed to be called from Python: del bht - it doesn't work (SWIG bug ???) */ ~BHTree() { freeBHtree(self); } /* Retrieve points in tree, withtin a given distance of a 3D point, the result is an array of point ids and a number of points NOTE: the result array must be allocated before the call, if it is too short (size=n), only the first n points are reported */ int closePoints(float pt[3], float cutoff, int result[1], int maxn) { return findBHcloseAtoms(self, pt, cutoff, result, maxn); } /* Retrieve points in tree, withtin a given distance of a 3D point, the result is an array of point ids and a number of points along with an array of distances to the given 3D point NOTE: both result array must be allocated before the call, if it is too short (size=n), only the first n points are reported */ int closePointsDist(float pt[3], float cutoff, int result[1], int maxn,float dist_2[1], int maxd) { return findBHcloseAtomsdist(self, pt, cutoff, result, dist_2, maxn); } /* Retrieve points in tree, within a given distance of a 3D point, the result is an array of point ids and a number of points along with an array of square of distances to the given 3D point NOTE: both result array must be allocated before the call, if it is too short (size=n), only the first n points are reported */ int closePointsDist2(float pt[3], float cutoff, int result[1], int maxn,float dist_2[1], int maxd) { return findBHcloseAtomsdist2(self, pt, cutoff, result, dist_2, maxn); } /* Retrieve pairs of points in tree, such that their distance is less than tolerance*(sum of radii) */ int *closePointsPairsInTree( float tolerance) { int *pairs = findClosePairsInTree(self, tolerance); pairs = findClosePairsInTree(self, tolerance); if (! self ) { PyErr_SetString(PyExc_RuntimeError, "Fail to build BHTree"); return NULL; } return pairs; } /* Retrieve pairs of points (pi,pj) such that their distance is less than tolerance*(sum of radii) and pi is a point in the tree and pj is a point in the set provided to the function. */ int *closePointsPairs( float xyz[1][3], int *nbxyz, float rad[1], int nbrad, float tolerance ) { int *pairs; if (nbxyz[0]!=nbrad) { PyErr_SetString(PyExc_ValueError, "Number of points and radii mismatch"); return NULL; } pairs = findClosePairs(self, xyz, nbxyz, rad, nbrad, tolerance); if (! self ) { PyErr_SetString(PyExc_RuntimeError, "Fail to build BHTree"); return NULL; } return pairs; } /* For every point in a given set of 3D points - xyz[1][3] - find a closest atom within a given cutoff. An array of corresponding atom indices is returned. */ int *closestPointsArray (float xyz[1][3], int *nbxyz, float cut, int returnNullIfFail=1) { int *array; array = findClosestAtoms(self, xyz, nbxyz, cut, returnNullIfFail); if (! self ) { PyErr_SetString(PyExc_RuntimeError, "Fail to build BHTree"); return NULL; } return array; } int *closestPointsArrayDist2(float xyz[1][3], int nbxyz, float *cl_dist, float cut, int returnNullIfFail=1) { int *array; array = findClosestAtomsDist2(self, xyz, nbxyz, cl_dist, cut, returnNullIfFail); if (! self ) { PyErr_SetString(PyExc_RuntimeError, "Fail to build BHTree"); return NULL; } return array; } } FLOAT_ARRAY2D(points3D, [1][3], dims) %{ /**************************************************************/ /* Input: INT_VECTOR */ /**************************************************************/ static PyArrayObject *contiguous_typed_array1(PyObject *obj, int typecode, int expectnd, int *expectdims, int noRealloc) { PyArrayObject *arr; int i, numitems, itemsize; char buf[255]; /* if the shape and type are OK, this function increments the reference count and arr points to obj */ if((arr = (PyArrayObject *)PyArray_ContiguousFromObject(obj, typecode, 0, 10)) == NULL) { sprintf(buf,"Failed to make a contiguous array of type %d\n", typecode); PyErr_SetString(PyExc_ValueError, buf); return NULL; } if (noRealloc==1) { if (arr!=obj) { sprintf(buf,"Numeric array of type %d was expected and not received\n", typecode); PyErr_SetString(PyExc_ValueError, buf); return NULL; } } if(expectnd>0) { if(arr->nd > expectnd + 1 || arr->nd < expectnd) { Py_DECREF((PyObject *)arr); PyErr_SetString(PyExc_ValueError, "Array has wrong number of dimensions"); return NULL; } if(arr->nd == expectnd + 1) { if(arr->dimensions[arr->nd - 1] != 1) { Py_DECREF((PyObject *)arr); PyErr_SetString(PyExc_ValueError, "Array has wrong number of dimensions"); return NULL; } } if(expectdims) { for(i = 0; i < expectnd; i++) if(expectdims[i]>0) if(expectdims[i] != arr->dimensions[i]) { Py_DECREF((PyObject *)arr); sprintf(buf,"The extent of dimension %d is %d while %d was expected\n", i, arr->dimensions[i], expectdims[i]); PyErr_SetString(PyExc_ValueError, buf); return NULL; } } } return arr; } %} %define INOUT_INT_VECTOR( ARRAYNAME, ARRAYSHAPE, LENGTH ) %typemap(in) (int ARRAYNAME##ARRAYSHAPE, int LENGTH) (PyArrayObject *array, int expected_dims[1]) %{ if ($input != Py_None) { expected_dims[0] = $1_dim0; if (expected_dims[0]==1) expected_dims[0]=0; array = contiguous_typed_array1($input, PyArray_INT, 1, expected_dims, 1); if (! array) return NULL; $1 = (int *)array->data; $2 = ((PyArrayObject *)(array))->dimensions[0]; } else { array = NULL; $1 = NULL; $2 = 0; } %} %typemap(freearg) (int ARRAYNAME##ARRAYSHAPE, int LENGTH) %{ if (array$argnum) Py_DECREF((PyObject *)array$argnum); %} %enddef INOUT_INT_VECTOR(result, [1], maxn) FLOAT_VECTOR(dist_2,[1],maxd) %include typemaps.i %typemap(in) float[3](float temp[3]) { /* temp[3] becomes a local variable*/ int i; if (PyTuple_Check($input)) { if (!PyArg_ParseTuple($input,"fff",temp,temp+1,temp+2)) { PyErr_SetString(PyExc_TypeError,"tuple must have 3 elements"); return NULL; } $1 = &temp[0]; } else { PyErr_SetString(PyExc_TypeError,"expected a tuple."); return NULL; } } %extend TBHTree { /* Build a TBHTree for a given set of 3D points ids: array of integers used to identify each point in the tree Granularity: average number of points in a leaf (10 is good) leafPadding: added space to allow for points motion in the tree (10) SpacePadding: margins added to the bounding box to insure that points remain in bounding box after motion (99999.9) example: bht = bhtree.TBHTree( pts, ids, 10, 10, 9999.0 ) result = Numeric.zeros( (400,) ).astype('i') nb = bht.ClosePoints(( 0.0, 0.0, 0.0 ), 10.0, result ) dist = Numeric.zeros( (400,) ).astype('f') nb = bht.ClosePointsDist2(( 0.0, 0.0, 0.0 ), 10.0, result, dist ) print Numeric.sqrt(dist[:nb]) */ TBHTree ( float points3D[1][3], int *dims, int ids[1], int nb, int Granularity, int LeafPadding, float SpacePadding) { TBHTree *self; TBHPoint *Pts; float *fp = &points3D[0][0]; int i, *ip = ids; if (dims[0] != nb) { PyErr_SetString(PyExc_RuntimeError, "Array size mismatch between point and ids"); return NULL; } Pts = (TBHPoint *)malloc(nb*sizeof(TBHPoint)); if (!Pts) { PyErr_SetString(PyExc_RuntimeError, "Failed to allocate memory for points"); return NULL; } for (i=0;i=nbVert){ new_fs[*count] = i; (*count)++; } } /*printf("count=%d\n", *count); */ if (*count < nx) new_fs = realloc(new_fs, (*count)*sizeof(int)); return new_fs; } %} INT_VECTOR( vs, [1], vs_len ) %typemap(in,numinputs=0) int *count %{ int arr_len; $1 = &arr_len; %} %typemap(in) (int fs[], int fs_len[2])(PyArrayObject *array, int intdims[2]) %{ if ($input != Py_None) { array = contiguous_typed_array($input, PyArray_INT, 2, NULL); if (! array) return NULL; intdims[0] = ((PyArrayObject *)(array))->dimensions[0]; intdims[1] = ((PyArrayObject *)(array))->dimensions[1]; $2 = intdims; $1 = (int *)array->data; } else { array = NULL; $1 = NULL; $2 = 0; } %} %typemap(freearg) ( int fs[], int* fs_len) %{ if (array$argnum) Py_DECREF((PyObject *)array$argnum); %} %typemap(out) int *findFaceSubset { if ($1) { PyArrayObject *array; npy_intp dims[1]; dims[0] = arr_len; if (dims[0]==0) { $result = PyList_New(0); } else { array = (PyArrayObject *)PyArray_SimpleNew(1, dims, PyArray_INT); memcpy( array->data, $1, arr_len*sizeof(int) ); $result = (PyObject *)array; } free($1); } else { $result = PyList_New(0); } } int *findFaceSubset(int vs[1], int vs_len, int fs[], int fs_len[2], int *count, int nbVert); %include "src/bhtree.h" bhtree-1.5.7~rc1+cvs.20140424/bhtree/numarr.i0000644000175000017500000006606611364156735020127 0ustar moellermoeller%init %{ import_array(); /* load the Numeric PyCObjects */ %} %{ #ifdef _MSC_VER #include #define WinVerMajor() LOBYTE(LOWORD(GetVersion())) #endif #include "numpy/arrayobject.h" static PyArrayObject *contiguous_typed_array(PyObject *obj, int typecode, int expectnd, int *expectdims) { PyArrayObject *arr; int i, numitems, itemsize; char buf[255]; /* if the shape and type are OK, this function increments the reference count and arr points to obj */ if((arr = (PyArrayObject *)PyArray_ContiguousFromObject(obj, typecode, 0, 10)) == NULL) { sprintf(buf,"Failed to make a contiguous array of type %d\n", typecode); PyErr_SetString(PyExc_ValueError, buf); return NULL; } if(expectnd>0) { if(arr->nd > expectnd + 1 || arr->nd < expectnd) { Py_DECREF((PyObject *)arr); PyErr_SetString(PyExc_ValueError, "Array has wrong number of dimensions"); return NULL; } if(arr->nd == expectnd + 1) { if(arr->dimensions[arr->nd - 1] != 1) { Py_DECREF((PyObject *)arr); PyErr_SetString(PyExc_ValueError, "Array has wrong number of dimensions"); return NULL; } } if(expectdims) { for(i = 0; i < expectnd; i++) if(expectdims[i]>0) if(expectdims[i] != arr->dimensions[i]) { Py_DECREF((PyObject *)arr); sprintf(buf,"The extent of dimension %d is %d while %d was expected\n", i, arr->dimensions[i], expectdims[i]); PyErr_SetString(PyExc_ValueError, buf); return NULL; } } } return arr; } %} /**********************************************************/ /* OUTPUT */ /**********************************************************/ %{ static PyObject* l_output_helper2(PyObject* target, PyObject* o) { PyObject* o2; PyObject* o3; if (!target) { target = o; } else if (target == Py_None) { Py_DECREF(Py_None); target = o; } else { if (!PyList_Check(target)) { o2 = target; target = PyList_New(0); PyList_Append(target, o2); Py_XDECREF(o2); } PyList_Append(target,o); Py_XDECREF(o); } return target; } %} /**********************************************************/ /* OUTPUT: int VECTOR, ARRAY */ /**********************************************************/ %typemap(argout) int OUT_VECTOR[ANY], int OUT_ARRAY2D[ANY][ANY] { $result = l_output_helper2($result, (PyObject *)array$argnum); } %typemap(in, numinputs=0) int OUT_VECTOR[ANY] (PyArrayObject *array, npy_intp out_dims[1]) %{ out_dims[0] = $1_dim0; $1 = (int *)malloc($1_dim0*sizeof(int)); if ($1 == NULL) { PyErr_SetString(PyExc_ValueError, "failed to allocate memory"); return NULL; } array = (PyArrayObject *)PyArray_SimpleNewFromData (1, out_dims, PyArray_INT, (char *)$1); #ifdef _MSC_VER switch ( WinVerMajor() ) { case 6: break; // Vista default: array->flags |= NPY_OWNDATA; } #else // so we'll free this memory when this // array will be garbage collected array->flags |= NPY_OWNDATA; #endif %} %typemap(in, numinputs=0) int OUT_ARRAY2D[ANY][ANY] (PyArrayObject *array, npy_intp out_dims[2]) { int *data = (int *)malloc($1_dim0*$1_dim1*sizeof(int)); out_dims[0] = $1_dim0; out_dims[1] = $1_dim1; if (!data) { PyErr_SetString(PyExc_ValueError, "failed to allocate data array"); return NULL; } array = (PyArrayObject *)PyArray_SimpleNewFromData(2, out_dims, PyArray_INT, (char *)(data)); #ifdef _MSC_VER switch ( WinVerMajor() ) { case 6: break; // Vista default: array->flags |= NPY_OWNDATA; } #else // so we'll free this memory when this // array will be garbage collected array->flags |= NPY_OWNDATA; #endif $1 = (int (*)[$1_dim1])data; } /**********************************************************/ /* OUTPUT: float VECTOR, ARRAY */ /**********************************************************/ %typemap(argout) float OUT_VECTOR[ANY], float OUT_ARRAY2D[ANY][ANY] { $result = l_output_helper2($result, (PyObject *)array$argnum); } %typemap(in, numinputs=0) float OUT_VECTOR[ANY](PyArrayObject *array, npy_intp out_dims[1]) %{ out_dims[0] = $1_dim0; $1= (float *)malloc($1_dim0*sizeof(float)); if ($1 == NULL) { PyErr_SetString(PyExc_ValueError, "failed to allocate memory"); return NULL; } array = (PyArrayObject *)PyArray_SimpleNewFromData(1, out_dims, PyArray_FLOAT, (char *)($1)); #ifdef _MSC_VER switch ( WinVerMajor() ) { case 6: break; // Vista default: array->flags |= NPY_OWNDATA; } #else // so we'll free this memory when this // array will be garbage collected array->flags |= NPY_OWNDATA; #endif %} %typemap(in, numinputs=0) float OUT_ARRAY2D[ANY][ANY] (PyArrayObject *array, npy_intp out_dims[2]) { float *data = (float *)malloc($1_dim0*$1_dim1*sizeof(float)); out_dims[0] = $1_dim0; out_dims[1] = $1_dim1; if (!data) { PyErr_SetString(PyExc_ValueError, "failed to allocate data array"); return NULL; } array = (PyArrayObject *)PyArray_SimpleNewFromData(2, out_dims, PyArray_FLOAT, (char *)(data)); #ifdef _MSC_VER switch ( WinVerMajor() ) { case 6: break; // Vista default: array->flags |= NPY_OWNDATA; } #else // so we'll free this memory when this // array will be garbage collected array->flags |= NPY_OWNDATA; #endif $1 = (float (*)[$1_dim1])data; } /**********************************************************/ /* OUTPUT: double VECTOR, ARRAY */ /**********************************************************/ %typemap(argout) double OUT_VECTOR[ANY], double OUT_ARRAY2D[ANY][ANY] { $result = l_output_helper2($result, (PyObject *)array$argnum); } %typemap(in, numinputs=0) double OUT_VECTOR[ANY] (PyArrayObject *array, npy_intp out_dims[1]) { out_dims[0] = $1_dim0; $1= (double *)malloc($1_dim0*sizeof(double)); if ($1 == NULL) { PyErr_SetString(PyExc_ValueError, "failed to allocate memory"); return NULL; } array = (PyArrayObject *)PyArray_SimpleNewFromData(1, out_dims, PyArray_DOUBLE, (char *)($1)); #ifdef _MSC_VER switch ( WinVerMajor() ) { case 6: break; // Vista default: array->flags |= NPY_OWNDATA; } #else // so we'll free this memory when this // array will be garbage collected array->flags |= NPY_OWNDATA; #endif } %typemap(in, numinputs=0) double OUT_ARRAY2D[ANY][ANY] (PyArrayObject *array, npy_intp out_dims[2]) { double *data = (double *)malloc($1_dim0*$1_dim1*sizeof(double)); out_dims[0] = $1_dim0; out_dims[1] = $1_dim1; if (!data) { PyErr_SetString(PyExc_ValueError, "failed to allocate data array"); return NULL; } array = (PyArrayObject *)PyArray_SimpleNewFromData(2, out_dims, PyArray_DOUBLE, (char *)(data)); #ifdef _MSC_VER switch ( WinVerMajor() ) { case 6: break; // Vista default: array->flags |= NPY_OWNDATA; } #else // so we'll free this memory when this // array will be garbage collected array->flags |= NPY_OWNDATA; #endif $1 = (double (*)[$1_dim1])data; } /*************************************************************/ /* INPUT */ /*************************************************************/ /*************************************************************/ /* INPUT: u_char VECTOR */ /*************************************************************/ %typemap(in) u_char VECTOR[ANY] (PyArrayObject *array, int expected_dims[1]) %{ expected_dims[1] = $1_dim0; if (expected_dims[0]==1) expected_dims[0]=0; array = contiguous_typed_array($input, PyArray_UBYTE, 1, expected_dims); if (! array) return NULL; $1 = (u_char *)array->data; %} %typemap(freearg) u_char VECTOR[ANY] %{ if ( array$argnum ) Py_DECREF((PyObject *)array$argnum); %} /*************************************************************/ /* INPUT: u_char ARRAY2D */ /*************************************************************/ %typemap(in) u_char ARRAY2D[ANY][ANY](PyArrayObject *array, int expected_dims[2]) %{ if ($input != Py_None) { expected_dims[0] = $1_dim0; expected_dims[1] = $1_dim1; if (expected_dims[0]==1) expected_dims[0]=0; if (expected_dims[1]==1) expected_dims[1]=0; array = contiguous_typed_array($input, PyArray_UBYTE, 2, expected_dims); if (! array) return NULL; $1 = (u_char (*)[$1_dim1])array->data; } else { array = NULL; $1 = NULL; } %} %typemap(freearg) u_char ARRAY2D[ANY][ANY] %{ if ( array$argnum ) Py_DECREF((PyObject *)array$argnum); %} /*************************************************************/ /* INPUT: int VECTOR */ /*************************************************************/ %typemap(in) int VECTOR[ANY] (PyArrayObject *array, int expected_dims[1]) %{ expected_dims[0] = $1_dim0; if (expected_dims[0]==1) expected_dims[0]=0; array = contiguous_typed_array($input, PyArray_INT, 1, expected_dims); if (! array) return NULL; $1 = (int *)array->data; %} %typemap(freearg) int VECTOR[ANY] %{ if ( array$argnum ) Py_DECREF((PyObject *)array$argnum); %} /*************************************************************/ /* INPUT: int ARRAY2D */ /*************************************************************/ %typemap(in) int ARRAY2D[ANY][ANY](PyArrayObject *array, int expected_dims[2]) %{ if ($input != Py_None) { expected_dims[0] = $1_dim0; expected_dims[1] = $1_dim1; if (expected_dims[0]==1) expected_dims[0]=0; if (expected_dims[1]==1) expected_dims[1]=0; array = contiguous_typed_array($input, PyArray_INT, 2, expected_dims); if (! array) return NULL; $1 = (int (*)[$1_dim1])array->data; } else { array = NULL; $1 = NULL; } %} %typemap(freearg) int ARRAY2D[ANY][ANY] %{ if ( array$argnum ) Py_DECREF((PyObject *)array$argnum); %} /*************************************************************/ /* INPUT: float VECTOR */ /*************************************************************/ %typemap(in) float VECTOR[ANY] (PyArrayObject *array, int expected_dims[1]) %{ expected_dims[0] = $1_dim0; if (expected_dims[0]==1) expected_dims[0]=0; array = contiguous_typed_array($input, PyArray_FLOAT, 1, expected_dims); if (! array) return NULL; $1 = (float *)array->data; %} %typemap(freearg) float VECTOR[ANY] %{ if ( array$argnum ) Py_DECREF((PyObject *)array$argnum); %} /*************************************************************/ /* INPUT: float ARRAY2D */ /*************************************************************/ %typemap(in) float ARRAY2D[ANY][ANY](PyArrayObject *array, int expected_dims[2]) %{ if ($input != Py_None) { expected_dims[0] = $1_dim0; expected_dims[1] = $1_dim1; if (expected_dims[0]==1) expected_dims[0]=0; if (expected_dims[1]==1) expected_dims[1]=0; array = contiguous_typed_array($input, PyArray_FLOAT, 2, expected_dims); if (! array) return NULL; $1 = (float (*)[$1_dim1])array->data; } else { array = NULL; $1 = NULL; } %} %typemap(freearg) float ARRAY2D[ANY][ANY] %{ if ( array$argnum ) Py_DECREF((PyObject *)array$argnum); %} /*******************************************************/ /** Input: float ARRAY2D_NULL **/ /*******************************************************/ %typemap(in) float ARRAY2D_NULL[ANY][ANY] (PyArrayObject *array , int expected_dims[2]) { if ($input == Py_None) { $1 = NULL; } else { expected_dims[0] = $1_dim0; expected_dims[1] = $1_dim1; if (expected_dims[0]==1) expected_dims[0]=0; if (expected_dims[1]==1) expected_dims[1]=0; array = contiguous_typed_array($input, PyArray_FLOAT, 2, expected_dims); if (! array) return NULL; $1 = (float (*)[$1_dim1])array->data; } } /*************************************************************/ /* INPUT: double VECTOR */ /*************************************************************/ %typemap(in) double VECTOR[ANY] (PyArrayObject *array, int expected_dims[1]) %{ expected_dims[0] = $1_dim0; if (expected_dims[0]==1) expected_dims[0]=0; array = contiguous_typed_array($input, PyArray_DOUBLE, 1, expected_dims); if (! array) return NULL; $1 = (double *)array->data; %} %typemap(freearg) double VECTOR[ANY] %{ if ( array$argnum ) Py_DECREF((PyObject *)array$argnum); %} /*************************************************************/ /* INPUT: double ARRAY2D */ /*************************************************************/ %typemap(in) double ARRAY2D[ANY][ANY](PyArrayObject *array, int expected_dims[2]) %{ if ($input != Py_None) { expected_dims[0] = $1_dim0; expected_dims[1] = $1_dim1; if (expected_dims[0]==1) expected_dims[0]=0; if (expected_dims[1]==1) expected_dims[1]=0; array = contiguous_typed_array($input, PyArray_DOUBLE, 2, expected_dims); if (! array) return NULL; $1 = (double (*)[$1_dim1])array->data; } else { array = NULL; $1 = NULL; } %} %typemap(freearg) double ARRAY2D[ANY][ANY] %{ if ( array$argnum ) Py_DECREF((PyObject *)array$argnum); %} %define UCHAR_ARRAY2D( DIM, ARRAYNAME, ARRAYSHAPE) %typemap(in) ( int* DIM, u_char ARRAYNAME##ARRAYSHAPE) (PyArrayObject *array, int expected_dims[2], int intdims[2]) %{ if ($input != Py_None) { expected_dims[0] = $2_dim0; expected_dims[1] = $2_dim1; if (expected_dims[0]==1) expected_dims[0]=0; if (expected_dims[1]==1) expected_dims[1]=0; array = contiguous_typed_array($input, PyArray_UBYTE, 2, expected_dims); if (! array) return NULL; intdims[0] = ((PyArrayObject *)(array))->dimensions[0]; intdims[1] = ((PyArrayObject *)(array))->dimensions[1]; $1 = intdims; $2 = (u_char (*)[$2_dim1])array->data; } else { array = NULL; $2 = NULL; $1 = NULL; } %} %typemap(freearg) (int* DIM, u_char ARRAYNAME##ARRAYSHAPE) %{ %{ if ( array$argnum ) Py_DECREF((PyObject *)array$argnum); %} %enddef /**************************************************************/ /*** Macros to generate typemaps for pairs of arguments *******/ /**************************************************************/ /**************************************************************/ /* Input: FLOAT_ARRAY4D */ /**************************************************************/ %define FLOAT_ARRAY4D(DIM, ARRAYNAME, ARRAYSHAPE) %typemap(in) (int *DIM, float ARRAYNAME##ARRAYSHAPE)(PyArrayObject *array, int expected_dims[4], int intdims[4]) %{ if ($input != Py_None) { expected_dims[0] = $2_dim0; expected_dims[1] = $2_dim1; expected_dims[2] = $2_dim2; expected_dims[3] = $2_dim3; if (expected_dims[0]==1) expected_dims[0]=0; if (expected_dims[1]==1) expected_dims[1]=0; if (expected_dims[2]==1) expected_dims[2]=0; if (expected_dims[3]==1) expected_dims[3]=0; array = contiguous_typed_array($input, PyArray_FLOAT, 4, expected_dims); if (! array) return NULL; $2 = (float(*)[$2_dim1][$2_dim2][$2_dim3])array->data; intdims[0] = ((PyArrayObject *)(array))->dimensions[0]; intdims[1] = ((PyArrayObject *)(array))->dimensions[1]; intdims[2] = ((PyArrayObject *)(array))->dimensions[2]; intdims[3] = ((PyArrayObject *)(array))->dimensions[3]; $1 = intdims; } else { array = NULL; $2 = NULL; $1 = NULL; } %} %typemap(freearg) (int *DIM, float ARRAYNAME##ARRAYSHAPE) %{ if ( array$argnum ) Py_DECREF((PyObject *)array$argnum); %} %enddef /**************************************************************/ /* Input: SHORT_ARRAY4D */ /**************************************************************/ %define SHORT_ARRAY4D( DIM, ARRAYNAME, ARRAYSHAPE) %typemap(in) ( int *DIM, short ARRAYNAME##ARRAYSHAPE)(PyArrayObject *array, int expected_dims[4], int intdims[4]) %{ if ($input != Py_None) { expected_dims[0] = $2_dim0; expected_dims[1] = $2_dim1; expected_dims[2] = $2_dim2; expected_dims[3] = $2_dim3; if (expected_dims[0]==1) expected_dims[0]=0; if (expected_dims[1]==1) expected_dims[1]=0; if (expected_dims[2]==1) expected_dims[2]=0; if (expected_dims[3]==1) expected_dims[3]=0; array = contiguous_typed_array($input, PyArray_SHORT, 4, expected_dims); if (! array) return NULL; $2 = (short(*)[$2_dim1][$2_dim2][$2_dim3])array->data; intdims[0] = ((PyArrayObject *)(array))->dimensions[0]; intdims[1] = ((PyArrayObject *)(array))->dimensions[1]; intdims[2] = ((PyArrayObject *)(array))->dimensions[2]; intdims[3] = ((PyArrayObject *)(array))->dimensions[3]; $1 = intdims; } else { array = NULL; $2 = NULL; $1 = NULL; } %} %typemap(freearg) (int *DIM, short ARRAYNAME##ARRAYSHAPE) %{ if ( array$argnum ) Py_DECREF((PyObject *)array$argnum); %} %enddef /**************************************************************/ /* Input: UCHAR_ARRAY4D */ /**************************************************************/ %define UCHAR_ARRAY4D( DIM, ARRAYNAME, ARRAYSHAPE) %typemap(in) (int *DIM, u_char ARRAYNAME##ARRAYSHAPE)(PyArrayObject *array, int expected_dims[4], int intdims[4]) %{ if ($input != Py_None) { expected_dims[0] = $2_dim0; expected_dims[1] = $2_dim1; expected_dims[2] = $2_dim2; expected_dims[3] = $2_dim3; if (expected_dims[0]==1) expected_dims[0]=0; if (expected_dims[1]==1) expected_dims[1]=0; if (expected_dims[2]==1) expected_dims[2]=0; if (expected_dims[3]==1) expected_dims[3]=0; array = contiguous_typed_array($input, PyArray_UBYTE, 4, expected_dims); if (! array) return NULL; $2 = (u_char(*)[$2_dim1][$2_dim2][$2_dim3])array->data; intdims[0] = ((PyArrayObject *)(array))->dimensions[0]; intdims[1] = ((PyArrayObject *)(array))->dimensions[1]; intdims[2] = ((PyArrayObject *)(array))->dimensions[2]; intdims[3] = ((PyArrayObject *)(array))->dimensions[3]; $1 = intdims; } else { array = NULL; $2 = NULL; $1 = NULL; } %} %typemap(freearg) (int *DIM, u_char ARRAYNAME##ARRAYSHAPE) %{ if ( array$argnum ) Py_DECREF((PyObject *)array$argnum); %} %enddef %define SHORT_ARRAY5D(DIM, ARRAYNAME, ARRAYSHAPE) %typemap(in) (int *DIM, short ARRAYNAME##ARRAYSHAPE)(PyArrayObject *array, int expected_dims[5], int intdims[5]) %{ if ($input != Py_None) { expected_dims[0] = $2_dim0; expected_dims[1] = $2_dim1; expected_dims[2] = $2_dim2; expected_dims[3] = $2_dim3; expected_dims[4] = $2_dim4; if (expected_dims[0]==1) expected_dims[0]=0; if (expected_dims[1]==1) expected_dims[1]=0; if (expected_dims[2]==1) expected_dims[2]=0; if (expected_dims[3]==1) expected_dims[3]=0; if (expected_dims[4]==1) expected_dims[4]=0; array = contiguous_typed_array($input, PyArray_SHORT, 5, expected_dims); if (! array) return NULL; $2 = (short(*)[$2_dim1][$2_dim2][$2_dim3][$2_dim4])array->data; intdims[0] = ((PyArrayObject *)(array))->dimensions[0]; intdims[1] = ((PyArrayObject *)(array))->dimensions[1]; intdims[2] = ((PyArrayObject *)(array))->dimensions[2]; intdims[3] = ((PyArrayObject *)(array))->dimensions[3]; intdims[4] = ((PyArrayObject *)(array))->dimensions[4]; $1 = intdims; } else { array = NULL; $2 = NULL; $1 = NULL; } %} %typemap(freearg) (int *DIM, short ARRAYNAME##ARRAYSHAPE) %{ if ( array$argnum ) Py_DECREF((PyObject *)array$argnum); %} %enddef /**************************************************************/ /* Input: UCHAR_ARRAY5D */ /**************************************************************/ %define UCHAR_ARRAY5D(DIM, ARRAYNAME, ARRAYSHAPE) %typemap(in) (int *DIM, u_char ARRAYNAME##ARRAYSHAPE)(PyArrayObject *array, int expected_dims[5], int intdims[5]) %{ if ($input != Py_None) { expected_dims[0] = $2_dim0; expected_dims[1] = $2_dim1; expected_dims[2] = $2_dim2; expected_dims[3] = $2_dim3; expected_dims[4] = $2_dim4; if (expected_dims[0]==1) expected_dims[0]=0; if (expected_dims[1]==1) expected_dims[1]=0; if (expected_dims[2]==1) expected_dims[2]=0; if (expected_dims[3]==1) expected_dims[3]=0; if (expected_dims[4]==1) expected_dims[4]=0; array = contiguous_typed_array($input, PyArray_UBYTE, 5, expected_dims); if (! array) return NULL; $2 = (u_char(*)[$2_dim1][$2_dim2][$2_dim3][$2_dim4])array->data; intdims[0] = ((PyArrayObject *)(array))->dimensions[0]; intdims[1] = ((PyArrayObject *)(array))->dimensions[1]; intdims[2] = ((PyArrayObject *)(array))->dimensions[2]; intdims[3] = ((PyArrayObject *)(array))->dimensions[3]; intdims[4] = ((PyArrayObject *)(array))->dimensions[4]; $1 = intdims; } else { array = NULL; $2 = NULL; $1 = NULL; } %} %typemap(freearg) (int *DIM, u_char ARRAYNAME##ARRAYSHAPE) %{ if ( array$argnum ) Py_DECREF((PyObject *)array$argnum); %} %enddef /**************************************************************/ /* Input: FLOAT_ARRAY5D */ /**************************************************************/ %define FLOAT_ARRAY5D( DIM, ARRAYNAME, ARRAYSHAPE) %typemap(in) ( int* DIM, float ARRAYNAME##ARRAYSHAPE)(PyArrayObject *array, int expected_dims[5], int intdims[5]) %{ if ($input != Py_None) { expected_dims[0] = $2_dim0; expected_dims[1] = $2_dim1; expected_dims[2] = $2_dim2; expected_dims[3] = $2_dim3; expected_dims[4] = $2_dim4; if (expected_dims[0]==1) expected_dims[0]=0; if (expected_dims[1]==1) expected_dims[1]=0; if (expected_dims[2]==1) expected_dims[2]=0; if (expected_dims[3]==1) expected_dims[3]=0; if (expected_dims[4]==1) expected_dims[4]=0; array = contiguous_typed_array($input, PyArray_FLOAT, 5, expected_dims); if (! array) return NULL; $2 = (float(*)[$2_dim1][$2_dim2][$2_dim3][$2_dim4])array->data; intdims[0] = ((PyArrayObject *)(array))->dimensions[0]; intdims[1] = ((PyArrayObject *)(array))->dimensions[1]; intdims[2] = ((PyArrayObject *)(array))->dimensions[2]; intdims[3] = ((PyArrayObject *)(array))->dimensions[3]; intdims[4] = ((PyArrayObject *)(array))->dimensions[4]; $1 = intdims; } else { array = NULL; $2 = NULL; $1 = NULL; } %} %typemap(freearg) (int* DIM, float ARRAYNAME##ARRAYSHAPE) %{ if ( array$argnum ) Py_DECREF((PyObject *)array$argnum); %} %enddef /**************************************************************/ /* Input: INT_VECTOR */ /**************************************************************/ %define INT_VECTOR( ARRAYNAME, ARRAYSHAPE, LENGTH ) %typemap(in) (int ARRAYNAME##ARRAYSHAPE, int LENGTH) (PyArrayObject *array=NULL, int expected_dims[1]) %{ if ($input != Py_None) { expected_dims[0] = $1_dim0; if (expected_dims[0]==1) expected_dims[0]=0; array = contiguous_typed_array($input, PyArray_INT, 1, expected_dims); if (! array) return NULL; $1 = (int *)array->data; $2 = ((PyArrayObject *)(array))->dimensions[0]; } else { array = NULL; $1 = NULL; $2 = 0; } %} %typemap(freearg) (int ARRAYNAME##ARRAYSHAPE, int LENGTH) %{ if (array$argnum) Py_DECREF((PyObject *)array$argnum); %} %enddef /**************************************************************/ /* Input: FLOAT_VECTOR */ /**************************************************************/ %define FLOAT_VECTOR( ARRAYNAME, ARRAYSHAPE, LENGTH ) %typemap(in) (float ARRAYNAME##ARRAYSHAPE, int LENGTH) (PyArrayObject *array=NULL, int expected_dims[1]) %{ if ($input != Py_None) { expected_dims[0] = $1_dim0; if (expected_dims[0]==1) expected_dims[0]=0; array = contiguous_typed_array($input, PyArray_FLOAT, 1, expected_dims); if (! array) return NULL; $1 = (float *)array->data; $2 = ((PyArrayObject *)(array))->dimensions[0]; } else { array = NULL; $1 = NULL; $2 = 0; } %} %typemap(freearg) (float ARRAYNAME##ARRAYSHAPE, int LENGTH) %{ if (array$argnum) Py_DECREF((PyObject *)array$argnum); %} %enddef /**************************************************************/ /* Input: INT_ARRAY2D */ /**************************************************************/ %define INT_ARRAY2D( ARRAYNAME, ARRAYSHAPE, DIMENSIONS ) %typemap(in) ( int ARRAYNAME##ARRAYSHAPE, int* DIMENSIONS)(PyArrayObject *array, int expected_dims[2], int intdims[2]) %{ if ($input != Py_None) { expected_dims[0] = $1_dim0; expected_dims[1] = $1_dim1; if (expected_dims[0]==1) expected_dims[0]=0; if (expected_dims[1]==1) expected_dims[1]=0; array = contiguous_typed_array($input, PyArray_INT, 2, expected_dims); if (! array) return NULL; $1 = (int (*)[$1_dim1])array->data; intdims[0] = ((PyArrayObject *)(array))->dimensions[0]; intdims[1] = ((PyArrayObject *)(array))->dimensions[1]; $2 = intdims; } else { array = NULL; $1 = NULL; $2 = 0; } %} %typemap(freearg) (int ARRAYNAME##ARRAYSHAPE, int* DIMENSIONS) %{ if (array$argnum) Py_DECREF((PyObject *)array$argnum); %} %enddef /**************************************************************/ /* Input: FLOAT_ARRAY2D */ /**************************************************************/ %define FLOAT_ARRAY2D( ARRAYNAME, ARRAYSHAPE, DIMENSIONS ) %typemap(in) ( float ARRAYNAME##ARRAYSHAPE, int* DIMENSIONS)(PyArrayObject *array=NULL, int expected_dims[2], int intdims[2]) %{ if ($input != Py_None) { expected_dims[0] = $1_dim0; expected_dims[1] = $1_dim1; if (expected_dims[0]==1) expected_dims[0]=0; if (expected_dims[1]==1) expected_dims[1]=0; array = contiguous_typed_array($input, PyArray_FLOAT, 2, expected_dims); if (! array) return NULL; $1 = (float (*)[$1_dim1])array->data; intdims[0] = ((PyArrayObject *)(array))->dimensions[0]; intdims[1] = ((PyArrayObject *)(array))->dimensions[1]; $2 = intdims; } else { array = NULL; $1 = NULL; $2 = 0; } %} %typemap(freearg) (float ARRAYNAME##ARRAYSHAPE, int* DIMENSIONS ) %{ if (array$argnum ) Py_DECREF((PyObject *)array$argnum); %} %enddef bhtree-1.5.7~rc1+cvs.20140424/bhtree/Tests/0000755000175000017500000000000012326212377017527 5ustar moellermoellerbhtree-1.5.7~rc1+cvs.20140424/bhtree/Tests/__init__.py0000644000175000017500000000000110064373261021624 0ustar moellermoeller bhtree-1.5.7~rc1+cvs.20140424/bhtree/Tests/crn_crd.py0000644000175000017500000002530010064373261021510 0ustar moellermoellerpts = [ (17.047000, 14.099000, 3.625000), (16.967000, 12.784000, 4.338000), (15.685000, 12.755000, 5.133000), (15.268000, 13.825000, 5.594000), (18.170000, 12.703000, 5.337000), (19.334000, 12.829000, 4.463000), (18.150000, 11.546000, 6.304000), (15.115000, 11.555000, 5.265000), (13.856000, 11.469000, 6.066000), (14.164000, 10.785000, 7.379000), (14.993000, 9.862000, 7.443000), (12.732000, 10.711000, 5.261000), (13.308000, 9.439000, 4.926000), (12.484000, 11.442000, 3.895000), (13.488000, 11.241000, 8.417000), (13.660000, 10.707000, 9.787000), (12.269000, 10.431000, 10.323000), (11.393000, 11.308000, 10.185000), (14.368000, 11.748000, 10.691000), (15.885000, 12.426000, 10.016000), (12.019000, 9.272000, 10.928000), (10.646000, 8.991000, 11.408000), (10.654000, 8.793000, 12.919000), (11.659000, 8.296000, 13.491000), (10.057000, 7.752000, 10.682000), (9.837000, 8.018000, 8.904000), (9.561000, 9.108000, 13.563000), (9.448000, 9.034000, 15.012000), (9.288000, 7.670000, 15.606000), (9.490000, 7.519000, 16.819000), (8.230000, 9.957000, 15.345000), (7.338000, 9.786000, 14.114000), (8.366000, 9.804000, 12.958000), (8.875000, 6.686000, 14.796000), (8.673000, 5.314000, 15.279000), (8.753000, 4.376000, 14.083000), (8.726000, 4.858000, 12.923000), (7.340000, 5.121000, 15.996000), (6.274000, 5.220000, 15.031000), (8.881000, 3.075000, 14.358000), (8.912000, 2.083000, 13.258000), (7.581000, 2.090000, 12.506000), (7.670000, 2.031000, 11.245000), (9.207000, 0.677000, 13.924000), (10.714000, 0.702000, 14.312000), (8.811000, -0.477000, 12.969000), (11.185000, -0.516000, 15.142000), (6.458000, 2.162000, 13.159000), (5.145000, 2.209000, 12.453000), (5.115000, 3.379000, 11.461000), (4.664000, 3.268000, 10.343000), (3.995000, 2.354000, 13.478000), (2.716000, 2.891000, 12.869000), (3.758000, 1.032000, 14.208000), (5.606000, 4.546000, 11.941000), (5.598000, 5.767000, 11.082000), (6.441000, 5.527000, 9.850000), (6.052000, 5.933000, 8.744000), (6.022000, 6.977000, 11.891000), (7.647000, 4.909000, 10.005000), (8.496000, 4.609000, 8.837000), (7.798000, 3.609000, 7.876000), (7.878000, 3.778000, 6.651000), (9.847000, 4.020000, 9.305000), (10.752000, 3.607000, 8.149000), (11.226000, 4.699000, 7.244000), (12.143000, 5.571000, 8.035000), (12.758000, 6.609000, 7.443000), (12.539000, 6.932000, 6.158000), (13.601000, 7.322000, 8.202000), (7.186000, 2.582000, 8.445000), (6.500000, 1.584000, 7.565000), (5.382000, 2.313000, 6.773000), (5.213000, 2.016000, 5.557000), (5.908000, 0.462000, 8.400000), (6.990000, -0.272000, 9.012000), (4.648000, 3.182000, 7.446000), (3.545000, 3.935000, 6.751000), (4.107000, 4.851000, 5.691000), (3.536000, 5.001000, 4.617000), (2.663000, 4.677000, 7.748000), (1.802000, 3.735000, 8.610000), (1.567000, 2.613000, 8.165000), (1.394000, 4.252000, 9.767000), (5.259000, 5.498000, 6.005000), (5.929000, 6.358000, 5.055000), (6.304000, 5.578000, 3.799000), (6.136000, 6.072000, 2.653000), (7.183000, 6.994000, 5.754000), (7.884000, 8.006000, 4.883000), (8.906000, 7.586000, 4.027000), (7.532000, 9.373000, 4.983000), (9.560000, 8.539000, 3.194000), (8.176000, 10.281000, 4.145000), (9.141000, 9.845000, 3.292000), (6.900000, 4.390000, 3.989000), (7.331000, 3.607000, 2.791000), (6.116000, 3.210000, 1.915000), (6.240000, 3.144000, 0.684000), (8.145000, 2.404000, 3.240000), (9.555000, 2.856000, 3.730000), (10.013000, 3.895000, 3.323000), (10.120000, 1.956000, 4.539000), (4.993000, 2.927000, 2.571000), (3.782000, 2.599000, 1.742000), (3.296000, 3.871000, 1.004000), (2.947000, 3.817000, -0.189000), (2.698000, 1.953000, 2.608000), (1.384000, 1.826000, 1.806000), (3.174000, 0.533000, 3.005000), (3.321000, 4.987000, 1.720000), (2.890000, 6.285000, 1.126000), (3.687000, 6.597000, -0.111000), (3.200000, 7.147000, -1.103000), (3.039000, 7.369000, 2.240000), (2.559000, 9.014000, 1.649000), (4.997000, 6.227000, -0.100000), (5.895000, 6.489000, -1.213000), (5.738000, 5.560000, -2.409000), (6.228000, 5.901000, -3.507000), (7.370000, 6.507000, -0.731000), (7.717000, 7.687000, 0.206000), (7.949000, 8.947000, -0.615000), (9.212000, 8.856000, -1.337000), (9.537000, 9.533000, -2.431000), (8.659000, 10.350000, -3.032000), (10.793000, 9.491000, -2.899000), (5.051000, 4.411000, -2.204000), (4.933000, 3.431000, -3.326000), (4.397000, 4.014000, -4.620000), (4.988000, 3.755000, -5.687000), (4.196000, 2.184000, -2.863000), (4.960000, 1.178000, -1.991000), (3.907000, 0.097000, -1.634000), (6.129000, 0.606000, -2.768000), (3.329000, 4.795000, -4.543000), (2.792000, 5.376000, -5.797000), (3.573000, 6.540000, -6.322000), (3.260000, 7.045000, -7.422000), (1.358000, 5.766000, -5.472000), (1.223000, 5.694000, -3.993000), (2.421000, 4.941000, -3.408000), (4.565000, 7.047000, -5.559000), (5.366000, 8.191000, -6.018000), (5.007000, 9.481000, -5.280000), (5.535000, 10.510000, -5.730000), (4.181000, 9.438000, -4.262000), (3.767000, 10.609000, -3.513000), (5.017000, 11.397000, -3.042000), (5.947000, 10.757000, -2.523000), (2.992000, 10.188000, -2.225000), (2.051000, 9.144000, -2.623000), (2.260000, 11.349000, -1.551000), (4.971000, 12.703000, -3.176000), (6.143000, 13.513000, -2.696000), (6.400000, 13.233000, -1.225000), (5.485000, 13.061000, -0.382000), (5.703000, 14.969000, -2.920000), (4.676000, 14.893000, -3.996000), (3.964000, 13.567000, -3.811000), (7.728000, 13.297000, -0.921000), (8.114000, 13.103000, 0.500000), (7.427000, 14.073000, 1.410000), (7.036000, 13.682000, 2.540000), (9.648000, 13.285000, 0.660000), (10.440000, 12.093000, 0.063000), (11.941000, 12.170000, 0.391000), (12.416000, 13.225000, 0.681000), (12.539000, 11.070000, 0.292000), (7.212000, 15.334000, 0.966000), (6.614000, 16.317000, 1.913000), (5.212000, 15.936000, 2.350000), (4.782000, 16.166000, 3.495000), (6.605000, 17.695000, 1.246000), (4.445000, 15.318000, 1.405000), (3.074000, 14.894000, 1.756000), (3.085000, 13.643000, 2.645000), (2.315000, 13.523000, 3.578000), (2.204000, 14.637000, 0.462000), (1.815000, 16.048000, -0.129000), (0.903000, 13.864000, 0.811000), (0.756000, 16.761000, 0.757000), (4.032000, 12.764000, 2.313000), (4.180000, 11.549000, 3.187000), (4.632000, 11.944000, 4.596000), (4.227000, 11.252000, 5.547000), (5.038000, 10.518000, 2.539000), (4.349000, 9.794000, 1.022000), (5.408000, 13.012000, 4.694000), (5.879000, 13.502000, 6.026000), (4.696000, 13.908000, 6.882000), (4.528000, 13.422000, 8.025000), (6.880000, 14.615000, 5.830000), (3.827000, 14.802000, 6.358000), (2.691000, 15.221000, 7.194000), (1.672000, 14.132000, 7.434000), (0.947000, 14.112000, 8.468000), (1.986000, 16.520000, 6.614000), (1.664000, 16.221000, 5.230000), (2.914000, 17.739000, 6.700000), (1.621000, 13.190000, 6.511000), (0.715000, 12.045000, 6.657000), (1.125000, 11.125000, 7.815000), (0.286000, 10.632000, 8.545000), (0.755000, 11.229000, 5.322000), (-0.203000, 10.044000, 5.354000), (-1.547000, 10.337000, 5.645000), (0.193000, 8.750000, 5.100000), (-2.496000, 9.329000, 5.673000), (-0.801000, 7.705000, 5.156000), (-2.079000, 8.031000, 5.430000), (-3.097000, 7.057000, 5.458000), (2.470000, 10.984000, 7.995000), (2.986000, 9.994000, 8.950000), (3.609000, 10.505000, 10.230000), (3.766000, 9.715000, 11.186000), (4.076000, 9.103000, 8.225000), (5.125000, 10.027000, 7.824000), (3.493000, 8.324000, 7.035000), (3.984000, 11.764000, 10.241000), (4.769000, 12.336000, 11.360000), (6.255000, 12.243000, 11.106000), (7.037000, 12.750000, 11.954000), (6.710000, 11.631000, 9.992000), (8.140000, 11.694000, 9.635000), (8.500000, 13.141000, 9.206000), (7.581000, 13.949000, 8.944000), (8.504000, 10.686000, 8.530000), (8.048000, 8.987000, 8.881000), (9.793000, 13.410000, 9.173000), (10.280000, 14.760000, 8.823000), (11.346000, 14.658000, 7.743000), (11.971000, 13.583000, 7.552000), (10.790000, 15.535000, 10.085000), (12.059000, 14.803000, 10.671000), (9.684000, 15.686000, 11.138000), (12.733000, 15.676000, 11.781000), (11.490000, 15.773000, 7.038000), (12.552000, 15.877000, 6.036000), (13.590000, 16.917000, 6.560000), (13.168000, 18.006000, 6.945000), (11.987000, 16.360000, 4.681000), (10.914000, 15.338000, 4.163000), (13.131000, 16.517000, 3.629000), (10.151000, 16.024000, 2.938000), (14.856000, 16.493000, 6.536000), (15.930000, 17.454000, 6.941000), (16.913000, 17.550000, 5.819000), (17.097000, 16.660000, 4.970000), (16.622000, 16.995000, 8.285000), (17.360000, 15.651000, 8.067000), (15.592000, 16.974000, 9.434000), (18.298000, 15.206000, 9.219000), (17.664000, 18.669000, 5.806000), (18.635000, 18.861000, 4.738000), (19.925000, 18.042000, 4.949000), (20.593000, 17.742000, 3.945000), (18.945000, 20.364000, 4.783000), (18.238000, 20.937000, 5.908000), (17.371000, 19.900000, 6.596000), (20.172000, 17.730000, 6.217000), (21.452000, 16.969000, 6.513000), (21.143000, 15.478000, 6.427000), (20.138000, 15.023000, 5.878000), (22.055000, 14.701000, 7.032000), (22.019000, 13.242000, 7.020000), (21.944000, 12.628000, 8.396000), (21.869000, 11.387000, 8.435000), (23.246000, 12.697000, 6.275000), (21.894000, 13.435000, 9.436000), (21.936000, 12.911000, 10.809000), (20.615000, 13.191000, 11.521000), (20.357000, 14.317000, 11.948000), (23.131000, 13.601000, 11.593000), (24.284000, 13.401000, 10.709000), (23.340000, 12.935000, 12.962000), (19.827000, 12.110000, 11.642000), (18.504000, 12.312000, 12.298000), (18.684000, 12.451000, 13.784000), (19.533000, 11.718000, 14.362000), (17.582000, 11.117000, 11.996000), (17.199000, 10.929000, 10.237000), (17.880000, 13.266000, 14.426000), (17.924000, 13.421000, 15.877000), (17.392000, 12.206000, 16.594000), (16.652000, 11.368000, 16.033000), (17.076000, 14.658000, 16.145000), (16.098000, 14.689000, 14.997000), (16.859000, 14.150000, 13.779000), (17.728000, 12.124000, 17.884000), (17.334000, 10.956000, 18.691000), (15.875000, 10.688000, 18.871000), (15.434000, 9.550000, 19.166000), (15.036000, 11.747000, 18.715000), (13.564000, 11.573000, 18.836000), (12.936000, 11.227000, 17.470000), (11.720000, 11.040000, 17.428000), (12.933000, 12.737000, 19.580000), (13.140000, 14.094000, 18.958000), (14.109000, 14.303000, 18.212000), (12.267000, 14.963000, 19.265000), (13.725000, 11.174000, 16.425000), (13.257000, 10.745000, 15.081000), (14.275000, 9.687000, 14.612000), (14.930000, 9.862000, 13.568000), (13.200000, 11.914000, 14.071000), (12.000000, 12.819000, 14.399000), (12.119000, 13.853000, 15.332000), (10.775000, 12.617000, 13.762000), (11.045000, 14.675000, 15.610000), (9.676000, 13.433000, 14.048000), (9.802000, 14.456000, 14.996000), (8.740000, 15.265000, 15.269000), (14.342000, 8.640000, 15.422000), (15.445000, 7.667000, 15.246000), (15.171000, 6.533000, 14.280000), (16.093000, 5.705000, 14.039000), (15.680000, 7.099000, 16.682000), (13.966000, 6.502000, 13.739000), (13.512000, 5.395000, 12.878000), (13.311000, 5.853000, 11.455000), (13.733000, 6.929000, 11.026000), (12.266000, 4.769000, 13.501000), (12.538000, 4.304000, 14.922000), (11.982000, 4.849000, 15.886000), (13.407000, 3.298000, 15.015000), (12.703000, 4.973000, 10.746000), ] bhtree-1.5.7~rc1+cvs.20140424/bhtree/Tests/crn_crd_and_rad.py0000644000175000017500000003472010064373261023166 0ustar moellermoeller# x,y,z and bondOrderRadius for 1crn.pdb pts = [ (17.047000, 14.099000, 3.625000, 0.676000), (16.967000, 12.784000, 4.338000, 0.762000), (15.685000, 12.755000, 5.133000, 0.762000), (15.268000, 13.825000, 5.594000, 0.640000), (18.170000, 12.703000, 5.337000, 0.762000), (19.334000, 12.829000, 4.463000, 0.640000), (18.150000, 11.546000, 6.304000, 0.762000), (15.115000, 11.555000, 5.265000, 0.676000), (13.856000, 11.469000, 6.066000, 0.762000), (14.164000, 10.785000, 7.379000, 0.762000), (14.993000, 9.862000, 7.443000 , 0.640000), (12.732000, 10.711000, 5.261000, 0.762000), (13.308000, 9.439000, 4.926000 , 0.640000), (12.484000, 11.442000, 3.895000, 0.762000), (13.488000, 11.241000, 8.417000, 0.676000), (13.660000, 10.707000, 9.787000, 0.762000), (12.269000, 10.431000, 10.323000, 0.762000), (11.393000, 11.308000, 10.185000, 0.640000), (14.368000, 11.748000, 10.691000, 0.762000), (15.885000, 12.426000, 10.016000, 1.053000), (12.019000, 9.272000, 10.928000, 0.676000), (10.646000, 8.991000, 11.408000, 0.762000), (10.654000, 8.793000, 12.919000, 0.762000), (11.659000, 8.296000, 13.491000, 0.640000), (10.057000, 7.752000, 10.682000, 0.762000), (9.837000, 8.018000, 8.904000 , 1.053000), (9.561000, 9.108000, 13.563000 , 0.676000), (9.448000, 9.034000, 15.012000 , 0.762000), (9.288000, 7.670000, 15.606000 , 0.762000), (9.490000, 7.519000, 16.819000 , 0.640000), (8.230000, 9.957000, 15.345000 , 0.762000), (7.338000, 9.786000, 14.114000 , 0.762000), (8.366000, 9.804000, 12.958000 , 0.762000), (8.875000, 6.686000, 14.796000 , 0.676000), (8.673000, 5.314000, 15.279000 , 0.762000), (8.753000, 4.376000, 14.083000 , 0.762000), (8.726000, 4.858000, 12.923000 , 0.640000), (7.340000, 5.121000, 15.996000 , 0.762000), (6.274000, 5.220000, 15.031000 , 0.640000), (8.881000, 3.075000, 14.358000 , 0.676000), (8.912000, 2.083000, 13.258000 , 0.762000), (7.581000, 2.090000, 12.506000 , 0.762000), (7.670000, 2.031000, 11.245000 , 0.640000), (9.207000, 0.677000, 13.924000 , 0.762000), (10.714000, 0.702000, 14.312000, 0.762000), (8.811000, -0.477000, 12.969000, 0.762000), (11.185000, -0.516000, 15.142000, 0.762000), (6.458000, 2.162000, 13.159000 , 0.676000), (5.145000, 2.209000, 12.453000 , 0.762000), (5.115000, 3.379000, 11.461000 , 0.762000), (4.664000, 3.268000, 10.343000 , 0.640000), (3.995000, 2.354000, 13.478000 , 0.762000), (2.716000, 2.891000, 12.869000 , 0.762000), (3.758000, 1.032000, 14.208000 , 0.762000), (5.606000, 4.546000, 11.941000 , 0.676000), (5.598000, 5.767000, 11.082000 , 0.762000), (6.441000, 5.527000, 9.850000 , 0.762000), (6.052000, 5.933000, 8.744000 , 0.640000), (6.022000, 6.977000, 11.891000 , 0.762000), (7.647000, 4.909000, 10.005000 , 0.676000), (8.496000, 4.609000, 8.837000 , 0.762000), (7.798000, 3.609000, 7.876000 , 0.762000), (7.878000, 3.778000, 6.651000 , 0.640000), (9.847000, 4.020000, 9.305000 , 0.762000), (10.752000, 3.607000, 8.149000 , 0.762000), (11.226000, 4.699000, 7.244000 , 0.762000), (12.143000, 5.571000, 8.035000 , 0.676000), (12.758000, 6.609000, 7.443000 , 0.762000), (12.539000, 6.932000, 6.158000 , 0.676000), (13.601000, 7.322000, 8.202000 , 0.676000), (7.186000, 2.582000, 8.445000 , 0.676000), (6.500000, 1.584000, 7.565000 , 0.762000), (5.382000, 2.313000, 6.773000 , 0.762000), (5.213000, 2.016000, 5.557000 , 0.640000), (5.908000, 0.462000, 8.400000 , 0.762000), (6.990000, -0.272000, 9.012000 , 0.640000), (4.648000, 3.182000, 7.446000 , 0.676000), (3.545000, 3.935000, 6.751000 , 0.762000), (4.107000, 4.851000, 5.691000 , 0.762000), (3.536000, 5.001000, 4.617000 , 0.640000), (2.663000, 4.677000, 7.748000 , 0.762000), (1.802000, 3.735000, 8.610000 , 0.762000), (1.567000, 2.613000, 8.165000 , 0.640000), (1.394000, 4.252000, 9.767000 , 0.676000), (5.259000, 5.498000, 6.005000 , 0.676000), (5.929000, 6.358000, 5.055000 , 0.762000), (6.304000, 5.578000, 3.799000 , 0.762000), (6.136000, 6.072000, 2.653000 , 0.640000), (7.183000, 6.994000, 5.754000 , 0.762000), (7.884000, 8.006000, 4.883000 , 0.762000), (8.906000, 7.586000, 4.027000 , 0.762000), (7.532000, 9.373000, 4.983000 , 0.762000), (9.560000, 8.539000, 3.194000 , 0.762000), (8.176000, 10.281000, 4.145000 , 0.762000), (9.141000, 9.845000, 3.292000 , 0.762000), (6.900000, 4.390000, 3.989000 , 0.676000), (7.331000, 3.607000, 2.791000 , 0.762000), (6.116000, 3.210000, 1.915000 , 0.762000), (6.240000, 3.144000, 0.684000 , 0.640000), (8.145000, 2.404000, 3.240000 , 0.762000), (9.555000, 2.856000, 3.730000 , 0.762000), (10.013000, 3.895000, 3.323000 , 0.640000), (10.120000, 1.956000, 4.539000 , 0.676000), (4.993000, 2.927000, 2.571000 , 0.676000), (3.782000, 2.599000, 1.742000 , 0.762000), (3.296000, 3.871000, 1.004000 , 0.762000), (2.947000, 3.817000, -0.189000 , 0.640000), (2.698000, 1.953000, 2.608000 , 0.762000), (1.384000, 1.826000, 1.806000 , 0.762000), (3.174000, 0.533000, 3.005000 , 0.762000), (3.321000, 4.987000, 1.720000 , 0.676000), (2.890000, 6.285000, 1.126000 , 0.762000), (3.687000, 6.597000, -0.111000 , 0.762000), (3.200000, 7.147000, -1.103000 , 0.640000), (3.039000, 7.369000, 2.240000 , 0.762000), (2.559000, 9.014000, 1.649000 , 1.053000), (4.997000, 6.227000, -0.100000 , 0.676000), (5.895000, 6.489000, -1.213000 , 0.762000), (5.738000, 5.560000, -2.409000 , 0.762000), (6.228000, 5.901000, -3.507000 , 0.640000), (7.370000, 6.507000, -0.731000 , 0.762000), (7.717000, 7.687000, 0.206000 , 0.762000), (7.949000, 8.947000, -0.615000 , 0.762000), (9.212000, 8.856000, -1.337000 , 0.676000), (9.537000, 9.533000, -2.431000 , 0.762000), (8.659000, 10.350000, -3.032000, 0.676000), (10.793000, 9.491000, -2.899000, 0.676000), (5.051000, 4.411000, -2.204000 , 0.676000), (4.933000, 3.431000, -3.326000 , 0.762000), (4.397000, 4.014000, -4.620000 , 0.762000), (4.988000, 3.755000, -5.687000 , 0.640000), (4.196000, 2.184000, -2.863000 , 0.762000), (4.960000, 1.178000, -1.991000 , 0.762000), (3.907000, 0.097000, -1.634000 , 0.762000), (6.129000, 0.606000, -2.768000 , 0.762000), (3.329000, 4.795000, -4.543000 , 0.676000), (2.792000, 5.376000, -5.797000 , 0.762000), (3.573000, 6.540000, -6.322000 , 0.762000), (3.260000, 7.045000, -7.422000 , 0.640000), (1.358000, 5.766000, -5.472000 , 0.762000), (1.223000, 5.694000, -3.993000 , 0.762000), (2.421000, 4.941000, -3.408000 , 0.762000), (4.565000, 7.047000, -5.559000 , 0.676000), (5.366000, 8.191000, -6.018000 , 0.762000), (5.007000, 9.481000, -5.280000 , 0.762000), (5.535000, 10.510000, -5.730000, 0.640000), (4.181000, 9.438000, -4.262000 , 0.676000), (3.767000, 10.609000, -3.513000, 0.762000), (5.017000, 11.397000, -3.042000, 0.762000), (5.947000, 10.757000, -2.523000, 0.640000), (2.992000, 10.188000, -2.225000, 0.762000), (2.051000, 9.144000, -2.623000 , 0.640000), (2.260000, 11.349000, -1.551000, 0.762000), (4.971000, 12.703000, -3.176000, 0.676000), (6.143000, 13.513000, -2.696000, 0.762000), (6.400000, 13.233000, -1.225000, 0.762000), (5.485000, 13.061000, -0.382000, 0.640000), (5.703000, 14.969000, -2.920000, 0.762000), (4.676000, 14.893000, -3.996000, 0.762000), (3.964000, 13.567000, -3.811000, 0.762000), (7.728000, 13.297000, -0.921000, 0.676000), (8.114000, 13.103000, 0.500000 , 0.762000), (7.427000, 14.073000, 1.410000 , 0.762000), (7.036000, 13.682000, 2.540000 , 0.640000), (9.648000, 13.285000, 0.660000 , 0.762000), (10.440000, 12.093000, 0.063000, 0.762000), (11.941000, 12.170000, 0.391000, 0.762000), (12.416000, 13.225000, 0.681000, 0.640000), (12.539000, 11.070000, 0.292000, 0.640000), (7.212000, 15.334000, 0.966000 , 0.676000), (6.614000, 16.317000, 1.913000 , 0.762000), (5.212000, 15.936000, 2.350000 , 0.762000), (4.782000, 16.166000, 3.495000 , 0.640000), (6.605000, 17.695000, 1.246000 , 0.762000), (4.445000, 15.318000, 1.405000 , 0.676000), (3.074000, 14.894000, 1.756000 , 0.762000), (3.085000, 13.643000, 2.645000 , 0.762000), (2.315000, 13.523000, 3.578000 , 0.640000), (2.204000, 14.637000, 0.462000 , 0.762000), (1.815000, 16.048000, -0.129000, 0.762000), (0.903000, 13.864000, 0.811000 , 0.762000), (0.756000, 16.761000, 0.757000 , 0.762000), (4.032000, 12.764000, 2.313000 , 0.676000), (4.180000, 11.549000, 3.187000 , 0.762000), (4.632000, 11.944000, 4.596000 , 0.762000), (4.227000, 11.252000, 5.547000 , 0.640000), (5.038000, 10.518000, 2.539000 , 0.762000), (4.349000, 9.794000, 1.022000 , 1.053000), (5.408000, 13.012000, 4.694000 , 0.676000), (5.879000, 13.502000, 6.026000 , 0.762000), (4.696000, 13.908000, 6.882000 , 0.762000), (4.528000, 13.422000, 8.025000 , 0.640000), (6.880000, 14.615000, 5.830000 , 0.762000), (3.827000, 14.802000, 6.358000 , 0.676000), (2.691000, 15.221000, 7.194000 , 0.762000), (1.672000, 14.132000, 7.434000 , 0.762000), (0.947000, 14.112000, 8.468000 , 0.640000), (1.986000, 16.520000, 6.614000 , 0.762000), (1.664000, 16.221000, 5.230000 , 0.640000), (2.914000, 17.739000, 6.700000 , 0.762000), (1.621000, 13.190000, 6.511000 , 0.676000), (0.715000, 12.045000, 6.657000 , 0.762000), (1.125000, 11.125000, 7.815000 , 0.762000), (0.286000, 10.632000, 8.545000 , 0.640000), (0.755000, 11.229000, 5.322000 , 0.762000), (-0.203000, 10.044000, 5.354000, 0.762000), (-1.547000, 10.337000, 5.645000, 0.762000), (0.193000, 8.750000, 5.100000 , 0.762000), (-2.496000, 9.329000, 5.673000 , 0.762000), (-0.801000, 7.705000, 5.156000 , 0.762000), (-2.079000, 8.031000, 5.430000 , 0.762000), (-3.097000, 7.057000, 5.458000 , 0.640000), (2.470000, 10.984000, 7.995000 , 0.676000), (2.986000, 9.994000, 8.950000 , 0.762000), (3.609000, 10.505000, 10.230000, 0.762000), (3.766000, 9.715000, 11.186000 , 0.640000), (4.076000, 9.103000, 8.225000 , 0.762000), (5.125000, 10.027000, 7.824000 , 0.640000), (3.493000, 8.324000, 7.035000 , 0.762000), (3.984000, 11.764000, 10.241000, 0.676000), (4.769000, 12.336000, 11.360000, 0.762000), (6.255000, 12.243000, 11.106000, 0.762000), (7.037000, 12.750000, 11.954000, 0.640000), (6.710000, 11.631000, 9.992000 , 0.676000), (8.140000, 11.694000, 9.635000 , 0.762000), (8.500000, 13.141000, 9.206000 , 0.762000), (7.581000, 13.949000, 8.944000 , 0.640000), (8.504000, 10.686000, 8.530000 , 0.762000), (8.048000, 8.987000, 8.881000 , 1.053000), (9.793000, 13.410000, 9.173000 , 0.676000), (10.280000, 14.760000, 8.823000, 0.762000), (11.346000, 14.658000, 7.743000, 0.762000), (11.971000, 13.583000, 7.552000, 0.640000), (10.790000, 15.535000, 10.085000, 0.762000), (12.059000, 14.803000, 10.671000, 0.762000), (9.684000, 15.686000, 11.138000, 0.762000), (12.733000, 15.676000, 11.781000, 0.762000), (11.490000, 15.773000, 7.038000, 0.676000), (12.552000, 15.877000, 6.036000, 0.762000), (13.590000, 16.917000, 6.560000, 0.762000), (13.168000, 18.006000, 6.945000, 0.640000), (11.987000, 16.360000, 4.681000, 0.762000), (10.914000, 15.338000, 4.163000, 0.762000), (13.131000, 16.517000, 3.629000, 0.762000), (10.151000, 16.024000, 2.938000, 0.762000), (14.856000, 16.493000, 6.536000, 0.676000), (15.930000, 17.454000, 6.941000, 0.762000), (16.913000, 17.550000, 5.819000, 0.762000), (17.097000, 16.660000, 4.970000, 0.640000), (16.622000, 16.995000, 8.285000, 0.762000), (17.360000, 15.651000, 8.067000, 0.762000), (15.592000, 16.974000, 9.434000, 0.762000), (18.298000, 15.206000, 9.219000, 0.762000), (17.664000, 18.669000, 5.806000, 0.676000), (18.635000, 18.861000, 4.738000, 0.762000), (19.925000, 18.042000, 4.949000, 0.762000), (20.593000, 17.742000, 3.945000, 0.640000), (18.945000, 20.364000, 4.783000, 0.762000), (18.238000, 20.937000, 5.908000, 0.762000), (17.371000, 19.900000, 6.596000, 0.762000), (20.172000, 17.730000, 6.217000, 0.676000), (21.452000, 16.969000, 6.513000, 0.762000), (21.143000, 15.478000, 6.427000, 0.762000), (20.138000, 15.023000, 5.878000, 0.640000), (22.055000, 14.701000, 7.032000, 0.676000), (22.019000, 13.242000, 7.020000, 0.762000), (21.944000, 12.628000, 8.396000, 0.762000), (21.869000, 11.387000, 8.435000, 0.640000), (23.246000, 12.697000, 6.275000, 0.762000), (21.894000, 13.435000, 9.436000, 0.676000), (21.936000, 12.911000, 10.809000, 0.762000), (20.615000, 13.191000, 11.521000, 0.762000), (20.357000, 14.317000, 11.948000, 0.640000), (23.131000, 13.601000, 11.593000, 0.7620000), (24.284000, 13.401000, 10.709000, 0.6400000), (23.340000, 12.935000, 12.962000, 0.7620000), (19.827000, 12.110000, 11.642000, 0.6760000), (18.504000, 12.312000, 12.298000, 0.7620000), (18.684000, 12.451000, 13.784000, 0.7620000), (19.533000, 11.718000, 14.362000, 0.6400000), (17.582000, 11.117000, 11.996000, 0.7620000), (17.199000, 10.929000, 10.237000, 1.0530000), (17.880000, 13.266000, 14.426000, 0.6760000), (17.924000, 13.421000, 15.877000, 0.7620000), (17.392000, 12.206000, 16.594000, 0.7620000), (16.652000, 11.368000, 16.033000, 0.6400000), (17.076000, 14.658000, 16.145000, 0.7620000), (16.098000, 14.689000, 14.997000, 0.7620000), (16.859000, 14.150000, 13.779000, 0.7620000), (17.728000, 12.124000, 17.884000, 0.6760000), (17.334000, 10.956000, 18.691000, 0.7620000), (15.875000, 10.688000, 18.871000, 0.7620000), (15.434000, 9.550000, 19.166000, 0.640000), (15.036000, 11.747000, 18.715000, 0.6760000), (13.564000, 11.573000, 18.836000, 0.7620000), (12.936000, 11.227000, 17.470000, 0.7620000), (11.720000, 11.040000, 17.428000, 0.6400000), (12.933000, 12.737000, 19.580000, 0.7620000), (13.140000, 14.094000, 18.958000, 0.7620000), (14.109000, 14.303000, 18.212000, 0.6400000), (12.267000, 14.963000, 19.265000, 0.6400000), (13.725000, 11.174000, 16.425000, 0.6760000), (13.257000, 10.745000, 15.081000, 0.7620000), (14.275000, 9.687000, 14.612000, 0.762000), (14.930000, 9.862000, 13.568000, 0.640000), (13.200000, 11.914000, 14.071000, 0.7620000), (12.000000, 12.819000, 14.399000, 0.7620000), (12.119000, 13.853000, 15.332000, 0.7620000), (10.775000, 12.617000, 13.762000, 0.7620000), (11.045000, 14.675000, 15.610000, 0.7620000), (9.676000, 13.433000, 14.048000, 0.762000), (9.802000, 14.456000, 14.996000, 0.762000), (8.740000, 15.265000, 15.269000, 0.640000), (14.342000, 8.640000, 15.422000, 0.676000), (15.445000, 7.667000, 15.246000, 0.762000), (15.171000, 6.533000, 14.280000, 0.762000), (16.093000, 5.705000, 14.039000, 0.640000), (15.680000, 7.099000, 16.682000, 0.762000), (13.966000, 6.502000, 13.739000, 0.676000), (13.512000, 5.395000, 12.878000, 0.762000), (13.311000, 5.853000, 11.455000, 0.762000), (13.733000, 6.929000, 11.026000, 0.640000), (12.266000, 4.769000, 13.501000, 0.762000), (12.538000, 4.304000, 14.922000, 0.762000), (11.982000, 4.849000, 15.886000, 0.640000), (13.407000, 3.298000, 15.015000, 0.676000), (12.703000, 4.973000, 10.746000, 0.640000), ] bhtree-1.5.7~rc1+cvs.20140424/bhtree/Tests/cv_crd_and_rad.py0000644000175000017500000000514210064373261023010 0ustar moellermoeller# x,y,z and bondOrderRadius for cv.pdb pts = [ (0.601000, -1.594000, -0.306000, 0.676000), (-0.339000, -1.512000, -1.485000, 0.762000), (-0.759000, -0.072000, -1.891000, 0.762000), (0.513000, 0.787000, -2.119000, 0.762000), (1.489000, 0.746000, -0.907000, 0.762000), (2.782000, 1.566000, -1.187000, 0.762000), (3.854000, 1.494000, -0.069000, 0.762000), (4.885000, 2.443000, -0.013000, 0.762000), (5.866000, 2.353000, 0.972000, 0.762000), (5.834000, 1.320000, 1.901000, 0.762000), (4.820000, 0.369000, 1.854000, 0.762000), (3.831000, 0.440000, 0.870000, 0.762000), (2.689000, -0.609000, 0.878000, 0.762000), (1.840000, -0.720000, -0.439000, 0.762000), (4.964000, 3.475000, -0.916000, 0.640000), (-1.672000, 0.597000, -0.912000, 0.676000), (-3.162000, 0.169000, -0.819000, 1.053000), (-3.261000, -1.094000, -0.408000, 0.640000), (-3.727000, 0.205000, -2.026000, 0.640000), (-3.960000, 1.089000, 0.162000, 0.676000), (-4.589000, 0.000000, 2.337000, 0.762000), (-3.626000, 0.955000, 1.623000, 0.762000), (-4.169000, 2.509000, -0.278000, 0.762000), (-5.547000, 3.062000, 0.117000, 0.762000), (0.805000, -3.010000, 0.163000, 0.762000), (-0.334000, -3.935000, -0.316000, 0.762000), (-0.149000, -5.389000, 0.134000, 0.762000), (-5.635000, 0.355000, 2.282000, 0.352000), (-4.559000, -1.020000, 1.911000, 0.352000), (-4.332000, -0.090000, 3.408000, 0.352000), (-3.651000, 1.931000, 2.144000, 0.352000), (-2.588000, 0.593000, 1.743000, 0.352000), (-3.365000, 3.160000, 0.115000, 0.352000), (-4.079000, 2.579000, -1.377000, 0.352000), (-5.681000, 3.095000, 1.214000, 0.352000), (-5.682000, 4.093000, -0.257000, 0.352000), (-6.369000, 2.453000, -0.302000, 0.352000), (1.783000, -3.397000, -0.185000, 0.352000), (0.870000, -3.073000, 1.262000, 0.352000), (-1.305000, -3.563000, 0.061000, 0.352000), (-0.410000, -3.911000, -1.420000, 0.352000), (-0.978000, -6.025000, -0.226000, 0.352000), (0.790000, -5.823000, -0.258000, 0.352000), (-0.123000, -5.477000, 1.236000, 0.352000), (0.146000, -1.992000, -2.358000, 0.352000), (-1.237000, -2.132000, -1.306000, 0.352000), (-1.294000, -0.132000, -2.857000, 0.352000), (1.036000, 0.416000, -3.022000, 0.352000), (0.238000, 1.832000, -2.359000, 0.352000), (0.969000, 1.250000, -0.067000, 0.352000), (3.245000, 1.216000, -2.130000, 0.352000), (2.502000, 2.620000, -1.374000, 0.352000), (6.660000, 3.085000, 1.017000, 0.352000), (6.597000, 1.257000, 2.664000, 0.352000), (3.130000, -1.594000, 1.119000, 0.352000), (2.018000, -0.380000, 1.727000, 0.352000), (2.487000, -1.176000, -1.215000, 0.352000), (4.808000, -0.421000, 2.592000, 0.352000), (-1.198000, 0.611000, -0.002000, 0.352000), (5.728000, 4.017000, -0.706000, 0.352000), ] bhtree-1.5.7~rc1+cvs.20140424/bhtree/Tests/test.py0000644000175000017500000002345611346540354021072 0ustar moellermoeller## Automatically adapted for numpy.oldnumeric Jul 30, 2007 by import sys from mglutil.regression import testplus import numpy.oldnumeric as Numeric points = None def setUpSuite(): from bhtree import bhtreelib print "bhtreelib imported from: ", bhtreelib.__file__ def test_buildTreeForSpheres(): print "#### test_buildTreeForSpheres ###" # 1 - build a fixed size BHtree for a set of spheres (points with radii) # radii come into play when calling closePointsPairs # 2 - retrieve points within 10.0 from (0.0, 0.0, 0.0) from bhtree import bhtreelib # load a bunch of 3D points from crn_crd_and_rad import pts # build a BHTree for 3D points (xyz), radii # granularity=10 # granularity specifies the maximum number of points per leaf node rad = map(lambda x: x[3], pts) xyz = map(lambda x: x[:3], pts) bht = bhtreelib.BHtree( xyz, rad, 10) # allocate an array for results of query # the array has to be long enough to hold the indices of all points # found. result = Numeric.zeros( (len(pts),) ).astype('i') # find all points in pts within 10.0 deom (0.,0.,0.) # nb will be the number of points, result[:nb] will hold the indices nb = bht.closePoints(( 0.0, 0.0, 0.0 ), 10.0, result ) assert nb == 53 # delete the tree del bht def test_badPoints(): # 1 - build a fixed size BHtree for a set of spheres (points with radii) # radii come into play when calling closePointsPairs # 2 - retrieve points within 10.0 from (0.0, 0.0, 0.0) print "#### test_badPoints ####" from bhtree import bhtreelib from crn_crd_and_rad import pts rad = map(lambda x: x[3], pts) # pass an Nx4 array for points .. this has to raise a ValueError try: bht = bhtreelib.BHtree( pts, rad, 10) assert 0, "failed to raise ValueError for bad first argument" except ValueError: pass def test_badNumberOfRadii(): print "#### test_badNumberOfRadii ####" from bhtree import bhtreelib from crn_crd_and_rad import pts rad = map(lambda x: x[3], pts) # pass a number of radii that does not match the number of points try: bht = bhtreelib.BHtree( pts, rad[:10], 10) assert 0, "failed to raise ValueError for bad set of radii" except ValueError: pass def test_noRadii(): print "#### test_noRadii ####" from bhtree import bhtreelib from crn_crd_and_rad import pts xyz = map(lambda x: x[:3], pts) bht = bhtreelib.BHtree( xyz, None, 10) # delete the tree del bht def test_findAllCloseWithRadii(): print "#### test_findAllCloseWithRadii ####" from bhtree import bhtreelib # load a bunch of 3D points from crn_crd_and_rad import pts # build a BHTree for the points, their ids # granularity=10 # granularity specifies the maximum number of points per leaf node rad = map(lambda x: x[3], pts) xyz = map(lambda x: x[:3], pts) bht = bhtreelib.BHtree( xyz, rad, 10) # find all pairs of atoms for which the distance is less than 1.1 # times the sum of the radii pairs = bht.closePointsPairsInTree(1.1) assert len(pairs)==337 # delete the tree del bht def test_findAllCloseNoRadii(): print "#### test_findAllCloseNoRadii ####" from bhtree import bhtreelib # load a bunch of 3D points from crn_crd_and_rad import pts # build a BHTree for the points, their ids # granularity=10 # granularity specifies the maximum number of points per leaf node xyz = map(lambda x: x[:3], pts) bht = bhtreelib.BHtree( xyz, None, 10) # find all pairs of atoms for which the distance is less than 1.1 # times the sum of the radii pairs = bht.closePointsPairsInTree(1.1) assert len(pairs)==0 # delete the tree del bht def test_findClosePairsWithSecondSetOfPoints(): print "#### test_findClosePairsWithSecondSetOfPoints ####" # no radii, we should find no pairs from bhtree import bhtreelib # load a bunch of 3D points from crn_crd_and_rad import pts pts1 = pts from cv_crd_and_rad import pts pts2 = pts # build a BHTree for the points, their ids # granularity=10 # granularity specifies the maximum number of points per leaf node xyz = map(lambda x: x[:3], pts1) bht = bhtreelib.BHtree( xyz, None, 10) # find all pairs of atoms (pi,pj) for which the distance is less than 1.1 # times the sum of the radii. Pi belongs to pts2 ant pj is a point from # the bhtree xyz = map(lambda x: x[:3], pts2) pairs = bht.closePointsPairs(xyz, None, 1.0) assert len(pairs)==0 # delete the tree del bht def test_findClosePairsWithSecondSetOfSpheres(): print "#### test_findClosePairsWithSecondSetOfSpheres ####" # we use radii, we should find pairs that are close from bhtree import bhtreelib # load a bunch of 3D points from crn_crd_and_rad import pts pts1 = pts from cv_crd_and_rad import pts pts2 = pts # build a BHTree for the points, their ids # granularity=10 # granularity specifies the maximum number of points per leaf node xyz = map(lambda x: x[:3], pts1) rad = map(lambda x: x[3], pts1) bht = bhtreelib.BHtree( xyz, rad, 10) # find all pairs of atoms (pi,pj) for which the distance is less than 1.1 # times the sum of the radii. Pi belongs to pts2 ant pj is a point from # the bhtree xyz = map(lambda x: x[:3], pts2) rad = map(lambda x: x[3], pts2) pairs = bht.closePointsPairs(xyz, rad, 1.1) assert len(pairs)==4 # delete the tree del bht def test_closestPointsArray(): print "#### test_closestPointsArray ####" from bhtree import bhtreelib # load a bunch of 3D points from crn_crd_and_rad import pts pts1 = map(lambda x: x[:3], pts) from cv_crd_and_rad import pts pts2 = map(lambda x: x[:3], pts) # build a BHTree for pts1 bht = bhtreelib.BHtree( pts1, None, 10) points = bht.closestPointsArray(pts2, 10) check_points = [133, 133, 108, 133, 133, 133, 133, 98, 98, 97, 109, 109, 109, 133, 127, 108, 108, 108, 108, 108, 108, 108, 108, 211, 133, 133, 133, 108, 108, 108, 108, 108, 108, 108, 211, 211, 108, 133, 109, 108, 133, 133, 133, 109, 133, 133, 133, 133, 141, 108, 133, 106, 98, 99, 109, 109, 133, 109, 108, 127] assert points.tolist() == check_points def test_closestPointsArrayDist2(): print "#### test_closestPointsArrayDist2 ####" from bhtree import bhtreelib # load a bunch of 3D points from crn_crd_and_rad import pts pts1 = map(lambda x: x[:3], pts) from cv_crd_and_rad import pts pts2 = map(lambda x: x[:3], pts) # build a BHTree for pts1 bht = bhtreelib.BHtree( pts1, None, 10) points, dist = bht.closestPointsArrayDist2(pts2, 10) check_points = [133, 133, 108, 133, 133, 133, 133, 98, 98, 97, 109, 109, 109, 133, 127, 108, 108, 108, 108, 108, 108, 108, 108, 211, 133, 133, 133, 108, 108, 108, 108, 108, 108, 108, 211, 211, 108, 133, 109, 108, 133, 133, 133, 109, 133, 133, 133, 133, 141, 108, 133, 106, 98, 99, 109, 109, 133, 109, 108, 127] assert points.tolist() == check_points #print "DISTANSES:" #print dist assert dist.shape == (len(points), ) def test_tbhtree_ClosePoints(): print "#### test_tbhtree_ClosePoints####" # 1 - build a TBHtree (points can be added and moved, NOT deleted) # 2 - retrieve points within 10.0 from (0.0, 0.0, 0.0) from bhtree import bhtreelib # load a bunch of 3D points from crn_crd import pts # create an array of IDs for these points ids = Numeric.arrayrange(len(pts)).astype('i') # build a TBHTree for the points, their ids # granularity=10 # granularity specifies the maximum number of points per leaf node # LeafPadding=10 # Padding added to the array of points in each leaf; # The number of new points that can be added to each box # through the move and insert commands # SpacePadding=9999.0 # SpacePadding is the amount of void "padding" space stored around # the tree to allow inserts and moves outside of the original # boundaries of the tree # bht = bhtreelib.TBHTree( pts, ids, 10, 10, 9999.0 ) # allocate an array for results of query # the array has to be long enough to hold the indices of all points # found. result = Numeric.zeros( (len(pts),) ).astype('i') # find all points in pts within 10.0 deom (0.,0.,0.) # nb will be the number of points, result[:nb] will hold the indices nb = bht.ClosePoints(( 0.0, 0.0, 0.0 ), 10.0, result ) assert nb == 53 # delete the tree del bht def test_refCount(): """ tests a function that takes Numeric array arguments. Checks that there is no memory leak by counting reference of the Numeric array objects. """ print "#### test_refCount ####" from bhtree import bhtreelib from crn_crd_and_rad import pts xyz = map(lambda x: x[:3], pts) bht = bhtreelib.BHtree( xyz, None, 10) i = 0 while i<10: indices = Numeric.zeros( (len(xyz),) ).astype('i') dist = Numeric.zeros( (len(xyz),) ).astype('f') nb = bht.closePointsDist2((0.0, 0.0, 0.0), 10.0, indices, dist) i += 1 assert sys.getrefcount(indices) == 2 assert sys.getrefcount(dist) == 2 # delete the tree del bht if __name__ == '__main__': testplus.chdir() harness = testplus.TestHarness( __name__, connect = (setUpSuite, (), {}), funs = testplus.testcollect( globals()), ) print harness sys.exit( len( harness)) bhtree-1.5.7~rc1+cvs.20140424/bhtree/Tests/test_dependencies.py0000644000175000017500000000151410406104562023560 0ustar moellermoeller# ################################################################# # Author: Sowjanya Karnati ################################################################# # #Purpose:To update dependencies list # # $Id: test_dependencies.py,v 1.2 2006/03/15 21:38:58 sowjanya Exp $ from mglutil.TestUtil.Tests.dependenciestest import DependencyTester import unittest d = DependencyTester() result_expected =[] class test_dep(unittest.TestCase): def test_dep_1(self): result = d.rundeptester('bhtree') if result !=[]: print "\nThe Following Packages are not present in CRITICAL or NONCRITICAL DEPENDENCIES of bhtree :\n %s" %result self.assertEqual(result,result_expected) else: self.assertEqual(result,result_expected) if __name__ == '__main__': unittest.main() bhtree-1.5.7~rc1+cvs.20140424/src/0000755000175000017500000000000012326212400015726 5ustar moellermoellerbhtree-1.5.7~rc1+cvs.20140424/src/bhtree.c0000644000175000017500000014005011364157100017351 0ustar moellermoeller#include #include #include #include #include "bhtree.h" /************************************************************************ original BHTree (fixed size, fixed points locations) ************************************************************************/ static int findBHcloseAtomsInNode(BHnode *node,float *x,float cutoff, int *atom,int maxn); static int findBHcloseAtomsInNodedist(BHnode *node,float *x,float cutoff, int *atom,float *dist,int maxn); /*-------------------- generateBHtree --------------------*/ BHtree *generateBHtree(BHpoint **atoms,int nbat,int granularity) { /* 3D space-sort atoms into Barnes-Hut tree with given granularity */ /* (max number of atoms per leaf-node) */ BHtree *r; BHpoint **p; int i,k; /* allocate tree data structure */ r=(BHtree *)malloc(sizeof(BHtree)); if (r==NULL) return(r); r->atom=(BHpoint **)NULL; r->bfl=0; r->rm=0.0; for (i=0;irmr) r->rm = atoms[i]->r; r->rm += 0.1; #ifdef STATBHTREE r->tot=0; /* total number of neighbors returned by findBHclose */ r->max=0; r->min=9999999; /* min and max of these numbers */ r->nbr=0; /* number of calls to findBHclose */ #endif r->nbp = nbat; /* allocate root node data structure */ r->root=(BHnode *)malloc(sizeof(BHnode)); if (r->root==NULL) { freeBHtree(r); return((BHtree *)NULL); } /* initialize root node data structure */ r->root->atom=(BHpoint **)NULL; r->root->n=0; r->root->dim= -1; r->root->left=(BHnode *)NULL; r->root->right=(BHnode *)NULL; /* count atoms in chain */ if (nbat==0) { freeBHtree(r); return((BHtree *)NULL); } /* allocate atom pointer array */ r->atom=atoms; if (r->atom == NULL) { freeBHtree(r); return((BHtree *)NULL); } r->root->atom=r->atom; /* fill atom pointer array */ p=r->root->atom; r->root->n=nbat; /* determine box dimension */ p=r->root->atom; for (k=0;k<3;k++) { r->xmin[k]=p[0]->x[k]; r->xmax[k]=r->xmin[k]; } for (i=1;iroot->n;i++) { for (k=0;k<3;k++) { if (r->xmin[k] > p[i]->x[k]) r->xmin[k] = p[i]->x[k]; if (r->xmax[k] < p[i]->x[k]) r->xmax[k] = p[i]->x[k]; } } /* start recursive 3D space sort */ divideBHnode(r->root,r->xmin,r->xmax,granularity); /* build a lookup table that restores the order in which the points where given to build the bhtree */ r->nodeLookUp = (int *)malloc(r->root->n*sizeof(int)); if (!r->nodeLookUp) { fprintf(stderr, "Error: failed to malloc lookup table" ); return NULL; } for (i=0; iroot->n; i++) r->nodeLookUp[atoms[i]->at] = i; /* done... */ return(r); } /*-------------------- find_BHnode --------------------*/ BHnode *findBHnode(BHtree *tree,float *x) { /* find leaf in BH tree that contains 3D point x */ BHnode *r; int k; if (tree==NULL) return((BHnode *)NULL); /* first check if point is in tree */ for (k=0;k<3;k++) { if (x[k]xmin[k]) return((BHnode *)NULL); if (x[k]>tree->xmax[k]) return((BHnode *)NULL); } /* if point is in tree, locate the leaf */ r=tree->root; while (r!=NULL) { if (r->dim<0) break; if (x[r->dim]cut) r=r->left; else r=r->right; } return(r); } /*-------------------- freeBHtree --------------------*/ void freeBHtree(BHtree *tree) { int i; if (tree->atom!=NULL) { for (i=0;iroot->n;i++) free(tree->atom[i]); free(tree->atom); } free(tree->nodeLookUp); freeBHnode(tree->root); //A.O -commented out the following line - causes a Segmentation fault (???) //free(tree); } /*-------------------- freeBHnode --------------------*/ void freeBHnode(BHnode *node) { //printf ("freeBHnode \n"); if (node != NULL) { freeBHnode(node->left); freeBHnode(node->right); free(node); } } /*-------------------- divideBHtree --------------------*/ void divideBHnode(BHnode *node,float *xmin,float *xmax,int granularity) { float cut,dx,xminl[3],xmaxl[3],xminr[3],xmaxr[3]; int dim,i,j,k,n[NSTEPS],lm,rm; BHpoint *a; /* if nothing left to divide just return */ if (node==NULL) return; if (granularity<1 || node->n <= granularity) return; if (node->atom==NULL) return; /* determine dimension along which to cut */ dim=0; if (xmax[1]-xmin[1] > xmax[dim]-xmin[dim]) dim=1; if (xmax[2]-xmin[2] > xmax[dim]-xmin[dim]) dim=2; /* determine position of cutting plane */ dx=(xmax[dim]-xmin[dim])/NSTEPS; if (dx<0.0001) return; for (i=0;in;j++) { i=(node->atom[j]->x[dim]-xmin[dim])/dx; if (i>=0 && inode->n/2) break; } cut=xmin[dim]+i*dx; if (n[i]>=node->n) return; /* create left/right descendants */ node->left=(BHnode *)malloc(sizeof(BHnode)); if (node->left==NULL) return; node->left->dim= -1; node->left->left=(BHnode *)NULL; node->left->right=(BHnode *)NULL; node->right=(BHnode *)malloc(sizeof(BHnode)); if (node->right==NULL) { freeBHnode(node->left); return; } node->right->dim= -1; node->right->left=(BHnode *)NULL; node->right->right=(BHnode *)NULL; node->cut=cut; node->dim=dim; /* sort atoms into left/right descendants */ lm=0; rm=node->n-1; while (lmn;lm++) if (node->atom[lm]->x[dim]>=cut) break; for(;rm>=0;rm--) if (node->atom[rm]->x[dim]atom[rm]; node->atom[rm]=node->atom[lm]; node->atom[lm]=a; rm--; lm++; } } if (lm==rm) { if (node->atom[rm]->x[dim]left->n=rm+1; node->left->atom=node->atom; node->right->n=node->n-(rm+1); node->right->atom=node->atom+lm; /* if descendants are coarse, cut them up... */ if (node->left->n > granularity) { for (k=0;k<3;k++) { xminl[k]=xmin[k]; xmaxl[k]=xmax[k]; } xmaxl[dim]=cut; divideBHnode(node->left,xminl,xmaxl,granularity); } if (node->right->n > granularity) { for (k=0;k<3;k++) { xminr[k]=xmin[k]; xmaxr[k]=xmax[k]; } xminr[dim]=cut; divideBHnode(node->right,xminr,xmaxr,granularity); } /* done... */ return; } /*-------------------- findBHcloseAtoms --------------------*/ int findBHcloseAtomsdist(BHtree *tree, float *x, float cutoff, int *atom, float *dist, int maxn) { int i; if (maxn<1 || tree==NULL || cutoff <=0.0) return(0); if (tree->root==NULL) return(0); for (i=0;i<3;i++) { if (x[i] < tree->xmin[i] - cutoff) break; if (x[i] > tree->xmax[i] + cutoff) break; } if (i<3) return(0); return(findBHcloseAtomsInNodedist(tree->root,x,cutoff,atom,dist,maxn)); } /*-------------------- findBHcloseAtomsInNode --------------------*/ static int findBHcloseAtomsInNodedist(BHnode *node,float *x,float cutoff, int *atom,float *dist,int maxn) { int j,n; float d[3],D,C; if (node==NULL) return(0); if (maxn<=0) return(0); if (node->n < 1) return(0); if (node->dim<0) { n=0; C=cutoff*cutoff; for (j=0;jn;j++) { d[0]=x[0]-node->atom[j]->x[0]; if (d[0]>cutoff || d[0]< -cutoff) continue; d[1]=x[1]-node->atom[j]->x[1]; if (d[1]>cutoff || d[1]< -cutoff) continue; d[2]=x[2]-node->atom[j]->x[2]; if (d[2]>cutoff || d[2]< -cutoff) continue; D=d[0]*d[0]+d[1]*d[1]+d[2]*d[2]; if (D>C) continue; if (natom[j]->at; dist[n]=sqrt(D); n++; }else{ n++; break; } } }else{ n=0; if (x[node->dim]cut+cutoff) { n+=findBHcloseAtomsInNodedist(node->left,x,cutoff,atom,dist,maxn); } if (x[node->dim]>=node->cut-cutoff) { n+=findBHcloseAtomsInNodedist(node->right,x,cutoff,atom+n,dist+n,maxn-n); } } return(n); } /*-------------------- findBHcloseAtomsInNodedist2 --------------------*/ static int findBHcloseAtomsInNodedist2(BHnode *node, float *x, float cutoff, int *atom, float *dist, int maxn) { int j,n; float d[3],D,C; if (node==NULL) return(0); if (maxn<=0) return(0); if (node->n < 1) return(0); if (node->dim<0) { n=0; C=cutoff*cutoff; for (j=0;jn;j++) { d[0]=x[0]-node->atom[j]->x[0]; if (d[0]>cutoff || d[0]< -cutoff) continue; d[1]=x[1]-node->atom[j]->x[1]; if (d[1]>cutoff || d[1]< -cutoff) continue; d[2]=x[2]-node->atom[j]->x[2]; if (d[2]>cutoff || d[2]< -cutoff) continue; D=d[0]*d[0]+d[1]*d[1]+d[2]*d[2]; if (D>C) continue; if (natom[j]->at; dist[n]=D; n++; }else{ n++; break; } } }else{ n=0; if (x[node->dim]cut+cutoff) { n+=findBHcloseAtomsInNodedist2(node->left,x,cutoff,atom,dist,maxn); } if (x[node->dim]>=node->cut-cutoff) { n+=findBHcloseAtomsInNodedist2(node->right,x,cutoff,atom+n,dist+n,maxn-n); } } return(n); } /*-------------------- findBHcloseAtomsdist2 --------------------*/ int findBHcloseAtomsdist2(BHtree *tree, float *x, float cutoff, int *atom, float *dist, int maxn) { int i; if (maxn<1 || tree==NULL || cutoff <=0.0) return(0); if (tree->root==NULL) return(0); for (i=0;i<3;i++) { if (x[i] < tree->xmin[i] - cutoff) break; if (x[i] > tree->xmax[i] + cutoff) break; } if (i<3) return(0); return(findBHcloseAtomsInNodedist2(tree->root, x, cutoff, atom, dist, maxn)); } /*-------------------- findBHcloseAtoms --------------------*/ int findBHcloseAtoms(BHtree *tree,float *x,float cutoff, int *atom,int maxn) { int i,n; if (maxn<1 || tree==NULL || cutoff <=0.0) return(0); if (tree->root==NULL) return(0); for (i=0;i<3;i++) { if (x[i] < tree->xmin[i] - cutoff) break; if (x[i] > tree->xmax[i] + cutoff) break; } if (i<3) return(0); #ifdef STATBHTREE n = findBHcloseAtomsInNode(tree->root,x,cutoff,atom,maxn); tree->nbr++; tree->tot += n; if (n>tree->max) tree->max=n; if (nmin) tree->min=n; return(n); #else return(findBHcloseAtomsInNode(tree->root,x,cutoff,atom,maxn)); #endif } /*-------------------- findBHcloseAtomsInNode --------------------*/ static int findBHcloseAtomsInNode(BHnode *node,float *x,float cutoff, int *atom, int maxn) { int j,n; float C,D; double d1,d2,d3; BHpoint *p; /* if (node==NULL) return(0); if (maxn<=0) return(0); if (node->n < 1) return(0); */ if (node->dim<0) { C=cutoff*cutoff; for (n=j=0;jn;j++) { p = node->atom[j]; d1 = x[0] - p->x[0]; if (d1 > cutoff || d1 < -cutoff) continue; d2 = x[1] - p->x[1]; if (d2 > cutoff || d2 < -cutoff) continue; d3 = x[2] - p->x[2]; if (d3 > cutoff || d3 < -cutoff) continue; D=d1*d1 + d2*d2 + d3*d3; if (D>C) continue; if (nat; n++; }else{ printf("ERROR: findBHcloseAtomsInNode: result array too small\n"); break; } } } else { n=0; if (x[node->dim]cut+cutoff) { n+=findBHcloseAtomsInNode(node->left,x,cutoff,atom,maxn); } if (x[node->dim]>=node->cut-cutoff) { n+=findBHcloseAtomsInNode(node->right,x,cutoff,atom+n,maxn-n); } } return(n); } int *findClosePairsInTree(BHtree *bht, float tolerance) { BHpoint *bhpp1, *bhpp2; BHpoint **atoms; int *bondsp, *newbonds; int maxNbBonds=20000, newmaxNbBonds, nbb, nba; int cl[200], id, i, j, k; float cld[200], cut, r, lim2, dist2; bondsp = (int *)malloc((1+maxNbBonds)*sizeof(int)); if (bondsp==NULL) return NULL; atoms = bht->root->atom; /* loops over all points */ for (i=0, nbb=1; iroot->n; i++) { bhpp1 = atoms[i]; r = bhpp1->r; id = bhpp1->at; /* find all atoms within max cutoff of point */ cut = (r + bht->maxr)*tolerance; nba = findBHcloseAtomsdist2(bht, bhpp1->x, cut, cl, cld, 200); /* if (id==186) { printf("%d %d %f %f %f %f\n", id, nba, bhpp1->x[0], bhpp1->x[1], bhpp1->x[2], bhpp1->r); } */ /* select the points within strict cutoff */ for (j=0; jx[0], bhpp2->x[1], bhpp2->x[2], bhpp2->r); printf("%f %f\n", lim2*lim2, dist2); */ if (k<=id) continue; bhpp2 = atoms[bht->nodeLookUp[k]]; lim2 = (r + bhpp2->r)*tolerance; dist2 = cld[j]; if (dist2 < lim2*lim2) { /* atom k is bonded to atom id */ bondsp[nbb] = id; bondsp[nbb+1] = k; nbb+=2; if (nbb>maxNbBonds-1) { newmaxNbBonds = maxNbBonds + 20000; newbonds = (int *)malloc((1+newmaxNbBonds)*sizeof(int)); if (newbonds==NULL) { free(bondsp); return NULL; } memcpy(newbonds, bondsp, (1+maxNbBonds)*sizeof(int)); maxNbBonds = newmaxNbBonds; free(bondsp); bondsp = newbonds; } } } } bondsp[0] = nbb; return bondsp; } /* Retrieve pairs of points (pi,pj) such that their distance is less than tolerance*(sum of radii) and pi is a point in the tree and pj is a point in the set provided to the function. */ int *findClosePairs( BHtree *bht, float xyz[1][3], int *nbxyz, float rad[1], int nbrad, float tolerance ) { BHpoint *bhpp; BHpoint **atoms; int *bondsp, *newbonds; int maxNbBonds=20000, newmaxNbBonds, nbb, nba; int cl[200], i, j, k; float cld[200], maxr, r, cut, lim2, dist2; bondsp = (int *)malloc((1+maxNbBonds)*sizeof(int)); if (bondsp==NULL) return NULL; atoms = bht->root->atom; /* find largest radius */ maxr = bht->maxr; for (i=0; imaxr) maxr = rad[i]; /* loops over all given points */ for (i=0, nbb=1; inodeLookUp[k]]; lim2 = (r + bhpp->r)*tolerance; dist2 = cld[j]; if (dist2 < lim2*lim2) { /* atom k is close to atom i */ bondsp[nbb] = i; bondsp[nbb+1] = k; nbb+=2; if (nbb>maxNbBonds-1) { newmaxNbBonds = maxNbBonds + 20000; newbonds = (int *)malloc((1+newmaxNbBonds)*sizeof(int)); if (newbonds==NULL) { free(bondsp); return NULL; } memcpy(newbonds, bondsp, (1+maxNbBonds)*sizeof(int)); maxNbBonds = newmaxNbBonds; bondsp = newbonds; } } } } bondsp[0] = nbb; return bondsp; } /* For every point in a given set of 3D points - xyz[1][3]- find a closest atom within a given cutoff. An array of corresponding atom indices is returned, if not close point is found Null is returned if returnNullIfFail is true else -1 is put into the index list */ int *findClosestAtoms( BHtree *bht, float xyz[1][3], int *nbxyz, float cut, int returnNullIfFail ) { int *cl_inds; int *cl, closest, i, j, k, nba; float *cld, lim; cl_inds = (int *)malloc((nbxyz[0]+1)*sizeof(int)); if (cl_inds==NULL) { printf("Failed to allocate array cl_inds of %d integers \n", nbxyz[0]); return NULL; } cl = (int *)malloc(bht->nbp*sizeof(int)); if (cl==NULL) { printf("Failed to allocate array cl of %d integers \n", bht->nbp); return NULL; } cld = (float *)malloc(bht->nbp*sizeof(float)); if (cl==NULL) { printf("Failed to allocate array clf of %d float\n", bht->nbp); return NULL; } cl_inds[0] = nbxyz[0]; /* loops over all given points */ for (i=1; inbp); /* find the closest atom */ lim = 9999999.; closest = -1; for (j=0; j= 0){ if (closest > bht->root->n) { printf("ERROR %d %d %d %f %f %f %f\n", i,closest, nba, cut, xyz[i-1][0], xyz[i-1][1], xyz[i-1][2]); } cl_inds[i]=closest; /*cl_inds[i] = bht->nodeLookUp[closest];*/ /*printf ("i=%d, closest =%d, nba = %d \n", i-1, cl_inds[i], nba); */ } else if (returnNullIfFail) { free(cl_inds); printf ("No atoms found for point %d. Cutoff: %f \n", i-1, cut); return NULL; } else { cl_inds[i]=-1; } } free(cl); free(cld); return cl_inds; } /* For every point in a given set of 3D points - xyz[1][3]- find a closest atom within a given cutoff. An array of corresponding atom indices is returned. An array of corresponding distances from closest atoms is computed (and returned in Python wrapper). If not close point is found Null is returned if returnNullIfFail is true else -1 is put into the index list */ int *findClosestAtomsDist2( BHtree *bht, float xyz[1][3], int nbxyz, float *cl_dist, float cut, int returnNullIfFail ) { int *cl_inds; int *cl, closest, i, j, k, nba; float *cld, lim; cl_inds = (int *)malloc((nbxyz+1)*sizeof(int)); if (cl_inds==NULL) { printf("Failed to allocate array cl_inds of %d integers \n", nbxyz); return NULL; } cl = (int *)malloc(bht->nbp*sizeof(int)); if (cl==NULL) { printf("Failed to allocate array cl of %d integers \n",bht->nbp ); return NULL; } cld = (float *)malloc(bht->nbp*sizeof(float)); if (cl==NULL) { printf("Failed to allocate array clf of %d float\n", bht->nbp); return NULL; } cl_inds[0] = nbxyz; /* loops over all given points */ for (i=1; inbp); /* find the closest atom */ lim = 9999999.; closest = -1; for (j=0; j= 0){ if (closest > bht->root->n) { printf("ERROR %d %d %d %f %f %f %f\n", i,closest, nba, cut, xyz[i-1][0], xyz[i-1][1], xyz[i-1][2]); } cl_inds[i]=closest; cl_dist[i-1]=lim; /*printf ("dist %d, %f \n", i-1, lim); */ /*cl_inds[i] = bht->nodeLookUp[closest];*/ /*printf ("i=%d, closest =%d, nba = %d \n", i-1, cl_inds[i], nba); */ } else if (returnNullIfFail) { free(cl_inds); printf ("No atoms found for point %d. Cutoff: %f \n", i-1, cut); return NULL; } else { cl_inds[i]=-1; cl_dist[i-1]=-1; } } free(cl); free(cld); return cl_inds; } /************************************************************************ mutable bhtrees: (Chris Carrillo) TBHtree: points can be translated RTBHtree: points can be translated, added and deleted ************************************************************************/ static int RegenerateRBHTree(RBHTree *tree); TBHTree *GenerateTBHTree(TBHPoint *Pts, int NumPts, int Granularity, int LeafPadding, float SpacePadding) { TBHTree *r; int i, j; float xmin[3], xmax[3], sxmin[3], sxmax[3]; r = (TBHTree *)malloc((size_t)sizeof(TBHTree)); if(r == NULL) return(r); r->bfl = 0; r->rm=0.0; for (i=0;irmrm = Pts[i].Rad; r->rm += 0.1; #ifdef STATBHTREE r->tot = 0; r->max = 0; r->min = 9999999; r->nbr = 0; #endif r->Root=(TBHNode *)malloc((size_t)sizeof(TBHNode)); if (r->Root==NULL) { FreeTBHTree(r); return((TBHTree *)NULL); } r->Root->Index.NumPts = 0; r->Root->Index.Pts = (TBHPoint **)NULL; r->Root->Index.Size = 0; r->Root->Parent = (TBHNode *)NULL; r->Root->dim = -1; r->Root->Left = (TBHNode *)NULL; r->Root->Right = (TBHNode *)NULL; if(NumPts == 0) { FreeTBHTree(r); return((TBHTree *)NULL); } for(j = BH_X; j <= BH_Z; j++) { xmin[j] = Pts[0].Pos[j]; xmax[j] = xmin[j]; } for(i = 1; i < NumPts; i++) { for(j = BH_X; j <= BH_Z; j++) { if (xmin[j] > Pts[i].Pos[j]) xmin[j] = Pts[i].Pos[j]; if (xmax[j] < Pts[i].Pos[j]) xmax[j] = Pts[i].Pos[j]; } } r->Pts = Pts; r->NumPts = NumPts; r->Root->Index.Pts = (TBHPoint **)NULL; r->Root->Index.NumPts = NumPts; for(j = BH_X; j <= BH_Z; j++) { sxmin[j] = xmin[j] - SpacePadding; sxmax[j] = xmax[j] + SpacePadding; r->xmin[j] = sxmin[j]; r->xmax[j] = sxmax[j]; } r->Root->Buffer = (TBHPoint **)malloc(sizeof(TBHPoint *) * NumPts); if(r->Root->Buffer == NULL) return (TBHTree *)NULL; for(i = 0; i < NumPts; i++) { Pts[i].ID = i; r->Root->Buffer[i] = &Pts[i]; } DivideTBHNode(r->Root, xmin, xmax, sxmin, sxmax, Granularity, LeafPadding); if(r->Root->dim == -1 && r->Root->Index.Size == 0) { for(i = BH_X; i <= BH_Z; i++) { r->Root->xmin[i] = sxmin[i]; r->Root->xmax[i] = sxmax[i]; } r->Root->Index.Size = r->Root->Index.NumPts + LeafPadding; r->Root->Index.Pts = (TBHPoint **)malloc(sizeof(TBHPoint *) * r->Root->Index.Size); for(i = 0; i < r->Root->Index.NumPts; i++) { r->Root->Index.Pts[i] = r->Root->Buffer[i]; r->Root->Index.Pts[i]->Box = r->Root; } } return(r); } TBHNode *FindTBHNode(TBHTree *tree, float *x) { TBHNode *r; int k; if (tree == NULL) return((TBHNode *)NULL); for(k = 0; k < 3; k++) { if(x[k] < tree->xmin[k]) return((TBHNode *)NULL); if(x[k] > tree->xmax[k]) return((TBHNode *)NULL); } r = tree->Root; while(r != NULL) { if(r->dim < 0) break; if(x[r->dim] < r->cut) r = r->Left; else r = r->Right; } return(r); } TBHNode *FindTBHNodeUp(TBHNode *node, float x[3]) { TBHNode *r; int i, j, k; r = node; i = BH_NO; while(i == BH_NO) { r = r->Parent; if(r == NULL) return ((TBHNode *)NULL); k = BH_YES; for(j = BH_X; j <= BH_Z && k == BH_YES; j++) if(x[j] > r->xmax[j] || x[j] < r->xmin[j]) k = BH_NO; if(k == BH_YES) i = BH_YES; } while(r != NULL) { if(r->dim < 0) break; if(x[r->dim] < r->cut) r = r->Left; else r = r->Right; } return(r); } void FreeTBHTree(TBHTree *tree) { if(tree->Pts != NULL) free((void *)(tree->Pts)); free((void *)tree->Root->Buffer); FreeTBHNode(tree->Root); free((void *)tree); } void FreeTBHNode(TBHNode *node) { if(node != NULL) { if(node->Left != NULL) FreeTBHNode(node->Left); if(node->Right != NULL) FreeTBHNode(node->Right); if(node->Index.Size > 0) free((void *)(node->Index.Pts)); free((void *)(node)); } } int MoveTBHPoint(TBHTree *tree, int ID, float NewPos[3], int FindDirection) { int i, j; TBHNode *OrigBox; TBHNode *NewBox; TBHPoint *Pt; if(ID >= tree->NumPts || ID < 0) { #ifdef VERBOSE_BH fprintf(stderr, "Error: MoveBHPoint: Invalid point number.\n"); #endif return BH_INVALID_POINT; } OrigBox = tree->Pts[ID].Box; if(OrigBox == NULL) { #ifdef VERBOSE_BH fprintf(stderr, "Error: MoveBHPoint: NULL box pointer.\n"); #endif return BH_ALREADY_DELETED; } j = BH_YES; for(i = BH_X; i <= BH_Z && j == BH_YES; i++) { if(NewPos[i] > OrigBox->xmax[i] || NewPos[i] < OrigBox->xmin[i]) j = BH_NO; } if(j == BH_YES) { tree->Pts[ID].Pos[BH_X] = NewPos[BH_X]; tree->Pts[ID].Pos[BH_Y] = NewPos[BH_Y]; tree->Pts[ID].Pos[BH_Z] = NewPos[BH_Z]; return BH_YES; } if(OrigBox->Index.NumPts == 0) { #ifdef VERBOSE_BH fprintf(stderr, "Error: MoveBHPoint: Empty box.\n"); #endif return BH_EMPTY_BOX; } tree->Pts[ID].Pos[BH_X] = NewPos[BH_X]; tree->Pts[ID].Pos[BH_Y] = NewPos[BH_Y]; tree->Pts[ID].Pos[BH_Z] = NewPos[BH_Z]; if(FindDirection == BH_SEARCH_UP) NewBox = FindTBHNodeUp(OrigBox, NewPos); else NewBox = FindTBHNode(tree, NewPos); if(NewBox == NULL) { #ifdef VERBOSE_BH fprintf(stderr, "Error: MoveBHPoint: Point outside the tree.\n"); #endif return BH_OUTSIDE_TREE; /* Regenerate the tree */ } Pt = &tree->Pts[ID]; for(i = 0; i < OrigBox->Index.NumPts; i++) { if(OrigBox->Index.Pts[i] == Pt) break; } if (i == OrigBox->Index.NumPts) { #ifdef VERBOSE_BH fprintf(stderr, "ERROR: DeleteTBHPoint: Unable to find point\n\ in leaf array.\n"); #endif return BH_INVALID_POINT; } for(; i < OrigBox->Index.NumPts - 1; i++) OrigBox->Index.Pts[i] = OrigBox->Index.Pts[i + 1]; OrigBox->Index.NumPts--; if(NewBox->Index.NumPts == NewBox->Index.Size) { #ifdef VERBOSE_BH fprintf(stderr, "Error: MoveTBHPoint: Leaf padding filled.\n"); #endif return BH_FILLED_PADDING; } tree->Pts[ID].Box = NewBox; NewBox->Index.Pts[NewBox->Index.NumPts] = &tree->Pts[ID]; NewBox->Index.NumPts++; return BH_YES; } void DivideTBHNode(TBHNode *node, float *xmin, float *xmax, float *sxmin, float *sxmax, int granularity, int LeafPadding) { float cut, dx, xminl[3], xmaxl[3], xminr[3], xmaxr[3]; float sxminl[3], sxmaxl[3], sxminr[3], sxmaxr[3]; int dim, i, j, k, n[NSTEPS], lm, rm; TBHPoint *a; if(node == NULL) return; if(granularity < 1 || node->Index.NumPts <= granularity) return; if(node->Buffer == NULL) return; for(i = BH_X; i <= BH_Z; i++) { node->xmin[i] = sxmin[i]; node->xmax[i] = sxmax[i]; } dim = BH_X; if (xmax[BH_Y] - xmin[BH_Y] > xmax[dim] - xmin[dim]) dim = BH_Y; if (xmax[BH_Z] - xmin[BH_Z] > xmax[dim] - xmin[dim]) dim = BH_Z; dx = (xmax[dim] - xmin[dim]) / NSTEPS; if(dx < 0.0001) return; for(i = 0; i < NSTEPS; i++) n[i]=0; for(j = 0; j < node->Index.NumPts; j++) { i = (node->Buffer[j]->Pos[dim] - xmin[dim]) / dx; if(i >= 0 && i < NSTEPS) n[i]++; } for(i = 1; i < NSTEPS; i++) { n[i] += n[i-1]; if(n[i] > node->Index.NumPts / 2) break; } cut = xmin[dim] + i * dx; if (n[i] >= node->Index.NumPts) return; node->Left = (TBHNode *)malloc((size_t)sizeof(TBHNode)); if(node->Left == NULL) return; node->Left->dim = -1; node->Left->Index.NumPts = 0; node->Left->Index.Size = 0; node->Left->Index.Pts = (TBHPoint **)NULL; node->Left->Parent = node; node->Left->Left = (TBHNode *)NULL; node->Left->Right = (TBHNode *)NULL; node->Right = (TBHNode *)malloc((size_t)sizeof(TBHNode)); if(node->Right == NULL) { FreeTBHNode(node->Left); return; } node->Right->dim = -1; node->Right->Index.NumPts = 0; node->Right->Index.Size = 0; node->Right->Index.Pts = (TBHPoint **)NULL; node->Right->Parent = node; node->Right->Left = (TBHNode *)NULL; node->Right->Right = (TBHNode *)NULL; node->cut = cut; node->dim = dim; lm = 0; rm = node->Index.NumPts - 1; while(lm < rm) { for(;lm < node->Index.NumPts; lm++) if(node->Buffer[lm]->Pos[dim] >= cut) break; for(;rm >= 0; rm--) if(node->Buffer[rm]->Pos[dim] < cut) break; if(lm < rm) { a = node->Buffer[rm]; node->Buffer[rm] = node->Buffer[lm]; node->Buffer[lm] = a; rm--; lm++; } } if(lm == rm) { if(node->Buffer[rm]->Pos[dim] < cut) lm++; else rm--; } node->Left->Index.NumPts = rm + 1; node->Left->Buffer = node->Buffer; node->Right->Index.NumPts = node->Index.NumPts - (rm + 1); node->Right->Buffer = node->Buffer + lm; for(k = BH_X; k <= BH_Z; k++) { xminl[k] = xmin[k]; xmaxl[k] = xmax[k]; sxminl[k] = sxmin[k]; sxmaxl[k] = sxmax[k]; } xmaxl[dim] = cut; sxmaxl[dim] = cut; if(node->Left->Index.NumPts > granularity) DivideTBHNode(node->Left, xminl, xmaxl, sxminl, sxmaxl, granularity, LeafPadding); if(node->Left->dim == -1 && node->Left->Index.Size == 0) { for(i = BH_X; i <= BH_Z; i++) { node->Left->xmin[i] = sxminl[i]; node->Left->xmax[i] = sxmaxl[i]; } node->Left->Index.Size = node->Left->Index.NumPts + LeafPadding; node->Left->Index.Pts = (TBHPoint **)malloc(sizeof(TBHPoint *) * node->Left->Index.Size); for(i = 0; i < node->Left->Index.NumPts; i++) { node->Left->Index.Pts[i] = node->Left->Buffer[i]; node->Left->Index.Pts[i]->Box = node->Left; } } for(k = BH_X; k <= BH_Z; k++) { xminr[k] = xmin[k]; xmaxr[k] = xmax[k]; sxminr[k] = sxmin[k]; sxmaxr[k] = sxmax[k]; } xminr[dim] = cut; sxminr[dim] = cut; if(node->Right->Index.NumPts > granularity) DivideTBHNode(node->Right, xminr, xmaxr, sxminr, sxmaxr, granularity, LeafPadding); if(node->Right->dim == -1 && node->Right->Index.Size == 0) { for(i = BH_X; i <= BH_Z; i++) { node->Right->xmin[i] = sxminr[i]; node->Right->xmax[i] = sxmaxr[i]; } node->Right->Index.Size = node->Right->Index.NumPts + LeafPadding; node->Right->Index.Pts = (TBHPoint **)malloc(sizeof(TBHPoint *) * node->Right->Index.Size); for(i = 0; i < node->Right->Index.NumPts; i++) { node->Right->Index.Pts[i] = node->Right->Buffer[i]; node->Right->Index.Pts[i]->Box = node->Right; } } return; } int FindTBHCloseAtomsDist(TBHTree *tree, float *x, float cutoff, int *atom, float *dist, int maxn) { int i; if(maxn < 1 || tree == NULL || cutoff <= 0.0) return(0); if(tree->Root == NULL) return(0); for(i = 0; i < 3; i++) { if(x[i] < tree->xmin[i] - cutoff) break; if(x[i] > tree->xmax[i] + cutoff) break; } if(i < 3) return(0); return (FindTBHCloseAtomsInNodeDist(tree->Root, x, cutoff, atom, dist, maxn)); } int FindTBHCloseAtomsInNodeDist(TBHNode *node, float *x, float cutoff, int *atom, float *dist, int maxn) { int j, n; float d[3], D, C; if(node == NULL) return(0); if(maxn <= 0) return(0); if(node->Index.NumPts < 1) return(0); if(node->dim < 0) { n=0; C=cutoff*cutoff; for(j = 0; j < node->Index.NumPts; j++) { d[0] = x[0] - node->Index.Pts[j]->Pos[0]; if(d[0] > cutoff || d[0] < -cutoff) continue; d[1] = x[1] - node->Index.Pts[j]->Pos[1]; if(d[1] > cutoff || d[1] < -cutoff) continue; d[2] = x[2] - node->Index.Pts[j]->Pos[2]; if(d[2] > cutoff || d[2] < -cutoff) continue; D = d[0]*d[0] + d[1]*d[1] + d[2]*d[2]; if(D > C) continue; if(n < maxn) { atom[n] = node->Index.Pts[j]->ID; dist[n]=D; n++; } else { n++; break; } } } else { n = 0; if(x[node->dim] < node->cut + cutoff) { n += FindTBHCloseAtomsInNodeDist(node->Left, x, cutoff, atom, dist, maxn); } if(x[node->dim] >= node->cut - cutoff) { n += FindTBHCloseAtomsInNodeDist(node->Right, x, cutoff, atom + n, dist + n, maxn - n); } } return(n); } int FindTBHCloseAtoms(TBHTree *tree, float *x, float cutoff, int *atom, int maxn) { int i; if(maxn < 1 || tree == NULL || cutoff <= 0.0) return(0); if(tree->Root == NULL) return(0); for(i = 0; i < 3; i++) { if(x[i] < tree->xmin[i] - cutoff) break; if(x[i] > tree->xmax[i] + cutoff) break; } if(i < 3) return(0); #ifdef STATBHTREE i = FindTBHCloseAtomsInNode(tree->Root, x, cutoff, atom, maxn); tree->nbr++; tree->tot += i; if(i > tree->max) tree->max = i; if(i < tree->min) tree->min = i; return(i); #else return(FindTBHCloseAtomsInNode(tree->Root, x, cutoff, atom, maxn)); #endif } int FindTBHCloseAtomsInNode(TBHNode *node, float *x, float cutoff, int *atom, int maxn) { int j, n; float d[3], D, C; if(node == NULL) return(0); if(maxn <= 0) return(0); if(node->Index.NumPts < 1) return(0); if(node->dim < 0) { n = 0; C = cutoff * cutoff; for(j = 0; j < node->Index.NumPts; j++) { d[0] = x[0] - node->Index.Pts[j]->Pos[0]; if(d[0] > cutoff || d[0] < -cutoff) continue; d[1] = x[1] - node->Index.Pts[j]->Pos[1]; if(d[1] > cutoff || d[1] < -cutoff) continue; d[2] = x[2] - node->Index.Pts[j]->Pos[2]; if(d[2] > cutoff || d[2] < -cutoff) continue; D = d[0]*d[0] + d[1]*d[1] + d[2]*d[2]; if(D > C) continue; if(n < maxn) { atom[n] = node->Index.Pts[j]->ID; n++; } else { n++; break; } } } else { n = 0; if(x[node->dim] < node->cut + cutoff) n += FindTBHCloseAtomsInNode(node->Left, x, cutoff, atom, maxn); if(x[node->dim] >= node->cut - cutoff) n += FindTBHCloseAtomsInNode(node->Right, x, cutoff, atom + n, maxn - n); } return(n); } RBHTree *GenerateRBHTree(TBHPoint *Pts, int NumPts, int MaxPts, int Granularity, int LeafPadding, int DeletePadding, float SpacePadding, int OwnsMemory) { RBHTree *r; int i, j; float xmin[3], xmax[3], sxmin[3], sxmax[3]; r = (RBHTree *)malloc((size_t)sizeof(RBHTree)); if(r == NULL) return(r); r->Granularity = Granularity; r->LeafPadding = LeafPadding; r->SpacePadding = SpacePadding; r->Flags = 0; r->SizePts = MaxPts; r->FreePts.NumPts = MaxPts - NumPts; r->FreePts.Size = (MaxPts - NumPts) + DeletePadding; r->FreePts.Pts = (TBHPoint **)malloc(sizeof(TBHPoint *) * r->FreePts.Size); if(r->FreePts.Pts == NULL) return NULL; for(i = 0; i < NumPts; i++) Pts[i].ID = i; for(i = 0; i < r->FreePts.NumPts; i++) { Pts[i + NumPts].ID = i + NumPts; Pts[i + NumPts].Box = NULL; r->FreePts.Pts[i] = &Pts[MaxPts - i - 1]; } r->bfl = 0; r->rm=0.0; for (i=0;irmrm = Pts[i].Rad; r->rm += 0.1; #ifdef STATBHTREE r->tot = 0; r->max = 0; r->min = 9999999; r->nbr = 0; #endif r->Root = (TBHNode *)malloc((size_t)sizeof(TBHNode)); if(r->Root == NULL) { FreeRBHTree(r); return((RBHTree *)NULL); } r->Root->Index.NumPts = 0; r->Root->Index.Size = 0; r->Root->Index.Pts = (TBHPoint **)NULL; r->Root->Parent = (TBHNode *)NULL; r->Root->dim = -1; r->Root->Left = (TBHNode *)NULL; r->Root->Right = (TBHNode *)NULL; r->Pts = Pts; r->Flags = 0; if(OwnsMemory) r->Flags |= FLAG_OWNSMEMORY; r->NumPts = NumPts; r->Root->Index.Pts = (TBHPoint **)NULL; r->Root->Index.NumPts = NumPts; r->Root->Buffer = (TBHPoint **)NULL; if(NumPts == 0) { r->Flags |= FLAG_EMPTY_TREE; return r; } for(j = BH_X; j <= BH_Z; j++) { xmin[j] = Pts[0].Pos[j]; xmax[j] = xmin[j]; } for(i = 1; i < NumPts; i++) { for(j = BH_X; j <= BH_Z; j++) { if (xmin[j] > Pts[i].Pos[j]) xmin[j] = Pts[i].Pos[j]; if (xmax[j] < Pts[i].Pos[j]) xmax[j] = Pts[i].Pos[j]; } } for(j = BH_X; j <= BH_Z; j++) { sxmin[j] = xmin[j] - SpacePadding; sxmax[j] = xmax[j] + SpacePadding; r->xmin[j] = sxmin[j]; r->xmax[j] = sxmax[j]; } r->Root->Buffer = (TBHPoint **)malloc(sizeof(TBHPoint *) * NumPts); if(r->Root->Buffer == NULL) return NULL; for(i = 0; i < NumPts; i++) { Pts[i].ID = i; r->Root->Buffer[i] = &Pts[i]; } DivideTBHNode(r->Root, xmin, xmax, sxmin, sxmax, Granularity, LeafPadding); if(r->Root->dim == -1 && r->Root->Index.Size == 0) { for(i = BH_X; i <= BH_Z; i++) { r->Root->xmin[i] = sxmin[i]; r->Root->xmax[i] = sxmax[i]; } r->Root->Index.Size = r->Root->Index.NumPts + LeafPadding; r->Root->Index.Pts = (TBHPoint **)malloc(sizeof(TBHPoint *) * r->Root->Index.Size); for(i = 0; i < r->Root->Index.NumPts; i++) { r->Root->Index.Pts[i] = r->Root->Buffer[i]; r->Root->Index.Pts[i]->Box = r->Root; } } return(r); } TBHNode *FindRBHNode(RBHTree *tree, float *x) { TBHNode *r; int k; if(tree == NULL) return((TBHNode *)NULL); if(tree->Flags & FLAG_EMPTY_TREE) return((TBHNode *)NULL); for(k = 0; k < 3; k++) { if(x[k] < tree->xmin[k]) return((TBHNode *)NULL); if(x[k] > tree->xmax[k]) return((TBHNode *)NULL); } r = tree->Root; while(r != NULL) { if(r->dim < 0) break; if(x[r->dim] < r->cut) r = r->Left; else r = r->Right; } return(r); } void FreeRBHTree(RBHTree *tree) { if(!tree) return; if(tree->Pts != NULL && (tree->Flags & FLAG_OWNSMEMORY)) free((void *)(tree->Pts)); free((void *)(tree->FreePts.Pts)); if(!(tree->Flags & FLAG_EMPTY_TREE)) { free((void *)tree->Root->Buffer); FreeTBHNode(tree->Root); } free((void *)tree); } int InsertRBHPoint(RBHTree *tree, float *Pos, float Rad, void *Data, int uInt, int *ID) { int f; TBHPoint *point; TBHNode *NewBox, *PBox; if(!tree) { #ifdef VERBOSE_BH fprintf(stderr, "Error: InsertRBHPoint: Sent a NULL pointer.\n"); #endif return BH_NO; } if(tree->FreePts.NumPts == 0) { #ifdef VERBOSE_BH fprintf(stderr, "Error: InsertRBHPoint: No free space left.\n"); #endif return BH_NO; } f = tree->FreePts.NumPts - 1; *ID = tree->FreePts.Pts[f]->ID; point = &tree->Pts[*ID]; point->Pos[0] = Pos[0]; point->Pos[1] = Pos[1]; point->Pos[2] = Pos[2]; point->Rad = Rad; point->Data = Data; point->uInt = uInt; point->Box = tree->Root; /* For regenerate; show that point is used */ tree->FreePts.NumPts--; tree->NumPts++; NewBox = FindRBHNode(tree, Pos); if(NewBox == NULL) { #ifdef VERBOSE_BH fprintf(stderr, "Warning: InsertRBHPoint: Regenerating the tree.\n"); #endif if(!RegenerateRBHTree(tree)) { #ifdef VERBOSE_BH fprintf(stderr, "Error: InsertRBHPoint: Failed to regenerate" "the tree.\n"); #endif return BH_NO; } return BH_YES; } if(NewBox->Index.NumPts == NewBox->Index.Size) { #ifdef VERBOSE_BH fprintf(stderr, "Error: InsertRBHPoint: Leaf padding filled.\n"); #endif if(!RegenerateRBHTree(tree)) { #ifdef VERBOSE_BH fprintf(stderr, "Error: InsertRBHPoint: Failed to regenerate" "the tree.\n"); #endif return BH_NO; } return BH_YES; } point->Box = NewBox; NewBox->Index.Pts[NewBox->Index.NumPts] = &tree->Pts[*ID]; PBox = NewBox; while(PBox != NULL) { PBox->Index.NumPts += 1; PBox = PBox->Parent; } return BH_YES; } static void FreeTreeStructure(TBHNode *node) { if(!node) return; FreeTreeStructure(node->Left); FreeTreeStructure(node->Right); if(node->Index.Pts) free((void *)node->Index.Pts); free((void *)node); return; } static int RegenerateRBHTree(RBHTree *tree) { int i, j; float xmin[3], xmax[3], sxmin[3], sxmax[3]; TBHPoint **RealPts; /* This function restructures the tree while keeping the structure of the Pts array in O(n log(n)) */ if(tree == NULL) return 0; RealPts = (TBHPoint **)malloc(sizeof(TBHPoint *) * tree->NumPts); for(i = j = 0; i < tree->NumPts; i++) { while(tree->Pts[j].Box == NULL) j++; RealPts[i] = &(tree->Pts[j]); j++; } if(tree->NumPts) tree->Flags &= ~FLAG_EMPTY_TREE; tree->bfl = 0; tree->rm=0.0; for(i = 0; i < tree->NumPts; i++) if(tree->rm < RealPts[i]->Rad) tree->rm = RealPts[i]->Rad; tree->rm += 0.1; #ifdef STATBHTREE tree->tot = 0; tree->max = 0; tree->min = 9999999; tree->nbr = 0; #endif if(tree->Root->Buffer) free((void *)tree->Root->Buffer); FreeTreeStructure(tree->Root); tree->Root = (TBHNode *)malloc((size_t)sizeof(TBHNode)); if(tree->Root == NULL) { free((void *)RealPts); return 0; } tree->Root->Index.NumPts = 0; tree->Root->Index.Pts = (TBHPoint **)NULL; tree->Root->Index.Size = 0; tree->Root->Parent = (TBHNode *)NULL; tree->Root->dim = -1; tree->Root->Left = (TBHNode *)NULL; tree->Root->Right = (TBHNode *)NULL; if(tree->NumPts == 0) { /* This will never happen */ free((void *)RealPts); return 0; } for(j = BH_X; j <= BH_Z; j++) { xmin[j] = RealPts[0]->Pos[j]; xmax[j] = xmin[j]; } for(i = 1; i < tree->NumPts; i++) { for(j = BH_X; j <= BH_Z; j++) { if (xmin[j] > RealPts[i]->Pos[j]) xmin[j] = RealPts[i]->Pos[j]; if (xmax[j] < RealPts[i]->Pos[j]) xmax[j] = RealPts[i]->Pos[j]; } } tree->Root->Index.Pts = (TBHPoint **)NULL; tree->Root->Index.NumPts = tree->NumPts; for(j = BH_X; j <= BH_Z; j++) { sxmin[j] = xmin[j] - tree->SpacePadding; sxmax[j] = xmax[j] + tree->SpacePadding; tree->xmin[j] = sxmin[j]; tree->xmax[j] = sxmax[j]; } tree->Root->Buffer = (TBHPoint **)malloc(sizeof(TBHPoint *) * tree->NumPts); if(tree->Root->Buffer == NULL) { free((void *)RealPts); return 0; } for(i = 0; i < tree->NumPts; i++) tree->Root->Buffer[i] = RealPts[i]; DivideTBHNode(tree->Root, xmin, xmax, sxmin, sxmax, tree->Granularity, tree->LeafPadding); if(tree->Root->dim == -1 && tree->Root->Index.Size == 0) { for(i = BH_X; i <= BH_Z; i++) { tree->Root->xmin[i] = sxmin[i]; tree->Root->xmax[i] = sxmax[i]; } tree->Root->Index.Size = tree->Root->Index.NumPts + tree->LeafPadding; tree->Root->Index.Pts = (TBHPoint **)malloc(sizeof(TBHPoint *) * tree->Root->Index.Size); for(i = 0; i < tree->Root->Index.NumPts; i++) { tree->Root->Index.Pts[i] = tree->Root->Buffer[i]; tree->Root->Index.Pts[i]->Box = tree->Root; } } free((void *)RealPts); return 1; } int DeleteRBHPoint(RBHTree *tree, int ID) { int i; TBHNode *OrigBox; TBHPoint *Pt; if(!tree) { #ifdef VERBOSE_BH fprintf(stderr, "Error: DeleteRBHPoint: Sent a NULL pointer.\n"); #endif return BH_NO; } if(tree->Flags & FLAG_EMPTY_TREE) { #ifdef VERBOSE_BH fprintf(stderr, "Error: DeleteRBHPoint: Sent an empty tree.\n"); #endif return BH_NO; } if(ID >= tree->SizePts || ID < 0) { #ifdef VERBOSE_BH fprintf(stderr, "Error: DeleteRBHPoint: Invalid point ID.\n"); #endif return BH_INVALID_POINT; } OrigBox = tree->Pts[ID].Box; if(OrigBox == NULL) { #ifdef VERBOSE_BH fprintf(stderr, "ERROR: DeleteRBHPoint: Tried to delete\n\ already deleted point.\n"); #endif return BH_ALREADY_DELETED; } if(OrigBox->Index.NumPts == 0) { #ifdef VERBOSE_BH fprintf(stderr, "ERROR: DeleteRBHPoint: Tried to delete\n\ a point from an empty box.\n"); #endif return BH_EMPTY_BOX; } Pt = &tree->Pts[ID]; for(i = 0; i < OrigBox->Index.NumPts; i++) { if(OrigBox->Index.Pts[i] == Pt) break; } if (i == OrigBox->Index.NumPts) { #ifdef VERBOSE_BH fprintf(stderr, "ERROR: DeleteRBHPoint: Unable to find point\n\ in leaf array.\n"); #endif return BH_INVALID_POINT; } for(; i < OrigBox->Index.NumPts - 1; i++) OrigBox->Index.Pts[i] = OrigBox->Index.Pts[i + 1]; OrigBox->Index.NumPts--; if(tree->FreePts.NumPts == tree->FreePts.Size) { tree->FreePts.Size += BH_PADDING; tree->FreePts.Pts = (TBHPoint **)realloc(tree->FreePts.Pts, sizeof(TBHPoint *) * tree->FreePts.Size); if(tree->FreePts.Pts == NULL) return BH_NO; #ifdef VERBOSE_BH fprintf(stderr, "WARNING: DeleteRBHPoint: Too many points deleted from\n\ RBHTree, reallocating space in tree->FreePts.Pts\n"); #endif } tree->FreePts.Pts[tree->FreePts.NumPts] = &tree->Pts[ID]; tree->Pts[ID].Box = NULL; tree->FreePts.NumPts++; tree->NumPts--; return BH_YES; } int MoveRBHPoint(RBHTree *tree, int ID, float NewPos[3], int FindDirection) { int i, j; TBHNode *OrigBox; TBHNode *NewBox; TBHPoint *Pt; if(!tree || (tree->Flags & FLAG_EMPTY_TREE)) { #ifdef VERBOSE_BH fprintf(stderr, "Error: MoveRBHPoint: Sent an empty tree.\n"); #endif return BH_NO; } if(ID >= tree->SizePts || ID < 0) { #ifdef VERBOSE_BH fprintf(stderr, "Error: MoveRBHPoint: Invalid point ID.\n"); #endif return BH_INVALID_POINT; } OrigBox = tree->Pts[ID].Box; if(OrigBox == NULL) { #ifdef VERBOSE_BH fprintf(stderr, "Error: MoveRBHPoint: Point already deleted.\n"); #endif return BH_ALREADY_DELETED; } j = BH_YES; for(i = BH_X; i <= BH_Z && j == BH_YES; i++) { if(NewPos[i] > OrigBox->xmax[i] || NewPos[i] < OrigBox->xmin[i]) j = BH_NO; } if(j == BH_YES) { tree->Pts[ID].Pos[BH_X] = NewPos[BH_X]; tree->Pts[ID].Pos[BH_Y] = NewPos[BH_Y]; tree->Pts[ID].Pos[BH_Z] = NewPos[BH_Z]; return BH_YES; } if(OrigBox->Index.NumPts == 0) { #ifdef VERBOSE_BH fprintf(stderr, "Error: MoveRBHPoint: Empty box.\n"); #endif return BH_EMPTY_BOX; } tree->Pts[ID].Pos[BH_X] = NewPos[BH_X]; tree->Pts[ID].Pos[BH_Y] = NewPos[BH_Y]; tree->Pts[ID].Pos[BH_Z] = NewPos[BH_Z]; if(FindDirection == BH_SEARCH_UP) NewBox = FindTBHNodeUp(OrigBox, NewPos); else NewBox = FindRBHNode(tree, NewPos); if(NewBox == NULL) { #ifdef VERBOSE_BH fprintf(stderr, "Error: MoveRBHPoint: Point outside tree.\n"); #endif return BH_OUTSIDE_TREE; /* Regenerate the tree */ } Pt = &tree->Pts[ID]; for(i = 0; i < OrigBox->Index.NumPts; i++) { if(OrigBox->Index.Pts[i] == Pt) break; } if (i == OrigBox->Index.NumPts) { #ifdef VERBOSE_BH fprintf(stderr, "ERROR: DeleteRBHPoint: Unable to find point\n\ in leaf array.\n"); #endif return BH_INVALID_POINT; } for(; i < OrigBox->Index.NumPts - 1; i++) OrigBox->Index.Pts[i] = OrigBox->Index.Pts[i + 1]; OrigBox->Index.NumPts--; if(NewBox->Index.NumPts == NewBox->Index.Size) { #ifdef VERBOSE_BH fprintf(stderr, "Error: MoveRBHPoint: Leaf padding filled.\n"); #endif if(!RegenerateRBHTree(tree)) { #ifdef VERBOSE_BH fprintf(stderr, "Error: InsertRBHPoint: Failed to regenerate" "the tree.\n"); #endif return BH_NO; } return BH_YES; } tree->Pts[ID].Box = NewBox; NewBox->Index.Pts[NewBox->Index.NumPts] = &tree->Pts[ID]; NewBox->Index.NumPts++; return BH_YES; } int ModifyRBHPoint(RBHTree *tree, int ID, float Rad) { if(!tree || (tree->Flags & FLAG_EMPTY_TREE)) { #ifdef VERBOSE_BH fprintf(stderr, "Error: ModifyRBHPoint: Sent an empty tree.\n"); #endif return BH_NO; } if(ID >= tree->SizePts || ID < 0) { #ifdef VERBOSE_BH fprintf(stderr, "Error: ModifyRBHPoint: Invalid point ID.\n"); #endif return BH_INVALID_POINT; } if(tree->Pts[ID].Box == NULL) { #ifdef VERBOSE_BH fprintf(stderr, "Error: ModifyRBHPoint: Invalid point ID;\ NULL box.\n"); #endif return BH_INVALID_POINT; } tree->Pts[ID].Rad = Rad; if (Rad > tree->rm) tree->rm = Rad; return BH_YES; } int ModifyBHPoint(TBHTree *tree, int ID, float Rad) { if(!tree) { #ifdef VERBOSE_BH fprintf(stderr, "Error: ModifyRBHPoint: Sent an empty tree.\n"); #endif return BH_NO; } if(ID >= tree->NumPts || ID < 0) { #ifdef VERBOSE_BH fprintf(stderr, "Error: ModifyRBHPoint: Invalid point ID.\n"); #endif return BH_INVALID_POINT; } if(tree->Pts[ID].Box == NULL) { #ifdef VERBOSE_BH fprintf(stderr, "Error: ModifyRBHPoint: Invalid point ID;\ NULL box.\n"); #endif return BH_INVALID_POINT; } tree->Pts[ID].Rad = Rad; if (Rad > tree->rm) tree->rm = Rad; return BH_YES; } int FindRBHCloseAtomsDist(RBHTree *tree, float *x, float cutoff, int *atom, float *dist, int maxn) { int i; if(!tree || (tree->Flags & FLAG_EMPTY_TREE)) { #ifdef VERBOSE_BH fprintf(stderr, "Error: ModifyRBHPoint: Sent an empty tree.\n"); #endif return BH_NO; } if(maxn < 1 || tree == NULL || cutoff <= 0.0) return(0); if(tree->Root == NULL) return(0); for(i = 0; i < 3; i++) { if(x[i] < tree->xmin[i] - cutoff) break; if(x[i] > tree->xmax[i] + cutoff) break; } if(i < 3) return(0); return (FindTBHCloseAtomsInNodeDist(tree->Root, x, cutoff, atom, dist, maxn)); } int FindRBHCloseAtoms(RBHTree *tree, float *x, float cutoff, int *atom, int maxn) { int i; if(!tree || (tree->Flags & FLAG_EMPTY_TREE)) { #ifdef VERBOSE_BH fprintf(stderr, "Error: ModifyRBHPoint: Sent an empty tree.\n"); #endif return BH_NO; } if(maxn < 1 || tree == NULL || cutoff <= 0.0) return(0); if(tree->Root == NULL) return(0); for(i = BH_X; i <= BH_Z; i++) { if(x[i] < tree->xmin[i] - cutoff) break; if(x[i] > tree->xmax[i] + cutoff) break; } if(i < 3) return(0); #ifdef STATBHTREE i = FindTBHCloseAtomsInNode(tree->Root, x, cutoff, atom, maxn); tree->nbr++; tree->tot += i; if(i > tree->max) tree->max = i; if(i < tree->min) tree->min = i; return(i); #else return(FindTBHCloseAtomsInNode(tree->Root, x, cutoff, atom, maxn)); #endif } bhtree-1.5.7~rc1+cvs.20140424/src/bhtree.h0000644000175000017500000003047611346537767017415 0ustar moellermoeller/************************************************************************ original BHTree (fixed size, fixed points locations) ************************************************************************/ typedef struct BHpoint { float x[3]; float r; int at; } BHpoint; typedef struct BHnode { struct BHnode *left,*right; struct BHpoint **atom; float cut; int dim,n; } BHnode; typedef struct BHtree { struct BHnode *root; struct BHpoint **atom; float xmin[3]; float xmax[3]; float maxr; float rm; #ifdef STATBHTREE long tot; /* total number of neighbors returned by findBHclose */ int max,min; /* min and max of these numbers */ int nbr; /* number of calls to findBHclose */ #endif int *nodeLookUp; /* atoms[nodeLookUp[i]] = point #i in intial array */ int nbp; /* nbp, length of **atom */ short bfl; } BHtree; BHtree *generateBHtree(BHpoint **atoms, int nbat, int granularity); BHnode *findBHnode(BHtree *tree, float *x); int findBHcloseAtoms(BHtree *tree, float *x, float cutoff, int *atom, int maxn); int findBHcloseAtomsdist(BHtree *tree, float *x, float cutoff, int *atom, float *d, int maxn); int findBHcloseAtomsdist2(BHtree *tree, float *x, float cutoff, int *atom, float *dist, int maxn); void freeBHtree(BHtree *tree); void divideBHnode(BHnode *node, float *xmin, float *xmax, int granularity); void freeBHnode(BHnode *node); int *findClosePairsInTree(BHtree *bht, float tolerance); int *findClosePairs( BHtree *bht, float xyz[1][3], int *nbxyz, float rad[1], int nbrad, float tolerance ); int *findClosestAtoms( BHtree *bht, float xyz[1][3], int *nbxyz, float cut, int returnNullIfFail ); int *findClosestAtomsDist2( BHtree *bht, float xyz[1][3], int nbxyz, float *cl_dist, float cut, int returnNullIfFail ); /************************************************************************ mutable bhtrees: (Chris Carrillo) TBHtree: points can be translated RTBHtree: points can be translated, added and deleted ************************************************************************/ #ifndef RBHTREEDEF #define RBHTREEDEF #define BH_MAXFINDCOUNT 512 /* Maximum number of points that can be returned in a Find call */ #define BH_FINDCOUNT 512 /* See above */ #define BH_MAXBOX 10 /* Maximum original size of a leaf node in points */ #define BH_PADDING 10 /* Size of a chunk of memory reallocated when InsertPadding or DeletePadding runs out */ #define BH_SEARCH_UP 0 /* for FindDirection, search up and then down the tree */ #define BH_SEARCH_DOWN 1 /* for FindDirection, search from the root downward */ #define RBH_INSERTPADDING 25 /* Not used in the code */ #define RBH_DELETEPADDING 50 /* Not used in the code */ #define RBH_SPACEPADDING 15.0 #define BH_LARGE_SPACE_PADDING 999999.9 /* used for infinite padding */ #define BH_LEAFPADDING 10 /* Not used in the code */ #define BH_OUTSIDE_TREE 3 /* Warning: Point being moved or inserted is outside the tree boundaries */ #define BH_FILLED_PADDING 4 /* Warning: The leaf node array is full; cannot insert or move a point */ #define BH_EMPTY_BOX 5 /* Warning: Cannot delete this point; its parent box would become empty */ #define BH_ALREADY_DELETED 6 /* Warning: This point has already been deleted */ #define BH_INVALID_POINT 7 /* Warning: The ID number passed is invalid for one of a number of reasons */ #define BH_MEMORY_ERROR 8 /* Failed in malloc or realloc*/ #define BH_X 0 #define BH_Y 1 #define BH_Z 2 #define BH_YES 1 #define BH_NO 0 #define NSTEPS 128 /* Number of steps in the histogram used for dividing nodes */ #define FLAG_OWNSMEMORY 1 #define FLAG_EMPTY_TREE 2 typedef struct TBHPoint { float Pos[3]; /* 3D coordinate of point */ float Rad; /* Radius of point */ void *Data; /* void Data, for reduced surface */ int uInt; /* user settable integer */ int ID; /* identification, same as "at", address in the array */ struct TBHNode *Box; /* box to which this point belongs */ } TBHPoint; typedef struct TBHIndex { struct TBHPoint **Pts; /* leaf array of pointers to points plus any padding space */ int NumPts; /* number of valid points in Pts array; does not include padding; points are consecutive */ int Size; /* size of Pts array; is the number of points originally in this leaf plus the padding; does not change, no reallocating is done for leaves */ } TBHIndex; typedef struct TBHNode { struct TBHNode *Left, *Right, *Parent; /* The Left and Right child nodes of this node, and the Parent node; Parent is NULL if this is root; Left and Right are NULL if this is a leaf */ struct TBHPoint **Buffer; /* Array of pointers to members of the tree->Pts array; this array is shuffled whereas the actual tree->Pts array is not */ struct TBHIndex Index; /* Index, see above */ float xmin[3]; /* Minimum extents on the three axes */ float xmax[3]; /* Maximum extents on the three axes */ float cut; /* Place along the axis where the cutting plane is constructed */ int dim; /* Dimension on which the cutting plane is constructed; -1 if leaf */ } TBHNode; /* Static BH Tree */ typedef struct TBHTree { struct TBHNode *Root; /* Root node */ TBHPoint *Pts; /* Pts array, is not reshuffled */ int NumPts; /* Number of points in Pts array */ float xmin[3]; /* Minimum extents on the three axes */ float xmax[3]; /* Maximum extents on the three axes */ float rm; #ifdef STATBHTREE long tot; /* total number of neighbors returned by findBHclose */ int max,min; /* min and max of these numbers */ int nbr; /* number of calls to findBHclose */ #endif short bfl; } TBHTree; typedef struct RBHTree { struct TBHNode *Root; /* Root node */ TBHPoint *Pts; /* Pts array, is not reshuffled */ TBHIndex FreePts; /* Index to the free points in the Pts array */ int NumPts; /* Number of points in Pts array ORIGINALLY; is not maintained through updates; Pts array is NOT CONTIGUOUS */ int SizePts; /* Size of Pts array */ float xmin[3]; /* Minimum extents on the three axes */ float xmax[3]; /* Maximum extents on the three axes */ float rm; #ifdef STATBHTREE long tot; /* total number of neighbors returned by findBHclose */ int max,min; /* min and max of these numbers */ int nbr; /* number of calls to findBHclose */ #endif short bfl; int Flags; int Granularity; int LeafPadding; float SpacePadding; } RBHTree; /* LeafPadding: Padding added to the array of points in each leaf; The number of new points that can be added to each box through the move and insert commands */ /* Static BH Tree */ extern TBHTree *GenerateTBHTree(TBHPoint *Pts, int NumPts, int Granularity, int LeafPadding, float SpacePadding); extern TBHNode *FindTBHNodeUp(TBHNode *node, float x[3]); extern TBHNode *FindTBHNode(TBHTree *tree, float *x); extern void FreeTBHTree(TBHTree *tree); extern void FreeTBHNode(TBHNode *node); /* ID: Identification number of the point to be moved; same as "at"; NewPos: New position of the point; FindDirection: Either BH_SEARCH_UP (go up the tree and then down) or BH_SEARCH_DOWN (start at the root and search down) This function is for the Static BH Tree */ extern int MoveTBHPoint(TBHTree *tree, int ID, float NewPos[3], int FindDirection); /* LeafPadding: The same as in the Generate call, passed to this function by Generate */ extern void DivideTBHNode(TBHNode *node, float *xmin, float *xmax, float *sxmin, float *sxmax, int granularity, int LeafPadding); extern int FindTBHCloseAtomsDist(TBHTree *tree, float *x, float cutoff, int *atom, float *dist, int maxn); extern int FindTBHCloseAtomsInNodeDist(TBHNode *node, float *x, float cutoff, int *atom, float *dist, int maxn); extern int FindTBHCloseAtoms(TBHTree *tree, float *x, float cutoff, int *atom, int maxn); extern int FindTBHCloseAtomsInNode(TBHNode *node, float *x, float cutoff, int *atom, int maxn); extern int ModifyBHPoint(TBHTree *tree, int ID, float Rad); /* LeafPadding : for every leaf an array of leaf->Index.NumPts + LeafPadding pointer is allocated to store pointers to TBHPoints in box InsertPadding: Number of points that can be added to the tree. If limit is reached error message and error code returned. DeletePadding: Number of points that can be deleted from the tree. SpacePadding: The amount of void "padding" space stored around the tree to allow inserts and moves outside of the original boundaries of the tree */ extern RBHTree *GenerateRBHTree(TBHPoint *Pts, int NumPts, int MaxPts, int Granularity, int LeafPadding, int DeletePadding, float SpacePadding, int OwnsMemory); extern TBHNode *FindRBHNode(RBHTree *tree, float *x); extern void FreeRBHTree(RBHTree *tree); /* *ID: Pointer to integer, filled with the new identification number of the inserted point; the array to which this point is added is not contiguous; thus this number cannot be predicted and should be stored when returned for future reference */ /* The below functions apply only to the Dynamic BH Tree */ extern int InsertRBHPoint(RBHTree *tree, float *Pos, float Rad, void *Data, int uInt, int *ID); extern int DeleteRBHPoint(RBHTree *tree, int ID); /* FindDirection: Either BH_SEARCH_UP (search up and then down the tree) or BH_SEARCH_DOWN (start at the root and search down) */ extern int MoveRBHPoint(RBHTree *tree, int ID, float NewPos[3], int FindDirection); extern int ModifyRBHPoint(RBHTree *tree, int ID, float Rad); extern int FindRBHCloseAtomsDist(RBHTree *tree, float *x, float cutoff, int *atom, float *dist, int maxn); extern int FindRBHCloseAtoms(RBHTree *tree, float *x, float cutoff, int *atom, int maxn); #endif