Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:49:41

0001 #pragma once
0002 /**
0003 snd.hh : constituent CSG node in preparation
0004 ============================================= 
0005 
0006 Canonical use from U4Solid::init
0007 
0008 snd.h intended as minimal first step, holding parameters of 
0009 G4VSolid CSG trees for subsequent use by CSGNode::Make
0010 and providing dependency fire break between G4 and CSG 
0011 
0012 * snd.hh instances are one-to-one related to CSG/CSGNode.h
0013 
0014 * initially thought snd.hh would be transient with no persisting and no role on GPU. 
0015   But that is inconsistent with the rest of stree.h and also want to experiment 
0016   with non-binary intersection in future, so are using snd.hh to test non-binary 
0017   solid persisting following the same approach as snode.h structural nodes
0018 
0019 Usage requires the scsg.hh POOL. That is now done at scsg instanciation.
0020 
0021 TODO: 
0022 
0023 * add polycone ZNudge and tests ?
0024 * how about convexpolyhedron with planes ? just add spl ? 
0025 * how about multiunion ?
0026 
0027 
0028 TODO:
0029 
0030 * add complement (sn.h has it but snd.hh does not) : its essential 
0031   for positivization and getting the minimum bounding box
0032 
0033 
0034 **/
0035 
0036 #include <vector>
0037 #include <string>
0038 #include <functional>
0039 
0040 #include "glm/fwd.hpp"
0041 #include "OpticksCSG.h"
0042 #include "SYSRAP_API_EXPORT.hh"
0043 
0044 struct spa ; 
0045 struct sbb ; 
0046 struct sxf ; 
0047 
0048 struct scsg ; 
0049 struct NPFold ; 
0050 struct scanvas ; 
0051 
0052 struct SYSRAP_API snd
0053 {
0054     int typecode ;         // 0
0055     int complement ;       // 1    not impl
0056     int lvid ;             // 2
0057     int xform ;            // 3
0058     int param ;            // 4
0059     int aabb ;             // 5 
0060     int parent ;           // 6
0061 
0062     int sibdex ;           // 7     0-based sibling index 
0063     int num_child ;        // 8
0064     int first_child ;      // 9
0065     int next_sibling ;     // 10
0066  
0067     int index ;            // 11      set on adding to POOL by the scsg::add_ snd template specialization 
0068     int depth ;            // 12      set by calling SetLVID 
0069 
0070     char label[16] ;       // 13,14,14,16        sizeof 4 int 
0071 
0072 
0073     static constexpr const int VERSION = 0 ;
0074     static constexpr const char* NAME = "snd" ; 
0075     static constexpr const double zero = 0. ; 
0076     static scsg* POOL ; 
0077     static void SetPOOL( scsg* pool ); 
0078     static int  Level(); 
0079 
0080     static NPFold* Serialize(); 
0081     static void    Import(const NPFold* fold); 
0082 
0083     static std::string Desc();
0084     static std::string Brief(int idx);
0085     static std::string Brief( const std::vector<int>& nodes);
0086     static std::string Brief_(const std::vector<snd>& nodes);
0087 
0088     static const snd* Get( int idx);
0089     static       snd* Get_(int idx);
0090 
0091     static int  GetMaxDepth(int idx) ; 
0092     static int  GetNumNode(int idx) ; 
0093     static void GetTypes(std::vector<int>& types, const std::vector<int>& nd_idxs ); 
0094 
0095     static int  GetNodeXForm(int idx) ; 
0096     static void SetNodeXForm(int idx, const glm::tmat4x4<double>& t );
0097     static void SetNodeXForm(int idx, const glm::tmat4x4<double>& t, const glm::tmat4x4<double>& v );
0098 
0099     void setXF( const glm::tmat4x4<double>& t ); 
0100     void setXF( const glm::tmat4x4<double>& t, const glm::tmat4x4<double>& v ) ; 
0101 
0102     void combineXF( const glm::tmat4x4<double>& t ); 
0103     void combineXF( const glm::tmat4x4<double>& t, const glm::tmat4x4<double>& v ) ; 
0104 
0105 
0106 
0107     static const sxf* GetXF(int idx) ; 
0108     static       sxf* GetXF_(int idx) ; 
0109 
0110     const sxf* getXF() const ; 
0111     sxf*       getXF_(); 
0112 
0113     static void            NodeTransformProduct(int nidx, glm::tmat4x4<double>& t, glm::tmat4x4<double>& v, bool reverse, std::ostream* out ) ; 
0114     static std::string DescNodeTransformProduct(int nidx, glm::tmat4x4<double>& t, glm::tmat4x4<double>& v, bool reverse ) ; 
0115     void node_transform_product(                          glm::tmat4x4<double>& t, glm::tmat4x4<double>& v, bool reverse, std::ostream* out ) const ; 
0116 
0117 
0118 
0119     static void SetLabel( int idx , const char* label ); 
0120     static void SetLVID(  int idx , int lvid);  // label node tree 
0121 
0122     static void GetLVID( std::vector<snd>& nds, int lvid ); 
0123 
0124 
0125     static const snd* GetLVRoot( int lvid );
0126  
0127     static int GetLVNumNode( int lvid ); // total nodes 
0128     static int GetLVBinNode( int lvid ); // binary tree nodes
0129     static int GetLVSubNode( int lvid ); // compound constituent nodes
0130 
0131     int getLVNumNode() const ;  // total nodes 
0132     int getLVBinNode() const ;  // binary tree nodes (compound constituents not included) 
0133     int getLVSubNode() const ;  // sub nodes : ie the compound constituents  
0134 
0135     static void GetLVNodesComplete(std::vector<const snd*>& nds, int lvid); // unbalanced deep trees will have many nullptr 
0136     void        getLVNodesComplete(std::vector<const snd*>& nds) const ; 
0137     static void GetLVNodesComplete_r(std::vector<const snd*>& nds, const snd* nd, int idx); 
0138 
0139 
0140 
0141     static std::string Desc(int idx);
0142     static std::string DescParam(int idx);
0143     static std::string DescXForm(int idx);
0144     static std::string DescAABB( int idx);
0145 
0146     static int Add(const snd& nd); 
0147 
0148 
0149     bool is_listnode() const ; 
0150     int idx() const ; 
0151     std::string tag() const ; 
0152     std::string brief() const ; 
0153 
0154     std::string rbrief() const ; 
0155     void rbrief_r(std::ostream& os, int d) const ; 
0156 
0157     std::string desc() const ; 
0158 
0159 
0160     static const char* ERROR_NO_POOL_NOTES ; 
0161     static void CheckPOOL(const char* msg); 
0162 
0163     void setParam( double x,  double y,  double z,  double w,  double z1, double z2 ); 
0164     void setAABB(  double x0, double y0, double z0, double x1, double y1, double z1 );
0165 
0166     const double* getParam() const ; 
0167     const double* getAABB() const ; 
0168     bool hasUnsetAABB() const ;  // nullptr or all zero 
0169     bool hasAABB() const ;       // not-nullptr and not all zero 
0170 
0171     void setLabel( const char* l ); 
0172 
0173     void setLVID(int lvid_ ); 
0174     void setLVID_r(int lvid_, int d ); 
0175 
0176     int checktree() const ; 
0177     int checktree_r(char code,  int d ) const ; 
0178 
0179     static void Visit(int idx) ; 
0180 
0181     static void PreorderTraverse(int idx, std::function<void(int)> fn ); 
0182     void preorder_traverse(   std::function<void(int)> fn ) ; 
0183     void preorder_traverse_r( std::function<void(int)> fn, int d) ; 
0184 
0185     static void PostorderTraverse(int idx, std::function<void(int)> fn); 
0186     void postorder_traverse(   std::function<void(int)> fn ) ; 
0187     void postorder_traverse_r( std::function<void(int)> fn, int d) ; 
0188 
0189     int max_depth() const ; 
0190     int max_depth_r(int d) const ; 
0191 
0192     int max_binary_depth() const ;   // listnodes not recursed, listnodes regarded as leaf node primitives 
0193     int max_binary_depth_r(int d) const ; 
0194 
0195     int num_node() const ; 
0196     int num_node_r(int d) const ; 
0197 
0198     bool is_root() const ; 
0199     bool is_leaf() const ; 
0200     bool is_binary_leaf() const ;   // listnodes are regarded as binary leaves
0201     bool is_sibdex(int q_sibdex) const ; 
0202 
0203 
0204     static void Inorder(std::vector<int>& order, int idx ); 
0205     void inorder(std::vector<int>& nodes ) const ;    // collects absolute snd::index from inorder traversal 
0206     void inorder_r(std::vector<int>& order, int d ) const ; 
0207 
0208     static void Ancestors(int idx, std::vector<int>& nodes); 
0209     void ancestors(std::vector<int>& nodes) const; 
0210 
0211 
0212     void leafnodes( std::vector<int>& nodes ) const ; 
0213     void leafnodes_r( std::vector<int>& nodes, int d  ) const ; 
0214 
0215     static int Find(int idx, char l0); 
0216     int find(char l0) const ; 
0217     void find_r(std::vector<int>& nodes, char l0, int d) const ; 
0218 
0219     
0220     template<typename ... Args> 
0221     void typenodes_(  std::vector<int>& nodes, Args ... tcs ) const ; 
0222     void typenodes_r_(std::vector<int>& nodes, const std::vector<OpticksCSG_t>& types, int d) const ; 
0223     bool has_type(const std::vector<OpticksCSG_t>& types) const  ; 
0224 
0225     template<typename ... Args> 
0226     static std::string DescType(Args ... tcs); 
0227 
0228     void typenodes(std::vector<int>& nodes, int tc ) const ; 
0229     void typenodes_r(std::vector<int>& nodes, int tc, int d) const ; 
0230 
0231     int get_ordinal( const std::vector<int>& order ) const ; 
0232 
0233 
0234     std::string dump() const ; 
0235     void dump_r( std::ostream& os, int d ) const ; 
0236 
0237     std::string dump2() const ; 
0238     void dump2_r( std::ostream& os, int d ) const ; 
0239 
0240 
0241     static std::string Render(int idx, int mode=-1 ) ; 
0242     std::string render(int mode=-1) const ; 
0243     void render_r( scanvas* canvas, const std::vector<int>& order, int mode, int d) const ; 
0244     void render_v( scanvas* canvas, const std::vector<int>& order, int mode, int d) const ; 
0245 
0246 
0247     void check_z() const ; 
0248     double zmin() const ; 
0249     double zmax() const ; 
0250     void decrease_zmin( double dz ); 
0251     void increase_zmax( double dz ); 
0252     static std::string ZDesc(const std::vector<int>& prims);
0253     static void ZNudgeExpandEnds(    const std::vector<int>& prims); 
0254     static void ZNudgeOverlapJoints( const std::vector<int>& prims); 
0255 
0256 
0257     static snd Init(int tc); 
0258     void init(); 
0259 
0260     static int Boolean( int op, int l, int r ); 
0261     static int Compound(int type, const std::vector<int>& prims ); 
0262 
0263     private:
0264     static int UnionTree( const std::vector<int>& prims ); 
0265     static int Contiguous(const std::vector<int>& prims ); 
0266     public:
0267     static int Collection(const std::vector<int>& prims );   // UnionTree OR Contiguous depending on VERSION
0268 
0269     // signatures need to match CSGNode where these param will end up 
0270     static int Cylinder(double radius, double z1, double z2) ;
0271     static int Cone(double r1, double z1, double r2, double z2); 
0272     static int Sphere(double radius); 
0273     static int ZSphere(double radius, double z1, double z2); 
0274     static int Box3(double fullside); 
0275     static int Box3(double fx, double fy, double fz ); 
0276     static int Zero(double  x,  double y,  double z,  double w,  double z1, double z2); 
0277     static int Zero(); 
0278 };
0279 
0280