Commit 98d8ceb2 authored by Alexandre Halbach's avatar Alexandre Halbach

Duplicates keeps pointer equalitiy links

parent 969b9e07
......@@ -280,6 +280,26 @@ std::vector<std::shared_ptr<rawshape>> geotools::concatenate(std::vector<std::ve
return output;
}
void geotools::sortrawshapepointers(std::vector<rawshape*>& tosort, std::vector<int>& reorderingvector)
{
if (reorderingvector.size() != tosort.size())
reorderingvector.resize(tosort.size());
// Set 'reorderingvector' to [0 1 2 ...]:
std::iota(reorderingvector.begin(), reorderingvector.end(), 0);
// Sort 'reorderingvector' according to 'tosort':
// The < operator is overloaded by a lambda function.
std::sort(reorderingvector.begin(), reorderingvector.end(), [&](int elem1, int elem2)
{
if (tosort[elem1] < tosort[elem2])
return true;
if (tosort[elem1] > tosort[elem2])
return false;
// For identical entries make a COHERENT decision for a stable sorting.
return elem1 < elem2;
});
}
std::vector<double> geotools::appendcoords(std::vector<std::shared_ptr<rawshape>> rawshapes)
{
std::vector< std::vector<double>* > coordsptrs(rawshapes.size());
......
......@@ -65,6 +65,9 @@ namespace geotools
// Concatenate lists of rawshapes:
std::vector<std::shared_ptr<rawshape>> concatenate(std::vector<std::vector<std::shared_ptr<rawshape>>> input);
// Get an int vector to sort a vector of rawshape pointers:
void sortrawshapepointers(std::vector<rawshape*>& tosort, std::vector<int>& reorderingvector);
// Append the coordinate of multiple rawshapes:
std::vector<double> appendcoords(std::vector<std::shared_ptr<rawshape>> rawshapes);
// Append the elements of multiple rawshapes:
......
......@@ -39,6 +39,8 @@ std::shared_ptr<rawshape> rawarc::duplicate(void)
out->sons = geotools::duplicate(sons);
out->mycenterpoint = geotools::duplicate({mycenterpoint})[0];
out->replicatelinks(shared_from_this());
return out;
}
......@@ -75,6 +77,13 @@ std::vector<std::shared_ptr<rawshape>> rawarc::getsubshapes(void)
return geotools::concatenate({sons,{mycenterpoint}});
}
void rawarc::setsubshapes(std::vector<std::shared_ptr<rawshape>> subshapes)
{
mycenterpoint = subshapes[subshapes.size()-1];
subshapes.pop_back();
sons = subshapes;
}
int rawarc::getphysicalregion(void)
{
return myphysicalregion;
......
......@@ -60,8 +60,9 @@ class rawarc: public rawshape
std::vector<std::shared_ptr<rawshape>> getsons(void);
// Get ALL subshapes (sons are included):
// Get subshapes (sons are included):
std::vector<std::shared_ptr<rawshape>> getsubshapes(void);
void setsubshapes(std::vector<std::shared_ptr<rawshape>> subshapes);
// Get the mesh info of the shape:
int getphysicalregion(void);
......
......@@ -48,6 +48,8 @@ std::shared_ptr<rawshape> rawdisk::duplicate(void)
out->sons = geotools::duplicate(sons);
out->mycenterpoint = mycenterpoint->duplicate();
out->replicatelinks(shared_from_this());
return out;
}
......@@ -77,6 +79,13 @@ std::vector<std::shared_ptr<rawshape>> rawdisk::getsubshapes(void)
return geotools::concatenate({sons,{mycenterpoint}});
}
void rawdisk::setsubshapes(std::vector<std::shared_ptr<rawshape>> subshapes)
{
mycenterpoint = subshapes[subshapes.size()-1];
subshapes.pop_back();
sons = subshapes;
}
int rawdisk::getphysicalregion(void)
{
return myphysicalregion;
......
......@@ -62,8 +62,9 @@ class rawdisk: public rawshape
std::vector<std::shared_ptr<rawshape>> getsons(void);
// Get ALL subshapes (sons are included):
// Get subshapes (sons are included):
std::vector<std::shared_ptr<rawshape>> getsubshapes(void);
void setsubshapes(std::vector<std::shared_ptr<rawshape>> subshapes);
// Get the mesh info of the shape:
int getphysicalregion(void);
......
......@@ -24,6 +24,8 @@ std::shared_ptr<rawshape> rawextrusion::duplicate(void)
out->sons = geotools::duplicate(sons);
out->mybaseshape = mybaseshape->duplicate();
out->replicatelinks(shared_from_this());
return out;
}
......@@ -53,6 +55,11 @@ std::vector<std::shared_ptr<rawshape>> rawextrusion::getsubshapes(void)
return sons;
}
void rawextrusion::setsubshapes(std::vector<std::shared_ptr<rawshape>> subshapes)
{
sons = subshapes;
}
int rawextrusion::getphysicalregion(void)
{
return myphysicalregion;
......
......@@ -61,9 +61,10 @@ class rawextrusion: public rawshape
std::vector<std::shared_ptr<rawshape>> getsons(void);
// Get ALL subshapes (sons are included):
// Get subshapes (sons are included):
std::vector<std::shared_ptr<rawshape>> getsubshapes(void);
void setsubshapes(std::vector<std::shared_ptr<rawshape>> subshapes);
// Get the mesh info of the shape:
int getphysicalregion(void);
std::vector<double>* getcoords(void);
......
......@@ -67,6 +67,8 @@ std::shared_ptr<rawshape> rawline::duplicate(void)
out->sons = geotools::duplicate(sons);
out->replicatelinks(shared_from_this());
return out;
}
......@@ -103,6 +105,11 @@ std::vector<std::shared_ptr<rawshape>> rawline::getsubshapes(void)
return sons;
}
void rawline::setsubshapes(std::vector<std::shared_ptr<rawshape>> subshapes)
{
sons = subshapes;
}
int rawline::getphysicalregion(void)
{
return myphysicalregion;
......
......@@ -59,9 +59,10 @@ class rawline: public rawshape
std::vector<std::shared_ptr<rawshape>> getsons(void);
// Get ALL subshapes (sons are included):
// Get subshapes (sons are included):
std::vector<std::shared_ptr<rawshape>> getsubshapes(void);
void setsubshapes(std::vector<std::shared_ptr<rawshape>> subshapes);
// Get the mesh info of the shape:
int getphysicalregion(void);
std::vector<double>* getcoords(void);
......
......@@ -29,6 +29,8 @@ std::shared_ptr<rawshape> rawpoint::duplicate(void)
out->sons = geotools::duplicate(sons);
out->replicatelinks(shared_from_this());
return out;
}
......@@ -58,6 +60,11 @@ std::vector<std::shared_ptr<rawshape>> rawpoint::getsubshapes(void)
return sons;
}
void rawpoint::setsubshapes(std::vector<std::shared_ptr<rawshape>> subshapes)
{
sons = subshapes;
}
int rawpoint::getphysicalregion(void)
{
return myphysicalregion;
......
......@@ -50,9 +50,10 @@ class rawpoint: public rawshape
std::vector<std::shared_ptr<rawshape>> getsons(void);
// Get ALL subshapes (sons are included):
// Get subshapes (sons are included):
std::vector<std::shared_ptr<rawshape>> getsubshapes(void);
void setsubshapes(std::vector<std::shared_ptr<rawshape>> subshapes);
// Get the mesh info of the shape:
int getphysicalregion(void);
std::vector<double>* getcoords(void);
......
......@@ -57,6 +57,8 @@ std::shared_ptr<rawshape> rawquadrangle::duplicate(void)
out->sons = geotools::duplicate(sons);
out->replicatelinks(shared_from_this());
return out;
}
......@@ -86,6 +88,11 @@ std::vector<std::shared_ptr<rawshape>> rawquadrangle::getsubshapes(void)
return sons;
}
void rawquadrangle::setsubshapes(std::vector<std::shared_ptr<rawshape>> subshapes)
{
sons = subshapes;
}
int rawquadrangle::getphysicalregion(void)
{
return myphysicalregion;
......
......@@ -56,9 +56,10 @@ class rawquadrangle: public rawshape
std::vector<std::shared_ptr<rawshape>> getsons(void);
// Get ALL subshapes (sons are included):
// Get subshapes (sons are included):
std::vector<std::shared_ptr<rawshape>> getsubshapes(void);
void setsubshapes(std::vector<std::shared_ptr<rawshape>> subshapes);
// Get the mesh info of the shape:
int getphysicalregion(void);
std::vector<double>* getcoords(void);
......
......@@ -131,25 +131,100 @@ std::vector<std::shared_ptr<rawshape>> rawshape::getsubshapes(void)
std::vector<std::shared_ptr<rawshape>> rawshape::getsubshapesrecursively(void)
{
std::vector<std::shared_ptr<rawshape>> subshapes = getsubshapes();
if (subshapes.size() == 0)
return subshapes;
std::vector<std::shared_ptr<rawshape>> output(countsubshapesrecursively());
std::vector<std::shared_ptr<rawshape>> output = {};
std::vector<std::shared_ptr<rawshape>> subshapes = getsubshapes();
int index = 0;
for (int i = 0; i < subshapes.size(); i++)
{
output.push_back(subshapes[i]);
output[index] = subshapes[i];
index++;
std::vector<std::shared_ptr<rawshape>> toappend = subshapes[i]->getsubshapesrecursively();
for (int j = 0; j < toappend.size(); j++)
output.push_back(toappend[j]);
output[index+j] = toappend[j];
index += toappend.size();
}
return output;
}
void rawshape::setsubshapes(std::vector<std::shared_ptr<rawshape>> subshapes)
{
std::cout << "Error in 'rawshape' object: 'setsubshapes' has not been defined for this shape" << std::endl;
abort();
}
void rawshape::setsubshapesrecursively(std::vector<std::shared_ptr<rawshape>> subshapes)
{
// Prepare to store the direct subshapes of this raw shape:
int numberofdirectsubshapes = (getsubshapes()).size();
std::vector<std::shared_ptr<rawshape>> directsubshapes(numberofdirectsubshapes);
int subshapeindex = 0;
for (int n = 0; n < numberofdirectsubshapes; n++)
{
directsubshapes[n] = subshapes[subshapeindex];
subshapeindex++;
// Count the number of subshapes recursively in the current direct subshape:
int numberoflowerlevelsubshapes = directsubshapes[n]->countsubshapesrecursively();
// Get all subshapes of lower levels:
std::vector<std::shared_ptr<rawshape>> lowerlevelsubshapes(numberoflowerlevelsubshapes);
for (int i = 0; i < numberoflowerlevelsubshapes; i++)
lowerlevelsubshapes[i] = subshapes[subshapeindex+i];
directsubshapes[n]->setsubshapesrecursively(lowerlevelsubshapes);
subshapeindex += numberoflowerlevelsubshapes;
}
setsubshapes(directsubshapes);
}
int rawshape::countsubshapesrecursively(void)
{
std::vector<std::shared_ptr<rawshape>> subshapes = getsubshapes();
int numsubshapes = subshapes.size();
for (int i = 0; i < subshapes.size(); i++)
numsubshapes += subshapes[i]->countsubshapesrecursively();
return numsubshapes;
}
void rawshape::replicatelinks(std::shared_ptr<rawshape> origin)
{
// Get the rawshape pointers of all subshapes in the origin:
std::vector<rawshape*> originsubshapes = geotools::getpointers(origin->getsubshapesrecursively());
if (originsubshapes.size() <= 1)
return;
// Get all subshapes in this rawshape:
std::vector<std::shared_ptr<rawshape>> subshapes = this->getsubshapesrecursively();
// Copy the equality relations from the origin to 'subshapes':
std::vector<int> reorderingvector;
geotools::sortrawshapepointers(originsubshapes, reorderingvector);
std::vector<std::shared_ptr<rawshape>> linkedsubshapes(reorderingvector.size());
linkedsubshapes[reorderingvector[0]] = subshapes[reorderingvector[0]];
for (int i = 1; i < reorderingvector.size(); i++)
{
if (originsubshapes[reorderingvector[i]] != originsubshapes[reorderingvector[i-1]])
linkedsubshapes[reorderingvector[i]] = subshapes[reorderingvector[i]];
else
linkedsubshapes[reorderingvector[i]] = linkedsubshapes[reorderingvector[i-1]];
}
// Transfer linked subshapes to this rawshape:
setsubshapesrecursively(linkedsubshapes);
}
int rawshape::getphysicalregion(void)
{
std::cout << "Error in 'rawshape' object: 'getphysicalregion' has not been defined for this shape" << std::endl;
......
......@@ -51,6 +51,14 @@ class rawshape : public std::enable_shared_from_this<rawshape>
// Get all subshapes recursively:
virtual std::vector<std::shared_ptr<rawshape>> getsubshapesrecursively(void);
virtual void setsubshapes(std::vector<std::shared_ptr<rawshape>> subshapes);
virtual void setsubshapesrecursively(std::vector<std::shared_ptr<rawshape>> subshapes);
virtual int countsubshapesrecursively(void);
// With this the pointer equalities between subshapes in the origin are replicated in this rawshape:
void replicatelinks(std::shared_ptr<rawshape> origin);
// Get the mesh info of the shape:
virtual int getphysicalregion(void);
virtual std::vector<double>* getcoords(void);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment