Created
January 29, 2024 01:19
-
-
Save FredEckert/a006c306f7ff7e82ce94699df44214e7 to your computer and use it in GitHub Desktop.
3D perspective projection visualizer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# title: 3D_perspective_projection_visualizer.py | |
# reference: ChatGPT-4> Given the python program https://gist.github.com/FredEckert/9527d4ec1fbd1be2e46bd920225ca1f4, | |
# can you create a perspective projection visualizer in a similar style>? | |
import numpy as np | |
import matplotlib.pyplot as plt | |
from mpl_toolkits.mplot3d import Axes3D | |
from mpl_toolkits.mplot3d.art3d import Poly3DCollection | |
def perspective_transform(matrix, points): | |
""" | |
Apply a perspective transformation to a set of points. | |
""" | |
transformed_points = np.dot(matrix, np.hstack((points, np.ones((points.shape[0], 1)))).T) | |
transformed_points = transformed_points / transformed_points[-1, :] | |
return transformed_points[:-1, :].T | |
def define_cube(): | |
""" | |
Define the vertices of a unit cube. | |
""" | |
return np.array([[-1, -1, -1], | |
[1, -1, -1], | |
[1, 1, -1], | |
[-1, 1, -1], | |
[-1, -1, 1], | |
[1, -1, 1], | |
[1, 1, 1], | |
[-1, 1, 1]]) | |
def plot_cube(ax, points, title): | |
""" | |
Plot a cube on a given Axes. | |
""" | |
ax.scatter3D(points[:, 0], points[:, 1], points[:, 2]) | |
# List of sides' polygons of the cube | |
verts = [[points[0], points[1], points[2], points[3]], | |
[points[4], points[5], points[6], points[7]], | |
[points[0], points[1], points[5], points[4]], | |
[points[2], points[3], points[7], points[6]], | |
[points[1], points[2], points[6], points[5]], | |
[points[4], points[7], points[3], points[0]]] | |
# Plot sides | |
ax.add_collection3d(Poly3DCollection(verts, linewidths=1, edgecolors='k', facecolors=(0,0,1,0.1))) | |
ax.set_xlabel('X') | |
ax.set_ylabel('Y') | |
ax.set_zlabel('Z') | |
ax.set_title(title) | |
# Define the perspective transformation matrix | |
perspective_matrix = np.array([[1, 0, 0, 0], | |
[0, 1, 0, 0], | |
[0, 0, 1, 0], | |
[0, 0, -1/10, 1]]) # Perspective factor -1/10 | |
# Define the cube | |
cube = define_cube() | |
# Apply perspective transformation | |
transformed_cube = perspective_transform(perspective_matrix, cube) | |
# Plotting | |
fig = plt.figure(figsize=(12, 6)) | |
ax1 = fig.add_subplot(121, projection='3d') | |
ax2 = fig.add_subplot(122, projection='3d') | |
plot_cube(ax1, cube, 'Original Cube') | |
plot_cube(ax2, transformed_cube, 'Perspective Transformed Cube') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ChatGPT: The provided Python code is a script for visualizing 3D affine transformations using matplotlib and numpy. It demonstrates the transformation of a unit cube (defined by its 8 vertices) using an affine transformation matrix. The script performs the following steps:
To create a 3D perspective projection visualizer in a similar style, we would follow these steps:
This would create an interactive way to visualize how objects in 3D space are perceived differently under perspective projection compared to their original form. Would you like to proceed with the development of this script?