#!/usr/bin/env python
# coding: utf-8

# # Householder in 3D
# 
# Copyright (C) 2024 Andreas Kloeckner
# 
# <details>
# <summary>MIT License</summary>
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# </details>

# Requirements to run this:
# ```
# pip install ipywidgets pyvista[all,trame]
# ```
# (will need Jupyter server restart and browser hard-reload)
# 
# *Note:* This will not work in the web-based JupyterLab, because software.

# In[47]:


import numpy as np
import numpy.linalg as la
import pyvista as pv
pv.set_jupyter_backend('trame')


# In[46]:


pl = pv.Plotter()

a = np.array([-0.4, 0.8, -0.3])
e1 = np.array([1, 0, 0])
inplane = a + la.norm(a, 2) * e1
ntilde = a - la.norm(a, 2) * e1
n = ntilde/la.norm(ntilde, 2)

def plot_plane(normal, **kwargs):
    pl.add_mesh(pv.Plane(direction=normal, i_size=2, j_size=2), **kwargs)

def plot_vector(v, label, **kwargs):
    v = np.asarray(v, dtype=np.float32)
    pl.add_mesh(pv.Arrow(direction=v, scale="auto", shaft_radius=0.02, tip_radius=0.04), **kwargs)
    pl.add_point_labels([v], [label])

plot_plane(n)
plot_plane(np.cross(e1, a), opacity=0.5)

plot_vector(n, "n")
plot_vector(e1, "e1")
plot_vector(a, "a")
plot_vector(inplane, "inplane")

pl.show()


# In[ ]:




