Browse Source

random-connected rod crystal

master
JjG 2 years ago
parent
commit
3796b3cae1
  1. BIN
      data/crystal_rod.raw
  2. BIN
      data/crystal_rod.sdf
  3. BIN
      data/sphere.raw
  4. BIN
      data/sphere.sdf
  5. 1
      run.py
  6. 1
      sdf_generate/CMakeLists.txt
  7. 129
      sdf_generate/src/rod_generate.cu
  8. 28
      test_make_sphere_sdf.py

BIN
data/crystal_rod.raw

Binary file not shown.

BIN
data/crystal_rod.sdf

Binary file not shown.

BIN
data/sphere.raw

Binary file not shown.

BIN
data/sphere.sdf

Binary file not shown.

1
run.py

@ -32,6 +32,7 @@ def loadSdfFile(file_path):
print("data:")
print(data)
print("x,", x, "y,", y, "z,", z)
data = -data
return x, y, z, data

1
sdf_generate/CMakeLists.txt

@ -23,6 +23,7 @@ AUX_SOURCE_DIRECTORY(src DIR_SRCS)
FILE(GLOB_RECURSE DIR_INCLUDE include/*.h include/*.hpp include/*.cuh)
pybind11_add_module(sdfGenerate ${DIR_SRCS} ${DIR_INCLUDE} main.cpp)
set_target_properties(sdfGenerate PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/..)
#ADD_LIBRARY(sdfGenerate SHARED ${DIR_SRCS} ${DIR_INCLUDE} main.cpp)
#add_executable(sdfGenerate ${DIR_SRCS} ${DIR_INCLUDE} main.cpp)

129
sdf_generate/src/rod_generate.cu

@ -5,6 +5,7 @@
#include "rod_generate.cuh"
#include "device_functions.h"
#include "float.h"
#include "ctime"
py::array_t<float> add(int a, int b) {
py::array_t<float> res = py::array_t<float>(4);
@ -13,8 +14,8 @@ py::array_t<float> add(int a, int b) {
ptr[0] = a - b;
ptr[1] = a + b;
ptr[2] = a * b;
ptr[3] = (float)a / b;
res.resize({2,2});
ptr[3] = (float) a / b;
res.resize({2, 2});
return res;
}
@ -29,14 +30,14 @@ g_rod_generate(int *beamData, int beamCnt, float *pointData, int pointCnt, cudaP
auto ix = blockIdx.x * blockDim.x + threadIdx.x;
auto iy = blockIdx.y * blockDim.y + threadIdx.y;
auto iz = blockIdx.z * blockDim.z + threadIdx.z;
// if (ix == 0 && iy == 0 && iz == 0) {
// for (int i = 0; i < beamCnt; ++i) {
// printf("%d, %d\n", rod_beams(i, 0), rod_beams(i, 1));
// }
// for(int i = 0; i < pointCnt; ++i) {
// printf("%f, %f, %f\n", rod_points(i, 0), rod_points(i, 1), rod_points(i, 2));
// }
// }
if (ix == 0 && iy == 0 && iz == 0) {
for (int i = 0; i < beamCnt; ++i) {
printf("%d, %d\n", rod_beams(i, 0), rod_beams(i, 1));
}
for (int i = 0; i < pointCnt; ++i) {
printf("%f, %f, %f\n", rod_points(i, 0), rod_points(i, 1), rod_points(i, 2));
}
}
int cellCntX = extent->width / floatSize - 1;
int cellCntY = extent->height - 1;
int cellCntZ = extent->depth - 1;
@ -62,14 +63,14 @@ g_rod_generate(int *beamData, int beamCnt, float *pointData, int pointCnt, cudaP
row[ix] = FLT_MAX;
// auto aTmp = Eigen::Vector3f(rod_points.row(rod_beams(2, 1)));
// printf("aTmp: (%f, %f, %f)\n", aTmp.x(), aTmp.y(), aTmp.z());
// printf("rod_beams.rows(): %ld\n", rod_beams.rows());
for (int i = 0; i < rod_beams.rows(); ++i) {
auto a = Eigen::Vector3f(rod_points.row(rod_beams(i, 0)));
auto b = Eigen::Vector3f(rod_points.row(rod_beams(i, 1)));
auto ab = b - a;
auto ap = p - a;
auto bp = p - b;
if (ab.x() * bp.x() + ab.y() * bp.y() + ab.z() + bp.z() < 0 &&
ab.x() * ap.x() + ab.y() * ap.y() + ab.z() * ap.z() > 0) {
if (ab.dot(bp) < 0 && ab.dot(ap) > 0) {
row[ix] = min(row[ix], (ap.cross(bp)).norm() / ab.norm());
} else {
row[ix] = min(row[ix], min(ap.norm(), bp.norm()));
@ -79,7 +80,8 @@ g_rod_generate(int *beamData, int beamCnt, float *pointData, int pointCnt, cudaP
}
__host__ float *
h_rod_generate(float *h_sdf, const RodCrystal &rodCrystal, const Eigen::Vector3i &sampleCnt, const Eigen::Vector3f &sampleMin,
h_rod_generate(float *h_sdf, const RodCrystal &rodCrystal, const Eigen::Vector3i &sampleCnt,
const Eigen::Vector3f &sampleMin,
const Eigen::Vector3f &sampleMax, float radius) {
int *d_beamData;
size_t beamBytes = rodCrystal.rod_beams.rows() * rodCrystal.rod_beams.cols() * sizeof(int);
@ -182,8 +184,35 @@ h_rod_generate(float *h_sdf, const RodCrystal &rodCrystal, const Eigen::Vector3i
return h_sdf;
}
py::array_t<float> generate(int sampleX = 101, int sampleY = 101, int sampleZ = 101, float halfSampleRegion = 10) {
// a case with 3*3*3 points (2*2*2 resolution)
RodCrystal randomCrystal(int points_cnt, float axisMin, float axisMax, float threshold) {
Eigen::Matrix<float, Eigen::Dynamic, 3> rod_points;
Eigen::Matrix<int, Eigen::Dynamic, 2> rod_beams;
rod_points.resize(points_cnt, 3);
srandom(time(nullptr));
const int MAX_SIZE = 1000;
for (int i = 0; i < points_cnt; ++i) {
rod_points(i, 0) = axisMin + static_cast<float>(random() % MAX_SIZE) / MAX_SIZE * (axisMax - axisMin);
rod_points(i, 1) = axisMin + static_cast<float>(random() % MAX_SIZE) / MAX_SIZE * (axisMax - axisMin);
rod_points(i, 2) = axisMin + static_cast<float>(random() % MAX_SIZE) / MAX_SIZE * (axisMax - axisMin);
}
std::vector<std::pair<int, int>> rod_beams_tmp;
for (int i = 0; i < points_cnt; ++i) {
for (int j = i + 1; j < points_cnt; ++j) {
if ((rod_points.row(i) - rod_points.row(j)).norm() < threshold) {
rod_beams_tmp.emplace_back(i, j);
}
}
}
rod_beams.resize(rod_beams_tmp.size(), 2);
for (int i = 0; i < rod_beams_tmp.size(); ++i) {
rod_beams(i, 0) = rod_beams_tmp[i].first;
rod_beams(i, 1) = rod_beams_tmp[i].second;
}
return {rod_points, rod_beams};
}
RodCrystal cube2() {
Eigen::Matrix<float, Eigen::Dynamic, 3> rod_points;
rod_points.resize(27, 3);
rod_points << 0, 0, 0,
@ -214,6 +243,61 @@ py::array_t<float> generate(int sampleX = 101, int sampleY = 101, int sampleZ =
1, 2, 2,
2, 2, 2;
Eigen::Matrix<int, Eigen::Dynamic, 2> rod_beams;
// rod_beams.resize(54, 2);
// rod_beams << 0, 1,
// 1, 2,
// 3, 4,
// 4, 5,
// 6, 7,
// 7, 8,
// 0, 3,
// 3, 6,
// 1, 4,
// 4, 7,
// 2, 5,
// 5, 8,
// 9, 10,
// 10, 11,
// 12, 13,
// 13, 14,
// 15, 16,
// 16, 17,
// 9, 12,
// 12, 15,
// 10, 13,
// 13, 16,
// 11, 14,
// 14, 17,
// 18, 19,
// 19, 20,
// 21, 22,
// 22, 23,
// 24, 25,
// 25, 26,
// 18, 21,
// 21, 24,
// 19, 22,
// 22, 25,
// 20, 23,
// 23, 26,
// 0, 9,
// 9, 18,
// 1, 10,
// 10, 19,
// 2, 11,
// 11, 20,
// 3, 12,
// 12, 21,
// 4, 13,
// 13, 22,
// 5, 14,
// 14, 23,
// 6, 15,
// 15, 24,
// 7, 16,
// 16, 25,
// 8, 17,
// 17, 26;
rod_beams.resize(54, 2);
rod_beams << 0, 1,
1, 2,
@ -269,14 +353,23 @@ py::array_t<float> generate(int sampleX = 101, int sampleY = 101, int sampleZ =
16, 25,
8, 17,
17, 26;
RodCrystal rod(rod_points, rod_beams);
return {rod_points, rod_beams};
}
py::array_t<float> generate(int sampleX = 101, int sampleY = 101, int sampleZ = 101, float halfSampleRegion = 10) {
// a case with 3*3*3 points (2*2*2 resolution)
// auto rod = cube2();
auto rod = randomCrystal(60, 0, 2, 0.8);
// RodBVH bvh(rod);
// bvh.build();
py::array_t<float> res = py::array_t<float>(sampleX * sampleY * sampleZ);
py::buffer_info buf = res.request();
auto *ptr = (float *) buf.ptr;
float *sdf = h_rod_generate(ptr, rod, Eigen::Vector3i(sampleX, sampleY, sampleZ), Eigen::Vector3f(-halfSampleRegion, -halfSampleRegion, -halfSampleRegion),
Eigen::Vector3f(halfSampleRegion, halfSampleRegion, halfSampleRegion), 0.2f);
float *sdf = h_rod_generate(ptr, rod, Eigen::Vector3i(sampleX, sampleY, sampleZ),
Eigen::Vector3f(-halfSampleRegion, -halfSampleRegion, -halfSampleRegion),
Eigen::Vector3f(halfSampleRegion, halfSampleRegion, halfSampleRegion), 0.14f);
res.resize({sampleX, sampleY, sampleZ});
return res;
}

28
test_make_sphere_sdf.py

@ -1,12 +1,18 @@
import numpy as np
from math import *
import pickle
import sys
import sdfGenerate
# sys.path.append('/home/dtouch/projects/implicit/implicit_rendering/renderSDF/sdf_generate/cmake-build-debug')
print('run test_make_sphere_sdf.py...')
sample_cnt_x, sample_cnt_y, sample_cnt_z = 128, 128, 128
sample_cnt_x, sample_cnt_y, sample_cnt_z = 188, 188, 188
spatial_size = (4, 4, 4)
rod_crystal_sdf = sdfGenerate.generate(sample_cnt_x, sample_cnt_y, sample_cnt_z, spatial_size[0]/2)
spatial_size = (100, 100, 100)
sdf = np.ones((sample_cnt_x, sample_cnt_y, sample_cnt_z))
@ -33,8 +39,8 @@ def sd_box(p):
def sd_sphere(x, y, z):
sphere_center = (50, 50, 50)
radius = 45
sphere_center = (6, 6, 6)
radius = 5
return sqrt((x - sphere_center[0]) ** 2 + (y - sphere_center[1]) ** 2 + (z - sphere_center[2]) ** 2) - radius
@ -44,17 +50,19 @@ for i in range(sample_cnt_x):
x = (spatial_size[0] * i / (sample_cnt_x - 1))
y = (spatial_size[1] * j / (sample_cnt_y - 1))
z = (spatial_size[2] * k / (sample_cnt_z - 1))
# sdf[i][j][k] = sd_sphere(x, y, z)
sdf[i][j][k] = sd_box(np.array([x, y, z]))
sdf[i][j][k] = sd_sphere(x, y, z)
# sdf[i][j][k] = sd_box(np.array([x, y, z]))
# sdf[i][j][k] = sd_plane_x(x, y, z, 0)
save_file = '/home/dtouch/projects/implicit/implicit_rendering/renderSDF/data/box.sdf'
sdf = rod_crystal_sdf
# save_file = '/home/dtouch/projects/implicit/implicit_rendering/renderSDF/data/sphere.sdf'
# save_file = '/home/dtouch/projects/implicit/implicit_rendering/renderSDF/data/box.sdf'
save_file = '/home/dtouch/projects/implicit/implicit_rendering/renderSDF/data/crystal_rod.sdf'
with open(save_file, 'wb') as f:
pickle.dump([sdf], f)
data = pickle.load(open(save_file, 'rb'))
for i in range(19):
print(i)
# print(data[0])

Loading…
Cancel
Save