Spinning Topp Logo BlackTopp Studios
inc
transform.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 _transform_cpp
41 #define _transform_cpp
42 
43 #include "serialization.h"
44 #include "stringtool.h"
45 #include "transform.h"
46 #include "exception.h"
47 
48 #include <btBulletDynamicsCommon.h>
49 
50 
51 
52 namespace Mezzanine
53 {
54  Transform::Transform(const Vector3& Vec, const Quaternion& Quat):
55  Location(Vec),
56  Rotation(Quat)
57  {}
58 
59  Transform::Transform(const btTransform& Btt)
60  { this->ExtractBulletTransform(Btt); }
61 
63  { this->operator=(TheOther); }
64 
66  {
67  this->Location.Zero();
68  this->Rotation.SetIdentity();
69  }
70 
71  btTransform Transform::GetBulletTransform() const
72  {
73  return btTransform( this->Rotation.GetBulletQuaternion(),
75  );
76  }
77 
78  void Transform::ExtractBulletTransform(const btTransform& temp)
79  {
80  this->Location.ExtractBulletVector3(temp.getOrigin());
81  this->Rotation.ExtractBulletQuaternion(temp.getRotation());
82  }
83 
84 
86  {
87  if( this != &rhs ) // Are we assigning to ourselves?!?!?!?
88  {
89  this->Location = rhs.Location;
90  this->Rotation = rhs.Rotation;
91  }
92  return *this;
93  }
94 
95  Transform& Transform::operator= (const btTransform& rhs)
96  {
97  this->Location.ExtractBulletVector3(rhs.getOrigin());
98  this->Rotation.ExtractBulletQuaternion(rhs.getRotation());
99  return *this;
100  }
101 
102  void Transform::ProtoSerialize(XML::Node& CurrentRoot) const
103  {
104  XML::Node TransformNode = CurrentRoot.AppendChild(GetSerializableName()); // The base node all the base constraint stuff will go in
105  if (!TransformNode)
106  { SerializeError("Create TransformNode", GetSerializableName()); }
107 
108  Mezzanine::XML::Attribute Version = TransformNode.AppendAttribute("Version"); // Version
109  if (!Version)
110  { SerializeError("Create Version", GetSerializableName()); }
111  Version.SetValue(1);
112 
113  this->Location.ProtoSerialize(TransformNode);
114  this->Rotation.ProtoSerialize(TransformNode);
115  }
116 
118  {
120  {
121  if(OneNode.GetAttribute("Version").AsInt() == 1)
122  {
123  this->Location.ProtoDeSerialize(OneNode.GetChild("Vector3"));
124  this->Rotation.ProtoDeSerialize(OneNode.GetChild("Quaternion"));
125  }else{
126  MEZZ_EXCEPTION(ExceptionBase::INVALID_VERSION_EXCEPTION,"Incompatible XML Version for " + GetSerializableName() + ": Not Version 1.");
127  }
128  }else{
129  MEZZ_EXCEPTION(ExceptionBase::II_IDENTITY_INVALID_EXCEPTION,"Attempting to deserialize a " + GetSerializableName() + ", found a " + String(OneNode.Name()));
130  }
131  }
132 
134  { return String("Transform"); }
135 
137  { return Transform(this->Location-rhs.Location,this->Rotation-rhs.Rotation); }
138 
140  { return Transform(this->Location+rhs.Location, this->Rotation+rhs.Rotation); }
141 
143  { return Transform(this->Location*rhs,this->Rotation*rhs); }
144 
146  { return Transform(this->Location/rhs,this->Rotation/rhs); }
147 
149  { return this->Location<=rhs.Location && this->Rotation<=rhs.Rotation; }
150 
152  { return this->Location>=rhs.Location && this->Rotation>=rhs.Rotation; }
153 }
154 
155 
156 std::ostream& operator << (std::ostream& stream, const Mezzanine::Transform& rhs)
157 {
158  Serialize(stream,rhs);
159  return stream;
160 }
161 
162 btTransform& operator<< (btTransform& lhs, const Mezzanine::Transform& rhs)
163 {
164  lhs.setOrigin(rhs.Location.GetBulletVector3());
165  lhs.setRotation(rhs.Rotation.GetBulletQuaternion());
166  return lhs;
167 }
168 
170  { return lhs=rhs; }
171 
172 
173 #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.
Transform operator/(Real rhs) const
Divide all the values of this by a single scalar.
Definition: transform.cpp:145
A light-weight handle for manipulating attributes in DOM tree.
Definition: attribute.h:74
Boole operator>=(const Transform &rhs) const
Is every value in this Transform greater than or equal to its corresponding value in another...
Definition: transform.cpp:151
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
Transform operator+(const Transform &rhs) const
Create a Transform with the sum of this and another.
Definition: transform.cpp:139
#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...
Thrown when a version is accessed/parsed/required and it cannot work correctly or is missing...
Definition: exception.h:112
Transform operator*(Real rhs) const
Multiply all the values of this by a single scalar.
Definition: transform.cpp:142
void ProtoDeSerialize(const XML::Node &OneNode)
Take the data stored in an XML and overwrite this instance of this object with it.
Definition: transform.cpp:117
static String GetSerializableName()
Get the name of the the XML tag this class will leave behind as its instances are serialized...
Definition: transform.cpp:133
This implements the exception hiearchy for Mezzanine.
Transform operator-(const Transform &rhs) const
Create a Transform with the difference of this and another.
Definition: transform.cpp:136
btQuaternion GetBulletQuaternion(Boole normalize=false) const
Gets a Bullet quaternion.
Definition: quaternion.cpp:243
Transform & operator=(const Transform &rhs)
Set the values of this Transform to match an existing Transform.
Definition: transform.cpp:85
float Real
A Datatype used to represent a real floating point number.
Definition: datatypes.h:141
The interface for serialization.
bool SetValue(const Char8 *rhs)
Set the value of this.
btTransform GetBulletTransform() const
Gets a Bullet Transform.
Definition: transform.cpp:71
void ExtractBulletQuaternion(const btQuaternion &Ours)
Copies an existing Bullet quaternion.
Definition: quaternion.cpp:255
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
Quaternion Rotation
The Rotation or relative rotation is stored in a Quaternion.
Definition: transform.h:71
int AsInt(int def=0) const
Attempts to convert the value of the attribute to an int and returns the results. ...
Transform(const Vector3 &Vec=Vector3(), const Quaternion &Quat=Quaternion())
The multipurpose constructor.
Definition: transform.cpp:54
void ExtractBulletVector3(const btVector3 &temp)
Copies an existing Bullet vector3.
Definition: vector3.cpp:565
The defintion of the transform is stored in this file.
Stores information about relative location and rotation in 3d space.
Definition: transform.h:62
void ProtoDeSerialize(const XML::Node &OneNode)
Take the data stored in an XML and overwrite this instance of this object with it.
Definition: vector3.cpp:614
void ExtractBulletTransform(const btTransform &temp)
Copies an existing Bullet transform.
Definition: transform.cpp:78
Vector3 Location
The location or relative location is stored in a Vector3.
Definition: transform.h:68
Thrown when the identity string wasn't valid at all.
Definition: exception.h:93
This is used to represent a point in space, or a vector through space.
Definition: vector3.h:77
void Zero()
Sets all the members of this vector3 to zero.
Definition: vector3.cpp:517
void SetIdentity()
Sets default/identity values to the members of this quaternion.
Definition: quaternion.cpp:99
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
void SetIdentity()
Sets default construction values for all members.
Definition: transform.cpp:65
void ProtoSerialize(XML::Node &CurrentRoot) const
Convert this class to an XML::Node ready for serialization.
Definition: quaternion.cpp:502
void ProtoSerialize(XML::Node &CurrentRoot) const
Convert this class to an XML::Node ready for serialization.
Definition: transform.cpp:102
const Char8 * Name() const
ptrdiff_tGet the name of this Node.
This is used to store information about rotation in 3d space.
Definition: quaternion.h:68
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.
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
void ProtoDeSerialize(const XML::Node &OneNode)
Take the data stored in an XML and overwrite this instance of this object with it.
Definition: quaternion.cpp:526
Attribute GetAttribute(const Char8 *Name) const
Attempt to get an Attribute on this Node with a given name.
Boole operator<=(const Transform &rhs) const
Is every value in this Transform less than or equal to its corresponding value in another...
Definition: transform.cpp:148
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.