Exemplo n.º 1
0
 def test_edge_curves_finitestrain_lshape(self):
     # Create an L-shape geometry with an interior 270-degree angle at the origin (u=.5, v=1)
     c1 = CurveFactory.polygon([[-1, 1], [-1,-1], [1,-1]])
     c2 = CurveFactory.polygon([[ 1,-1], [ 1, 0]])
     c3 = CurveFactory.polygon([[ 1, 0], [ 0, 0], [0, 1]])
     c4 = CurveFactory.polygon([[ 0, 1], [-1, 1]])
     c1.refine(2).raise_order(1)
     c2.refine(2).raise_order(1)
     surf = SurfaceFactory.edge_curves([c1, c2, c3, c4], type='finitestrain')
     # the quickest way to check for self-intersecting geometry here is that
     # the normal is pointing the wrong way: down z-axis instead of up
     surf.reparam().set_dimension(3)
     self.assertTrue(surf.normal(0.5, 0.98)[2] > 0.0)
     # also check that no controlpoints leak away into the first quadrant
     self.assertFalse(np.any(np.logical_and(surf[:,:,0] > 0, surf[:,:,1] > 0)))
Exemplo n.º 2
0
    def test_polygon(self):
        pts = [[1,0], [1,1], [0,1], [0,2], [6,2]]
        c = CurveFactory.polygon(pts)
        expected_knots = [0,0,1,2,3,9,9]
        actual_knots   = c.knots(0,True)

        self.assertEqual(len(c),      5)
        self.assertEqual(c.order(0),  2)
        self.assertEqual(c.dimension, 2)
        self.assertAlmostEqual(np.linalg.norm(expected_knots - actual_knots), 0.0)

        c = CurveFactory.polygon([0,0], [1,0], [0,1], [-1,0], relative=True)
        self.assertEqual(len(c), 4)
        self.assertAlmostEqual(c[2][0], 1)
        self.assertAlmostEqual(c[2][1], 1)
        self.assertAlmostEqual(c[3][0], 0)
        self.assertAlmostEqual(c[3][1], 1)
Exemplo n.º 3
0
    def test_polygon(self):
        pts = [[1, 0], [1, 1], [0, 1], [0, 2], [6, 2]]
        c = cf.polygon(pts)
        expected_knots = [0, 0, 1, 2, 3, 9, 9]
        actual_knots = c.knots(0, True)

        self.assertEqual(len(c), 5)
        self.assertEqual(c.order(0), 2)
        self.assertEqual(c.dimension, 2)
        self.assertAlmostEqual(norm(expected_knots - actual_knots), 0.0)

        c = cf.polygon([0, 0], [1, 0], [0, 1], [-1, 0], relative=True)
        self.assertEqual(len(c), 4)
        self.assertAlmostEqual(c[2][0], 1)
        self.assertAlmostEqual(c[2][1], 1)
        self.assertAlmostEqual(c[3][0], 0)
        self.assertAlmostEqual(c[3][1], 1)
Exemplo n.º 4
0
    def texture(self, p, ngeom, ntexture, method='full', irange=[None,None], jrange=[None,None]):
        # Set the dimensions of geometry and texture map
        # ngeom    = np.floor(self.n / (p-1))
        # ntexture = np.floor(self.n * n)
        # ngeom    = ngeom.astype(np.int32)
        # ntexture = ntexture.astype(np.int32)
        ngeom    = ensure_listlike(ngeom, 3)
        ntexture = ensure_listlike(ntexture, 3)
        p        = ensure_listlike(p, 3)


        # Create the geometry
        ngx, ngy, ngz = ngeom
        b1 = BSplineBasis(p[0], [0]*(p[0]-1) + [i/ngx for i in range(ngx+1)] + [1]*(p[0]-1))
        b2 = BSplineBasis(p[1], [0]*(p[1]-1) + [i/ngy for i in range(ngy+1)] + [1]*(p[1]-1))
        b3 = BSplineBasis(p[2], [0]*(p[2]-1) + [i/ngz for i in range(ngz+1)] + [1]*(p[2]-1))

        l2_fit = surface_factory.least_square_fit
        vol = self.get_c0_mesh()

        i = slice(irange[0], irange[1], None)
        j = slice(jrange[0], jrange[1], None)
        # special case number of evaluation points for full domain
        if irange[1] == None: irange[1] = vol.shape[0]
        if jrange[1] == None: jrange[1] = vol.shape[1]
        if irange[0] == None: irange[0] = 0
        if jrange[0] == None: jrange[0] = 0

        nu = np.diff(irange)
        nv = np.diff(jrange)
        nw = vol.shape[2]

        u = np.linspace(0, 1, nu)
        v = np.linspace(0, 1, nv)
        w = np.linspace(0, 1, nw)

        crvs = []
        crvs.append(curve_factory.polygon(vol[i          ,jrange[0]  , 0,:].squeeze()))
        crvs.append(curve_factory.polygon(vol[i          ,jrange[0]  ,-1,:].squeeze()))
        crvs.append(curve_factory.polygon(vol[i          ,jrange[1]-1, 0,:].squeeze()))
        crvs.append(curve_factory.polygon(vol[i          ,jrange[1]-1,-1,:].squeeze()))
        crvs.append(curve_factory.polygon(vol[irange[0]  ,j          , 0,:].squeeze()))
        crvs.append(curve_factory.polygon(vol[irange[0]  ,j          ,-1,:].squeeze()))
        crvs.append(curve_factory.polygon(vol[irange[1]-1,j          , 0,:].squeeze()))
        crvs.append(curve_factory.polygon(vol[irange[1]-1,j          ,-1,:].squeeze()))
        crvs.append(curve_factory.polygon(vol[irange[0]  ,jrange[0]  , :,:].squeeze()))
        crvs.append(curve_factory.polygon(vol[irange[0]  ,jrange[1]-1, :,:].squeeze()))
        crvs.append(curve_factory.polygon(vol[irange[1]-1,jrange[0]  , :,:].squeeze()))
        crvs.append(curve_factory.polygon(vol[irange[1]-1,jrange[1]-1, :,:].squeeze()))
#        with G2('curves.g2') as myfile:
#            myfile.write(crvs)
#        print('Written curve.g2')


        if method == 'full':
            bottom = l2_fit(vol[i,          j,          0,:].squeeze(), [b1, b2], [u, v])
            top    = l2_fit(vol[i,          j,         -1,:].squeeze(), [b1, b2], [u, v])
            left   = l2_fit(vol[irange[0]  ,j,          :,:].squeeze(), [b2, b3], [v, w])
            right  = l2_fit(vol[irange[1]-1,j,          :,:].squeeze(), [b2, b3], [v, w])
            front  = l2_fit(vol[i,          jrange[0],  :,:].squeeze(), [b1, b3], [u, w])
            back   = l2_fit(vol[i,          jrange[1]-1,:,:].squeeze(), [b1, b3], [u, w])
            volume = volume_factory.edge_surfaces([left, right, front, back, bottom, top])

        elif method == 'z':
            bottom = l2_fit(vol[i,j, 0,:].squeeze(), [b1, b2], [u, v])
            top    = l2_fit(vol[i,j,-1,:].squeeze(), [b1, b2], [u, v])
            volume = volume_factory.edge_surfaces([bottom, top])
            volume.set_order(*p)
            volume.refine(ngz - 1, direction='w')

        volume.reverse(direction=2)

        # Point-to-cell mapping
        # TODO: Optimize more
        eps = 1e-2
        u = [np.linspace(eps, 1-eps, n) for n in ntexture]
        points = volume(*u).reshape(-1, 3)
        cellids = np.zeros(points.shape[:-1], dtype=int)
        cell = None
        nx, ny, nz = self.n
        for ptid, point in enumerate(tqdm(points, desc='Inverse mapping')):
            i, j, k = cell = self.raw.cell_at(point) # , guess=cell)
            cellid = i*ny*nz + j*nz + k
            cellids[ptid] = cellid

        cellids = cellids.reshape(tuple(ntexture))

        all_textures = {}
        for name in self.attribute:
            data = self.attribute[name][cellids]

            # TODO: This flattens the image if it happens to be 3D (or higher...)
            # do we need a way to communicate the structure back to the caller?
            # data = data.reshape(-1, data.shape[-1])

            # TODO: This normalizes the image,
            # but we need a way to communicate the ranges back to the caller
            # a, b = min(data.flat), max(data.flat)
            # data = ((data - a) / (b - a) * 255).astype(np.uint8)

            all_textures[name] = data
        all_textures['cellids'] = cellids

        return volume, all_textures
Exemplo n.º 5
0
 def test_write_curve(self):
     crv = CurveFactory.polygon([[0, 0], [1, 0], [1, 1], [0, 1]])
     with SVG('output.svg') as myfile:
         myfile.write(crv)
     self.assertTrue(os.path.isfile('output.svg'))
     os.remove('output.svg')
Exemplo n.º 6
0
 def test_write_curve(self):
     crv = CurveFactory.polygon([[0,0], [1,0], [1,1], [0,1]])
     with SVG('output.svg') as myfile:
         myfile.write(crv)
     self.assertTrue(os.path.isfile('output.svg'))
     os.remove('output.svg')