Skip to content

Commit

Permalink
Port classes and features from other projects
Browse files Browse the repository at this point in the history
  • Loading branch information
csparker247 committed Mar 21, 2022
1 parent edf9844 commit 8d1b806
Show file tree
Hide file tree
Showing 29 changed files with 1,504 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ CheckOptions:
- key: readability-identifier-naming.ConstexprMethodCase
value: camelBack
- key: readability-identifier-naming.ConstexprVariableCase
value: UPPER_CASE
value: lower_case
- key: readability-identifier-naming.EnumCase
value: CamelCase
- key: readability-identifier-naming.EnumConstantCase
Expand Down
8 changes: 4 additions & 4 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@
- *test_script

### Tests ###
test:debian:10:
test:debian:11:
extends: .build_and_test
stage: test
needs: []
image: volcart/vcbuilder-debian:10_v1.static
image: volcart/vcbuilder-debian:11_v1.static
variables:
EXTRA_CMAKE_FLAGS: "-DEDUCE_CORE_BUILD_TESTS=ON"
tags:
- docker

examples:debian:10:
examples:debian:11:
extends: .build
stage: test
needs: []
image: volcart/vcbuilder-debian:10_v1.static
image: volcart/vcbuilder-debian:11_v1.static
variables:
EXTRA_CMAKE_FLAGS: "-DEDUCE_CORE_BUILD_EXAMPLES=ON"
tags:
Expand Down
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,27 @@ include(CheckToNumericFP)
set(public_hdrs
include/educelab/core.hpp
include/educelab/core/Version.hpp
include/educelab/core/io/ImageIO.hpp
include/educelab/core/types/Color.hpp
include/educelab/core/types/Image.hpp
include/educelab/core/types/Mat.hpp
include/educelab/core/types/Mesh.hpp
include/educelab/core/types/Signals.hpp
include/educelab/core/types/Uuid.hpp
include/educelab/core/types/Vec.hpp
include/educelab/core/utils/Filesystem.hpp
include/educelab/core/utils/Iteration.hpp
include/educelab/core/utils/LinearAlgebra.hpp
include/educelab/core/utils/Math.hpp
include/educelab/core/utils/String.hpp
)

configure_file(src/Version.cpp.in Version.cpp)
set(srcs
${public_hdrs}
${CMAKE_CURRENT_BINARY_DIR}/Version.cpp
src/Image.cpp
src/ImageIO.cpp
src/Uuid.cpp
)

Expand Down
57 changes: 56 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ cmake -S . -B build/ -DCMAKE_BUILD_TYPE=Release
```

## Installation
Follow the build instrcutions above, then run the following command from the root of the source directory:
Follow the build instructions above, then run the following command from the root of the source directory:

```shell
# Install the library to the system
Expand All @@ -36,9 +36,21 @@ following files can be installed in this way:
- `utils/Iteration.hpp`
- `utils/Math.hpp`
- `utils/String.hpp`
- `utils/Filesystem.hpp`
- Requires:
- `utils/String.hpp`
- `utils/LinearAlgebra.hpp`
- Requires:
- `utils/Math.hpp`
- `MatrixType` and `VectorType` which implement the `Mat` and `Vec`
interfaces.
- `types/Signals.hpp`
- `types/Vec.hpp`
- Requires:
- `utils/Math.hpp`
- `types/Mat.hpp`
- Requires:
- `types/Vec.hpp`
- `types/Color.hpp`
- Requires:
- `types/Vec.hpp`
Expand Down Expand Up @@ -69,6 +81,49 @@ std::cout << v0.cross(v1) << "\n"; // "[0, 0, 1]"
See [examples/VecExample.cpp](examples/VecExample.cpp) for more usage
examples.
### Dense 2D matrix class
```c++
// Input point
Vec<float, 4> p{0, 0, 0, 1};
std::cout << p << "\n"; // [0, 0, 0, 1]
// Construct a translation matrix
auto M = Matrix::Eye();
M(0, 3) = 1.f;
M(1, 3) = 2.f;
M(2, 3) = 3.f;
std::cout << "\n" << M << "\n"; // [[1, 0, 0, 1]
// [0, 1, 0, 2]
// [0, 0, 1, 3]
// [0, 0, 0, 1]]
// Apply transform
p = translate * p;
std::cout << p << "\n"; // [1, 2, 3, 1]
```

See [examples/MatExample.cpp](examples/MatExample.cpp) for more usage
examples.

### Image class
```c++
#include <educelab/core/types/Mat.hpp>

// Construct an image
Image image(600, 800, 3, Depth::F32);

// Fill image with a color gradient
for (const auto [y, x] : range2D(image.height(), image.width())) {
auto r = float(x) / float(image.width() - 1);
auto g = float(y) / float(image.height() - 1);
auto b = 0.25F;
image.at<Vec3f>(y, x) = {r, g, b};
}
```
See [examples/ImageExample.cpp](examples/ImageExample.cpp) for more usage
examples.
### Iteration utilities
```c++
#include <educelab/core/utils/Iteration.hpp>
Expand Down
37 changes: 37 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,43 @@ std::cout << v0 + v1 << "\n"; // "[1, 1, 0]"
std::cout << v0.cross(v1) << "\n"; // "[0, 0, 1]"
```

### Dense 2D matrix class
```{.cpp}
// Input point
Vec<float, 4> p{0, 0, 0, 1};
std::cout << p << "\n"; // [0, 0, 0, 1]
// Construct a translation matrix
auto M = Matrix::Eye();
M(0, 3) = 1.f;
M(1, 3) = 2.f;
M(2, 3) = 3.f;
std::cout << "\n" << M << "\n"; // [[1, 0, 0, 1]
// [0, 1, 0, 2]
// [0, 0, 1, 3]
// [0, 0, 0, 1]]
// Apply transform
p = translate * p;
std::cout << p << "\n"; // [1, 2, 3, 1]
```

### Image class
```{.cpp}
#include <educelab/core/types/Mat.hpp>
// Construct an image
Image image(600, 800, 3, Depth::F32);
// Fill image with a color gradient
for (const auto [y, x] : range2D(image.height(), image.width())) {
auto r = float(x) / float(image.width() - 1);
auto g = float(y) / float(image.height() - 1);
auto b = 0.25F;
image.at<Vec3f>(y, x) = {r, g, b};
}
```

### Iteration utilities
```{.cpp}
#include <educelab/core/utils/Iteration.hpp>
Expand Down
5 changes: 5 additions & 0 deletions docs/modules.dox
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
* @brief EduceLab Core library namespace
*/

/**
* @namespace educelab::linalg
* @brief Linear algebra library
*/

/**
* @namespace educelab::traits
* @brief Class traits
Expand Down
1 change: 1 addition & 0 deletions docs/pages/.empty_dir
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a placeholder file for an empty directory.
8 changes: 7 additions & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,10 @@ add_executable(educelab_core_UuidExample UuidExample.cpp)
target_link_libraries(educelab_core_UuidExample educelab::core)

add_executable(educelab_core_VecExample VecExample.cpp)
target_link_libraries(educelab_core_VecExample educelab::core)
target_link_libraries(educelab_core_VecExample educelab::core)

add_executable(educelab_core_ImageExample ImageExample.cpp)
target_link_libraries(educelab_core_ImageExample educelab::core)

add_executable(educelab_core_MatExample MatExample.cpp)
target_link_libraries(educelab_core_MatExample educelab::core)
37 changes: 37 additions & 0 deletions examples/ImageExample.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "educelab/core/io/ImageIO.hpp"
#include "educelab/core/types/Image.hpp"
#include "educelab/core/types/Vec.hpp"
#include "educelab/core/utils/Iteration.hpp"

using namespace educelab;

auto main() -> int
{
// Construct an image
Image image(600, 800, 3, Depth::F32);

// Fill image with a color gradient
for (const auto [y, x] : range2D(image.height(), image.width())) {
// Create RGB values
auto r = float(x) / float(image.width() - 1);
auto g = float(y) / float(image.height() - 1);
auto b = 0.25F;

// Assign value to the correct pixel. Like its equivalent in OpenCV,
// Image.at gives you access to the raw image buffer memory,
// reinterpreted as the type you request. Make sure you know the data
// type and channels when you access a pixel!
image.at<Vec3f>(y, x) = {r, g, b};
}

// (Optional) Apply gamma correction
image = Image::Gamma(image);

// (Optional) Convert image to the correct format for output. write_image()
// will automatically convert if the output format doesn't support the
// given bit depth, but it's always good to control the conversion.
image = image.convert(Depth::U8);

// Write the image
write_image("educelab_core_ImageExample.ppm", image);
}
55 changes: 55 additions & 0 deletions examples/MatExample.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <cmath>
#include <iostream>

#include "educelab/core/types/Mat.hpp"
#include "educelab/core/types/Vec.hpp"
#include "educelab/core/utils/Iteration.hpp"
#include "educelab/core/utils/Math.hpp"

using namespace educelab;

auto main() -> int
{
// Type aliases
using Matrix = Mat<4, 4, float>;
using Point = Vec<float, 4>;

// Input point
Point p{0, 0, 0, 1};
std::cout << "Starting point: " << p << "\n\n";

// Translate 1 unit along x
auto translate = Matrix::Eye();
translate(0, 3) = 1.f;
p = translate * p;
std::cout << "Translation matrix:\n" << translate << "\n";
std::cout << "After translation: " << p << "\n\n";

// Rotate around z
auto theta = to_radians(90.F);
auto rotate = Matrix::Eye();
rotate(0, 0) = std::cos(theta);
rotate(0, 1) = -std::sin(theta);
rotate(1, 0) = std::sin(theta);
rotate(1, 1) = std::cos(theta);
p = rotate * p;
std::cout << "Rotation matrix:\n" << rotate << "\n";
std::cout << "After rotation: " << p << "\n\n";

// Scale by 10
auto scale = Matrix::Eye();
for (auto i : range(3)) {
scale(i, i) = 10.F;
}
p = scale * p;
std::cout << "Scale matrix:\n" << scale << "\n";
std::cout << "After scale: " << p << "\n\n";

// Restore original point and apply a single multiplication
p = {0, 0, 0, 1};
auto transform = scale * rotate * translate;
std::cout << "Restored starting point: " << p << "\n";
p = transform * p;
std::cout << "Transform matrix:\n" << transform << "\n";
std::cout << "After transform: " << p << "\n";
}
6 changes: 6 additions & 0 deletions include/educelab/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@

#include "educelab/core/Version.hpp"

#include "educelab/core/io/ImageIO.hpp"

#include "educelab/core/types/Color.hpp"
#include "educelab/core/types/Image.hpp"
#include "educelab/core/types/Mat.hpp"
#include "educelab/core/types/Mesh.hpp"
#include "educelab/core/types/Signals.hpp"
#include "educelab/core/types/Uuid.hpp"
#include "educelab/core/types/Vec.hpp"

#include "educelab/core/utils/Filesystem.hpp"
#include "educelab/core/utils/Iteration.hpp"
#include "educelab/core/utils/LinearAlgebra.hpp"
#include "educelab/core/utils/Math.hpp"
#include "educelab/core/utils/String.hpp"
16 changes: 16 additions & 0 deletions include/educelab/core/io/ImageIO.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

/** @file */

#include <filesystem>
#include <fstream>

#include "educelab/core/types/Image.hpp"

namespace educelab
{

/** @brief Write an Image to disk */
void write_image(const std::filesystem::path& path, const Image& image);

} // namespace educelab
3 changes: 2 additions & 1 deletion include/educelab/core/types/Color.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ class Color
F32C3,
/** 32-bit float RGBA color */
F32C4,
/** Hexadecimal RGB color string of 3 or 6 digits (`#0a3` or `#00aa33`)
/**
* Hexadecimal RGB color string of 3 or 6 digits (`#0a3` or `#00aa33`)
*/
HexCode
};
Expand Down
Loading

0 comments on commit 8d1b806

Please sign in to comment.