def extract_selected_submesh(obj): # switch to edit mode bpy.ops.object.mode_set(mode='EDIT') me = obj.data # Get a BMesh representation bm = bmesh.from_edit_mesh(me) # get list of selected faces listfaces = [f for f in bm.faces if f.select] # re-order vertices of the selected submesh and extract local connectivity verts = {} # dictionary: global vertex index -> local vertex index xyz = [] # xyz coordinates of the submesh's vertices faces = [] # connectivity table of the submesh's faces (local indices) for f in listfaces: fv = [] for v in f.verts: if v.index not in verts: verts[v.index] = len(verts) xyz.append(list(v.co)) fv.append(verts[v.index]) faces.append(fv) # leave edit mode bpy.ops.object.mode_set(mode='OBJECT') # clone submesh in a separate object subobj = lbu.pydata_to_mesh(verts, faces, name=obj.name + "_submesh") subobj.location = obj.location subobj.rotation_euler = obj.rotation_euler subobj.scale = obj.scale bm.free() return subobj
obj.select = True bpy.ops.view3d.camera_to_view_selected() # set camera to fit surf in screen cam.data.angle += numpy.pi / 180.0 # increase FOV angle by 1 degree bpy.ops.object.delete() ################################################# clf = numpy.loadtxt(pthout + 'face_color.dat') clf = lco.cc_hsv(clf, fs=1.2, fv=1.0) tri = numpy.loadtxt(pthin + 'brepmesh/tri_' + strf + '.dat', dtype=int) - 1 xyz = numpy.loadtxt(pthin + 'brepmesh/xyz_' + strf + '.dat', dtype=float) verts = [[x for x in p] for p in xyz] faces = [[int(v) for v in t] for t in tri] obj = lbu.pydata_to_mesh(verts, faces, name='face') lbe.set_smooth(obj) # material mat = bpy.data.materials.new('mat_face') mat.diffuse_color = clf[iface] mat.diffuse_intensity = 1 mat.specular_intensity = 0 mat.specular_hardness = 30 mat.use_transparency = False obj.data.materials.append(mat) # make group for Freestyle bpy.ops.object.select_all(action='DESELECT') scene.objects.active = obj
g = Bxyz.eval(u) dg = Bxyz.evald(u) d2g = Bxyz.evald2(u) r = Br.eval(u)[:, 1] dr = Br.evald(u)[:, 1] e = canal_surface(g.T, dg.T, d2g.T, r, dr, v) verts, faces = lbu.tensor_product_mesh_vf(e[:, :, 0], e[:, :, 1], e[:, :, 2], periodv=True) EdS = lbu.pydata_to_mesh(verts, faces, edges=None, name='EdSpropre') lbe.set_smooth(EdS) #EdS.show_wire = True #EdS.show_all_edges = True EdS.show_transparent = True mat_EdS = bpy.data.materials.new('mat_EdS') mat_EdS.diffuse_color = (0.527, 0.800, 0.213) mat_EdS.diffuse_intensity = 1 mat_EdS.use_transparency = True mat_EdS.alpha = 0.68 mat_EdS.emit = 1.0 mat_EdS.specular_intensity = 0.5 mat_EdS.specular_hardness = 30 mat_EdS.raytrace_transparency.fresnel = 2.7
import lib_blender_util as lbu import lib_blender_edit as lbe import lib_cadcheb as lcad import lib_chebyshev as lch lbu.clear_scene(True, True, True) cs = lch.read_polynomial2(ROOT+'GitHub/FFTsurf/test/coeffstest/C2_test10.txt') m = 100 u = numpy.linspace(-1, 1, m) xyz = chebgrid2d(u, u, cs) v, f = lbu.tensor_product_mesh_vf(xyz[0], xyz[1], xyz[2]) surf = lbu.pydata_to_mesh( v, f, name='surface' ) lbe.set_smooth(surf) hmin = 1e-3 hmax = 1 tolchord = 1e-3 N = 6 cc = (2*numpy.random.rand(N,2) - 1)/numpy.tile(numpy.arange(1,N+1)**2, (2,1)).T cc[0,:] = 0 cc = 3*cc xyz, uv, t = lcad.discretize_curve_on_surface( cc,
] lon = [ numpy.arctan2(xyz.dot(B[:, 1]), xyz.dot(B[:, 0])) / numpy.pi for xyz in arc ] lat = [ numpy.arcsin(xyz.dot(B[:, 2]) / xyz.length) / numpy.pi for xyz in arc ] for j in range(ncc): print(ui[j]) print(arc[j]) print(j + 1, lon[j], lat[j]) print('\n\n') scene.layers[ilayer] = True scene.layers[ilayer - 1] = False obj = lbu.pydata_to_mesh(verts=[(lon[j], lat[j], 0) for j in range(ncc)], faces=[], edges=edges, name='lonlat_arc_' + str(i)) box = lbu.pydata_to_mesh(verts=box_verts, faces=[], edges=box_edges, name='lonlat_box_' + str(i)) box.location.x = 0.5 * (minmax_lon[0] + minmax_lon[1]) box.location.y = 0.5 * (minmax_lat[0] + minmax_lat[1]) box.scale.x = 0.5 * (minmax_lon[1] - minmax_lon[0]) box.scale.y = 0.5 * (minmax_lat[1] - minmax_lat[0]) ################################################################
def LL_trimmed_patch(planes, corners, center, degr=16, name='trimmed_LLpatch', tol_chord=1e-3): # path_tmp = pthout + 'meshgen/' # # ravg, B, ctr_tl, rng_tl = LL_patch_from_arcs(planes, corners, center, nsample=10) # uc = ctr_tl[0] + lcheb.cgl_nodes(degr) * rng_tl[0] vc = ctr_tl[1] + lcheb.cgl_nodes(degr) * rng_tl[1] xc = numpy.outer(numpy.cos(uc), numpy.cos(vc)) yc = numpy.outer(numpy.sin(uc), numpy.cos(vc)) zc = numpy.outer(numpy.ones(len(uc)), numpy.sin(vc)) xyzc = numpy.zeros((3, degr + 1, degr + 1)) xyzc[0] = center[0] + ravg * (xc * B[0, 0] + yc * B[0, 1] + zc * B[0, 2]) xyzc[1] = center[1] + ravg * (xc * B[1, 0] + yc * B[1, 1] + zc * B[1, 2]) xyzc[2] = center[2] + ravg * (xc * B[2, 0] + yc * B[2, 1] + zc * B[2, 2]) c = numpy.zeros((degr + 1, degr + 1, 3)) for k in range(3): c[:, :, k] = lcheb.fcht(lcheb.fcht(xyzc[k]).T).T lcheb.write_polynomial2(c, path_tmp + 'c.cheb') # frac = 0.5 / numpy.sqrt(tol_chord * (2 - tol_chord)) xyz = [] uv = [] m = len(planes) for i, (tng, occ) in enumerate(planes): ci = corners[i] cj = corners[(i - 1) % m] # r1 = cj - occ r2 = ci - occ ri = 0.5 * (Vector(r1).length + Vector(r2).length) r1perp = numpy.cross(r1, tng) a = numpy.arctan2(r2.dot(r1perp), r2.dot(r1)) % (2 * numpy.pi) # ni = max(2, int(frac * a)) v = numpy.linspace(0, a, ni) for j in range(ni - 1): xyzj = occ + r1 * numpy.cos(v[j]) + r1perp * numpy.sin(v[j]) sj = xyzj - center lon = numpy.arctan2(sj.dot(B[:, 1]), sj.dot(B[:, 0])) lat = numpy.arcsin(sj.dot(B[:, 2]) / numpy.sqrt(numpy.sum(sj**2))) xyz.append(xyzj) uv.append([(lon - ctr_tl[0]) / rng_tl[0], (lat - ctr_tl[1]) / rng_tl[1]]) n = len(uv) edg = numpy.asarray([(i, (i + 1) % n) for i in range(n)]) #h0 = frac*ravg h0 = 0 for e in edg: h0 += Vector(xyz[e[0]] - xyz[e[1]]).length h0 /= float(len(edg)) hmin = 0.9 * h0 hmax = 1.1 * h0 f = open(path_tmp + 'info.dat', 'w') f.write('%s\n%s\n%s' % (hmin, hmax, tol_chord)) f.close() numpy.savetxt(path_tmp + 'bpts.dat', uv) numpy.savetxt(path_tmp + 'bedg.dat', edg + 1, fmt='%d') # cmd = '/home/bastien/MeshGen/./meshgen.out ' cmd += path_tmp + 'c.cheb ' cmd += path_tmp + 'bpts.dat ' cmd += path_tmp + 'bedg.dat ' cmd += path_tmp + 'info.dat ' cmd += path_tmp + 'tri.dat ' cmd += path_tmp + 'uv.dat ' cmd += path_tmp + 'xyz.dat ' print('Run meshgen...') os.system(cmd) print('ok!') # tri = numpy.loadtxt(path_tmp + 'tri.dat', dtype=int) - 1 xyz = numpy.loadtxt(path_tmp + 'xyz.dat') uv = numpy.loadtxt(path_tmp + 'uv.dat') obj = lbu.pydata_to_mesh(verts=xyz.tolist(), faces=tri.tolist(), edges=None, name=name) return (obj, uv, ravg, B, ctr_tl, rng_tl)
def LL_patch_from_corners(arcs, center, mrg=0, construction_steps=False, critere='area'): m = len(arcs) # normalize corners onto unit sphere s = numpy.array([arc[0] - center for arc in arcs]) r = numpy.sqrt(numpy.sum(s**2, axis=1)) ravg = numpy.sum(r) / float(m) s = s / numpy.tile(r, (3, 1)).T # orthonormal basis R = complete_orthonormal_matrix(numpy.sum(s, axis=0), i=0).T # central projection onto plane tangent to unit sphere at point r1 = R[:,0] s_dot_r1 = s[:, 0] * R[0, 0] + s[:, 1] * R[1, 0] + s[:, 2] * R[2, 0] s_dot_r1 = numpy.sign(s_dot_r1) * numpy.maximum(1e-6, numpy.absolute(s_dot_r1)) inv_s_dot_r1 = 1. / s_dot_r1 p = s * numpy.tile(inv_s_dot_r1, (3, 1)).T # coordinates in local frame (r2, r3) ab = lib_linalg.matmul(p, R[:, 1:3]) if construction_steps: abverts = [(a, b, 0) for a, b in ab] obj = lbu.pydata_to_mesh(verts=abverts, faces=[], edges=[(i, (i + 1) % m) for i in range(m)], name='ab_points') obj.layers[3] = True obj.layers[0] = False # mimimum-area OBB ctr_ab, rng_ab, axes_ab = minimal_OBB(ab) if construction_steps: OBBverts = [((-1)**(i + 1), (-1)**(j + 1), 0) for j in range(2) for i in range(2)] OBBedges = [(0, 1), (1, 3), (3, 2), (2, 0)] obj = lbu.pydata_to_mesh(verts=OBBverts, faces=[], edges=OBBedges, name='ab_OBB') obj.layers[3] = True obj.layers[0] = False obj.scale = (rng_ab[0], rng_ab[1], 1) obj.location = (ctr_ab[0], ctr_ab[1], 0) obj.rotation_euler[2] = numpy.arctan2(axes_ab[1, 0], axes_ab[0, 0]) R[:, 1:3] = lib_linalg.matmul(R[:, 1:3], axes_ab) # xyz-coords in rotated frame s = numpy.empty((0, 3), dtype=float) for arc in arcs: s = numpy.vstack([ s, lib_linalg.matmul((arc - numpy.tile(center, (len(arc), 1))) / ravg, R) ]) n = len(s) if construction_steps: obj = lbu.pydata_to_mesh(verts=s, faces=[], edges=[(i, (i + 1) % n) for i in range(n)], name='s_points') obj.layers[2] = True obj.layers[0] = False obj.location = center obj.scale = ravg * numpy.ones(3) obj.rotation_euler = Matrix(R).to_euler() # spherical coords: longitude t(heta), latitude l(ambda) tl = numpy.zeros((n, 2)) tl[:, 0] = numpy.arctan2(s[:, 1], s[:, 0]) tl[:, 1] = numpy.arcsin(s[:, 2]) if construction_steps: tlverts = [(t / numpy.pi, l / numpy.pi, 0) for t, l in tl] obj = lbu.pydata_to_mesh(verts=tlverts, faces=[], edges=[(i, (i + 1) % n) for i in range(n)], name='tl_points') obj.layers[4] = True obj.layers[0] = False min_tl = numpy.amin(tl, axis=0) max_tl = numpy.amax(tl, axis=0) ctr_tl = 0.5 * (min_tl + max_tl) print('lambda_0/pi = ', ctr_tl[1] / numpy.pi) rng_tl = (1 + mrg) * 0.5 * (max_tl - min_tl) print('max |lambda|/pi = ', max(abs(ctr_tl[1] - rng_tl[1]), ctr_tl[1] + rng_tl[1]) / numpy.pi) if construction_steps: obj = lbu.pydata_to_mesh(verts=OBBverts, faces=[], edges=OBBedges, name='tl_OBB') obj.layers[4] = True obj.layers[0] = False obj.scale = (rng_tl[0] / numpy.pi, rng_tl[1] / numpy.pi, 1) obj.location = (ctr_tl[0] / numpy.pi, ctr_tl[1] / numpy.pi, 0) # uv-coords uv = (tl - numpy.tile(ctr_tl, (n, 1))) / numpy.tile(rng_tl, (n, 1)) return (ravg, R, ctr_tl, rng_tl, uv)
fclr = open(pthout + 'colors.dat', 'w') for iloc, iface in enumerate(V.faces): strf = format(iface, '03') mat = bpy.data.materials.new('mat_face_' + strf) mat.diffuse_color = color_face[iface - 1] fclr.write('%s, %s, %s\n' % (color_face[iface - 1][0], color_face[iface - 1][1], color_face[iface - 1][2])) c = lcheb.read_polynomial2(pthin + 'brepmesh/c_' + strf + '.cheb') xyz = chebgrid2d(u, u, c) mverts, mfaces = lbu.tensor_product_mesh_vf(xyz[0], xyz[1], xyz[2]) obj = lbu.pydata_to_mesh(mverts, mfaces, name='face_' + strf) lbe.set_smooth(obj) obj.data.materials.append(mat) # OFFSET cu, cv = lcheb.diff2(c) xyz = chebgrid2d(u, u, c) xyz_u = chebgrid2d(u, u, cu) xyz_v = chebgrid2d(u, u, cv) invsqrtdet = 1 / numpy.sqrt( numpy.sum(xyz_u**2, axis=0) * numpy.sum(xyz_v**2, axis=0) - numpy.sum(xyz_u * xyz_v, axis=0)**2) nor = numpy.zeros((3, m, m)) for i in range(3):
nf = len(V.faces) corners = [] for iloc, iface in enumerate(V.faces): strf = format(iface, '03') mat = bpy.data.materials.new('mat_face_' + strf) mat.diffuse_color = color_face[iface - 1] c = lcheb.read_polynomial2(pthin + 'brepmesh/c_' + strf + '.cheb') xyz = chebgrid2d(u, u, c) mverts, mfaces = lbu.tensor_product_mesh_vf(xyz[0], xyz[1], xyz[2]) obj = lbu.pydata_to_mesh(mverts, mfaces, name='face_' + strf) lbe.set_smooth(obj) obj.data.materials.append(mat) # OFFSET c = lcheb.read_polynomial2(pthin + 'brepmesh_eos/c_' + strf + '.cheb') xyz = chebgrid2d(u, u, c) mverts, mfaces = lbu.tensor_product_mesh_vf(xyz[0], xyz[1], xyz[2]) obj = lbu.pydata_to_mesh(mverts, mfaces, name='EdS_face_' + strf) lbe.set_smooth(obj) obj.data.materials.append(mat) # xyz = chebval2d(Vuv[iloc][0], Vuv[iloc][1], c) corners.append(xyz)
################################################# m = 100 u = numpy.linspace(-1, 1, m) v = 0.5 * (u + 1) clr = [(1, 1, 1), (1, 0.5, 0.5)] suff = ['skeleton', 'eos'] for isurf in range(2): tri = numpy.loadtxt(pthin + 'tri' + str(isurf) + '.dat', dtype=int) - 1 xyz = numpy.loadtxt(pthin + 'xyz' + str(isurf) + '.dat', dtype=float) verts = [[x for x in p] for p in xyz] faces = [[int(v) for v in t] for t in tri] obj = lbu.pydata_to_mesh(verts, faces, name='face_' + str(isurf)) mat_face = bpy.data.materials.new('mat_face_' + str(isurf)) mat_face.diffuse_color = clr[isurf] mat_face.diffuse_intensity = 1 mat_face.specular_intensity = 0.2 mat_face.specular_hardness = 30 mat_face.use_transparency = False obj.data.materials.append(mat_face) lbe.set_smooth(obj) c = lcheb.read_polynomial2(pthin + 'c_' + suff[isurf] + '.cheb') xyz = chebgrid2d(u, u, c) verts, faces = lbu.tensor_product_mesh_vf(xyz[0], xyz[1], xyz[2]) surf = lbu.pydata_to_mesh(verts, faces, name='surf_' + str(isurf))
import sys sys.path.append(ROOT + 'GitHub/Code/Python/') import lib_blender_util as lbu import lib_cadcheb as lcad lbu.clear_scene(True, True, True) hmin = 1e-3 hmax = 1 tolchord = 1e-3 N = 8 c = (2 * numpy.random.rand(N, 3) - 1) / numpy.tile( numpy.arange(1, N + 1)**2, (3, 1)).T c[0, :] = 0 c = 10 * c xyz, t = lcad.discretize_curve(c, hmin, hmax, tolchord, n0=20) adap = lbu.pydata_to_mesh(xyz.T, faces=[], edges=[(i, i + 1) for i in range(len(t) - 1)], name='adaptive') u = numpy.linspace(-1, 1, 200) unif = lbu.pydata_to_mesh(chebval(u, c).T, faces=[], edges=[(i, i + 1) for i in range(len(u) - 1)], name='uniform')
################################################################ # LOAD RIGHT/LEFT FACES EoS MESHES ptheos = pthin + 'brepmesh_eos/' facesRL = [] suffixe = 'RL' for j, iface in enumerate(ifaceRL): strf = format(iface, '03') tri = numpy.loadtxt(ptheos + 'tri_' + strf + '.dat', dtype=int) - 1 xyz = numpy.loadtxt(ptheos + 'xyz_' + strf + '.dat', dtype=float) verts = [[x for x in p] for p in xyz] faces = [[int(v) for v in t] for t in tri] obj = lbu.pydata_to_mesh(verts, faces, edges=None, name='eos_face' + suffixe[j]) lbe.set_smooth(obj) # material if j == 0: obj.data.materials.append(mat_faceR) elif j == 1: obj.data.materials.append(mat_faceL) facesRL.append(obj) ################################################################ ################################################################ # LOAD EDGE POLYLINE mat_edge = bpy.data.materials.new('mat_edge')
chebpoly = [] nappes = [] nappes_eos = [] XYZR = [] E = [] for inappe in range(2): # convert Bezier -> Chebyshev surf = bpy.data.objects['SurfPatch'+str(inappe)] cxyz = lbu.bezier_surface_to_chebyshev(surf) chebpoly.append(cxyz) # add mesh xyz = chebgrid2d(u, v, cxyz) verts, faces = lbu.tensor_product_mesh_vf(xyz[0], xyz[1], xyz[2]) obj = lbu.pydata_to_mesh(verts, faces, edges=None, name='nappe'+str(inappe)) lbe.set_smooth(obj) obj.hide_render = True nappes.append(obj) # make envelope of spheres cxyz_u, cxyz_v = lcheb.diff2(cxyz) xyz_u = chebgrid2d(u, v, cxyz_u) xyz_v = chebgrid2d(u, v, cxyz_v) xyzcgl = chebgrid2d(ucgl, ucgl, cxyz) rcgl = radius_function(xyzcgl[0], xyzcgl[1], xyzcgl[2]) cr = lcheb.fcht(lcheb.fcht(rcgl).T).T cr_u, cr_v = lcheb.diff2(cr) r = chebgrid2d(u, v, cr) r_u = chebgrid2d(u, v, cr_u)