Skip to content

Commit

Permalink
fix overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Qinqing Liu committed Nov 12, 2020
1 parent 8fd8c7e commit 478d059
Show file tree
Hide file tree
Showing 163 changed files with 2,037 additions and 220,561 deletions.
Binary file modified .DS_Store
Binary file not shown.
31 changes: 18 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,23 @@ cd pdbbind/java
javac -cp cdk-2.3-SNAPSHOT.jar Surface_for_single.java
cd ../..
# compile cpp
cd octree/external && git clone --recursive https://github.com/wang-ps/octree-ext.git
cd .. && mkdir build && cd build
cmake .. && cmake --build . --config Release
export PATH=`pwd`:$PATH
# set-up TF enviroment
# set-up TF enviroment and compile cpp
conda create -n OctSurf_env tensorflow-gpu==1.14.0
conda activate OctSurf_env
conda install -c conda-forge yacs tqdm
pip install sklearn
pip install matplotlib
pip install seaborn
pip install -U openbabel
cmake .. -DUSE_CUDA=ON && make
cd ../../tensorflow/libs
cd octree/external && git clone --recursive https://github.com/wang-ps/octree-ext.git
cd .. && mkdir build && cd build
cmake .. -DUSE_CUDA=ON && cmake --build . --config Release
export PATH=`pwd`:$PATH
cd python
export PATH=`pwd`:$PATH
cd ../../../tensorflow/libs
python build.py
cd ../../
```
Expand All @@ -41,11 +45,12 @@ cd ../../
```angular2
cd pdbbind/data_folder/refined-set/1a1e
bash ../octree_for_single.sh
cd ../../../..
cd octree_folder
ls -lh
cd ../../../../
# visualization
pip install vtk
cd visualize
# parse by python
cd python
python octree_parse.py
cd ../
```
Expand Down
Binary file modified octree/.DS_Store
Binary file not shown.
55 changes: 45 additions & 10 deletions octree/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ option(USE_PYTHON "Build the python interface with pybind11" ON)
option(USE_GLOG "Use glog" OFF)
option(USE_OPENMP "Use OpenMP for speed up" OFF)
option(USE_RPLY "Use the library rply in the project octree" OFF)
option(KEY64 "Use 64-bit key for deeper octrees" OFF)
option(ABI "Open CXX11_ABI compatibility" OFF)
set(VCPKG "<Set VCPKG path>" CACHE PATH "The VCPKG path, containing glog and gtest")


# set languanges
if(USE_CUDA)
message(STATUS "octree: USE_CUDA")
project(Octree LANGUAGES CUDA CXX C)
add_definitions(-DUSE_CUDA)
set(CMAKE_CUDA_STANDARD 11)
Expand All @@ -25,30 +28,48 @@ endif()
set_property(GLOBAL PROPERTY USE_FOLDERS ON)

# # c++11 Support
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set (CMAKE_CXX_STANDARD 11)
set (CMAKE_POSITION_INDEPENDENT_CODE ON)
if(ABI)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_GLIBCXX_USE_CXX11_ABI=0")
endif()

# add target_link_libraries
if(USE_MINIBALL)
# add libraries
if(USE_MINIBALL)
message(STATUS "octree: USE_MINIBALL")
add_definitions(-DUSE_MINIBALL)
set(miniball_path "${PROJECT_SOURCE_DIR}/external/octree-ext/miniball")
include_directories(${miniball_path})
endif()

if(USE_RPLY)
message(STATUS "octree: USE_RPLY")
add_definitions(-DUSE_RPLY)
file(GLOB src_rply
"${PROJECT_SOURCE_DIR}/external/octree-ext/rply-1.1.4/*.h"
"${PROJECT_SOURCE_DIR}/external/octree-ext/rply-1.1.4/*.c")
include_directories("${PROJECT_SOURCE_DIR}/external/octree-ext/rply-1.1.4")
add_library(rply ${src_rply})
endif()
file(GLOB src_rply
"${PROJECT_SOURCE_DIR}/external/octree-ext/rply-1.1.4/*.h"
"${PROJECT_SOURCE_DIR}/external/octree-ext/rply-1.1.4/*.c")
include_directories("${PROJECT_SOURCE_DIR}/external/octree-ext/rply-1.1.4")
add_library(rply ${src_rply})
set_target_properties(rply PROPERTIES FOLDER "ext")

if(KEY64)
message(STATUS "octree: KEY64")
add_definitions(-DKEY64)
endif()

if(USE_GLOG)
message(STATUS "octree: USE_GLOG")
add_definitions(-DUSE_GLOG)
set(glog_path "${PROJECT_SOURCE_DIR}/external/octree-ext/glog")
set(WITH_GFLAGS OFF CACHE BOOL "Disable WITH_GFLAGS")
set(BUILD_TESTING OFF CACHE BOOL "Disable BUILD_TESTING")
add_subdirectory(${glog_path})
set_target_properties(glog PROPERTIES FOLDER "ext")
endif()

if(USE_WINDOWS_IO)
message(STATUS "octree: USE_WINDOWS_IO")
add_definitions(-DUSE_WINDOWS_IO)
endif()

Expand Down Expand Up @@ -100,6 +121,9 @@ target_compile_features(octree_lib PUBLIC cxx_std_11)
if(USE_RPLY)
target_link_libraries(octree_lib rply)
endif(USE_RPLY)
if(USE_GLOG)
target_link_libraries(octree_lib glog::glog)
endif(USE_GLOG)

# add tools
file(GLOB_RECURSE srcs "${PROJECT_SOURCE_DIR}/tools/*.cpp")
Expand All @@ -116,5 +140,16 @@ endforeach(source)

# add the python interface
if(USE_PYTHON)
message(STATUS "octree: USE_PYTHON")
add_subdirectory(python)
endif()
endif()

# add gtest
set(gtest_path "${PROJECT_SOURCE_DIR}/external/octree-ext/googletest")
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
add_subdirectory(${gtest_path} EXCLUDE_FROM_ALL)
set_target_properties(gtest gtest_main PROPERTIES FOLDER "ext")

# add the test
add_executable(octree_test ${src_test})
target_link_libraries(octree_test octree_lib gtest gtest_main)
86 changes: 44 additions & 42 deletions octree/octree/contour.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#include "contour.h"
#include "marching_cube.h"

#include "marching_cube.h"

void Contour::marching_cube(vector<float>& V, vector<int>& F) {
// subdivide
const int depth = octree_->info().depth();
vector<uint32> nodes_subdivided;
vector<uintk> nodes_subdivided;
for (int d = octree_->info().full_layer(); d < depth; ++d) {
// Check subdividion of octree nodes
int nnum = octree_->info().node_num(d);
vector<uint32> nodes_need_subdivide;
vector<uintk> nodes_need_subdivide;
for (int i = 0; i < nnum; ++i) {
// Only check the leaf nodes
if (octree_->children_cpu(d)[i] < 0) {
uint32 keyi = octree_->key_cpu(d)[i];
uintk keyi = octree_->key_cpu(d)[i];
if (check_subdividion(keyi, d)) {
nodes_need_subdivide.push_back(keyi);
}
Expand All @@ -22,7 +22,7 @@ void Contour::marching_cube(vector<float>& V, vector<int>& F) {

// check the subdivided nodes in the last iteration
for (int i = 0; i < nodes_subdivided.size(); ++i) {
uint32 keyi = nodes_subdivided[i];
uintk keyi = nodes_subdivided[i];
if (check_subdividion(keyi, d)) {
nodes_need_subdivide.push_back(keyi);
}
Expand All @@ -37,11 +37,13 @@ void Contour::marching_cube(vector<float>& V, vector<int>& F) {
}

// marching cube
V.clear(); F.clear();
nodes_subdivided.insert(nodes_subdivided.end(),
octree_->key_cpu(depth), octree_->key_cpu(depth) + octree_->info().node_num(depth));
V.clear();
F.clear();
nodes_subdivided.insert(
nodes_subdivided.end(), octree_->key_cpu(depth),
octree_->key_cpu(depth) + octree_->info().node_num(depth));
for (int i = 0; i < nodes_subdivided.size(); ++i) {
uint32 weight_case = 0;
uintk weight_case = 0;
float corner_val[8], pt[3];
octree_->key2xyz(pt, nodes_subdivided[i], depth);
for (int j = 0; j < 8; ++j) {
Expand All @@ -60,39 +62,43 @@ void Contour::marching_cube(vector<float>& V, vector<int>& F) {
}

// translate and scale points
const float* bbmin = octree_->info().bbmin();
const float scale = octree_->info().bbox_max_width() / float(1 << depth);
for (int i = 0; i < V.size() / 3; ++i) {
for (int c = 0; c < 3; ++c) {
V[i * 3 + c] = V[i * 3 + c] * scale + bbmin[c];
if (rescale_) {
const float* bbmin = octree_->info().bbmin();
const float scale = octree_->info().bbox_max_width() / float(1 << depth);
for (int i = 0; i < V.size() / 3; ++i) {
for (int c = 0; c < 3; ++c) {
V[i * 3 + c] = V[i * 3 + c] * scale + bbmin[c];
}
}
}
}

pair<float, float> Contour::fval(int x, int y, int z) {
const int depth = octree_->info().depth();
int key = (x << 2 * depth) | (y << depth) | z;
int key = (x << 2 * depth) | (y << depth) | z; // qq: TODO: uintk
auto it = fval_map_.find(key);
if (it == fval_map_.end()) {
auto v = value_.fval(x, y, z);
fval_map_[key] = v; // insert new element
fval_map_[key] = v; // insert new element
return v;
} else {
return it->second;
}
}

bool Contour::check_subdividion(const uint32 node_key, const int depth) {
bool Contour::check_subdividion(const uintk node_key, const int depth) {
// get cooridinates
uint32 xyz[3] = { 0 };
uintk xyz[3] = {0};
octree_->key2xyz(xyz, node_key, depth);
int depth_ = octree_->info().depth();
const int scale = 1 << (depth_ - depth);
for (int c = 0; c < 3; ++c) { xyz[c] *= scale; }
for (int c = 0; c < 3; ++c) {
xyz[c] *= scale;
}

// check 8 cornors
const uint32 mask[8] = { 1, 2, 4, 8, 16, 32, 64, 128 };
uint32 cube_case = 0, weight_case = 0;
const uintk mask[8] = {1, 2, 4, 8, 16, 32, 64, 128};
uintk cube_case = 0, weight_case = 0;
for (int i = 0; i < 8; ++i) {
int x = xyz[0] + MarchingCube::corner_[i][0] * scale;
int y = xyz[1] + MarchingCube::corner_[i][2] * scale;
Expand All @@ -106,20 +112,17 @@ bool Contour::check_subdividion(const uint32 node_key, const int depth) {

// check 6 faces
const int coord[6][3] = {
{xyz[0], xyz[1], xyz[2]}, {xyz[0] + scale, xyz[1], xyz[2]},
{xyz[0], xyz[1], xyz[2]}, {xyz[0], xyz[1] + scale, xyz[2]},
{xyz[0], xyz[1], xyz[2]}, {xyz[0], xyz[1], xyz[2] + scale}
};
const int axis1[6][3] = {
{0, 1, 0}, {0, 1, 0}, {1, 0, 0}, {1, 0, 0}, {1, 0, 0}, {1, 0, 0}
};
const int axis2[6][3] = {
{0, 0, 1}, {0, 0, 1}, {0, 0, 1}, {0, 0, 1}, {0, 1, 0}, {0, 1, 0}
};
{xyz[0], xyz[1], xyz[2]}, {xyz[0] + scale, xyz[1], xyz[2]},
{xyz[0], xyz[1], xyz[2]}, {xyz[0], xyz[1] + scale, xyz[2]},
{xyz[0], xyz[1], xyz[2]}, {xyz[0], xyz[1], xyz[2] + scale}};
const int axis1[6][3] = {{0, 1, 0}, {0, 1, 0}, {1, 0, 0},
{1, 0, 0}, {1, 0, 0}, {1, 0, 0}};
const int axis2[6][3] = {{0, 0, 1}, {0, 0, 1}, {0, 0, 1},
{0, 0, 1}, {0, 1, 0}, {0, 1, 0}};
for (int i = 0; i < 6; ++i) {
for (int m = 0; m < scale; ++m) {
for (int n = 0; n < scale; ++n) {
uint32 face_case = 0, wt_case = 0;
uintk face_case = 0, wt_case = 0;
for (int k = 0; k < 4; ++k) {
int m1 = (k & 1) ? m + 1 : m;
int n1 = (k & 2) ? n + 1 : n;
Expand All @@ -130,7 +133,6 @@ bool Contour::check_subdividion(const uint32 node_key, const int depth) {
auto fvalue = fval(x, y, z); // pair<value, weight>
if (fvalue.first < 0) face_case |= mask[k];
if (fvalue.second != 0) wt_case |= mask[k];

}
if (face_case != 0 && face_case != 15 && wt_case == 15) return true;
}
Expand All @@ -140,22 +142,22 @@ bool Contour::check_subdividion(const uint32 node_key, const int depth) {
return false;
}

void Contour::subdivide(uint32* key_output, const uint32 key_input) const {
// !!! caveat: depth should be smaller than 8
typedef unsigned char ubyte;
void Contour::subdivide(uintk* key_output, const uintk key_input) const {
typedef typename KeyTrait<uintk>::uints uints;

if (octree_->info().is_key2xyz()) {
const ubyte* pt = reinterpret_cast<const ubyte*>(&key_input);
ubyte x = pt[0] << 1;
ubyte y = pt[1] << 1;
ubyte z = pt[2] << 1;
const uints* pt = reinterpret_cast<const uints*>(&key_input);
uints x = pt[0] << 1;
uints y = pt[1] << 1;
uints z = pt[2] << 1;
for (int i = 0; i < 8; ++i) {
ubyte* xyz = reinterpret_cast<ubyte*>(key_output + i);
uints* xyz = reinterpret_cast<uints*>(key_output + i);
xyz[0] = (i & 1) ? x + 1 : x;
xyz[1] = (i & 2) ? y + 1 : y;
xyz[2] = (i & 4) ? z + 1 : z;
}
} else {
uint32 key_in = key_input << 3;
uintk key_in = key_input << 3;
for (int i = 0; i < 8; ++i) {
key_output[i] = key_in | i;
}
Expand Down
19 changes: 10 additions & 9 deletions octree/octree/contour.h
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
#ifndef _OCTREE_CONTOUR_
#define _OCTREE_CONTOUR_

#include <vector>
#include <utility>
#include <unordered_map>
#include <utility>
#include <vector>

#include "octree_parser.h"
#include "octree_value.h"
#include "types.h"

using std::pair;
using std::vector;
using std::unordered_map;

using std::vector;

class Contour {
public:
Contour(const OctreeParser* oct = nullptr): value_(oct) {
Contour(const OctreeParser* oct = nullptr, bool rescale = true)
: value_(oct), rescale_(rescale) {
set_octree(oct);
}

void set_octree(const OctreeParser* oct) {
octree_ = oct;
fval_map_.clear();
Expand All @@ -26,16 +28,15 @@ class Contour {
void marching_cube(vector<float>& V, vector<int>& F);

protected:
typedef unsigned int uint32;
pair<float, float> fval(int x, int y, int z);
bool check_subdividion(const uint32 node_key, const int depth);
void subdivide(uint32* key_output, const uint32 key_input) const;
bool check_subdividion(const uintk node_key, const int depth);
void subdivide(uintk* key_output, const uintk key_input) const;

protected:
OctreeValue value_;
const OctreeParser* octree_;
unordered_map<int, pair<float, float> > fval_map_;

bool rescale_;
};

#endif
Loading

0 comments on commit 478d059

Please sign in to comment.