MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
convexhullcollisionshape.cpp
1 // © Copyright 2010 - 2014 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(Exception::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::SerializableName());
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  bool 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::SerializableName());
138  if (!CollisionNode) { SerializeError("create CollisionNode",this->ConvexHullCollisionShape::SerializableName());}
139 
140  XML::Attribute Version = CollisionNode.AppendAttribute("Version");
141  if (Version)
142  { Version.SetValue(1); }
143  else
144  { SerializeError("Create Version Attribute", SerializableName()); }
145 
146  XML::Node PointsNode = CollisionNode.AppendChild("UnscaledPoints");
147  if (!PointsNode) { SerializeError("create UnscaledPoints",this->ConvexHullCollisionShape::SerializableName());}
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::SerializableName());
164  if(!CollisionNode)
165  { DeSerializeError("locate PrimitiveCollisionShape node",SerializableName()); }
166  this->PrimitiveCollisionShape::ProtoDeSerialize(CollisionNode);
167 
168  XML::Node UnscaledPoints = OneNode.GetChild("UnscaledPoints");
169  if(!UnscaledPoints)
170  { DeSerializeError("locate UnscaledPoints node",SerializableName()); }
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",SerializableName());
181  }
182  }else{
183  DeSerializeError(String("find correct class to deserialize, found a ")+OneNode.Name(),SerializableName());
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 
199  void operator >> (const Mezzanine::XML::Node& OneNode, Mezzanine::Physics::ConvexHullCollisionShape& x)
200  { x.ProtoDeSerialize(OneNode); }
201 
202 #endif