Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:48:55

0001 #include "SLOG.hh"
0002 #include "OpticksCSG.h"
0003 
0004 #include "scanvas.h"
0005 #include "scuda.h"
0006 #include "squad.h"
0007 
0008 #include "CSGNode.h"
0009 #include "CSGQuery.h"
0010 #include "CSGDraw.h"
0011 
0012 
0013 const plog::Severity CSGDraw::LEVEL = SLOG::EnvLevel("CSGDraw", "DEBUG"); 
0014 
0015 
0016 CSGDraw::CSGDraw(const CSGQuery* q_, char axis_ )
0017     :
0018     q(q_),
0019     axis(axis_),
0020     type(q->getSelectedType()), 
0021     width(q->select_prim_numNode),
0022     height( CSG::IsTree((OpticksCSG_t)type) ? q->getSelectedTreeHeight() : 1),
0023     canvas(new scanvas(width+2, height+2, 10, 5)),
0024     dump(false)
0025 {
0026 }
0027 
0028 char* CSGDraw::get() const
0029 {
0030     return canvas ? canvas->c : nullptr ; 
0031 }
0032 
0033 void CSGDraw::render()
0034 {
0035     if( CSG::IsTree((OpticksCSG_t)type) )
0036     {
0037         int nodeIdxRel_root = 1 ;
0038         int inorder = 0 ; 
0039         draw_tree_r( nodeIdxRel_root,  0, inorder ); 
0040     }
0041     else if( CSG::IsList((OpticksCSG_t)type) )
0042     {
0043         draw_list(); 
0044     }
0045     else if( CSG::IsLeaf((OpticksCSG_t)type) )
0046     {
0047         draw_leaf(); 
0048     }
0049     else
0050     {
0051         assert(0) ; // unexpected type 
0052     }
0053 }
0054 
0055 std::string CSGDraw::hdr() const  
0056 {
0057     std::stringstream ss ; 
0058     ss 
0059        << " axis " << axis 
0060        << " type " << type
0061        << " CSG::Name(type) " << CSG::Name(type)
0062        << " IsTree " <<  CSG::IsTree((OpticksCSG_t)type) 
0063        << " width " << width 
0064        << " height " << height
0065        ;
0066 
0067     std::string s = ss.str(); 
0068     return s ; 
0069 }
0070 
0071 std::string CSGDraw::desc() 
0072 {
0073     render(); 
0074     std::stringstream ss ; 
0075     ss << hdr() << std::endl ; 
0076     ss << canvas->c << std::endl ; 
0077     std::string s = ss.str(); 
0078     return s ; 
0079 }
0080 
0081 
0082 /**
0083 CSGDraw::draw_tree_r
0084 -----------------------
0085 
0086 nodeIdxRel
0087    1-based tree index, root=1 
0088 
0089 **/
0090 
0091 void CSGDraw::draw_tree_r(int nodeIdxRel, int depth, int& inorder ) 
0092 {
0093     const CSGNode* nd = q->getSelectedNode( nodeIdxRel - 1  );  // convert 1-based index to 0-based
0094     if( nd == nullptr ) return ; 
0095     if( nd->is_zero() ) return ; 
0096 
0097     const float* aabb = nd->AABB();  
0098     float a0, a1 ; 
0099     switch(axis)
0100     {
0101        case 'X': a0 = aabb[0] ; a1 = aabb[0+3] ; break ; 
0102        case 'Y': a0 = aabb[1] ; a1 = aabb[1+3] ; break ; 
0103        case 'Z': a0 = aabb[2] ; a1 = aabb[2+3] ; break ; 
0104     }
0105 
0106     std::string brief = nd->brief(); 
0107     const char* label = brief.c_str(); 
0108 
0109     int left = nodeIdxRel << 1 ; 
0110     int right = left + 1 ; 
0111 
0112     draw_tree_r(left,  depth+1, inorder ); 
0113 
0114     // inorder visit 
0115     {
0116         if(dump) std::cout 
0117              << " nodeIdxRel " << std::setw(5) << nodeIdxRel
0118              << " depth " << std::setw(5) << depth
0119              << " inorder " << std::setw(5) << inorder
0120              << " brief " << brief
0121              << " : " << nd->desc() 
0122              << std::endl 
0123              ;
0124 
0125         int ix = inorder ;  
0126         int iy = depth ; 
0127 
0128         canvas->draw(   ix, iy, 0,0,  label ); 
0129         canvas->draw(   ix, iy, 0,1,  nodeIdxRel ); 
0130 
0131         const char* fmt = "%7.2f" ; 
0132         canvas->drawf(  ix, iy, 0,2,  a1 , fmt); 
0133         canvas->drawf(  ix, iy, 0,3,  a0 , fmt); 
0134      
0135         inorder += 1 ; 
0136     }
0137     draw_tree_r(right, depth+1, inorder ); 
0138 }
0139 
0140 void CSGDraw::draw_list()
0141 {
0142     assert( CSG::IsList((OpticksCSG_t)type) ); 
0143 
0144     unsigned idx = 0 ; 
0145     const CSGNode* head = q->getSelectedNode(idx);
0146     unsigned sub_num = head->subNum() ; 
0147 
0148     LOG(info)
0149         << " sub_num " << sub_num 
0150         ; 
0151 
0152     draw_list_item( head, idx ); 
0153 
0154     for(unsigned isub=0 ; isub < sub_num ; isub++)
0155     {
0156         idx = 1+isub ;   // 0-based node idx
0157         const CSGNode* sub = q->getSelectedNode(idx); 
0158 
0159         draw_list_item( sub, idx ); 
0160     }
0161 }
0162 
0163 void CSGDraw::draw_leaf()
0164 {
0165     assert( CSG::IsLeaf((OpticksCSG_t)type) ); 
0166 
0167     unsigned idx = 0 ; 
0168     const CSGNode* leaf = q->getSelectedNode(idx);
0169  
0170     draw_list_item( leaf, idx ); 
0171 } 
0172 
0173 void CSGDraw::draw_list_item( const CSGNode* nd, unsigned idx )
0174 {
0175     int ix = idx == 0 ? 0 : idx+1  ; 
0176     int iy = idx == 0 ? 0 : 1      ;
0177 
0178     std::string brief = nd->brief(); 
0179     const char* label = brief.c_str(); 
0180 
0181     canvas->draw(   ix, iy, 0,0,  label ); 
0182     canvas->draw(   ix, iy, 0,1,  idx ); 
0183 }
0184 
0185