# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 2
#     language: python
#     name: python2
# ---

# +
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import scipy

from src import inception_v3_imagenet
from src import imagenet_labels
from src import utils
from src.diff_renderer import make_render_op
# -

from src.utils import angles_to_matrix

# %matplotlib inline

width, height = 1000, 1000
mesh = utils.load_obj('resources/dog.obj')
original_texture = mesh.texture_image.copy()
render_op = make_render_op(mesh, width, height)

trans = [0, 0, 2.6]
rotation = [0.4, np.pi+.7, 2.9]
fscale = 0.4
bgcolor = [0.9, 0.9, 0.9]
texture_image = mesh.texture_image

view_matrix = np.hstack((angles_to_matrix(rotation) , np.reshape(trans, (3, 1)) )) 
view_matrix

view2_matrix = np.vstack((view_matrix, np.array([0, 0, 0, 1])))
view2_matrix

# +
pixel_center_offset = 0.5
near = 0.1
far = 100.

f = 0.5 * (fmat[0] + fmat[1])

center = [width/2.,height/2.]

right  =  (width-(center[0]+pixel_center_offset)) * (near/f)
left   =           -(center[0]+pixel_center_offset)  * (near/f)
top    = -(height-(center[1]+pixel_center_offset)) * (near/f)
bottom =            (center[1]+pixel_center_offset)  * (near/f)


A = (right + left) / (right - left)
B = (top + bottom) / (top - bottom)
C = (far + near) / (far - near)
D = (2 * far * near) / (far - near)

projMatrix = np.array([
    [2 * near / (right - left), 0, A, 0],
    [0, 2 * near / (top - bottom), B, 0],
    [0, 0, C, D],
    [0, 0, -1, 0]
])
# -

homo_v = np.hstack((mesh.v, np.ones((mesh.v.shape[0], 1) )))
homo_v

# +
# proj_matrix = camera_matrix.dot(view_matrix)
proj_matrix = projMatrix.dot(view2_matrix)

# unhomo(proj_matrix.dot(homo_v[0,:]))
# -

abnormal = proj_matrix.dot(homo_v.reshape((-1, 4, 1)))[:, :, 0]
XY = (abnormal[:,:] / abnormal[3,:]).T
XY

# +

# plt.set_autoscale_on(False)
plt.figure(figsize=(5,5))
plt.scatter(XY[:,0], XY[:, 1], c = XY[:, 2], s=3)
# plt.axes().set_aspect('equal', 'datalim')

plt.xlim([1, -1])
plt.ylim([1, -1])


# -



StarcoderdataJupyterScriptsDedupFiltered