Spinning Topp Logo BlackTopp Studios
inc
convexhullcollisionshape.cpp
1 // © Copyright 2010 - 2016 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 #ifndef _physicsconvexhullcollisionshape_cpp
41 #define _physicsconvexhullcollisionshape_cpp
42 
43 #include "Physics/convexhullcollisionshape.h"
44 #include "collisionshapemanager.h"
45 #include "stringtool.h"
46 
47 #include "btBulletDynamicsCommon.h"
48 
49 namespace Mezzanine
50 {
51  namespace Physics
52  {
53  /////////////////////////////////////////
54  // ConvexHullCollisionShape Functions
55 
56  ConvexHullCollisionShape::ConvexHullCollisionShape(const String& Name, const std::vector<Vector3>& Points)
57  {
58  btScalar* BulletPoints = new btScalar[Points.size() * 3];
59  for( Whole X = 0 ; X < Points.size() ; ++X )
60  {
61  BulletPoints[X*3] = Points[X][0];
62  BulletPoints[X*3+1] = Points[X][1];
63  BulletPoints[X*3+2] = Points[X][2];
64  }
65 
66  this->Name = Name;
67  SetPointers(new btConvexHullShape(BulletPoints,Points.size(),3*sizeof(btScalar)));
68  this->GetBulletHullShape()->setImplicitShapeDimensions(Vector3(0,0,0).GetBulletVector3());
69  delete[] BulletPoints;
70  }
71 
72  ConvexHullCollisionShape::ConvexHullCollisionShape(const String& Name, btConvexHullShape* BulletShape)
73  {
74  this->Name = Name;
75  SetPointers(BulletShape);
76  this->GetBulletHullShape()->setImplicitShapeDimensions(Vector3(0,0,0).GetBulletVector3());
77  }
78 
80  {
81  if(OneNode.GetAttribute("Version").AsInt() == 1)
82  {
83  XML::Attribute OneName = OneNode.GetChild("PrimitiveCollisionShape").GetChild("CollisionShape").GetAttribute("Name"); // get name
84  if(!OneName) { MEZZ_EXCEPTION(ExceptionBase::PARAMETERS_EXCEPTION,"Could not find Name Attribute on CollsionShape Node during preparation for deserialization"); }
85 
86  this->Name = OneName.AsString();
87 
88  SetPointers(new btConvexHullShape());
89 
90  this->ProtoDeSerialize(OneNode);
91  }else{
92  DeSerializeError("find usable serialization version", ConvexHullCollisionShape::GetSerializableName());
93  }
94  }
95 
97  {
98  delete GetBulletHullShape();
99  }
100 
102  {
103  GetBulletHullShape()->addPoint(Point.GetBulletVector3());
104  }
105 
107  {
108 
109  return Vector3 ( *(GetBulletHullShape()->getUnscaledPoints()+Index) );
110  }
111 
113  {
114  return Vector3(GetBulletHullShape()->getScaledPoint(Index));
115  }
116 
118  {
119  return GetBulletHullShape()->getNumPoints();
120  }
121 
122  Boole ConvexHullCollisionShape::IsInside(const Vector3& Location, const Real& Tolerance) const
123  {
124  return GetBulletHullShape()->isInside(Location.GetBulletVector3(),Tolerance);
125  }
126 
128  {
130  }
131 
133  { return static_cast<btConvexHullShape*>(ShapeBase); }
134 
136  {
137  XML::Node CollisionNode = CurrentRoot.AppendChild(this->ConvexHullCollisionShape::GetSerializableName());
138  if (!CollisionNode) { SerializeError("create CollisionNode",this->ConvexHullCollisionShape::GetSerializableName());}
139 
140  XML::Attribute Version = CollisionNode.AppendAttribute("Version");
141  if (Version)
142  { Version.SetValue(1); }
143  else
144  { SerializeError("Create Version Attribute", GetSerializableName()); }
145 
146  XML::Node PointsNode = CollisionNode.AppendChild("UnscaledPoints");
147  if (!PointsNode) { SerializeError("create UnscaledPoints",this->ConvexHullCollisionShape::GetSerializableName());}
148 
149  for(Whole c=0; c<this->GetNumPoints(); ++c)
150  {
151  this->GetUnscaledPoint(c).ProtoSerialize(PointsNode);
152  }
153 
154  this->PrimitiveCollisionShape::ProtoSerialize(CollisionNode);
155  }
156 
158  {
160  {
161  if(OneNode.GetAttribute("Version").AsInt() == 1)
162  {
163  XML::Node CollisionNode = OneNode.GetChild(this->PrimitiveCollisionShape::GetSerializableName());
164  if(!CollisionNode)
165  { DeSerializeError("locate PrimitiveCollisionShape node",GetSerializableName()); }
166  this->PrimitiveCollisionShape::ProtoDeSerialize(CollisionNode);
167 
168  XML::Node UnscaledPoints = OneNode.GetChild("UnscaledPoints");
169  if(!UnscaledPoints)
170  { DeSerializeError("locate UnscaledPoints node",GetSerializableName()); }
171 
172  XML::Node OnePoint = UnscaledPoints.GetFirstChild();
173  while (OnePoint)
174  {
175  this->AddPoint(Vector3(OnePoint));
176  OnePoint = OnePoint.GetNextSibling();
177  }
178 
179  }else{
180  DeSerializeError("find usable serialization version",GetSerializableName());
181  }
182  }else{
183  DeSerializeError(String("find correct class to deserialize, found a ")+OneNode.Name(),GetSerializableName());
184  }
185  }
186 
188  { return String("ConvexHullCollisionShape"); }
189 
190  }//Physics
191 }//Mezzanine
192 
193  std::ostream& operator << (std::ostream& stream, const Mezzanine::Physics::ConvexHullCollisionShape& ShapeToSerialize)
194  { Mezzanine::Serialize(stream, ShapeToSerialize); return stream; }
195 
196  std::istream& operator >> (std::istream& stream, Mezzanine::Physics::ConvexHullCollisionShape& x)
197  { return Mezzanine::DeSerialize(stream, x); }
198 
200  { x.ProtoDeSerialize(OneNode); }
201 
202 #endif
std::ostream & operator<<(std::ostream &stream, const Mezzanine::LinearInterpolator< T > &Lint)
Used to Serialize an Mezzanine::LinearInterpolator to a human readable stream.
Definition: interpolator.h:433
Attribute AppendAttribute(const Char8 *Name)
Creates an Attribute and puts it at the end of this Nodes attributes.
A light-weight handle for manipulating attributes in DOM tree.
Definition: attribute.h:74
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
ConvexHullCollisionShape(const String &Name, const std::vector< Vector3 > &Points)
Class Constructor.
virtual void ProtoDeSerialize(const XML::Node &OneNode)
Node GetFirstChild() const
Get the first child Node of this Node.
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
std::ostream & Serialize(std::ostream &Stream, const T &Converted, const String &Indent=String(""))
Convert any class that supports serialization or has a serializer to a string of chars in a stream...
static String GetSerializableName()
Get the name of the the XML tag this class will leave behind as its instances are serialized...
const Char8 * AsString(const Char8 *def="") const
Attempts to convert the value of the attribute to a String and returns the results.
virtual void ProtoSerialize(XML::Node &CurrentRoot) const
virtual void AddPoint(const Vector3 &Point)
Adds a point to this shape.
A simple convex shape built from a low number of points in local space.
Node GetNextSibling() const
Attempt to retrieve the next sibling of this Node.
virtual void ProtoSerialize(XML::Node &CurrentRoot) const
virtual btConvexHullShape * GetBulletHullShape() const
float Real
A Datatype used to represent a real floating point number.
Definition: datatypes.h:141
virtual Whole GetNumPoints() const
Gets the total number of points being stored in this shape.
bool SetValue(const Char8 *rhs)
Set the value of this.
btVector3 GetBulletVector3() const
Gets a Bullet vector3.
Definition: vector3.cpp:555
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
int AsInt(int def=0) const
Attempts to convert the value of the attribute to an int and returns the results. ...
static String GetSerializableName()
Get the name of the the XML tag this class will leave behind as its instances are serialized...
ShapeType
This enum describes what kind of shape you are currently working with.
std::istream & DeSerialize(std::istream &Stream, T &Converted)
Deserialize the next xml tag in the stream into a specific in memory class instance.
virtual CollisionShape::ShapeType GetType() const
Gets the type of Collision shape this is.
void DeSerializeError(const String &FailedTo, const String &ClassName, Boole SOrD)
Simply does some string concatenation, then throws an Exception.
Thrown when parameters are checked at runtime and found invalid.
Definition: exception.h:108
virtual Vector3 GetUnscaledPoint(const Whole &Index) const
Gets an unscaled stored point in this ConvexHull.
This is used to represent a point in space, or a vector through space.
Definition: vector3.h:77
void SetPointers(btConvexInternalShape *Shape)
Sets the internal pointers on the base classes.
virtual void ProtoDeSerialize(const XML::Node &OneNode)
String Name
Storage for the name of this class instance.
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
unsigned long Whole
Whole is an unsigned integer, it will be at least 32bits in size.
Definition: datatypes.h:151
std::istream & operator>>(std::istream &stream, Mezzanine::LinearInterpolator< T > &Lint)
Used to de-serialize an Mezzanine::LinearInterpolator from a stream.
Definition: interpolator.h:448
btCollisionShape * ShapeBase
A pointer to the bullet collision this uses.
const Char8 * Name() const
ptrdiff_tGet the name of this Node.
void SerializeError(const String &FailedTo, const String &ClassName, Boole SOrD)
Simply does some string concatenation, then throws an Exception.
Node AppendChild(NodeType Type=NodeElement)
Creates a Node and makes it a child of this one.
virtual Boole IsInside(const Vector3 &Location, const Real &Tolerance) const
Checks to see if a point in local space is inside this shape.
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
Attribute GetAttribute(const Char8 *Name) const
Attempt to get an Attribute on this Node with a given name.
virtual Vector3 GetPoint(const Whole &Index) const
Gets a stored point as it is scaled in this ConvexHull.
Indicates the class is a ConvexHullCollisionShape.
void ProtoSerialize(XML::Node &CurrentRoot) const
Convert this class to an XML::Node ready for serialization.
Definition: vector3.cpp:588
Node GetChild(const Char8 *Name) const
Attempt to get a child Node with a given name.