File indexing completed on 2026-04-09 07:48:57
0001 #include "SPath.hh"
0002 #include "NP.hh"
0003 #include "scuda.h"
0004 #include "CSGGrid.h"
0005 #include "SLOG.hh"
0006
0007
0008 CSGGrid::CSGGrid( const float4& ce_, int nx_, int ny_, int nz_ )
0009 :
0010 ce(ce_),
0011 margin(1.2f),
0012 nx(nx_),
0013 ny(ny_),
0014 nz(nz_),
0015 gridscale(make_float3(margin*ce.w/float(nx), margin*ce.w/float(ny), margin*ce.w/float(nz))),
0016 ni(2*nz+1),
0017 nj(2*ny+1),
0018 nk(2*nx+1),
0019 sdf(NP::Make<float>(ni,nj,nk)),
0020 sdf_v(sdf->values<float>()),
0021 xyzd(NP::Make<float>(ni,nj,nk,4)),
0022 xyzd_v(xyzd->values<float>())
0023 {
0024 init();
0025 }
0026 void CSGGrid::init()
0027 {
0028 init_meta();
0029 }
0030
0031 void CSGGrid::init_meta()
0032 {
0033 sdf->set_meta<float>("cex", ce.x );
0034 sdf->set_meta<float>("cey", ce.y );
0035 sdf->set_meta<float>("cez", ce.z );
0036 sdf->set_meta<float>("cew", ce.w );
0037
0038 sdf->set_meta<float>("ox", float(-nx)*gridscale.x );
0039 sdf->set_meta<float>("oy", float(-ny)*gridscale.y );
0040 sdf->set_meta<float>("oz", float(-nz)*gridscale.z );
0041
0042 sdf->set_meta<float>("sx", gridscale.x );
0043 sdf->set_meta<float>("sy", gridscale.y );
0044 sdf->set_meta<float>("sz", gridscale.z );
0045 }
0046
0047 void CSGGrid::scan( std::function<float(const float3&)> sdf )
0048 {
0049 float3 position = make_float3( 0.f, 0.f, 0.f );
0050
0051
0052
0053 for(int i=0 ; i < ni ; i++ )
0054 {
0055 int iz = -nz + i ;
0056 position.z = ce.z + float(iz)*gridscale.z ;
0057
0058 for(int j=0 ; j < nj ; j++ )
0059 {
0060 int iy = -ny + j ;
0061 position.y = ce.y + float(iy)*gridscale.y ;
0062
0063 for(int k=0 ; k < nk ; k++ )
0064 {
0065 int ix = -nx + k ;
0066 position.x = ce.x + float(ix)*gridscale.x ;
0067
0068 float sd = sdf( position );
0069
0070 int idx = i*nj*nk + j*nk + k ;
0071
0072 xyzd_v[idx*4 + 0] = position.x ;
0073 xyzd_v[idx*4 + 1] = position.y ;
0074 xyzd_v[idx*4 + 2] = position.z ;
0075 xyzd_v[idx*4 + 3] = sd ;
0076
0077 sdf_v[idx] = sd ;
0078 }
0079 }
0080 }
0081 }
0082
0083 const char* CSGGrid::BASE = "$TMP/CSG/CSGSignedDistanceFieldTest" ;
0084
0085 void CSGGrid::save(const char* geom, const char* base) const
0086 {
0087 int create_dirs = 2 ;
0088 const char* fold = SPath::Resolve(base ? base : BASE, geom, create_dirs );
0089 LOG(info) << "[ saving sdf.npy " << sdf->sstr() << " to " << fold ;
0090 sdf->save(fold, "sdf.npy");
0091 LOG(info) << "]" ;
0092
0093 LOG(info) << "[ saving xyzd.npy " << xyzd->sstr() << " to " << fold ;
0094 xyzd->save(fold, "xyzd.npy");
0095 LOG(info) << "]" ;
0096 }
0097
0098
0099