Skip to content

Commit

Permalink
fix seg fault
Browse files Browse the repository at this point in the history
  • Loading branch information
dicarne committed Sep 8, 2022
1 parent 8da3d5d commit 5f17684
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,4 @@ datasets/TartanAir.pickle
datasets/TartanAir
TartanAirResults
thirdparty/*
movies/*
67 changes: 31 additions & 36 deletions DPViewer/dpviewer/viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
typedef unsigned char uchar;

std::mutex mtx;
std::mutex mtx_update;

class Viewer {
public:
Expand Down Expand Up @@ -42,6 +43,7 @@ class Viewer {

// main visualization
void run();
void loop();

private:
bool running;
Expand Down Expand Up @@ -76,8 +78,8 @@ class Viewer {

// OpenGL buffers (vertex buffer, color buffer)
GLuint vbo, cbo;
struct cudaGraphicsResource *xyz_res;
struct cudaGraphicsResource *rgb_res;
struct cudaGraphicsResource *xyz_res=nullptr;
struct cudaGraphicsResource *rgb_res=nullptr;

};

Expand All @@ -97,44 +99,28 @@ Viewer::Viewer(
ux = 0;
h = image.size(0);
w = image.size(1);

transformMatrix = poseToMatrix(poses);
transformMatrix = transformMatrix.transpose(1,2);
transformMatrix = transformMatrix.contiguous().to(torch::kCPU);
tViewer = std::thread(&Viewer::run, this);
};

void Viewer::drawPoints() {
float *xyz_ptr;
uchar *rgb_ptr;
size_t xyz_bytes;
size_t rgb_bytes;

unsigned int size_xyz = 3 * points.size(0) * sizeof(float);
unsigned int size_rgb = 3 * points.size(0) * sizeof(uchar);

cudaGraphicsResourceGetMappedPointer((void **) &xyz_ptr, &xyz_bytes, xyz_res);
cudaGraphicsResourceGetMappedPointer((void **) &rgb_ptr, &rgb_bytes, rgb_res);

float *xyz_data = points.data_ptr<float>();
cudaMemcpy(xyz_ptr, xyz_data, xyz_bytes, cudaMemcpyDeviceToDevice);

uchar *rgb_data = colors.data_ptr<uchar>();
cudaMemcpy(rgb_ptr, rgb_data, rgb_bytes, cudaMemcpyDeviceToDevice);
auto b = points.cpu();
float *xyz_data = b.data_ptr<float>();

// bind color buffer
glBindBuffer(GL_ARRAY_BUFFER, cbo);
glColorPointer(3, GL_UNSIGNED_BYTE, 0, 0);
glEnableClientState(GL_COLOR_ARRAY);
auto c = colors.cpu();
uchar *rgb_data = c.data_ptr<uchar>();

glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexPointer(3, GL_FLOAT, 0, 0);
glPointSize(10.0f);
glBegin(GL_POINTS);

// bind vertex buffer
glEnableClientState(GL_VERTEX_ARRAY);
glDrawArrays(GL_POINTS, 0, points.size(0));
glDisableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
for (auto i = 0; i < points.size(0); i++) {

glDisableClientState(GL_COLOR_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glColor4ub(*(rgb_data + i), *(rgb_data + i + 1), *(rgb_data + i + 2), 255);
glVertex3f(*(xyz_data + i), *(xyz_data + i + 1), *(xyz_data + 2 + i));
}
glEnd();

}

Expand Down Expand Up @@ -268,13 +254,15 @@ void Viewer::run() {
Visualization3D_display.Activate(Visualization3D_camera);

// maybe possible to draw cameras without copying to CPU?
transformMatrix = poseToMatrix(poses);
transformMatrix = transformMatrix.transpose(1,2);
transformMatrix = transformMatrix.contiguous().to(torch::kCPU);
// transformMatrix = poseToMatrix(poses);
// transformMatrix = transformMatrix.transpose(1,2);
// transformMatrix = transformMatrix.contiguous().to(torch::kCPU);

// draw poses using OpenGL
mtx_update.lock();
drawPoints();
drawPoses();
mtx_update.unlock();

mtx.lock();
if (redraw) {
Expand All @@ -296,7 +284,13 @@ void Viewer::run() {

exit(1);
}

void Viewer::loop() {
mtx_update.lock();
transformMatrix = poseToMatrix(poses);
transformMatrix = transformMatrix.transpose(1, 2);
transformMatrix = transformMatrix.contiguous().to(torch::kCPU);
mtx_update.unlock();
}

namespace py = pybind11;

Expand All @@ -309,5 +303,6 @@ PYBIND11_MODULE(dpviewerx, m) {
const torch::Tensor,
const torch::Tensor>())
.def("update_image", &Viewer::update_image)
.def("loop", &Viewer::loop)
.def("join", &Viewer::join);
}
1 change: 1 addition & 0 deletions dpvo/dpvo.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ def __call__(self, tstamp, image, intrinsics):

if self.viewer is not None:
self.viewer.update_image(image)
self.viewer.loop()

image = 2 * (image[None,None] / 255.0) - 0.5

Expand Down

0 comments on commit 5f17684

Please sign in to comment.