Skip to content

Commit

Permalink
fix Ellipsoid, add_frame()
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomusy committed Nov 19, 2023
1 parent 0561120 commit 5a40677
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 73 deletions.
14 changes: 2 additions & 12 deletions examples/notebooks/distance2mesh.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand All @@ -11,16 +11,6 @@
"text": [
"Distance: [2.54950976 2.54950976 2.41422468 ... 2.14595456 2.27999987 2.41536531]\n"
]
},
{
"data": {
"text/plain": [
"<vedo.plotter.Plotter at 0x7f4a3c7ef250>"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
Expand Down Expand Up @@ -122,7 +112,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.4"
"version": "3.11.5"
}
},
"nbformat": 4,
Expand Down
73 changes: 56 additions & 17 deletions examples/notebooks/pca.ipynb

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions vedo/plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2205,12 +2205,12 @@ def add_renderer_frame(self, c=None, alpha=None, lw=None, padding=None):
padding : (float)
padding space in pixels.
"""
if lw:
if c is None: # automatic black or white
c = (0.9, 0.9, 0.9)
if np.sum(vedo.plotter_instance.renderer.GetBackground()) > 1.5:
c = (0.1, 0.1, 0.1)
renf = addons.RendererFrame(c, alpha, lw, padding)
if c is None: # automatic black or white
c = (0.9, 0.9, 0.9)
if np.sum(vedo.plotter_instance.renderer.GetBackground()) > 1.5:
c = (0.1, 0.1, 0.1)
renf = addons.RendererFrame(c, alpha, lw, padding)
if renf:
self.renderer.AddActor(renf)
return self

Expand Down
61 changes: 23 additions & 38 deletions vedo/shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import vedo
from vedo import settings
from vedo.transformations import pol2cart, cart2spher, spher2cart
from vedo.transformations import LinearTransform, pol2cart, cart2spher, spher2cart
from vedo.colors import cmaps_names, get_color, printc
from vedo import utils
from vedo.pointcloud import Points, merge
Expand Down Expand Up @@ -2871,9 +2871,9 @@ class Ellipsoid(Mesh):
def __init__(
self,
pos=(0, 0, 0),
axis1=(1, 0, 0),
axis2=(0, 2, 0),
axis3=(0, 0, 3),
axis1=(0.5, 0, 0),
axis2=(0, 1, 0),
axis3=(0, 0, 1.5),
res=24,
c="cyan4",
alpha=1.0,
Expand All @@ -2891,56 +2891,41 @@ def __init__(
.. note:: `axis1` and `axis2` are only used to define sizes and one azimuth angle.
"""

self.center = pos

self.axis1 = np.asarray(axis1)
self.axis2 = np.asarray(axis2)
self.axis3 = np.asarray(axis3)

self.va = np.linalg.norm(self.axis1)
self.vb = np.linalg.norm(self.axis2)
self.vc = np.linalg.norm(self.axis3)

self.va_error = 0
self.vb_error = 0
self.vc_error = 0
self.axis1 = axis1
self.axis2 = axis2
self.axis3 = axis3
self.nr_of_points = 1 # used by pcaEllipsoid

self.nr_of_points = 1 # used by pca_ellipsoid()

if utils.is_sequence(res):
res_t, res_phi = res
else:
res_t, res_phi = 2 * res, res

elli_source = vtk.new("SphereSource")
elli_source.SetRadius(1)
elli_source.SetThetaResolution(res_t)
elli_source.SetPhiResolution(res_phi)
elli_source.Update()
l1 = np.linalg.norm(axis1)
l2 = np.linalg.norm(axis2)
l3 = np.linalg.norm(axis3)
self.va = l1
self.vb = l2
self.vc = l3
axis1 = np.array(axis1) / l1
axis2 = np.array(axis2) / l2
axis3 = np.array(axis3) / l3
angle = np.arcsin(np.dot(axis1, axis2))
theta = np.arccos(axis3[2])
phi = np.arctan2(axis3[1], axis3[0])

t = vtk.vtkTransform()
t.PostMultiply()
t.Scale(l1, l2, l3)
t.RotateX(np.rad2deg(angle))
t.RotateY(np.rad2deg(theta))
t.RotateZ(np.rad2deg(phi))
tf = vtk.new("TransformPolyDataFilter")
tf.SetInputData(elli_source.GetOutput())
tf.SetTransform(t)
tf.Update()
pd = tf.GetOutput()
self.transformation = t
super().__init__(elli_source.GetOutput(), c, alpha)

pos = utils.make3d(pos)

matrix = np.c_[self.axis1, self.axis2, self.axis3]
lt = LinearTransform(matrix).translate(pos)
self.apply_transform(lt)

super().__init__(pd, c, alpha)
self.phong()
if len(pos) == 2:
pos = (pos[0], pos[1], 0)
self.pos(pos)
self.name = "Ellipsoid"

def asphericity(self):
Expand Down

0 comments on commit 5a40677

Please sign in to comment.