Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.88 KB | None | 0 0
  1. #include <string>
  2. #include <iostream>
  3.  
  4. struct Point3D {
  5. protected:
  6.     float x, y, z;
  7.  
  8. public:
  9.     Point3D() : x(0.0f), y(0.0f), z(0.0f) {}
  10.     Point3D(float x, float y, float z) : x(x), y(y), z(z) {}
  11.     void output() { std::wcout << L"{" << x << L", " << y << L", " << z << L"}"; }
  12.     Point3D operator-(const Point3D &point) const {
  13.         Point3D temp;
  14.         temp.setX(getX() - point.getX());
  15.         temp.setY(getY() - point.getY());
  16.         temp.setZ(getZ() - point.getZ());
  17.         return temp;
  18.     }
  19.     Point3D operator+ (const Point3D &value) const {
  20.         Point3D temp;
  21.         temp.setX(getX() + value.getX());
  22.         temp.setY(getY() + value.getY());
  23.         temp.setZ(getY() + value.getZ());
  24.         return temp;
  25.     }
  26.     inline float getX() const { return x; } inline float getY() const { return y; } inline float getZ() const { return z; }
  27.     inline void setX(float x) { this->x = x; } inline void setY(float y) { this->y = y; } inline void setZ(float z) { this->z = z; }
  28.  
  29.     inline float getLengthAsVector() const {
  30.         return sqrt(x*x + y*y + z*z);
  31.     }
  32.     inline Point3D getCrossProduct(const Point3D &anotherVector) const {
  33.         //based on: http://www.sciencehq.com/physics/vector-product-multiplying-vectors.html
  34.         return Point3D(
  35.             y * anotherVector.z - anotherVector.y * z,
  36.             z * anotherVector.x - anotherVector.z * x,
  37.             x * anotherVector.y - anotherVector.x * y
  38.             );
  39.     }
  40.     inline float getDotProduct(const Point3D &anotherVector) const {
  41.         //based on: https://www.ltcconline.net/greenl/courses/107/Vectors/DOTCROS.HTM
  42.         return x * anotherVector.x + y * anotherVector.y + z * anotherVector.z;
  43.     }
  44.     inline float getAngleToAnotherVectorInRadians(const Point3D &anotherVector) const {
  45.         //based on: http://math.stackexchange.com/questions/974178/how-to-calculate-the-angle-between-2-vectors-in-3d-space-given-a-preset-function
  46.         return acos(getDotProduct(anotherVector) / (getLengthAsVector() * anotherVector.getLengthAsVector()));
  47.     }
  48.     Point3D getNormalized() const {
  49.         float length = std::abs(sqrt(x*x + y*y + z*z));
  50.         Point3D result(x / length, y / length, z / length);
  51.         return result;
  52.     }
  53. };
  54.  
  55. class AxisAngle {
  56. protected:
  57.     Point3D axis;
  58.     float angleInRadians;
  59.  
  60. public:
  61.     AxisAngle(const AxisAngle &other) { axis = other.axis; angleInRadians = other.angleInRadians; }
  62.     AxisAngle::AxisAngle(float x, float y, float z, float angleInRadians) {
  63.         this->axis = Point3D(x, y, z);
  64.         this->angleInRadians = angleInRadians;
  65.     }
  66.     AxisAngle::AxisAngle(const Point3D &axis, float angleInRadians) {
  67.         this->axis = axis;
  68.         this->angleInRadians = angleInRadians;
  69.     }
  70.  
  71.     Point3D getAxis() const { return axis; }
  72.     float getAngleInRadians() const { return angleInRadians; }
  73.  
  74.     void output() { std::wcout << L"{";  axis.output(); std::wcout << L", " << angleInRadians << L"}"; }
  75.  
  76. };
  77.  
  78. class Quaternion {
  79. protected:
  80.     float x; float y; float z; float w;
  81.  
  82. public:
  83.     Quaternion() { x = 0.0f; y = 0.0f; z = 0.0f; w = 1.0f; }
  84.     Quaternion(const Quaternion &other) { x = other.x; y = other.y; z = other.z; w = other.w; }
  85.     Quaternion(float x, float y, float z, float w) { this->x = x; this->y = y; this->z = z; this->w = w; }
  86.     Quaternion(const AxisAngle &axisAngle) {
  87.         Point3D axis = axisAngle.getAxis();
  88.         float angleInRadians = axisAngle.getAngleInRadians();
  89.         x = sin(angleInRadians / 2) * axis.getX();
  90.         y = sin(angleInRadians / 2) * axis.getY();
  91.         z = sin(angleInRadians / 2) * axis.getZ();
  92.         w = cos(angleInRadians / 2);
  93.         normalizeIt();
  94.     }
  95.  
  96.     float getLength() const {
  97.         return sqrt(x*x + y*y + z*z + w*w);
  98.     }
  99.  
  100.     void normalizeIt() {
  101.         float length = getLength();
  102.         x = x / length;
  103.         y = y / length;
  104.         z = z / length;
  105.         w = w / length;
  106.     }
  107.  
  108.     Quaternion getConjugated() const {
  109.         return Quaternion(-x, -y, -z, w);
  110.     }
  111.  
  112.     Quaternion multiply(Quaternion by) {
  113.         //"R" for result
  114.         float wR = w * by.getW() - x * by.getX() - y * by.getY() - z * by.getZ();
  115.         float xR = x * by.getW() + w * by.getX() + y * by.getZ() - z * by.getY();
  116.         float yR = y * by.getW() + w * by.getY() + z * by.getX() - x * by.getZ();
  117.         float zR = z * by.getW() + w * by.getZ() + x * by.getY() - y * by.getX();
  118.         return Quaternion(xR, yR, zR, wR);
  119.     }
  120.  
  121.     //rotate Point3D p around [0,0,0] with this Quaternion
  122.     Point3D rotatePoint(Point3D p) const {
  123.         Quaternion temp = multiply(p).multiply(getConjugated());
  124.         return Point3D(temp.getX(), temp.getY(), temp.getZ());
  125.  
  126.         //G: P' = Q(P-G)Q' + G <- to rotate P around G with Quaternion
  127.     }
  128.  
  129.     Quaternion multiply(Point3D r) const {
  130.         float wR = -x * r.getX() - y * r.getY() - z * r.getZ();
  131.         float xR = w * r.getX() + y * r.getZ() - z * r.getY();
  132.         float yR = w * r.getY() + z * r.getX() - x * r.getZ();
  133.         float zR = w * r.getZ() + x * r.getY() - y * r.getX();
  134.         return Quaternion(xR, yR, zR, wR);
  135.     }
  136.  
  137.     inline float getX() const { return x; } inline void setX(float x) { this->x = x; }
  138.     inline float getY() const { return y; } inline void setY(float y) { this->y = y; }
  139.     inline float getZ() const { return z; } inline void setZ(float z) { this->z = z; }
  140.     inline float getW() const { return w; } inline void setW(float w) { this->w = w; }
  141.  
  142.     void output() { std::wcout << L"{" << x << L", " << y << L", " << z << L", " << w << L"}"; }
  143. };
  144.  
  145. int main() {
  146.  
  147.     Point3D start = { 5.0f, 4.0f, 7.0f };
  148.     Point3D end = { 15.0f, 6.0f, 14.0f };
  149.  
  150.     Point3D direction = (end - start);
  151.     std::wcout << L"Direction: "; direction.output();
  152.  
  153.     float angle = Point3D(0.0f, 1.0f, 0.0f).getAngleToAnotherVectorInRadians(direction);
  154.     Point3D axis = direction.getCrossProduct(Point3D(0.0f, 1.0f, 0.0f)).getNormalized();
  155.     Quaternion o = Quaternion(AxisAngle(axis, angle));
  156.  
  157.     std::wcout << L"\nAxisAngle: ";  AxisAngle(axis, angle).output();
  158.     std::wcout << L"\nOrientation: "; o.output();
  159.  
  160.     //test - end2 should be equal to end
  161.     Point3D offset(0.0f, (end - start).getLengthAsVector(), 0.0f);
  162.     offset = o.rotatePoint(offset);
  163.     std::wcout << L"\nOffset: "; offset.output();
  164.     Point3D end2 = start + offset;
  165.     std::wcout << L"\nEnd2: "; end2.output();
  166.  
  167.     int a;
  168.     std::cin >> a;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement