MercuryDPM  Trunk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BasicUnionOfWalls.cc
Go to the documentation of this file.
1 //Copyright (c) 2013-2020, The MercuryDPM Developers Team. All rights reserved.
2 //For the list of developers, see <http://www.MercuryDPM.org/Team>.
3 //
4 //Redistribution and use in source and binary forms, with or without
5 //modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name MercuryDPM nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 //ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 //DISCLAIMED. IN NO EVENT SHALL THE MERCURYDPM DEVELOPERS TEAM BE LIABLE FOR ANY
19 //DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 //(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 //ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 //(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 #include <limits>
27 
28 #include "BasicUnionOfWalls.h"
29 #include "Particles/BaseParticle.h"
31 #include "InteractionHandler.h"
32 #include "WallHandler.h"
33 #include "DPMBase.h"
34 
36 {
37  logger(DEBUG, "BasicUnionOfWalls::BasicUnionOfWalls ) finished");
38 }
39 
46  : BaseWall(b)
47 {
48  for (auto& w : b.walls_)
49  {
50  walls_.push_back(w->copy());
51  }
52  logger(DEBUG, "BasicUnionOfWalls::BasicUnionOfWalls(const BasicUnionOfWalls &p) finished");
53 }
54 
56 {
57  for (auto& w : walls_)
58  {
59  delete w;
60  }
61  logger(DEBUG, "BasicUnionOfWalls::~BasicUnionOfWalls finished");
62 }
63 
68 {
69  return new BasicUnionOfWalls(*this);
70 }
71 
78 {
79  return walls_.size();
80 }
81 
82 
83 /*
84  * \param[in] normal A Vec3D that represents the normal to the wall.
85  * \param[in] point A Vec3D which is a point on the wall.
86  * \details Sets the wall such that for all points x on the wall it holds that
87  * normal*x=normal*point.
88  */
91 {
92  walls_.push_back(wall.copy());
93  walls_.back()->setId(walls_.size());
94 }
95 
103 bool BasicUnionOfWalls::getDistanceAndNormal(const BaseParticle& p, Mdouble& distance, Vec3D& normal) const
104 {
105  if (walls_.empty()) return false;
106 
107  Vec3D position = p.getPosition() - getPosition();
108  getOrientation().rotateBack(position);
109  SphericalParticle shifted;
110  shifted.setSpecies(p.getSpecies());
111  shifted.setPosition(position);
112  shifted.setRadius(p.getRadius());
113 
114  //check wall after wall; the first wall that returns an interaction is chosen
115  for (auto w : walls_) {
116  if (w->getDistanceAndNormal(shifted, distance, normal) == true) {
117  getOrientation().rotate(normal);
118  return true;
119  }
120  }
121 
122  return false;
123 }
124 
128 void BasicUnionOfWalls::read(std::istream& is)
129 {
130  BaseWall::read(is);
131  std::string dummy;
132  unsigned size;
133  is >> dummy >> size;
134  for (unsigned i = 0; i < size; i++)
135  {
136  std::string type;
137  is >> type;
138  BaseWall* wall = getHandler()->createObject(type);
139  wall->setHandler(getHandler());
140  wall->read(is);
141  walls_.push_back(wall);
142  walls_.back()->setId(walls_.size());
143  }
144 }
145 
149 void BasicUnionOfWalls::write(std::ostream& os) const
150 {
151  BaseWall::write(os);
152  os << " numWalls " << walls_.size();
153  for (auto w : walls_)
154  {
155  os << " ";
156  w->write(os);
157  }
158 }
159 
163 std::string BasicUnionOfWalls::getName() const
164 {
165  return "BasicUnionOfWalls";
166 }
167 
168 void BasicUnionOfWalls::getVTK(std::vector<Vec3D>& points, std::vector<std::vector<double>>& triangleStrips)
169 {
170 // for (auto w : walls_) {
171 // w->writeVTK (points, triangleStrips);
172 // }
173  //writes points and strips for all walls; points are added to the global point vector, but the strips are held back
174  std::vector<std::vector<double>> myTriangleStrips;
175  unsigned long n = points.size();
176  for (auto w : walls_)
177  {
178  w->getVTK(points, myTriangleStrips);
179  }
180  //add position of the BasicUnionOfWalls to the point
181  for (std::vector<Vec3D>::iterator p = points.begin() + n; p != points.end(); p++)
182  {
183  getOrientation().rotate(*p);
184  }
185  //create a vector which points are in the wall (actually, only the new points are necessary)
186  std::vector<bool> pointInWall;
187  pointInWall.reserve(points.size());
188  SphericalParticle particle;
189  particle.setSpecies(getSpecies());
190  particle.setRadius(1e-10); //points within that distance are declared part of the wall
191  Mdouble distance;
192  Vec3D normal;
193  for (auto p : points)
194  {
195  particle.setPosition(p);
196  pointInWall.push_back(getDistanceAndNormal(particle, distance, normal));
197  }
198  //now loop through myTriangleStrips to find the strip parts that are fully inside the wall
199  std::vector<double> strip;
200  for (auto t : myTriangleStrips)
201  {
202  for (unsigned i : t)
203  {
204  if (pointInWall[i] == true)
205  {
206  strip.push_back(i);
207  }
208  else
209  {
210  if (strip.size() > 2)
211  triangleStrips.push_back(strip);
212  strip.clear();
213  }
214  }
215  if (strip.size() > 2)
216  triangleStrips.push_back(strip);
217  strip.clear();
218  }
220 }
221 
223 {
224  logger.assert_always(walls_.size() > i, "Index % exceeds number of walls %", i, walls_.size());
225  return walls_[i];
226 }
~BasicUnionOfWalls() override
Default destructor.
std::vector< BaseWall * > walls_
const Vec3D & getPosition() const
Returns the position of this BaseInteractable.
A basic particle.
void rotateBack(Vec3D &position) const
Applies the inverse rotation to a position.
Definition: Quaternion.cc:592
Logger< MERCURY_LOGLEVEL > logger("MercuryKernel")
double Mdouble
Definition: GeneralDefine.h:34
const ParticleSpecies * getSpecies() const
Returns a pointer to the species of this BaseInteractable.
virtual void setRadius(Mdouble radius)
Sets the particle's radius_ (and adjusts the mass_ accordingly, based on the particle's species) ...
void read(std::istream &is) override
Reads BasicUnionOfWalls from a restart file.
std::string getName() const override
Returns the name of the object, in this case the string "BasicUnionOfWalls".
const std::complex< Mdouble > i
Definition: ExtendedMath.h:50
virtual void setHandler(WallHandler *handler)
A function which sets the WallHandler for this BaseWall.
Definition: BaseWall.cc:127
void write(std::ostream &os) const override
Function that writes a BaseWall to an output stream, usually a restart file.
Definition: BaseWall.cc:102
This is a class defining walls.
void setSpecies(const ParticleSpecies *species)
void rotate(Vec3D &position) const
Applies the rotation to a position.
Definition: Quaternion.cc:563
BasicUnionOfWalls()
Default constructor, the normal is infinitely long.
WallHandler * getHandler() const
A function which returns the WallHandler that handles this BaseWall.
Definition: BaseWall.cc:136
static BaseWall * createObject(const std::string &type)
Create a new wall, with the type given as a string (required for restarting).
Definition: WallHandler.cc:122
BaseWall * getObject(unsigned i)
void add(BaseWall &wall)
Defines a standard wall, given an outward normal vector s.t. normal*x=normal*point for all x of the w...
BasicUnionOfWalls * copy() const override
Wall copy method. It calls the copy constructor of this Wall, useful for polymorphism.
void write(std::ostream &os) const override
Writes the BasicUnionOfWalls to an output stream, usually a restart file.
void getVTK(std::vector< Vec3D > &points, std::vector< std::vector< double >> &triangleStrips)
bool getDistanceAndNormal(const BaseParticle &p, Mdouble &distance, Vec3D &normal_return) const override
Compute the distance from the wall for a given BaseParticle and return if there is a collision...
Mdouble getRadius() const
Returns the particle's radius.
Definition: BaseParticle.h:345
Basic class for walls.
Definition: BaseWall.h:47
void read(std::istream &is) override
Function that reads a BaseWall from an input stream, usually a restart file.
Definition: BaseWall.cc:80
void setPosition(const Vec3D &position)
Sets the position of this BaseInteractable.
Definition: Vector.h:49
unsigned long getNumberOfObjects()
Returns the number of objects.
const Quaternion & getOrientation() const
Returns the orientation of this BaseInteractable.
virtual BaseWall * copy() const =0
Pure virtual function that can be overwritten in inherited classes in order to copy a BaseWall...