2// ********************************************************************
3// * License and Disclaimer *
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. *
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. *
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// ********************************************************************
27// Author: HoangTRAN 16/7/2019
29//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
30#include "G4Molecule.hh"
32template<class T,typename CONTAINER>
33G4ThreadLocal G4OctreeFinder<T,CONTAINER> * G4OctreeFinder<T,CONTAINER>::fInstance = nullptr;
35template<class T, typename CONTAINER>
36G4OctreeFinder<T,CONTAINER> * G4OctreeFinder<T,CONTAINER>::Instance()
40 fInstance = new G4OctreeFinder();
45template<class T,typename CONTAINER>
46G4OctreeFinder<T,CONTAINER>::G4OctreeFinder()
49 , fIsOctreeUsed(false)
50 , fIsOctreeBuit(false)
53//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
55template<class T,typename CONTAINER>
56G4OctreeFinder<T,CONTAINER>::~G4OctreeFinder()
58 typename TreeMap::iterator it;
59 for (it = fTreeMap.begin(); it != fTreeMap.end(); it++)
61 if (it->second == nullptr)
68 fIsOctreeBuit = false;
69 if(fInstance != nullptr)
76//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
78template<class T,typename CONTAINER>
79void G4OctreeFinder<T,CONTAINER>::Clear()
81 typename TreeMap::iterator it;
82 for (it = fTreeMap.begin(); it != fTreeMap.end(); it++)
84 if (it->second == nullptr)
91 fIsOctreeBuit = false;
95template<class T,typename CONTAINER>
96void G4OctreeFinder<T,CONTAINER>::FindNaive(const G4ThreeVector& position,
99 std::vector<CONTAINER::iterator>& result)
101 std::map<int,PriorityList*>& listMap_test = G4ITTrackHolder::Instance()->GetLists();
102 std::map<int,PriorityList*>::iterator it = listMap_test.begin();
103 std::map<int,PriorityList*>::iterator end = listMap_test.end();
105 for (; it!= end; it++)
109 PriorityList* Mollist=it->second;
112 if(Mollist == nullptr || Mollist->GetMainList() == nullptr
113 || Mollist->GetMainList()->empty() )
119 G4TrackList* trackList = Mollist->GetMainList();
120 G4TrackList::iterator __it = trackList->begin();
121 G4TrackList::iterator __end = trackList->end();
122 for (; __it != __end; __it++)
124 G4double r = (position - (*__it)->GetPosition()).mag();
127 result.push_back(__it);
136template<class T,typename CONTAINER>
137void G4OctreeFinder<T,CONTAINER>::FindNearestInRange(const G4Track& track,
140 std::vector<std::pair<
141 typename CONTAINER::iterator,G4double> >&
143 G4bool isSorted) const
145 typename TreeMap::const_iterator it = fTreeMap.find(key);
146 if (it == fTreeMap.end())
150 std::vector<std::pair<typename CONTAINER::iterator,G4double>> tempResult;
151 if(it->second == nullptr)
157 it->second->template radiusNeighbors
158 <std::vector<std::pair<typename CONTAINER::iterator,G4double>>& >(
159 track.GetPosition(), R, tempResult);
163 G4double value(G4IRTUtils::GetRCutOff(0.1 * CLHEP::ns));
165 G4double binOfR(std::pow(G4IRTUtils::GetRCutOff(1000 * CLHEP::ns) / value,
166 1. / static_cast<G4double>(nBin - 1)));
168 if( R <= G4IRTUtils::GetRCutOff(1000 * CLHEP::ns) )
170 if(tempResult.size() < 10 && R < G4IRTUtils::GetRCutOff(1000 * CLHEP::ns))
174 G4cout<<"recurring up R : "<< R<<" tempResult.size() : "<<tempResult.size()<<G4endl;
176 FindNearestInRange(track, key, R, tempResult, isSorted);
182 std::sort(tempResult.begin(),tempResult.end(),fExtractor.compareInterval);
188template<class T,typename CONTAINER>
189void G4OctreeFinder<T,CONTAINER>::FindNearest(const G4Track& track,
192 std::vector<std::pair<
193 typename CONTAINER::iterator,G4double> >&
195 G4bool isSorted) const
197 typename TreeMap::const_iterator it = fTreeMap.find(key);
198 if (it == fTreeMap.end())
202 std::vector<std::pair<typename CONTAINER::iterator,G4double>> tempResult;
203 if(it->second == nullptr)
209 it->second->template radiusNeighbors
210 <std::vector<std::pair<typename CONTAINER::iterator,G4double>>& >(
211 track.GetPosition(), R, tempResult);
215 std::sort(tempResult.begin(),tempResult.end(),fExtractor.compareInterval);
222template<class T,typename CONTAINER>
223void G4OctreeFinder<T,CONTAINER>::FindNearestInRange(const G4ThreeVector& position,
226 std::vector<std::pair<
227 typename CONTAINER::iterator,G4double> >&
229 G4bool isSorted) const
231 typename TreeMap::const_iterator it = fTreeMap.find(key);
232 if (it == fTreeMap.end())
236 std::vector<std::pair<typename CONTAINER::iterator,G4double>> tempResult;
237 if(it->second == nullptr)
243 it->second->template radiusNeighbors
244 <std::vector<std::pair<typename CONTAINER::iterator,G4double>>& >(
245 position, R, tempResult);
249 G4double value(G4IRTUtils::GetRCutOff(0.1 * CLHEP::ns));
250 G4double binOfR(std::pow(G4IRTUtils::GetRCutOff(1000 * CLHEP::ns) / value,
251 1. / static_cast<G4double>(nBin - 1)));
253 if( R <= G4IRTUtils::GetRCutOff(1000 * CLHEP::ns) )
255 if(tempResult.size() < 10 && R < G4IRTUtils::GetRCutOff(1000 * CLHEP::ns))
259 G4cout<<"recurring up R : "<< R<<" tempResult.size() : "<<tempResult.size()<<G4endl;
261 FindNearestInRange(position, key, R, tempResult, isSorted);
268 std::sort(tempResult.begin(),tempResult.end(),fExtractor.compareInterval);
274//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
276template<class T,typename CONTAINER>
277void G4OctreeFinder<T,CONTAINER>::
278FindNearestInRange(const G4ThreeVector& position,
280 std::vector<std::pair<
281 typename CONTAINER::iterator,G4double> >&
283 G4bool isSorted) const
285 typename TreeMap::const_iterator it = fTreeMap.begin();
286 std::vector<std::pair<typename CONTAINER::iterator,G4double>> Result;
288 for(;it != fTreeMap.end(); it++)
290 std::vector<std::pair<typename CONTAINER::iterator,G4double>> tempResult;
291 it->second->template radiusNeighbors
292 <std::vector<std::pair<typename CONTAINER::iterator,G4double>>& >(
293 position, R, tempResult);
294 if(tempResult.empty())
299 Result.insert( Result.end(), tempResult.begin(), tempResult.end() );
304 std::sort(Result.begin(),Result.end(),fExtractor.compareInterval);
309//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
311template<class T,typename CONTAINER>
312void G4OctreeFinder<T,CONTAINER>::BuildTreeMap(
313const std::map<G4int,CONTAINER*>& listMap)
315 auto it = listMap.begin();
316 typename TreeMap::iterator it_Tree;
318 for (; it!= listMap.end(); it++)
321 it_Tree = fTreeMap.find(key);
322 if (it_Tree != fTreeMap.end())
324 if (it_Tree->second == nullptr)
328 it_Tree->second.reset();
330 auto Mollist = it->second;
339 G4cout << "** " << "Create new tree for : " << key << G4endl;
341 if(!Mollist->empty())
343 fTreeMap[key].reset(new Octree(Mollist->begin(),Mollist->end(),fExtractor));
347 G4ExceptionDescription exceptionDescription;
348 exceptionDescription << "should not create new tree for : " << key;
349 G4Exception("G4OCtreeFinder"
350 "::BuildReactionMap()", "BuildReactionMap02",
351 FatalException, exceptionDescription);
356 auto __it = Mollist->begin();
357 auto __end = Mollist->end();
359 for (; __it != __end; __it++)
361 G4cout<< "molecule : "<<(*__it)->GetPosition()<< G4endl;
368 fIsOctreeBuit = true;
371//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
373template<class T,typename CONTAINER>
374void G4OctreeFinder<T,CONTAINER>::SetOctreeUsed(G4bool used)
376 fIsOctreeUsed = used;
379template<class T,typename CONTAINER>
380G4bool G4OctreeFinder<T,CONTAINER>::IsOctreeUsed() const
382 return fIsOctreeUsed;
385template<class T,typename CONTAINER>
386void G4OctreeFinder<T,CONTAINER>::SetOctreeBuilt(G4bool used)
388 fIsOctreeBuit = used;
391template<class T,typename CONTAINER>
392G4bool G4OctreeFinder<T,CONTAINER>::IsOctreeBuilt() const
394 return fIsOctreeBuit;