move BoxTree to headers

This commit is contained in:
Joachim Schöberl 2019-09-21 09:45:29 +02:00
parent d46453050d
commit 6ad36f73cb
3 changed files with 282 additions and 32 deletions

View File

@ -1794,7 +1794,7 @@ namespace netgen
/*
template <int dim, typename T>
T_ADTree<dim,T> :: T_ADTree (Point<dim> acmin, Point<dim> acmax)
// : ela(0)
@ -1805,14 +1805,18 @@ namespace netgen
root = new T_ADTreeNode<dim,T>;
root->sep = (cmin[0] + cmax[0]) / 2;
}
*/
/*
template <int dim, typename T>
T_ADTree<dim,T> :: ~T_ADTree ()
{
root->DeleteChilds();
delete root;
}
*/
/*
template <int dim, typename T>
void T_ADTree<dim,T> :: Insert (Point<dim> p, T pi)
{
@ -1883,7 +1887,9 @@ namespace netgen
node = node->father;
}
}
*/
/*
template <int dim, typename T>
void T_ADTree<dim,T> :: DeleteElement (T pi)
{
@ -1899,7 +1905,9 @@ namespace netgen
node = node->father;
}
}
*/
/*
template <int dim, typename T>
void T_ADTree<dim,T> :: PrintMemInfo (ostream & ost) const
{
@ -1908,8 +1916,9 @@ namespace netgen
<< Elements() * sizeof(T_ADTreeNode<dim,T>) << endl;
ost << "maxind = " << ela.Size() << " = " << sizeof(T_ADTreeNode<dim,T>*) * ela.Size() << " Bytes" << endl;
}
*/
/*
template <int dim, typename T>
class inttn {
public:
@ -1948,19 +1957,18 @@ namespace netgen
found = false;
if (found)
pis.Append (node->pi);
/*
if (node->data[0] > bmax[0] ||
node->data[1] > bmax[1] ||
node->data[2] > bmax[2] ||
node->data[3] < bmin[3] ||
node->data[4] < bmin[4] ||
node->data[5] < bmin[5])
;
else
{
pis.Append (node->pi);
}
*/
// if (node->data[0] > bmax[0] ||
// node->data[1] > bmax[1] ||
// node->data[2] > bmax[2] ||
// node->data[3] < bmin[3] ||
// node->data[4] < bmin[4] ||
// node->data[5] < bmin[5])
// ;
// else
// {
// pis.Append (node->pi);
// }
}
int ndir = (dir+1) % dim;
@ -1979,7 +1987,8 @@ namespace netgen
}
}
}
*/
template <int dim, typename T>
void T_ADTree<dim,T> :: PrintRec (ostream & ost, const T_ADTreeNode<dim,T> * node) const
{
@ -2346,7 +2355,7 @@ namespace netgen
/*
template <int dim, typename T>
BoxTree<dim,T> :: BoxTree (const Box<dim> & abox)
{
@ -2360,7 +2369,9 @@ namespace netgen
}
tree = new T_ADTree<2*dim,T> (tpmin, tpmax);
}
*/
/*
template <int dim, typename T>
BoxTree<dim,T> :: BoxTree (const Point<dim> & apmin, const Point<dim> & apmax)
{
@ -2394,7 +2405,9 @@ namespace netgen
tree->Insert (tp, pi);
}
*/
/*
template <int dim, typename T>
void BoxTree<dim,T> ::GetIntersecting (const Point<dim> & pmin, const Point<dim> & pmax,
NgArray<T> & pis) const
@ -2412,8 +2425,9 @@ namespace netgen
tree->GetIntersecting (tpmin, tpmax, pis);
}
*/
/*
template<> BlockAllocator T_ADTreeNode<4,INDEX> :: ball(sizeof (T_ADTreeNode<4,INDEX>));
template class T_ADTree<4,INDEX>;
template class BoxTree<2,INDEX>;
@ -2426,4 +2440,6 @@ namespace netgen
template<> BlockAllocator T_ADTreeNode<6,INDEX> :: ball(sizeof (T_ADTreeNode<6,INDEX>));
template class T_ADTree<6,INDEX>;
template class BoxTree<3,INDEX>;
*/
}

View File

@ -422,6 +422,9 @@ public:
// friend class T_ADTree<DIM>;
/*
// slow without blockallocator !!!!!
static BlockAllocator ball;
void * operator new(size_t)
{
@ -432,8 +435,11 @@ public:
{
ball.Free(p);
}
*/
};
// template <int DIM, typename T>
// BlockAllocator T_ADTreeNode<DIM,T> :: ball(sizeof (T_ADTreeNode<DIM,T>));
@ -446,14 +452,174 @@ public:
// NgArray<T_ADTreeNode<dim>*> ela;
ClosedHashTable<T, T_ADTreeNode<dim,T>*> ela;
public:
T_ADTree (Point<dim> acmin, Point<dim> acmax);
~T_ADTree ();
T_ADTree (Point<dim> acmin, Point<dim> acmax)
{
cmin = acmin;
cmax = acmax;
root = new T_ADTreeNode<dim,T>;
root->sep = (cmin[0] + cmax[0]) / 2;
}
~T_ADTree ()
{
root->DeleteChilds();
delete root;
}
void Insert (Point<dim> p, T pi)
{
T_ADTreeNode<dim,T> *node(NULL);
T_ADTreeNode<dim,T> *next;
int dir;
int lr(0);
Point<dim> bmin = cmin;
Point<dim> bmax = cmax;
next = root;
dir = 0;
while (next)
{
node = next;
if (IsInvalid(node->pi))
{
// memcpy (node->data, p, dim * sizeof(float));
node->data = p;
node->pi = pi;
// if (ela.Size() < pi+1)
// ela.SetSize (pi+1);
ela[pi] = node;
return;
}
if (node->sep > p[dir])
{
next = node->left;
bmax(dir) = node->sep;
lr = 0;
}
else
{
next = node->right;
bmin(dir) = node->sep;
lr = 1;
}
dir++;
if (dir == dim) dir = 0;
}
next = new T_ADTreeNode<dim,T>;
// memcpy (next->data, p, dim * sizeof(float));
next->data = p;
next->pi = pi;
next->sep = (bmin[dir] + bmax[dir]) / 2;
// if (ela.Size() < pi+1)
// ela.SetSize (pi+1);
ela[pi] = next;
if (lr)
node->right = next;
else
node->left = next;
next -> father = node;
while (node)
{
node->nchilds++;
node = node->father;
}
}
class inttn {
public:
int dir;
T_ADTreeNode<dim,T> * node;
};
void Insert (Point<dim> p, T pi);
void GetIntersecting (Point<dim> bmin, Point<dim> bmax,
NgArray<T> & pis) const;
NgArray<T> & pis) const
{
NgArrayMem<inttn,10000> stack(10000);
pis.SetSize(0);
stack[0].node = root;
stack[0].dir = 0;
int stacks = 0;
while (stacks >= 0)
{
T_ADTreeNode<dim,T> * node = stack[stacks].node;
int dir = stack[stacks].dir;
stacks--;
if (!IsInvalid(node->pi)) // != -1)
{
bool found = true;
for (int i = 0; i < dim/2; i++)
if (node->data[i] > bmax[i])
found = false;
for (int i = dim/2; i < dim; i++)
if (node->data[i] < bmin[i])
found = false;
if (found)
pis.Append (node->pi);
/*
if (node->data[0] > bmax[0] ||
node->data[1] > bmax[1] ||
node->data[2] > bmax[2] ||
node->data[3] < bmin[3] ||
node->data[4] < bmin[4] ||
node->data[5] < bmin[5])
;
else
{
pis.Append (node->pi);
}
*/
}
int ndir = (dir+1) % dim;
if (node->left && bmin[dir] <= node->sep)
{
stacks++;
stack[stacks].node = node->left;
stack[stacks].dir = ndir;
}
if (node->right && bmax[dir] >= node->sep)
{
stacks++;
stack[stacks].node = node->right;
stack[stacks].dir = ndir;
}
}
}
void DeleteElement (T pi)
{
T_ADTreeNode<dim,T> * node = ela[pi];
ela.Delete(pi);
SetInvalid(node->pi); // = -1;
node = node->father;
while (node)
{
node->nchilds--;
node = node->father;
}
}
void DeleteElement (T pi);
void Print (ostream & ost) const
{ PrintRec (ost, root); }
@ -466,7 +632,14 @@ public:
int DepthRec (const T_ADTreeNode<dim,T> * node) const;
int ElementsRec (const T_ADTreeNode<dim,T> * node) const;
void PrintMemInfo (ostream & ost) const;
void PrintMemInfo (ostream & ost) const
{
ost << Elements() << " elements a " << sizeof(ADTreeNode6)
<< " Bytes = "
<< Elements() * sizeof(T_ADTreeNode<dim,T>) << endl;
ost << "maxind = " << ela.Size() << " = " << sizeof(T_ADTreeNode<dim,T>*) * ela.Size() << " Bytes" << endl;
}
};
@ -558,22 +731,82 @@ public:
T_ADTree<2*dim,T> * tree;
Point<dim> boxpmin, boxpmax;
public:
BoxTree (const Box<dim> & abox);
BoxTree (const Point<dim> & apmin, const Point<dim> & apmax);
~BoxTree ();
void Insert (const Point<dim> & bmin, const Point<dim> & bmax, T pi);
BoxTree (const Box<dim> & abox)
{
boxpmin = abox.PMin();
boxpmax = abox.PMax();
Point<2*dim> tpmin, tpmax;
for (int i = 0; i < dim; i++)
{
tpmin(i) = tpmin(i+dim) = boxpmin(i);
tpmax(i) = tpmax(i+dim) = boxpmax(i);
}
tree = new T_ADTree<2*dim,T> (tpmin, tpmax);
}
BoxTree (const Point<dim> & apmin, const Point<dim> & apmax)
{
boxpmin = apmin;
boxpmax = apmax;
Point<2*dim> tpmin, tpmax;
for (int i = 0; i < dim; i++)
{
tpmin(i) = tpmin(i+dim) = boxpmin(i);
tpmax(i) = tpmax(i+dim) = boxpmax(i);
}
tree = new T_ADTree<2*dim,T> (tpmin, tpmax);
}
~BoxTree ()
{
delete tree;
}
void Insert (const Point<dim> & bmin, const Point<dim> & bmax, T pi)
{
Point<2*dim> tp;
for (size_t i = 0; i < dim; i++)
{
tp(i) = bmin(i);
tp(i+dim) = bmax(i);
}
tree->Insert (tp, pi);
}
void Insert (const Box<dim> & box, T pi)
{
Insert (box.PMin(), box.PMax(), pi);
}
void DeleteElement (T pi)
{ tree->DeleteElement(pi); }
{
tree->DeleteElement(pi);
}
void GetIntersecting (const Point<dim> & pmin, const Point<dim> & pmax,
NgArray<T> & pis) const;
NgArray<T> & pis) const
{
Point<2*dim> tpmin, tpmax;
double tol = Tolerance();
for (size_t i = 0; i < dim; i++)
{
tpmin(i) = boxpmin(i);
tpmax(i) = pmax(i)+tol;
tpmin(i+dim) = pmin(i)-tol;
tpmax(i+dim) = boxpmax(i);
}
tree->GetIntersecting (tpmin, tpmax, pis);
}
double Tolerance() const { return 1e-7 * Dist(boxpmax, boxpmin); } // single precision
const auto & Tree() const { return *tree; };
auto & Tree() { return *tree; };
};
};
}

View File

@ -53,6 +53,7 @@ public:
};
inline void SetInvalid (STLTrigId & id) { id = 0; }
}