def query(node, query_string, required="no"): " required = 'no/=0/=1/>0'. Returns list of c_void_p." rnum = ct.c_int(0) rans = ct.POINTER(ct.c_void_p)() query_string = query_string.encode('utf-8') ccall(cport.hmxml_query, node, query_string, ct.byref(rnum), ct.byref(rans)) try: if required not in ['no', '>0', '=1', '=0']: raise if required == '>0' and rnum.value == 0: raise if required == '=1' and rnum.value != 1: raise if required == '=0' and rnum.value != 0: raise except: for i in range(rnum.value): ccall(cport.hmxml_free_node, rans[i]) raise Exception("Improper number of entries (=%i) for %s" % (rnum.value, query_string)) ret = [] for i in range(rnum.value): ret.append(ct.c_void_p(rans[i])) free_cside_array(rans, 'void*') return ret
def decompose_contour(obj): ret = ct.POINTER(ct.c_void_p)() nret = ct.c_void_p() ccall(cport.c2_decompose, obj, ct.byref(nret), ct.byref(ret)) a = [ret[i] for i in range(nret.value)] free_cside_array(ret, 'void*') return a
def to_hm(doc, node, obj, name, fmt, afields, cb=None): name = name.encode('utf-8') naf = ct.c_int(len(afields)) af = (ct.c_char_p * len(afields))() for i in range(len(afields)): af[i] = afields[i].encode('utf-8') ccall(cport.g2_to_hm, doc, node, obj, name, fmt, naf, af)
def contour_partition(obj, step, algo, a0, keepbnd, nedges, crosses, keeppts, start, end): """ step: [double]*n, where n dependes on algo: algo = "const" => n = 1, algo = "ref_points" => n = 3*k algo = "ref_weights/lengths" => n = 2*k nedges: None or forced number of resulting edges """ # options nstep = ct.c_int(len(step)) step = list_to_c(step, "float") a0 = ct.c_double(a0) keepbnd = ct.c_int(keepbnd) nedges = ct.c_int(nedges) if nedges is not None else ct.c_int(-1) ncrosses = ct.c_int(len(crosses)) crosses = list_to_c(crosses, "void*") nkeeppts = ct.c_int(len(keeppts)) keeppts = list_to_c(concat(keeppts), float) start = list_to_c(start, float) if start is not None else None end = list_to_c(end, float) if end is not None else None # call ret = ct.c_void_p() ccall(cport.c2_partition, obj, algo, nstep, step, a0, keepbnd, nedges, ncrosses, crosses, nkeeppts, keeppts, start, end, ct.byref(ret)) return ret
def closest_points(obj, pts, proj): npts = ct.c_int(len(pts)) pts = list_to_c(concat(pts), 'float') ret = (ct.c_double * len(pts))() ccall(cport.c2_closest_points, obj, npts, pts, proj, ret) it = iter(ret) return [[a, b] for a, b in zip(it, it)]
def unstructed_fill(domain, constraint, embpts, filler): nembpts = ct.c_int(len(embpts) / 3) embpts = list_to_c(embpts, float) ret = ct.c_void_p() ccall(cport.g2_unstructured_fill, domain, constraint, nembpts, embpts, filler, ct.byref(ret)) return ret
def skewness(obj, threshold): """ reports skewness of the grid -> {'max_skew': float, 'max_skew_cell': int, 'bad_cells': [cell indicies: int], 'bad_skew': [cell skew: float]} returns {} if error """ threshold = ct.c_double(threshold) maxskew = ct.c_double() maxskewindex = ct.c_int() badnum = ct.c_int() badindex = ct.POINTER(ct.c_int)() badvals = ct.POINTER(ct.c_double)() ccall(cport.g2_skewness, obj, threshold, ct.byref(maxskew), ct.byref(maxskewindex), ct.byref(badnum), ct.byref(badindex), ct.byref(badvals)) ret = {} ret['max_skew'] = maxskew.value ret['max_skew_cell'] = maxskewindex.value ret['bad_cells'] = badindex[:badnum.value] ret['bad_skew'] = badvals[:badnum.value] free_cside_array(badindex, int) free_cside_array(badvals, float) return ret
def build_tri_grid(verts, nedge, bnds): verts = list_to_c(concat(verts), float) nedge = ct.c_int(nedge) bnds = list_to_c(supplement(bnds, 3), int) ret = ct.c_void_p() ccall(cport.g2_tri_grid, verts, nedge, bnds, ct.byref(ret)) return ret
def spline(pts, bnds, nedges): bnds = list_to_c(supplement(bnds, len(pts)), int) npts = ct.c_int(len(pts)) pts = list_to_c(concat(pts), float) nedges = ct.c_int(nedges) ret = ct.c_void_p() ccall(cport.c2_spline, npts, pts, nedges, bnds, ct.byref(ret)) return ret
def build_from_points(pts, force_closed, bnds): bnds = list_to_c(supplement(bnds, len(pts)), int) force_closed = ct.c_int(force_closed) npts = ct.c_int(len(pts)) pts = list_to_c(concat(pts), float) ret = ct.c_void_p() ccall(cport.c2_frompoints, npts, pts, bnds, force_closed, ct.byref(ret)) return ret
def snap_to_contour(g, c, p1, p2, p3, p4, algo): p1 = list_to_c(p1, float) p2 = list_to_c(p2, float) p3 = list_to_c(p3, float) p4 = list_to_c(p4, float) ret = ct.c_void_p() ccall(cport.g2_snap_to_contour, g, c, p1, p2, p3, p4, algo, ct.byref(ret)) return ret
def regular_hex_grid(area, crad, strict): area = list_to_c(area, float) crad = ct.c_double(crad) areatype = "hex" if len(area) == 3 else "rect" strict = ct.c_int(strict) ret = ct.c_void_p() ccall(cport.g2_hex_grid, areatype, area, crad, strict, ct.byref(ret)) return ret
def purged_string(doc): """ returns string representing hmxml document without CONTOUR2D, GRID2D, GRID3D elements """ ret = ct.c_char_p() ccall(cport.hmxml_purged_string, doc, ct.byref(ret)) out = str(ret.value) free_cside_array(ret, "char") return out
def build_rect_grid(xdata, ydata, bnds): nx = ct.c_int(len(xdata)) xdata = list_to_c(xdata, float) ny = ct.c_int(len(ydata)) ydata = list_to_c(ydata, float) bnds = list_to_c(supplement(bnds, 4), 'int') ret = ct.c_void_p() ccall(cport.g2_rect_grid, nx, xdata, ny, ydata, bnds, ct.byref(ret)) return ret
def quick_separate(obj): nret = ct.c_int(0) ret = ct.POINTER(ct.c_void_p)() ccall(cport.s3_quick_separate, obj, ct.byref(nret), ct.byref(ret)) ret2 = [] for i in range(nret.value): ret2.append(ret[i]) free_cside_array(ret, 'void*') return ret2
def circ4grid(algo, p0, rad, step, sqrside, rcoef): p0 = list_to_c(p0, float) rad = ct.c_double(rad) step = ct.c_double(step) sqrside = ct.c_double(sqrside) rcoef = ct.c_double(rcoef) ret = ct.c_void_p() ccall(cport.g2_circ4grid, algo, p0, rad, step, sqrside, rcoef, ct.byref(ret)) return ret
def build_ring_grid(p0, rdata, adata, bnds): p0 = list_to_c(p0, float) nr = ct.c_int(len(rdata)) rdata = list_to_c(rdata, float) na = ct.c_int(len(adata)) adata = list_to_c(adata, float) bnds = list_to_c(supplement(bnds, 2), int) ret = ct.c_void_p() ccall(cport.g2_ring_grid, p0, nr, rdata, na, adata, bnds, ct.byref(ret)) return ret
def connect_subcontours(objs, fx, close, shift): nobjs = ct.c_int(len(objs)) objs = list_to_c(objs, "void*") nfx = ct.c_int(len(fx)) fx = list_to_c(fx, int) shift = ct.c_int(shift) ret = ct.c_void_p() ccall(cport.c2_connect_subcontours, nobjs, objs, nfx, fx, shift, close, ct.byref(ret)) return ret
def revolve(g2obj, vec, phivals, center_tri, bt1, bt2): vec = list_to_c(concat(vec), float) nphivals = ct.c_int(len(phivals)) phivals = list_to_c(phivals, float) center_tri = ct.c_int(center_tri) bt1 = ct.c_int(bt1) bt2 = ct.c_int(bt2) ret = ct.c_void_p() ccall(cport.g3_revolve, g2obj, vec, nphivals, phivals, center_tri, bt1, bt2, ct.byref(ret)) return ret
def quick_separate(obj): ret_nconts = ct.c_int(0) ret_conts = ct.POINTER(ct.c_void_p)() ccall(cport.c2_quick_separate, obj, ct.byref(ret_nconts), ct.byref(ret_conts)) ret = [] for i in range(ret_nconts.value): ret.append(ret_conts[i]) free_cside_array(ret_conts, 'void*') return ret
def extrude(g2obj, zvals, bbot, btop, bside=None): d2 = g2.dims(g2obj) nzvals = ct.c_int(len(zvals)) zvals = list_to_c(zvals, float) bbot = list_to_c(supplement(bbot, d2[2]), int) btop = list_to_c(supplement(btop, d2[2]), int) bside = ct.c_int(bside if bside is not None else -1) ret = ct.c_void_p() ccall(cport.g3_extrude, g2obj, nzvals, zvals, bbot, btop, bside, ct.byref(ret)) return ret
def build_circ_grid(p0, rdata, adata, istrian, bnd): p0 = list_to_c(p0, float) nr = ct.c_int(len(rdata)) rdata = list_to_c(rdata, float) na = ct.c_int(len(adata)) adata = list_to_c(adata, float) istrian = ct.c_int(istrian) bnd = ct.c_int(bnd) ret = ct.c_void_p() ccall(cport.g2_circ_grid, p0, nr, rdata, na, adata, istrian, bnd, ct.byref(ret)) return ret
def contour_type(obj): ret = ct.c_int() ccall(cport.c2_contour_type, obj, ct.byref(ret)) ret = ret.value if ret == 0: return 'open' elif ret == 1: return 'closed' elif ret == 2: return 'mdomain' else: return 'compound'
def from_points_edges(points, eds): """ eds = [[p0, p1, cleft, cright, btype], ....] """ npoints = ct.c_int(len(points)) points = list_to_c(concat(points), float) neds = ct.c_int(len(eds)) eds = list_to_c(concat(eds), int) ret = ct.c_void_p() ccall(cport.g2_from_points_edges, npoints, points, neds, eds, ct.byref(ret)) return ret
def closest_points(obj, pts, proj): npts = ct.c_int(len(pts)) pts = list_to_c(concat(pts), 'float') ret = (ct.c_double * len(pts))() if proj == "vertex": proj = ct.c_int(0) elif proj == "edge": proj = ct.c_int(1) else: raise ValueError ccall(cport.g2_closest_points, obj, npts, pts, proj, ret) it = iter(ret) return [[a, b] for a, b in zip(it, it)]
def segment_partition(start, end, hstart, hend, hinternal): start = ct.c_double(start) end = ct.c_double(end) hstart = ct.c_double(hstart) hend = ct.c_double(hend) ninternal = ct.c_int(len(hinternal) / 2) hinternal = list_to_c(hinternal, float) nret = ct.c_int() ret = ct.POINTER(ct.c_double)() ccall(cport.c2_segment_partition, start, end, hstart, hend, ninternal, hinternal, ct.byref(nret), ct.byref(ret)) r = ret[:nret.value] free_cside_array(ret, float) return r
def matched_partition(obj, conts, pts, step, infdist, power, a0): nconts = ct.c_int(len(conts)) conts = list_to_c(conts, "void*") npts = ct.c_int(len(pts) / 3) pts = list_to_c(pts, float) step = ct.c_double(step) infdist = ct.c_double(infdist) power = ct.c_double(power) a0 = ct.c_double(a0) ret = ct.c_void_p() ccall(cport.c2_matched_partition, obj, nconts, conts, npts, pts, step, infdist, power, a0, ct.byref(ret)) return ret
def from_points_cells(points, cls, bedges): """ cls = [[p1, p2, p3, ...], [p1, p2, ....]] bedges: [[p1, p2, btype], ...] """ npoints = ct.c_int(len(points)) points = list_to_c(concat(points), float) ncells = ct.c_int(len(cls)) cellsizes = list_to_c(map(len, cls), int) cellvert = list_to_c(concat(cls), int) nbedges = ct.c_int(len(bedges)) bedges = list_to_c(concat(bedges), int) ret = ct.c_void_p() ccall(cport.g2_from_points_cells, npoints, points, ncells, cellsizes, cellvert, nbedges, bedges, ct.byref(ret)) return ret
def raw_data(obj, what): ret = None d = dims(obj) if what == 'vert': ret = (ct.c_double * (2*d[0]))() ccall(cport.c2_tab_vertices, obj, ret) elif what == 'edge_vert': ret = (ct.c_int * (2*d[1]))() ccall(cport.c2_tab_edgevert, obj, ret) elif what == 'bt': ret = (ct.c_int * d[1])() ccall(cport.c2_tab_btypes, obj, ret) else: raise Exception('unknown what: %s' % repr(what)) return ret
def unite_contours(objs): nconts = ct.c_int(len(objs)) objs = list_to_c(objs, 'void*') ret_cont = ct.c_void_p(0) ccall(cport.c2_unite, nconts, objs, ct.byref(ret_cont)) return ret_cont