support parallel rendering

This commit is contained in:
fxia22 2017-08-17 16:00:27 -07:00
parent 8919a8378d
commit 24246dee15
5 changed files with 167 additions and 42 deletions

7
.gitignore vendored
View File

@ -3,4 +3,9 @@
*.so
*.egg-info/
.DS_Store
data/
data/
pw
*.o
*.ipynb
*.out
*.pth

3
dev/build.sh Executable file
View File

@ -0,0 +1,3 @@
#!/bin/bash
g++ -std=c++11 -fopenmp -Wall -c render.cpp -shared -fPIC -D_GLIBCXX_USE_CXX11_ABI=0
g++ -shared -lgomp -lrt -o render.so render.o

114
dev/render.cpp Normal file
View File

@ -0,0 +1,114 @@
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <math.h>
#include <omp.h>
using namespace std;
extern "C"{
void render(int h,int w,unsigned char * img, float * depth,float * pose, unsigned char * render, float * depth_render){
int ih, iw, i, ic;
omp_set_num_threads(8);
float * points3d = (float*) malloc(sizeof(float) * h * w * 4);
float * points3d_after = (float*) malloc(sizeof(float) * h * w * 3);
float * points3d_polar = (float*) malloc(sizeof(float) * h * w * 3);
//for (i = 0; i < 5; i++) {
// printf("%f ", pose[i]);
//}
//printf("\n");
#pragma omp parallel for collapse(2)
for (ih = 0; ih < h; ih ++ ) {
for (iw = 0; iw < w; iw ++ ) {
render[(ih * w + iw) * 3 +0] = 0;
render[(ih * w + iw) * 3 +1] = 0;
render[(ih * w + iw) * 3 +2] = 0;
depth_render[ih * w + iw] = 1e10;
}
}
#pragma omp parallel for collapse(2)
for (ih = 0; ih < h; ih ++ ) {
for (iw = 0; iw < w; iw ++ ) {
float depth_point = depth[ih * w + iw] * 128.0;
//printf("%f %f\n", depth[ih * w + iw], depth_point);
float phi = ((float)(ih) + 0.5) / float(h) * M_PI;
float theta = ((float)(iw) + 0.5) / float(w) * 2 * M_PI + M_PI;
points3d[(ih * w + iw) * 4 + 0] = depth_point * sin(phi) * cos(theta);
points3d[(ih * w + iw) * 4 + 1] = depth_point * sin(phi) * sin(theta);
points3d[(ih * w + iw) * 4 + 2] = depth_point * cos(phi);
points3d[(ih * w + iw) * 4 + 3] = 1;
}
}
//float alpha, beta, gamma, x, y, z;
//x = pose[0];
//y = pose[1];
//z = pose[2];
//alpha = pose[4];
//beta = pose[3];
//gamma = pose[5];
float transformation_matrix[16];
for (i = 0; i < 16; i++) transformation_matrix[i] = pose[i];
#pragma omp parallel for collapse(3)
for (ih = 0; ih < h; ih ++ ) {
for (iw = 0; iw < w; iw ++ ) {
for (ic = 0; ic < 3; ic ++) {
points3d_after[(ih * w + iw) * 3 + ic] = points3d[(ih * w + iw) * 4 + 0] * transformation_matrix[4 * ic + 0]
+points3d[(ih * w + iw) * 4 + 1] * transformation_matrix[4 * ic + 1]
+points3d[(ih * w + iw) * 4 + 2] * transformation_matrix[4 * ic + 2]
+points3d[(ih * w + iw) * 4 + 3] * transformation_matrix[4 * ic + 3];
}
}
}
#pragma omp parallel for collapse(2)
for (ih = 0; ih < h; ih ++ ) {
for (iw = 0; iw < w; iw ++ ) {
float x = points3d_after[(ih * w + iw) * 3 + 0];
float y = points3d_after[(ih * w + iw) * 3 + 1];
float z = points3d_after[(ih * w + iw) * 3 + 2];
points3d_polar[(ih * w + iw) * 3 + 0] = sqrt(x * x + y * y + z * z);
points3d_polar[(ih * w + iw) * 3 + 1] = atan2(y, x);
points3d_polar[(ih * w + iw) * 3 + 2] = atan2(sqrt(x * x + y * y), z);
}
}
#pragma omp parallel for collapse(2)
for (ih = 0; ih < h; ih ++ ) {
for (iw = 0; iw < w; iw ++ ) {
int x = round((points3d_polar[(ih * w + iw) * 3 + 1] + M_PI)/(2*M_PI) * w - 0.5);
int y = round((points3d_polar[(ih * w + iw) * 3 + 2])/M_PI * h - 0.5);
//printf("%d %d\n", x, y);
if (points3d_polar[(ih * w + iw) * 3 + 0] < depth_render[(y * w + x)]) {
render[(y * w + x) * 3 + 0] = img[(ih * w + iw) * 3 +0];
render[(y * w + x) * 3 + 1] = img[(ih * w + iw) * 3 +1];
render[(y * w + x) * 3 + 2] = img[(ih * w + iw) * 3 +2];
depth_render[(y * w + x)] = points3d_polar[(ih * w + iw) * 3 + 0];
}
}
}
#pragma omp parallel for collapse(2)
for (ih = 0; ih < h; ih ++ ) {
for (iw = 0; iw < w; iw ++ ) {
if (depth_render[ih * w + iw] > 1e5) depth_render[ih * w + iw] = 0;
}
}
free(points3d);
free(points3d_after);
free(points3d_polar);
}
}//extern "C"

View File

@ -13,7 +13,7 @@ from numpy import cos, sin
import utils
showsz = 256
mousex,mousey=0.5,0.5
changed=True
pitch,yaw,x,y,z = 0,0,0,0,0
@ -24,7 +24,7 @@ mousedown = False
clickstart = (0,0)
fps = 0
dll=np.ctypeslib.load_library('../realenv/envs/render','.')
dll=np.ctypeslib.load_library('render','.')
def onmouse(*args):
@ -54,8 +54,8 @@ def onmouse(*args):
my=args[1]
mx=args[2]
mousex=mx/float(showsz)
mousey=my/float(showsz * 2)
mousex=mx/float(256)
mousey=my/float(256 * 2)
@ -63,6 +63,9 @@ def showpoints(img, depth, pose, model, target):
global mousex,mousey,changed
global pitch,yaw,x,y,z,roll
global fps
showsz = target.shape[0]
show=np.zeros((showsz,showsz * 2,3),dtype='uint8')
target_depth = np.zeros((showsz,showsz * 2)).astype(np.float32)
overlay = False
@ -71,11 +74,11 @@ def showpoints(img, depth, pose, model, target):
cv2.moveWindow('show3d',0,0)
cv2.setMouseCallback('show3d',onmouse)
imgv = Variable(torch.zeros(1,3, 256, 512)).cuda()
maskv = Variable(torch.zeros(1,1, 256, 512)).cuda()
imgv = Variable(torch.zeros(1,3, showsz, showsz*2)).cuda()
maskv = Variable(torch.zeros(1,1, showsz, showsz*2)).cuda()
cpose = np.eye(4)
def render(img, depth, pose, model):
global fps
t0 = time.time()
@ -114,34 +117,34 @@ def showpoints(img, depth, pose, model, target):
beta = pitch
gamma = roll
cpose = cpose.flatten()
cpose[0] = cos(alpha) * cos(beta);
cpose[1] = cos(alpha) * sin(beta) * sin(gamma) - sin(alpha) * cos(gamma);
cpose[2] = cos(alpha) * sin(beta) * cos(gamma) + sin(alpha) * sin(gamma);
cpose[3] = 0
cpose[4] = sin(alpha) * cos(beta);
cpose[5] = sin(alpha) * sin(beta) * sin(gamma) + cos(alpha) * cos(gamma);
cpose[6] = sin(alpha) * sin(beta) * cos(gamma) - cos(alpha) * sin(gamma);
cpose[7] = 0
cpose[8] = -sin(beta);
cpose[9] = cos(beta) * sin(gamma);
cpose[10] = cos(beta) * cos(gamma);
cpose[11] = 0
cpose[12:16] = 0
cpose[15] = 1
cpose = cpose.reshape((4,4))
cpose2 = np.eye(4)
cpose2[0,3] = x
cpose2[1,3] = y
cpose2[2,3] = z
cpose = np.dot(cpose, cpose2)
print('cpose',cpose)
render(img, depth, cpose.astype(np.float32), model)
changed = False
@ -175,14 +178,14 @@ def showpoints(img, depth, pose, model, target):
elif cmd == ord('d'):
y -= 0.05
changed = True
elif cmd == ord('z'):
z += 0.01
changed = True
elif cmd == ord('x'):
z -= 0.01
changed = True
elif cmd == ord('r'):
pitch,yaw,x,y,z = 0,0,0,0,0
roll = 0
@ -190,16 +193,16 @@ def showpoints(img, depth, pose, model, target):
elif cmd == ord('t'):
print('pose', pose)
RT = pose.reshape((4,4))
R = RT[:3,:3]
T = RT[:3,-1]
x,y,z = np.dot(np.linalg.inv(R),T)
roll, pitch, yaw = (utils.rotationMatrixToEulerAngles(R))
changed = True
changed = True
elif cmd == ord('o'):
overlay = not overlay
@ -211,7 +214,6 @@ def show_target(target_img):
cv2.namedWindow('target')
cv2.moveWindow('target',0,256 + 50)
show_rgb = cv2.cvtColor(target_img, cv2.COLOR_BGR2RGB)
cv2.imshow('target', show_rgb)
if __name__=='__main__':
@ -221,12 +223,13 @@ if __name__=='__main__':
parser.add_argument('--dataroot' , required = True, help='dataset path')
parser.add_argument('--idx' , type = int, default = 0, help='index of data')
parser.add_argument('--model' , type = str, default = '', help='path of model')
opt = parser.parse_args()
d = ViewDataSet3D(root=opt.dataroot, transform = np.array, mist_transform = np.array, seqlen = 2, off_3d = False)
idx = opt.idx
data = d[idx]
source = data[0][0]
target = data[1]
source_depth = data[2][0]
@ -243,4 +246,4 @@ if __name__=='__main__':
#print(source_depth)
print(source.shape, source_depth.shape)
show_target(target)
showpoints(source, source_depth, pose, model, target)
showpoints(source, source_depth, pose, model, target)

View File

@ -159,23 +159,23 @@ def showpoints_full(img, depth, model, rts):
render(img, depth, relative.astype(np.float32), model, current_rt)
changed = False
if overlay:
min_idx = np.argsort(dist)[0]
target = sources[min_idx]
show_out = (show/2 + target/2).astype(np.uint8)
elif show_depth:
show_out = (target_depth * 10).astype(np.uint8)
else:
show_out = show
if overlay:
min_idx = np.argsort(dist)[0]
target = sources[min_idx]
show_out = (show/2 + target/2).astype(np.uint8)
elif show_depth:
show_out = (target_depth * 10).astype(np.uint8)
else:
show_out = show
cv2.putText(show,'pitch %.3f yaw %.2f roll %.3f x %.2f y %.2f z %.2f'%(pitch, yaw, roll, x, y, z),(15,showsz-15),0,0.5,cv2.cv.CV_RGB(255,255,255))
cv2.putText(show,'fps %.1f'%(fps),(15,15),0,0.5,cv2.cv.CV_RGB(255,255,255))
cv2.putText(show,'pitch %.3f yaw %.2f roll %.3f x %.2f y %.2f z %.2f'%(pitch, yaw, roll, x, y, z),(15,showsz-15),0,0.5,cv2.cv.CV_RGB(255,255,255))
cv2.putText(show,'fps %.1f'%(fps),(15,15),0,0.5,cv2.cv.CV_RGB(255,255,255))
show_rgb = cv2.cvtColor(show_out, cv2.COLOR_BGR2RGB)
cv2.imshow('show3d',show_rgb)
#cv2.imshow('minimap',minimap)
show_rgb = cv2.cvtColor(show_out, cv2.COLOR_BGR2RGB)
cv2.imshow('show3d',show_rgb)
#cv2.imshow('minimap',minimap)
cmd=cv2.waitKey(10)%256
cmd=cv2.waitKey(30)%256
if cmd==ord('q'):
break