Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2013
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.10 KB | None | 0 0
  1. #pragma once
  2. #include <iostream>
  3. #include <sstream>
  4. #include <cmath>
  5.  
  6. namespace geom {
  7.  
  8. struct Vector3f {
  9.     float x, y, z;
  10.    
  11.     Vector3f(float x = 0.0f, float y = 0.0f, float z = 0.0f)
  12.     : x(x), y(y), z(z)
  13.     { }
  14.  
  15.     Vector3f(Vector3f const &other)
  16.     : x(other.x), y(other.y), z(other.z)
  17.     { }
  18.  
  19.     void set(Vector3f const &other) {
  20.         this->x = other.x;
  21.         this->y = other.y;
  22.         this->z = other.z;
  23.     }
  24.  
  25.     void set(const float x, const float y, const float z)
  26.     {
  27.         this->x = x;
  28.         this->y = y;
  29.         this->z = z;
  30.     }
  31.  
  32.     void set(const float a)
  33.     {
  34.         this->x = a;
  35.         this->y = a;
  36.         this->z = a;
  37.     }
  38.  
  39.     void setToZero() {
  40.         x = y = z = 0.0f;
  41.     }
  42.  
  43.     bool isZero()
  44.     {
  45.         return x == 0.0f && y == 0.0f && z == 0.0f;
  46.     }
  47.     bool isZero(float epsilon) {
  48.         float epsSquare = epsilon*epsilon;
  49.         return x * x <= epsSquare && y * y <= epsSquare && z * z <= epsSquare;
  50.     }
  51.  
  52.     void setToAbsoluteValues() {
  53.         if (x < 0.0f) x = -x;
  54.         if (y < 0.0f) y = -y;
  55.         if (z < 0.0f) z = -z;
  56.     }
  57.     void add(Vector3f const &other) {
  58.         x += other.x;
  59.         y += other.y;
  60.         z += other.z;
  61.     }
  62.  
  63.     void add(const float ax, const float ay, const float az) {
  64.         x += ax;
  65.         y += ay;
  66.         z += az;
  67.     }
  68.  
  69.     void add(float a) {
  70.         x += a;
  71.         y += a;
  72.         z += a;
  73.     }
  74.  
  75.     void subtract(Vector3f const &other) {
  76.         x -= other.x;
  77.         y -= other.y;
  78.         z -= other.z;
  79.     }
  80.  
  81.     void subtract(const float ax, const float ay, const float az) {
  82.         x -= ax;
  83.         y -= ay;
  84.         z -= az;
  85.     }
  86.  
  87.     void subtract(float a) {
  88.         x -= a;
  89.         y -= a;
  90.         z -= a;
  91.     }
  92.  
  93.     void scale(const float scale) {
  94.         this->x *= scale;
  95.         this->y *= scale;
  96.         this->z *= scale;
  97.     }
  98.    
  99.     void scale(const float sx, const float sy, const float sz) {
  100.         this->x *= sx;
  101.         this->y *= sy;
  102.         this->z *= sz;
  103.     }
  104.  
  105.     void scaleAdd(const float scale, Vector3f const &other) {
  106.         x += scale*other.x;
  107.         y += scale*other.y;
  108.         z += scale*other.z;
  109.     }
  110.  
  111.     void scaleAdd(const double scale, Vector3f const &other) {
  112.         x = (float)(x + scale*other.x);
  113.         y = (float)(y + scale*other.y);
  114.         z = (float)(z + scale*other.z);
  115.     }
  116.  
  117.     void divide(const float divisor) {
  118.         this->x /= divisor;
  119.         this->y /= divisor;
  120.         this->z /= divisor;
  121.     }
  122.  
  123.     /**
  124.      * scalar product
  125.      */
  126.     float dot(Vector3f const &other) const
  127.     {
  128.         return x * other.x + y * other.y + z * other.z;
  129.     }
  130.  
  131.     /**
  132.      * cross product stored in this vector
  133.      */
  134.     void crossWith(Vector3f const &other)
  135.     {
  136.         float nx = y * other.z - z * other.y;
  137.         float ny = z * other.x - x * other.z;
  138.         float nz = x * other.y - y * other.x;
  139.         x = nx;
  140.         y = ny;
  141.         z = nz;
  142.     }
  143.  
  144.     bool normalize() {
  145.         float l = length();
  146.         if (l == 0.0f) return false;
  147.         x /= l; y /= l; z /= l;
  148.         return true;
  149.     }
  150.     bool normalizeDoublePrececision() {
  151.         double l = std::sqrt(x * (double)x  +  y * (double)y  +  z * (double)z);
  152.         if (l == 0.0) return false;
  153.         x = (float)(x/l); y = (float)(y/l); z = (float)(z/l);
  154.         return true;
  155.     }
  156.  
  157.     bool normalizeFastInaccurateIEEE() {
  158.         float x = lengthSquared();
  159.         float lhalf = 0.5f*x;
  160.         union
  161.         {
  162.              float l;
  163.              int i;
  164.         } u;
  165.         u.l = x;
  166.         u.i = 0x5f3759df - (u.i >> 1);
  167.         u.l = u.l * (1.5f - lhalf * u.l * u.l); /* This line can be repeated arbitrarily many times to increase accuracy */
  168.         u.l = u.l * (1.5f - lhalf * u.l * u.l);
  169.        
  170.         if (u.l==0.0f) return false;
  171.  
  172.         x /= u.l;   y /= u.l; z /= u.l;
  173.        
  174.         return true;
  175.     }
  176.  
  177.     float lengthSquared() const {
  178.         return x * x  +  y * y  +  z * z;
  179.     }
  180.     float length() const {
  181.         return std::sqrt(lengthSquared());
  182.     }
  183.  
  184.     bool setLength(float newLength) {
  185.         float l = length();
  186.         if (l == 0.0f) return false;
  187.         x = (x/l)*newLength; y = (y/l)*newLength; z = (z/l)*newLength; 
  188.         return true;
  189.     }
  190.  
  191.     /**
  192.      * inverts the components so that invert[v(x,y,z)] = v(1/x,1/y,1/z)
  193.      */
  194.     void invert()
  195.     {
  196.         x = 1.0f/x;
  197.         y = 1.0f/y;
  198.         z = 1.0f/z;
  199.     }
  200.  
  201.     float distanceSquared(Vector3f const &other) const {
  202.         float dx = other.x - x;
  203.         float dy = other.y - y;
  204.         float dz = other.z - z;
  205.         return dx * dx + dy * dy + dz * dz;
  206.     }
  207.     float distance(Vector3f const &other) const {
  208.         return std::sqrt(distanceSquared(other));
  209.     }
  210.  
  211.     float getMaxComponent() const {
  212.         return x > y ? (x > z ? x : z) : (y > z ? y : z);
  213.     }
  214.  
  215.     float getMinComponent() const {
  216.         return x < y ? (x < z ? x : z) : (y < z ? y : z);
  217.     }
  218.     void rotateAroundZ(float angle) {
  219.         float nx;
  220.         nx = x*cosf(angle) - y*sinf(angle);
  221.         y  = y*sinf(angle) + y*cosf(angle);
  222.         x = nx;
  223.     }
  224.  
  225.     void rotateAroundY(float angle) {
  226.         float nx;
  227.         nx = x*cosf(angle) + z*sinf(angle);
  228.         z  =-x*sinf(angle) + z*cosf(angle);
  229.         x = nx;
  230.     }
  231.  
  232.     void rotateAroundX(float angle) {
  233.         float ny;
  234.         ny = y*cosf(angle) - z*sinf(angle);
  235.         z  = y*sinf(angle) + z*cosf(angle);
  236.         y = ny;
  237.     }
  238.  
  239.     /*
  240.     Vector3f const& operator = (Vector3f const & other) const
  241.     {
  242.           set(other);
  243.           return *this;
  244.     }
  245.     */
  246.     Vector3f operator + (Vector3f const & other) const
  247.     {
  248.           return Vector3f(x + other.x, y + other.y, z + other.z);
  249.     }
  250.  
  251.     Vector3f operator + (float offset) const
  252.     {
  253.           return Vector3f(x + offset, y + offset, z + offset);
  254.     }
  255.  
  256.     Vector3f operator - (Vector3f const & other) const
  257.     {
  258.           return Vector3f(x - other.x, y - other.y, z - other.z);
  259.     }
  260.  
  261.     Vector3f operator - (float offset) const
  262.     {
  263.           return Vector3f(x - offset, y - offset, z - offset);
  264.     }
  265.  
  266.     Vector3f operator * (float scale) const
  267.     {
  268.           return Vector3f(x * scale, y * scale, z * scale);
  269.     }
  270.  
  271.     Vector3f operator / (float scale) const
  272.     {
  273.         return Vector3f(x / scale, y / scale, z / scale);
  274.     }
  275.  
  276.     /**
  277.      * cross product
  278.      */
  279.     Vector3f operator *(Vector3f const &other) const
  280.     {
  281.         return Vector3f(
  282.             y * other.z - z * other.y,
  283.             z * other.x - x * other.z,
  284.             x * other.y - y * other.x
  285.         );
  286.     }
  287.  
  288.     bool operator == (Vector3f const &b) const {
  289.         return x == b.x && y == b.y && z == b.z;
  290.     }
  291.  
  292.     bool operator != (Vector3f const &b) const
  293.     {
  294.         return x != b.x || y != b.y || z != b.z;
  295.     }
  296.  
  297.     bool operator < (Vector3f const &b) const
  298.     {
  299.         return lengthSquared() < b.lengthSquared();
  300.     }
  301.     bool operator <= (Vector3f const &b) const
  302.     {
  303.         return lengthSquared() <= b.lengthSquared();
  304.     }
  305.     bool operator > (Vector3f const &b) const
  306.     {
  307.         return lengthSquared() > b.lengthSquared();
  308.     }
  309.     bool operator >= (Vector3f const &b) const
  310.     {
  311.         return lengthSquared() >= b.lengthSquared();
  312.     }
  313.  
  314.     float& operator[] (const unsigned int i) {
  315.         switch (i) {
  316.             case 0: return x;
  317.             case 1: return y;
  318.             default: return z;
  319.         }
  320.     }
  321.  
  322.     std::string toString() const {
  323.         std::stringstream str;
  324.         str << "("<<x<<"; "<<y<<"; "<<z<<")";
  325.         return str.str();
  326.     }
  327.  
  328.     friend std::ostream& operator<<(std::ostream& out, const Vector3f& vec) // output
  329.     {
  330.         out << "("<<vec.x<<"; "<<vec.y<<"; "<<vec.z<<")";
  331.         return out;
  332.     }
  333. };
  334.  
  335. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement