G4KDTreeResult.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 // $Id: G4KDTreeResult.cc 64057 2012-10-30 15:04:49Z gcosmo $
00027 //
00028 // Author: Mathieu Karamitros (kara (AT) cenbg . in2p3 . fr) 
00029 //
00030 // History:
00031 // -----------
00032 // 10 Oct 2011 M.Karamitros created
00033 //
00034 // -------------------------------------------------------------------
00035 
00036 #include "G4KDTreeResult.hh"
00037 #include "G4KDNode.hh"
00038 #include "G4KDTree.hh"
00039 
00040 using namespace std;
00041 
00042 struct ResNode
00043 {
00044 public:
00045     ResNode():fNode(0),fDistanceSqr(0){;}
00046     ResNode(double distsqr, G4KDNode* node):fNode(node),fDistanceSqr(distsqr){;}
00047     ResNode(const ResNode& right)
00048     {
00049         fNode = right.fNode;
00050         fDistanceSqr= right.fDistanceSqr;
00051     }
00052     ~ResNode(){;}
00053 
00054     bool operator<(const ResNode& right) const
00055     {
00056         return (fDistanceSqr < right.fDistanceSqr);
00057     }
00058 
00059     G4KDNode* GetNode() { return fNode;}
00060     double GetDistanceSqr() { return fDistanceSqr;}
00061 
00062 protected:
00063     G4KDNode* fNode;
00064     double fDistanceSqr;
00065 
00066 private:
00067     ResNode& operator=(const ResNode& rhs)
00068     {
00069         if(this == &rhs) return *this;
00070         return *this;
00071     }
00072 };
00073 
00074 // comparison
00075 bool CompareResNode(const ResNode& left, const ResNode& right)
00076 {
00077     return left < right;
00078 }
00079 
00080 G4KDTreeResult::G4KDTreeResult(G4KDTree* tree) : std::list<ResNode>()
00081 {
00082     fTree = tree;
00083 }
00084 
00085 G4KDTreeResult::~G4KDTreeResult()
00086 {
00087     std::list<ResNode>::erase(begin(),end());
00088 }
00089 
00090 void G4KDTreeResult::Insert(double pos, G4KDNode* node)
00091 {
00092     std::list<ResNode>::push_back(ResNode(pos,node));
00093 }
00094 
00095 void G4KDTreeResult::Clear()
00096 {
00097     std::list<ResNode>::erase(begin(),end());
00098     fIterator = std::list<ResNode>::begin();
00099 }
00100 
00101 void G4KDTreeResult::Sort()
00102 {
00103     std::list<ResNode>::sort(CompareResNode);
00104 }
00105 
00106 size_t G4KDTreeResult::GetSize()
00107 {
00108     return std::list<ResNode>::size();
00109 }
00110 
00111 size_t G4KDTreeResult::size()
00112 {
00113     return std::list<ResNode>::size();
00114 }
00115 
00116 void G4KDTreeResult::Rewind()
00117 {
00118     fIterator = begin();
00119 }
00120 
00121 bool G4KDTreeResult::End()
00122 {
00123     return (fIterator == end());
00124 }
00125 
00126 void G4KDTreeResult::Next()
00127 {
00128     fIterator++;
00129 }
00130 
00131 void* G4KDTreeResult::GetItem(double*& pos)
00132 {
00133     if(!pos)   pos = new double[fTree->GetDim()];
00134     memcpy(pos, (*fIterator).GetNode()->GetPosition(), fTree->GetDim() * sizeof *pos);
00135     return (*fIterator).GetNode()->GetData();
00136 }
00137 
00138 void* G4KDTreeResult::GetItem(double& x, double& y, double& z)
00139 {
00140     x = (*fIterator).GetNode()->GetPosition()[0];
00141     y = (*fIterator).GetNode()->GetPosition()[1];
00142     z = (*fIterator).GetNode()->GetPosition()[2];
00143 
00144     return (*fIterator).GetNode()->GetData();
00145 }
00146 
00147 void* G4KDTreeResult::GetItemNDistanceSQ(double& dist_sq)
00148 {
00149     dist_sq = (*fIterator).GetDistanceSqr();
00150     return (*fIterator).GetNode()->GetData();
00151 }
00152 
00153 void* G4KDTreeResult::GetItemNDistanceSQ(double*& pos, double& dist_sq)
00154 {
00155     dist_sq = (*fIterator).GetDistanceSqr();
00156     return GetItem(pos);
00157 }
00158 
00159 void* G4KDTreeResult::GetItemData()
00160 {
00161     return (*fIterator).GetNode()->GetData();
00162 }
00163 
00164 double G4KDTreeResult::GetDistanceSqr()
00165 {
00166     return (*fIterator).GetDistanceSqr();
00167 }

Generated on Mon May 27 17:48:43 2013 for Geant4 by  doxygen 1.4.7