# Path: nnutils/geom_utils.py
# def obj_to_cam(verts, Rmat, Tmat,nmesh,n_hypo,skin,tocam=True):
#     """
#     transform from canonical object coordinates to camera coordinates
#     """
#     verts = verts.clone()
#     Rmat = Rmat.clone()
#     Tmat = Tmat.clone()
#     verts = verts.view(-1,verts.shape[1],3)
# 
#     bodyR = Rmat[::nmesh].clone()
#     bodyT = Tmat[::nmesh].clone()
#     if nmesh>1:
#         vs = []
#         for k in range(nmesh-1):
#             partR = Rmat[k+1::nmesh].clone()
#             partT = Tmat[k+1::nmesh].clone()
#             vs.append( (verts.matmul(partR) + partT)[:,np.newaxis] )
#         vs = torch.cat(vs,1) # N, K, Nv, 3
#         vs = (vs * skin).sum(1)
#     else:
#         vs = verts
#     
#     if tocam:
#         vs =  vs.clone().matmul(bodyR) + bodyT
#     else:
#         vs = vs.clone()
#     return vs
# 
# def pinhole_cam(verts,pp,fl):
#     n_hypo = verts.shape[0] // pp.shape[0]
#     pp = pp.clone()[:,None].repeat(1,n_hypo,1).view(-1,2)
#     fl = fl.clone()[:,None].view(-1,1)
#     verts = verts.clone()
#     verts[:,:,1] = pp[:,1:2]+verts[:, :, 1].clone()*fl/ verts[:,:,2].clone()
#     verts[:,:,0] = pp[:,0:1]+verts[:, :, 0].clone()*fl/ verts[:,:,2].clone()
#     return verts


import sys, os
import subprocess
import imageio
import glob
import matplotlib.pyplot as plt
import numpy as np
import torch
import cv2
import pdb
import soft_renderer as sr
import argparse
import trimesh
import pyrender
import configparser
from ext_utils.badja_data import BADJAData
from ext_utils.joint_catalog import SMALJointInfo
from nnutils.geom_utils import obj_to_cam, pinhole_cam
from pyrender import IntrinsicsCamera,Mesh, Node, Scene,OffscreenRenderer
            refmesh.vertices /= 1.2*np.abs(refmesh.vertices).max()
            refcam = all_cam[0].copy()
            refcam[:3,:3] = refcam[:3,:3].dot(cv2.Rodrigues(np.asarray([0.,-i*2*np.pi/size,0.]))[0])
            refcam[:2,3] = 0  # trans xy
            refcam[2,3] = 20 # depth
            if args.cam_type=='perspective':
                refcam[3,2] = refimg.shape[1]/2 # px py
                refcam[3,3] = refimg.shape[0]/2 # px py
                refcam[3,:2] = 8*img_size/2 # fl
            else:
                refcam[3,2] = refimg.shape[1]/2 # px py
                refcam[3,3] = refimg.shape[1]/2 # px py
                refcam[3,:2] =0.5 * img_size/2 # fl
        else:
            refimg, refsil, refkp, refvis, refname = all_anno[i]
            print('%s'%(refname))
            img_size = max(refimg.shape)
            renderer_softflf.rasterizer.image_size = img_size
            refmesh = all_mesh[i]
            refcam = all_cam[i]
        currcam = np.concatenate([refcam[:3,:4],np.asarray([[0,0,0,1]])],0)
        if i==0:
            initcam = currcam.copy()
        
        refface = torch.Tensor(refmesh.faces[None]).cuda()
        verts = torch.Tensor(refmesh.vertices[None]).cuda()
        Rmat =  torch.Tensor(refcam[None,:3,:3]).cuda()
        Tmat =  torch.Tensor(refcam[None,:3,3]).cuda()
        ppoint =refcam[3,2:4]
        scale = refcam[3,0]

# Path: nnutils/geom_utils.py
# def obj_to_cam(verts, Rmat, Tmat,nmesh,n_hypo,skin,tocam=True):
#     """
#     transform from canonical object coordinates to camera coordinates
#     """
#     verts = verts.clone()
#     Rmat = Rmat.clone()
#     Tmat = Tmat.clone()
#     verts = verts.view(-1,verts.shape[1],3)
# 
#     bodyR = Rmat[::nmesh].clone()
#     bodyT = Tmat[::nmesh].clone()
#     if nmesh>1:
#         vs = []
#         for k in range(nmesh-1):
#             partR = Rmat[k+1::nmesh].clone()
#             partT = Tmat[k+1::nmesh].clone()
#             vs.append( (verts.matmul(partR) + partT)[:,np.newaxis] )
#         vs = torch.cat(vs,1) # N, K, Nv, 3
#         vs = (vs * skin).sum(1)
#     else:
#         vs = verts
#     
#     if tocam:
#         vs =  vs.clone().matmul(bodyR) + bodyT
#     else:
#         vs = vs.clone()
#     return vs
# 
# def pinhole_cam(verts,pp,fl):
#     n_hypo = verts.shape[0] // pp.shape[0]
#     pp = pp.clone()[:,None].repeat(1,n_hypo,1).view(-1,2)
#     fl = fl.clone()[:,None].view(-1,1)
#     verts = verts.clone()
#     verts[:,:,1] = pp[:,1:2]+verts[:, :, 1].clone()*fl/ verts[:,:,2].clone()
#     verts[:,:,0] = pp[:,0:1]+verts[:, :, 0].clone()*fl/ verts[:,:,2].clone()
#     return verts


# Path: render_vis.py
import sys, os
import subprocess
import imageio
import glob
import matplotlib.pyplot as plt
import numpy as np
import torch
import cv2
import pdb
import soft_renderer as sr
import argparse
import trimesh
import pyrender
import configparser
from ext_utils.badja_data import BADJAData
from ext_utils.joint_catalog import SMALJointInfo
from nnutils.geom_utils import obj_to_cam, pinhole_cam
from pyrender import IntrinsicsCamera,Mesh, Node, Scene,OffscreenRenderer

            refmesh.vertices /= 1.2*np.abs(refmesh.vertices).max()
            refcam = all_cam[0].copy()
            refcam[:3,:3] = refcam[:3,:3].dot(cv2.Rodrigues(np.asarray([0.,-i*2*np.pi/size,0.]))[0])
            refcam[:2,3] = 0  # trans xy
            refcam[2,3] = 20 # depth
            if args.cam_type=='perspective':
                refcam[3,2] = refimg.shape[1]/2 # px py
                refcam[3,3] = refimg.shape[0]/2 # px py
                refcam[3,:2] = 8*img_size/2 # fl
            else:
                refcam[3,2] = refimg.shape[1]/2 # px py
                refcam[3,3] = refimg.shape[1]/2 # px py
                refcam[3,:2] =0.5 * img_size/2 # fl
        else:
            refimg, refsil, refkp, refvis, refname = all_anno[i]
            print('%s'%(refname))
            img_size = max(refimg.shape)
            renderer_softflf.rasterizer.image_size = img_size
            refmesh = all_mesh[i]
            refcam = all_cam[i]
        currcam = np.concatenate([refcam[:3,:4],np.asarray([[0,0,0,1]])],0)
        if i==0:
            initcam = currcam.copy()
        
        refface = torch.Tensor(refmesh.faces[None]).cuda()
        verts = torch.Tensor(refmesh.vertices[None]).cuda()
        Rmat =  torch.Tensor(refcam[None,:3,:3]).cuda()
        Tmat =  torch.Tensor(refcam[None,:3,3]).cuda()
        ppoint =refcam[3,2:4]
        scale = refcam[3,0]

        verts = obj_to_cam(verts, Rmat, Tmat,nmesh=1,n_hypo=1,skin=None)