MercuryDPM  Trunk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RestrictedWall.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 "RestrictedWall.h"
29 #include "Particles/BaseParticle.h"
30 #include "InteractionHandler.h"
31 #include "WallHandler.h"
32 #include "DPMBase.h"
33 
35 {
36  wall_ = nullptr;
37  restriction_ = nullptr;
38  logger(DEBUG, "RestrictedWall::RestrictedWall ) finished");
39 }
40 
47  : BaseWall(w)
48 {
49  wall_ = nullptr;
50  restriction_ = nullptr;
51  set(w.wall_, w.restriction_);
52  logger(DEBUG, "RestrictedWall::RestrictedWall(const RestrictedWall &p) finished");
53 }
54 
56 {
57  wall_ = nullptr;
58  restriction_ = nullptr;
59  set(wall, restriction);
60 }
61 
63 {
64  if (wall_ != nullptr)
65  {
66  delete wall_;
67  wall_ = nullptr;
68  }
69  if (restriction_ != nullptr)
70  {
71  delete restriction_;
72  restriction_ = nullptr;
73  }
74  logger(DEBUG, "RestrictedWall::~RestrictedWall finished");
75 }
76 
81 {
82  return new RestrictedWall(*this);
83 }
84 
85 /*
86  * \param[in] normal A Vec3D that represents the normal to the wall.
87  * \param[in] point A Vec3D which is a point on the wall.
88  * \details Sets the wall such that for all points x on the wall it holds that
89  * normal*x=normal*point.
90  */
92 void RestrictedWall::set(BaseWall* wall, InfiniteWall* restriction)
93 {
94  if (wall_ != nullptr)
95  {
96  delete wall_;
97  wall_ = nullptr;
98  }
99  wall_ = wall->copy();
101 
102  if (restriction_ != nullptr)
103  {
104  delete restriction_;
105  restriction_ = nullptr;
106  }
107  restriction_ = restriction->copy();
108 
109  // std::cout << *this << std::endl;
110 }
111 
124 bool RestrictedWall::getDistanceAndNormal(const BaseParticle& p, Mdouble& distance, Vec3D& normal_return) const
125 {
127  return wall_->getDistanceAndNormal(p, distance, normal_return);
128  else
129  return false;
130 }
131 
135 void RestrictedWall::read(std::istream& is)
136 {
137  wall_->read(is);
138  restriction_->read(is);
139 }
140 
144 void RestrictedWall::write(std::ostream& os) const
145 {
146  //todo check
147  os << getName() << ' ';
148  wall_->write(os);
149  os << ' ';
150  restriction_->write(os);
151 }
152 
156 std::string RestrictedWall::getName() const
157 {
158  return "RestrictedWall";
159 }
160 
170 RestrictedWall::getInteractionWith(BaseParticle* p, unsigned timeStamp, InteractionHandler* interactionHandler)
171 {
173  {
175  wall_->setIndex(getIndex());
176  return wall_->getInteractionWith(p, timeStamp, interactionHandler);
177  }
178  else
179  {
180  return nullptr;
181  }
182 }
183 
185 {
186  const size_t size0 = vtk.triangleStrips.size();
187  //write full BaseWall
188  wall_->writeVTK(vtk);
189 
190  //copy out the new triangle strips, so we can modify them
191  std::vector<std::vector<double>> myTriangleStrips;
192  while (vtk.triangleStrips.size() > size0)
193  {
194  myTriangleStrips.push_back(vtk.triangleStrips.back());
195  vtk.triangleStrips.pop_back();
196  }
197 
198  //now remove points that are in restricted area
199  for (const std::vector<double>& myTriangleStrip: myTriangleStrips)
200  {
201  unsigned counter = 0;
202  std::vector<double> cell;
203  //for each triangle
204  for (const double c: myTriangleStrip)
205  {
206  Mdouble distance = restriction_->getDistance(vtk.points[c]);
207  if (distance < 0)
208  {
209  //if the current point is inside the restricted volume, write it
210  cell.push_back(c);
211  counter = 0;
212  }
213  else
214  {
215  if (counter >= 2)
216  {
217  //if the current point is not in restriction, don't write it and flush the cell into the cells array
218  if (cell.size() > 2)
219  vtk.triangleStrips.push_back(cell);
220  cell.clear();
221  counter = 0;
222  }
223  vtk.points[c] += distance * restriction_->getNormal();
224  cell.push_back(c);
225  counter++;
226  }
227  }
228  if (cell.size() > 2)
229  {
230  //if the current point is not in restriction, don't write it and flush the cell into the cells array
231  vtk.triangleStrips.push_back(cell);
232  cell.clear();
233  }
234  }
235 }
void setIndex(unsigned int index)
Allows one to assign an index to an object in the handler/container.
Definition: BaseObject.cc:64
unsigned int getIndex() const
Returns the index of the object in the handler.
Definition: BaseObject.h:118
const Vec3D & getPosition() const
Returns the position of this BaseInteractable.
Logger< MERCURY_LOGLEVEL > logger("MercuryKernel")
virtual void writeVTK(VTKContainer &vtk) const
Definition: BaseWall.cc:466
~RestrictedWall() override
Default destructor.
double Mdouble
Definition: GeneralDefine.h:34
const ParticleSpecies * getSpecies() const
Returns a pointer to the species of this BaseInteractable.
Vec3D getNormal() const
Access function for normal.
void write(std::ostream &os) const override
Function that writes a BaseWall to an output stream, usually a restart file.
Definition: BaseWall.cc:102
BaseInteraction * getInteractionWith(BaseParticle *p, unsigned timeStamp, InteractionHandler *interactionHandler) override
Returns the interaction between this wall and a given particle, nullptr if there is no interaction...
Definition: BaseWall.cc:369
This is a class defining walls.
Stores information about interactions between two interactable objects; often particles but could be ...
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...
void read(std::istream &is) override
Reads RestrictedWall from a restart file.
Mdouble getWallInteractionRadius(const BaseWall *wall) const
returns the radius plus the interactionDistance
Definition: BaseParticle.h:383
void write(std::ostream &os) const override
Writes the RestrictedWall to an output stream, usually a restart file.
virtual bool getDistanceAndNormal(const BaseParticle &P, Mdouble &distance, Vec3D &normal_return) const =0
Pure virtual function that computes the distance of a BaseParticle to this wall and returns the norma...
std::vector< std::vector< double > > triangleStrips
Definition: BaseWall.h:39
Container to store Interaction objects.
void set(BaseWall *wall, InfiniteWall *restriction)
Defines a standard wall, given an outward normal vector s.t. normal*x=normal*point for all x of the w...
RestrictedWall * copy() const override
Wall copy method. It calls the copy constructor of this Wall, useful for polymorphism.
InfiniteWall * copy() const override
Wall copy method. It calls the copy constructor of this Wall, useful for polymorphism.
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
BaseWall * wall_
std::vector< Vec3D > points
Definition: BaseWall.h:38
void read(std::istream &is) override
Reads InfiniteWall from a restart file.
InfiniteWall * restriction_
std::string getName() const override
Returns the name of the object, in this case the string "RestrictedWall".
This is a class defining walls.
Definition: InfiniteWall.h:47
Mdouble getDistance(Vec3D position) const
Returns the distance of the wall to the particle.
RestrictedWall()
Default constructor, the normal is infinitely long.
Definition: Vector.h:49
BaseInteraction * getInteractionWith(BaseParticle *p, unsigned timeStamp, InteractionHandler *interactionHandler) override
Look up the interaction between this wall and a BaseParticle at a certain timeStamp.
void writeVTK(VTKContainer &vtk) const override
virtual BaseWall * copy() const =0
Pure virtual function that can be overwritten in inherited classes in order to copy a BaseWall...
void setSpecies(const ParticleSpecies *species)
Defines the species of the current wall.
Definition: BaseWall.cc:171