File indexing completed on 2026-04-09 07:49:19
0001
0002
0003 #include <iostream>
0004
0005 #include "ssys.h"
0006 #include "stra.h"
0007 #include "stree.h"
0008 #include "snd.hh"
0009 #include "scsg.hh"
0010 #include "NPFold.h"
0011 #include "OpticksCSG.h"
0012
0013 const char* TEST = getenv("TEST");
0014 const char* FOLD = getenv("FOLD");
0015 const int TREE = ssys::getenvint("TREE", 0);
0016
0017
0018 void do_Add()
0019 {
0020 std::cout << "test_Add" << std::endl ;
0021 int a = snd::Zero( 1., 2., 3., 4., 5., 6. );
0022 int b = snd::Sphere(100.) ;
0023 int c = snd::ZSphere(100., -10., 10. ) ;
0024
0025 std::vector<int> prims = {a,b,c} ;
0026 int d = snd::Compound(CSG_CONTIGUOUS, prims ) ;
0027
0028 std::cout << "test_Add : Desc dumping " << std::endl;
0029
0030 std::cout << " a " << snd::Desc(a) << std::endl ;
0031 std::cout << " b " << snd::Desc(b) << std::endl ;
0032 std::cout << " c " << snd::Desc(c) << std::endl ;
0033 std::cout << " d " << snd::Desc(d) << std::endl ;
0034 }
0035
0036 void test_save()
0037 {
0038 do_Add();
0039
0040 std::cout << snd::Desc() ;
0041 NPFold* fold = snd::Serialize() ;
0042 std::cout << " save snd to FOLD " << FOLD << std::endl ;
0043 fold->save(FOLD);
0044 }
0045
0046 void test_load()
0047 {
0048 NPFold* fold = NPFold::Load(FOLD) ;
0049 std::cout << " load snd from FOLD " << FOLD << std::endl ;
0050 snd::Import(fold);
0051 std::cout << snd::Desc() ;
0052 }
0053
0054
0055 void test_max_depth_(int n, int xdepth)
0056 {
0057 const snd* nd = snd::Get(n);
0058 assert( nd );
0059 assert( nd->max_depth() == xdepth );
0060 assert( snd::GetMaxDepth(n) == xdepth );
0061
0062 std::cout << "test_max_depth_ n " << n << " xdepth " << xdepth << std::endl ;
0063 }
0064
0065 void test_max_depth()
0066 {
0067 std::cout << "test_max_depth" << std::endl ;
0068
0069 int a = snd::Sphere(100.) ;
0070 int b = snd::Sphere(100.) ;
0071 int c = snd::Boolean(CSG_UNION, a, b ) ;
0072 int d = snd::Box3(100.) ;
0073 int e = snd::Boolean(CSG_UNION, c, d ) ;
0074 int f = snd::Box3(100.) ;
0075 int g = snd::Boolean(CSG_UNION, f, e ) ;
0076
0077
0078
0079 test_max_depth_( a, 0 );
0080 test_max_depth_( b, 0 );
0081 test_max_depth_( c, 1 );
0082 test_max_depth_( d, 0 );
0083 test_max_depth_( e, 2 );
0084 test_max_depth_( f, 0 );
0085 test_max_depth_( g, 3 );
0086
0087 }
0088
0089
0090
0091 void test_max_binary_depth_(int n, int xdepth)
0092 {
0093 const snd* nd = snd::Get(n);
0094 assert( nd );
0095 assert( nd->max_binary_depth() == xdepth );
0096
0097 std::cout << "test_max_binary_depth_ n " << n << " xdepth " << xdepth << std::endl ;
0098 }
0099
0100
0101 void test_max_binary_depth()
0102 {
0103 std::cout << "test_max_binary_depth" << std::endl ;
0104
0105 int a = snd::Sphere(100.) ;
0106 int b = snd::Sphere(100.) ;
0107 int c = snd::Boolean(CSG_UNION, a, b ) ;
0108 int d = snd::Box3(100.) ;
0109 int e = snd::Boolean(CSG_UNION, c, d ) ;
0110 int f = snd::Box3(100.) ;
0111 int g = snd::Boolean(CSG_UNION, f, e ) ;
0112
0113 test_max_binary_depth_( a, 0 );
0114 test_max_binary_depth_( b, 0 );
0115 test_max_binary_depth_( c, 1 );
0116 test_max_binary_depth_( d, 0 );
0117 test_max_binary_depth_( e, 2 );
0118 test_max_binary_depth_( f, 0 );
0119 test_max_binary_depth_( g, 3 );
0120 }
0121
0122
0123
0124 void test_num_node_(int n, int xnn)
0125 {
0126 const snd* nd = snd::Get(n);
0127
0128 int nnn = nd->num_node() ;
0129 int gnn = snd::GetNumNode(n) ;
0130
0131 std::cout
0132 << "test_num_node_ n " << n
0133 << " xnn " << xnn
0134 << " nnn " << nnn
0135 << " gnn " << gnn
0136 << std::endl
0137 ;
0138
0139 assert( nd );
0140 assert( nnn == xnn );
0141 assert( gnn == xnn );
0142
0143 }
0144
0145
0146 void test_num_node()
0147 {
0148 std::cout << "test_num_node" << std::endl ;
0149
0150 int a = snd::Sphere(100.) ;
0151 int b = snd::Sphere(100.) ;
0152 int c = snd::Boolean(CSG_UNION, a, b ) ;
0153 int d = snd::Box3(100.) ;
0154 int e = snd::Boolean(CSG_UNION, c, d ) ;
0155 int f = snd::Box3(100.) ;
0156 int g = snd::Boolean(CSG_UNION, f, e ) ;
0157
0158
0159
0160 test_num_node_( a, 1 );
0161 test_num_node_( b, 1 );
0162 test_num_node_( c, 3 );
0163 test_num_node_( d, 1 );
0164 test_num_node_( e, 5 );
0165 test_num_node_( f, 1 );
0166 test_num_node_( g, 7 );
0167 }
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196 int make_csgtree_0()
0197 {
0198 int a = snd::Sphere(100.) ;
0199 int b = snd::Sphere(100.) ;
0200 int c = snd::Boolean(CSG_UNION, a, b ) ;
0201 int d = snd::Box3(100.) ;
0202 int e = snd::Boolean(CSG_UNION, c, d ) ;
0203 int f = snd::Box3(100.) ;
0204 int g = snd::Boolean(CSG_UNION, f, e ) ;
0205
0206 snd::SetLabel(a, "a");
0207 snd::SetLabel(b, "b");
0208 snd::SetLabel(c, "c");
0209 snd::SetLabel(d, "d");
0210 snd::SetLabel(e, "e");
0211 snd::SetLabel(f, "f");
0212 snd::SetLabel(g, "g");
0213
0214 snd::SetLVID(g, 0);
0215
0216 return g ;
0217 }
0218
0219 int make_csgtree_1()
0220 {
0221 int a = snd::Sphere(100.) ;
0222 int b = snd::Sphere(100.) ;
0223 int c = snd::Boolean(CSG_UNION, a, b ) ;
0224
0225 snd::SetLabel(a, "a");
0226 snd::SetLabel(b, "b");
0227 snd::SetLabel(c, "c");
0228
0229 snd::SetLVID(c, 0);
0230 return c ;
0231 }
0232
0233 int make_csgtree_2()
0234 {
0235 int a = snd::Sphere(100.) ;
0236 int b = snd::Sphere(100.) ;
0237 int c = snd::Sphere(100.) ;
0238 std::vector<int> prims = {a,b,c} ;
0239
0240 int d = snd::Compound(CSG_CONTIGUOUS, prims ) ;
0241
0242 snd::SetLabel(a, "a");
0243 snd::SetLabel(b, "b");
0244 snd::SetLabel(c, "c");
0245 snd::SetLabel(d, "d");
0246
0247 snd::SetLVID(d, 0);
0248 return d ;
0249 }
0250
0251 int make_csgtree_3()
0252 {
0253 int a = snd::Sphere(100.) ;
0254 int b = snd::Sphere(100.) ;
0255 int c = snd::Sphere(100.) ;
0256 std::vector<int> abc = {a,b,c} ;
0257 int d = snd::Compound(CSG_CONTIGUOUS, abc ) ;
0258
0259 snd::SetLabel(a, "a");
0260 snd::SetLabel(b, "b");
0261 snd::SetLabel(c, "c");
0262 snd::SetLabel(d, "d");
0263
0264 int e = snd::Sphere(100.) ;
0265 int f = snd::Sphere(100.) ;
0266 int g = snd::Sphere(100.) ;
0267 std::vector<int> efg = {e,f,g} ;
0268 int h = snd::Compound(CSG_CONTIGUOUS, efg ) ;
0269
0270 snd::SetLabel(e, "e");
0271 snd::SetLabel(f, "f");
0272 snd::SetLabel(g, "g");
0273 snd::SetLabel(h, "h");
0274
0275 int i = snd::Boolean(CSG_UNION, d, h );
0276 snd::SetLabel(i, "i");
0277
0278 snd::SetLVID(i, 0);
0279 return i ;
0280 }
0281
0282
0283 int make_csgtree_4()
0284 {
0285 int a = snd::Sphere(100.) ;
0286 int b = snd::Sphere(100.) ;
0287 int c = snd::Sphere(100.) ;
0288 int d = snd::Sphere(100.) ;
0289 int e = snd::Sphere(100.) ;
0290 int f = snd::Sphere(100.) ;
0291 int g = snd::Sphere(100.) ;
0292
0293 snd::SetLabel(a, "a");
0294 snd::SetLabel(b, "b");
0295 snd::SetLabel(c, "c");
0296 snd::SetLabel(d, "d");
0297 snd::SetLabel(e, "e");
0298 snd::SetLabel(f, "f");
0299 snd::SetLabel(g, "g");
0300
0301 std::vector<int> prims = {a,b,c,d,e,f,g} ;
0302 int z = snd::Collection(prims) ;
0303 snd::SetLabel(z, "z");
0304
0305
0306 return z ;
0307 }
0308
0309
0310
0311 void AddDummyTransformsVisit(int idx)
0312 {
0313 const snd* nd = snd::Get(idx);
0314 bool rhs = nd->is_sibdex(1) ;
0315
0316 if( rhs )
0317 {
0318 glm::tmat4x4<double> tr = stra<double>::Translate(double(idx), 0., 100.*double(idx), 1., false );
0319 snd::SetNodeXForm(idx, tr );
0320 std::cout << "AddDummyTransformsVisit idx " << idx << std::endl ;
0321 }
0322 }
0323 void AddDummyTransforms(int root)
0324 {
0325 std::cout << "AddDummyTransforms root " << root << std::endl ;
0326 snd::PreorderTraverse(root, std::bind( &AddDummyTransformsVisit , std::placeholders::_1 ) );
0327 }
0328
0329
0330 int make_csgtree()
0331 {
0332 int t = -1 ;
0333 switch(TREE)
0334 {
0335 case 0: t = make_csgtree_0() ; break ;
0336 case 1: t = make_csgtree_1() ; break ;
0337 case 2: t = make_csgtree_2() ; break ;
0338 case 3: t = make_csgtree_3() ; break ;
0339 case 4: t = make_csgtree_4() ; break ;
0340 }
0341 assert( t > -1 );
0342 AddDummyTransforms(t);
0343 return t ;
0344 }
0345
0346
0347
0348
0349
0350
0351 void test_inorder()
0352 {
0353 std::cout << "test_inorder" << std::endl ;
0354
0355 int g = make_csgtree();
0356 const snd* nd = snd::Get(g);
0357
0358 std::vector<int> order ;
0359 nd->inorder(order);
0360
0361 std::cout << " order.size " << order.size() << std::endl ;
0362
0363 for(int z=0 ; z < int(order.size()) ; z++)
0364 {
0365 int idx = order[z] ;
0366 const snd* n = snd::Get(idx);
0367 std::cout << " idx " << idx ;
0368 std::cout << " n.brief " << ( n ? n->brief() : "null" ) << std::endl ;
0369
0370 if(n == nullptr) continue ;
0371 std::cout << n->label << std::endl ;
0372 }
0373
0374 }
0375
0376 void test_dump()
0377 {
0378 std::cout << "test_dump" << std::endl ;
0379 int g = make_csgtree();
0380 const snd* nd = snd::Get(g);
0381
0382 std::cout << nd->dump() << std::endl ;
0383 std::cout << nd->dump2() << std::endl ;
0384 }
0385
0386 void test_render()
0387 {
0388 int mode = ssys::getenvint("MODE", -1);
0389 std::cout << "test_render mode " << mode << std::endl ;
0390
0391 int g = make_csgtree();
0392 const snd* nd = snd::Get(g);
0393
0394 std::cout << nd->render(mode) << std::endl ;
0395 std::cout << nd->rbrief() << std::endl ;
0396 }
0397
0398 void test_rbrief()
0399 {
0400 std::cout << "test_rbrief" << std::endl ;
0401
0402 int g = make_csgtree();
0403 const snd* nd = snd::Get(g);
0404 std::cout << nd->rbrief() << std::endl ;
0405
0406 }
0407
0408
0409 void test_typenodes(int tc)
0410 {
0411 std::cout << "test_typenodes tc " << tc << " " << CSG::Tag(tc) << std::endl ;
0412
0413 int g = make_csgtree();
0414 const snd* nd = snd::Get(g);
0415
0416 std::vector<int> nodes ;
0417 nd->typenodes(nodes, tc );
0418
0419 std::cout << snd::Brief(nodes) ;
0420 }
0421
0422
0423
0424 void test_typenodes()
0425 {
0426 test_typenodes(CSG_UNION);
0427 test_typenodes(CSG_SPHERE);
0428 test_typenodes(CSG_CONTIGUOUS);
0429 }
0430
0431 void test_typenodes_()
0432 {
0433 std::cout << "test_typenodes_ " << std::endl ;
0434
0435 int g = make_csgtree();
0436 const snd* nd = snd::Get(g);
0437
0438 std::vector<int> nodes ;
0439
0440 nd->typenodes_(nodes, CSG_UNION, CSG_SPHERE, CSG_CONTIGUOUS );
0441 std::cout << "snd::DescType " << snd::DescType(CSG_UNION, CSG_SPHERE, CSG_CONTIGUOUS) << std::endl ;
0442 std::cout << snd::Brief(nodes) ;
0443 nodes.clear();
0444
0445 nd->typenodes_(nodes, CSG_UNION, CSG_SPHERE );
0446 std::cout << "snd::DescType " << snd::DescType(CSG_UNION, CSG_SPHERE, CSG_CONTIGUOUS) << std::endl ;
0447 std::cout << snd::Brief(nodes) ;
0448 nodes.clear();
0449
0450 nd->typenodes_(nodes, CSG_UNION );
0451 std::cout << "snd::DescType " << snd::DescType(CSG_UNION) << std::endl ;
0452 std::cout << snd::Brief(nodes) ;
0453 nodes.clear();
0454
0455 nd->typenodes_(nodes, CSG_SPHERE );
0456 std::cout << "snd::DescType " << snd::DescType(CSG_SPHERE) << std::endl ;
0457 std::cout << snd::Brief(nodes) ;
0458 nodes.clear();
0459
0460 }
0461
0462 void test_leafnodes()
0463 {
0464 std::cout << "test_leafnodes " << std::endl ;
0465
0466 int g = make_csgtree();
0467 const snd* nd = snd::Get(g);
0468
0469 std::vector<int> nodes ;
0470 nd->leafnodes(nodes);
0471
0472 std::cout << " leafnodes " << nodes.size() << std::endl ;
0473 std::cout << snd::Brief(nodes) ;
0474
0475 std::cout << nd->render(0) ;
0476 std::cout << nd->render(3) ;
0477 }
0478
0479
0480 void test_find()
0481 {
0482 std::cout << "test_find" << std::endl ;
0483
0484 int z = make_csgtree();
0485 const snd* nd = snd::Get(z);
0486
0487 int a = nd->find('a') ;
0488 std::cout << snd::Desc(a) << std::endl ;
0489
0490 int f = nd->find('f') ;
0491 std::cout << snd::Desc(f) << std::endl ;
0492
0493
0494 std::cout << snd::Render(z, 0) ;
0495 std::cout << snd::Render(z, 3) ;
0496 }
0497
0498
0499
0500
0501
0502 void test_ancestors()
0503 {
0504 std::cout << "test_ancestors " << std::endl ;
0505
0506 int iz = make_csgtree();
0507 const snd* z = snd::Get(iz);
0508
0509 std::vector<int> za ;
0510 z->ancestors(za);
0511 assert( za.size() == 0 );
0512
0513 std::cout << z->render(0) ;
0514 std::cout << z->render(3) ;
0515
0516 int a = z->find('a') ;
0517
0518 std::vector<int> aa ;
0519 snd::Ancestors(a, aa);
0520 assert( aa.size() > 0 );
0521
0522 std::cout << "aa:" << snd::Brief(aa) ;
0523
0524 std::cout << " a:" << snd::Brief(a) << std::endl ;
0525
0526 }
0527
0528
0529
0530 void LocalVisit(int idx)
0531 {
0532 const snd* nd = snd::Get(idx);
0533 bool rhs = nd->is_sibdex(1) ;
0534
0535 std::cout
0536 << "LocalVisit"
0537 << " rhs " << ( rhs ? "Y" : "N" )
0538 << " "
0539 << nd->brief()
0540 << std::endl
0541 ;
0542 }
0543
0544 void test_traverse()
0545 {
0546 std::cout << "test_traverse " << std::endl ;
0547 int z = make_csgtree();
0548
0549
0550 std::cout << "snd::PreorderTraverse.LocalVisit" << std::endl ;
0551 snd::PreorderTraverse(z, std::bind( &LocalVisit, std::placeholders::_1 ) );
0552
0553 std::cout << snd::Render(z, 1) ;
0554
0555 std::cout << "snd::PostorderTraverse.LocalVisit" << std::endl ;
0556 snd::PostorderTraverse(z, std::bind( &LocalVisit, std::placeholders::_1 ) );
0557 }
0558
0559
0560 void test_NodeTransformProduct()
0561 {
0562 std::cout << "test_NodeTransformProduct " << std::endl ;
0563
0564 int z = make_csgtree();
0565 char q = 'f' ;
0566 int n = snd::Find(z, q) ;
0567
0568 std::cout
0569 << " z " << z
0570 << " q " << q
0571 << " n " << n
0572 << std::endl
0573 << snd::Render(z, 0)
0574 << snd::Render(z, 1)
0575 ;
0576
0577 glm::tmat4x4<double> t(1.) ;
0578 glm::tmat4x4<double> v(1.) ;
0579 snd::NodeTransformProduct(n, t, v, false );
0580 std::cout << "t: " << glm::to_string(t) << std::endl ;
0581 std::cout << "v: " << glm::to_string(v) << std::endl ;
0582
0583 std::cout << snd::DescNodeTransformProduct(n, t, v, false ) << std::endl ;
0584 std::cout << "t: " << glm::to_string(t) << std::endl ;
0585 std::cout << "v: " << glm::to_string(v) << std::endl ;
0586
0587
0588 }
0589
0590 void _test_GetXF(int idx)
0591 {
0592 const sxf* xf = snd::GetXF(idx);
0593 std::cout
0594 << "_test_GetXF"
0595 << " idx " << std::setw(2) << idx
0596 << " xf " << ( xf ? "Y" : "N" )
0597 << std::endl
0598 ;
0599
0600 if(xf) std::cout << xf->desc() << std::endl ;
0601 }
0602
0603 void test_GetXF()
0604 {
0605 int z = make_csgtree();
0606
0607 std::cout
0608 << snd::Render(z,0)
0609 << snd::Render(z,1)
0610 << snd::Render(z,2)
0611 << snd::Render(z,3)
0612 ;
0613
0614
0615 snd::PreorderTraverse(z, std::bind( &_test_GetXF, std::placeholders::_1 ) );
0616
0617 snd::PreorderTraverse(z, [](int idx){ std::cout << snd::Brief(idx) << std::endl ; } );
0618
0619
0620 NPFold* fold = snd::Serialize() ;
0621 std::cout << " save snd to FOLD " << FOLD << std::endl ;
0622 fold->save(FOLD, "GetXF");
0623 }
0624
0625
0626 int main(int argc, char** argv)
0627 {
0628 stree st ;
0629
0630 if( strcmp(TEST, "save")==0) test_save();
0631 else if(strcmp(TEST, "load")==0) test_load();
0632 else if(strcmp(TEST, "max_depth")==0) test_max_depth() ;
0633 else if(strcmp(TEST, "max_binary_depth")==0) test_max_binary_depth() ;
0634 else if(strcmp(TEST, "num_node")==0) test_num_node() ;
0635 else if(strcmp(TEST, "inorder")==0) test_inorder() ;
0636 else if(strcmp(TEST, "dump")==0) test_dump() ;
0637 else if(strcmp(TEST, "render")==0) test_render() ;
0638 else if(strcmp(TEST, "rbrief")==0) test_rbrief() ;
0639 else if(strcmp(TEST, "typenodes")==0) test_typenodes() ;
0640 else if(strcmp(TEST, "typenodes_")==0) test_typenodes_() ;
0641 else if(strcmp(TEST, "leafnodes")==0) test_leafnodes() ;
0642 else if(strcmp(TEST, "find")==0) test_find() ;
0643 else if(strcmp(TEST, "ancestors")==0) test_ancestors() ;
0644 else if(strcmp(TEST, "traverse")==0) test_traverse() ;
0645 else if(strcmp(TEST, "NodeTransformProduct")==0) test_NodeTransformProduct() ;
0646 else if(strcmp(TEST, "GetXF")==0) test_GetXF() ;
0647 else std::cerr << " TEST not matched " << TEST << std::endl ;
0648
0649 return 0 ;
0650 }
0651