{"id":13688800,"url":"https://github.com/rougier/numpy-glm","last_synced_at":"2025-03-25T10:32:13.857Z","repository":{"id":188018160,"uuid":"677949687","full_name":"rougier/numpy-glm","owner":"rougier","description":"GL Mathematics for Numpy","archived":false,"fork":false,"pushed_at":"2024-03-23T13:24:17.000Z","size":1904,"stargazers_count":21,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-25T02:06:08.574Z","etag":null,"topics":["3d-graphics","glm","numpy","opengl"],"latest_commit_sha":null,"homepage":"https://rougier.github.io/numpy-glm/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rougier.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-08-13T07:20:34.000Z","updated_at":"2024-10-25T07:38:58.000Z","dependencies_parsed_at":"2023-08-13T10:22:59.415Z","dependency_job_id":"8a7e0283-de3c-4642-936e-22d1944f5cd4","html_url":"https://github.com/rougier/numpy-glm","commit_stats":{"total_commits":31,"total_committers":3,"mean_commits":"10.333333333333334","dds":"0.32258064516129037","last_synced_commit":"87b96659f2ae9f82402bacbb2fe08101c7390247"},"previous_names":["rougier/numpy-glm"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rougier%2Fnumpy-glm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rougier%2Fnumpy-glm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rougier%2Fnumpy-glm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rougier%2Fnumpy-glm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rougier","download_url":"https://codeload.github.com/rougier/numpy-glm/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245444227,"owners_count":20616342,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["3d-graphics","glm","numpy","opengl"],"created_at":"2024-08-02T15:01:23.022Z","updated_at":"2025-03-25T10:32:13.096Z","avatar_url":"https://github.com/rougier.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"\n\u003cimg align=\"right\" width=\"30%\" src=\"examples/bunny.png\"\u003e\n\n# GL Mathematics with Numpy\n\nGLM offers a set of objects and functions to ease the design of 3D applications while taking advantage of the numpy library. The main objects are vectors (`vec2`, `vec3`, `vec4`), matrices (`mat2`, `mat3`, `mat4`) and vectorized lists (`vlist`).  Vectors and matrices possess several variants depending on the base type (see below) and are memory tracked. This means it is possible to know anytime the smallest contiguous block of memory that has changed. This can be used to maintain a GPU copy up-to-date.\n\n* [Example usages](#example-usage)\n* [Vectors \u0026 matrices](#vectors--matrices)\n* [Vectorized lists](#vectorized-lists)\n* [GL and GLU API](#gl-and-glu)\n\n\n## Example usage\n\nThis is the code to get the 3D bunny display on the top-right corner. You'll need  [meshio](https://github.com/nschloe/meshio) to read the mesh file and [matplotlib](https://matplotlib.org/) to display it.\n\n```python\nimport glm\nimport numpy as np\n\n# Read mesh, get vertices (vec3) and face indices (int)\nimport meshio\nmesh = meshio.read(\"bunny.obj\")\nvertices, indices = mesh.points, mesh.cells[0].data\n\n# Transform: Model / View / Projection matrix (MVP)\nMVP = glm.perspective(25, 1, 1, 100) @ glm.translate(0.1, -0.45, -2.5)\nMVP = MVP @ glm.xrotate(20) @ glm.yrotate(45) @ glm.scale(5)\n\n# Apply transform\nvertices = glm.to_vec3(glm.to_vec4(vertices) @ M.T)\n\n# Generate faces and sort them\nfaces = vertices[indices]\nfaces = faces[np.argsort(-faces[...,2].mean(axis=1))]\n\n# Render faces using matplotlib\nimport matplotlib.pyplot as plt\nfrom matplotlib.collections import PolyCollection\nfig = plt.figure(figsize=(6,6))\nax = fig.add_axes([0,0,1,1], aspect=1, frameon=False, xlim=[-1,+1], ylim=[-1,+1])\nax.add_collection(PolyCollection(faces[...,:2], alpha=0.85, linewidth = 0.5,\n                                 facecolor=\"white\", edgecolor=\"black\"))\nplt.show()\n```\n\n## Vectors \u0026 Matrices\n\nThe generic notation is:\n\n* `TvecN` for vectors\n* `TmatN` for matrices,\n\nwith T in [`b`, `i` , `u`, `h`, `d`, `f`, Ø] and N in [2,3,4].\n\n| Symbol | Type             | Size    | Dtype         |\n|--------|------------------|---------|---------------|\n| `b`    | unsigned integer | 8 bits  | `np.uint8`    |\n| `i`    | signed integer   | 32 bits | `np.int32`    |\n| `u`    | unsigned integer | 32 bits | `np.uint32`   |\n| `h`    | float            | 16 bits | `np.float16`  |\n|  Ø     | float            | 32 bits | `np.float32`  |\n| `h`    | float            | 64 bits | `np.float64`  |\n\n```python\nimport glm\n\n# 10 x 3-components vectors of integers (32 bits)\nV = glm.ivec3(10)\nV.xyz = 1,2,3\n\n# 10 x 3-components vectors of float (32 bits)\nV = glm.vec3(10)\n\n# Conversion from vec3 to vec4 (w set to 1)\nV = glm.vec4(V)\n\n# Conversion from vec4 to vec3 (xyz divided by w)\nV = glm.vec3(V)\n\n# A 4x4 matrix of floats (32 bits)\nM = glm.mat4()\nM.xyzw = 1,2,3,1\n```\n\n\u003cp align=\"right\"\u003e\u003ca href=\"#gl-mathematics-with-numpy\"\u003e\u003cb\u003eBack to top\u003c/b\u003e\u003c/a\u003e\u003c/p\u003e\n\n\n\n## Vectorized lists \n\nVectorized lists (`vlist`) correspond to ragged arrays where items have the same type but different lengths. There are three way to declare a vectorized list.\n\n```python\nimport glm\n\n# A list of 3 groups of vectors with size 3,3,4\nV = glm.vlist([glm.vec4(3), glm.vec4(3), glm.vec4(3)])\n\n# A list of 3 groups of vectors with size 3,3,4\nV = glm.vlist(glm.vec4(10), [3,3,4])\n\n# A list of 5 groups of vectors with size 2\nV = glm.vlist(glm.vec4(10), 2)\n\n# Display each item\nfor v in V: print(v)\n```\n\nThe underlying structure is a regular numpy array (`vlist.data`).\n\n\u003cp align=\"right\"\u003e\u003ca href=\"#gl-mathematics-with-numpy\"\u003e\u003cb\u003eBack to top\u003c/b\u003e\u003c/a\u003e\u003c/p\u003e\n\n\n## GL and GLU\n\n* glm.**[viewport](https://registry.khronos.org/OpenGL-Refpages/gl4/html/glViewport.xhtml)**(x, y, width, height, *dtype=np.float32, transpose=False*)\n  \n  \u003e Return a 4x4 matrix that transforms normalized device coordinates\n  \u003e to window coordinates.\n  \u003e\n  \u003e | Parameters  | Type    | Description                    |\n  \u003e |:------------|:--------|:-------------------------------|\n  \u003e | `x`         | float   | Viewport x origin (lower left) |\n  \u003e | `y`         | float   | Viewport x origin (lower left) |\n  \u003e | `width`     | float   | Viewport width                 |\n  \u003e | `height`    | float   | Viewport height                |\n  \u003e | `dtype`     | dtype   | Dtype of the resulting matrix  |\n  \u003e | `transpose` | boolean | Whether to transpose result    |\n\n* glm.**[ortho](https://registry.khronos.org/OpenGL-Refpages/gl2.1/xhtml/glOrtho.xml)**(left, right, bottom, top, znear, zfar, *dtype=np.float32, transpose=False*)\n\n  \u003e Return a 4x4 matrix that produces a parallel projection. \n  \u003e\n  \u003e | Parameters  | Type    | Description                    |\n  \u003e |:------------|:--------|:-------------------------------|\n  \u003e | `left`      | float   | Left clipping plane            |\n  \u003e | `right`     | float   | Right clipping plane           |\n  \u003e | `bottom`    | float   | Bottom clipping plane          |\n  \u003e | `top`       | float   | Top clipping plane             |\n  \u003e | `znear`     | float   | Near clipping plane            |\n  \u003e | `zfar`      | float   | Far clipping plane             |\n  \u003e | `dtype`     | dtype   | Dtype of the resulting matrix  |\n  \u003e | `transpose` | boolean | Whether to transpose result    |\n\n* glm.**[frustum](https://registry.khronos.org/OpenGL-Refpages/gl2.1/xhtml/glFrustum.xml)**(left, right, bottom, top, znear, zfar, *dtype=np.float32, transpose=False*)\n\n  \u003e Return a 4x4 matrix that produces a perspective projection. \n  \u003e\n  \u003e | Parameters  | Type    | Description                    |\n  \u003e |:------------|:--------|:-------------------------------|\n  \u003e | `left`      | float   | Left clipping plane            |\n  \u003e | `right`     | float   | Right clipping plane           |\n  \u003e | `bottom`    | float   | Bottom clipping plane          |\n  \u003e | `top`       | float   | Top clipping plane             |\n  \u003e | `znear`     | float   | Near clipping plane            |\n  \u003e | `zfar`      | float   | Far clipping plane             |\n  \u003e | `dtype`     | dtype   | Dtype of the resulting matrix  |\n  \u003e | `transpose` | boolean | Whether to transpose result    |\n\n* glm.**[perpective](https://registry.khronos.org/OpenGL-Refpages/gl2.1/xhtml/gluPerspective.xml)**(fovy, aspect, znear, zfar, *dtype=np.float32, transpose=False*)\n\n  \u003e Return a 4x4 matrix corresponding to a viewing frustum into the\n  \u003e world coordinate system. In general, the aspect ratio in\n  \u003e perspective should match the aspect ratio of the associated\n  \u003e viewport. For example, aspect = 2.0 means the viewer's angle of\n  \u003e view is twice as wide in x as it is in y. If the viewport is twice\n  \u003e as wide as it is tall, it displays the image without distortion.\n  \u003e  \n  \u003e | Parameters  | Type    | Description                    |\n  \u003e |:------------|:--------|:-------------------------------|\n  \u003e | `fovy`      | float   | Field of view angle (degrees in the y direction |\n  \u003e | `aspect`    | float   | Aspect ratio that determines the field of view in the x direction |\n  \u003e | `znear`     | float   | Near clipping plane            |\n  \u003e | `zfar`      | float   | Far clipping plane             |\n  \u003e | `dtype`     | dtype   | Dtype of the resulting matrix  |\n  \u003e | `transpose` | boolean | Whether to transpose result    |\n\n* glm.**[scale](https://registry.khronos.org/OpenGL-Refpages/gl2.1/xhtml/glScale.xml)**(x, y, z, *dtype=np.float32, transpose=False*)\n  \n  \u003e Return a 4x4 matrix for nonuniform scaling along the x, y, and z\n  \u003e axes. The three parameters indicate the desired scale factor along\n  \u003e each of the three axes.\n  \u003e\n  \u003e | Parameters  | Type    | Description                    |\n  \u003e |:------------|:--------|:-------------------------------|\n  \u003e | `x`         | float   | x scale factor                 |\n  \u003e | `y`         | float   | y scale factor                 |\n  \u003e | `z`         | float   | z scale factor                 |\n  \u003e | `dtype`     | dtype   | Dtype of the resulting matrix  |\n  \u003e | `transpose` | boolean | Whether to transpose result    |\n\n* glm.**fit**(V)\n\n  \u003e Return vertices fitted to the normalized cube.\n  \u003e\n  \u003e | Parameters  | Type    | Description                    |\n  \u003e |:------------|:--------|:-------------------------------|\n  \u003e | `V`         | vecN    | Vertices to fit                |\n\n* glm.**[rotate](https://registry.khronos.org/OpenGL-Refpages/gl2.1/xhtml/glRotate.xml)**(angle, x, y, z, *dtype=np.float32, transpose=False*)\n  \n  \u003e Return a 4x4 matrix for a rotation of angle degrees around the\n  \u003e vector (x,y,z).\n  \u003e\n  \u003e | Parameters  | Type    | Description                    |\n  \u003e |:------------|:--------|:-------------------------------|\n  \u003e | `angle`     | float   | angle in degrees               |\n  \u003e | `x`         | float   | x scale factor                 |\n  \u003e | `y`         | float   | y scale factor                 |\n  \u003e | `z`         | float   | z scale factor                 |\n  \u003e | `dtype`     | dtype   | Dtype of the resulting matrix  |\n  \u003e | `transpose` | boolean | Whether to transpose result    |\n\n* glm.**[xrotate]()**(angle, *dtype=np.float32, transpose=False*)\n  \n  \u003e Return a 4x4 matrix for a rotation of angle degrees around the\n  \u003e x axis\n  \u003e\n  \u003e | Parameters  | Type    | Description                    |\n  \u003e |:------------|:--------|:-------------------------------|\n  \u003e | `angle`     | float   | angle in degrees               |\n  \u003e | `dtype`     | dtype   | Dtype of the resulting matrix  |\n  \u003e | `transpose` | boolean | Whether to transpose result    |\n\n* glm.**[yrotate]()**(angle, *dtype=np.float32, transpose=False*)\n  \n  \u003e Return a 4x4 matrix for a rotation of angle degrees around the\n  \u003e y axis\n  \u003e\n  \u003e | Parameters  | Type    | Description                    |\n  \u003e |:------------|:--------|:-------------------------------|\n  \u003e | `angle`     | float   | angle in degrees               |\n  \u003e | `dtype`     | dtype   | Dtype of the resulting matrix  |\n  \u003e | `transpose` | boolean | Whether to transpose result    |\n\n* glm.**[zrotate]()**(angle, *dtype=np.float32, transpose=False*)\n  \n  \u003e Return a 4x4 matrix for a rotation of angle degrees around the\n  \u003e z axis\n  \u003e\n  \u003e | Parameters  | Type    | Description                    |\n  \u003e |:------------|:--------|:-------------------------------|\n  \u003e | `angle`     | float   | angle in degrees               |\n  \u003e | `dtype`     | dtype   | Dtype of the resulting matrix  |\n  \u003e | `transpose` | boolean | Whether to transpose result    |\n\n* glm.**align**(U, V, dtype=np.float32, transpose=False)\n\n  \u003e Return the rotation matrix M that aligns U to V such that V = U @\n  \u003e M.T\n  \u003e\n  \u003e | Parameters  | Type    | Description                    |\n  \u003e |:------------|:--------|:-------------------------------|\n  \u003e | `U`         | vecN    | First vector                   |\n  \u003e | `V`         | vecN    | Second vector                  |\n  \u003e | `dtype`     | dtype   | Dtype of the resulting matrix  |\n  \u003e | `transpose` | boolean | Whether to transpose result    |\n\n* glm.**[translate](https://registry.khronos.org/OpenGL-Refpages/gl2.1/xhtml/glTranslate.xml)**(x, y, z, *dtype=np.float32, transpose=False*)\n  \n  \u003e Return a 4x4 matrix for a translation by (x,y,z).\n  \u003e\n  \u003e | Parameters  | Type    | Description                    |\n  \u003e |:------------|:--------|:-------------------------------|\n  \u003e | `x`         | float   | x scale factor                 |\n  \u003e | `y`         | float   | y scale factor                 |\n  \u003e | `z`         | float   | z scale factor                 |\n  \u003e | `dtype`     | dtype   | Dtype of the resulting matrix  |\n  \u003e | `transpose` | boolean | Whether to transpose result    |\n\n* glm.**center**(V)\n\n  \u003e Return vertices centered aroung the origin\n  \u003e\n  \u003e | Parameters  | Type    | Description                    |\n  \u003e |:------------|:--------|:-------------------------------|\n  \u003e | `V`         | vecN    | Vectices to center             |\n\n* glm.**[lookat](https://registry.khronos.org/OpenGL-Refpages/gl2.1/xhtml/gluLookAt.xml)**(eye, center, up, *dtype=np.float32, transpose=False*)\n\n  \u003e Return a 4x4 a viewing matrix derived from an eye point, a\n  \u003e reference point indicating the center of the scene, and an UP\n  \u003e vector.\n  \u003e\n  \u003e The matrix maps the reference point to the negative z axis and the\n  \u003e eye point to the origin. When a typical projection matrix is used,\n  \u003e the center of the scene therefore maps to the center of the\n  \u003e viewport. Similarly, the direction described by the UP vector\n  \u003e projected onto the viewing plane is mapped to the positive y axis\n  \u003e so that it points upward in the viewport. The UP vector must not\n  \u003e be parallel to the line of sight from the eye point to the\n  \u003e reference point.\n  \u003e\n  \u003e | Parameters  | Type    | Description                    |\n  \u003e |:------------|:--------|:-------------------------------|\n  \u003e | `eye`       | vec3    | Eye position                   |\n  \u003e | `center`    | vec3    | Reference point                |\n  \u003e | `up`        | vec3    | Direction of the up vector     |\n  \u003e | `dtype`     | dtype   | Dtype of the resulting matrix  |\n  \u003e | `transpose` | boolean | Whether to transpose result    |\n\n* glm.**[normalize](https://registry.khronos.org/OpenGL-Refpages/gl4/html/normalize.xhtml)**(V)\n\n  \u003e Return the unit vector in the same direction as the original vector\n  \u003e\n  \u003e | Parameters  | Type    | Description                    |\n  \u003e |:------------|:--------|:-------------------------------|\n  \u003e | `V`         | vecN    | Vector to normalize            |\n\n* glm.**[clamp](https://registry.khronos.org/OpenGL-Refpages/gl4/html/clamp.xhtml)**(value, vmin, vmax)\n\n  \u003e Return value bound between vmin and vmax\n  \u003e\n  \u003e | Parameters  | Type             | Description                    |\n  \u003e |:------------|:-----------------|:-------------------------------|\n  \u003e | `value`     | float or vecN    | Value to normalize             |\n  \u003e | `vmin`      | float            | Minimum value                  |\n  \u003e | `vmax`      | float            | Maximum value                  |\n\n\u003cp align=\"right\"\u003e\u003ca href=\"#gl-mathematics-with-numpy\"\u003e\u003cb\u003eBack to top\u003c/b\u003e\u003c/a\u003e\u003c/p\u003e\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frougier%2Fnumpy-glm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frougier%2Fnumpy-glm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frougier%2Fnumpy-glm/lists"}