MCPcopy Create free account
hub / github.com/apple/ml-pointersect / UVMap

Class UVMap

plib/uv_mapping.py:9–61  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7
8
9class UVMap:
10 def __init__(
11 self,
12 texture: np.ndarray,
13 mode: str = 'wrap',
14 ):
15 """
16 Args:
17 texture:
18 (h, w, dim) for example, an rgb image, a displacement map, a bump map, etc
19 mode:
20 'wrap': used when 1 <= uv or uv <= 0.
21 'edge': used when no wrapping is needed
22 """
23 self.texture = texture
24 self.texture_height = self.texture.shape[0]
25 self.texture_width = self.texture.shape[1]
26 self.mode = mode
27
28 # handle padding
29 pad_widths = [[0, 0]] * self.texture.ndim
30 pad_widths[0] = [1, 1]
31 pad_widths[1] = [1, 1]
32 padded_texture = np.pad(self.texture, pad_width=pad_widths, mode=mode)
33
34 # create interpolator for the texture
35 ys = np.linspace(-1, self.texture_height, self.texture_height + 2) # 0, 1, ..., h-1
36 xs = np.linspace(-1, self.texture_width, self.texture_width + 2) # 0, 1, ..., w-1
37 # yg, xg = np.meshgrid(ys, xs, indexing='ij')
38 self.interpolator = RegularGridInterpolator(
39 (ys, xs), padded_texture, method='linear', bounds_error=True)
40 # image grid defined on 0..h-1
41
42 def __call__(self, uv: np.ndarray):
43 """
44 query the texture map at locations uv
45 Args:
46 uv: (*, 2) u is in the x/width direction, v is in the y/height direction,
47
48 Returns:
49 (*, dim)
50 """
51 if isinstance(uv, (list, tuple)):
52 uv = np.array(uv)
53
54 # in case want to tile the texture map
55 uv = np.mod(uv, 1)
56
57 # convert uv to yx
58 y = uv[..., 1:2] * self.texture_height - 0.5 # (*, 1)
59 x = uv[..., 0:1] * self.texture_width - 0.5 # (*, 1)
60 yx = np.concatenate((y, x), axis=-1)
61 return self.interpolator(yx)

Calls

no outgoing calls

Tested by

no test coverage detected