Garfield++ 4.0
A toolkit for the detailed simulation of particle detectors based on ionisation measurement in gases and semiconductors
Loading...
Searching...
No Matches
Garfield::QuadTree Class Reference

Quadtree search. More...

#include <QuadTree.hh>

Public Member Functions

 QuadTree (const double x0, const double y0, const double hx, const double hy)
 Constructor.
 
 ~QuadTree ()
 Destructor.
 
void InsertMeshNode (const double x, const double y, const int index)
 Insert a mesh node (a vertex/point) to the tree.
 
void InsertMeshElement (const double bb[4], const int index)
 Insert a mesh element with given bounding box and index to the tree.
 
std::vector< int > GetElementsInBlock (const double x, const double y) const
 Get all elements linked to a block corresponding to the given point.
 

Detailed Description

Quadtree search.

Definition at line 12 of file QuadTree.hh.

Constructor & Destructor Documentation

◆ QuadTree()

Garfield::QuadTree::QuadTree ( const double  x0,
const double  y0,
const double  hx,
const double  hy 
)

Constructor.

Definition at line 6 of file QuadTree.cc.

8 : m_x0(x0), m_y0(y0), m_hx(hx), m_hy(hy) {
9 m_xmin = x0 - hx;
10 m_ymin = y0 - hy;
11 m_xmax = x0 + hx;
12 m_ymax = y0 + hy;
13
14 // Initially, there are no children.
15 for (int i = 0; i < 4; ++i) children[i] = nullptr;
16}

◆ ~QuadTree()

Garfield::QuadTree::~QuadTree ( )

Destructor.

Definition at line 18 of file QuadTree.cc.

18 {
19 for (int i = 0; i < 4; ++i) delete children[i];
20}

Member Function Documentation

◆ GetElementsInBlock()

std::vector< int > Garfield::QuadTree::GetElementsInBlock ( const double  x,
const double  y 
) const

Get all elements linked to a block corresponding to the given point.

Definition at line 92 of file QuadTree.cc.

93 {
94 const auto node = GetBlockFromPoint(x, y);
95 if (node) return node->elements;
96 return std::vector<int>();
97}

◆ InsertMeshElement()

void Garfield::QuadTree::InsertMeshElement ( const double  bb[4],
const int  index 
)

Insert a mesh element with given bounding box and index to the tree.

Definition at line 79 of file QuadTree.cc.

79 {
80 if (IsLeafNode()) {
81 // Add the element to the list of this quadrant.
82 elements.push_back(index);
83 return;
84 }
85 // Check which children overlap with the element's bounding box.
86 for (int i = 0; i < 4; ++i) {
87 if (!children[i]->DoesBoxOverlap(bb)) continue;
88 children[i]->InsertMeshElement(bb, index);
89 }
90}
void InsertMeshElement(const double bb[4], const int index)
Insert a mesh element with given bounding box and index to the tree.
Definition: QuadTree.cc:79

Referenced by InsertMeshElement().

◆ InsertMeshNode()

void Garfield::QuadTree::InsertMeshNode ( const double  x,
const double  y,
const int  index 
)

Insert a mesh node (a vertex/point) to the tree.

Definition at line 41 of file QuadTree.cc.

41 {
42 // Check if it is a leaf node.
43 if (!IsLeafNode()) {
44 // We are at an interior node.
45 // Insert recursively into appropriate child quadrant.
46 int quad = GetQuadrant(x, y);
47 children[quad]->InsertMeshNode(x, y, index);
48 return;
49 }
50
51 // Add the new point if the block is not full.
52 if (nodes.size() < BlockCapacity) {
53 nodes.push_back(std::make_tuple(x, y, index));
54 return;
55 }
56 // Block is full, so we need to partition it.
57 // Split the current node and create new empty trees for each child.
58 for (int i = 0; i < 4; ++i) {
59 // Compute new bounding box for this child.
60 const double xi = m_x0 + m_hx * (i & 2 ? 0.5 : -0.5);
61 const double yi = m_y0 + m_hy * (i & 1 ? 0.5 : -0.5);
62 children[i] = new QuadTree(xi, yi, 0.5 * m_hx, 0.5 * m_hy);
63 }
64
65 // Move the mesh nodes from the partitioned node (now marked as interior) to
66 // its children.
67 while (!nodes.empty()) {
68 auto node = nodes.back();
69 nodes.pop_back();
70 const double xn = std::get<0>(node);
71 const double yn = std::get<1>(node);
72 const int quad = GetQuadrant(xn, yn);
73 children[quad]->InsertMeshNode(xn, yn, std::get<2>(node));
74 }
75 // Insert the new point in the appropriate octant.
76 children[GetQuadrant(x, y)]->InsertMeshNode(x, y, index);
77}
QuadTree(const double x0, const double y0, const double hx, const double hy)
Constructor.
Definition: QuadTree.cc:6
void InsertMeshNode(const double x, const double y, const int index)
Insert a mesh node (a vertex/point) to the tree.
Definition: QuadTree.cc:41

Referenced by InsertMeshNode().


The documentation for this class was generated from the following files: