|
| 1 | +/** |
| 2 | + * @file apply_transformation.cpp |
| 3 | + * @author Xuhua Huang |
| 4 | + * @brief Uses raw matrix to demonstrate the concept of applying a transformation matrix to a point in 3D space. |
| 5 | + * Also note that transformation is a linear operation, so it can be represented as a matrix multiplication followed by |
| 6 | + * a vector addition. |
| 7 | + * |
| 8 | + * @version 0.1 |
| 9 | + * @date 2025-01-20 |
| 10 | + * |
| 11 | + * @copyright Copyright (c) 2025 |
| 12 | + * |
| 13 | + */ |
| 14 | + |
| 15 | +#include <array> |
| 16 | +#include <cmath> |
| 17 | +#include <iostream> |
| 18 | + |
| 19 | +// Define a 3x3 rotation matrix and a translation vector |
| 20 | +using Matrix3x3 = std::array<std::array<double, 3>, 3>; |
| 21 | +using Vector3 = std::array<double, 3>; |
| 22 | + |
| 23 | +Vector3 applyTransformation(const Matrix3x3& rotation, const Vector3& translation, const Vector3& point) |
| 24 | +{ |
| 25 | + Vector3 transformed_point = { |
| 26 | + rotation[0][0] * point[0] + rotation[0][1] * point[1] + rotation[0][2] * point[2] + translation[0], |
| 27 | + rotation[1][0] * point[0] + rotation[1][1] * point[1] + rotation[1][2] * point[2] + translation[1], |
| 28 | + rotation[2][0] * point[0] + rotation[2][1] * point[1] + rotation[2][2] * point[2] + translation[2] |
| 29 | + }; |
| 30 | + return transformed_point; |
| 31 | +} |
| 32 | + |
| 33 | +int main() |
| 34 | +{ |
| 35 | + // Example rotation matrix (identity, no rotation) |
| 36 | + Matrix3x3 rotation = { |
| 37 | + {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}} |
| 38 | + }; |
| 39 | + // Example translation vector |
| 40 | + Vector3 translation = {1, 2, 3}; |
| 41 | + // Example point |
| 42 | + Vector3 point = {4, 5, 6}; |
| 43 | + |
| 44 | + // Apply transformation |
| 45 | + Vector3 transformed_point = applyTransformation(rotation, translation, point); |
| 46 | + |
| 47 | + // Output the result |
| 48 | + std::cout << "Transformed point: (" << transformed_point[0] << ", " << transformed_point[1] << ", " |
| 49 | + << transformed_point[2] << ")\n"; |
| 50 | + |
| 51 | + return 0; |
| 52 | +} |
0 commit comments