def test_revolve(self): # square torus square = Surface() + (1, 0) square.rotate( pi / 2, (1, 0, 0)) # in xz-plane with corners at (1,0),(2,0),(2,1),(1,1) vol = VolumeFactory.revolve(square) vol.reparam() # set parametric space to (0,1)^3 u = np.linspace(0, 1, 7) v = np.linspace(0, 1, 7) w = np.linspace(0, 1, 7) x = vol.evaluate(u, v, w) V, U, W = np.meshgrid(v, u, w) R = np.sqrt(x[:, :, :, 0]**2 + x[:, :, :, 1]**2) self.assertEqual(np.allclose(R, U + 1), True) self.assertEqual(np.allclose(x[:, :, :, 2], V), True) self.assertAlmostEqual(vol.volume(), 2 * pi * 1.5, places=3) # test incomplete reolve vol = VolumeFactory.revolve(square, theta=pi / 3) vol.reparam() # set parametric space to (0,1)^3 u = np.linspace(0, 1, 7) v = np.linspace(0, 1, 7) w = np.linspace(0, 1, 7) x = vol.evaluate(u, v, w) V, U, W = np.meshgrid(v, u, w) R = np.sqrt(x[:, :, :, 0]**2 + x[:, :, :, 1]**2) self.assertEqual(np.allclose(R, U + 1), True) self.assertEqual(np.allclose(x[:, :, :, 2], V), True) self.assertAlmostEqual(vol.volume(), 2 * pi * 1.5 / 6, places=3) self.assertTrue(np.all(x >= 0)) # completely contained in first octant # test axis revolve vol = VolumeFactory.revolve(Surface() + (1, 1), theta=pi / 3, axis=(1, 0, 0)) vol.reparam() # set parametric space to (0,1)^3 u = np.linspace(0, 1, 7) v = np.linspace(0, 1, 7) w = np.linspace(0, 1, 7) x = vol.evaluate(u, v, w) V, U, W = np.meshgrid(v, u, w) R = np.sqrt(x[:, :, :, 1]**2 + x[:, :, :, 2]**2) self.assertEqual(np.allclose(R, V + 1), True) self.assertEqual(np.allclose(x[:, :, :, 0], U + 1), True) self.assertTrue(np.all(x >= 0)) # completely contained in first octant
def test_revolve(self): # square torus square = Surface() + (1,0) square.rotate(pi / 2, (1, 0, 0)) # in xz-plane with corners at (1,0),(2,0),(2,1),(1,1) vol = VolumeFactory.revolve(square) vol.reparam() # set parametric space to (0,1)^3 u = np.linspace(0, 1, 7) v = np.linspace(0, 1, 7) w = np.linspace(0, 1, 7) x = vol.evaluate(u,v,w) V,U,W = np.meshgrid(v,u,w) R = np.sqrt(x[:,:,:,0]**2 + x[:,:,:,1]**2) self.assertEqual(np.allclose(R, U+1), True) self.assertEqual(np.allclose(x[:,:,:,2], V), True) self.assertAlmostEqual(vol.volume(), 2*pi*1.5, places=3)
def test_revolve(self): # square torus square = Surface() + (1,0) square.rotate(pi / 2, (1, 0, 0)) # in xz-plane with corners at (1,0),(2,0),(2,1),(1,1) vol = VolumeFactory.revolve(square) vol.reparam() # set parametric space to (0,1)^3 u = np.linspace(0, 1, 7) v = np.linspace(0, 1, 7) w = np.linspace(0, 1, 7) x = vol.evaluate(u,v,w) V,U,W = np.meshgrid(v,u,w) R = np.sqrt(x[:,:,:,0]**2 + x[:,:,:,1]**2) self.assertEqual(np.allclose(R, U+1), True) self.assertEqual(np.allclose(x[:,:,:,2], V), True) self.assertAlmostEqual(vol.volume(), 2*pi*1.5, places=3) # test incomplete reolve vol = VolumeFactory.revolve(square, theta=pi/3) vol.reparam() # set parametric space to (0,1)^3 u = np.linspace(0, 1, 7) v = np.linspace(0, 1, 7) w = np.linspace(0, 1, 7) x = vol.evaluate(u,v,w) V,U,W = np.meshgrid(v,u,w) R = np.sqrt(x[:,:,:,0]**2 + x[:,:,:,1]**2) self.assertEqual(np.allclose(R, U+1), True) self.assertEqual(np.allclose(x[:,:,:,2], V), True) self.assertAlmostEqual(vol.volume(), 2*pi*1.5/6, places=3) self.assertTrue(np.all(x >= 0)) # completely contained in first octant # test axis revolve vol = VolumeFactory.revolve(Surface()+(1,1), theta=pi/3, axis=(1,0,0)) vol.reparam() # set parametric space to (0,1)^3 u = np.linspace(0, 1, 7) v = np.linspace(0, 1, 7) w = np.linspace(0, 1, 7) x = vol.evaluate(u,v,w) V,U,W = np.meshgrid(v,u,w) R = np.sqrt(x[:,:,:,1]**2 + x[:,:,:,2]**2) self.assertEqual(np.allclose(R, V+1), True) self.assertEqual(np.allclose(x[:,:,:,0], U+1), True) self.assertTrue(np.all(x >= 0)) # completely contained in first octant
def revolve(curve, theta=2 * pi, axis=[0,0,1]): """revolve(curve, [theta=2pi], [axis=[0,0,1]]) Revolve a surface by sweeping a curve in a rotational fashion around the *z* axis. :param Curve curve: Curve to revolve :param float theta: Angle to revolve, in radians :param vector-like axis: Axis of rotation :return: The revolved surface :rtype: Surface """ curve = curve.clone() # clone input curve, throw away input reference curve.set_dimension(3) # add z-components (if not already present) curve.force_rational() # add weight (if not already present) # align axis with the z-axis normal_theta = atan2(axis[1], axis[0]) normal_phi = atan2(sqrt(axis[0]**2 + axis[1]**2), axis[2]) curve.rotate(-normal_theta, [0,0,1]) curve.rotate(-normal_phi, [0,1,0]) circle_seg = CurveFactory.circle_segment(theta) n = len(curve) # number of control points of the curve m = len(circle_seg) # number of control points of the sweep cp = np.zeros((m * n, 4)) # loop around the circle and set control points by the traditional 9-point # circle curve with weights 1/sqrt(2), only here C0-periodic, so 8 points dt = 0 t = 0 for i in range(m): x,y,w = circle_seg[i] dt = atan2(y,x) - t t += dt curve.rotate(dt) cp[i * n:(i + 1) * n, :] = curve[:] cp[i * n:(i + 1) * n, 2] *= w cp[i * n:(i + 1) * n, 3] *= w result = Surface(curve.bases[0], circle_seg.bases[0], cp, True) # rotate it back again result.rotate(normal_phi, [0,1,0]) result.rotate(normal_theta, [0,0,1]) return result
def revolve(curve, theta=2 * pi, axis=(0, 0, 1)): """ Revolve a surface by sweeping a curve in a rotational fashion around the *z* axis. :param Curve curve: Curve to revolve :param float theta: Angle to revolve, in radians :param array-like axis: Axis of rotation :return: The revolved surface :rtype: Surface """ curve = curve.clone() # clone input curve, throw away input reference curve.set_dimension(3) # add z-components (if not already present) curve.force_rational() # add weight (if not already present) # align axis with the z-axis normal_theta = atan2(axis[1], axis[0]) normal_phi = atan2(sqrt(axis[0]**2 + axis[1]**2), axis[2]) curve.rotate(-normal_theta, [0, 0, 1]) curve.rotate(-normal_phi, [0, 1, 0]) circle_seg = CurveFactory.circle_segment(theta) n = len(curve) # number of control points of the curve m = len(circle_seg) # number of control points of the sweep cp = np.zeros((m * n, 4)) # loop around the circle and set control points by the traditional 9-point # circle curve with weights 1/sqrt(2), only here C0-periodic, so 8 points dt = 0 t = 0 for i in range(m): x, y, w = circle_seg[i] dt = atan2(y, x) - t t += dt curve.rotate(dt) cp[i * n:(i + 1) * n, :] = curve[:] cp[i * n:(i + 1) * n, 2] *= w cp[i * n:(i + 1) * n, 3] *= w result = Surface(curve.bases[0], circle_seg.bases[0], cp, True) # rotate it back again result.rotate(normal_phi, [0, 1, 0]) result.rotate(normal_theta, [0, 0, 1]) return result
def plane(self): dim = int( self.read_next_non_whitespace().strip()) center = np.array(next(self.fstream).split(), dtype=float) normal = np.array(next(self.fstream).split(), dtype=float) x_axis = np.array(next(self.fstream).split(), dtype=float) finite = next(self.fstream).strip() != '0' if finite: param_u= np.array(next(self.fstream).split(), dtype=float) param_v= np.array(next(self.fstream).split(), dtype=float) else: param_u= [-state.unlimited, +state.unlimited] param_v= [-state.unlimited, +state.unlimited] swap = next(self.fstream).strip() != '0' result = Surface() * [param_u[1]-param_u[0], param_v[1]-param_v[0]] + [param_u[0],param_v[0]] result.rotate(rotate_local_x_axis(x_axis, normal)) result = flip_and_move_plane_geometry(result,center,normal) result.reparam(param_u, param_v) if(swap): result.swap() return result
def test_3d_self_connection(self): square = Surface() + [1, 0] square = square.rotate(np.pi / 2, (1, 0, 0)) vol = volume_factory.revolve(square) vol = vol.split(vol.knots('w')[0], direction='w') # break periodicity model = SplineModel(3, 3) model.add(vol, raise_on_twins=False) writer = IFEMWriter(model) expected = [IFEMConnection(1, 1, 5, 6, 0)] for connection, want in zip(writer.connections(), expected): self.assertEqual(connection, want)