Skip to content

Instantly share code, notes, and snippets.

@FredEckert
Created January 29, 2024 01:19
Show Gist options
  • Save FredEckert/a006c306f7ff7e82ce94699df44214e7 to your computer and use it in GitHub Desktop.
Save FredEckert/a006c306f7ff7e82ce94699df44214e7 to your computer and use it in GitHub Desktop.
3D perspective projection visualizer
# 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()
@FredEckert
Copy link
Author

Screenshot from 2024-01-28 20-03-21

@FredEckert
Copy link
Author

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:

  1. Defining the Cube: It starts by defining the coordinates of a unit cube's vertices.
  2. Setting Up Transformation: An affine transformation matrix and translation vector are calculated based on given input and output coordinate sets.
  3. Applying Transformation: The affine transformation is applied to each vertex of the cube.
  4. Visualization: Two 3D plots are generated using matplotlib's 3D plotting capabilities. The first plot shows the original cube, and the second one shows the transformed cube.

To create a 3D perspective projection visualizer in a similar style, we would follow these steps:

  1. Define 3D Objects: Like the unit cube in your script, we'd define the 3D coordinates of objects to be transformed.
  2. Perspective Transformation: Instead of an affine transformation, we would implement a perspective transformation. This involves defining a perspective transformation matrix that simulates the effect of perspective (objects appearing smaller as they are farther from the viewer).
  3. Applying the Transformation: Apply the perspective transformation to each vertex of the 3D objects.
  4. Visualization: Using matplotlib, we would plot the original and the perspective-transformed objects in a 3D space, allowing for a visual comparison.

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment