Commit b2e9cdae authored by Alexandre Halbach's avatar Alexandre Halbach

Solve more than once deform/shift/rotate problem

parent 009fb3d8
......@@ -221,6 +221,15 @@ std::vector<shape> geotools::getshapes(std::vector< std::shared_ptr<rawshape> >
return shapes;
}
std::vector<rawshape*> geotools::getpointers(std::vector< std::shared_ptr<rawshape> > sharedptrs)
{
std::vector<rawshape*> output(sharedptrs.size());
for (int i = 0; i < output.size(); i++)
output[i] = sharedptrs[i].get();
return output;
}
std::vector< std::shared_ptr<rawshape> > geotools::flip(std::vector< std::shared_ptr<rawshape> > input)
{
std::vector< std::shared_ptr<rawshape> > output(input.size());
......@@ -231,6 +240,14 @@ std::vector< std::shared_ptr<rawshape> > geotools::flip(std::vector< std::shared
return output;
}
std::vector<rawshape*> geotools::unique(std::vector<rawshape*> ptrs)
{
std::sort(ptrs.begin(), ptrs.end());
ptrs.erase( std::unique(ptrs.begin(), ptrs.end()), ptrs.end());
return ptrs;
}
std::vector<std::shared_ptr<rawshape>> geotools::duplicate(std::vector<std::shared_ptr<rawshape>> input)
{
std::vector<std::shared_ptr<rawshape>> output(input.size());
......
......@@ -13,6 +13,7 @@
#include <iostream>
#include <numeric>
#include <cmath>
#include <algorithm>
#include <memory>
#include "shape.h"
#include "rawshape.h"
......@@ -49,9 +50,15 @@ namespace geotools
// Transform a vector of rawshapes into a vector of shapes:
std::vector<shape> getshapes(std::vector< std::shared_ptr<rawshape> > rawshapes);
// Transform a vector of rawshape shared pointers to a vector of rawshape pointers:
std::vector<rawshape*> getpointers(std::vector< std::shared_ptr<rawshape> > sharedptrs);
// Flip the rawshape vector direction:
std::vector< std::shared_ptr<rawshape> > flip(std::vector< std::shared_ptr<rawshape> > input);
// Unique a list of rawshape pointers:
std::vector<rawshape*> unique(std::vector<rawshape*> ptrs);
// Duplicate a list of rawshapes:
std::vector<std::shared_ptr<rawshape>> duplicate(std::vector<std::shared_ptr<rawshape>> input);
......
......@@ -2,7 +2,7 @@
#include "geotools.h"
void rawshape::deform(expression xdeform, expression ydeform, expression zdeform)
void rawshape::deform(expression xdeform, expression ydeform, expression zdeform, bool recursively)
{
std::vector<double>* mycoords = getcoords();
......@@ -31,13 +31,16 @@ void rawshape::deform(expression xdeform, expression ydeform, expression zdeform
}
// Also deform the sub shapes:
std::vector<std::shared_ptr<rawshape>> subshapes = getsubshapes();
for (int i = 0; i < subshapes.size(); i++)
subshapes[i]->deform(xdeform, ydeform, zdeform);
// Also deform (only once) every sub shape:
if (recursively)
{
std::vector<rawshape*> subshapes = geotools::unique(geotools::getpointers(getsubshapesrecursively()));
for (int i = 0; i < subshapes.size(); i++)
subshapes[i]->deform(xdeform, ydeform, zdeform, false);
}
}
void rawshape::shift(double shiftx, double shifty, double shiftz)
void rawshape::shift(double shiftx, double shifty, double shiftz, bool recursively)
{
std::vector<double>* mycoords = getcoords();
......@@ -51,13 +54,16 @@ void rawshape::shift(double shiftx, double shifty, double shiftz)
}
// Also shift the sub shapes:
std::vector<std::shared_ptr<rawshape>> subshapes = getsubshapes();
for (int i = 0; i < subshapes.size(); i++)
subshapes[i]->shift(shiftx, shifty, shiftz);
// Also shift (only once) every sub shape:
if (recursively)
{
std::vector<rawshape*> subshapes = geotools::unique(geotools::getpointers(getsubshapesrecursively()));
for (int i = 0; i < subshapes.size(); i++)
subshapes[i]->shift(shiftx, shifty, shiftz, false);
}
}
void rawshape::rotate(double alphax, double alphay, double alphaz)
void rawshape::rotate(double alphax, double alphay, double alphaz, bool recursively)
{
// Get the coordinates of the shape to rotate:
std::vector<double>* mycoords = getcoords();
......@@ -65,10 +71,13 @@ void rawshape::rotate(double alphax, double alphay, double alphaz)
geotools::rotate(alphax, alphay, alphaz, mycoords);
// Also rotate the sub shapes:
std::vector<std::shared_ptr<rawshape>> subshapes = getsubshapes();
for (int i = 0; i < subshapes.size(); i++)
subshapes[i]->rotate(alphax, alphay, alphaz);
// Also rotate (only once) every sub shape:
if (recursively)
{
std::vector<rawshape*> subshapes = geotools::unique(geotools::getpointers(getsubshapesrecursively()));
for (int i = 0; i < subshapes.size(); i++)
subshapes[i]->rotate(alphax, alphay, alphaz, false);
}
}
......@@ -120,6 +129,27 @@ std::vector<std::shared_ptr<rawshape>> rawshape::getsubshapes(void)
abort();
}
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 = {};
for (int i = 0; i < subshapes.size(); i++)
{
output.push_back(subshapes[i]);
std::vector<std::shared_ptr<rawshape>> toappend = subshapes[i]->getsubshapesrecursively();
for (int j = 0; j < toappend.size(); j++)
output.push_back(toappend[j]);
}
return output;
}
int rawshape::getphysicalregion(void)
{
std::cout << "Error in 'rawshape' object: 'getphysicalregion' has not been defined for this shape" << std::endl;
......
......@@ -25,10 +25,10 @@ class rawshape : public std::enable_shared_from_this<rawshape>
public:
virtual void deform(expression xdeform, expression ydeform, expression zdeform);
virtual void deform(expression xdeform, expression ydeform, expression zdeform, bool recursively = true);
virtual void shift(double shiftx, double shifty, double shiftz);
virtual void rotate(double alphax, double alphay, double alphaz);
virtual void shift(double shiftx, double shifty, double shiftz, bool recursively = true);
virtual void rotate(double alphax, double alphay, double alphaz, bool recursively = true);
virtual std::shared_ptr<rawshape> extrude(int physreg, double height, int numlayers);
......@@ -46,8 +46,10 @@ class rawshape : public std::enable_shared_from_this<rawshape>
virtual std::vector<std::shared_ptr<rawshape>> getsons(void);
// Get ALL subshapes (sons are included):
// Get subshapes (sons are included):
virtual std::vector<std::shared_ptr<rawshape>> getsubshapes(void);
// Get all subshapes recursively:
virtual std::vector<std::shared_ptr<rawshape>> getsubshapesrecursively(void);
// Get the mesh info of the shape:
virtual int getphysicalregion(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