Skip to content

Commit

Permalink
as_image only show nonzero
Browse files Browse the repository at this point in the history
  • Loading branch information
Tumiz authored May 1, 2024
1 parent 0047800 commit ff6f152
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 137 deletions.
2 changes: 1 addition & 1 deletion docs/Rotation.html

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions docs/Transform.html

Large diffs are not rendered by default.

36 changes: 18 additions & 18 deletions docs/Vector.html

Large diffs are not rendered by default.

138 changes: 72 additions & 66 deletions docs/Vector3.html

Large diffs are not rendered by default.

8 changes: 1 addition & 7 deletions notebooks/Color.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,6 @@
"source": [
"from py3d import Color\n",
"assert Color.BASE_SHAPE == (4,)\n",
"assert Color.rand().shape == (4,)\n",
"assert Color.rand(1).shape == (1, 4)\n",
"assert Color.rand(2).shape == (2, 4)\n",
"assert Color.rand().n == ()\n",
"assert Color.rand(1).n == (1,)\n",
"assert Color.rand(2, 3).n == (2, 3)\n",
"assert (Color(r=1) == [1, 0, 0, 1]).all()\n",
"assert Color(r=[1, 0.5]).n == (2,)"
]
Expand Down Expand Up @@ -91,7 +85,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.6"
"version": "3.10.12"
},
"orig_nbformat": 4,
"vscode": {
Expand Down
6 changes: 3 additions & 3 deletions notebooks/Transform.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,8 @@
"metadata": {},
"outputs": [],
"source": [
"from py3d import Vector3, Transform\n",
"points = (Vector3.rand(1000)-0.5).U @ Transform.from_scaling(x=2)\n",
"import py3d\n",
"points = (py3d.rand(1000, 3)-0.5).U @ py3d.Transform.from_scaling(x=2)\n",
"points.as_point()\n"
]
},
Expand Down Expand Up @@ -435,7 +435,7 @@
"outputs": [],
"source": [
"import py3d\n",
"p=(py3d.Vector3.rand(1000)-0.5).U+py3d.Vector3(x=1,y=1,z=2)\n",
"p=(py3d.rand(1000, 3)-0.5).U+py3d.Vector3(x=1,y=1,z=2)\n",
"py3d.render(p.as_point())\n",
"p.z=0\n",
"py3d.render(p.as_point())\n"
Expand Down
4 changes: 2 additions & 2 deletions notebooks/Vector.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@
"outputs": [],
"source": [
"import py3d\n",
"py3d.Vector.rand(2, 3, 4)"
"py3d.rand(2, 3, 4)"
]
},
{
Expand All @@ -224,7 +224,7 @@
"metadata": {},
"outputs": [],
"source": [
"assert _.n == _.shape == (2, 3, 4)\n",
"assert _.shape == (2, 3, 4)\n",
"assert (_.H[..., -1] == 1).all()"
]
},
Expand Down
37 changes: 15 additions & 22 deletions notebooks/Vector3.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@
"outputs": [],
"source": [
"import py3d\n",
"p = py3d.Vector3.rand(1000)\n",
"p = py3d.rand(1000, 3)\n",
"p[p.L > 0.7].as_point()"
]
},
Expand All @@ -290,16 +290,8 @@
"metadata": {},
"outputs": [],
"source": [
"py3d.Vector3.rand()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"assert _.n == ()\n"
"import py3d\n",
"py3d.rand(3)\n"
]
},
{
Expand All @@ -316,7 +308,8 @@
"metadata": {},
"outputs": [],
"source": [
"py3d.Vector3.rand(3)\n"
"import py3d\n",
"py3d.rand(3, 3)"
]
},
{
Expand All @@ -325,7 +318,7 @@
"metadata": {},
"outputs": [],
"source": [
"assert _.n == (3,)\n"
"assert _.n == (3,)"
]
},
{
Expand Down Expand Up @@ -535,10 +528,14 @@
"outputs": [],
"source": [
"import py3d\n",
"v: py3d.Vector3 = py3d.Vector3.rand(10).cumsum(axis=0)\n",
"v = py3d.Vector3([\n",
" [0,0,0],\n",
" [3,4,8],\n",
" [6,13,9]\n",
"])\n",
"py3d.render(v.as_line())\n",
"i = v.lerp([0.5, 1.6, 8.2, 7.44, 9.7], range(10))\n",
"py3d.render(i.as_point().paint(py3d.Color(r=1)))\n"
"i = v.lerp([0.5, 1.6, 3.1, 5.2], [0,2,4])\n",
"py3d.render(i.as_point(py3d.Color(r=1)))"
]
},
{
Expand Down Expand Up @@ -600,7 +597,7 @@
"outputs": [],
"source": [
"import py3d\n",
"points = (py3d.Vector3.rand(1000)-0.5).U * 2\n",
"points = (py3d.rand(1000, 3)-0.5).U * 2\n",
"points.as_point()\n"
]
},
Expand All @@ -613,7 +610,6 @@
"import py3d\n",
"from numpy import allclose\n",
"assert py3d.Vector3().U.L == 0\n",
"assert allclose(py3d.Vector3.rand().U.L, 1)\n",
"assert py3d.Vector3().tile(2, 3).n == (2, 3)\n",
"assert (py3d.Vector3([\n",
" [1, 2, 3],\n",
Expand All @@ -623,9 +619,6 @@
" [2, 3, 4, 1]\n",
"]).all()\n",
"assert type(py3d.Vector3(x=[1, 2]).H) == py3d.Vector4\n",
"assert type(py3d.Vector3.rand(2, 3)) == py3d.Vector3\n",
"assert py3d.Vector3.rand(2, 3).shape == (2, 3, 3)\n",
"assert py3d.Vector3.rand(2, 3).n == (2, 3)\n",
"assert py3d.Vector3(y=[2, 3]).n == (2,)\n"
]
},
Expand Down Expand Up @@ -848,7 +841,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.6"
"version": "3.10.12"
},
"vscode": {
"interpreter": {
Expand Down
32 changes: 19 additions & 13 deletions py3d/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,18 @@ def read_npy(path) -> Vector:
return numpy.load(path).view(Vector)


def rand(*n) -> Vector | Vector2 | Vector3 | Vector4:
'''
Create a random vector with shape of n
'''
w = n[-1]
if w in [2, 3, 4]:
vtype = getattr(__module__, f"Vector{w}")
else:
vtype = Vector
return numpy.random.rand(*n).view(vtype)


class Vector(numpy.ndarray):
'''
Base class of Vector2, Vector3, Vector4 and Transform
Expand Down Expand Up @@ -287,11 +299,6 @@ def n(self):
else:
return self.shape

@classmethod
def rand(cls, *n) -> Vector | Vector2 | Vector3 | Vector4:
n += cls.BASE_SHAPE
return numpy.random.rand(*n).view(cls)

@property
def x(self) -> Vector:
return self[..., 0].view(Vector)
Expand Down Expand Up @@ -451,27 +458,26 @@ def to_csv(self, path):
def to_npy(self, path):
numpy.save(path, self)

def as_image(self, align_center=True, sample_rate=None):
def as_image(self, nonzero=True, align_center=True, sample_rate=None):
'''
Visualize the vector as an image, with mapped colors from black to yellow or the image's own colors
'''
if not sample_rate:
sample_rate = max(round(self.size / 1e6), 1)
sample = self[::-sample_rate, ::sample_rate]
if sample.dtype == numpy.uint8:
sample = sample / 255
sample = Color(sample / 255)
elif sample.ndim == 2:
sample = Color.map(sample)
c = numpy.ones(sample.shape[:-1] + (4,)).view(Color)
c[..., :min(sample.shape[-1], 4)] = sample
c = c.transpose(1, 0, 2)
w, h, _ = numpy.shape(c)
ret = Point(w, h)
*_, h, w = sample.n
ret = Point(*_, w, h)
if align_center:
ret.xyz = Vector3.grid(range(-w//2, w//2), range(-h//2, h//2))
else:
ret.xyz = Vector3.grid(range(w), range(h))
ret.color = c
ret.color = sample.transpose(1, 0, 2)
if nonzero:
return ret[ret.color.rgb.any(-1)]
return ret


Expand Down

0 comments on commit ff6f152

Please sign in to comment.