def coords_c(shape, coeffs_x, coeffs_y): #pixel_x = 0.5*nx*(x + 1) ny, nx = shape x = np.linspace(-1,1,nx) y = np.linspace(-1,1,ny) offset_x = C.chebgrid2d(x,y,coeffs_x) offset_y = C.chebgrid2d(x,y,coeffs_y) zero_offset = fill_coords2(shape, 0, 0) #zero_offset = np.zeros((2,) + shape) coords = zero_offset + np.array([offset_y, offset_x]) return coords
def coords_c0(shape, x): #pixel_x = 0.5*nx*(x + 1) poly_deg = int(len(x)/2)**0.5 coeffs_x = x[:len(x)/2].reshape((poly_deg,poly_deg)) coeffs_y = x[len(x)/2:].reshape((poly_deg,poly_deg)) ny, nx = shape x = np.linspace(-1,1,nx) y = np.linspace(-1,1,ny) offset_x = C.chebgrid2d(x,y,coeffs_x) offset_y = C.chebgrid2d(x,y,coeffs_y) coords = np.array([offset_y, offset_x]) return coords
def test_chebgrid2d(self): x1, x2, x3 = self.x y1, y2, y3 = self.y #test values tgt = np.einsum('i,j->ij', y1, y2) res = cheb.chebgrid2d(x1, x2, self.c2d) assert_almost_equal(res, tgt) #test shape z = np.ones((2, 3)) res = cheb.chebgrid2d(z, z, self.c2d) assert_(res.shape == (2, 3) * 2)
def test_chebgrid2d(self): x1, x2, x3 = self.x y1, y2, y3 = self.y #test values tgt = np.einsum('i,j->ij', y1, y2) res = cheb.chebgrid2d(x1, x2, self.c2d) assert_almost_equal(res, tgt) #test shape z = np.ones((2,3)) res = cheb.chebgrid2d(z, z, self.c2d) assert_(res.shape == (2, 3)*2)
def warp_cheb(shape, params, offset=True): ny, nx = shape params = np.array(params) poly_deg = int(len(params)/2)**0.5 coeffs_x = params[:len(params)/2].reshape((poly_deg,poly_deg)) coeffs_y = params[len(params)/2:].reshape((poly_deg,poly_deg)) x = np.linspace(-1,1,nx) y = np.linspace(-1,1,ny) offset_x = C.chebgrid2d(x,y,coeffs_x) offset_y = C.chebgrid2d(x,y,coeffs_y) if offset: zero_offset = warp(shape, [0,0]) coords = zero_offset + np.array([offset_y, offset_x]) else: coords = np.array([offset_y, offset_x]) return coords
def warp_cheb(shape, params, offset=True): ny, nx = shape params = np.array(params) poly_deg = int(len(params) / 2)**0.5 coeffs_x = params[:len(params) / 2].reshape((poly_deg, poly_deg)) coeffs_y = params[len(params) / 2:].reshape((poly_deg, poly_deg)) x = np.linspace(-1, 1, nx) y = np.linspace(-1, 1, ny) offset_x = C.chebgrid2d(x, y, coeffs_x) offset_y = C.chebgrid2d(x, y, coeffs_y) if offset: zero_offset = warp(shape, [0, 0]) coords = zero_offset + np.array([offset_y, offset_x]) else: coords = np.array([offset_y, offset_x]) return coords
def chebvals(shape, degree): """2d grid of chebyshev polynomials""" nd = degree+1 vals = np.zeros(shape + (nd**2,)) coeffs = np.zeros((nd,nd)) x = np.linspace(-1,1, shape[1]) y = np.linspace(-1,1, shape[0]) for i,j in itertools.product(range(degree+1), repeat=2): idx = nd*i + j coeffs[i,j] = 1.0 vals[...,idx] = C.chebgrid2d(x,y,coeffs) coeffs[i,j] = 0.0 return vals
def warpim(self, im, coeffs): xoff = np.arange(im.shape[1]) yoff = np.arange(im.shape[0]) offset = np.array(np.meshgrid(xoff, yoff)[::-1]) c = np.array(coeffs) nc = self.ncoeffs pd = self.poly_deg coeffs_x = c[:nc / 2].reshape((pd, pd)) coeffs_y = c[nc / 2:].reshape((pd, pd)) ny, nx = im.shape x = np.linspace(-1, 1, nx) y = np.linspace(-1, 1, ny) disp_x = C.chebgrid2d(x, y, coeffs_x) disp_y = C.chebgrid2d(x, y, coeffs_y) coords = offset + np.array([disp_y, disp_x]) im2 = map_coordinates(im, coords, order=1) return im2
def chebvals(shape, degree): """2d grid of chebyshev polynomials""" nd = degree + 1 vals = np.zeros(shape + (nd**2, )) coeffs = np.zeros((nd, nd)) x = np.linspace(-1, 1, shape[1]) y = np.linspace(-1, 1, shape[0]) for i, j in itertools.product(range(degree + 1), repeat=2): idx = nd * i + j coeffs[i, j] = 1.0 vals[..., idx] = C.chebgrid2d(x, y, coeffs) coeffs[i, j] = 0.0 return vals
# make group for Freestyle bpy.ops.object.select_all(action='DESELECT') scene.objects.active = obj obj.select = True bpy.ops.group.create(name='face_group') ################################################ # Add untrimmed surface step = 20 niso = 6 m = step * niso + 1 u = numpy.linspace(-1.0, 1.0, m) c = myl.readCoeffs(pthin + 'init/coef/c_' + strf + '.cheb') cs, ct = lcheb.diff2(c) surf = myl.addTensorProductPatch(chebgrid2d(u, u, c[:, :, 0]), chebgrid2d(u, u, c[:, :, 1]), chebgrid2d(u, u, c[:, :, 2]), name="surf", location=[0, 0, 0], smooth=True, color=clf[iface], alpha=1) # UV coordinates u = 0.5 * (u + 1.0) bpy.ops.object.select_all(action='DESELECT') scene.objects.active = surf surf.select = True bpy.ops.group.create(name='surf_group')
smooth=True, color=[1,1,1], alpha=1, emit=0.0) """ ### uv0 = numpy.zeros(2) uv1 = numpy.zeros(2) for jchild in range(2): uv0[1] = -1.0 + jchild uv1[1] = jchild for ichild in range(2): uv0[0] = -1.0 + ichild uv1[0] = ichild s = cheb.chgvar2(c, uv0, uv1) xyz = chebgrid2d(u, u, s) myl.addTensorProductPatch(xyz[0], xyz[1], xyz[2], name='sub_'+str(ichild)+'_'+str(jchild), periodu=False, periodv=False, location=[0,0,0], smooth=True, color=cl[2*jchild + ichild], alpha=1, emit=0.0) center, ranges, axes = cheb.obb_chebyshev2(s, AABB) obb = lbu.obb_to_mesh(center, ranges, axes) obb.name = 'OBB'+str(1+2*jchild + ichild)
mat.specular_intensity = 0 mat.specular_hardness = 30 #xyz_facelabel = [] normals = [] xyz_facecenter = [] 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') cu, cv = lcheb.diff2(c) xyz_u = chebgrid2d(Vuv[iloc][0], Vuv[iloc][1], cu) xyz_v = chebgrid2d(Vuv[iloc][0], Vuv[iloc][1], cv) normals.append(Vector(xyz_u).cross(Vector(xyz_v)).normalized()) xyz_facecenter.append(chebval2d(0, 0, c)) normal_avg = Vector((0, 0, 0)) for nor in normals: normal_avg += nor normal_avg.normalize() ################################################################ ################################################################ # ADJUST CAMERA # align with average normal at vertex cam.rotation_mode = 'QUATERNION' cam.rotation_quaternion = Vector(normal_avg).to_track_quat('Z', 'Y')
from numpy.polynomial.chebyshev import chebval, chebgrid2d, chebval2d import sys sys.path.append(ROOT + 'GitHub/Code/Python/') 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
m = 100 u = numpy.linspace(-1, 1, m) 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)
f.close() ################################################################ ################################################################ # EDGE PSEUDO-EoS m = 200 n = 50 u = numpy.linspace(-1.0, 1.0, m) v = numpy.linspace(-1.0, 1.0, n) c = lcheb.read_polynomial2(pthin + 'debug/eos_edge_c_' + format(iedge, '03') + '.cheb') c = lcheb.flip_polynomial2(c, flip_u=True, flip_v=True) cu, cv = lcheb.diff2(c) xyz = chebgrid2d(u, v, c) verts, faces = lbu.tensor_product_mesh_vf(xyz[0], xyz[1], xyz[2]) pseudoEdS = lbu.pydata_to_mesh(verts, faces, edges=None, name='pseudoEdS') # set smooth shading lbe.set_smooth(pseudoEdS) # set material mat_pseudoEdS = bpy.data.materials.new('mat_pseudoEdS') mat_pseudoEdS.diffuse_color = numpy.array([137, 126, 218 ]) / 255. #(0.603, 0.488, 0.8) pseudoEdS.data.materials.append(mat_pseudoEdS) ################################################################ ################################################################ # ADJUST CAMERA
ucgl = lcheb.cgl_nodes(3) 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)