Skip to content

Commit

Permalink
add readme for neural rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
rainy_start@163.com authored and rainy_start@163.com committed Jul 9, 2024
1 parent eb9a7bb commit 1870833
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 40 deletions.
6 changes: 3 additions & 3 deletions include/rt/rt_trainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace rt_sample
// generate random datas randomly
RayParam SampleRandom(size_t num);
// generate random datas randomly within a sphere
RayParam SampleSphere(size_t num);
RayParam SampleSphereFixhit(size_t num);
// generate uniform datas within on a sphere
RayParam UniformSphere(size_t num);
// generate datas with random points and fix vec
Expand All @@ -52,8 +52,8 @@ namespace rt_sample
RayParam SampleSphereFixVec(size_t num, std::vector<fastf_t> vec);
// generate datas with random points and fix vec on sphere surface, rays are forced to hit sphere
RayParam SampleFixVecHit(size_t num, std::vector < fastf_t> vec);
// generate datas with random points fix range and fix vec on sphere surface, rays are forced to hit sphere
RayParam RangeFixVecHit(size_t num, fastf_t max, fastf_t min, std::vector < fastf_t> vec);
// generate datas with random points fix range, rays are forced to hit sphere
RayParam RangeHit(size_t num, fastf_t max, fastf_t min);
}

namespace rt_neu
Expand Down
1 change: 1 addition & 0 deletions include/rt/torch_runner.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef RT_TORCH_RUNNER_H
#define RT_TORCH_RUNNER_H
void set_model_path(const char* model_path);
void set_model_type(int num);
typedef enum {
normal = 1,
neu_coordinate = 2,
Expand Down
2 changes: 1 addition & 1 deletion src/rt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ if(NOT TORCHVISION_FOUND)
execute_process(COMMAND ${Python_EXECUTABLE} -m pip install torchvision)
endif()

# 设置 Torch 的路径
# set libTorch path
set(Torch_DIR "C:/lib/VS/libtorch/share/cmake/Torch")
set(Torch_INCLUDE_DIRS "C:/lib/VS/libtorch/include")
set(Torch_INCLUDE_LIBS "C:/lib/VS/libtorch/lib")
Expand Down
16 changes: 16 additions & 0 deletions src/rt/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
## Dependence
[libtorch](https://pytorch.org/cppdocs/installing.html)
used for loading model
do remember to change the absolute path in:src/rt/CMakeLists.txt Line 56-58
[nlohmann](https://github.com/nlohmann/json)
used for write json file
## Project structure
Add two projects:
### rt_trainneural
used for sampling and rendering
new files:train_neural.cpp(main) rt/neu_util.h rt/rt_trainer.h
neural.c rt_trainer.cpp

see train_neural.cpp for usage
### torch_runner
used for run torch model
67 changes: 41 additions & 26 deletions src/rt/rt_trainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ vect_t ambient_color = { 1, 1, 1 }; /* Ambient white light */
int ibackground[3] = { 0 }; /* integer 0..255 version */
int inonbackground[3] = { 0 }; /* integer non-background */
fastf_t gamma_corr = 0.0; /* gamma correction if !0 */
const std::string model_path = "C:\\works\\soc\\rainy\\test\\model.pt";


namespace convert
Expand All @@ -46,29 +45,30 @@ namespace convert
{
RayParamSph res;
res.reserve(datas.size());
fastf_t elevation(0.0);
fastf_t azimuth(0.0);
fastf_t pelevation(0.0);
fastf_t pazimuth(0.0);
fastf_t theta;
fastf_t phi;
fastf_t ptheta;
fastf_t pphi;
for (auto& data : datas)
{
fastf_t x = data.first[0] - origin[0];
fastf_t y = data.first[1] - origin[1];
fastf_t z = data.first[2] - origin[2];
fastf_t z_abs = abs(z);
fastf_t r_abs = abs(r);
elevation = acos(z / r); //0-pi
azimuth = SIGN(y)*acos(x / (r * sin(elevation))); //-pi-pi

theta = acos(z / r); //0-pi
phi = SIGN(y)*acos(x / (r * sin(theta))); //-pi-pi
if (pow(data.second[0], 2) + pow(data.second[1], 2) + pow(data.second[2], 2) != 1)
{
fastf_t square_sum = pow(pow(data.second[0], 2) + pow(data.second[1], 2) + pow(data.second[2], 2), 0.5);
data.second[0] /= square_sum;
data.second[1] /= square_sum;
data.second[2] /= square_sum;
}
pelevation = acos(data.second[2]);
pazimuth = SIGN(data.second[1]) * acos(data.second[0] / (1 * sin(pelevation)));
res.push_back(std::make_pair(std::make_pair(elevation, azimuth), std::make_pair(pelevation, pazimuth)));
ptheta = acos(data.second[2]);
pphi = SIGN(data.second[1]) * acos(data.second[0] / (1 * sin(ptheta)));
res.push_back(std::make_pair(std::make_pair(theta,phi), std::make_pair(ptheta, pphi)));
}
return res;
}
Expand Down Expand Up @@ -197,7 +197,7 @@ namespace rt_sample
}
return res;
}
RayParam SampleSphere(size_t num)
RayParam SampleSphereFixhit(size_t num)
{
RayParam res;
point_t center{ 0 };
Expand All @@ -206,21 +206,28 @@ namespace rt_sample
fastf_t square_sum(0);
std::vector<fastf_t> p;
std::vector<fastf_t> d;
vect_t center_v;
for (int i = 0; i < num; ++i) {
p.clear();
d.clear();
fastf_t theta = RandomNum(0, 2 * M_PI);
fastf_t phi = acos(2 * RandomNum(0, 1) - 1);
p.push_back(center[0] + radius * sin(phi) * cos(theta));
p.push_back(center[1] + radius * sin(phi) * sin(theta));
p.push_back(center[2] + radius * cos(phi));
d.push_back(center[0] - p[0]);
d.push_back(center[1] - p[1]);
d.push_back(center[2] - p[2]);
fastf_t theta = RandomNum(0, M_PI);
fastf_t phi = RandomNum(-M_PI, M_PI);
p.push_back(center[0] + radius * sin(theta) * cos(phi));
p.push_back(center[1] + radius * sin(theta) * sin(phi));
p.push_back(center[2] + radius * cos(theta));
d.push_back(RandomNum(0, 1));
d.push_back(RandomNum(0, 1));
d.push_back(RandomNum(0, 1));
square_sum = pow(pow(d[0], 2) + pow(d[1], 2) + pow(d[2], 2), 0.5);
d[0] /= square_sum;
d[1] /= square_sum;
d[2] /= square_sum;
VSUB2(center_v, center, p);
if (VDOT(center_v, d) <= 0)
{
i--;
continue;
}
res.push_back(std::make_pair(p, d));
}
return res;
Expand Down Expand Up @@ -315,22 +322,30 @@ namespace rt_sample
}
return res;
}
RayParam RangeFixVecHit(size_t num, fastf_t max, fastf_t min, std::vector < fastf_t> vec)
RayParam RangeHit(size_t num, fastf_t max, fastf_t min)
{
RayParam res;
point_t center{ 0 }, random_point{ 0 };
fastf_t radius = APP.a_rt_i->rti_radius;
VADD2SCALE(center, APP.a_rt_i->rti_pmin, APP.a_rt_i->rti_pmax, 0.5);
std::vector<fastf_t> p;
std::vector<fastf_t> d = vec;
std::vector<fastf_t> d;
vect_t center_v;
fastf_t square_sum;
fastf_t theta;
fastf_t phi;
for (int i = 0; i < num; ++i) {
p.clear();
fastf_t theta = RandomNum(0, 2 * M_PI);
fastf_t phi = acos(2 * RandomNum(0, 1) - 1);
VSET(p, center[0] + radius * sin(phi) * cos(theta), center[1] + radius * sin(phi) * sin(theta), center[2] + radius * cos(phi));
p = std::vector<fastf_t>(3, 0);
d = std::vector<fastf_t>(3, 0);
theta = RandomNum(0,M_PI);
theta = theta == 0 ? 1e-5 : theta;
phi = RandomNum(-M_PI,M_PI);
fastf_t ptheta = RandomNum(0.5*M_PI, 0.85*M_PI);
fastf_t pphi = RandomNum(-0.85*M_PI, -0.5*M_PI);
VSET(p, center[0] + radius * sin(theta) * cos(phi), center[1] + radius * sin(theta) * sin(phi), center[2] + radius * cos(theta));
VSUB2(center_v, center, p);
if (VDOT(center_v, vec) <= 0)
VSET(d, 1 * sin(ptheta) * cos(pphi), 1 * sin(ptheta) * sin(pphi), 1 * cos(ptheta));
if (VDOT(center_v, d) <= 0)
{
i--;
continue;
Expand Down
17 changes: 14 additions & 3 deletions src/rt/torch_runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "torch_runner.h"
#include <iostream>
std::string global_model_path;
int model_type;
typedef enum {
normal = 1,
neu_coordinate = 2,
Expand All @@ -12,6 +13,10 @@ void set_model_path(const char* model_path)
{
global_model_path = std::string(model_path);
}
void set_model_type(int num)
{
model_type = num;
}
static torch::jit::script::Module& get_model() {
static torch::jit::script::Module module;
static bool is_loaded = false;
Expand Down Expand Up @@ -49,16 +54,22 @@ void run_torch(double* para, int* res)
else if (int(para[0]) == neu_sphere)
{
para++;
for (int i = 0; i < 5; i++)
for (int i = 0; i < 6; i++)
{
input_vec.push_back(*para);
para++;
}
inputs.push_back(torch::tensor({ {input_vec[0], input_vec[1]} }, torch::kFloat).to(at::kCUDA));
if(model_type==2)
inputs.push_back(torch::tensor({ {input_vec[0], input_vec[1]} }, torch::kFloat).to(at::kCUDA));
else if (model_type == 4)
{
/*inputs.push_back(torch::tensor({ {input_vec[0], input_vec[1]} }, torch::kFloat).to(at::kCUDA));
inputs.push_back(torch::tensor({ {input_vec[2], input_vec[3]} }, torch::kFloat).to(at::kCUDA));*/
inputs.push_back(torch::tensor({ {input_vec[0], input_vec[1],input_vec[2], input_vec[3]} }, torch::kFloat).to(at::kCUDA));
}
}
// inputs.push_back(input_tensor);
at::Tensor output = module.forward(inputs).toTensor().to(torch::kCPU);
auto tmp = output.data_ptr<float>();
std::vector<double> output_vector(output.data_ptr<float>(), output.data_ptr<float>() + output.numel());
res[0] = int(output_vector[0]);
if (res[0] > 255)
Expand Down
30 changes: 23 additions & 7 deletions src/rt/train_neural.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,51 @@
render_type rt_render_type;
int main(int argc, char* argv[])
{
fastf_t a = 74.49;
fastf_t b = -a;
// database name
const char* db = "C:\\works\\soc\\rainy\\brlcad\\build\\share\\db\\moss.g";
// object name
const char* ob = "all.g";
struct rt_i* rtip = NULL;
// set size
set_size(64);
rt_tool::init_rt(db, ob, rtip);
// do_ae(10, 10);
//rt_perspective = 90;
// do_ae(30, 25);
// rt_perspective = 120;

//rendering
#if 1
// choose rendering type:normal , neu_coordinate(Cartesian coordinate) neu_sphere
set_type(neu_sphere);
set_model_path("C:\\works\\soc\\rainy\\Rendernn\\models\\model_sph5.pt");
// choose model's input number: 2 for pos, 4 for both pos and dir
set_model_type(2);
// choose your model type
set_model_path("C:\\works\\soc\\rainy\\Rendernn\\models\\model_sph_grid6.pt");
// set_model_path("C:\\works\\soc\\rainy\\Rendernn\\models\\dir_model_sph_grid2.pt");
// set_model_path("C:\\works\\soc\\rainy\\Rendernn\\models\\test.pt");
// begin rendering
rt_neu::render();
#endif

//sampling
# if 0
// there are many different sample methods:
// auto ray_list = rt_sample::RangeFixVec(100000, 400, -100, { -0.742403865,-0.519836783,-0.422618270 });
// auto ray_list = rt_sample::SampleRandom(1000);
// auto ray_list = rt_sample::SampleFixVecHit(100000, { -0.74240387650610373, -0.51983679072568467, -0.42261826174069961 });
// auto ray_list = rt_sample::RangeFixVecHit(1000, 100,-100,{ -0.74240387650610373, -0.51983679072568467, -0.42261826174069961 });
auto ray_list = rt_sample::SampleFixVecHit(1000000, { -0.74240387650610373, -0.51983679072568467, -0.42261826174069961 });
// auto ray_list = rt_sample::SampleFixVecHit(1000000, { -0.74240387650610373, -0.51983679072568467, -0.42261826174069961 });
auto ray_list = rt_sample::RangeHit(1000,M_PI,0.75*M_PI);
point_t center{ 0 };
get_center(center);
// shoot these points
auto ray_res = rt_tool::ShootSamples(ray_list);
// convert Cartesian coordinate to Spherical coordinate
auto ray_list_sph = convert::cert_to_sph(ray_list, center, get_r());
// test for write json
// util::write_json(ray_list, ray_res, "C:\\works\\soc\\rainy\\Rendernn\\datas\\e.json");
util::write_sph_json(ray_list_sph, ray_res, "C:\\works\\soc\\rainy\\Rendernn\\datas\\sph_6.json");
ray_list.clear();
// write to a local file
util::write_sph_json(ray_list_sph, ray_res, "C:\\works\\soc\\rainy\\Rendernn\\datas\\all_dir_sph_range_3.json");
#endif
return 0;
}

0 comments on commit 1870833

Please sign in to comment.