aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2017-10-12 14:57:26 +0100
committerYann Herklotz <ymherklotz@gmail.com>2017-10-12 14:57:26 +0100
commit1bb0ef8960c71ef505a351702bec54c01ba15e22 (patch)
tree365811364237f1e55e66f421b93cad8adcf51eda
parentba733040afb85d4c287a2ec464db05cb86a53fca (diff)
downloadYAGE-1bb0ef8960c71ef505a351702bec54c01ba15e22.tar.gz
YAGE-1bb0ef8960c71ef505a351702bec54c01ba15e22.zip
Fixing spritesheet and fixed #12
-rw-r--r--CMakeLists.txt5
-rw-r--r--editor/editorwindow.cpp8
-rw-r--r--editor/main.cpp8
-rwxr-xr-xscripts/add_version_headers21
-rwxr-xr-xscripts/update_docs12
-rw-r--r--tests/particlebodytest.cpp2
-rw-r--r--tests/resources/floor_atlas.json4
-rw-r--r--tests/simplegame.cpp16
-rw-r--r--tests/spritesheettest.cpp18
-rw-r--r--tests/vector3test.cpp40
-rw-r--r--tests/vector4test.cpp67
-rw-r--r--yage/CMakeLists.txt4
-rw-r--r--yage/base/resourcemanager.h2
-rw-r--r--yage/base/spritebatch.cpp4
-rw-r--r--yage/base/spritebatch.h10
-rw-r--r--yage/base/spritesheet.cpp33
-rw-r--r--yage/base/spritesheet.h6
-rw-r--r--yage/base/texturecache.cpp2
-rw-r--r--yage/base/texturecache.h3
-rw-r--r--yage/base/window.cpp26
-rw-r--r--yage/base/window.h5
-rw-r--r--yage/math/matrix.h174
22 files changed, 359 insertions, 111 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index f2c538a6..75379d19 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -23,6 +23,9 @@ find_package(OpenGL REQUIRED)
include(${CMAKE_CURRENT_SOURCE_DIR}/lib/CMakeLists.txt)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/yage)
+add_executable(simplegame tests/simplegame.cpp)
+target_link_libraries(simplegame yage)
+
if($ENV{UNIT_TESTS})
# enable tests
enable_testing()
@@ -41,4 +44,6 @@ if($ENV{UNIT_TESTS})
make_test(particlebodytest ${SIMULATION_RUNS})
make_test(windowtest ${SIMULATION_RUNS})
make_test(spritesheettest ${SIMULATION_RUNS})
+ make_test(vector3test ${SIMULATION_RUNS})
+ make_test(vector4test ${SIMULATION_RUNS})
endif()
diff --git a/editor/editorwindow.cpp b/editor/editorwindow.cpp
index 35eeeb31..aad84612 100644
--- a/editor/editorwindow.cpp
+++ b/editor/editorwindow.cpp
@@ -1,3 +1,11 @@
+/* ----------------------------------------------------------------------------
+ * /home/yannherklotz/Github/YAGE/editor/editorwindow.cpp
+ *
+ * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
+ * MIT License, see LICENSE file for more details.
+ * ----------------------------------------------------------------------------
+ */
+
#include "editorwindow.h"
#include "ui_editorwindow.h"
diff --git a/editor/main.cpp b/editor/main.cpp
index a3aa7cbc..bc80117b 100644
--- a/editor/main.cpp
+++ b/editor/main.cpp
@@ -1,3 +1,11 @@
+/* ----------------------------------------------------------------------------
+ * /home/yannherklotz/Github/YAGE/editor/main.cpp
+ *
+ * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
+ * MIT License, see LICENSE file for more details.
+ * ----------------------------------------------------------------------------
+ */
+
#include "editorwindow.h"
#include <QApplication>
diff --git a/scripts/add_version_headers b/scripts/add_version_headers
index def7d55a..ffbdd036 100755
--- a/scripts/add_version_headers
+++ b/scripts/add_version_headers
@@ -8,17 +8,14 @@
# See file LICENSE for more details
# -----------------------------------------------------------------------------
-"""\file add_version_headers
-\brief Adds the version headers to every file.
+""" @file Adds the version headers to every file.
-Add Version Headers
-===================
+## Add Version Headers
This file adds the version headers to every file in the directory
-Improvements
-------------
+### Improvements
Add the ability to input command line arguments.
"""
@@ -48,7 +45,7 @@ class HeaderUpdate(object):
self.exclude_build = True
self.exclude_dir = ".*build.*"
self.match_dir = ""
- self.comment_match = "^\/[*] -+"
+ self.comment_match = "^\/\* -+"
self.starting_dir = os.getcwd()
for key, value in kwargs.items():
setattr(self, key, value)
@@ -72,8 +69,9 @@ class HeaderUpdate(object):
def _traverseDir(self, path, fileOperation):
for subdir, dirs, files in os.walk(os.getcwd()):
- if (not re.match(self.exclude_dir, subdir)) and \
- (re.match(self.match_dir, subdir)):
+ if (re.match(self.match_dir, subdir)) and \
+ (not re.match(self.exclude_dir, subdir)):
+ print(subdir)
for fileName in files:
if (re.match(self.match_re, fileName)) and \
(not re.match(self.exclude_re, fileName)):
@@ -84,9 +82,8 @@ class HeaderUpdate(object):
def main(argv):
- update = HeaderUpdate(exclude_re="^picopng[.]cpp",
- exclude_dir=".*build.*|.*google.*",
- match_dir=".*src.*|.*include.*|.*test.*")
+ update = HeaderUpdate(exclude_re="^picopng\.cpp",
+ match_dir=".*yage|.*tests")
update.writeHeader()
diff --git a/scripts/update_docs b/scripts/update_docs
index 19e47145..cf117f73 100755
--- a/scripts/update_docs
+++ b/scripts/update_docs
@@ -3,17 +3,15 @@
if [[ "$TRAVIS_BRANCH" == "master" ]]; then
doxygen ./docs/Doxyfile
if [ -d "$TRAVIS_BUILD_DIR/html" ]; then
- cd $TRAVIS_BUILD_DIR/html
- git init
+ git clone -b gh-pages "https://$GH_TOKEN@github.com/ymherklotz/YAGE.git" YAGE_gh-pages
+ cd YAGE_gh-pages
git config user.name "TravisBot"
git config user.email ""
- git remote add upstream "https://$GH_TOKEN@github.com/ymherklotz/YAGE.git"
- git fetch upstream
- git reset upstream/gh-pages
- touch .
+ rm -rf *
+ cp -r ../html/* .
git add -A .
git commit -m "Rebuilding documentation"
- git push -q upstream HEAD:gh-pages
+ git push -q origin gh-pages
cd ..
fi
fi
diff --git a/tests/particlebodytest.cpp b/tests/particlebodytest.cpp
index 4b8c92b2..41c9a5c9 100644
--- a/tests/particlebodytest.cpp
+++ b/tests/particlebodytest.cpp
@@ -6,8 +6,8 @@
* ----------------------------------------------------------------------------
*/
-#include <yage/yage.h>
#include <gtest/gtest.h>
+#include <yage/yage.h>
#include <cmath>
#include <cstdlib>
diff --git a/tests/resources/floor_atlas.json b/tests/resources/floor_atlas.json
index a4c30f3d..ff9f0aa1 100644
--- a/tests/resources/floor_atlas.json
+++ b/tests/resources/floor_atlas.json
@@ -125,7 +125,7 @@
"bog_green2.png": {
"x": 128,
"y": 32,
- "width": 32,
+n "width": 32,
"height": 32
},
"floor_sand_stone3.png": {
@@ -1023,4 +1023,4 @@
"height": 32
}
}
-} \ No newline at end of file
+}
diff --git a/tests/simplegame.cpp b/tests/simplegame.cpp
new file mode 100644
index 00000000..f6565fc9
--- /dev/null
+++ b/tests/simplegame.cpp
@@ -0,0 +1,16 @@
+#include <yage.cpp>
+
+using namespace yage;
+
+int main()
+{
+ Window window;
+
+ window.create("Simple Game", 800, 640);
+
+ while(!window.shouldClose()) {
+ SpriteBatch sp;
+ sp.begin();
+ window.pollEvents();
+ }
+}
diff --git a/tests/spritesheettest.cpp b/tests/spritesheettest.cpp
index 84c4c1cc..47469cf9 100644
--- a/tests/spritesheettest.cpp
+++ b/tests/spritesheettest.cpp
@@ -1,3 +1,13 @@
+/* ----------------------------------------------------------------------------
+ * spritesheettest.cpp
+ *
+ * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
+ * MIT License, see LICENSE file for more details.
+ * ----------------------------------------------------------------------------
+ */
+
+/** @file */
+
#include <gtest/gtest.h>
#include <yage/yage.h>
@@ -5,12 +15,8 @@ using namespace yage;
TEST(SpriteSheet, Load)
{
- yage::init();
- Window window;
- window.create("SpriteSheet test", 800, 640);
- window.hide();
- SpriteSheet("resources/floor_atlas.png", "resources/floor_atlas.json");
- yage::quit();
+ /// @todo add a test to test the spritesheet loading
+ ASSERT_TRUE(true);
}
int main(int argc, char **argv)
diff --git a/tests/vector3test.cpp b/tests/vector3test.cpp
new file mode 100644
index 00000000..b618f2fc
--- /dev/null
+++ b/tests/vector3test.cpp
@@ -0,0 +1,40 @@
+/* ----------------------------------------------------------------------------
+ * vector3test.cpp
+ *
+ * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
+ * MIT License, see LICENSE file for more details.
+ * ----------------------------------------------------------------------------
+ */
+
+#include <gtest/gtest.h>
+#include <yage/yage.h>
+
+#include <ctime>
+#include <cstdlib>
+
+using namespace yage;
+
+TEST(Vector4, Initialize)
+{
+ Vector3i vec{{rand(), rand(), rand()}};
+ ASSERT_EQ(vec.x, vec[0]);
+ ASSERT_EQ(vec.y, vec[1]);
+ ASSERT_EQ(vec.z, vec[2]);
+}
+
+TEST(Vector3, Assigning)
+{
+ Vector3i vec{{rand(), rand(), rand()}};
+ ASSERT_EQ(vec.x, vec[0]);
+ vec.x = rand();
+ ASSERT_EQ(vec.x, vec[0]);
+ vec[0] = rand();
+ ASSERT_EQ(vec.x, vec[0]);
+}
+
+int main(int argc, char **argv)
+{
+ testing::InitGoogleTest(&argc, argv);
+ srand(time(nullptr));
+ return RUN_ALL_TESTS();
+}
diff --git a/tests/vector4test.cpp b/tests/vector4test.cpp
new file mode 100644
index 00000000..cc2418f4
--- /dev/null
+++ b/tests/vector4test.cpp
@@ -0,0 +1,67 @@
+/* ----------------------------------------------------------------------------
+ * vector4test.cpp
+ *
+ * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
+ * MIT License, see LICENSE file for more details.
+ * ----------------------------------------------------------------------------
+ */
+
+#include <gtest/gtest.h>
+#include <yage/yage.h>
+
+#include <cstdlib>
+#include <ctime>
+
+using namespace yage;
+
+TEST(Vector4, Initialize)
+{
+ Vector4i vec{{rand(), rand(), rand(), rand()}};
+ ASSERT_EQ(vec.x, vec[0]);
+ ASSERT_EQ(vec.y, vec[1]);
+ ASSERT_EQ(vec.z, vec[2]);
+ ASSERT_EQ(vec.w, vec[3]);
+}
+
+TEST(Vector4, Assigning_x)
+{
+ Vector4i vec{{rand(), rand(), rand(), rand()}};
+ vec.x = rand();
+ ASSERT_EQ(vec.x, vec[0]);
+ vec[0] = rand();
+ ASSERT_EQ(vec.x, vec[0]);
+}
+
+TEST(Vector4, Assigning_y)
+{
+ Vector4i vec{{rand(), rand(), rand(), rand()}};
+ vec.y = rand();
+ ASSERT_EQ(vec.y, vec[1]);
+ vec[1] = rand();
+ ASSERT_EQ(vec.y, vec[1]);
+}
+
+TEST(Vector4, Assigning_z)
+{
+ Vector4i vec{{rand(), rand(), rand(), rand()}};
+ vec.z = rand();
+ ASSERT_EQ(vec.z, vec[2]);
+ vec[2] = rand();
+ ASSERT_EQ(vec.z, vec[2]);
+}
+
+TEST(Vector4, Assigning_w)
+{
+ Vector4i vec{{rand(), rand(), rand(), rand()}};
+ vec.w = rand();
+ ASSERT_EQ(vec.w, vec[3]);
+ vec[3] = rand();
+ ASSERT_EQ(vec.w, vec[3]);
+}
+
+int main(int argc, char **argv)
+{
+ testing::InitGoogleTest(&argc, argv);
+ srand(time(nullptr));
+ return RUN_ALL_TESTS();
+}
diff --git a/yage/CMakeLists.txt b/yage/CMakeLists.txt
index e9d4071b..d7df4a94 100644
--- a/yage/CMakeLists.txt
+++ b/yage/CMakeLists.txt
@@ -1,9 +1,5 @@
cmake_policy(SET CMP0048 NEW)
-project(yage
- VERSION 0.1.1.0
- LANGUAGES CXX)
-
include(base/CMakeLists.txt)
include(physics/CMakeLists.txt)
include(math/CMakeLists.txt)
diff --git a/yage/base/resourcemanager.h b/yage/base/resourcemanager.h
index 3c5081c4..52d98e12 100644
--- a/yage/base/resourcemanager.h
+++ b/yage/base/resourcemanager.h
@@ -1,3 +1,4 @@
+
/* ----------------------------------------------------------------------------
* resourcemanager.h
*
@@ -24,6 +25,7 @@ private:
public:
static Texture getTexture(const std::string &texture_path);
+
};
} // namespace yage
diff --git a/yage/base/spritebatch.cpp b/yage/base/spritebatch.cpp
index 3aec8153..aaeec32f 100644
--- a/yage/base/spritebatch.cpp
+++ b/yage/base/spritebatch.cpp
@@ -61,8 +61,8 @@ void SpriteBatch::end()
createRenderBatches();
}
-void SpriteBatch::draw(const glm::vec4 &destination_rect,
- const glm::vec4 &uv_rect, GLuint texture,
+void SpriteBatch::draw(const yage::Vector4f &destination_rect,
+ const yage::Vector4f &uv_rect, GLuint texture,
const Color &color, float depth)
{
Vertex top_left, top_right, bottom_right, bottom_left;
diff --git a/yage/base/spritebatch.h b/yage/base/spritebatch.h
index 3b4aca76..953055f6 100644
--- a/yage/base/spritebatch.h
+++ b/yage/base/spritebatch.h
@@ -6,8 +6,8 @@
* ----------------------------------------------------------------------------
*/
-/** @file
- */
+/** @file
+ */
#ifndef YAGE_SPRITE_BATCH_H
#define YAGE_SPRITE_BATCH_H
@@ -16,6 +16,7 @@
#include <glad/glad.h>
#include <glm/glm.hpp>
+#include <yage/math/matrix.h>
#include <vector>
@@ -92,8 +93,9 @@ public:
void begin();
void end();
// adds a sprite to the sprite batch to be rendered later
- void draw(const glm::vec4 &destination_rect, const glm::vec4 &uv_rect,
- GLuint texture, const Color &color, float depth);
+ void draw(const yage::Vector4f &destination_rect,
+ const yage::Vector4f &uv_rect, GLuint texture, const Color &color,
+ float depth);
// render the batch
void render();
diff --git a/yage/base/spritesheet.cpp b/yage/base/spritesheet.cpp
index d53f64aa..5c3499cc 100644
--- a/yage/base/spritesheet.cpp
+++ b/yage/base/spritesheet.cpp
@@ -6,6 +6,9 @@
* ----------------------------------------------------------------------------
*/
+/** @file
+ */
+
#include "spritesheet.h"
#include <cassert>
@@ -25,7 +28,15 @@ namespace yage
SpriteSheet::SpriteSheet(string pngFileName, string jsonFileName)
{
- string fileContents = fileContent(jsonFileName);
+ int jsonWidth, jsonHeight;
+ fileLocations_ =
+ parseJson(jsonWidth, jsonHeight, fileContent(jsonFileName));
+ texture_ = ImageLoader::loadPng(pngFileName);
+
+ if (texture_.width != jsonWidth)
+ throw runtime_error("JSON width does not match texture width");
+ if (texture_.height != jsonHeight)
+ throw runtime_error("JSON height does not match texture height");
}
string SpriteSheet::fileContent(string jsonFileName) const
@@ -38,7 +49,8 @@ string SpriteSheet::fileContent(string jsonFileName) const
return stream.str();
}
-SpriteMap SpriteSheet::parseJson(int &width, int &height, const string &jsonContent) const
+SpriteMap SpriteSheet::parseJson(int &width, int &height,
+ string jsonContent) const
{
SpriteMap spriteMap;
Document jsonAtlas;
@@ -48,10 +60,23 @@ SpriteMap SpriteSheet::parseJson(int &width, int &height, const string &jsonCont
height = jsonAtlas["height"].GetInt();
for (auto &texture : jsonAtlas["sprites"].GetObject()) {
- spriteMap[texture.name.GetString()] = Coordinate();
+ Coordinate coord;
for (auto &value : texture.value.GetObject()) {
- /// @todo add the coordinate to the map
+ string keyName{value.value.GetString()};
+ int keyValue{value.value.GetInt()};
+ if (keyName == "x") {
+ coord.x = keyValue;
+ } else if (keyName == "y") {
+ coord.y = keyValue;
+ } else if (keyName == "width") {
+ coord.width = keyValue;
+ } else if (keyName == "height") {
+ coord.height = keyValue;
+ } else {
+ throw runtime_error("JSON key incorrect: " + keyName);
+ }
}
+ spriteMap[texture.name.GetString()] = coord;
}
return spriteMap;
diff --git a/yage/base/spritesheet.h b/yage/base/spritesheet.h
index d44e1dc0..2ead0ba1 100644
--- a/yage/base/spritesheet.h
+++ b/yage/base/spritesheet.h
@@ -6,6 +6,9 @@
* ----------------------------------------------------------------------------
*/
+/** @file
+ */
+
#ifndef YAGE_SPRITESHEET_H
#define YAGE_SPRITESHEET_H
@@ -52,7 +55,8 @@ private:
Texture texture_;
details::SpriteMap fileLocations_;
- details::SpriteMap parseJson(int &width, int &height, const std::string &jsonContent) const;
+ details::SpriteMap parseJson(int &width, int &height,
+ std::string jsonContent) const;
};
} // namespace yage
diff --git a/yage/base/texturecache.cpp b/yage/base/texturecache.cpp
index 5d2950a3..628f3604 100644
--- a/yage/base/texturecache.cpp
+++ b/yage/base/texturecache.cpp
@@ -12,8 +12,6 @@
namespace yage
{
-TextureCache::TextureCache() = default;
-
Texture TextureCache::getTexture(const std::string &texture_path)
{
auto itr = texture_map_.find(texture_path);
diff --git a/yage/base/texturecache.h b/yage/base/texturecache.h
index 414c9ec3..b28349ec 100644
--- a/yage/base/texturecache.h
+++ b/yage/base/texturecache.h
@@ -22,9 +22,10 @@ private:
std::unordered_map<std::string, Texture> texture_map_;
public:
- TextureCache();
+ TextureCache() = default;
Texture getTexture(const std::string &texture_path);
+ Texture getTextureFromSpriteSheet();
};
} // namespace yage
diff --git a/yage/base/window.cpp b/yage/base/window.cpp
index 38056b14..5ac2d8dc 100644
--- a/yage/base/window.cpp
+++ b/yage/base/window.cpp
@@ -18,13 +18,17 @@ Window::Window() = default;
Window::~Window()
{
glfwDestroyWindow(window_);
+ glfwTerminate();
}
-void Window::create(const std::string &window_name, int width, int height,
- unsigned)
+void Window::create(std::string window_name, int width, int height)
{
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
+ if(glfwInit() == GLFW_FALSE) {
+ throw std::runtime_error("GLFW Initialisation failed");
+ }
+
+ glfwWindowHint(GLFW_VERSION_MAJOR, 4);
+ glfwWindowHint(GLFW_VERSION_MINOR, 5);
window_ =
glfwCreateWindow(width, height, window_name.c_str(), nullptr, nullptr);
@@ -46,6 +50,8 @@ void Window::create(const std::string &window_name, int width, int height,
// set alpha blending
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ // set the clear depth
+ glClearDepth(1.f);
}
void Window::swapBuffer()
@@ -56,8 +62,6 @@ void Window::swapBuffer()
void Window::clearBuffer()
{
- // set the clear depth
- glClearDepth(1.f);
// clears buffer with clear color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
@@ -72,4 +76,14 @@ void Window::show()
glfwShowWindow(window_);
}
+bool Window::shouldClose()
+{
+ return glfwWindowShouldClose(window_);
+}
+
+void Window::pollEvents() const
+{
+ glfwPollEvents();
+}
+
} // namespace yage
diff --git a/yage/base/window.h b/yage/base/window.h
index 3f448132..84ba8303 100644
--- a/yage/base/window.h
+++ b/yage/base/window.h
@@ -47,8 +47,7 @@ public:
Window &operator=(Window &&) = delete;
/// create the window, initialize the handle and update the width and height
- void create(const std::string &window_name, int width, int height,
- unsigned flags = WindowFlags::SHOWN);
+ void create(std::string window_name, int width, int height);
/// swap the buffer
void swapBuffer();
/// clear buffer
@@ -57,6 +56,8 @@ public:
void hide();
/// show window
void show();
+ bool shouldClose();
+ void pollEvents() const;
};
} // namespace yage
diff --git a/yage/math/matrix.h b/yage/math/matrix.h
index 3df1509d..d6d281d3 100644
--- a/yage/math/matrix.h
+++ b/yage/math/matrix.h
@@ -7,7 +7,7 @@
*/
/** @file
- */
+ */
#ifndef YAGE_MATH_MATRIX_H
#define YAGE_MATH_MATRIX_H
@@ -26,23 +26,23 @@ template <int Rows, int Cols, class Type>
class Matrix;
/** @internal Namespace for internal details.
- *
- * Details Namespace
- * ================
- *
- * This is the namespace used for implementation details.
- */
+ *
+ * Details Namespace
+ * ================
+ *
+ * This is the namespace used for implementation details.
+ */
namespace details
{
/** @internal Internal Row class used by the Matrix class to return the
- * internal data structure of the Matrix.
- *
- * Row
- * ===
- *
- * Internal Row class to return a value in the row of the matrix.
- */
+ * internal data structure of the Matrix.
+ *
+ * Row
+ * ===
+ *
+ * Internal Row class to return a value in the row of the matrix.
+ */
template <int Rows, int Cols, class Type>
class Row
{
@@ -71,7 +71,7 @@ public:
} // namespace details
/** Base Matrix class used by other similar classes.
- */
+ */
template <int Rows = 4, int Cols = 4, class Type = double>
class Matrix
{
@@ -94,10 +94,10 @@ public:
int colSize() const { return Cols; }
/** Return the row specified row as a Matrix with only one row.
- *
- * @param row Row number to be returned.
- * @return The row that is specified by the row variables.
- */
+ *
+ * @param row Row number to be returned.
+ * @return The row that is specified by the row variables.
+ */
Matrix<1, Cols, Type> getRow(int row) const
{
Matrix<1, Cols, Type> rowMatrix;
@@ -108,10 +108,10 @@ public:
}
/** Get a specific column in a column vector.
- *
- * @param col Column number to be returned.
- * @return Column Matrix of the selected column.
- */
+ *
+ * @param col Column number to be returned.
+ * @return Column Matrix of the selected column.
+ */
Matrix<Rows, 1, Type> getCol(int col) const
{
Matrix<Rows, 1, Type> colMatrix;
@@ -122,23 +122,23 @@ public:
}
/** Iterator support for the start.
- *
- * @return Iterator pointing to the start of the data.
- */
+ *
+ * @return Iterator pointing to the start of the data.
+ */
typename std::vector<Type>::iterator begin() { return data_.begin(); }
/** Iterator support for the end.
- *
- * @return Iterator pointing to the end of the data.
- */
+ *
+ * @return Iterator pointing to the end of the data.
+ */
typename std::vector<Type>::iterator end() { return data_.end(); }
/** Prints out the matrix, but can also be implemented by other classes to
- * print data differently.
- *
- * @bug When printing certain matrices, it omits a row or column. Still
- * need to determine under which conditions.
- */
+ * print data differently.
+ *
+ * @bug When printing certain matrices, it omits a row or column. Still
+ * need to determine under which conditions.
+ */
virtual std::string toString() const
{
std::stringstream ss;
@@ -166,7 +166,7 @@ public:
details::Row<Rows, Cols, Type> operator[](int row) const
{
return details::Row<Rows, Cols, Type>((Matrix<Rows, Cols, Type> *)this,
- row);
+ row);
}
Matrix<Rows, Cols, Type> &operator+=(const Matrix<Rows, Cols, Type> &rhs)
@@ -320,10 +320,10 @@ public:
};
/** 2D Vector class.
- *
- * Two dimensional vector class.
- */
-template <class Type = double>
+ *
+ * Two dimensional vector class.
+ */
+template <typename Type = double>
class Vector2 : public Vector<2, Type>
{
public:
@@ -339,30 +339,90 @@ public:
Vector2<Type>(const Matrix<2, 1, Type> &other) : Vector<2, Type>(other) {}
Type &x() { return this->data_[0]; }
-
const Type &x() const { return this->data_[0]; }
Type &y() { return this->data_[1]; }
-
const Type &y() const { return this->data_[1]; }
};
+/** 3D Vector class.
+ *
+ * Two dimensional vector class.
+ */
+template <typename Type = double>
+class Vector3 : public Vector<3, Type>
+{
+public:
+ Type &x, &y, &z;
+
+ Vector3<Type>() : Vector<4, Type>() {}
+
+ Vector3<Type>(std::vector<Type> data)
+ : Vector<3, Type>(data), x(this->data_[0]), y(this->data_[1]),
+ z(this->data_[2])
+ {
+ }
+
+ Vector3<Type>(Type x_in, Type y_in, Type z_in)
+ : Vector<3, Type>({x_in, y_in, z_in}), x(this->data_[0]),
+ y(this->data_[1]), z(this->data_[2])
+ {
+ }
+};
+
+/** 4D Vector class
+ */
+template <typename Type = double>
+class Vector4 : public Vector<4, Type>
+{
+public:
+ Type &x, &y, &z, &w;
+
+ Vector4<Type>() : Vector<4, Type>() {}
+
+ Vector4<Type>(std::vector<Type> data)
+ : Vector<4, Type>(data), x(this->data_[0]), y(this->data_[1]),
+ z(this->data_[2]), w(this->data_[3])
+ {
+ }
+
+ Vector4<Type>(Type x_in, Type y_in, Type z_in, Type w_in)
+ : Vector<4, Type>({x_in, y_in, z_in, w_in}), x(this->data_[0]),
+ y(this->data_[1]), z(this->data_[2]), w(this->data[3])
+ {
+ }
+};
+
/** Definition of a 2D vector.
- */
+ */
using Vector2d = Vector2<double>;
+using Vector2f = Vector2<float>;
+using Vector2i = Vector2<int>;
+
+/** Definition of a 3D vector.
+ */
+using Vector3d = Vector3<double>;
+using Vector3f = Vector3<float>;
+using Vector3i = Vector3<int>;
+
+/** Definition of a 4D vector
+ */
+using Vector4d = Vector4<double>;
+using Vector4f = Vector4<float>;
+using Vector4i = Vector4<int>;
/** Namespace containing functions that operate on matrices.
- *
- * Implementations defined here are meant to operate on anything that inherits
- * from the base Matrix class.
- */
+ *
+ * Implementations defined here are meant to operate on anything that inherits
+ * from the base Matrix class.
+ */
namespace matrix
{
/** Transposes a matrix and returns the result
- *
- * @param m input matrix.
- */
+ *
+ * @param m input matrix.
+ */
template <int M, int N, class T>
Matrix<N, M, T> transpose(const Matrix<M, N, T> &m)
{
@@ -376,9 +436,9 @@ Matrix<N, M, T> transpose(const Matrix<M, N, T> &m)
}
/** Returns the dot product between two vectors
- *
- * @param m1,m2 Input matrices.
- */
+ *
+ * @param m1,m2 Input matrices.
+ */
template <int R, class T>
T dot(const Matrix<R, 1, T> &m1, const Matrix<R, 1, T> &m2)
{
@@ -390,11 +450,11 @@ T dot(const Matrix<R, 1, T> &m1, const Matrix<R, 1, T> &m2)
}
/** Multiplies two matrices together.
- *
- * @param m1,m2 Matrix inputs
- *
- * Requires the two matrices to be compatible with multiplication.
- */
+ *
+ * @param m1,m2 Matrix inputs
+ *
+ * Requires the two matrices to be compatible with multiplication.
+ */
template <int M, int N, int P, int Q, class T>
Matrix<M, Q, T> multiply(const Matrix<M, N, T> &m1, const Matrix<P, Q, T> &m2)
{