def test_split_rect_1(): # two functions from https://stackoverflow.com/a/9997374 def ccw(A,B,C): return (C[1]-A[1]) * (B[0]-A[0]) > (B[1]-A[1]) * (C[0]-A[0]) # Return true if line segments AB and CD intersect def intersect(A,B,C,D): return ccw(A,C,D) != ccw(B,C,D) and ccw(A,B,C) != ccw(A,B,D) mesh = om.read_trimesh('../data/rect.obj') mesh_pts = mesh.points() mesh_edges = mesh.ev_indices() x = np.linspace(-1, 1, 101) y = T.basis(5)(x) # export Chebyshev polynomial pts = np.zeros((x.size, 3)) pts[:, 0] = x pts[:, 1] = y mu.write_obj_lines('../data/curve_Chebyshev.obj', pts, [np.arange(pts.shape[0])]) edges = [np.array([0,0], 'i')] ratios = [0.0] for i in range(1, x.size-2): A = np.array([x[i], y[i]]) B = np.array([x[i+1], y[i+1]]) temp_x = [] temp_e = [] temp_u = [] for e in mesh_edges: C = mesh_pts[e[0], :2] D = mesh_pts[e[1], :2] if intersect(A, B, C, D): # A = (x1, y1), B = (x2, y2) # C = (x3, y3), D = (x4, y4) u = ((A[0]-C[0])*(A[1]-B[1])-(A[1]-C[1])*(A[0]-B[0]))/((A[0]-B[0])*(C[1]-D[1])-(A[1]-B[1])*(C[0]-D[0])) temp_e.append(e) temp_u.append(u) temp_x.append(C[0]*(1-u)+D[0]*u) order = np.argsort(np.array(temp_x)) edges.extend([temp_e[i] for i in order]) ratios.extend([temp_u[i] for i in order]) edges.append(np.array([2,2], 'i')) ratios.append(0.0) edges = np.array(edges, 'i') #print(ratios) ratios = np.array(ratios, 'd') pts0 = mesh_pts[edges[:, 0], :] pts1 = mesh_pts[edges[:, 1], :] pts = np.multiply(pts0, 1.-ratios[:, np.newaxis]) + \ np.multiply(pts1, ratios[:, np.newaxis]) mu.write_obj_lines('../data/curve.obj', pts, [np.arange(pts.shape[0])]) # print(on_edges, ratios) mesh, curve_idx = mu.split_mesh_complete(mesh.points(), mesh.face_vertex_indices(), edges, ratios) # print(curve_idx) om.write_mesh('../data/mesh_split.obj', mesh)
def _create_Tderiv(self, k, n): r"""Create the n'th derivative of the k'th Chebyshev polynomial.""" if not self._use_mp: return ChebyshevT.basis(k).deriv(n) else: if n == 0: return lambda x: mp.chebyt(k, x) # Since the Chebyshev derivatives attain high values near the # borders +-1 and usually are subtracted to obtain small values, # minimizing their relative error (i.e. fixing the number of # correct decimal places (dps)) is not enough to get precise # results. Hence, the below `moredps` variable is set to higher # values close to the border. extradps = 5 pos = mp.fprod( [mp.mpf(k**2 - p**2) / (2 * p + 1) for p in range(n)]) neg = (-1)**(k + n) * pos if n == 1: def dTk(x): with mp.extradps(extradps): x = clip(x, -1.0, 1.0) if mp.almosteq(x, mp.one): return pos if mp.almosteq(x, -mp.one): return neg moredps = max( 0, int(-math.log10( min(abs(x - mp.one), abs(x + mp.one))) / 2)) moredps = min(moredps, 100) with mp.extradps(moredps): t = mp.acos(x) return k * mp.sin(k * t) / mp.sin(t) return dTk if n == 2: def ddTk(x): with mp.extradps(extradps): x = clip(x, -1.0, 1.0) if mp.almosteq(x, mp.one): return pos if mp.almosteq(x, -mp.one): return neg moredps = max( 0, int(-math.log10( min(abs(x - mp.one), abs(x + mp.one))) * 1.5) + 2) moredps = min(moredps, 100) with mp.extradps(moredps): t = mp.acos(x) s = mp.sin(t) return -k**2 * mp.cos(k * t) / s**2 + k * mp.cos( t) * mp.sin(k * t) / s**3 return ddTk raise NotImplementedError( 'Derivatives of order > 2 not implemented.')
import matplotlib.pyplot as plt from numpy.polynomial import Chebyshev as T x = np.linspace(-2, 2, 100) for i in range(6): ax = plt.plot(x, T.basis(i)(x), lw=2, label="T_%d" % i) # ... plt.legend(loc="lower right") # <matplotlib.legend.Legend object at 0x3b3ee10> plt.show()
#figure_width = 0.35*text_width #figure_height = 0.75*figure_width #(figure_width / golden_ratio) #figure_width figure_width = 0.75*text_width figure_height = (figure_width / golden_ratio) #figure_width figure_size = [figure_width, figure_height] config.load_config_medium() #fig = plt.figure(figsize=figure_size, dpi=100) fig, axes = plt.subplots(nrows=1, ncols=1, sharex=True, sharey=True, squeeze=False, figsize=figure_size, dpi=100) ax = axes[0][0] x = np.linspace(-1, 1, 100) for i in range(6): ax.plot(x, T.basis(i)(x), lw=1.0, color=colors[i], label="$t_%d(x)$"%i) #ax.plot(x, pow(x, i), lw=1.0, color=colors[i], label="$x^%d$"%i) ax.set_xlabel(r"$x$") ax.set_ylabel(r"$t_n(x)$", rotation=90) ax.set_xlim(-1.1, 1.1) ax.set_ylim(-1.1, 1.1) ax.xaxis.labelpad = 5 ax.yaxis.labelpad = 2 # Shrink current axis's height by 10% on the bottom #box = ax.get_position() #ax.set_position([box.x0, box.y0 - 0.1*box.height, box.width, 0.9*box.height]) #left, bottom, width, height plt.subplots_adjust(top=0.85, left=0.12, bottom=0.12, right=0.98) # Legend on top
import matplotlib.pyplot as plt from numpy.polynomial import Chebyshev as T x = np.linspace(-1, 1, 100) for i in range(6): ax = plt.plot(x, T.basis(i)(x), lw=2, label="T_%d"%i) # ... plt.legend(loc="upper left") # <matplotlib.legend.Legend object at 0x3b3ee10> plt.show()
def phi(x, i): return T.basis(i)(x)
x = np.linspace(-1, 1, 100) for i in range(5): plt.plot(x, phi(x, i), lw=2) # In[12]: V = np.zeros((len(nodes), deg+1)) rhs = np.zeros(len(nodes)) for i in range(1,len(nodes)-1): # interior nodes for j in range(deg+1): V[i, j] = T.basis(j).deriv(2)(nodes[i]) #V[i, j] += 1000 * (1 + nodes[i]**2) * T.basis(j)(nodes[i]) rhs[i] = f(nodes[i]) for j in range(deg+1): V[0, j] = T.basis(j)(nodes[0]) rhs[0] = 0 V[-1, j] = T.basis(j)(nodes[-1]) rhs[-1] = 0 # In[13]:
figure_size = [figure_width, figure_height] config.load_config_medium() #fig = plt.figure(figsize=figure_size, dpi=100) fig, axes = plt.subplots(nrows=1, ncols=1, sharex=True, sharey=True, squeeze=False, figsize=figure_size, dpi=100) ax = axes[0][0] x = np.linspace(-1, 1, 100) for i in range(6): ax.plot(x, T.basis(i)(x), lw=1.0, color=colors[i], label="$t_%d(x)$" % i) #ax.plot(x, pow(x, i), lw=1.0, color=colors[i], label="$x^%d$"%i) ax.set_xlabel(r"$x$") ax.set_ylabel(r"$t_n(x)$", rotation=90) ax.set_xlim(-1.1, 1.1) ax.set_ylim(-1.1, 1.1) ax.xaxis.labelpad = 5 ax.yaxis.labelpad = 2 # Shrink current axis's height by 10% on the bottom #box = ax.get_position() #ax.set_position([box.x0, box.y0 - 0.1*box.height, box.width, 0.9*box.height]) #left, bottom, width, height plt.subplots_adjust(top=0.85, left=0.12, bottom=0.12, right=0.98) # Legend on top