{ "cells": [ { "cell_type": "markdown", "id": "eac2be7d", "metadata": {}, "source": [ "# Interaction WolfArray/vectors - Transfer Z data\n", "\n", "It is easy to transfer Z data from a WolfArray to a vector.\n", "\n", "'z' is a property of the vector, so it can be set directly.\n", "\n", "The setter `vector.z = wolfarray_instance` will transfer the Z data from the WolfArray to the vector. Thre vector will convert its vertices coordinates to the WolfArray's coordinate system and extract the corresponding values." ] }, { "cell_type": "code", "execution_count": 1, "id": "c1cb040f", "metadata": {}, "outputs": [], "source": [ "# import _add_path # for debugging purposes only - can be removed in production\n", "from wolfhece import is_enough, __version__\n", "assert is_enough(\"2.2.7\"), \"Please update wolfhece to 2.2.7 or later - you have \" + __version__\n", "\n", "from wolfhece.wolf_array import WolfArray, header_wolf\n", "from wolfhece.PyVertexvectors import Zones, zone, vector\n", "\n", "import numpy as np\n", "from packaging import version" ] }, { "cell_type": "markdown", "id": "71bb75e0", "metadata": {}, "source": [ "## Creation of an array\n", "\n", "1. Create a header\n", "1. Set shape and resolution\n", "1. Create an array from the header" ] }, { "cell_type": "code", "execution_count": 2, "id": "c7f95bca", "metadata": {}, "outputs": [], "source": [ "h = header_wolf()\n", "h.shape = (100, 100)\n", "h.set_resolution(1., 1.)\n", "\n", "a = WolfArray(srcheader=h)\n", "\n", "a.array = np.random.rand(h.shape[0], h.shape[1]) * 10.0 # Random values between 0 and 10" ] }, { "cell_type": "markdown", "id": "e0ee7924", "metadata": {}, "source": [ "## Creation of a vector\n", "\n", "- Create a vector and add vertices from Numpy array\n", "- Split the vector with a given distance (1 meter) and replace it" ] }, { "cell_type": "code", "execution_count": 3, "id": "3f0c5d2a", "metadata": {}, "outputs": [], "source": [ "v = vector(fromnumpy= np.asarray([[5.,5.],\n", " [10.,5.],\n", " [10.,20.],\n", " [15.,30.],\n", " [5., 45.]]))\n", "\n", "v.split(1., new = False) # Split the vector into segments of 1 unit length" ] }, { "cell_type": "markdown", "id": "1f698b55", "metadata": {}, "source": [ "## Copy the values from the array to the vector\n", "\n", "See vector \"z.setter\" for more information." ] }, { "cell_type": "code", "execution_count": 4, "id": "7b99fdf3", "metadata": {}, "outputs": [], "source": [ "v.z = a # Transfer the Z values from the array to the vector -- The vector will retrieve the values from the array based on the vertices' coordinates" ] }, { "cell_type": "code", "execution_count": 5, "id": "a6d9fbde", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[6.17796835 3.78758632 1.54473996 1.54473996 2.24521166 5.31537719\n", " 5.31537719 7.26549213 7.26549213 3.30763981 3.08678037 3.08678037\n", " 4.82598289 4.82598289 3.90230366 3.90230366 8.76591086 8.57796737\n", " 4.24841736 4.24841736 2.65791145 2.65791145 0.2473708 3.67111596\n", " 9.73361847 4.08649935 2.01570756 1.57680202 4.57863609 7.79916745\n", " 7.79916745 4.01318474 3.98424547 7.98752954 3.23897928 6.01450406\n", " 2.21527156 0.27510984 3.34785821 2.83141191 9.95544353 8.46809499\n", " 5.79207877 1.48870911 1.48870911 8.44722858 5.26646236 9.541056\n", " 1.72990087 3.68136678 4.97815591 8.97383011]\n" ] } ], "source": [ "print(v.z) # This will print the Z values of the vector, which are now linked to the array values" ] }, { "cell_type": "markdown", "id": "e7ec2c1c", "metadata": {}, "source": [ "## Other vector's properties \n", "\n", "Other properties of the vector exist, such as :\n", "- `vector.x` : the X coordinates of the vertices\n", "- `vector.y` : the Y coordinates of the vertices\n", "- `vector.z` : the Z coordinates of the vertices\n", "- `vector.xy` : the XY coordinates of the vertices\n", "- `vector.xyz` : the XYZ coordinates of the vertices\n", "- `vector.s_curvi` : the curvilinear abscissa of the vertices\n", "- `vector.sz_curvi` : the curvilinear abscissa of the vertices associated with the Z values\n", "- `vector.xyi` : the XY coordinates of the vertices asscociated with an integer indicating if the segment is used (1) or not (0)" ] }, { "cell_type": "code", "execution_count": null, "id": "26949324", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "X-coordinates : [ 5. 6. 7. 8. 9. 10.\n", " 10. 10. 10. 10. 10. 10.\n", " 10. 10. 10. 10. 10. 10.\n", " 10. 10. 10. 10.4472136 10.89442719 11.34164079\n", " 11.78885438 12.23606798 12.68328157 13.13049517 13.57770876 14.02492236\n", " 14.47213595 14.91934955 15. 14.54533437 13.99063418 13.43593398\n", " 12.88123379 12.32653359 11.77183339 11.2171332 10.662433 10.1077328\n", " 9.55303261 8.99833241 8.44363222 7.88893202 7.33423182 6.77953163\n", " 6.22483143 5.67013124 5.11543104 5. ]\n", "Y-coordinates : [ 5. 5. 5. 5. 5. 5.\n", " 6. 7. 8. 9. 10. 11.\n", " 12. 13. 14. 15. 16. 17.\n", " 18. 19. 20. 20.89442719 21.78885438 22.68328157\n", " 23.57770876 24.47213595 25.36656315 26.26099034 27.15541753 28.04984472\n", " 28.94427191 29.8386991 30. 30.68199844 31.51404873 32.34609903\n", " 33.17814932 34.01019962 34.84224991 35.6743002 36.5063505 37.33840079\n", " 38.17045109 39.00250138 39.83455168 40.66660197 41.49865226 42.33070256\n", " 43.16275285 43.99480315 44.82685344 45. ]\n", "XY-coordinates : [[ 5. 5. ]\n", " [ 6. 5. ]\n", " [ 7. 5. ]\n", " [ 8. 5. ]\n", " [ 9. 5. ]\n", " [10. 5. ]\n", " [10. 6. ]\n", " [10. 7. ]\n", " [10. 8. ]\n", " [10. 9. ]\n", " [10. 10. ]\n", " [10. 11. ]\n", " [10. 12. ]\n", " [10. 13. ]\n", " [10. 14. ]\n", " [10. 15. ]\n", " [10. 16. ]\n", " [10. 17. ]\n", " [10. 18. ]\n", " [10. 19. ]\n", " [10. 20. ]\n", " [10.4472136 20.89442719]\n", " [10.89442719 21.78885438]\n", " [11.34164079 22.68328157]\n", " [11.78885438 23.57770876]\n", " [12.23606798 24.47213595]\n", " [12.68328157 25.36656315]\n", " [13.13049517 26.26099034]\n", " [13.57770876 27.15541753]\n", " [14.02492236 28.04984472]\n", " [14.47213595 28.94427191]\n", " [14.91934955 29.8386991 ]\n", " [15. 30. ]\n", " [14.54533437 30.68199844]\n", " [13.99063418 31.51404873]\n", " [13.43593398 32.34609903]\n", " [12.88123379 33.17814932]\n", " [12.32653359 34.01019962]\n", " [11.77183339 34.84224991]\n", " [11.2171332 35.6743002 ]\n", " [10.662433 36.5063505 ]\n", " [10.1077328 37.33840079]\n", " [ 9.55303261 38.17045109]\n", " [ 8.99833241 39.00250138]\n", " [ 8.44363222 39.83455168]\n", " [ 7.88893202 40.66660197]\n", " [ 7.33423182 41.49865226]\n", " [ 6.77953163 42.33070256]\n", " [ 6.22483143 43.16275285]\n", " [ 5.67013124 43.99480315]\n", " [ 5.11543104 44.82685344]\n", " [ 5. 45. ]]\n", "XYZ-coordinates : [[ 5. 5. 6.17796835]\n", " [ 6. 5. 3.78758632]\n", " [ 7. 5. 1.54473996]\n", " [ 8. 5. 1.54473996]\n", " [ 9. 5. 2.24521166]\n", " [10. 5. 5.31537719]\n", " [10. 6. 5.31537719]\n", " [10. 7. 7.26549213]\n", " [10. 8. 7.26549213]\n", " [10. 9. 3.30763981]\n", " [10. 10. 3.08678037]\n", " [10. 11. 3.08678037]\n", " [10. 12. 4.82598289]\n", " [10. 13. 4.82598289]\n", " [10. 14. 3.90230366]\n", " [10. 15. 3.90230366]\n", " [10. 16. 8.76591086]\n", " [10. 17. 8.57796737]\n", " [10. 18. 4.24841736]\n", " [10. 19. 4.24841736]\n", " [10. 20. 2.65791145]\n", " [10.4472136 20.89442719 2.65791145]\n", " [10.89442719 21.78885438 0.2473708 ]\n", " [11.34164079 22.68328157 3.67111596]\n", " [11.78885438 23.57770876 9.73361847]\n", " [12.23606798 24.47213595 4.08649935]\n", " [12.68328157 25.36656315 2.01570756]\n", " [13.13049517 26.26099034 1.57680202]\n", " [13.57770876 27.15541753 4.57863609]\n", " [14.02492236 28.04984472 7.79916745]\n", " [14.47213595 28.94427191 7.79916745]\n", " [14.91934955 29.8386991 4.01318474]\n", " [15. 30. 3.98424547]\n", " [14.54533437 30.68199844 7.98752954]\n", " [13.99063418 31.51404873 3.23897928]\n", " [13.43593398 32.34609903 6.01450406]\n", " [12.88123379 33.17814932 2.21527156]\n", " [12.32653359 34.01019962 0.27510984]\n", " [11.77183339 34.84224991 3.34785821]\n", " [11.2171332 35.6743002 2.83141191]\n", " [10.662433 36.5063505 9.95544353]\n", " [10.1077328 37.33840079 8.46809499]\n", " [ 9.55303261 38.17045109 5.79207877]\n", " [ 8.99833241 39.00250138 1.48870911]\n", " [ 8.44363222 39.83455168 1.48870911]\n", " [ 7.88893202 40.66660197 8.44722858]\n", " [ 7.33423182 41.49865226 5.26646236]\n", " [ 6.77953163 42.33070256 9.541056 ]\n", " [ 6.22483143 43.16275285 1.72990087]\n", " [ 5.67013124 43.99480315 3.68136678]\n", " [ 5.11543104 44.82685344 4.97815591]\n", " [ 5. 45. 8.97383011]]\n", "SZ-coordinates : (array([ 0. , 1. , 2. , 3. , 4. ,\n", " 5. , 6. , 7. , 8. , 9. ,\n", " 10. , 11. , 12. , 13. , 14. ,\n", " 15. , 16. , 17. , 18. , 19. ,\n", " 20. , 21. , 22. , 23. , 24. ,\n", " 25. , 26. , 27. , 28. , 29. ,\n", " 30. , 31. , 31.18033989, 32. , 33. ,\n", " 34. , 35. , 36. , 37. , 38. ,\n", " 39. , 40. , 41. , 42. , 43. ,\n", " 44. , 45. , 46. , 47. , 48. ,\n", " 49. , 49.20809626]), array([6.17796835, 3.78758632, 1.54473996, 1.54473996, 2.24521166,\n", " 5.31537719, 5.31537719, 7.26549213, 7.26549213, 3.30763981,\n", " 3.08678037, 3.08678037, 4.82598289, 4.82598289, 3.90230366,\n", " 3.90230366, 8.76591086, 8.57796737, 4.24841736, 4.24841736,\n", " 2.65791145, 2.65791145, 0.2473708 , 3.67111596, 9.73361847,\n", " 4.08649935, 2.01570756, 1.57680202, 4.57863609, 7.79916745,\n", " 7.79916745, 4.01318474, 3.98424547, 7.98752954, 3.23897928,\n", " 6.01450406, 2.21527156, 0.27510984, 3.34785821, 2.83141191,\n", " 9.95544353, 8.46809499, 5.79207877, 1.48870911, 1.48870911,\n", " 8.44722858, 5.26646236, 9.541056 , 1.72990087, 3.68136678,\n", " 4.97815591, 8.97383011]))\n", "S-coordinates : [ 0. 1. 2. 3. 4. 5.\n", " 6. 7. 8. 9. 10. 11.\n", " 12. 13. 14. 15. 16. 17.\n", " 18. 19. 20. 21. 22. 23.\n", " 24. 25. 26. 27. 28. 29.\n", " 30. 31. 31.18033989 32. 33. 34.\n", " 35. 36. 37. 38. 39. 40.\n", " 41. 42. 43. 44. 45. 46.\n", " 47. 48. 49. 49.20809626]\n", "XY-coordinates and 1 if visible (0 otherwise) : [[ 5. 5. 1. ]\n", " [ 6. 5. 1. ]\n", " [ 7. 5. 1. ]\n", " [ 8. 5. 1. ]\n", " [ 9. 5. 1. ]\n", " [10. 5. 1. ]\n", " [10. 6. 1. ]\n", " [10. 7. 1. ]\n", " [10. 8. 1. ]\n", " [10. 9. 1. ]\n", " [10. 10. 1. ]\n", " [10. 11. 1. ]\n", " [10. 12. 1. ]\n", " [10. 13. 1. ]\n", " [10. 14. 1. ]\n", " [10. 15. 1. ]\n", " [10. 16. 1. ]\n", " [10. 17. 1. ]\n", " [10. 18. 1. ]\n", " [10. 19. 1. ]\n", " [10. 20. 1. ]\n", " [10.4472136 20.89442719 1. ]\n", " [10.89442719 21.78885438 1. ]\n", " [11.34164079 22.68328157 1. ]\n", " [11.78885438 23.57770876 1. ]\n", " [12.23606798 24.47213595 1. ]\n", " [12.68328157 25.36656315 1. ]\n", " [13.13049517 26.26099034 1. ]\n", " [13.57770876 27.15541753 1. ]\n", " [14.02492236 28.04984472 1. ]\n", " [14.47213595 28.94427191 1. ]\n", " [14.91934955 29.8386991 1. ]\n", " [15. 30. 1. ]\n", " [14.54533437 30.68199844 1. ]\n", " [13.99063418 31.51404873 1. ]\n", " [13.43593398 32.34609903 1. ]\n", " [12.88123379 33.17814932 1. ]\n", " [12.32653359 34.01019962 1. ]\n", " [11.77183339 34.84224991 1. ]\n", " [11.2171332 35.6743002 1. ]\n", " [10.662433 36.5063505 1. ]\n", " [10.1077328 37.33840079 1. ]\n", " [ 9.55303261 38.17045109 1. ]\n", " [ 8.99833241 39.00250138 1. ]\n", " [ 8.44363222 39.83455168 1. ]\n", " [ 7.88893202 40.66660197 1. ]\n", " [ 7.33423182 41.49865226 1. ]\n", " [ 6.77953163 42.33070256 1. ]\n", " [ 6.22483143 43.16275285 1. ]\n", " [ 5.67013124 43.99480315 1. ]\n", " [ 5.11543104 44.82685344 1. ]\n", " [ 5. 45. 1. ]]\n" ] } ], "source": [ "print('X-coordinates : ', v.x) # X coordinates\n", "print('Y-coordinates : ', v.y) # Y coordinates\n", "print('XY-coordinates : ', v.xy) # XY coordinates\n", "print('XZ-coordinates : ', v.xz) # XZ coordinates\n", "print('XYZ-coordinates : ', v.xyz) # XYZ coordinates\n", "print('SZ-coordinates : ', v.sz_curvi) # Curvilinear coordinates with Z values\n", "print('S-coordinates : ', v.s_curvi) # Curvilinear coordinates\n", "print('XY-coordinates and 1 if visible (0 otherwise) :', v.xyi) # XY coordinates and segment's visibility indicator\n", "print('XYZ-coordinates and 1 if visible (0 otherwise) :', v.xyzi) # XYZ coordinates and segment's visibility indicator\n", "print('1 if visible (0 otherwise) for each segment :', v.i) # segment's visibility indicator" ] }, { "cell_type": "code", "execution_count": null, "id": "e988db30", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "True\n", "True\n", "True\n", "True\n", "False\n", "True\n" ] } ], "source": [ "# Pour savoir si un setter est défini pour une propriété d'un objet Python,\n", "# on peut utiliser la fonction built-in `getattr` et vérifier l'attribut `fset` de la propriété.\n", "\n", "def has_setter(obj, prop_name):\n", " cls = obj.__class__\n", " prop = getattr(cls, prop_name, None)\n", " return isinstance(prop, property) and prop.fset is not None\n", "\n", "# Exemple d'utilisation :\n", "print(has_setter(v, 'z')) # Affiche True si un setter est défini pour la propriété 'z' de l'objet v\n", "print(has_setter(v, 'x')) # Affiche True si un setter est défini pour la propriété 'z' de l'objet v\n", "print(has_setter(v, 'y')) # Affiche True si un setter est défini pour la propriété 'z' de l'objet v\n", "print(has_setter(v, 'xy')) # Affiche True si un setter est défini pour la propriété 'z' de l'objet v\n", "print(has_setter(v, 'xz')) # Affiche True si un setter est défini pour la propriété 'z' de l'objet v\n", "print(has_setter(v, 'xyz')) # Affiche True si un setter est défini pour la propriété 'z' de l'objet v\n", "print(has_setter(v, 's_curvi')) # Affiche True si un setter est défini pour la propriété 'z' de l'objet v\n", "print(has_setter(v, 'sz_curvi')) # Affiche True si un setter est défini pour la propriété 'z' de l'objet v" ] } ], "metadata": { "kernelspec": { "display_name": "python3.11", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.9" } }, "nbformat": 4, "nbformat_minor": 5 }