From 4d7b1da29e0957ac798ee8e6da8288cbd4ae5c79 Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Thu, 18 May 2017 19:03:05 +0500 Subject: 2017-05-18 --- src/utility/Vector.hpp | 118 ++++++++++++++++++++++++++++++++++++++++++++++++ src/utility/utility.cpp | 66 +++++++++++++++++++++++++++ src/utility/utility.h | 16 +++++++ 3 files changed, 200 insertions(+) create mode 100644 src/utility/Vector.hpp create mode 100644 src/utility/utility.cpp create mode 100644 src/utility/utility.h (limited to 'src/utility') diff --git a/src/utility/Vector.hpp b/src/utility/Vector.hpp new file mode 100644 index 0000000..7b34ac3 --- /dev/null +++ b/src/utility/Vector.hpp @@ -0,0 +1,118 @@ +#pragma once + +#include +#include +#include + +template +class Vector3 { + T x, y, z; +public: + Vector3(T X = 0, T Y = 0, T Z = 0) : x(X), y(Y), z(Z) {} + + Vector3(const Vector3 &rhs) : x(rhs.x), y(rhs.y), z(rhs.z) {} + + ~Vector3() = default; + + void SetX(T X) { x = X; } + + void SetY(T Y) { y = Y; } + + void setZ(T Z) { z = Z; } + + T GetX() const { return x; } + + T GetY() const { return y; } + + T GetZ() const { return z; } + + double GetDistance() const { return std::sqrt(std::pow(x, 2) + std::pow(y, 2) + std::pow(z, 2)); } + + void swap(Vector3 &rhs){ + std::swap(x,rhs.x); + std::swap(y,rhs.y); + std::swap(z,rhs.z); + } + + Vector3 &operator=(Vector3 rhs) { + rhs.swap(*this); + return *this; + } + + Vector3 operator*(T rhs) const { + return Vector3( + x * rhs, + y * rhs, + z * rhs + ); + } + + Vector3 operator/(T rhs) const { + return Vector3( + x / rhs, + y / rhs, + z / rhs + ); + } + + Vector3 operator+(const Vector3 &rhs) const { + return Vector3( + x + rhs.x, + y + rhs.y, + z + rhs.z + ); + } + + Vector3 operator-(const Vector3 &rhs) const { + return Vector3( + x - rhs.x, + y - rhs.y, + z - rhs.z + ); + } + + Vector3 operator*(const Vector3 &rhs) const { + return Vector3( + x * rhs.x, + y * rhs.y, + z * rhs.z + ); + } + + Vector3 operator/(const Vector3 &rhs) const { + return Vector3( + x / rhs.x, + y / rhs.y, + z / rhs.z + ); + } + + bool operator==(const Vector3 &rhs) const { + return (x == rhs.x && y == rhs.y && z == rhs.z); + } + + bool operator!=(const Vector3 &rhs) const { + return !(*this == rhs); + } + + bool operator<(const Vector3 &rhs) const { + //return (x < rhs.x || y < rhs.y ||z z < rhs.z); + /*if (x < rhs.x) + return true; + else if (z < rhs.z) + return true; + else if (y < rhs.y) + return true; + return false;*/ + return std::tie(x,y,z) VectorF; +typedef Vector3 Vector; \ No newline at end of file diff --git a/src/utility/utility.cpp b/src/utility/utility.cpp new file mode 100644 index 0000000..aa50e9f --- /dev/null +++ b/src/utility/utility.cpp @@ -0,0 +1,66 @@ +#include "utility.h" + +int VarIntRead(unsigned char *data, size_t &readed) { + readed = 0; + int result = 0; + char read; + do { + read = data[readed]; + int value = (read & 0b01111111); + result |= (value << (7 * readed)); + + readed++; + if (readed > 5) { + throw "VarInt is too big"; + } + } while ((read & 0b10000000) != 0); + + return result; +} + +size_t VarIntWrite(unsigned int value, unsigned char *data) { + size_t len = 0; + do { + unsigned char temp = (unsigned char) (value & 0b01111111); + value >>= 7; + if (value != 0) { + temp |= 0b10000000; + } + data[len] = temp; + len++; + } while (value != 0); + return len; +} + +long long int ReadVarLong(unsigned char *data, int &readed) { + readed = 0; + long long result = 0; + unsigned char read; + do { + read = data[readed]; + long long value = (read & 0b01111111); + result |= (value << (7 * readed)); + + readed++; + if (readed > 10) { + throw "VarLong is too big"; + } + } while ((read & 0b10000000) != 0); + return result; +} + +unsigned char *WriteVarLong(unsigned long long int value, int &len) { + unsigned char *data = new unsigned char[10]; + len = 0; + do { + unsigned char temp = (unsigned char) (value & 0b01111111); + value >>= 7; + if (value != 0) { + temp |= 0b10000000; + } + data[len] = temp; + len++; + } while (value != 0); + + return data; +} \ No newline at end of file diff --git a/src/utility/utility.h b/src/utility/utility.h new file mode 100644 index 0000000..32120cb --- /dev/null +++ b/src/utility/utility.h @@ -0,0 +1,16 @@ +#pragma once +#include + +int VarIntRead(unsigned char *data, size_t &readed); + +size_t VarIntWrite(unsigned int value, unsigned char *data); + +long long int ReadVarLong(unsigned char *data, int &readed); + +unsigned char *WriteVarLong(unsigned long long int value, int &len); + +template +void endswap(T *objp) { + unsigned char *memp = reinterpret_cast(objp); + std::reverse(memp, memp + sizeof(T)); +} \ No newline at end of file -- cgit v1.2.3