vec3.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2016-2017, the mcl_3dl authors
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the copyright holder nor the names of its 
00014  *       contributors may be used to endorse or promote products derived from 
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #ifndef MCL_3DL_VEC3_H
00031 #define MCL_3DL_VEC3_H
00032 
00033 namespace mcl_3dl
00034 {
00035 class Vec3
00036 {
00037 public:
00038   float x_;
00039   float y_;
00040   float z_;
00041   Vec3(const float x, const float y, const float z)
00042   {
00043     x_ = x;
00044     y_ = y;
00045     z_ = z;
00046   }
00047   Vec3()
00048   {
00049     x_ = y_ = z_ = 0.0;
00050   }
00051   float& operator[](const size_t i)
00052   {
00053     switch (i)
00054     {
00055       case 0:
00056         return x_;
00057         break;
00058       case 1:
00059         return y_;
00060         break;
00061       case 2:
00062         return z_;
00063         break;
00064       default:
00065         break;
00066     }
00067     return x_;
00068   }
00069   float operator[](const size_t i) const
00070   {
00071     switch (i)
00072     {
00073       case 0:
00074         return x_;
00075         break;
00076       case 1:
00077         return y_;
00078         break;
00079       case 2:
00080         return z_;
00081         break;
00082       default:
00083         break;
00084     }
00085     return x_;
00086   }
00087   bool operator==(const Vec3& q) const
00088   {
00089     return x_ == q.x_ && y_ == q.y_ && z_ == q.z_;
00090   }
00091   bool operator!=(const Vec3& q) const
00092   {
00093     return !operator==(q);
00094   }
00095   Vec3 operator+(const Vec3& q) const
00096   {
00097     return Vec3(x_ + q.x_, y_ + q.y_, z_ + q.z_);
00098   }
00099   Vec3 operator-(const Vec3& q) const
00100   {
00101     return Vec3(x_ - q.x_, y_ - q.y_, z_ - q.z_);
00102   }
00103   Vec3 operator-() const
00104   {
00105     return Vec3(-x_, -y_, -z_);
00106   }
00107   Vec3 operator*(const float& s) const
00108   {
00109     return Vec3(x_ * s, y_ * s, z_ * s);
00110   }
00111   Vec3 operator/(const float& s) const
00112   {
00113     return Vec3(x_ / s, y_ / s, z_ / s);
00114   }
00115   Vec3& operator+=(const Vec3& q)
00116   {
00117     *this = *this + q;
00118     return *this;
00119   }
00120   Vec3& operator-=(const Vec3& q)
00121   {
00122     *this = *this - q;
00123     return *this;
00124   }
00125   Vec3& operator*=(const float& s)
00126   {
00127     *this = *this * s;
00128     return *this;
00129   }
00130   Vec3& operator/=(const float& s)
00131   {
00132     *this = *this / s;
00133     return *this;
00134   }
00135   float dot(const Vec3& q) const
00136   {
00137     return x_ * q.x_ + y_ * q.y_ + z_ * q.z_;
00138   }
00139   Vec3 cross(const Vec3& q) const
00140   {
00141     return Vec3(y_ * q.z_ - z_ * q.y_,
00142                 z_ * q.x_ - x_ * q.z_,
00143                 x_ * q.y_ - y_ * q.x_);
00144   }
00145   Vec3 times(const Vec3& q) const
00146   {
00147     return Vec3(x_ * q.x_, y_ * q.y_, z_ * q.z_);
00148   }
00149   float norm() const
00150   {
00151     return sqrtf(dot(*this));
00152   }
00153   Vec3 normalized() const
00154   {
00155     return *this / norm();
00156   }
00157 };
00158 }  // namespace mcl_3dl
00159 
00160 #endif  // MCL_3DL_VEC3_H


mcl_3dl
Author(s): Atsushi Watanabe
autogenerated on Thu Jun 20 2019 20:04:43