Advertisement
chironex

interp3d.pyx

Jun 29th, 2011
644
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.96 KB | None | 0 0
  1. import numpy as np
  2. cimport numpy as np
  3. from libc.stdlib cimport free, malloc
  4.  
  5. ########### from utils.h
  6.  
  7. ctypedef struct stack:
  8.     int top
  9.     int slots
  10.     void** arr
  11.  
  12. ctypedef struct arrayList:
  13.     int   num_slots
  14.     int   num_elements
  15.     void** arr
  16.  
  17. cdef struct _listNode:
  18.     void *data
  19.     _listNode *next
  20.     _listNode *prev
  21.  
  22. ctypedef _listNode listNode
  23.  
  24. ctypedef struct linkedList:
  25.     listNode *head
  26.     listNode *last
  27.     int nelem
  28.     stack   *deadNodes
  29.  
  30. ############ from delaunay.h
  31.  
  32. ctypedef struct vertex:
  33.     ### This is the location of this point.
  34.     double v[3]
  35.  
  36.     ### This is the point index in the point list.
  37.     ### We only store this so that it is convenient for using the
  38.     ### tet-mesh function in Matlab.
  39.     ### We can remove it for when the code is actually used.
  40.     int    index
  41.  
  42.     ### These are the values for our vector field at this location.
  43.     double data[3]
  44.  
  45.     ### We use this for caching the voronoi volume of this point.
  46.     ### it will provide a good speed-up!
  47.     double voronoiVolume
  48.  
  49. cdef struct _simplex:
  50.     ### The verticies of this simplex.
  51.     vertex  *p[4]
  52.     ### The neighbouring simlpicies of this simplex.
  53.     ### These are ordered in accordance with our 'get face' routine:
  54.     ### so that the i'th face is shared with the i'th neighbour.
  55.     _simplex *s[4]
  56.     ### This is the node in our auxillary list structure that holds this simplex.
  57.     ### It's far from an elegant solution: but it is fast and space-efficient.
  58.     listNode *node
  59.  
  60. ctypedef _simplex simplex
  61.  
  62. ctypedef struct neighbourUpdate:
  63.     stack  *ptrs
  64.     stack  *old
  65.  
  66. ctypedef struct mesh:
  67.     ### a linked list of all the simplicies.
  68.     linkedList    *tets
  69.  
  70.     ### The simplex which contains all of the points.
  71.     ### its verticies contain no data values.
  72.     simplex *super
  73.     vertex   superVerticies[4]
  74.  
  75.     ### Memory pool.
  76.     stack   *deadSimplicies
  77.     stack   *deadVoronoiCells
  78.  
  79.     ### We modify these when a point is inserted/removed.
  80.     arrayList       *conflicts
  81.     arrayList       *updates
  82.     neighbourUpdate *neighbourUpdates
  83.  
  84.     ### Keep count of the number of degenerecies we find in the mesh,
  85.     ### so that we can spot errors, and be aware of particularly degenerate data.
  86.     int coplanar_degenerecies
  87.     int cospherical_degenerecies
  88.  
  89.  
  90.  
  91. cdef extern from "3d_interp_v1/natural.h":
  92.     void          interpolate3_3(double x, double y, double z, double *u, double *v, double *w, mesh *m)
  93.     vertex       *loadPoints(char *filename, int *n)
  94.     vertex       *initPoints(double *x, double *y, double *z, double *u, double *v, double *w, int n)
  95.     void          writePointsToFile(vertex *ps, int n)
  96.     # void        lastNaturalNeighbours(vertex *v, mesh *m, arrayList *neighbours, arrayList *neighbourSimplicies)
  97.  
  98. cdef extern from "3d_interp_v1/delaunay.h":
  99.     mesh         *newMesh()
  100.     void          freeMesh(mesh *m)
  101.     void          buildMesh(vertex *ps, int n, mesh *m)
  102.  
  103. DT = np.float
  104. ctypedef np.float_t DT_t
  105.  
  106. ########### begin python-available function
  107.  
  108. cdef class interp3d:
  109.  
  110.     cdef int n
  111.     cdef vertex* ps
  112.     cdef mesh* workingMesh
  113.  
  114.     def __init__( self, np.ndarray[DT_t, ndim=1] x, np.ndarray[DT_t, ndim=1] y, np.ndarray[DT_t, ndim=1] z,    \
  115.                          np.ndarray[DT_t, ndim=1] u, np.ndarray[DT_t, ndim=1] v, np.ndarray[DT_t, ndim=1] w):
  116.  
  117.         # {
  118.         assert x.shape == y.shape == z.shape == u.shape == v.shape == w.shape
  119.         self.n = x.size
  120.  
  121.         cdef double *xC = <double *> malloc(self.n * sizeof(double))
  122.         cdef double *yC = <double *> malloc(self.n * sizeof(double))
  123.         cdef double *zC = <double *> malloc(self.n * sizeof(double))
  124.         cdef double *uC = <double *> malloc(self.n * sizeof(double))
  125.         cdef double *vC = <double *> malloc(self.n * sizeof(double))
  126.         cdef double *wC = <double *> malloc(self.n * sizeof(double))
  127.  
  128.  
  129.         cdef unsigned int i
  130.         for i in range(self.n):
  131.             xC[i] = x[i]
  132.         for i in range(self.n):
  133.             yC[i] = y[i]
  134.         for i in range(self.n):
  135.             zC[i] = z[i]
  136.         for i in range(self.n):
  137.             uC[i] = u[i]
  138.         for i in range(self.n):
  139.             vC[i] = v[i]
  140.         for i in range(self.n):
  141.             wC[i] = u[i]
  142.  
  143.  
  144.         self.ps = initPoints(xC, yC, zC, uC, vC, wC, self.n)
  145.         self.workingMesh = newMesh()
  146.         buildMesh(self.ps,self.n, self.workingMesh)
  147.         # }
  148.  
  149.     cpdef np.ndarray[DT_t, ndim=1] interpolatePoint(self, double x, double y, double z):
  150.  
  151.         # {
  152.         cdef double uOut
  153.         cdef double vOut
  154.         cdef double wOut
  155.  
  156.         interpolate3_3(x, y, z, &uOut, &vOut, &wOut, self.workingMesh)
  157.  
  158.         cdef np.ndarray[DT_t, ndim=1] returnArray
  159.         returnArray = np.array([ uOut, vOut, wOut ])
  160.         return returnArray
  161.         # }
  162.  
  163.     def clearData(self):
  164.  
  165.         # {
  166.         freeMesh(self.workingMesh)
  167.         free(self.ps)
  168.         free(&self.n)
  169.         # }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement