Geant4 Cross Reference |
1 // 2 // ******************************************************************** 3 // * License and Disclaimer * 4 // * * 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. * 10 // * * 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitation of liability. * 17 // * * 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************************************** 25 // 26 // G4Voxelizer 27 // 28 // Class description: 29 // 30 // Voxelizer for tessellated surfaces and solids positioning in 3D space, 31 // used in G4TessellatedSolid and G4MultiUnion. 32 33 // 19.10.12 Marek Gayer, created 34 // -------------------------------------------------------------------- 35 #ifndef G4VOXELIZER_HH 36 #define G4VOXELIZER_HH 37 38 #include <vector> 39 #include <string> 40 #include <map> 41 42 #include "G4Transform3D.hh" 43 #include "G4RotationMatrix.hh" 44 #include "G4SurfBits.hh" 45 #include "G4Box.hh" 46 #include "G4VFacet.hh" 47 #include "G4VSolid.hh" 48 49 struct G4VoxelBox 50 { 51 G4ThreeVector hlen; // half length of the box 52 G4ThreeVector pos; // position of the box 53 }; 54 55 struct G4VoxelInfo 56 { 57 G4int count; 58 G4int previous; 59 G4int next; 60 }; 61 62 class G4Voxelizer 63 { 64 public: 65 66 template <typename T> 67 static inline G4int BinarySearch(const std::vector<T>& vec, T value); 68 69 void Voxelize(std::vector<G4VSolid*>& solids, 70 std::vector<G4Transform3D>& transforms); 71 void Voxelize(std::vector<G4VFacet*>& facets); 72 73 void DisplayVoxelLimits() const; 74 void DisplayBoundaries(); 75 void DisplayListNodes() const; 76 77 G4Voxelizer(); 78 ~G4Voxelizer(); 79 80 void GetCandidatesVoxel(std::vector<G4int>& voxels); 81 // Method displaying the nodes located in a voxel characterized 82 // by its three indexes. 83 84 G4int GetCandidatesVoxelArray(const G4ThreeVector& point, 85 std::vector<G4int>& list, 86 G4SurfBits* crossed = nullptr) const; 87 // Method returning in a vector container the nodes located in a voxel 88 // characterized by its three indexes. 89 G4int GetCandidatesVoxelArray(const std::vector<G4int>& voxels, 90 const G4SurfBits bitmasks[], 91 std::vector<G4int>& list, 92 G4SurfBits* crossed = nullptr) const; 93 G4int GetCandidatesVoxelArray(const std::vector<G4int>& voxels, 94 std::vector<G4int>& list, 95 G4SurfBits* crossed = nullptr)const; 96 97 inline const std::vector<G4VoxelBox>& GetBoxes() const; 98 // Method returning the pointer to the array containing the 99 // characteristics of each box. 100 101 inline const std::vector<G4double>& GetBoundary(G4int index) const; 102 103 G4bool UpdateCurrentVoxel(const G4ThreeVector& point, 104 const G4ThreeVector& direction, 105 std::vector<G4int>& curVoxel) const; 106 107 inline void GetVoxel(std::vector<G4int>& curVoxel, 108 const G4ThreeVector& point) const; 109 110 inline G4int GetBitsPerSlice () const; 111 112 G4bool Contains(const G4ThreeVector& point) const; 113 114 G4double DistanceToNext(const G4ThreeVector& point, 115 const G4ThreeVector& direction, 116 std::vector<G4int>& curVoxel) const; 117 118 G4double DistanceToFirst(const G4ThreeVector& point, 119 const G4ThreeVector& direction) const; 120 121 G4double DistanceToBoundingBox(const G4ThreeVector& point) const; 122 123 inline G4int GetVoxelsIndex(G4int x, G4int y, G4int z) const; 124 inline G4int GetVoxelsIndex(const std::vector<G4int>& voxels) const; 125 inline G4bool GetPointVoxel(const G4ThreeVector& p, 126 std::vector<G4int>& voxels) const; 127 inline G4int GetPointIndex(const G4ThreeVector& p) const; 128 129 inline const G4SurfBits& Empty() const; 130 inline G4bool IsEmpty(G4int index) const; 131 132 void SetMaxVoxels(G4int max); 133 void SetMaxVoxels(const G4ThreeVector& reductionRatio); 134 135 inline G4int GetMaxVoxels(G4ThreeVector& ratioOfReduction); 136 137 G4int AllocatedMemory(); 138 139 inline long long GetCountOfVoxels() const; 140 141 inline long long CountVoxels(std::vector<G4double> boundaries[]) const; 142 143 inline const std::vector<G4int>& 144 GetCandidates(std::vector<G4int>& curVoxel) const; 145 146 inline G4int GetVoxelBoxesSize() const; 147 148 inline const G4VoxelBox &GetVoxelBox(G4int i) const; 149 150 inline const std::vector<G4int>& GetVoxelBoxCandidates(G4int i) const; 151 152 inline G4int GetTotalCandidates() const; 153 154 static G4double MinDistanceToBox (const G4ThreeVector& aPoint, 155 const G4ThreeVector& f); 156 157 static void SetDefaultVoxelsCount(G4int count); 158 159 static G4int GetDefaultVoxelsCount(); 160 161 private: 162 163 class G4VoxelComparator 164 { 165 public: 166 167 std::vector<G4VoxelInfo>& fVoxels; 168 169 G4VoxelComparator(std::vector<G4VoxelInfo>& voxels) : fVoxels(voxels) {} 170 171 G4bool operator()(const G4int& l, const G4int& r) const 172 { 173 G4VoxelInfo &lv = fVoxels[l], &rv = fVoxels[r]; 174 G4int left = lv.count + fVoxels[lv.next].count; 175 G4int right = rv.count + fVoxels[rv.next].count; 176 return (left == right) ? l < r : left < right; 177 } 178 }; 179 180 void BuildEmpty (); 181 182 G4String GetCandidatesAsString(const G4SurfBits& bits) const; 183 184 void CreateSortedBoundary(std::vector<G4double>& boundaryRaw, G4int axis); 185 186 void BuildBoundaries(); 187 188 void BuildReduceVoxels(std::vector<G4double> fBoundaries[], 189 G4ThreeVector reductionRatio); 190 void BuildReduceVoxels2(std::vector<G4double> fBoundaries[], 191 G4ThreeVector reductionRatio); 192 193 void BuildVoxelLimits(std::vector<G4VSolid*>& solids, 194 std::vector<G4Transform3D>& transforms); 195 void BuildVoxelLimits(std::vector<G4VFacet*>& facets); 196 197 void DisplayBoundaries(std::vector<G4double>& fBoundaries); 198 199 void BuildBitmasks(std::vector<G4double> fBoundaries[], 200 G4SurfBits bitmasks[], G4bool countsOnly = false); 201 202 void BuildBoundingBox(); 203 void BuildBoundingBox(G4ThreeVector& amin, G4ThreeVector& amax, 204 G4double tolerance = 0.0); 205 206 void SetReductionRatio(G4int maxVoxels, G4ThreeVector& reductionRatio); 207 208 void CreateMiniVoxels(std::vector<G4double> fBoundaries[], 209 G4SurfBits bitmasks[]); 210 static void FindComponentsFastest(unsigned int mask, 211 std::vector<G4int>& list, G4int i); 212 213 inline G4ThreeVector GetGlobalPoint(const G4Transform3D& trans, 214 const G4ThreeVector& lpoint) const; 215 void TransformLimits(G4ThreeVector& min, G4ThreeVector& max, 216 const G4Transform3D& transformation) const; 217 218 private: 219 220 static G4ThreadLocal G4int fDefaultVoxelsCount; 221 222 std::vector<G4VoxelBox> fVoxelBoxes; 223 std::vector<std::vector<G4int> > fVoxelBoxesCandidates; 224 mutable std::map<G4int, std::vector<G4int> > fCandidates; 225 226 const std::vector<G4int> fNoCandidates; 227 228 long long fCountOfVoxels; 229 230 G4int fNPerSlice; 231 232 std::vector<G4VoxelBox> fBoxes; 233 // Array of box limits on the 3 cartesian axis 234 235 std::vector<G4double> fBoundaries[3]; 236 // Sorted and if need skimmed fBoundaries along X,Y,Z axis 237 238 std::vector<G4int> fCandidatesCounts[3]; 239 240 G4int fTotalCandidates; 241 242 G4SurfBits fBitmasks[3]; 243 244 G4ThreeVector fBoundingBoxCenter; 245 246 G4Box fBoundingBox; 247 248 G4ThreeVector fBoundingBoxSize; 249 250 G4ThreeVector fReductionRatio; 251 252 G4int fMaxVoxels; 253 254 G4double fTolerance; 255 256 G4SurfBits fEmpty; 257 }; 258 259 #include "G4Voxelizer.icc" 260 261 #endif 262