mirror of
https://github.com/orange-cpp/omath.git
synced 2026-02-13 07:03:25 +00:00
Compare commits
114 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 29795e9906 | |||
| 0328fef4f7 | |||
| 687772f073 | |||
| c66df11754 | |||
| 0450dc3547 | |||
| 0bd9eda48f | |||
| 858314fa0a | |||
| f277b1038c | |||
| 15c055beb7 | |||
| 4d24815f3e | |||
| d96b0cd2f7 | |||
| d238bd137f | |||
| 17b150499d | |||
| c7228c9674 | |||
| 7a9f5d4966 | |||
| a16050242a | |||
| ea8f3d8d51 | |||
| 08d2ccc03a | |||
| 21ec23d77b | |||
| 2c4ff37062 | |||
| 695a8035b5 | |||
| d12b236e56 | |||
| 7a5090d9f6 | |||
| ec76a7239c | |||
| 2758f549a3 | |||
| 493931ef0f | |||
| 9e1990942b | |||
| f1984fbe46 | |||
| f1fbea21a7 | |||
| 4b44ce0667 | |||
| 231ef35a0a | |||
| 1aa62cb396 | |||
| 8e411771c2 | |||
| d65852d1a4 | |||
| 21f5e82a20 | |||
|
|
851ec37350 | ||
| f1cd9dbeb3 | |||
| 7a1c7d6cc4 | |||
| cb704b3621 | |||
|
|
646d295876 | ||
| 8e09556c25 | |||
| 7dbebc996d | |||
| 278ffba0ff | |||
| 647cf02a38 | |||
| 4be2986681 | |||
| 06d9b4c910 | |||
| a074fdcb92 | |||
|
|
8241d9c355 | ||
| 66258f0f6d | |||
| 65541fa2c7 | |||
| 7e4a6134bf | |||
| a54a537239 | |||
| f01bbde537 | |||
| 2c710555d6 | |||
| 9c2be6306c | |||
| d37840d4ef | |||
| b4a3b5d529 | |||
| c2a772142c | |||
| 76fca7f527 | |||
| 73ccd24e3e | |||
| a642feafb5 | |||
| faf9f34af8 | |||
| b56801ac91 | |||
| a81d12d480 | |||
| 17eb0cd0dc | |||
| 52024285d2 | |||
| f179aea4d7 | |||
| 1196bb86b4 | |||
| 5489c296e9 | |||
| 50ddf2d31e | |||
| a6e4c0461d | |||
| 6749f9f759 | |||
| 9a38d47b0d | |||
| f6f8bba032 | |||
| ce08fcdd29 | |||
| 49ea113348 | |||
| af21f9a946 | |||
| f096f7179f | |||
| e025e99d18 | |||
| 30eef59360 | |||
| c2a6cf5c47 | |||
| 7f55383bc1 | |||
| 449c60133c | |||
| 31d3359507 | |||
| dd738f365d | |||
| b5e788385d | |||
| df6d75e554 | |||
| be3fae63b8 | |||
| dd731b60c3 | |||
| b06fd00673 | |||
| 998c8f3a43 | |||
| faeef594b9 | |||
| 40a301186e | |||
| a41526c494 | |||
| a0d1dc4313 | |||
| 1c5c9360c8 | |||
| 4615769682 | |||
| 4ef674f7b4 | |||
| 69b9049fb0 | |||
| 2734b58bdd | |||
| d7f1f49165 | |||
| 94b1453cae | |||
| 3e67d8a99c | |||
| 8c00ab3d9d | |||
| d14cb1e93e | |||
| c692cf39e1 | |||
| 9c934c5d9c | |||
|
|
f8202b116d | ||
| 8bf0bb8e0d | |||
| a340766348 | |||
| 254674a62e | |||
| 97c2da893b | |||
| 0ce30a7038 | |||
| 492ddfd566 |
@@ -1,62 +1,64 @@
|
||||
# Generated from CLion C/C++ Code Style settings
|
||||
---
|
||||
Language: Cpp
|
||||
# Generated by CLion for Stroustrup
|
||||
# The Stroustrup style, named after Bjarne Stroustrup, the creator of C++, is similar to the K&R style but differs
|
||||
# in its treatment of the class definitions and the placement of braces in certain contexts. The opening brace is
|
||||
# placed on the same line as the control statement, and the closing brace is on its own line.
|
||||
BasedOnStyle: LLVM
|
||||
|
||||
AccessModifierOffset: -4
|
||||
AlignConsecutiveAssignments: false
|
||||
AlignConsecutiveDeclarations: false
|
||||
AlignOperands: true
|
||||
AlignConsecutiveAssignments: None
|
||||
AlignConsecutiveBitFields: None
|
||||
AlignConsecutiveDeclarations: None
|
||||
AlignConsecutiveMacros: AcrossEmptyLinesAndComments
|
||||
AlignTrailingComments: false
|
||||
AllowShortBlocksOnASingleLine: false
|
||||
AllowShortBlocksOnASingleLine: Never
|
||||
AllowShortFunctionsOnASingleLine: None
|
||||
AlwaysBreakTemplateDeclarations: Yes
|
||||
AllowShortIfStatementsOnASingleLine: false
|
||||
AllowShortLoopsOnASingleLine: false
|
||||
BreakTemplateDeclarations: Leave
|
||||
BreakBeforeBraces: Custom
|
||||
BraceWrapping:
|
||||
AfterCaseLabel: true
|
||||
AfterClass: true
|
||||
AfterControlStatement: true
|
||||
AfterEnum: true
|
||||
AfterFunction: true
|
||||
AfterControlStatement: true
|
||||
SplitEmptyFunction: true
|
||||
AfterEnum: true
|
||||
AfterNamespace: true
|
||||
AfterStruct: true
|
||||
AfterUnion: true
|
||||
AfterExternBlock: true
|
||||
BeforeCatch: false
|
||||
BeforeElse: false
|
||||
BeforeCatch: true
|
||||
BeforeElse: true
|
||||
BeforeLambdaBody: true
|
||||
BeforeWhile: false
|
||||
IndentBraces: false
|
||||
SplitEmptyFunction: true
|
||||
BeforeWhile: true
|
||||
SplitEmptyRecord: true
|
||||
SplitEmptyNamespace: true
|
||||
BreakBeforeBraces: Custom
|
||||
BreakConstructorInitializers: AfterColon
|
||||
BreakConstructorInitializersBeforeComma: false
|
||||
BreakBeforeBinaryOperators: NonAssignment
|
||||
BreakBeforeConceptDeclarations: false
|
||||
ColumnLimit: 120
|
||||
ConstructorInitializerAllOnOneLineOrOnePerLine: false
|
||||
ContinuationIndentWidth: 8
|
||||
IncludeCategories:
|
||||
- Regex: '^<.*'
|
||||
Priority: 1
|
||||
- Regex: '^".*'
|
||||
Priority: 2
|
||||
- Regex: '.*'
|
||||
Priority: 3
|
||||
IncludeIsMainRegex: '([-_](test|unittest))?$'
|
||||
IndentCaseLabels: true
|
||||
IncludeBlocks: Merge
|
||||
IndentExternBlock: Indent
|
||||
IndentRequiresClause: false
|
||||
IndentWidth: 4
|
||||
InsertNewlineAtEOF: true
|
||||
MacroBlockBegin: ''
|
||||
MacroBlockEnd: ''
|
||||
MaxEmptyLinesToKeep: 2
|
||||
ContinuationIndentWidth: 8
|
||||
KeepEmptyLinesAtTheStartOfBlocks: false
|
||||
NamespaceIndentation: All
|
||||
PointerAlignment: Left
|
||||
SpaceAfterCStyleCast: true
|
||||
SortUsingDeclarations: true
|
||||
SpaceAfterTemplateKeyword: false
|
||||
SpaceBeforeCtorInitializerColon: false
|
||||
SpaceBeforeParens: Custom
|
||||
SpaceBeforeParensOptions:
|
||||
AfterControlStatements: true
|
||||
AfterFunctionDeclarationName: false
|
||||
AfterFunctionDefinitionName: false
|
||||
AfterForeachMacros: true
|
||||
AfterIfMacros: true
|
||||
AfterOverloadedOperator: false
|
||||
BeforeNonEmptyParentheses: false
|
||||
SpaceBeforeRangeBasedForLoopColon: false
|
||||
SpaceInEmptyParentheses: false
|
||||
SpacesInAngles: false
|
||||
SpacesInConditionalStatement: false
|
||||
SpacesInCStyleCastParentheses: false
|
||||
SpacesInConditionalStatement: false
|
||||
SpacesInContainerLiterals: false
|
||||
SpacesInParentheses: false
|
||||
TabWidth: 4
|
||||
...
|
||||
|
||||
38
.github/ISSUE_TEMPLATE/bug_report.md
vendored
Normal file
38
.github/ISSUE_TEMPLATE/bug_report.md
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
---
|
||||
name: Bug report
|
||||
about: Create a report to help us improve
|
||||
title: ''
|
||||
labels: ''
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
**Describe the bug**
|
||||
A clear and concise description of what the bug is.
|
||||
|
||||
**To Reproduce**
|
||||
Steps to reproduce the behavior:
|
||||
1. Go to '...'
|
||||
2. Click on '....'
|
||||
3. Scroll down to '....'
|
||||
4. See error
|
||||
|
||||
**Expected behavior**
|
||||
A clear and concise description of what you expected to happen.
|
||||
|
||||
**Screenshots**
|
||||
If applicable, add screenshots to help explain your problem.
|
||||
|
||||
**Desktop (please complete the following information):**
|
||||
- OS: [e.g. iOS]
|
||||
- Browser [e.g. chrome, safari]
|
||||
- Version [e.g. 22]
|
||||
|
||||
**Smartphone (please complete the following information):**
|
||||
- Device: [e.g. iPhone6]
|
||||
- OS: [e.g. iOS8.1]
|
||||
- Browser [e.g. stock browser, safari]
|
||||
- Version [e.g. 22]
|
||||
|
||||
**Additional context**
|
||||
Add any other context about the problem here.
|
||||
10
.github/ISSUE_TEMPLATE/custom.md
vendored
Normal file
10
.github/ISSUE_TEMPLATE/custom.md
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
---
|
||||
name: Custom issue template
|
||||
about: Describe this issue template's purpose here.
|
||||
title: ''
|
||||
labels: ''
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
|
||||
20
.github/ISSUE_TEMPLATE/feature_request.md
vendored
Normal file
20
.github/ISSUE_TEMPLATE/feature_request.md
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
---
|
||||
name: Feature request
|
||||
about: Suggest an idea for this project
|
||||
title: ''
|
||||
labels: ''
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
**Is your feature request related to a problem? Please describe.**
|
||||
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
|
||||
|
||||
**Describe the solution you'd like**
|
||||
A clear and concise description of what you want to happen.
|
||||
|
||||
**Describe alternatives you've considered**
|
||||
A clear and concise description of any alternative solutions or features you've considered.
|
||||
|
||||
**Additional context**
|
||||
Add any other context or screenshots about the feature request here.
|
||||
BIN
.github/images/banner.png
vendored
Normal file
BIN
.github/images/banner.png
vendored
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 137 KiB |
79
.github/workflows/cmake-multi-platform.yml
vendored
Normal file
79
.github/workflows/cmake-multi-platform.yml
vendored
Normal file
@@ -0,0 +1,79 @@
|
||||
name: Omath CI (Arch Linux / Windows)
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ main ]
|
||||
pull_request:
|
||||
branches: [ main ]
|
||||
|
||||
concurrency:
|
||||
group: ci-${{ github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
|
||||
##############################################################################
|
||||
# 1) ARCH LINUX – Clang / Ninja
|
||||
##############################################################################
|
||||
jobs:
|
||||
arch-build-and-test:
|
||||
name: Arch Linux (Clang)
|
||||
runs-on: ubuntu-latest
|
||||
container: archlinux:latest
|
||||
|
||||
steps:
|
||||
- name: Install basic tool-chain with pacman
|
||||
shell: bash
|
||||
run: |
|
||||
pacman -Sy --noconfirm archlinux-keyring
|
||||
pacman -Syu --noconfirm --needed \
|
||||
git base-devel clang cmake ninja
|
||||
|
||||
- name: Checkout repository (with sub-modules)
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: recursive
|
||||
|
||||
- name: Configure (cmake --preset)
|
||||
shell: bash
|
||||
run: cmake --preset linux-release -DOMATH_BUILD_TESTS=ON
|
||||
|
||||
- name: Build
|
||||
shell: bash
|
||||
run: cmake --build cmake-build/build/linux-release --target all
|
||||
|
||||
- name: Run unit_tests
|
||||
shell: bash
|
||||
run: ./out/Release/unit_tests
|
||||
|
||||
|
||||
|
||||
##############################################################################
|
||||
# 2) Windows – MSVC / Ninja
|
||||
##############################################################################
|
||||
windows-build-and-test:
|
||||
name: Windows (MSVC)
|
||||
runs-on: windows-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout repository (with sub-modules)
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: recursive
|
||||
|
||||
- name: Install Ninja
|
||||
uses: seanmiddleditch/gha-setup-ninja@v4
|
||||
|
||||
- name: Set up MSVC developer command-prompt
|
||||
uses: ilammy/msvc-dev-cmd@v1
|
||||
|
||||
- name: Configure (cmake --preset)
|
||||
shell: bash
|
||||
run: cmake --preset windows-release -DOMATH_BUILD_TESTS=ON
|
||||
|
||||
- name: Build
|
||||
shell: bash
|
||||
run: cmake --build cmake-build/build/windows-release --target all
|
||||
|
||||
- name: Run unit_tests.exe
|
||||
shell: bash
|
||||
run: ./out/Release/unit_tests.exe
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,3 +2,4 @@
|
||||
/.idea
|
||||
/out
|
||||
*.DS_Store
|
||||
/extlibs/vcpkg
|
||||
8
.idea/modules.xml
generated
8
.idea/modules.xml
generated
@@ -1,8 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/uml.iml" filepath="$PROJECT_DIR$/.idea/uml.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
2
.idea/uml.iml
generated
2
.idea/uml.iml
generated
@@ -1,2 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module classpath="CMake" type="CPP_MODULE" version="4" />
|
||||
7
.idea/vcs.xml
generated
7
.idea/vcs.xml
generated
@@ -1,7 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
<mapping directory="$PROJECT_DIR$/extlibs/googletest" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
114
CMakeLists.txt
114
CMakeLists.txt
@@ -1,96 +1,108 @@
|
||||
cmake_minimum_required(VERSION 3.26)
|
||||
|
||||
project(omath VERSION 1.0.1 LANGUAGES CXX)
|
||||
project(omath VERSION 3.2.1 LANGUAGES CXX)
|
||||
|
||||
include(CMakePackageConfigHelpers)
|
||||
|
||||
|
||||
option(OMATH_BUILD_TESTS "Build unit tests" OFF)
|
||||
option(OMATH_BUILD_TESTS "Build unit tests" ${PROJECT_IS_TOP_LEVEL})
|
||||
option(OMATH_THREAT_WARNING_AS_ERROR "Set highest level of warnings and force compiler to treat them as errors" ON)
|
||||
option(OMATH_BUILD_AS_SHARED_LIBRARY "Build Omath as .so or .dll" OFF)
|
||||
option(OMATH_USE_AVX2 "Omath will use AVX2 to boost performance" ON)
|
||||
option(OMATH_IMGUI_INTEGRATION "Omath will define method to convert omath types to imgui types" OFF)
|
||||
option(OMATH_BUILD_EXAMPLES "Build example projects with you can learn & play" OFF)
|
||||
option(OMATH_STATIC_MSVC_RUNTIME_LIBRARY "Force Omath to link static runtime" OFF)
|
||||
option(OMATH_STATIC_MSVC_RUNTIME_LIBRARY "Force Omath to link static runtime" OFF)
|
||||
option(OMATH_SUPRESS_SAFETY_CHECKS "Supress some safety checks in release build to improve general performance" ON)
|
||||
option(OMATH_USE_UNITY_BUILD "Will enable unity build to speed up compilation" ON)
|
||||
option(OMATH_ENABLE_LEGACY "Will enable legacy classes that MUST be used ONLY for backward compatibility" OFF)
|
||||
|
||||
|
||||
file(GLOB_RECURSE OMATH_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp")
|
||||
file(GLOB_RECURSE OMATH_HEADERS CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/include/*.hpp")
|
||||
|
||||
|
||||
if (OMATH_BUILD_AS_SHARED_LIBRARY)
|
||||
add_library(omath SHARED source/matrix.cpp)
|
||||
else()
|
||||
add_library(omath STATIC source/matrix.cpp
|
||||
source/matrix.cpp)
|
||||
endif()
|
||||
add_library(${PROJECT_NAME} SHARED ${OMATH_SOURCES} ${OMATH_HEADERS})
|
||||
else ()
|
||||
add_library(${PROJECT_NAME} STATIC ${OMATH_SOURCES} ${OMATH_HEADERS})
|
||||
endif ()
|
||||
|
||||
message(STATUS "Building on ${CMAKE_HOST_SYSTEM_NAME}")
|
||||
add_library(omath::omath ALIAS omath)
|
||||
message(STATUS "[OMATH]: Building on ${CMAKE_HOST_SYSTEM_NAME}")
|
||||
|
||||
add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
|
||||
|
||||
if (OMATH_IMGUI_INTEGRATION)
|
||||
target_compile_definitions(omath PUBLIC OMATH_IMGUI_INTEGRATION)
|
||||
target_compile_definitions(${PROJECT_NAME} PUBLIC OMATH_IMGUI_INTEGRATION)
|
||||
|
||||
# IMGUI is being linked as submodule
|
||||
if(TARGET imgui)
|
||||
target_link_libraries(omath PUBLIC imgui)
|
||||
if (TARGET imgui)
|
||||
target_link_libraries(${PROJECT_NAME} PUBLIC imgui)
|
||||
install(TARGETS imgui
|
||||
EXPORT omathTargets
|
||||
ARCHIVE DESTINATION lib
|
||||
LIBRARY DESTINATION lib
|
||||
RUNTIME DESTINATION bin)
|
||||
else()
|
||||
else ()
|
||||
# Assume that IMGUI linked via VCPKG.
|
||||
find_package(imgui CONFIG REQUIRED)
|
||||
target_link_libraries(omath PUBLIC imgui::imgui)
|
||||
endif()
|
||||
target_link_libraries(${PROJECT_NAME} PUBLIC imgui::imgui)
|
||||
endif ()
|
||||
|
||||
endif()
|
||||
endif ()
|
||||
|
||||
if (OMATH_USE_AVX2)
|
||||
target_compile_definitions(omath PUBLIC OMATH_USE_AVX2)
|
||||
endif()
|
||||
target_compile_definitions(${PROJECT_NAME} PUBLIC OMATH_USE_AVX2)
|
||||
endif ()
|
||||
|
||||
if (OMATH_SUPRESS_SAFETY_CHECKS)
|
||||
target_compile_definitions(omath PUBLIC OMATH_SUPRESS_SAFETY_CHECKS)
|
||||
endif()
|
||||
target_compile_definitions(${PROJECT_NAME} PUBLIC OMATH_SUPRESS_SAFETY_CHECKS)
|
||||
endif ()
|
||||
|
||||
set_target_properties(omath PROPERTIES
|
||||
if (OMATH_ENABLE_LEGACY)
|
||||
target_compile_options(${PROJECT_NAME} PUBLIC OMATH_ENABLE_LEGACY)
|
||||
endif ()
|
||||
|
||||
set_target_properties(${PROJECT_NAME} PROPERTIES
|
||||
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
|
||||
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
|
||||
UNITY_BUILD ON
|
||||
UNITY_BUILD_BATCH_SIZE 20
|
||||
CXX_STANDARD 23
|
||||
CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
if (OMATH_USE_UNITY_BUILD)
|
||||
set_target_properties(${PROJECT_NAME} PROPERTIES
|
||||
UNITY_BUILD ON
|
||||
UNITY_BUILD_BATCH_SIZE 20)
|
||||
endif ()
|
||||
|
||||
if (OMATH_STATIC_MSVC_RUNTIME_LIBRARY)
|
||||
set_target_properties(omath PROPERTIES
|
||||
set_target_properties(${PROJECT_NAME} PROPERTIES
|
||||
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>"
|
||||
)
|
||||
endif()
|
||||
endif ()
|
||||
|
||||
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
|
||||
target_compile_options(omath PRIVATE -mavx2 -mfma)
|
||||
endif()
|
||||
target_compile_options(${PROJECT_NAME} PRIVATE -mavx2 -mfma)
|
||||
endif ()
|
||||
|
||||
target_compile_features(omath PUBLIC cxx_std_23)
|
||||
target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_23)
|
||||
|
||||
|
||||
add_subdirectory(source)
|
||||
|
||||
if(OMATH_BUILD_TESTS)
|
||||
if (OMATH_BUILD_TESTS)
|
||||
add_subdirectory(extlibs)
|
||||
add_subdirectory(tests)
|
||||
endif()
|
||||
endif ()
|
||||
|
||||
if (OMATH_BUILD_EXAMPLES)
|
||||
add_subdirectory(examples)
|
||||
endif()
|
||||
endif ()
|
||||
|
||||
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" AND OMATH_THREAT_WARNING_AS_ERROR)
|
||||
target_compile_options(omath PRIVATE /W4 /WX)
|
||||
elseif(OMATH_THREAT_WARNING_AS_ERROR)
|
||||
target_compile_options(omath PRIVATE -Wall -Wextra -Wpedantic -Werror)
|
||||
endif()
|
||||
target_compile_options(${PROJECT_NAME} PRIVATE /W4 /WX)
|
||||
elseif (OMATH_THREAT_WARNING_AS_ERROR)
|
||||
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wextra -Wpedantic -Werror)
|
||||
endif ()
|
||||
|
||||
target_include_directories(omath
|
||||
target_include_directories(${PROJECT_NAME}
|
||||
PUBLIC
|
||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> # Use this path when building the project
|
||||
$<INSTALL_INTERFACE:include> # Use this path when the project is installed
|
||||
@@ -100,21 +112,21 @@ target_include_directories(omath
|
||||
# Installation rules
|
||||
|
||||
# Install the library
|
||||
install(TARGETS omath
|
||||
EXPORT omathTargets
|
||||
ARCHIVE DESTINATION lib COMPONENT omath # For static libraries
|
||||
LIBRARY DESTINATION lib COMPONENT omath # For shared libraries
|
||||
RUNTIME DESTINATION bin COMPONENT omath # For executables (on Windows)
|
||||
install(TARGETS ${PROJECT_NAME}
|
||||
EXPORT ${PROJECT_NAME}Targets
|
||||
ARCHIVE DESTINATION lib COMPONENT ${PROJECT_NAME} # For static libraries
|
||||
LIBRARY DESTINATION lib COMPONENT ${PROJECT_NAME} # For shared libraries
|
||||
RUNTIME DESTINATION bin COMPONENT ${PROJECT_NAME} # For executables (on Windows)
|
||||
)
|
||||
|
||||
# Install headers as part of omath_component
|
||||
install(DIRECTORY include/ DESTINATION include COMPONENT omath)
|
||||
install(DIRECTORY include/ DESTINATION include COMPONENT ${PROJECT_NAME})
|
||||
|
||||
# Export omath target for CMake find_package support, also under omath_component
|
||||
install(EXPORT omathTargets
|
||||
FILE omathTargets.cmake
|
||||
NAMESPACE omath::
|
||||
DESTINATION lib/cmake/omath COMPONENT omath
|
||||
install(EXPORT ${PROJECT_NAME}Targets
|
||||
FILE ${PROJECT_NAME}Targets.cmake
|
||||
NAMESPACE ${PROJECT_NAME}::
|
||||
DESTINATION lib/cmake/${PROJECT_NAME} COMPONENT ${PROJECT_NAME}
|
||||
)
|
||||
|
||||
|
||||
@@ -129,12 +141,12 @@ write_basic_package_version_file(
|
||||
configure_package_config_file(
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/omathConfig.cmake.in" # Path to the .in file
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/omathConfig.cmake" # Output path for the generated file
|
||||
INSTALL_DESTINATION lib/cmake/omath
|
||||
INSTALL_DESTINATION lib/cmake/${PROJECT_NAME}
|
||||
)
|
||||
|
||||
# Install the generated config files
|
||||
install(FILES
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/omathConfig.cmake"
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/omathConfigVersion.cmake"
|
||||
DESTINATION lib/cmake/omath
|
||||
DESTINATION lib/cmake/${PROJECT_NAME}
|
||||
)
|
||||
|
||||
@@ -40,8 +40,8 @@
|
||||
"binaryDir": "${sourceDir}/cmake-build/build/${presetName}",
|
||||
"installDir": "${sourceDir}/cmake-build/install/${presetName}",
|
||||
"cacheVariables": {
|
||||
"CMAKE_C_COMPILER": "gcc",
|
||||
"CMAKE_CXX_COMPILER": "g++"
|
||||
"CMAKE_C_COMPILER": "clang",
|
||||
"CMAKE_CXX_COMPILER": "clang++"
|
||||
},
|
||||
"condition": {
|
||||
"type": "equals",
|
||||
|
||||
32
CONTRIBUTING.md
Normal file
32
CONTRIBUTING.md
Normal file
@@ -0,0 +1,32 @@
|
||||
## 🤝 Contributing to OMath or other Orange's Projects
|
||||
|
||||
### ❕ Prerequisites
|
||||
|
||||
- A working up-to-date OMath installation
|
||||
- C++ knowledge
|
||||
- Git knowledge
|
||||
- Ability to ask for help (Feel free to create empty pull-request or PM a maintainer
|
||||
in [Telegram](https://t.me/orange_cpp))
|
||||
|
||||
### ⏬ Setting up OMath
|
||||
|
||||
Please read INSTALL.md file in repository
|
||||
|
||||
### 🔀 Pull requests and Branches
|
||||
|
||||
In order to send code back to the official OMath repository, you must first create a copy of OMath on your github
|
||||
account ([fork](https://help.github.com/articles/creating-a-pull-request-from-a-fork/)) and
|
||||
then [create a pull request](https://help.github.com/articles/creating-a-pull-request-from-a-fork/) back to OMath.
|
||||
|
||||
OMath developement is performed on multiple branches. Changes are then pull requested into master. By default, changes
|
||||
merged into master will not roll out to stable build users unless the `stable` tag is updated.
|
||||
|
||||
### 📜 Code-Style
|
||||
|
||||
The orange code-style can be found in `.clang-format`.
|
||||
|
||||
### 📦 Building
|
||||
|
||||
OMath has already created the `cmake-build` and `out` directories where cmake/bin files are located. By default, you
|
||||
can build OMath by running `cmake --build cmake-build/build/windows-release --target omath -j 6` in the source
|
||||
directory.
|
||||
11
CREDITS.md
Normal file
11
CREDITS.md
Normal file
@@ -0,0 +1,11 @@
|
||||
# OMATH CREDITS
|
||||
|
||||
Thanks to everyone who made this possible, including:
|
||||
|
||||
- Saikari aka luadebug for VCPKG port.
|
||||
|
||||
And a big hand to everyone else who has contributed over the past!
|
||||
|
||||
THANKS! <3
|
||||
|
||||
-- Orange++ <orange-cpp@yandex.ru>
|
||||
68
INSTALL.md
Normal file
68
INSTALL.md
Normal file
@@ -0,0 +1,68 @@
|
||||
# 📥Installation Guide
|
||||
|
||||
## <img width="28px" src="https://vcpkg.io/assets/mark/mark.svg" /> Using vcpkg
|
||||
**Note**: Support vcpkg for package management
|
||||
1. Install [vcpkg](https://github.com/microsoft/vcpkg)
|
||||
2. Run the following command to install the orange-math package:
|
||||
```
|
||||
vcpkg install orange-math
|
||||
```
|
||||
CMakeLists.txt
|
||||
```cmake
|
||||
find_package(omath CONFIG REQUIRED)
|
||||
target_link_libraries(main PRIVATE omath::omath)
|
||||
```
|
||||
For detailed commands on installing different versions and more information, please refer to Microsoft's [official instructions](https://learn.microsoft.com/en-us/vcpkg/get_started/overview).
|
||||
|
||||
## <img width="28px" src="https://xmake.io/assets/img/logo.svg" /> Using xrepo
|
||||
**Note**: Support xrepo for package management
|
||||
1. Install [xmake](https://xmake.io/)
|
||||
2. Run the following command to install the omath package:
|
||||
```
|
||||
xrepo install omath
|
||||
```
|
||||
xmake.lua
|
||||
```xmake
|
||||
add_requires("omath")
|
||||
target("...")
|
||||
add_packages("omath")
|
||||
```
|
||||
|
||||
## <img width="28px" src="https://upload.wikimedia.org/wikipedia/commons/e/ef/CMake_logo.svg?" /> Build from source using CMake
|
||||
1. **Preparation**
|
||||
|
||||
Install needed tools: cmake, clang, git, msvc (windows only).
|
||||
|
||||
1. **Linux:**
|
||||
```bash
|
||||
sudo pacman -Sy cmake ninja clang git
|
||||
```
|
||||
2. **MacOS:**
|
||||
```bash
|
||||
brew install llvm git cmake ninja
|
||||
```
|
||||
3. **Windows:**
|
||||
|
||||
Install Visual Studio from [here](https://visualstudio.microsoft.com/downloads/) and Git from [here](https://git-scm.com/downloads).
|
||||
|
||||
Use x64 Native Tools shell to execute needed commands down below.
|
||||
2. **Clone the repository:**
|
||||
```bash
|
||||
git clone https://github.com/orange-cpp/omath.git
|
||||
```
|
||||
3. **Navigate to the project directory:**
|
||||
```bash
|
||||
cd omath
|
||||
```
|
||||
4. **Build the project using CMake:**
|
||||
```bash
|
||||
cmake --preset windows-release -S .
|
||||
cmake --build cmake-build/build/windows-release --target omath -j 6
|
||||
```
|
||||
Use **\<platform\>-\<build configuration\>** preset to build siutable version for yourself. Like **windows-release** or **linux-release**.
|
||||
|
||||
| Platform Name | Build Config |
|
||||
|---------------|---------------|
|
||||
| windows | release/debug |
|
||||
| linux | release/debug |
|
||||
| darwin | release/debug |
|
||||
20
LICENSE
20
LICENSE
@@ -1,9 +1,17 @@
|
||||
The MIT License (MIT)
|
||||
Copyright (C) 2024-2025 Orange++ <orange-cpp@yandex.ru>
|
||||
|
||||
Copyright (c) 2024 Orange++
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
57
README.md
57
README.md
@@ -1,16 +1,31 @@
|
||||
<div align = center>
|
||||
|
||||

|
||||

|
||||
|
||||

|
||||

|
||||

|
||||
[](https://www.codefactor.io/repository/github/orange-cpp/omath)
|
||||

|
||||
[](https://repology.org/project/orange-math/versions)
|
||||

|
||||
[](https://discord.gg/eDgdaWbqwZ)
|
||||
[](https://t.me/orangennotes)
|
||||
|
||||
</div>
|
||||
|
||||
Oranges's Math Library (omath) is a comprehensive, open-source library aimed at providing efficient, reliable, and versatile mathematical functions and algorithms. Developed primarily in C++, this library is designed to cater to a wide range of mathematical operations essential in scientific computing, engineering, and academic research.
|
||||
|
||||
<div align = center>
|
||||
<a href="https://www.star-history.com/#orange-cpp/omath&Date">
|
||||
<picture>
|
||||
<source media="(prefers-color-scheme: dark)" srcset="https://api.star-history.com/svg?repos=orange-cpp/omath&type=Date&theme=dark" />
|
||||
<source media="(prefers-color-scheme: light)" srcset="https://api.star-history.com/svg?repos=orange-cpp/omath&type=Date" />
|
||||
<img alt="Star History Chart" src="https://api.star-history.com/svg?repos=orange-cpp/omath&type=Date" />
|
||||
</picture>
|
||||
</a>
|
||||
</div>
|
||||
|
||||
## 👁🗨 Features
|
||||
- **Efficiency**: Optimized for performance, ensuring quick computations using AVX2.
|
||||
- **Versatility**: Includes a wide array of mathematical functions and algorithms.
|
||||
@@ -37,40 +52,9 @@ Oranges's Math Library (omath) is a comprehensive, open-source library aimed at
|
||||
| Linux | ✅YES |
|
||||
| Darwin (MacOS) | ✅YES |
|
||||
|
||||
## ⏬ Getting Started
|
||||
### Prerequisites
|
||||
- C++ Compiler
|
||||
- CMake (for building the project)
|
||||
## ⏬ Installation
|
||||
Please read our [installation guide](https://github.com/orange-cpp/omath/blob/main/INSTALL.md). If this link doesn't work check out INSTALL.md file.
|
||||
|
||||
### Installation
|
||||
### vcpkg
|
||||
**Note**: Support vcpkg for package management
|
||||
1. Install vcpkg (https://github.com/microsoft/vcpkg)
|
||||
2. Run the following command to install the orange-math package:
|
||||
```
|
||||
vcpkg install orange-math
|
||||
```
|
||||
CMakeLists.txt
|
||||
```cmake
|
||||
find_package(omath CONFIG REQUIRED)
|
||||
target_link_libraries(main PRIVATE omath::omath)
|
||||
```
|
||||
For detailed commands on installing different versions and more information, please refer to Microsoft's official instructions (https://learn.microsoft.com/en-us/vcpkg/get_started/overview)
|
||||
### Build from source
|
||||
1. Clone the repository:
|
||||
```
|
||||
git clone https://github.com/orange-cpp/omath.git
|
||||
```
|
||||
2. Navigate to the project directory:
|
||||
```
|
||||
cd omath
|
||||
```
|
||||
3. Build the project using CMake:
|
||||
```
|
||||
cmake --preset windows-release -S .
|
||||
cmake --build cmake-build/build/windows-release --target omath -j 6
|
||||
```
|
||||
Use **\<platform\>-\<build configuration\>** preset to build siutable version for yourself. Like **windows-release** or **linux-release**.
|
||||
## ❔ Usage
|
||||
Simple world to screen function
|
||||
```c++
|
||||
@@ -91,7 +75,8 @@ With `omath/projection` module you can achieve simple ESP hack for powered by So
|
||||

|
||||
Or for InfinityWard Engine based games. Like Call of Duty Black Ops 2!
|
||||

|
||||
|
||||
Or create simple trigger bot with embeded traceline from omath::collision::LineTrace
|
||||

|
||||
Or even advanced projectile aimbot
|
||||
[Watch Video](https://youtu.be/lM_NJ1yCunw?si=5E87OrQMeypxSJ3E)
|
||||
</details>
|
||||
@@ -100,7 +85,7 @@ Or even advanced projectile aimbot
|
||||
Contributions to `omath` are welcome! Please read `CONTRIBUTING.md` for details on our code of conduct and the process for submitting pull requests.
|
||||
|
||||
## 📜 License
|
||||
This project is licensed under the MIT - see the `LICENSE` file for details.
|
||||
This project is licensed under the ZLIB - see the `LICENSE` file for details.
|
||||
|
||||
## 💘 Acknowledgments
|
||||
- [All contributors](https://github.com/orange-cpp/omath/graphs/contributors)
|
||||
|
||||
@@ -2,6 +2,10 @@
|
||||
|
||||
include(CMakeFindDependencyMacro)
|
||||
|
||||
if (@OMATH_IMGUI_INTEGRATION@)
|
||||
find_dependency(imgui CONFIG)
|
||||
endif()
|
||||
|
||||
# Load the targets for the omath library
|
||||
include("${CMAKE_CURRENT_LIST_DIR}/omathTargets.cmake")
|
||||
check_required_components(omath)
|
||||
|
||||
@@ -15,8 +15,8 @@ int main()
|
||||
float fov = 0;
|
||||
float near = 0;
|
||||
float far = 0;
|
||||
float viewPortWidth = 0;
|
||||
float viewPortHeight = 0;
|
||||
float view_port_width = 0;
|
||||
float view_port_height = 0;
|
||||
|
||||
std::print("Enter camera fov: ");
|
||||
std::cin >> fov;
|
||||
@@ -28,13 +28,13 @@ int main()
|
||||
std::cin >> far;
|
||||
|
||||
std::print("Enter camera screen width: ");
|
||||
std::cin >> viewPortWidth;
|
||||
std::cin >> view_port_width;
|
||||
|
||||
std::print("Enter camera screen height: ");
|
||||
std::cin >> viewPortHeight;
|
||||
std::cin >> view_port_height;
|
||||
|
||||
const auto mat =
|
||||
omath::opengl_engine::CalcPerspectiveProjectionMatrix(fov, viewPortWidth / viewPortHeight, near, far);
|
||||
omath::opengl_engine::calc_perspective_projection_matrix(fov, view_port_width / view_port_height, near, far);
|
||||
|
||||
std::print("{}", mat.ToString());
|
||||
std::print("{}", mat.to_string());
|
||||
};
|
||||
|
||||
Submodule extlibs/googletest updated: d83fee138a...52eb8108c5
17
include/omath/3d_primitives/box.hpp
Normal file
17
include/omath/3d_primitives/box.hpp
Normal file
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// Created by Vlad on 4/18/2025.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
#include <array>
|
||||
#include "omath/triangle.hpp"
|
||||
#include "omath/vector3.hpp"
|
||||
|
||||
|
||||
namespace omath::primitives
|
||||
{
|
||||
[[nodiscard]]
|
||||
std::array<Triangle<Vector3<float>>, 12> create_box(const Vector3<float>& top, const Vector3<float>& bottom,
|
||||
const Vector3<float>& dir_forward, const Vector3<float>& dir_right,
|
||||
float ratio = 4.f) noexcept;
|
||||
}
|
||||
@@ -3,10 +3,9 @@
|
||||
//
|
||||
|
||||
#pragma once
|
||||
#include "omath/angles.hpp"
|
||||
#include <algorithm>
|
||||
#include <utility>
|
||||
#include "omath/angles.hpp"
|
||||
|
||||
|
||||
namespace omath
|
||||
{
|
||||
@@ -17,14 +16,14 @@ namespace omath
|
||||
};
|
||||
|
||||
template<class Type = float, Type min = Type(0), Type max = Type(360), AngleFlags flags = AngleFlags::Normalized>
|
||||
requires std::is_arithmetic_v<Type>
|
||||
requires std::is_arithmetic_v<Type>
|
||||
class Angle
|
||||
{
|
||||
Type m_angle;
|
||||
constexpr explicit Angle(const Type& degrees)
|
||||
constexpr explicit Angle(const Type& degrees) noexcept
|
||||
{
|
||||
if constexpr (flags == AngleFlags::Normalized)
|
||||
m_angle = angles::WrapAngle(degrees, min, max);
|
||||
m_angle = angles::wrap_angle(degrees, min, max);
|
||||
|
||||
else if constexpr (flags == AngleFlags::Clamped)
|
||||
m_angle = std::clamp(degrees, min, max);
|
||||
@@ -37,71 +36,71 @@ namespace omath
|
||||
|
||||
public:
|
||||
[[nodiscard]]
|
||||
constexpr static Angle FromDegrees(const Type& degrees)
|
||||
constexpr static Angle from_degrees(const Type& degrees) noexcept
|
||||
{
|
||||
return Angle{degrees};
|
||||
}
|
||||
constexpr Angle() : m_angle(0)
|
||||
constexpr Angle() noexcept: m_angle(0)
|
||||
{
|
||||
}
|
||||
[[nodiscard]]
|
||||
constexpr static Angle FromRadians(const Type& degrees)
|
||||
constexpr static Angle from_radians(const Type& degrees) noexcept
|
||||
{
|
||||
return Angle{angles::RadiansToDegrees<Type>(degrees)};
|
||||
return Angle{angles::radians_to_degrees<Type>(degrees)};
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr const Type& operator*() const
|
||||
constexpr const Type& operator*() const noexcept
|
||||
{
|
||||
return m_angle;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Type AsDegrees() const
|
||||
constexpr Type as_degrees() const noexcept
|
||||
{
|
||||
return m_angle;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Type AsRadians() const
|
||||
constexpr Type as_radians() const noexcept
|
||||
{
|
||||
return angles::DegreesToRadians(m_angle);
|
||||
return angles::degrees_to_radians(m_angle);
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
Type Sin() const
|
||||
Type sin() const noexcept
|
||||
{
|
||||
return std::sin(AsRadians());
|
||||
return std::sin(as_radians());
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
Type Cos() const
|
||||
Type cos() const noexcept
|
||||
{
|
||||
return std::cos(AsRadians());
|
||||
return std::cos(as_radians());
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
Type Tan() const
|
||||
Type tan() const noexcept
|
||||
{
|
||||
return std::tan(AsRadians());
|
||||
return std::tan(as_radians());
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
Type Atan() const
|
||||
Type atan() const noexcept
|
||||
{
|
||||
return std::atan(AsRadians());
|
||||
return std::atan(as_radians());
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
Type Cot() const
|
||||
Type cot() const noexcept
|
||||
{
|
||||
return Cos() / Sin();
|
||||
return cos() / sin();
|
||||
}
|
||||
|
||||
constexpr Angle& operator+=(const Angle& other)
|
||||
constexpr Angle& operator+=(const Angle& other) noexcept
|
||||
{
|
||||
if constexpr (flags == AngleFlags::Normalized)
|
||||
m_angle = angles::WrapAngle(m_angle + other.m_angle, min, max);
|
||||
m_angle = angles::wrap_angle(m_angle + other.m_angle, min, max);
|
||||
|
||||
else if constexpr (flags == AngleFlags::Clamped)
|
||||
m_angle = std::clamp(m_angle + other.m_angle, min, max);
|
||||
@@ -115,18 +114,18 @@ namespace omath
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr std::partial_ordering operator<=>(const Angle& other) const = default;
|
||||
constexpr std::partial_ordering operator<=>(const Angle& other) const noexcept = default;
|
||||
|
||||
constexpr Angle& operator-=(const Angle& other)
|
||||
constexpr Angle& operator-=(const Angle& other) noexcept
|
||||
{
|
||||
return operator+=(-other);
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Angle& operator+(const Angle& other)
|
||||
constexpr Angle& operator+(const Angle& other) noexcept
|
||||
{
|
||||
if constexpr (flags == AngleFlags::Normalized)
|
||||
return {angles::WrapAngle(m_angle + other.m_angle, min, max)};
|
||||
return {angles::wrap_angle(m_angle + other.m_angle, min, max)};
|
||||
|
||||
else if constexpr (flags == AngleFlags::Clamped)
|
||||
return {std::clamp(m_angle + other.m_angle, min, max)};
|
||||
@@ -138,13 +137,13 @@ namespace omath
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Angle& operator-(const Angle& other)
|
||||
constexpr Angle& operator-(const Angle& other) noexcept
|
||||
{
|
||||
return operator+(-other);
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Angle operator-() const
|
||||
constexpr Angle operator-() const noexcept
|
||||
{
|
||||
return Angle{-m_angle};
|
||||
}
|
||||
|
||||
@@ -10,54 +10,55 @@ namespace omath::angles
|
||||
{
|
||||
template<class Type>
|
||||
requires std::is_floating_point_v<Type>
|
||||
[[nodiscard]] constexpr Type RadiansToDegrees(const Type& radians)
|
||||
[[nodiscard]] constexpr Type radians_to_degrees(const Type& radians) noexcept
|
||||
{
|
||||
return radians * (Type(180) / std::numbers::pi_v<Type>);
|
||||
return radians * (static_cast<Type>(180) / std::numbers::pi_v<Type>);
|
||||
}
|
||||
|
||||
template<class Type>
|
||||
requires std::is_floating_point_v<Type>
|
||||
[[nodiscard]] constexpr Type DegreesToRadians(const Type& degrees)
|
||||
[[nodiscard]] constexpr Type degrees_to_radians(const Type& degrees) noexcept
|
||||
{
|
||||
return degrees * (std::numbers::pi_v<Type> / Type(180));
|
||||
}
|
||||
|
||||
template<class type>
|
||||
requires std::is_floating_point_v<type>
|
||||
[[nodiscard]] type HorizontalFovToVertical(const type& horFov, const type& aspect)
|
||||
{
|
||||
const auto fovRad = DegreesToRadians(horFov);
|
||||
|
||||
const auto vertFov = type(2) * std::atan(std::tan(fovRad / type(2)) / aspect);
|
||||
|
||||
return RadiansToDegrees(vertFov);
|
||||
return degrees * (std::numbers::pi_v<Type> / static_cast<Type>(180));
|
||||
}
|
||||
|
||||
template<class Type>
|
||||
requires std::is_floating_point_v<Type>
|
||||
[[nodiscard]] Type VerticalFovToHorizontal(const Type& vertFov, const Type& aspect)
|
||||
[[nodiscard]] Type horizontal_fov_to_vertical(const Type& horizontal_fov, const Type& aspect) noexcept
|
||||
{
|
||||
const auto fovRad = DegreesToRadians(vertFov);
|
||||
const auto fov_rad = degrees_to_radians(horizontal_fov);
|
||||
|
||||
const auto horFov = Type(2) * std::atan(std::tan(fovRad / Type(2)) * aspect);
|
||||
const auto vert_fov = static_cast<Type>(2) * std::atan(std::tan(fov_rad / static_cast<Type>(2)) / aspect);
|
||||
|
||||
return RadiansToDegrees(horFov);
|
||||
return radians_to_degrees(vert_fov);
|
||||
}
|
||||
|
||||
template<class Type>
|
||||
requires std::is_floating_point_v<Type>
|
||||
[[nodiscard]] Type vertical_fov_to_horizontal(const Type& vertical_fov, const Type& aspect) noexcept
|
||||
{
|
||||
const auto fov_as_radians = degrees_to_radians(vertical_fov);
|
||||
|
||||
const auto horizontal_fov =
|
||||
static_cast<Type>(2) * std::atan(std::tan(fov_as_radians / static_cast<Type>(2)) * aspect);
|
||||
|
||||
return radians_to_degrees(horizontal_fov);
|
||||
}
|
||||
|
||||
template<class Type>
|
||||
requires std::is_arithmetic_v<Type>
|
||||
[[nodiscard]] Type WrapAngle(const Type& angle, const Type& min, const Type& max)
|
||||
[[nodiscard]] Type wrap_angle(const Type& angle, const Type& min, const Type& max) noexcept
|
||||
{
|
||||
if (angle <= max && angle >= min)
|
||||
return angle;
|
||||
|
||||
const Type range = max - min;
|
||||
|
||||
Type wrappedAngle = std::fmod(angle - min, range);
|
||||
Type wrapped_angle = std::fmod(angle - min, range);
|
||||
|
||||
if (wrappedAngle < 0)
|
||||
wrappedAngle += range;
|
||||
if (wrapped_angle < 0)
|
||||
wrapped_angle += range;
|
||||
|
||||
return wrappedAngle + min;
|
||||
return wrapped_angle + min;
|
||||
}
|
||||
}
|
||||
} // namespace omath::angles
|
||||
|
||||
@@ -13,26 +13,25 @@ namespace omath::collision
|
||||
public:
|
||||
Vector3<float> start;
|
||||
Vector3<float> end;
|
||||
bool infinite_length = false;
|
||||
|
||||
[[nodiscard]]
|
||||
Vector3<float> DirectionVector() const;
|
||||
Vector3<float> direction_vector() const noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
Vector3<float> DirectionVectorNormalized() const;
|
||||
Vector3<float> direction_vector_normalized() const noexcept;
|
||||
};
|
||||
class LineTracer
|
||||
{
|
||||
public:
|
||||
LineTracer() = delete;
|
||||
|
||||
|
||||
[[nodiscard]]
|
||||
static bool CanTraceLine(const Ray& ray, const Triangle<Vector3<float>>& triangle);
|
||||
|
||||
static bool can_trace_line(const Ray& ray, const Triangle<Vector3<float>>& triangle) noexcept;
|
||||
|
||||
// Realization of Möller–Trumbore intersection algorithm
|
||||
// https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm
|
||||
[[nodiscard]]
|
||||
static Vector3<float> GetRayHitPoint(const Ray& ray, const Triangle<Vector3<float>>& triangle);
|
||||
static Vector3<float> get_ray_hit_point(const Ray& ray, const Triangle<Vector3<float>>& triangle) noexcept;
|
||||
};
|
||||
}
|
||||
} // namespace omath::collision
|
||||
|
||||
@@ -4,95 +4,93 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include "omath/vector3.hpp"
|
||||
#include "omath/vector4.hpp"
|
||||
#include <cstdint>
|
||||
|
||||
#ifdef max
|
||||
#undef max
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef min
|
||||
#undef min
|
||||
#endif
|
||||
|
||||
namespace omath
|
||||
{
|
||||
struct HSV
|
||||
struct Hsv
|
||||
{
|
||||
float hue{};
|
||||
float saturation{};
|
||||
float value{};
|
||||
};
|
||||
|
||||
|
||||
class Color final : public Vector4<float>
|
||||
{
|
||||
public:
|
||||
constexpr Color(const float r, const float g, const float b, const float a) : Vector4(r, g, b, a)
|
||||
constexpr Color(const float r, const float g, const float b, const float a) noexcept: Vector4(r, g, b, a)
|
||||
{
|
||||
Clamp(0.f, 1.f);
|
||||
clamp(0.f, 1.f);
|
||||
}
|
||||
|
||||
constexpr explicit Color() = default;
|
||||
constexpr explicit Color() noexcept = default;
|
||||
[[nodiscard]]
|
||||
constexpr static Color FromRGBA(const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t a)
|
||||
constexpr static Color from_rgba(const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t a) noexcept
|
||||
{
|
||||
return Color{Vector4(r, g, b, a) / 255.f};
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr static Color FromHSV(float hue, const float saturation, const float value)
|
||||
constexpr static Color from_hsv(float hue, const float saturation, const float value) noexcept
|
||||
{
|
||||
float r{}, g{}, b{};
|
||||
|
||||
hue = std::clamp(hue, 0.f, 1.f);
|
||||
|
||||
const int i = static_cast<int>(hue * 6.f);
|
||||
const float f = hue * 6 - i;
|
||||
const float f = hue * 6.f - static_cast<float>(i);
|
||||
const float p = value * (1 - saturation);
|
||||
const float q = value * (1 - f * saturation);
|
||||
const float t = value * (1 - (1 - f) * saturation);
|
||||
|
||||
switch (i % 6)
|
||||
{
|
||||
case 0:
|
||||
r = value, g = t, b = p;
|
||||
break;
|
||||
case 1:
|
||||
r = q, g = value, b = p;
|
||||
break;
|
||||
case 2:
|
||||
r = p, g = value, b = t;
|
||||
break;
|
||||
case 3:
|
||||
r = p, g = q, b = value;
|
||||
break;
|
||||
case 4:
|
||||
r = t, g = p, b = value;
|
||||
break;
|
||||
case 5:
|
||||
r = value, g = p, b = q;
|
||||
break;
|
||||
case 0:
|
||||
r = value, g = t, b = p;
|
||||
break;
|
||||
case 1:
|
||||
r = q, g = value, b = p;
|
||||
break;
|
||||
case 2:
|
||||
r = p, g = value, b = t;
|
||||
break;
|
||||
case 3:
|
||||
r = p, g = q, b = value;
|
||||
break;
|
||||
case 4:
|
||||
r = t, g = p, b = value;
|
||||
break;
|
||||
case 5:
|
||||
r = value, g = p, b = q;
|
||||
break;
|
||||
|
||||
default:
|
||||
return {0.f, 0.f, 0.f, 0.f};
|
||||
default:
|
||||
return {0.f, 0.f, 0.f, 0.f};
|
||||
}
|
||||
|
||||
return {r, g, b, 1.f};
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr static Color FromHSV(const HSV& hsv)
|
||||
constexpr static Color from_hsv(const Hsv& hsv) noexcept
|
||||
{
|
||||
return FromHSV(hsv.hue, hsv.saturation, hsv.value);
|
||||
return from_hsv(hsv.hue, hsv.saturation, hsv.value);
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr HSV ToHSV() const
|
||||
constexpr Hsv to_hsv() const noexcept
|
||||
{
|
||||
HSV hsvData;
|
||||
Hsv hsv_data;
|
||||
|
||||
const float& red = x;
|
||||
const float& green = y;
|
||||
@@ -102,72 +100,78 @@ namespace omath
|
||||
const float min = std::min({red, green, blue});
|
||||
const float delta = max - min;
|
||||
|
||||
|
||||
if (delta == 0.f)
|
||||
hsvData.hue = 0.f;
|
||||
hsv_data.hue = 0.f;
|
||||
|
||||
else if (max == red)
|
||||
hsvData.hue = 60.f * (std::fmodf(((green - blue) / delta), 6.f));
|
||||
hsv_data.hue = 60.f * (std::fmodf(((green - blue) / delta), 6.f));
|
||||
else if (max == green)
|
||||
hsvData.hue = 60.f * (((blue - red) / delta) + 2.f);
|
||||
hsv_data.hue = 60.f * (((blue - red) / delta) + 2.f);
|
||||
else if (max == blue)
|
||||
hsvData.hue = 60.f * (((red - green) / delta) + 4.f);
|
||||
hsv_data.hue = 60.f * (((red - green) / delta) + 4.f);
|
||||
|
||||
if (hsvData.hue < 0.f)
|
||||
hsvData.hue += 360.f;
|
||||
if (hsv_data.hue < 0.f)
|
||||
hsv_data.hue += 360.f;
|
||||
|
||||
hsvData.hue /= 360.f;
|
||||
hsvData.saturation = max == 0.f ? 0.f : delta / max;
|
||||
hsvData.value = max;
|
||||
hsv_data.hue /= 360.f;
|
||||
hsv_data.saturation = max == 0.f ? 0.f : delta / max;
|
||||
hsv_data.value = max;
|
||||
|
||||
return hsvData;
|
||||
return hsv_data;
|
||||
}
|
||||
|
||||
constexpr explicit Color(const Vector4& vec) : Vector4(vec)
|
||||
constexpr explicit Color(const Vector4& vec) noexcept: Vector4(vec)
|
||||
{
|
||||
Clamp(0.f, 1.f);
|
||||
clamp(0.f, 1.f);
|
||||
}
|
||||
constexpr void SetHue(const float hue)
|
||||
constexpr void set_hue(const float hue) noexcept
|
||||
{
|
||||
auto hsv = ToHSV();
|
||||
auto hsv = to_hsv();
|
||||
hsv.hue = hue;
|
||||
|
||||
*this = FromHSV(hsv);
|
||||
*this = from_hsv(hsv);
|
||||
}
|
||||
|
||||
constexpr void SetSaturation(const float saturation)
|
||||
constexpr void set_saturation(const float saturation) noexcept
|
||||
{
|
||||
auto hsv = ToHSV();
|
||||
auto hsv = to_hsv();
|
||||
hsv.saturation = saturation;
|
||||
|
||||
*this = FromHSV(hsv);
|
||||
*this = from_hsv(hsv);
|
||||
}
|
||||
|
||||
constexpr void SetValue(const float value)
|
||||
constexpr void set_value(const float value) noexcept
|
||||
{
|
||||
auto hsv = ToHSV();
|
||||
auto hsv = to_hsv();
|
||||
hsv.value = value;
|
||||
|
||||
*this = FromHSV(hsv);
|
||||
*this = from_hsv(hsv);
|
||||
}
|
||||
[[nodiscard]]
|
||||
constexpr Color Blend(const Color& other, float ratio) const
|
||||
constexpr Color blend(const Color& other, float ratio) const noexcept
|
||||
{
|
||||
ratio = std::clamp(ratio, 0.f, 1.f);
|
||||
return Color(*this * (1.f - ratio) + other * ratio);
|
||||
}
|
||||
|
||||
[[nodiscard]] static constexpr Color Red()
|
||||
[[nodiscard]] static constexpr Color red()
|
||||
{
|
||||
return {1.f, 0.f, 0.f, 1.f};
|
||||
}
|
||||
[[nodiscard]] static constexpr Color Green()
|
||||
[[nodiscard]] static constexpr Color green()
|
||||
{
|
||||
return {0.f, 1.f, 0.f, 1.f};
|
||||
}
|
||||
[[nodiscard]] static constexpr Color Blue()
|
||||
[[nodiscard]] static constexpr Color blue()
|
||||
{
|
||||
return {0.f, 0.f, 1.f, 1.f};
|
||||
}
|
||||
#ifdef OMATH_IMGUI_INTEGRATION
|
||||
[[nodiscard]]
|
||||
ImColor to_im_color() const noexcept
|
||||
{
|
||||
return {to_im_vec4()};
|
||||
}
|
||||
#endif
|
||||
};
|
||||
} // namespace omath
|
||||
|
||||
@@ -5,17 +5,9 @@
|
||||
#pragma once
|
||||
#include "omath/engines/iw_engine/constants.hpp"
|
||||
#include "omath/projection/camera.hpp"
|
||||
#include "traits/camera_trait.hpp"
|
||||
|
||||
namespace omath::iw_engine
|
||||
{
|
||||
class Camera final : public projection::Camera<Mat4x4, ViewAngles>
|
||||
{
|
||||
public:
|
||||
Camera(const Vector3<float>& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort,
|
||||
const Angle<float, 0.f, 180.f, AngleFlags::Clamped>& fov, float near, float far);
|
||||
void LookAt(const Vector3<float>& target) override;
|
||||
protected:
|
||||
[[nodiscard]] Mat4x4 CalcViewMatrix() const override;
|
||||
[[nodiscard]] Mat4x4 CalcProjectionMatrix() const override;
|
||||
};
|
||||
}
|
||||
using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait>;
|
||||
} // namespace omath::iw_engine
|
||||
@@ -3,23 +3,23 @@
|
||||
//
|
||||
|
||||
#pragma once
|
||||
#include <omath/vector3.hpp>
|
||||
#include <omath/mat.hpp>
|
||||
#include <omath/angle.hpp>
|
||||
#include <omath/mat.hpp>
|
||||
#include <omath/vector3.hpp>
|
||||
#include <omath/view_angles.hpp>
|
||||
|
||||
namespace omath::iw_engine
|
||||
{
|
||||
constexpr Vector3<float> kAbsUp = {0, 0, 1};
|
||||
constexpr Vector3<float> kAbsRight = {0, -1, 0};
|
||||
constexpr Vector3<float> kAbsForward = {1, 0, 0};
|
||||
constexpr Vector3<float> k_abs_up = {0, 0, 1};
|
||||
constexpr Vector3<float> k_abs_right = {0, -1, 0};
|
||||
constexpr Vector3<float> k_abs_forward = {1, 0, 0};
|
||||
|
||||
using Mat4x4 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>;
|
||||
using Mat3x3 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>;
|
||||
using Mat1x3 = Mat<1, 3, float, MatStoreType::ROW_MAJOR>;
|
||||
using Mat4X4 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>;
|
||||
using Mat3X3 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>;
|
||||
using Mat1X3 = Mat<1, 3, float, MatStoreType::ROW_MAJOR>;
|
||||
using PitchAngle = Angle<float, -89.f, 89.f, AngleFlags::Clamped>;
|
||||
using YawAngle = Angle<float, -180.f, 180.f, AngleFlags::Normalized>;
|
||||
using RollAngle = Angle<float, -180.f, 180.f, AngleFlags::Normalized>;
|
||||
|
||||
using ViewAngles = omath::ViewAngles<PitchAngle, YawAngle, RollAngle>;
|
||||
}
|
||||
} // namespace omath::iw_engine
|
||||
@@ -8,19 +8,19 @@
|
||||
namespace omath::iw_engine
|
||||
{
|
||||
[[nodiscard]]
|
||||
Vector3<float> ForwardVector(const ViewAngles& angles);
|
||||
Vector3<float> forward_vector(const ViewAngles& angles) noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
Vector3<float> RightVector(const ViewAngles& angles);
|
||||
Vector3<float> right_vector(const ViewAngles& angles) noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
Vector3<float> UpVector(const ViewAngles& angles);
|
||||
Vector3<float> up_vector(const ViewAngles& angles) noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
Mat4x4 RotationMatrix(const ViewAngles& angles);
|
||||
Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept;
|
||||
|
||||
[[nodiscard]] Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3<float>& cam_origin);
|
||||
[[nodiscard]] Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
Mat4x4 CalcPerspectiveProjectionMatrix(float fieldOfView, float aspectRatio, float near, float far);
|
||||
Mat4X4 calc_perspective_projection_matrix(float field_of_view, float aspect_ratio, float near, float far) noexcept;
|
||||
} // namespace omath::iw_engine
|
||||
|
||||
24
include/omath/engines/iw_engine/traits/camera_trait.hpp
Normal file
24
include/omath/engines/iw_engine/traits/camera_trait.hpp
Normal file
@@ -0,0 +1,24 @@
|
||||
//
|
||||
// Created by Vlad on 8/10/2025.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
#include "omath/engines/iw_engine/constants.hpp"
|
||||
#include "omath/projection/camera.hpp"
|
||||
|
||||
namespace omath::iw_engine
|
||||
{
|
||||
class CameraTrait final
|
||||
{
|
||||
public:
|
||||
[[nodiscard]]
|
||||
static ViewAngles calc_look_at_angle(const Vector3<float>& cam_origin, const Vector3<float>& look_at) noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept;
|
||||
[[nodiscard]]
|
||||
static Mat4X4 calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port,
|
||||
float near, float far) noexcept;
|
||||
};
|
||||
|
||||
} // namespace omath::iw_engine
|
||||
79
include/omath/engines/iw_engine/traits/pred_engine_trait.hpp
Normal file
79
include/omath/engines/iw_engine/traits/pred_engine_trait.hpp
Normal file
@@ -0,0 +1,79 @@
|
||||
//
|
||||
// Created by Vlad on 8/6/2025.
|
||||
//
|
||||
#pragma once
|
||||
|
||||
#include "omath/engines/iw_engine/formulas.hpp"
|
||||
#include "omath/projectile_prediction/projectile.hpp"
|
||||
#include "omath/projectile_prediction/target.hpp"
|
||||
#include <optional>
|
||||
|
||||
namespace omath::iw_engine
|
||||
{
|
||||
class PredEngineTrait final
|
||||
{
|
||||
public:
|
||||
constexpr static Vector3<float> predict_projectile_position(const projectile_prediction::Projectile& projectile,
|
||||
const float pitch, const float yaw,
|
||||
const float time, const float gravity) noexcept
|
||||
{
|
||||
auto current_pos = projectile.m_origin
|
||||
+ forward_vector({PitchAngle::from_degrees(-pitch), YawAngle::from_degrees(yaw),
|
||||
RollAngle::from_degrees(0)})
|
||||
* projectile.m_launch_speed * time;
|
||||
current_pos.z -= (gravity * projectile.m_gravity_scale) * (time * time) * 0.5f;
|
||||
|
||||
return current_pos;
|
||||
}
|
||||
[[nodiscard]]
|
||||
static constexpr Vector3<float> predict_target_position(const projectile_prediction::Target& target,
|
||||
const float time, const float gravity) noexcept
|
||||
{
|
||||
auto predicted = target.m_origin + target.m_velocity * time;
|
||||
|
||||
if (target.m_is_airborne)
|
||||
predicted.z -= gravity * (time * time) * 0.5f;
|
||||
|
||||
return predicted;
|
||||
}
|
||||
[[nodiscard]]
|
||||
static float calc_vector_2d_distance(const Vector3<float>& delta) noexcept
|
||||
{
|
||||
return std::sqrt(delta.x * delta.x + delta.y * delta.y);
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr static float get_vector_height_coordinate(const Vector3<float>& vec) noexcept
|
||||
{
|
||||
return vec.z;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
static Vector3<float> calc_viewpoint_from_angles(const projectile_prediction::Projectile& projectile,
|
||||
Vector3<float> predicted_target_position,
|
||||
const std::optional<float> projectile_pitch) noexcept
|
||||
{
|
||||
const auto delta2d = calc_vector_2d_distance(predicted_target_position - projectile.m_origin);
|
||||
const auto height = delta2d * std::tan(angles::degrees_to_radians(projectile_pitch.value()));
|
||||
|
||||
return {predicted_target_position.x, predicted_target_position.y, projectile.m_origin.z + height};
|
||||
}
|
||||
// Due to specification of maybe_calculate_projectile_launch_pitch_angle, pitch angle must be:
|
||||
// 89 look up, -89 look down
|
||||
[[nodiscard]]
|
||||
static float calc_direct_pitch_angle(const Vector3<float>& origin, const Vector3<float>& view_to) noexcept
|
||||
{
|
||||
const auto distance = origin.distance_to(view_to);
|
||||
const auto delta = view_to - origin;
|
||||
|
||||
return angles::radians_to_degrees(std::asin(delta.z / distance));
|
||||
}
|
||||
[[nodiscard]]
|
||||
static float calc_direct_yaw_angle(const Vector3<float>& origin, const Vector3<float>& view_to) noexcept
|
||||
{
|
||||
const auto delta = view_to - origin;
|
||||
|
||||
return angles::radians_to_degrees(std::atan2(delta.y, delta.x));
|
||||
};
|
||||
};
|
||||
} // namespace omath::iw_engine
|
||||
@@ -4,16 +4,9 @@
|
||||
#pragma once
|
||||
#include "omath/engines/opengl_engine/constants.hpp"
|
||||
#include "omath/projection/camera.hpp"
|
||||
#include "traits/camera_trait.hpp"
|
||||
|
||||
namespace omath::opengl_engine
|
||||
{
|
||||
class Camera final : public projection::Camera<Mat4x4, ViewAngles>
|
||||
{
|
||||
public:
|
||||
Camera(const Vector3<float>& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort,
|
||||
const Angle<float, 0.f, 180.f, AngleFlags::Clamped>& fov, float near, float far);
|
||||
void LookAt(const Vector3<float>& target) override;
|
||||
[[nodiscard]] Mat4x4 CalcViewMatrix() const override;
|
||||
[[nodiscard]] Mat4x4 CalcProjectionMatrix() const override;
|
||||
};
|
||||
}
|
||||
using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait>;
|
||||
} // namespace omath::opengl_engine
|
||||
@@ -10,17 +10,16 @@
|
||||
|
||||
namespace omath::opengl_engine
|
||||
{
|
||||
constexpr Vector3<float> kAbsUp = {0, 1, 0};
|
||||
constexpr Vector3<float> kAbsRight = {1, 0, 0};
|
||||
constexpr Vector3<float> kAbsForward = {0, 0, -1};
|
||||
constexpr Vector3<float> k_abs_up = {0, 1, 0};
|
||||
constexpr Vector3<float> k_abs_right = {1, 0, 0};
|
||||
constexpr Vector3<float> k_abs_forward = {0, 0, -1};
|
||||
|
||||
using Mat4x4 = Mat<4, 4, float, MatStoreType::COLUMN_MAJOR>;
|
||||
using Mat3x3 = Mat<4, 4, float, MatStoreType::COLUMN_MAJOR>;
|
||||
using Mat1x3 = Mat<1, 3, float, MatStoreType::COLUMN_MAJOR>;
|
||||
using Mat4X4 = Mat<4, 4, float, MatStoreType::COLUMN_MAJOR>;
|
||||
using Mat3X3 = Mat<4, 4, float, MatStoreType::COLUMN_MAJOR>;
|
||||
using Mat1X3 = Mat<1, 3, float, MatStoreType::COLUMN_MAJOR>;
|
||||
using PitchAngle = Angle<float, -90.f, 90.f, AngleFlags::Clamped>;
|
||||
using YawAngle = Angle<float, -180.f, 180.f, AngleFlags::Normalized>;
|
||||
using RollAngle = Angle<float, -180.f, 180.f, AngleFlags::Normalized>;
|
||||
|
||||
|
||||
using ViewAngles = omath::ViewAngles<PitchAngle, YawAngle, RollAngle>;
|
||||
}
|
||||
} // namespace omath::opengl_engine
|
||||
@@ -8,19 +8,19 @@
|
||||
namespace omath::opengl_engine
|
||||
{
|
||||
[[nodiscard]]
|
||||
Vector3<float> ForwardVector(const ViewAngles& angles);
|
||||
Vector3<float> forward_vector(const ViewAngles& angles) noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
Vector3<float> RightVector(const ViewAngles& angles);
|
||||
Vector3<float> right_vector(const ViewAngles& angles) noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
Vector3<float> UpVector(const ViewAngles& angles);
|
||||
Vector3<float> up_vector(const ViewAngles& angles) noexcept;
|
||||
|
||||
[[nodiscard]] Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3<float>& cam_origin);
|
||||
[[nodiscard]] Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
Mat4x4 RotationMatrix(const ViewAngles& angles);
|
||||
Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
Mat4x4 CalcPerspectiveProjectionMatrix(float fieldOfView, float aspectRatio, float near, float far);
|
||||
Mat4X4 calc_perspective_projection_matrix(float field_of_view, float aspect_ratio, float near, float far) noexcept;
|
||||
} // namespace omath::opengl_engine
|
||||
|
||||
24
include/omath/engines/opengl_engine/traits/camera_trait.hpp
Normal file
24
include/omath/engines/opengl_engine/traits/camera_trait.hpp
Normal file
@@ -0,0 +1,24 @@
|
||||
//
|
||||
// Created by Vlad on 8/10/2025.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
#include "omath/engines/opengl_engine/constants.hpp"
|
||||
#include "omath/projection/camera.hpp"
|
||||
|
||||
namespace omath::opengl_engine
|
||||
{
|
||||
class CameraTrait final
|
||||
{
|
||||
public:
|
||||
[[nodiscard]]
|
||||
static ViewAngles calc_look_at_angle(const Vector3<float>& cam_origin, const Vector3<float>& look_at) noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept;
|
||||
[[nodiscard]]
|
||||
static Mat4X4 calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port,
|
||||
float near, float far) noexcept;
|
||||
};
|
||||
|
||||
} // namespace omath::opengl_engine
|
||||
@@ -0,0 +1,78 @@
|
||||
//
|
||||
// Created by Vlad on 8/6/2025.
|
||||
//
|
||||
#pragma once
|
||||
#include "omath/engines/opengl_engine/formulas.hpp"
|
||||
#include "omath/projectile_prediction/projectile.hpp"
|
||||
#include "omath/projectile_prediction/target.hpp"
|
||||
#include <optional>
|
||||
|
||||
namespace omath::opengl_engine
|
||||
{
|
||||
class PredEngineTrait final
|
||||
{
|
||||
public:
|
||||
constexpr static Vector3<float> predict_projectile_position(const projectile_prediction::Projectile& projectile,
|
||||
const float pitch, const float yaw,
|
||||
const float time, const float gravity) noexcept
|
||||
{
|
||||
auto current_pos = projectile.m_origin
|
||||
+ forward_vector({PitchAngle::from_degrees(-pitch), YawAngle::from_degrees(yaw),
|
||||
RollAngle::from_degrees(0)})
|
||||
* projectile.m_launch_speed * time;
|
||||
current_pos.y -= (gravity * projectile.m_gravity_scale) * (time * time) * 0.5f;
|
||||
|
||||
return current_pos;
|
||||
}
|
||||
[[nodiscard]]
|
||||
static constexpr Vector3<float> predict_target_position(const projectile_prediction::Target& target,
|
||||
const float time, const float gravity) noexcept
|
||||
{
|
||||
auto predicted = target.m_origin + target.m_velocity * time;
|
||||
|
||||
if (target.m_is_airborne)
|
||||
predicted.y -= gravity * (time * time) * 0.5f;
|
||||
|
||||
return predicted;
|
||||
}
|
||||
[[nodiscard]]
|
||||
static float calc_vector_2d_distance(const Vector3<float>& delta) noexcept
|
||||
{
|
||||
return std::sqrt(delta.x * delta.x + delta.z * delta.z);
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr static float get_vector_height_coordinate(const Vector3<float>& vec) noexcept
|
||||
{
|
||||
return vec.y;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
static Vector3<float> calc_viewpoint_from_angles(const projectile_prediction::Projectile& projectile,
|
||||
Vector3<float> predicted_target_position,
|
||||
const std::optional<float> projectile_pitch) noexcept
|
||||
{
|
||||
const auto delta2d = calc_vector_2d_distance(predicted_target_position - projectile.m_origin);
|
||||
const auto height = delta2d * std::tan(angles::degrees_to_radians(projectile_pitch.value()));
|
||||
|
||||
return {predicted_target_position.x, predicted_target_position.y + height, projectile.m_origin.z};
|
||||
}
|
||||
// Due to specification of maybe_calculate_projectile_launch_pitch_angle, pitch angle must be:
|
||||
// 89 look up, -89 look down
|
||||
[[nodiscard]]
|
||||
static float calc_direct_pitch_angle(const Vector3<float>& origin, const Vector3<float>& view_to) noexcept
|
||||
{
|
||||
const auto distance = origin.distance_to(view_to);
|
||||
const auto delta = view_to - origin;
|
||||
|
||||
return angles::radians_to_degrees(std::asin(delta.y / distance));
|
||||
}
|
||||
[[nodiscard]]
|
||||
static float calc_direct_yaw_angle(const Vector3<float>& origin, const Vector3<float>& view_to) noexcept
|
||||
{
|
||||
const auto delta = view_to - origin;
|
||||
|
||||
return angles::radians_to_degrees(std::atan2(delta.z, delta.x));
|
||||
};
|
||||
};
|
||||
} // namespace omath::opengl_engine
|
||||
@@ -4,17 +4,8 @@
|
||||
#pragma once
|
||||
#include "omath/engines/source_engine/constants.hpp"
|
||||
#include "omath/projection/camera.hpp"
|
||||
|
||||
#include "traits/camera_trait.hpp"
|
||||
namespace omath::source_engine
|
||||
{
|
||||
class Camera final : public projection::Camera<Mat4x4, ViewAngles>
|
||||
{
|
||||
public:
|
||||
Camera(const Vector3<float>& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort,
|
||||
const Angle<float, 0.f, 180.f, AngleFlags::Clamped>& fov, float near, float far);
|
||||
void LookAt(const Vector3<float>& target) override;
|
||||
protected:
|
||||
[[nodiscard]] Mat4x4 CalcViewMatrix() const override;
|
||||
[[nodiscard]] Mat4x4 CalcProjectionMatrix() const override;
|
||||
};
|
||||
}
|
||||
using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait>;
|
||||
} // namespace omath::source_engine
|
||||
@@ -3,23 +3,23 @@
|
||||
//
|
||||
#pragma once
|
||||
|
||||
#include <omath/vector3.hpp>
|
||||
#include <omath/mat.hpp>
|
||||
#include <omath/angle.hpp>
|
||||
#include <omath/mat.hpp>
|
||||
#include <omath/vector3.hpp>
|
||||
#include <omath/view_angles.hpp>
|
||||
|
||||
namespace omath::source_engine
|
||||
{
|
||||
constexpr Vector3<float> kAbsUp = {0, 0, 1};
|
||||
constexpr Vector3<float> kAbsRight = {0, -1, 0};
|
||||
constexpr Vector3<float> kAbsForward = {1, 0, 0};
|
||||
constexpr Vector3<float> k_abs_up = {0, 0, 1};
|
||||
constexpr Vector3<float> k_abs_right = {0, -1, 0};
|
||||
constexpr Vector3<float> k_abs_forward = {1, 0, 0};
|
||||
|
||||
using Mat4x4 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>;
|
||||
using Mat3x3 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>;
|
||||
using Mat1x3 = Mat<1, 3, float, MatStoreType::ROW_MAJOR>;
|
||||
using Mat4X4 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>;
|
||||
using Mat3X3 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>;
|
||||
using Mat1X3 = Mat<1, 3, float, MatStoreType::ROW_MAJOR>;
|
||||
using PitchAngle = Angle<float, -89.f, 89.f, AngleFlags::Clamped>;
|
||||
using YawAngle = Angle<float, -180.f, 180.f, AngleFlags::Normalized>;
|
||||
using RollAngle = Angle<float, -180.f, 180.f, AngleFlags::Normalized>;
|
||||
|
||||
using ViewAngles = omath::ViewAngles<PitchAngle, YawAngle, RollAngle>;
|
||||
} // namespace omath::source
|
||||
} // namespace omath::source_engine
|
||||
|
||||
@@ -7,19 +7,19 @@
|
||||
namespace omath::source_engine
|
||||
{
|
||||
[[nodiscard]]
|
||||
Vector3<float> ForwardVector(const ViewAngles& angles);
|
||||
Vector3<float> forward_vector(const ViewAngles& angles) noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
Mat4x4 RotationMatrix(const ViewAngles& angles);
|
||||
Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
Vector3<float> RightVector(const ViewAngles& angles);
|
||||
Vector3<float> right_vector(const ViewAngles& angles) noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
Vector3<float> UpVector(const ViewAngles& angles);
|
||||
Vector3<float> up_vector(const ViewAngles& angles) noexcept;
|
||||
|
||||
[[nodiscard]] Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3<float>& cam_origin);
|
||||
[[nodiscard]] Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
Mat4x4 CalcPerspectiveProjectionMatrix(float fieldOfView, float aspectRatio, float near, float far);
|
||||
} // namespace omath::source
|
||||
Mat4X4 calc_perspective_projection_matrix(float field_of_view, float aspect_ratio, float near, float far) noexcept;
|
||||
} // namespace omath::source_engine
|
||||
|
||||
24
include/omath/engines/source_engine/traits/camera_trait.hpp
Normal file
24
include/omath/engines/source_engine/traits/camera_trait.hpp
Normal file
@@ -0,0 +1,24 @@
|
||||
//
|
||||
// Created by Vlad on 8/10/2025.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
#include "omath/engines/source_engine/constants.hpp"
|
||||
#include "omath/projection/camera.hpp"
|
||||
|
||||
namespace omath::source_engine
|
||||
{
|
||||
class CameraTrait final
|
||||
{
|
||||
public:
|
||||
[[nodiscard]]
|
||||
static ViewAngles calc_look_at_angle(const Vector3<float>& cam_origin, const Vector3<float>& look_at) noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept;
|
||||
[[nodiscard]]
|
||||
static Mat4X4 calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port,
|
||||
float near, float far) noexcept;
|
||||
};
|
||||
|
||||
} // namespace omath::source_engine
|
||||
@@ -0,0 +1,79 @@
|
||||
//
|
||||
// Created by Vlad on 8/3/2025.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
#include "omath/engines/source_engine/formulas.hpp"
|
||||
#include "omath/projectile_prediction/projectile.hpp"
|
||||
#include "omath/projectile_prediction/target.hpp"
|
||||
#include <optional>
|
||||
|
||||
namespace omath::source_engine
|
||||
{
|
||||
class PredEngineTrait final
|
||||
{
|
||||
public:
|
||||
constexpr static Vector3<float> predict_projectile_position(const projectile_prediction::Projectile& projectile,
|
||||
const float pitch, const float yaw,
|
||||
const float time, const float gravity) noexcept
|
||||
{
|
||||
auto current_pos = projectile.m_origin
|
||||
+ forward_vector({PitchAngle::from_degrees(-pitch), YawAngle::from_degrees(yaw),
|
||||
RollAngle::from_degrees(0)})
|
||||
* projectile.m_launch_speed * time;
|
||||
current_pos.z -= (gravity * projectile.m_gravity_scale) * (time * time) * 0.5f;
|
||||
|
||||
return current_pos;
|
||||
}
|
||||
[[nodiscard]]
|
||||
static constexpr Vector3<float> predict_target_position(const projectile_prediction::Target& target,
|
||||
const float time, const float gravity) noexcept
|
||||
{
|
||||
auto predicted = target.m_origin + target.m_velocity * time;
|
||||
|
||||
if (target.m_is_airborne)
|
||||
predicted.z -= gravity * (time * time) * 0.5f;
|
||||
|
||||
return predicted;
|
||||
}
|
||||
[[nodiscard]]
|
||||
static float calc_vector_2d_distance(const Vector3<float>& delta) noexcept
|
||||
{
|
||||
return std::sqrt(delta.x * delta.x + delta.y * delta.y);
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr static float get_vector_height_coordinate(const Vector3<float>& vec) noexcept
|
||||
{
|
||||
return vec.z;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
static Vector3<float> calc_viewpoint_from_angles(const projectile_prediction::Projectile& projectile,
|
||||
Vector3<float> predicted_target_position,
|
||||
const std::optional<float> projectile_pitch) noexcept
|
||||
{
|
||||
const auto delta2d = calc_vector_2d_distance(predicted_target_position - projectile.m_origin);
|
||||
const auto height = delta2d * std::tan(angles::degrees_to_radians(projectile_pitch.value()));
|
||||
|
||||
return {predicted_target_position.x, predicted_target_position.y, projectile.m_origin.z + height};
|
||||
}
|
||||
// Due to specification of maybe_calculate_projectile_launch_pitch_angle, pitch angle must be:
|
||||
// 89 look up, -89 look down
|
||||
[[nodiscard]]
|
||||
static float calc_direct_pitch_angle(const Vector3<float>& origin, const Vector3<float>& view_to) noexcept
|
||||
{
|
||||
const auto distance = origin.distance_to(view_to);
|
||||
const auto delta = view_to - origin;
|
||||
|
||||
return angles::radians_to_degrees(std::asin(delta.z / distance));
|
||||
}
|
||||
[[nodiscard]]
|
||||
static float calc_direct_yaw_angle(const Vector3<float>& origin, const Vector3<float>& view_to) noexcept
|
||||
{
|
||||
const auto delta = view_to - origin;
|
||||
|
||||
return angles::radians_to_degrees(std::atan2(delta.y, delta.x));
|
||||
};
|
||||
};
|
||||
} // namespace omath::source_engine
|
||||
@@ -5,17 +5,9 @@
|
||||
#pragma once
|
||||
#include "omath/engines/unity_engine/constants.hpp"
|
||||
#include "omath/projection/camera.hpp"
|
||||
#include "traits/camera_trait.hpp"
|
||||
|
||||
namespace omath::unity_engine
|
||||
{
|
||||
class Camera final : public projection::Camera<Mat4x4, ViewAngles>
|
||||
{
|
||||
public:
|
||||
Camera(const Vector3<float>& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort,
|
||||
const Angle<float, 0.f, 180.f, AngleFlags::Clamped>& fov, float near, float far);
|
||||
void LookAt(const Vector3<float>& target) override;
|
||||
protected:
|
||||
[[nodiscard]] Mat4x4 CalcViewMatrix() const override;
|
||||
[[nodiscard]] Mat4x4 CalcProjectionMatrix() const override;
|
||||
};
|
||||
}
|
||||
using Camera = projection::Camera<Mat4X4, ViewAngles, CameraTrait>;
|
||||
} // namespace omath::unity_engine
|
||||
@@ -4,23 +4,23 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <omath/vector3.hpp>
|
||||
#include <omath/mat.hpp>
|
||||
#include <omath/angle.hpp>
|
||||
#include <omath/mat.hpp>
|
||||
#include <omath/vector3.hpp>
|
||||
#include <omath/view_angles.hpp>
|
||||
|
||||
namespace omath::unity_engine
|
||||
{
|
||||
constexpr Vector3<float> kAbsUp = {0, 1, 0};
|
||||
constexpr Vector3<float> kAbsRight = {1, 0, 0};
|
||||
constexpr Vector3<float> kAbsForward = {0, 0, 1};
|
||||
constexpr Vector3<float> k_abs_up = {0, 1, 0};
|
||||
constexpr Vector3<float> k_abs_right = {1, 0, 0};
|
||||
constexpr Vector3<float> k_abs_forward = {0, 0, 1};
|
||||
|
||||
using Mat4x4 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>;
|
||||
using Mat3x3 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>;
|
||||
using Mat1x3 = Mat<1, 3, float, MatStoreType::ROW_MAJOR>;
|
||||
using Mat4X4 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>;
|
||||
using Mat3X3 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>;
|
||||
using Mat1X3 = Mat<1, 3, float, MatStoreType::ROW_MAJOR>;
|
||||
using PitchAngle = Angle<float, -90.f, 90.f, AngleFlags::Clamped>;
|
||||
using YawAngle = Angle<float, -180.f, 180.f, AngleFlags::Normalized>;
|
||||
using RollAngle = Angle<float, -180.f, 180.f, AngleFlags::Normalized>;
|
||||
|
||||
using ViewAngles = omath::ViewAngles<PitchAngle, YawAngle, RollAngle>;
|
||||
} // namespace omath::source
|
||||
} // namespace omath::unity_engine
|
||||
|
||||
@@ -8,19 +8,19 @@
|
||||
namespace omath::unity_engine
|
||||
{
|
||||
[[nodiscard]]
|
||||
Vector3<float> ForwardVector(const ViewAngles& angles);
|
||||
Vector3<float> forward_vector(const ViewAngles& angles) noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
Vector3<float> RightVector(const ViewAngles& angles);
|
||||
Vector3<float> right_vector(const ViewAngles& angles) noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
Vector3<float> UpVector(const ViewAngles& angles);
|
||||
Vector3<float> up_vector(const ViewAngles& angles) noexcept;
|
||||
|
||||
[[nodiscard]] Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3<float>& cam_origin);
|
||||
[[nodiscard]] Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
Mat4x4 RotationMatrix(const ViewAngles& angles);
|
||||
Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
Mat4x4 CalcPerspectiveProjectionMatrix(float fieldOfView, float aspectRatio, float near, float far);
|
||||
} // namespace omath::source
|
||||
Mat4X4 calc_perspective_projection_matrix(float field_of_view, float aspect_ratio, float near, float far) noexcept;
|
||||
} // namespace omath::unity_engine
|
||||
|
||||
24
include/omath/engines/unity_engine/traits/camera_trait.hpp
Normal file
24
include/omath/engines/unity_engine/traits/camera_trait.hpp
Normal file
@@ -0,0 +1,24 @@
|
||||
//
|
||||
// Created by Vlad on 8/10/2025.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
#include "omath/engines/unity_engine/formulas.hpp"
|
||||
#include "omath/projection/camera.hpp"
|
||||
|
||||
namespace omath::unity_engine
|
||||
{
|
||||
class CameraTrait final
|
||||
{
|
||||
public:
|
||||
[[nodiscard]]
|
||||
static ViewAngles calc_look_at_angle(const Vector3<float>& cam_origin, const Vector3<float>& look_at) noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept;
|
||||
[[nodiscard]]
|
||||
static Mat4X4 calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port,
|
||||
float near, float far) noexcept;
|
||||
};
|
||||
|
||||
} // namespace omath::unity_engine
|
||||
@@ -0,0 +1,78 @@
|
||||
//
|
||||
// Created by Vlad on 8/6/2025.
|
||||
//
|
||||
#pragma once
|
||||
#include "omath/engines/unity_engine/formulas.hpp"
|
||||
#include "omath/projectile_prediction/projectile.hpp"
|
||||
#include "omath/projectile_prediction/target.hpp"
|
||||
#include <optional>
|
||||
|
||||
namespace omath::unity_engine
|
||||
{
|
||||
class PredEngineTrait final
|
||||
{
|
||||
public:
|
||||
constexpr static Vector3<float> predict_projectile_position(const projectile_prediction::Projectile& projectile,
|
||||
const float pitch, const float yaw,
|
||||
const float time, const float gravity) noexcept
|
||||
{
|
||||
auto current_pos = projectile.m_origin
|
||||
+ forward_vector({PitchAngle::from_degrees(-pitch), YawAngle::from_degrees(yaw),
|
||||
RollAngle::from_degrees(0)})
|
||||
* projectile.m_launch_speed * time;
|
||||
current_pos.y -= (gravity * projectile.m_gravity_scale) * (time * time) * 0.5f;
|
||||
|
||||
return current_pos;
|
||||
}
|
||||
[[nodiscard]]
|
||||
static constexpr Vector3<float> predict_target_position(const projectile_prediction::Target& target,
|
||||
const float time, const float gravity) noexcept
|
||||
{
|
||||
auto predicted = target.m_origin + target.m_velocity * time;
|
||||
|
||||
if (target.m_is_airborne)
|
||||
predicted.y -= gravity * (time * time) * 0.5f;
|
||||
|
||||
return predicted;
|
||||
}
|
||||
[[nodiscard]]
|
||||
static float calc_vector_2d_distance(const Vector3<float>& delta) noexcept
|
||||
{
|
||||
return std::sqrt(delta.x * delta.x + delta.z * delta.z);
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr static float get_vector_height_coordinate(const Vector3<float>& vec) noexcept
|
||||
{
|
||||
return vec.y;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
static Vector3<float> calc_viewpoint_from_angles(const projectile_prediction::Projectile& projectile,
|
||||
Vector3<float> predicted_target_position,
|
||||
const std::optional<float> projectile_pitch) noexcept
|
||||
{
|
||||
const auto delta2d = calc_vector_2d_distance(predicted_target_position - projectile.m_origin);
|
||||
const auto height = delta2d * std::tan(angles::degrees_to_radians(projectile_pitch.value()));
|
||||
|
||||
return {predicted_target_position.x, predicted_target_position.y + height, projectile.m_origin.z};
|
||||
}
|
||||
// Due to specification of maybe_calculate_projectile_launch_pitch_angle, pitch angle must be:
|
||||
// 89 look up, -89 look down
|
||||
[[nodiscard]]
|
||||
static float calc_direct_pitch_angle(const Vector3<float>& origin, const Vector3<float>& view_to) noexcept
|
||||
{
|
||||
const auto distance = origin.distance_to(view_to);
|
||||
const auto delta = view_to - origin;
|
||||
|
||||
return angles::radians_to_degrees(std::asin(delta.y / distance));
|
||||
}
|
||||
[[nodiscard]]
|
||||
static float calc_direct_yaw_angle(const Vector3<float>& origin, const Vector3<float>& view_to) noexcept
|
||||
{
|
||||
const auto delta = view_to - origin;
|
||||
|
||||
return angles::radians_to_degrees(std::atan2(delta.z, delta.x));
|
||||
};
|
||||
};
|
||||
} // namespace omath::unity_engine
|
||||
@@ -2,21 +2,19 @@
|
||||
// Created by vlad on 9/29/2024.
|
||||
//
|
||||
#pragma once
|
||||
#include "omath/vector3.hpp"
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <iomanip>
|
||||
#include <numeric>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <utility>
|
||||
#include "omath/vector3.hpp"
|
||||
#include <numeric>
|
||||
|
||||
|
||||
#ifdef near
|
||||
#undef near
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef far
|
||||
#undef far
|
||||
#endif
|
||||
@@ -34,25 +32,22 @@ namespace omath
|
||||
COLUMN_MAJOR
|
||||
};
|
||||
|
||||
|
||||
template<typename M1, typename M2>
|
||||
concept MatTemplateEqual =
|
||||
(M1::rows == M2::rows) && (M1::columns == M2::columns) &&
|
||||
std::is_same_v<typename M1::value_type, typename M2::value_type> &&
|
||||
(M1::store_type == M2::store_type);
|
||||
template<typename M1, typename M2> concept MatTemplateEqual
|
||||
= (M1::rows == M2::rows) && (M1::columns == M2::columns)
|
||||
&& std::is_same_v<typename M1::value_type, typename M2::value_type> && (M1::store_type == M2::store_type);
|
||||
|
||||
template<size_t Rows = 0, size_t Columns = 0, class Type = float, MatStoreType StoreType = MatStoreType::ROW_MAJOR>
|
||||
requires std::is_arithmetic_v<Type>
|
||||
requires std::is_arithmetic_v<Type>
|
||||
class Mat final
|
||||
{
|
||||
public:
|
||||
constexpr Mat() noexcept
|
||||
{
|
||||
Clear();
|
||||
clear();
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr static MatStoreType GetStoreOrdering() noexcept
|
||||
constexpr static MatStoreType get_store_ordering() noexcept
|
||||
{
|
||||
return StoreType;
|
||||
}
|
||||
@@ -61,24 +56,24 @@ namespace omath
|
||||
if (rows.size() != Rows)
|
||||
throw std::invalid_argument("Initializer list rows size does not match template parameter Rows");
|
||||
|
||||
auto rowIt = rows.begin();
|
||||
for (size_t i = 0; i < Rows; ++i, ++rowIt)
|
||||
auto row_it = rows.begin();
|
||||
for (size_t i = 0; i < Rows; ++i, ++row_it)
|
||||
{
|
||||
if (rowIt->size() != Columns)
|
||||
if (row_it->size() != Columns)
|
||||
throw std::invalid_argument(
|
||||
"All rows must have the same number of columns as template parameter Columns");
|
||||
|
||||
auto colIt = rowIt->begin();
|
||||
for (size_t j = 0; j < Columns; ++j, ++colIt)
|
||||
auto col_it = row_it->begin();
|
||||
for (size_t j = 0; j < Columns; ++j, ++col_it)
|
||||
{
|
||||
At(i, j) = std::move(*colIt);
|
||||
at(i, j) = std::move(*col_it);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
constexpr explicit Mat(const Type* rawData)
|
||||
constexpr explicit Mat(const Type* raw_data)
|
||||
{
|
||||
std::copy_n(rawData, Rows * Columns, m_data.begin());
|
||||
std::copy_n(raw_data, Rows * Columns, m_data.begin());
|
||||
}
|
||||
|
||||
constexpr Mat(const Mat& other) noexcept
|
||||
@@ -89,13 +84,13 @@ namespace omath
|
||||
[[nodiscard]]
|
||||
constexpr Type& operator[](const size_t row, const size_t col)
|
||||
{
|
||||
return At(row, col);
|
||||
return at(row, col);
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Type& operator[](const size_t row, const size_t col) const
|
||||
{
|
||||
return At(row, col);
|
||||
return at(row, col);
|
||||
}
|
||||
|
||||
constexpr Mat(Mat&& other) noexcept
|
||||
@@ -104,35 +99,35 @@ namespace omath
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
static constexpr size_t RowCount() noexcept
|
||||
static constexpr size_t row_count() noexcept
|
||||
{
|
||||
return Rows;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
static constexpr size_t ColumnsCount() noexcept
|
||||
static constexpr size_t columns_count() noexcept
|
||||
{
|
||||
return Columns;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
static consteval MatSize Size() noexcept
|
||||
static consteval MatSize size() noexcept
|
||||
{
|
||||
return {Rows, Columns};
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr const Type& At(const size_t rowIndex, const size_t columnIndex) const
|
||||
constexpr const Type& at(const size_t row_index, const size_t column_index) const
|
||||
{
|
||||
#if !defined(NDEBUG) && defined(OMATH_SUPRESS_SAFETY_CHECKS)
|
||||
if (rowIndex >= Rows || columnIndex >= Columns)
|
||||
if (row_index >= Rows || column_index >= Columns)
|
||||
throw std::out_of_range("Index out of range");
|
||||
#endif
|
||||
if constexpr (StoreType == MatStoreType::ROW_MAJOR)
|
||||
return m_data[rowIndex * Columns + columnIndex];
|
||||
return m_data[row_index * Columns + column_index];
|
||||
|
||||
else if constexpr (StoreType == MatStoreType::COLUMN_MAJOR)
|
||||
return m_data[rowIndex + columnIndex * Rows];
|
||||
return m_data[row_index + column_index * Rows];
|
||||
|
||||
else
|
||||
{
|
||||
@@ -141,30 +136,29 @@ namespace omath
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Type& At(const size_t rowIndex, const size_t columnIndex)
|
||||
[[nodiscard]] constexpr Type& at(const size_t row_index, const size_t column_index)
|
||||
{
|
||||
return const_cast<Type&>(std::as_const(*this).At(rowIndex, columnIndex));
|
||||
return const_cast<Type&>(std::as_const(*this).at(row_index, column_index));
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Type Sum() const noexcept
|
||||
constexpr Type sum() const noexcept
|
||||
{
|
||||
return std::accumulate(m_data.begin(), m_data.end(), Type(0));
|
||||
return std::accumulate(m_data.begin(), m_data.end(), static_cast<Type>(0));
|
||||
}
|
||||
|
||||
constexpr void Clear() noexcept
|
||||
constexpr void clear() noexcept
|
||||
{
|
||||
Set(0);
|
||||
set(static_cast<Type>(0));
|
||||
}
|
||||
|
||||
constexpr void Set(const Type& value) noexcept
|
||||
constexpr void set(const Type& value) noexcept
|
||||
{
|
||||
std::ranges::fill(m_data, value);
|
||||
}
|
||||
|
||||
// Operator overloading for multiplication with another Mat
|
||||
template<size_t OtherColumns>
|
||||
[[nodiscard]]
|
||||
template<size_t OtherColumns> [[nodiscard]]
|
||||
constexpr Mat<Rows, OtherColumns, Type, StoreType>
|
||||
operator*(const Mat<Columns, OtherColumns, Type, StoreType>& other) const
|
||||
{
|
||||
@@ -175,20 +169,19 @@ namespace omath
|
||||
{
|
||||
Type sum = 0;
|
||||
for (size_t k = 0; k < Columns; ++k)
|
||||
sum += At(i, k) * other.At(k, j);
|
||||
result.At(i, j) = sum;
|
||||
sum += at(i, k) * other.at(k, j);
|
||||
result.at(i, j) = sum;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
constexpr Mat& operator*=(const Type& f) noexcept
|
||||
{
|
||||
std::ranges::for_each(m_data, [&f](auto& val) {val *= f;});
|
||||
std::ranges::for_each(m_data, [&f](auto& val) { val *= f; });
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<size_t OtherColumns>
|
||||
constexpr Mat<Rows, OtherColumns, Type, StoreType>
|
||||
template<size_t OtherColumns> constexpr Mat<Rows, OtherColumns, Type, StoreType>
|
||||
operator*=(const Mat<Columns, OtherColumns, Type, StoreType>& other)
|
||||
{
|
||||
return *this = *this * other;
|
||||
@@ -204,7 +197,7 @@ namespace omath
|
||||
|
||||
constexpr Mat& operator/=(const Type& value) noexcept
|
||||
{
|
||||
std::ranges::for_each(m_data, [&value](auto& val) {val /= value;});
|
||||
std::ranges::for_each(m_data, [&value](auto& val) { val /= value; });
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -233,41 +226,44 @@ namespace omath
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Mat<Columns, Rows, Type, StoreType> Transposed() const noexcept
|
||||
constexpr Mat<Columns, Rows, Type, StoreType> transposed() const noexcept
|
||||
{
|
||||
Mat<Columns, Rows, Type, StoreType> transposed;
|
||||
for (size_t i = 0; i < Rows; ++i)
|
||||
for (size_t j = 0; j < Columns; ++j)
|
||||
transposed.At(j, i) = At(i, j);
|
||||
transposed.at(j, i) = at(i, j);
|
||||
|
||||
return transposed;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Type Determinant() const
|
||||
constexpr Type determinant() const
|
||||
{
|
||||
static_assert(Rows == Columns, "Determinant is only defined for square matrices.");
|
||||
|
||||
if constexpr (Rows == 1)
|
||||
return At(0, 0);
|
||||
return at(0, 0);
|
||||
|
||||
if constexpr (Rows == 2)
|
||||
return At(0, 0) * At(1, 1) - At(0, 1) * At(1, 0);
|
||||
else
|
||||
return at(0, 0) * at(1, 1) - at(0, 1) * at(1, 0);
|
||||
|
||||
if constexpr (Rows > 2)
|
||||
{
|
||||
Type det = 0;
|
||||
for (size_t i = 0; i < Columns; ++i)
|
||||
for (size_t column = 0; column < Columns; ++column)
|
||||
{
|
||||
const Type cofactor = (i % 2 == 0 ? 1 : -1) * At(0, i) * Minor(0, i).Determinant();
|
||||
const Type cofactor = at(0, column) * alg_complement(0, column);
|
||||
det += cofactor;
|
||||
}
|
||||
return det;
|
||||
}
|
||||
std::unreachable();
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Mat<Rows - 1, Columns - 1, Type, StoreType> Minor(const size_t row, const size_t column) const
|
||||
constexpr Mat<Rows - 1, Columns - 1, Type, StoreType> strip(const size_t row, const size_t column) const
|
||||
{
|
||||
static_assert(Rows - 1 > 0 && Columns - 1 > 0);
|
||||
Mat<Rows - 1, Columns - 1, Type, StoreType> result;
|
||||
for (size_t i = 0, m = 0; i < Rows; ++i)
|
||||
{
|
||||
@@ -277,7 +273,7 @@ namespace omath
|
||||
{
|
||||
if (j == column)
|
||||
continue;
|
||||
result.At(m, n) = At(i, j);
|
||||
result.at(m, n) = at(i, j);
|
||||
++n;
|
||||
}
|
||||
++m;
|
||||
@@ -286,19 +282,32 @@ namespace omath
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr const std::array<Type, Rows * Columns>& RawArray() const
|
||||
constexpr Type minor(const size_t row, const size_t column) const
|
||||
{
|
||||
return strip(row, column).determinant();
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Type alg_complement(const size_t row, const size_t column) const
|
||||
{
|
||||
const auto minor_value = minor(row, column);
|
||||
return (row + column + 2) % 2 == 0 ? minor_value : -minor_value;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr const std::array<Type, Rows * Columns>& raw_array() const
|
||||
{
|
||||
return m_data;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr std::array<Type, Rows * Columns>& RawArray()
|
||||
constexpr std::array<Type, Rows * Columns>& raw_array()
|
||||
{
|
||||
return const_cast<std::array<Type, Rows * Columns>>(std::as_const(*this).RawArray());
|
||||
return m_data;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
std::string ToString() const noexcept
|
||||
std::string to_string() const noexcept
|
||||
{
|
||||
std::ostringstream oss;
|
||||
oss << "[[";
|
||||
@@ -310,7 +319,7 @@ namespace omath
|
||||
|
||||
for (size_t j = 0; j < Columns; ++j)
|
||||
{
|
||||
oss << std::setw(9) << std::fixed << std::setprecision(3) << At(i, j);
|
||||
oss << std::setw(9) << std::fixed << std::setprecision(3) << at(i, j);
|
||||
if (j != Columns - 1)
|
||||
oss << ", ";
|
||||
}
|
||||
@@ -333,37 +342,55 @@ namespace omath
|
||||
|
||||
// Static methods that return fixed-size matrices
|
||||
[[nodiscard]]
|
||||
constexpr static Mat<4, 4> ToScreenMat(const Type& screenWidth, const Type& screenHeight) noexcept
|
||||
constexpr static Mat<4, 4> to_screen_mat(const Type& screen_width, const Type& screen_height) noexcept
|
||||
{
|
||||
return {
|
||||
{screenWidth / 2, 0, 0, 0},
|
||||
{0, -screenHeight / 2, 0, 0},
|
||||
{screen_width / 2, 0, 0, 0},
|
||||
{0, -screen_height / 2, 0, 0},
|
||||
{0, 0, 1, 0},
|
||||
{screenWidth / 2, screenHeight / 2, 0, 1},
|
||||
{screen_width / 2, screen_height / 2, 0, 1},
|
||||
};
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr std::optional<Mat> inverted() const
|
||||
{
|
||||
const auto det = determinant();
|
||||
|
||||
if (det == 0)
|
||||
return std::nullopt;
|
||||
|
||||
const auto transposed_mat = transposed();
|
||||
Mat result;
|
||||
|
||||
for (std::size_t row = 0; row < Rows; row++)
|
||||
for (std::size_t column = 0; column < Rows; column++)
|
||||
result.at(row, column) = transposed_mat.alg_complement(row, column);
|
||||
|
||||
result /= det;
|
||||
|
||||
return {result};
|
||||
}
|
||||
|
||||
private:
|
||||
std::array<Type, Rows * Columns> m_data;
|
||||
};
|
||||
|
||||
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR>
|
||||
[[nodiscard]]
|
||||
constexpr static Mat<1, 4, Type, St> MatRowFromVector(const Vector3<Type>& vector) noexcept
|
||||
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR> [[nodiscard]]
|
||||
constexpr static Mat<1, 4, Type, St> mat_row_from_vector(const Vector3<Type>& vector) noexcept
|
||||
{
|
||||
return {{vector.x, vector.y, vector.z, 1}};
|
||||
}
|
||||
|
||||
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR>
|
||||
[[nodiscard]]
|
||||
constexpr static Mat<4, 1, Type, St> MatColumnFromVector(const Vector3<Type>& vector) noexcept
|
||||
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR> [[nodiscard]]
|
||||
constexpr static Mat<4, 1, Type, St> mat_column_from_vector(const Vector3<Type>& vector) noexcept
|
||||
{
|
||||
return {{vector.x}, {vector.y}, {vector.z}, {1}};
|
||||
}
|
||||
|
||||
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR>
|
||||
[[nodiscard]]
|
||||
constexpr Mat<4, 4, Type, St> MatTranslation(const Vector3<Type>& diff) noexcept
|
||||
constexpr Mat<4, 4, Type, St> mat_translation(const Vector3<Type>& diff) noexcept
|
||||
{
|
||||
return
|
||||
{
|
||||
@@ -376,38 +403,38 @@ namespace omath
|
||||
|
||||
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR, class Angle>
|
||||
[[nodiscard]]
|
||||
Mat<4, 4, Type, St> MatRotationAxisX(const Angle& angle) noexcept
|
||||
Mat<4, 4, Type, St> mat_rotation_axis_x(const Angle& angle) noexcept
|
||||
{
|
||||
return
|
||||
{
|
||||
{1, 0, 0, 0},
|
||||
{0, angle.Cos(), -angle.Sin(), 0},
|
||||
{0, angle.Sin(), angle.Cos(), 0},
|
||||
{0, angle.cos(), -angle.sin(), 0},
|
||||
{0, angle.sin(), angle.cos(), 0},
|
||||
{0, 0, 0, 1}
|
||||
};
|
||||
}
|
||||
|
||||
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR, class Angle>
|
||||
[[nodiscard]]
|
||||
Mat<4, 4, Type, St> MatRotationAxisY(const Angle& angle) noexcept
|
||||
Mat<4, 4, Type, St> mat_rotation_axis_y(const Angle& angle) noexcept
|
||||
{
|
||||
return
|
||||
{
|
||||
{angle.Cos(), 0, angle.Sin(), 0},
|
||||
{angle.cos(), 0, angle.sin(), 0},
|
||||
{0 , 1, 0, 0},
|
||||
{-angle.Sin(), 0, angle.Cos(), 0},
|
||||
{-angle.sin(), 0, angle.cos(), 0},
|
||||
{0 , 0, 0, 1}
|
||||
};
|
||||
}
|
||||
|
||||
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR, class Angle>
|
||||
[[nodiscard]]
|
||||
Mat<4, 4, Type, St> MatRotationAxisZ(const Angle& angle) noexcept
|
||||
Mat<4, 4, Type, St> mat_rotation_axis_z(const Angle& angle) noexcept
|
||||
{
|
||||
return
|
||||
{
|
||||
{angle.Cos(), -angle.Sin(), 0, 0},
|
||||
{angle.Sin(), angle.Cos(), 0, 0},
|
||||
{angle.cos(), -angle.sin(), 0, 0},
|
||||
{angle.sin(), angle.cos(), 0, 0},
|
||||
{ 0, 0, 1, 0},
|
||||
{ 0, 0, 0, 1},
|
||||
};
|
||||
@@ -415,8 +442,8 @@ namespace omath
|
||||
|
||||
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR>
|
||||
[[nodiscard]]
|
||||
static Mat<4, 4, Type, St> MatCameraView(const Vector3<Type>& forward, const Vector3<Type>& right,
|
||||
const Vector3<Type>& up, const Vector3<Type>& cameraOrigin) noexcept
|
||||
static Mat<4, 4, Type, St> mat_camera_view(const Vector3<Type>& forward, const Vector3<Type>& right,
|
||||
const Vector3<Type>& up, const Vector3<Type>& camera_origin) noexcept
|
||||
{
|
||||
return Mat<4, 4, Type, St>
|
||||
{
|
||||
@@ -424,32 +451,31 @@ namespace omath
|
||||
{up.x, up.y, up.z, 0},
|
||||
{forward.x, forward.y, forward.z, 0},
|
||||
{0, 0, 0, 1},
|
||||
|
||||
} * MatTranslation<Type, St>(-cameraOrigin);
|
||||
} * mat_translation<Type, St>(-camera_origin);
|
||||
}
|
||||
|
||||
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR>
|
||||
[[nodiscard]]
|
||||
Mat<4, 4, Type, St> MatPerspectiveLeftHanded(const float fieldOfView, const float aspectRatio, const float near,
|
||||
const float far) noexcept
|
||||
Mat<4, 4, Type, St> mat_perspective_left_handed(const float field_of_view, const float aspect_ratio,
|
||||
const float near, const float far) noexcept
|
||||
{
|
||||
const float fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2.f);
|
||||
const float fov_half_tan = std::tan(angles::degrees_to_radians(field_of_view) / 2.f);
|
||||
|
||||
return {{1.f / (aspectRatio * fovHalfTan), 0.f, 0.f, 0.f},
|
||||
{0.f, 1.f / fovHalfTan, 0.f, 0.f},
|
||||
return {{1.f / (aspect_ratio * fov_half_tan), 0.f, 0.f, 0.f},
|
||||
{0.f, 1.f / fov_half_tan, 0.f, 0.f},
|
||||
{0.f, 0.f, (far + near) / (far - near), -(2.f * near * far) / (far - near)},
|
||||
{0.f, 0.f, 1.f, 0.f}};
|
||||
}
|
||||
|
||||
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR>
|
||||
[[nodiscard]]
|
||||
Mat<4, 4, Type, St> MatPerspectiveRightHanded(const float fieldOfView, const float aspectRatio, const float near,
|
||||
const float far) noexcept
|
||||
Mat<4, 4, Type, St> mat_perspective_right_handed(const float field_of_view, const float aspect_ratio,
|
||||
const float near, const float far) noexcept
|
||||
{
|
||||
const float fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2.f);
|
||||
const float fov_half_tan = std::tan(angles::degrees_to_radians(field_of_view) / 2.f);
|
||||
|
||||
return {{1.f / (aspectRatio * fovHalfTan), 0.f, 0.f, 0.f},
|
||||
{0.f, 1.f / fovHalfTan, 0.f, 0.f},
|
||||
return {{1.f / (aspect_ratio * fov_half_tan), 0.f, 0.f, 0.f},
|
||||
{0.f, 1.f / fov_half_tan, 0.f, 0.f},
|
||||
{0.f, 0.f, -(far + near) / (far - near), -(2.f * near * far) / (far - near)},
|
||||
{0.f, 0.f, -1.f, 0.f}};
|
||||
}
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef OMATH_ENABLE_LEGACY
|
||||
|
||||
#include "omath/vector3.hpp"
|
||||
#include <initializer_list>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include "omath/vector3.hpp"
|
||||
|
||||
namespace omath
|
||||
{
|
||||
@@ -16,51 +19,51 @@ namespace omath
|
||||
Matrix(const std::initializer_list<std::initializer_list<float>>& rows);
|
||||
|
||||
[[nodiscard]]
|
||||
static Matrix ToScreenMatrix(float screenWidth, float screenHeight);
|
||||
static Matrix to_screen_matrix(float screen_width, float screen_height);
|
||||
|
||||
[[nodiscard]]
|
||||
static Matrix TranslationMatrix(const Vector3<float>& diff);
|
||||
static Matrix translation_matrix(const Vector3<float>& diff);
|
||||
|
||||
[[nodiscard]]
|
||||
static Matrix OrientationMatrix(const Vector3<float>& forward, const Vector3<float>& right, const Vector3<float>& up);
|
||||
static Matrix orientation_matrix(const Vector3<float>& forward, const Vector3<float>& right,
|
||||
const Vector3<float>& up);
|
||||
|
||||
[[nodiscard]]
|
||||
static Matrix ProjectionMatrix(float fieldOfView, float aspectRatio, float near, float far);
|
||||
static Matrix projection_matrix(float field_of_view, float aspect_ratio, float near, float far);
|
||||
|
||||
Matrix(const Matrix& other);
|
||||
|
||||
Matrix(size_t rows, size_t columns, const float* pRaw);
|
||||
Matrix(size_t rows, size_t columns, const float* raw_data);
|
||||
|
||||
Matrix(Matrix&& other) noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
size_t RowCount() const noexcept;
|
||||
|
||||
size_t row_count() const noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
float& operator[](size_t row, size_t column);
|
||||
|
||||
[[nodiscard]]
|
||||
size_t ColumnsCount() const noexcept;
|
||||
size_t columns_count() const noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
std::pair<size_t, size_t> Size() const noexcept;
|
||||
std::pair<size_t, size_t> size() const noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
float& At(size_t iRow, size_t iCol);
|
||||
float& at(size_t row, size_t col);
|
||||
|
||||
[[nodiscard]]
|
||||
float Sum();
|
||||
float sum();
|
||||
|
||||
void SetDataFromRaw(const float* pRawMatrix);
|
||||
void set_data_from_raw(const float* raw_matrix);
|
||||
|
||||
[[nodiscard]]
|
||||
Matrix Transpose() const;
|
||||
Matrix transpose() const;
|
||||
|
||||
void Set(float val);
|
||||
void set(float val);
|
||||
|
||||
[[nodiscard]]
|
||||
const float& At(size_t iRow, size_t iCol) const;
|
||||
const float& at(size_t row, size_t col) const;
|
||||
|
||||
Matrix operator*(const Matrix& other) const;
|
||||
|
||||
@@ -72,22 +75,22 @@ namespace omath
|
||||
|
||||
Matrix& operator/=(float f);
|
||||
|
||||
void Clear();
|
||||
void clear();
|
||||
|
||||
[[nodiscard]]
|
||||
Matrix Strip(size_t row, size_t column) const;
|
||||
Matrix strip(size_t row, size_t column) const;
|
||||
|
||||
[[nodiscard]]
|
||||
float Minor(size_t i, size_t j) const;
|
||||
float minor(size_t i, size_t j) const;
|
||||
|
||||
[[nodiscard]]
|
||||
float AlgComplement(size_t i, size_t j) const;
|
||||
float alg_complement(size_t i, size_t j) const;
|
||||
|
||||
[[nodiscard]]
|
||||
float Determinant() const;
|
||||
float determinant() const;
|
||||
|
||||
[[nodiscard]]
|
||||
const float* Raw() const;
|
||||
const float* raw() const;
|
||||
|
||||
Matrix& operator=(const Matrix& other);
|
||||
|
||||
@@ -96,7 +99,7 @@ namespace omath
|
||||
Matrix operator/(float f) const;
|
||||
|
||||
[[nodiscard]]
|
||||
std::string ToString() const;
|
||||
std::string to_string() const;
|
||||
|
||||
~Matrix();
|
||||
|
||||
@@ -106,3 +109,4 @@ namespace omath
|
||||
std::unique_ptr<float[]> m_data;
|
||||
};
|
||||
} // namespace omath
|
||||
#endif
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
//
|
||||
|
||||
#pragma once
|
||||
#include <vector>
|
||||
#include "omath/pathfinding/navigation_mesh.hpp"
|
||||
#include "omath/vector3.hpp"
|
||||
#include <vector>
|
||||
|
||||
namespace omath::pathfinding
|
||||
{
|
||||
@@ -14,17 +14,17 @@ namespace omath::pathfinding
|
||||
{
|
||||
public:
|
||||
[[nodiscard]]
|
||||
static std::vector<Vector3<float>> FindPath(const Vector3<float>& start, const Vector3<float>& end,
|
||||
const NavigationMesh& navMesh);
|
||||
static std::vector<Vector3<float>> find_path(const Vector3<float>& start, const Vector3<float>& end,
|
||||
const NavigationMesh& nav_mesh) noexcept;
|
||||
|
||||
private:
|
||||
[[nodiscard]]
|
||||
static std::vector<Vector3<float>>
|
||||
ReconstructFinalPath(const std::unordered_map<Vector3<float>, PathNode>& closedList,
|
||||
const Vector3<float>& current);
|
||||
reconstruct_final_path(const std::unordered_map<Vector3<float>, PathNode>& closed_list,
|
||||
const Vector3<float>& current) noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
static auto GetPerfectNode(const std::unordered_map<Vector3<float>, PathNode>& openList,
|
||||
const Vector3<float>& endVertex);
|
||||
static auto get_perfect_node(const std::unordered_map<Vector3<float>, PathNode>& open_list,
|
||||
const Vector3<float>& end_vertex) noexcept;
|
||||
};
|
||||
} // namespace omath::pathfinding
|
||||
|
||||
@@ -4,35 +4,34 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "omath/vector3.hpp"
|
||||
#include <expected>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "omath/vector3.hpp"
|
||||
|
||||
namespace omath::pathfinding
|
||||
{
|
||||
|
||||
enum Error
|
||||
{
|
||||
|
||||
};
|
||||
|
||||
class NavigationMesh final
|
||||
{
|
||||
public:
|
||||
[[nodiscard]]
|
||||
std::expected<Vector3<float>, std::string> GetClosestVertex(const Vector3<float>& point) const;
|
||||
std::expected<Vector3<float>, std::string> get_closest_vertex(const Vector3<float>& point) const noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
const std::vector<Vector3<float>>& GetNeighbors(const Vector3<float>& vertex) const;
|
||||
const std::vector<Vector3<float>>& get_neighbors(const Vector3<float>& vertex) const noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
bool Empty() const;
|
||||
bool empty() const;
|
||||
|
||||
[[nodiscard]] std::vector<uint8_t> Serialize() const;
|
||||
[[nodiscard]] std::vector<uint8_t> serialize() const noexcept;
|
||||
|
||||
void Deserialize(const std::vector<uint8_t>& raw);
|
||||
void deserialize(const std::vector<uint8_t>& raw) noexcept;
|
||||
|
||||
std::unordered_map<Vector3<float>, std::vector<Vector3<float>>> m_verTextMap;
|
||||
std::unordered_map<Vector3<float>, std::vector<Vector3<float>>> m_vertex_map;
|
||||
};
|
||||
} // namespace omath::pathfinding
|
||||
|
||||
@@ -6,15 +6,14 @@
|
||||
#include "omath/projectile_prediction/target.hpp"
|
||||
#include "omath/vector3.hpp"
|
||||
|
||||
|
||||
namespace omath::projectile_prediction
|
||||
{
|
||||
class ProjPredEngine
|
||||
class ProjPredEngineInterface
|
||||
{
|
||||
public:
|
||||
[[nodiscard]]
|
||||
virtual std::optional<Vector3<float>> MaybeCalculateAimPoint(const Projectile& projectile,
|
||||
const Target& target) const = 0;
|
||||
virtual ~ProjPredEngine() = default;
|
||||
virtual std::optional<Vector3<float>> maybe_calculate_aim_point(const Projectile& projectile,
|
||||
const Target& target) const = 0;
|
||||
virtual ~ProjPredEngineInterface() = default;
|
||||
};
|
||||
} // namespace omath::projectile_prediction
|
||||
|
||||
@@ -6,21 +6,23 @@
|
||||
|
||||
namespace omath::projectile_prediction
|
||||
{
|
||||
class ProjPredEngineAVX2 final : public ProjPredEngine
|
||||
class ProjPredEngineAvx2 final : public ProjPredEngineInterface
|
||||
{
|
||||
public:
|
||||
[[nodiscard]] std::optional<Vector3<float>> MaybeCalculateAimPoint(const Projectile& projectile,
|
||||
const Target& target) const override;
|
||||
[[nodiscard]] std::optional<Vector3<float>>
|
||||
maybe_calculate_aim_point(const Projectile& projectile, const Target& target) const override;
|
||||
|
||||
|
||||
ProjPredEngineAVX2(float gravityConstant, float simulationTimeStep, float maximumSimulationTime);
|
||||
~ProjPredEngineAVX2() override = default;
|
||||
ProjPredEngineAvx2(float gravity_constant, float simulation_time_step, float maximum_simulation_time);
|
||||
~ProjPredEngineAvx2() override = default;
|
||||
|
||||
private:
|
||||
[[nodiscard]] static std::optional<float> CalculatePitch(const Vector3<float>& projOrigin, const Vector3<float>& targetPos,
|
||||
float bulletGravity, float v0, float time);
|
||||
const float m_gravityConstant;
|
||||
const float m_simulationTimeStep;
|
||||
const float m_maximumSimulationTime;
|
||||
[[nodiscard]] static std::optional<float> calculate_pitch(const Vector3<float>& proj_origin,
|
||||
const Vector3<float>& target_pos,
|
||||
float bullet_gravity, float v0, float time) ;
|
||||
|
||||
// We use [[maybe_unused]] here since AVX2 is not available for ARM and ARM64 CPU
|
||||
[[maybe_unused]] const float m_gravity_constant;
|
||||
[[maybe_unused]] const float m_simulation_time_step;
|
||||
[[maybe_unused]] const float m_maximum_simulation_time;
|
||||
};
|
||||
} // namespace omath::projectile_prediction
|
||||
|
||||
@@ -4,38 +4,131 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <optional>
|
||||
#include "omath/engines/source_engine/traits/pred_engine_trait.hpp"
|
||||
#include "omath/projectile_prediction/proj_pred_engine.hpp"
|
||||
#include "omath/projectile_prediction/projectile.hpp"
|
||||
#include "omath/projectile_prediction/target.hpp"
|
||||
#include "omath/vector3.hpp"
|
||||
|
||||
#include <optional>
|
||||
|
||||
namespace omath::projectile_prediction
|
||||
{
|
||||
class ProjPredEngineLegacy final : public ProjPredEngine
|
||||
template<class T>
|
||||
concept PredEngineConcept =
|
||||
requires(const Projectile& projectile, const Target& target, const Vector3<float>& vec_a,
|
||||
const Vector3<float>& vec_b,
|
||||
Vector3<float> v3, // by-value for calc_viewpoint_from_angles
|
||||
float pitch, float yaw, float time, float gravity, std::optional<float> maybe_pitch) {
|
||||
// Presence + return types
|
||||
{ T::predict_projectile_position(projectile, pitch, yaw, time, gravity) } -> std::same_as<Vec3>;
|
||||
{ T::predict_target_position(target, time, gravity) } -> std::same_as<Vec3>;
|
||||
{ T::calc_vector_2d_distance(vec_a) } -> std::same_as<float>;
|
||||
{ T::get_vector_height_coordinate(vec_b) } -> std::same_as<float>;
|
||||
{ T::calc_viewpoint_from_angles(projectile, v3, maybe_pitch) } -> std::same_as<Vec3>;
|
||||
{ T::calc_direct_pitch_angle(vec_a, vec_b) } -> std::same_as<float>;
|
||||
{ T::calc_direct_yaw_angle(vec_a, vec_b) } -> std::same_as<float>;
|
||||
|
||||
// Enforce noexcept as in PredEngineTrait
|
||||
requires noexcept(T::predict_projectile_position(projectile, pitch, yaw, time, gravity));
|
||||
requires noexcept(T::predict_target_position(target, time, gravity));
|
||||
requires noexcept(T::calc_vector_2d_distance(vec_a));
|
||||
requires noexcept(T::get_vector_height_coordinate(vec_b));
|
||||
requires noexcept(T::calc_viewpoint_from_angles(projectile, v3, maybe_pitch));
|
||||
requires noexcept(T::calc_direct_pitch_angle(vec_a, vec_b));
|
||||
requires noexcept(T::calc_direct_yaw_angle(vec_a, vec_b));
|
||||
};
|
||||
template<class EngineTrait = source_engine::PredEngineTrait>
|
||||
requires PredEngineConcept<EngineTrait>
|
||||
class ProjPredEngineLegacy final : public ProjPredEngineInterface
|
||||
{
|
||||
public:
|
||||
explicit ProjPredEngineLegacy(float gravityConstant, float simulationTimeStep, float maximumSimulationTime,
|
||||
float distanceTolerance);
|
||||
explicit ProjPredEngineLegacy(const float gravity_constant, const float simulation_time_step,
|
||||
const float maximum_simulation_time, const float distance_tolerance)
|
||||
: m_gravity_constant(gravity_constant), m_simulation_time_step(simulation_time_step),
|
||||
m_maximum_simulation_time(maximum_simulation_time), m_distance_tolerance(distance_tolerance)
|
||||
{
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
std::optional<Vector3<float>> MaybeCalculateAimPoint(const Projectile& projectile,
|
||||
const Target& target) const override;
|
||||
std::optional<Vector3<float>> maybe_calculate_aim_point(const Projectile& projectile,
|
||||
const Target& target) const override
|
||||
{
|
||||
for (float time = 0.f; time < m_maximum_simulation_time; time += m_simulation_time_step)
|
||||
{
|
||||
const auto predicted_target_position =
|
||||
EngineTrait::predict_target_position(target, time, m_gravity_constant);
|
||||
|
||||
const auto projectile_pitch =
|
||||
maybe_calculate_projectile_launch_pitch_angle(projectile, predicted_target_position);
|
||||
|
||||
if (!projectile_pitch.has_value()) [[unlikely]]
|
||||
continue;
|
||||
|
||||
if (!is_projectile_reached_target(predicted_target_position, projectile, projectile_pitch.value(),
|
||||
time))
|
||||
continue;
|
||||
|
||||
return EngineTrait::calc_viewpoint_from_angles(projectile, predicted_target_position, projectile_pitch);
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
private:
|
||||
const float m_gravityConstant;
|
||||
const float m_simulationTimeStep;
|
||||
const float m_maximumSimulationTime;
|
||||
const float m_distanceTolerance;
|
||||
const float m_gravity_constant;
|
||||
const float m_simulation_time_step;
|
||||
const float m_maximum_simulation_time;
|
||||
const float m_distance_tolerance;
|
||||
|
||||
// Realization of this formula:
|
||||
// https://stackoverflow.com/questions/54917375/how-to-calculate-the-angle-to-shoot-a-bullet-in-order-to-hit-a-moving-target
|
||||
/*
|
||||
\[
|
||||
\theta \;=\; \arctan\!\Biggl(
|
||||
\frac{%
|
||||
v^{2}\;\pm\;\sqrt{\,v^{4}-g\!\left(gx^{2}+2yv^{2}\right)\,}
|
||||
}{%
|
||||
gx
|
||||
}\Biggr)
|
||||
\]
|
||||
*/
|
||||
[[nodiscard]]
|
||||
std::optional<float> MaybeCalculateProjectileLaunchPitchAngle(const Projectile& projectile,
|
||||
const Vector3<float>& targetPosition) const;
|
||||
std::optional<float>
|
||||
maybe_calculate_projectile_launch_pitch_angle(const Projectile& projectile,
|
||||
const Vector3<float>& target_position) const noexcept
|
||||
{
|
||||
const auto bullet_gravity = m_gravity_constant * projectile.m_gravity_scale;
|
||||
|
||||
if (bullet_gravity == 0.f)
|
||||
return EngineTrait::calc_direct_pitch_angle(projectile.m_origin, target_position);
|
||||
|
||||
const auto delta = target_position - projectile.m_origin;
|
||||
|
||||
const auto distance2d = EngineTrait::calc_vector_2d_distance(delta);
|
||||
const auto distance2d_sqr = distance2d * distance2d;
|
||||
const auto launch_speed_sqr = projectile.m_launch_speed * projectile.m_launch_speed;
|
||||
|
||||
float root = launch_speed_sqr * launch_speed_sqr
|
||||
- bullet_gravity
|
||||
* (bullet_gravity * distance2d_sqr
|
||||
+ 2.0f * EngineTrait::get_vector_height_coordinate(delta) * launch_speed_sqr);
|
||||
|
||||
if (root < 0.0f) [[unlikely]]
|
||||
return std::nullopt;
|
||||
|
||||
root = std::sqrt(root);
|
||||
const float angle = std::atan((launch_speed_sqr - root) / (bullet_gravity * distance2d));
|
||||
|
||||
return angles::radians_to_degrees(angle);
|
||||
}
|
||||
[[nodiscard]]
|
||||
bool IsProjectileReachedTarget(const Vector3<float>& targetPosition, const Projectile& projectile, float pitch,
|
||||
float time) const;
|
||||
bool is_projectile_reached_target(const Vector3<float>& target_position, const Projectile& projectile,
|
||||
const float pitch, const float time) const noexcept
|
||||
{
|
||||
const auto yaw = EngineTrait::calc_direct_yaw_angle(projectile.m_origin, target_position);
|
||||
const auto projectile_position =
|
||||
EngineTrait::predict_projectile_position(projectile, pitch, yaw, time, m_gravity_constant);
|
||||
|
||||
return projectile_position.distance_to(target_position) <= m_distance_tolerance;
|
||||
}
|
||||
};
|
||||
} // namespace omath::projectile_prediction
|
||||
|
||||
@@ -10,12 +10,8 @@ namespace omath::projectile_prediction
|
||||
class Projectile final
|
||||
{
|
||||
public:
|
||||
|
||||
[[nodiscard]]
|
||||
Vector3<float> PredictPosition(float pitch, float yaw, float time, float gravity) const;
|
||||
|
||||
Vector3<float> m_origin;
|
||||
float m_launchSpeed{};
|
||||
float m_gravityScale{};
|
||||
float m_launch_speed{};
|
||||
float m_gravity_scale{};
|
||||
};
|
||||
}
|
||||
} // namespace omath::projectile_prediction
|
||||
@@ -10,20 +10,8 @@ namespace omath::projectile_prediction
|
||||
class Target final
|
||||
{
|
||||
public:
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Vector3<float> PredictPosition(const float time, const float gravity) const
|
||||
{
|
||||
auto predicted = m_origin + m_velocity * time;
|
||||
|
||||
if (m_isAirborne)
|
||||
predicted.z -= gravity * std::pow(time, 2.f) * 0.5f;
|
||||
|
||||
return predicted;
|
||||
}
|
||||
|
||||
Vector3<float> m_origin;
|
||||
Vector3<float> m_velocity;
|
||||
bool m_isAirborne{};
|
||||
bool m_is_airborne{};
|
||||
};
|
||||
}
|
||||
} // namespace omath::projectile_prediction
|
||||
@@ -4,12 +4,12 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "omath/projection/error_codes.hpp"
|
||||
#include <expected>
|
||||
#include <omath/angle.hpp>
|
||||
#include <omath/mat.hpp>
|
||||
#include <omath/vector3.hpp>
|
||||
#include <type_traits>
|
||||
#include "omath/projection/error_codes.hpp"
|
||||
|
||||
namespace omath::projection
|
||||
{
|
||||
@@ -19,157 +19,186 @@ namespace omath::projection
|
||||
float m_width;
|
||||
float m_height;
|
||||
|
||||
[[nodiscard]] constexpr float AspectRatio() const
|
||||
[[nodiscard]] constexpr float aspect_ratio() const
|
||||
{
|
||||
return m_width / m_height;
|
||||
}
|
||||
};
|
||||
using FieldOfView = Angle<float, 0.f, 180.f, AngleFlags::Clamped>;
|
||||
|
||||
template<class Mat4x4Type, class ViewAnglesType>
|
||||
class Camera
|
||||
template<class T, class MatType, class ViewAnglesType>
|
||||
concept CameraEngineConcept =
|
||||
requires(const Vector3<float>& cam_origin, const Vector3<float>& look_at, const ViewAnglesType& angles,
|
||||
const FieldOfView& fov, const ViewPort& viewport, float znear, float zfar) {
|
||||
// Presence + return types
|
||||
{ T::calc_look_at_angle(cam_origin, look_at) } -> std::same_as<ViewAnglesType>;
|
||||
{ T::calc_view_matrix(angles, cam_origin) } -> std::same_as<MatType>;
|
||||
{ T::calc_projection_matrix(fov, viewport, znear, zfar) } -> std::same_as<MatType>;
|
||||
|
||||
// Enforce noexcept as in the trait declaration
|
||||
requires noexcept(T::calc_look_at_angle(cam_origin, look_at));
|
||||
requires noexcept(T::calc_view_matrix(angles, cam_origin));
|
||||
requires noexcept(T::calc_projection_matrix(fov, viewport, znear, zfar));
|
||||
};
|
||||
|
||||
template<class Mat4X4Type, class ViewAnglesType, class TraitClass>
|
||||
requires CameraEngineConcept<TraitClass, Mat4X4Type, ViewAnglesType>
|
||||
class Camera final
|
||||
{
|
||||
public:
|
||||
virtual ~Camera() = default;
|
||||
Camera(const Vector3<float>& position, const ViewAnglesType& viewAngles, const ViewPort& viewPort,
|
||||
const FieldOfView& fov, const float near, const float far) :
|
||||
m_viewPort(viewPort), m_fieldOfView(fov), m_farPlaneDistance(far), m_nearPlaneDistance(near),
|
||||
m_viewAngles(viewAngles), m_origin(position)
|
||||
~Camera() = default;
|
||||
Camera(const Vector3<float>& position, const ViewAnglesType& view_angles, const ViewPort& view_port,
|
||||
const FieldOfView& fov, const float near, const float far) noexcept
|
||||
: m_view_port(view_port), m_field_of_view(fov), m_far_plane_distance(far), m_near_plane_distance(near),
|
||||
m_view_angles(view_angles), m_origin(position)
|
||||
{
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual void LookAt(const Vector3<float>& target) = 0;
|
||||
|
||||
[[nodiscard]] virtual Mat4x4Type CalcViewMatrix() const = 0;
|
||||
|
||||
[[nodiscard]] virtual Mat4x4Type CalcProjectionMatrix() const = 0;
|
||||
|
||||
[[nodiscard]] Mat4x4Type CalcViewProjectionMatrix() const
|
||||
void look_at(const Vector3<float>& target)
|
||||
{
|
||||
return CalcProjectionMatrix() * CalcViewMatrix();
|
||||
m_view_angles = TraitClass::calc_look_at_angle(m_origin, target);
|
||||
}
|
||||
|
||||
[[nodiscard]] Mat4X4Type calc_view_projection_matrix() const noexcept
|
||||
{
|
||||
return TraitClass::calc_projection_matrix(m_field_of_view, m_view_port, m_near_plane_distance,
|
||||
m_far_plane_distance)
|
||||
* TraitClass::calc_view_matrix(m_view_angles, m_origin);
|
||||
}
|
||||
|
||||
public:
|
||||
[[nodiscard]] const Mat4x4Type& GetViewProjectionMatrix() const
|
||||
[[nodiscard]] const Mat4X4Type& get_view_projection_matrix() const noexcept
|
||||
{
|
||||
if (!m_viewProjectionMatrix.has_value())
|
||||
m_viewProjectionMatrix = CalcViewProjectionMatrix();
|
||||
if (!m_view_projection_matrix.has_value())
|
||||
m_view_projection_matrix = calc_view_projection_matrix();
|
||||
|
||||
return m_viewProjectionMatrix.value();
|
||||
return m_view_projection_matrix.value();
|
||||
}
|
||||
|
||||
void SetFieldOfView(const FieldOfView& fov)
|
||||
void set_field_of_view(const FieldOfView& fov) noexcept
|
||||
{
|
||||
m_fieldOfView = fov;
|
||||
m_field_of_view = fov;
|
||||
m_view_projection_matrix = std::nullopt;
|
||||
}
|
||||
|
||||
void SetNearPlane(const float near)
|
||||
void set_near_plane(const float near) noexcept
|
||||
{
|
||||
m_nearPlaneDistance = near;
|
||||
m_near_plane_distance = near;
|
||||
m_view_projection_matrix = std::nullopt;
|
||||
}
|
||||
|
||||
void SetFarPlane(const float far)
|
||||
void set_far_plane(const float far) noexcept
|
||||
{
|
||||
m_farPlaneDistance = far;
|
||||
m_far_plane_distance = far;
|
||||
m_view_projection_matrix = std::nullopt;
|
||||
}
|
||||
|
||||
void SetViewAngles(const ViewAnglesType& viewAngles)
|
||||
void set_view_angles(const ViewAnglesType& view_angles) noexcept
|
||||
{
|
||||
m_viewAngles = viewAngles;
|
||||
m_view_angles = view_angles;
|
||||
m_view_projection_matrix = std::nullopt;
|
||||
}
|
||||
|
||||
void SetOrigin(const Vector3<float>& origin)
|
||||
void set_origin(const Vector3<float>& origin) noexcept
|
||||
{
|
||||
m_origin = origin;
|
||||
m_view_projection_matrix = std::nullopt;
|
||||
}
|
||||
|
||||
void SetViewPort(const ViewPort& viewPort)
|
||||
void set_view_port(const ViewPort& view_port) noexcept
|
||||
{
|
||||
m_viewPort = viewPort;
|
||||
m_view_port = view_port;
|
||||
m_view_projection_matrix = std::nullopt;
|
||||
}
|
||||
|
||||
[[nodiscard]] const FieldOfView& GetFieldOfView() const
|
||||
[[nodiscard]] const FieldOfView& get_field_of_view() const noexcept
|
||||
{
|
||||
return m_fieldOfView;
|
||||
return m_field_of_view;
|
||||
}
|
||||
|
||||
[[nodiscard]] const float& GetNearPlane() const
|
||||
[[nodiscard]] const float& get_near_plane() const noexcept
|
||||
{
|
||||
return m_nearPlaneDistance;
|
||||
return m_near_plane_distance;
|
||||
}
|
||||
|
||||
[[nodiscard]] const float& GetFarPlane() const
|
||||
[[nodiscard]] const float& get_far_plane() const noexcept
|
||||
{
|
||||
return m_farPlaneDistance;
|
||||
return m_far_plane_distance;
|
||||
}
|
||||
|
||||
[[nodiscard]] const ViewAnglesType& GetViewAngles() const
|
||||
[[nodiscard]] const ViewAnglesType& get_view_angles() const noexcept
|
||||
{
|
||||
return m_viewAngles;
|
||||
return m_view_angles;
|
||||
}
|
||||
|
||||
[[nodiscard]] const Vector3<float>& GetOrigin() const
|
||||
[[nodiscard]] const Vector3<float>& get_origin() const noexcept
|
||||
{
|
||||
return m_origin;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::expected<Vector3<float>, Error> WorldToScreen(const Vector3<float>& worldPosition) const
|
||||
[[nodiscard]] std::expected<Vector3<float>, Error>
|
||||
world_to_screen(const Vector3<float>& world_position) const noexcept
|
||||
{
|
||||
auto normalizedCords = WorldToViewPort(worldPosition);
|
||||
auto normalized_cords = world_to_view_port(world_position);
|
||||
|
||||
if (!normalizedCords.has_value())
|
||||
return std::unexpected{normalizedCords.error()};
|
||||
if (!normalized_cords.has_value())
|
||||
return std::unexpected{normalized_cords.error()};
|
||||
|
||||
|
||||
return NdcToScreenPosition(*normalizedCords);
|
||||
return ndc_to_screen_position(*normalized_cords);
|
||||
}
|
||||
|
||||
[[nodiscard]] std::expected<Vector3<float>, Error> WorldToViewPort(const Vector3<float>& worldPosition) const
|
||||
[[nodiscard]] std::expected<Vector3<float>, Error>
|
||||
world_to_view_port(const Vector3<float>& world_position) const noexcept
|
||||
{
|
||||
auto projected = GetViewProjectionMatrix() *
|
||||
MatColumnFromVector<float, Mat4x4Type::GetStoreOrdering()>(worldPosition);
|
||||
auto projected = get_view_projection_matrix()
|
||||
* mat_column_from_vector<float, Mat4X4Type::get_store_ordering()>(world_position);
|
||||
|
||||
if (projected.At(3, 0) == 0.0f)
|
||||
if (projected.at(3, 0) == 0.0f)
|
||||
return std::unexpected(Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS);
|
||||
|
||||
projected /= projected.At(3, 0);
|
||||
projected /= projected.at(3, 0);
|
||||
|
||||
if (IsNdcOutOfBounds(projected))
|
||||
if (is_ndc_out_of_bounds(projected))
|
||||
return std::unexpected(Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS);
|
||||
|
||||
return Vector3<float>{projected.At(0, 0), projected.At(1, 0), projected.At(2, 0)};
|
||||
return Vector3<float>{projected.at(0, 0), projected.at(1, 0), projected.at(2, 0)};
|
||||
}
|
||||
|
||||
protected:
|
||||
ViewPort m_viewPort{};
|
||||
Angle<float, 0.f, 180.f, AngleFlags::Clamped> m_fieldOfView;
|
||||
ViewPort m_view_port{};
|
||||
Angle<float, 0.f, 180.f, AngleFlags::Clamped> m_field_of_view;
|
||||
|
||||
mutable std::optional<Mat4x4Type> m_viewProjectionMatrix;
|
||||
mutable std::optional<Mat4X4Type> m_view_projection_matrix;
|
||||
|
||||
float m_farPlaneDistance;
|
||||
float m_nearPlaneDistance;
|
||||
float m_far_plane_distance;
|
||||
float m_near_plane_distance;
|
||||
|
||||
|
||||
ViewAnglesType m_viewAngles;
|
||||
ViewAnglesType m_view_angles;
|
||||
Vector3<float> m_origin;
|
||||
|
||||
private:
|
||||
template<class Type>
|
||||
[[nodiscard]]
|
||||
constexpr static bool IsNdcOutOfBounds(const Type& ndc)
|
||||
[[nodiscard]] constexpr static bool is_ndc_out_of_bounds(const Type& ndc) noexcept
|
||||
{
|
||||
return std::ranges::any_of(ndc.RawArray(), [](const auto& val) { return val < -1 || val > 1; });
|
||||
return std::ranges::any_of(ndc.raw_array(), [](const auto& val) { return val < -1 || val > 1; });
|
||||
}
|
||||
|
||||
[[nodiscard]] Vector3<float> NdcToScreenPosition(const Vector3<float>& ndc) const
|
||||
[[nodiscard]] Vector3<float> ndc_to_screen_position(const Vector3<float>& ndc) const noexcept
|
||||
{
|
||||
return
|
||||
{
|
||||
(ndc.x + 1.f) / 2.f * m_viewPort.m_width,
|
||||
(1.f - ndc.y) / 2.f * m_viewPort.m_height,
|
||||
ndc.z
|
||||
};
|
||||
/*
|
||||
^
|
||||
| y
|
||||
1 |
|
||||
|
|
||||
|
|
||||
-1 ---------0--------- 1 --> x
|
||||
|
|
||||
|
|
||||
-1 |
|
||||
v
|
||||
*/
|
||||
return {(ndc.x + 1.f) / 2.f * m_view_port.m_width, (1.f - ndc.y) / 2.f * m_view_port.m_height, ndc.z};
|
||||
}
|
||||
};
|
||||
} // namespace omath::projection
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
|
||||
|
||||
namespace omath::projection
|
||||
{
|
||||
enum class Error : uint16_t
|
||||
|
||||
@@ -6,15 +6,15 @@
|
||||
|
||||
namespace omath
|
||||
{
|
||||
/*
|
||||
|\
|
||||
| \
|
||||
a | \ hypot
|
||||
| \
|
||||
-----
|
||||
b
|
||||
*/
|
||||
|
||||
/*
|
||||
v1
|
||||
|\
|
||||
| \
|
||||
a | \ hypot
|
||||
| \
|
||||
v2 ----- v3
|
||||
b
|
||||
*/
|
||||
|
||||
template<class Vector>
|
||||
class Triangle final
|
||||
@@ -31,52 +31,53 @@ namespace omath
|
||||
Vector3<float> m_vertex3;
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Vector3<float> CalculateNormal() const
|
||||
constexpr Vector3<float> calculate_normal() const
|
||||
{
|
||||
const auto b = SideBVector();
|
||||
const auto a = SideAVector();
|
||||
return b.Cross(a).Normalized();
|
||||
const auto b = side_b_vector();
|
||||
const auto a = side_a_vector();
|
||||
|
||||
return b.cross(a).normalized();
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
float SideALength() const
|
||||
float side_a_length() const
|
||||
{
|
||||
return m_vertex1.DistTo(m_vertex2);
|
||||
return m_vertex1.distance_to(m_vertex2);
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
float SideBLength() const
|
||||
float side_b_length() const
|
||||
{
|
||||
return m_vertex3.DistTo(m_vertex2);
|
||||
return m_vertex3.distance_to(m_vertex2);
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Vector3<float> SideAVector() const
|
||||
constexpr Vector3<float> side_a_vector() const
|
||||
{
|
||||
return m_vertex1 - m_vertex2;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr float Hypot() const
|
||||
constexpr float hypot() const
|
||||
{
|
||||
return m_vertex1.DistTo(m_vertex3);
|
||||
return m_vertex1.distance_to(m_vertex3);
|
||||
}
|
||||
[[nodiscard]]
|
||||
constexpr bool IsRectangular() const
|
||||
constexpr bool is_rectangular() const
|
||||
{
|
||||
const auto sideA = SideALength();
|
||||
const auto sideB = SideBLength();
|
||||
const auto hypot = Hypot();
|
||||
const auto side_a = side_a_length();
|
||||
const auto side_b = side_b_length();
|
||||
const auto hypot_value = hypot();
|
||||
|
||||
return std::abs(sideA*sideA + sideB*sideB - hypot*hypot) <= 0.0001f;
|
||||
return std::abs(side_a * side_a + side_b * side_b - hypot_value * hypot_value) <= 0.0001f;
|
||||
}
|
||||
[[nodiscard]]
|
||||
constexpr Vector3<float> SideBVector() const
|
||||
constexpr Vector3<float> side_b_vector() const
|
||||
{
|
||||
return m_vertex3 - m_vertex2;
|
||||
}
|
||||
[[nodiscard]]
|
||||
constexpr Vector3<float> MidPoint() const
|
||||
constexpr Vector3<float> mid_point() const
|
||||
{
|
||||
return (m_vertex1 + m_vertex2 + m_vertex3) / 3;
|
||||
}
|
||||
|
||||
@@ -10,12 +10,11 @@
|
||||
#include <imgui.h>
|
||||
#endif
|
||||
|
||||
|
||||
namespace omath
|
||||
{
|
||||
|
||||
template<class Type>
|
||||
requires std::is_arithmetic_v<Type>
|
||||
requires std::is_arithmetic_v<Type>
|
||||
class Vector2
|
||||
{
|
||||
public:
|
||||
@@ -25,184 +24,212 @@ namespace omath
|
||||
// Constructors
|
||||
constexpr Vector2() = default;
|
||||
|
||||
constexpr Vector2(const Type& x, const Type& y) : x(x), y(y)
|
||||
constexpr Vector2(const Type& x, const Type& y) noexcept: x(x), y(y)
|
||||
{
|
||||
}
|
||||
|
||||
// Equality operators
|
||||
[[nodiscard]]
|
||||
constexpr bool operator==(const Vector2& src) const
|
||||
constexpr bool operator==(const Vector2& other) const noexcept
|
||||
{
|
||||
return x == src.x && y == src.y;
|
||||
return x == other.x && y == other.y;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr bool operator!=(const Vector2& src) const
|
||||
constexpr bool operator!=(const Vector2& other) const noexcept
|
||||
{
|
||||
return !(*this == src);
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
// Compound assignment operators
|
||||
constexpr Vector2& operator+=(const Vector2& v)
|
||||
constexpr Vector2& operator+=(const Vector2& other) noexcept
|
||||
{
|
||||
x += v.x;
|
||||
y += v.y;
|
||||
x += other.x;
|
||||
y += other.y;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector2& operator-=(const Vector2& v)
|
||||
constexpr Vector2& operator-=(const Vector2& other) noexcept
|
||||
{
|
||||
x -= v.x;
|
||||
y -= v.y;
|
||||
x -= other.x;
|
||||
y -= other.y;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector2& operator*=(const Vector2& v)
|
||||
constexpr Vector2& operator*=(const Vector2& other) noexcept
|
||||
{
|
||||
x *= v.x;
|
||||
y *= v.y;
|
||||
x *= other.x;
|
||||
y *= other.y;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector2& operator/=(const Vector2& v)
|
||||
constexpr Vector2& operator/=(const Vector2& other) noexcept
|
||||
{
|
||||
x /= v.x;
|
||||
y /= v.y;
|
||||
x /= other.x;
|
||||
y /= other.y;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector2& operator*=(const Type& fl)
|
||||
constexpr Vector2& operator*=(const Type& value) noexcept
|
||||
{
|
||||
x *= fl;
|
||||
y *= fl;
|
||||
x *= value;
|
||||
y *= value;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector2& operator/=(const Type& fl)
|
||||
constexpr Vector2& operator/=(const Type& value) noexcept
|
||||
{
|
||||
x /= fl;
|
||||
y /= fl;
|
||||
x /= value;
|
||||
y /= value;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector2& operator+=(const Type& fl)
|
||||
constexpr Vector2& operator+=(const Type& value) noexcept
|
||||
{
|
||||
x += fl;
|
||||
y += fl;
|
||||
x += value;
|
||||
y += value;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector2& operator-=(const Type& fl)
|
||||
constexpr Vector2& operator-=(const Type& value) noexcept
|
||||
{
|
||||
x -= fl;
|
||||
y -= fl;
|
||||
x -= value;
|
||||
y -= value;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Basic vector operations
|
||||
[[nodiscard]] Type DistTo(const Vector2& vOther) const
|
||||
[[nodiscard]] Type distance_to(const Vector2& other) const noexcept
|
||||
{
|
||||
return std::sqrt(DistToSqr(vOther));
|
||||
return std::sqrt(distance_to_sqr(other));
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Type DistToSqr(const Vector2& vOther) const
|
||||
[[nodiscard]] constexpr Type distance_to_sqr(const Vector2& other) const noexcept
|
||||
{
|
||||
return (x - vOther.x) * (x - vOther.x) + (y - vOther.y) * (y - vOther.y);
|
||||
return (x - other.x) * (x - other.x) + (y - other.y) * (y - other.y);
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Type Dot(const Vector2& vOther) const
|
||||
[[nodiscard]] constexpr Type dot(const Vector2& other) const noexcept
|
||||
{
|
||||
return x * vOther.x + y * vOther.y;
|
||||
return x * other.x + y * other.y;
|
||||
}
|
||||
|
||||
#ifndef _MSC_VER
|
||||
[[nodiscard]] constexpr Type Length() const
|
||||
[[nodiscard]] constexpr Type length() const noexcept
|
||||
{
|
||||
return std::hypot(this->x, this->y);
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector2 Normalized() const
|
||||
[[nodiscard]] constexpr Vector2 normalized() const noexcept
|
||||
{
|
||||
const Type len = Length();
|
||||
const Type len = length();
|
||||
return len > 0.f ? *this / len : *this;
|
||||
}
|
||||
#else
|
||||
[[nodiscard]] Type Length() const
|
||||
[[nodiscard]] Type length() const noexcept
|
||||
{
|
||||
return std::hypot(x, y);
|
||||
}
|
||||
|
||||
[[nodiscard]] Vector2 Normalized() const
|
||||
[[nodiscard]] Vector2 normalized() const noexcept
|
||||
{
|
||||
const Type len = Length();
|
||||
return len > 0.f ? *this / len : *this;
|
||||
const Type len = length();
|
||||
return len > static_cast<Type>(0) ? *this / len : *this;
|
||||
}
|
||||
#endif
|
||||
[[nodiscard]] constexpr Type LengthSqr() const
|
||||
[[nodiscard]] constexpr Type length_sqr() const noexcept
|
||||
{
|
||||
return x * x + y * y;
|
||||
}
|
||||
|
||||
constexpr Vector2& Abs()
|
||||
constexpr Vector2& abs() noexcept
|
||||
{
|
||||
// FIXME: Replace with std::abs, if it will become constexprable
|
||||
x = x < 0 ? -x : x;
|
||||
y = y < 0 ? -y : y;
|
||||
x = x < static_cast<Type>(0) ? -x : x;
|
||||
y = y < static_cast<Type>(0) ? -y : y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector2 operator-() const
|
||||
[[nodiscard]] constexpr Vector2 operator-() const noexcept
|
||||
{
|
||||
return {-x, -y};
|
||||
}
|
||||
|
||||
// Binary arithmetic operators
|
||||
[[nodiscard]] constexpr Vector2 operator+(const Vector2& v) const
|
||||
[[nodiscard]] constexpr Vector2 operator+(const Vector2& other) const noexcept
|
||||
{
|
||||
return {x + v.x, y + v.y};
|
||||
return {x + other.x, y + other.y};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector2 operator-(const Vector2& v) const
|
||||
[[nodiscard]] constexpr Vector2 operator-(const Vector2& other) const noexcept
|
||||
{
|
||||
return {x - v.x, y - v.y};
|
||||
return {x - other.x, y - other.y};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector2 operator*(const float fl) const
|
||||
[[nodiscard]] constexpr Vector2 operator*(const Type& value) const noexcept
|
||||
{
|
||||
return {x * fl, y * fl};
|
||||
return {x * value, y * value};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector2 operator/(const float fl) const
|
||||
[[nodiscard]] constexpr Vector2 operator/(const Type& value) const noexcept
|
||||
{
|
||||
return {x / fl, y / fl};
|
||||
return {x / value, y / value};
|
||||
}
|
||||
|
||||
// Sum of elements
|
||||
[[nodiscard]] constexpr Type Sum() const
|
||||
[[nodiscard]] constexpr Type sum() const noexcept
|
||||
{
|
||||
return x + y;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr std::tuple<Type, Type> AsTuple() const
|
||||
bool operator<(const Vector2& other) const noexcept
|
||||
{
|
||||
return length() < other.length();
|
||||
}
|
||||
[[nodiscard]]
|
||||
bool operator>(const Vector2& other) const noexcept
|
||||
{
|
||||
return length() > other.length();
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
bool operator<=(const Vector2& other) const noexcept
|
||||
{
|
||||
return length() <= other.length();
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
bool operator>=(const Vector2& other) const noexcept
|
||||
{
|
||||
return length() >= other.length();
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr std::tuple<Type, Type> as_tuple() const noexcept
|
||||
{
|
||||
return std::make_tuple(x, y);
|
||||
}
|
||||
|
||||
#ifdef OMATH_IMGUI_INTEGRATION
|
||||
[[nodiscard]]
|
||||
ImVec2 ToImVec2() const
|
||||
ImVec2 to_im_vec2() const noexcept
|
||||
{
|
||||
return {static_cast<float>(this->x), static_cast<float>(this->y)};
|
||||
}
|
||||
[[nodiscard]]
|
||||
static Vector2 from_im_vec2(const ImVec2& other) noexcept
|
||||
{
|
||||
return {static_cast<Type>(other.x), static_cast<Type>(other.y)};
|
||||
}
|
||||
#endif
|
||||
};
|
||||
} // namespace omath
|
||||
|
||||
@@ -4,11 +4,11 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "omath/angle.hpp"
|
||||
#include "omath/vector2.hpp"
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
#include <functional>
|
||||
#include "omath/angle.hpp"
|
||||
#include "omath/vector2.hpp"
|
||||
|
||||
namespace omath
|
||||
{
|
||||
@@ -18,264 +18,282 @@ namespace omath
|
||||
IMPOSSIBLE_BETWEEN_ANGLE,
|
||||
};
|
||||
|
||||
template<class Type> requires std::is_arithmetic_v<Type>
|
||||
template<class Type>
|
||||
requires std::is_arithmetic_v<Type>
|
||||
class Vector3 : public Vector2<Type>
|
||||
{
|
||||
public:
|
||||
Type z = static_cast<Type>(0);
|
||||
constexpr Vector3(const Type& x, const Type& y, const Type& z) : Vector2<Type>(x, y), z(z) { }
|
||||
constexpr Vector3() : Vector2<Type>() {};
|
||||
|
||||
[[nodiscard]] constexpr bool operator==(const Vector3& src) const
|
||||
constexpr Vector3(const Type& x, const Type& y, const Type& z) noexcept: Vector2<Type>(x, y), z(z)
|
||||
{
|
||||
return Vector2<Type>::operator==(src) && (src.z == z);
|
||||
}
|
||||
constexpr Vector3() noexcept: Vector2<Type>() {};
|
||||
|
||||
[[nodiscard]] constexpr bool operator==(const Vector3& other) const noexcept
|
||||
{
|
||||
return Vector2<Type>::operator==(other) && (other.z == z);
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr bool operator!=(const Vector3& src) const
|
||||
[[nodiscard]] constexpr bool operator!=(const Vector3& other) const noexcept
|
||||
{
|
||||
return !(*this == src);
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
constexpr Vector3& operator+=(const Vector3& v)
|
||||
constexpr Vector3& operator+=(const Vector3& other) noexcept
|
||||
{
|
||||
Vector2<Type>::operator+=(v);
|
||||
z += v.z;
|
||||
Vector2<Type>::operator+=(other);
|
||||
z += other.z;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector3& operator-=(const Vector3& v)
|
||||
constexpr Vector3& operator-=(const Vector3& other) noexcept
|
||||
{
|
||||
Vector2<Type>::operator-=(v);
|
||||
z -= v.z;
|
||||
Vector2<Type>::operator-=(other);
|
||||
z -= other.z;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector3& operator*=(const float fl)
|
||||
constexpr Vector3& operator*=(const Type& value) noexcept
|
||||
{
|
||||
Vector2<Type>::operator*=(fl);
|
||||
z *= fl;
|
||||
Vector2<Type>::operator*=(value);
|
||||
z *= value;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector3& operator*=(const Vector3& v)
|
||||
constexpr Vector3& operator*=(const Vector3& other) noexcept
|
||||
{
|
||||
Vector2<Type>::operator*=(v);
|
||||
z *= v.z;
|
||||
Vector2<Type>::operator*=(other);
|
||||
z *= other.z;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector3& operator/=(const Vector3& v)
|
||||
constexpr Vector3& operator/=(const Vector3& other) noexcept
|
||||
{
|
||||
Vector2<Type>::operator/=(v);
|
||||
z /= v.z;
|
||||
Vector2<Type>::operator/=(other);
|
||||
z /= other.z;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector3& operator+=(const float fl)
|
||||
constexpr Vector3& operator+=(const Type& value) noexcept
|
||||
{
|
||||
Vector2<Type>::operator+=(fl);
|
||||
z += fl;
|
||||
Vector2<Type>::operator+=(value);
|
||||
z += value;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector3& operator/=(const float fl)
|
||||
constexpr Vector3& operator/=(const Type& value) noexcept
|
||||
{
|
||||
Vector2<Type>::operator/=(fl);
|
||||
z /= fl;
|
||||
Vector2<Type>::operator/=(value);
|
||||
z /= value;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector3& operator-=(const float fl)
|
||||
constexpr Vector3& operator-=(const Type& value) noexcept
|
||||
{
|
||||
Vector2<Type>::operator-=(fl);
|
||||
z -= fl;
|
||||
Vector2<Type>::operator-=(value);
|
||||
z -= value;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector3& Abs()
|
||||
constexpr Vector3& abs() noexcept
|
||||
{
|
||||
Vector2<Type>::Abs();
|
||||
Vector2<Type>::abs();
|
||||
z = z < 0.f ? -z : z;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Type DistToSqr(const Vector3& vOther) const
|
||||
[[nodiscard]] constexpr Type distance_to_sqr(const Vector3& other) const noexcept
|
||||
{
|
||||
return (*this - vOther).LengthSqr();
|
||||
return (*this - other).length_sqr();
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Type Dot(const Vector3& vOther) const
|
||||
[[nodiscard]] constexpr Type dot(const Vector3& other) const noexcept
|
||||
{
|
||||
return Vector2<Type>::Dot(vOther) + z * vOther.z;
|
||||
return Vector2<Type>::dot(other) + z * other.z;
|
||||
}
|
||||
|
||||
#ifndef _MSC_VER
|
||||
[[nodiscard]] constexpr Type Length() const
|
||||
[[nodiscard]] constexpr Type length() const
|
||||
{
|
||||
return std::hypot(this->x, this->y, z);
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Type Length2D() const
|
||||
[[nodiscard]] constexpr Type length_2d() const
|
||||
{
|
||||
return Vector2<Type>::Length();
|
||||
return Vector2<Type>::length();
|
||||
}
|
||||
[[nodiscard]] Type DistTo(const Vector3& vOther) const
|
||||
[[nodiscard]] Type distance_to(const Vector3& other) const
|
||||
{
|
||||
return (*this - vOther).Length();
|
||||
return (*this - other).length();
|
||||
}
|
||||
[[nodiscard]] constexpr Vector3 Normalized() const
|
||||
[[nodiscard]] constexpr Vector3 normalized() const
|
||||
{
|
||||
const Type length = this->Length();
|
||||
const Type length_value = this->length();
|
||||
|
||||
return length != 0 ? *this / length : *this;
|
||||
return length_value != 0 ? *this / length_value : *this;
|
||||
}
|
||||
#else
|
||||
[[nodiscard]] Type Length() const
|
||||
[[nodiscard]] Type length() const noexcept
|
||||
{
|
||||
return std::hypot(this->x, this->y, z);
|
||||
}
|
||||
|
||||
[[nodiscard]] Vector3 Normalized() const
|
||||
[[nodiscard]] Vector3 normalized() const noexcept
|
||||
{
|
||||
const Type length = this->Length();
|
||||
const Type len = this->length();
|
||||
|
||||
return length != 0 ? *this / length : *this;
|
||||
return len != static_cast<Type>(0) ? *this / len : *this;
|
||||
}
|
||||
|
||||
[[nodiscard]] Type Length2D() const
|
||||
[[nodiscard]] Type length_2d() const noexcept
|
||||
{
|
||||
return Vector2<Type>::Length();
|
||||
return Vector2<Type>::length();
|
||||
}
|
||||
|
||||
[[nodiscard]] Type DistTo(const Vector3& vOther) const
|
||||
[[nodiscard]] Type distance_to(const Vector3& vOther) const noexcept
|
||||
{
|
||||
return (*this - vOther).Length();
|
||||
return (*this - vOther).length();
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
[[nodiscard]] constexpr Type LengthSqr() const
|
||||
[[nodiscard]] constexpr Type length_sqr() const noexcept
|
||||
{
|
||||
return Vector2<Type>::LengthSqr() + z * z;
|
||||
return Vector2<Type>::length_sqr() + z * z;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector3 operator-() const
|
||||
[[nodiscard]] constexpr Vector3 operator-() const noexcept
|
||||
{
|
||||
return {-this->x, -this->y, -z};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector3 operator+(const Vector3& v) const
|
||||
[[nodiscard]] constexpr Vector3 operator+(const Vector3& other) const noexcept
|
||||
{
|
||||
return {this->x + v.x, this->y + v.y, z + v.z};
|
||||
return {this->x + other.x, this->y + other.y, z + other.z};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector3 operator-(const Vector3& v) const
|
||||
[[nodiscard]] constexpr Vector3 operator-(const Vector3& other) const noexcept
|
||||
{
|
||||
return {this->x - v.x, this->y - v.y, z - v.z};
|
||||
return {this->x - other.x, this->y - other.y, z - other.z};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector3 operator*(const float fl) const
|
||||
[[nodiscard]] constexpr Vector3 operator*(const Type& value) const noexcept
|
||||
{
|
||||
return {this->x * fl, this->y * fl, z * fl};
|
||||
return {this->x * value, this->y * value, z * value};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector3 operator*(const Vector3& v) const
|
||||
[[nodiscard]] constexpr Vector3 operator*(const Vector3& other) const noexcept
|
||||
{
|
||||
return {this->x * v.x, this->y * v.y, z * v.z};
|
||||
return {this->x * other.x, this->y * other.y, z * other.z};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector3 operator/(const float fl) const
|
||||
[[nodiscard]] constexpr Vector3 operator/(const Type& value) const noexcept
|
||||
{
|
||||
return {this->x / fl, this->y / fl, z / fl};
|
||||
return {this->x / value, this->y / value, z / value};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector3 operator/(const Vector3& v) const
|
||||
[[nodiscard]] constexpr Vector3 operator/(const Vector3& other) const noexcept
|
||||
{
|
||||
return {this->x / v.x, this->y / v.y, z / v.z};
|
||||
return {this->x / other.x, this->y / other.y, z / other.z};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector3 Cross(const Vector3 &v) const
|
||||
[[nodiscard]] constexpr Vector3 cross(const Vector3& other) const noexcept
|
||||
{
|
||||
return
|
||||
{
|
||||
this->y * v.z - z * v.y,
|
||||
z * v.x - this->x * v.z,
|
||||
this->x * v.y - this->y * v.x
|
||||
};
|
||||
return {this->y * other.z - z * other.y, z * other.x - this->x * other.z,
|
||||
this->x * other.y - this->y * other.x};
|
||||
}
|
||||
[[nodiscard]] constexpr Type Sum() const
|
||||
|
||||
[[nodiscard]] constexpr Type sum() const noexcept
|
||||
{
|
||||
return Sum2D() + z;
|
||||
return sum_2d() + z;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::expected<Angle<float, 0.f, 180.f, AngleFlags::Clamped>, Vector3Error>
|
||||
AngleBetween(const Vector3& other) const
|
||||
angle_between(const Vector3& other) const noexcept
|
||||
{
|
||||
const auto bottom = Length() * other.Length();
|
||||
const auto bottom = length() * other.length();
|
||||
|
||||
if (bottom == 0.f)
|
||||
if (bottom == static_cast<Type>(0))
|
||||
return std::unexpected(Vector3Error::IMPOSSIBLE_BETWEEN_ANGLE);
|
||||
|
||||
return Angle<float, 0.f, 180.f, AngleFlags::Clamped>::FromRadians(std::acos(Dot(other) / bottom));
|
||||
return Angle<float, 0.f, 180.f, AngleFlags::Clamped>::from_radians(std::acos(dot(other) / bottom));
|
||||
}
|
||||
|
||||
[[nodiscard]] bool IsPerpendicular(const Vector3& other) const
|
||||
[[nodiscard]] bool is_perpendicular(const Vector3& other) const noexcept
|
||||
{
|
||||
if (const auto angle = AngleBetween(other))
|
||||
return angle->AsDegrees() == 90.f;
|
||||
if (const auto angle = angle_between(other))
|
||||
return angle->as_degrees() == static_cast<Type>(90);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Type Sum2D() const
|
||||
[[nodiscard]] constexpr Type sum_2d() const noexcept
|
||||
{
|
||||
return Vector2<Type>::Sum();
|
||||
return Vector2<Type>::sum();
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr std::tuple<Type, Type, Type> AsTuple() const
|
||||
[[nodiscard]] constexpr std::tuple<Type, Type, Type> as_tuple() const noexcept
|
||||
{
|
||||
return std::make_tuple(this->x, this->y, z);
|
||||
}
|
||||
|
||||
[[nodiscard]] Vector3 ViewAngleTo(const Vector3 &other) const
|
||||
[[nodiscard]] Vector3 view_angle_to(const Vector3& other) const noexcept
|
||||
{
|
||||
const float distance = DistTo(other);
|
||||
const auto distance = distance_to(other);
|
||||
const auto delta = other - *this;
|
||||
|
||||
return
|
||||
{
|
||||
angles::RadiansToDegrees(std::asin(delta.z / distance)),
|
||||
angles::RadiansToDegrees(std::atan2(delta.y, delta.x)),
|
||||
0
|
||||
};
|
||||
return {angles::radians_to_degrees(std::asin(delta.z / distance)),
|
||||
angles::radians_to_degrees(std::atan2(delta.y, delta.x)), 0};
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
bool operator<(const Vector3& other) const noexcept
|
||||
{
|
||||
return length() < other.length();
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
bool operator>(const Vector3& other) const noexcept
|
||||
{
|
||||
return length() > other.length();
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
bool operator<=(const Vector3& other) const noexcept
|
||||
{
|
||||
return length() <= other.length();
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
bool operator>=(const Vector3& other) const noexcept
|
||||
{
|
||||
return length() >= other.length();
|
||||
}
|
||||
};
|
||||
}
|
||||
} // namespace omath
|
||||
// ReSharper disable once CppRedundantNamespaceDefinition
|
||||
namespace std
|
||||
{
|
||||
template<>
|
||||
struct hash<omath::Vector3<float>>
|
||||
template<> struct hash<omath::Vector3<float>>
|
||||
{
|
||||
std::size_t operator()(const omath::Vector3<float>& vec) const noexcept
|
||||
{
|
||||
std::size_t hash = 0;
|
||||
constexpr std::hash<float> hasher;
|
||||
|
||||
hash ^= hasher(vec.x) + 0x9e3779b9 + (hash<<6) + (hash>>2);
|
||||
hash ^= hasher(vec.y) + 0x9e3779b9 + (hash<<6) + (hash>>2);
|
||||
hash ^= hasher(vec.z) + 0x9e3779b9 + (hash<<6) + (hash>>2);
|
||||
hash ^= hasher(vec.x) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
|
||||
hash ^= hasher(vec.y) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
|
||||
hash ^= hasher(vec.z) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
|
||||
|
||||
return hash;
|
||||
}
|
||||
};
|
||||
}
|
||||
} // namespace std
|
||||
|
||||
@@ -6,100 +6,102 @@
|
||||
#include <algorithm>
|
||||
#include <omath/vector3.hpp>
|
||||
|
||||
|
||||
namespace omath
|
||||
{
|
||||
template <class Type>
|
||||
template<class Type>
|
||||
requires std::is_arithmetic_v<Type>
|
||||
class Vector4 : public Vector3<Type>
|
||||
{
|
||||
public:
|
||||
Type w;
|
||||
|
||||
constexpr Vector4(const Type& x, const Type& y, const Type& z, const Type& w) : Vector3<Type>(x, y, z), w(w) {}
|
||||
constexpr Vector4() : Vector3<Type>(), w(0) {};
|
||||
constexpr Vector4(const Type& x, const Type& y, const Type& z, const Type& w): Vector3<Type>(x, y, z), w(w)
|
||||
{
|
||||
}
|
||||
constexpr Vector4() noexcept: Vector3<Type>(), w(static_cast<Type>(0)) {};
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr bool operator==(const Vector4& src) const
|
||||
constexpr bool operator==(const Vector4& other) const noexcept
|
||||
{
|
||||
return Vector3<Type>::operator==(src) && w == src.w;
|
||||
return Vector3<Type>::operator==(other) && w == other.w;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr bool operator!=(const Vector4& src) const
|
||||
constexpr bool operator!=(const Vector4& other) const noexcept
|
||||
{
|
||||
return !(*this == src);
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
constexpr Vector4& operator+=(const Vector4& v)
|
||||
constexpr Vector4& operator+=(const Vector4& other) noexcept
|
||||
{
|
||||
Vector3<Type>::operator+=(v);
|
||||
w += v.w;
|
||||
Vector3<Type>::operator+=(other);
|
||||
w += other.w;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector4& operator-=(const Vector4& v)
|
||||
constexpr Vector4& operator-=(const Vector4& other) noexcept
|
||||
{
|
||||
Vector3<Type>::operator-=(v);
|
||||
w -= v.w;
|
||||
Vector3<Type>::operator-=(other);
|
||||
w -= other.w;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector4& operator*=(const float scalar)
|
||||
constexpr Vector4& operator*=(const Type& value) noexcept
|
||||
{
|
||||
Vector3<Type>::operator*=(scalar);
|
||||
w *= scalar;
|
||||
Vector3<Type>::operator*=(value);
|
||||
w *= value;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector4& operator*=(const Vector4& v)
|
||||
constexpr Vector4& operator*=(const Vector4& other) noexcept
|
||||
{
|
||||
Vector3<Type>::operator*=(v);
|
||||
w *= v.w;
|
||||
Vector3<Type>::operator*=(other);
|
||||
w *= other.w;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector4& operator/=(const float scalar)
|
||||
constexpr Vector4& operator/=(const Type& value) noexcept
|
||||
{
|
||||
Vector3<Type>::operator/=(scalar);
|
||||
w /= scalar;
|
||||
Vector3<Type>::operator/=(value);
|
||||
w /= value;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector4& operator/=(const Vector4& v)
|
||||
constexpr Vector4& operator/=(const Vector4& other) noexcept
|
||||
{
|
||||
Vector3<Type>::operator/=(v);
|
||||
w /= v.w;
|
||||
Vector3<Type>::operator/=(other);
|
||||
w /= other.w;
|
||||
return *this;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Type LengthSqr() const
|
||||
[[nodiscard]] constexpr Type length_sqr() const noexcept
|
||||
{
|
||||
return Vector3<Type>::LengthSqr() + w * w;
|
||||
return Vector3<Type>::length_sqr() + w * w;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Type Dot(const Vector4& vOther) const
|
||||
[[nodiscard]] constexpr Type dot(const Vector4& other) const noexcept
|
||||
{
|
||||
return Vector3<Type>::Dot(vOther) + w * vOther.w;
|
||||
return Vector3<Type>::dot(other) + w * other.w;
|
||||
}
|
||||
|
||||
[[nodiscard]] Vector3<Type> Length() const
|
||||
[[nodiscard]] Type length() const noexcept
|
||||
{
|
||||
return std::sqrt(LengthSqr());
|
||||
return std::sqrt(length_sqr());
|
||||
}
|
||||
|
||||
constexpr Vector4& Abs()
|
||||
constexpr Vector4& abs() noexcept
|
||||
{
|
||||
Vector3<Type>::Abs();
|
||||
Vector3<Type>::abs();
|
||||
w = w < 0.f ? -w : w;
|
||||
|
||||
return *this;
|
||||
}
|
||||
constexpr Vector4& Clamp(const Type& min, const Type& max)
|
||||
constexpr Vector4& clamp(const Type& min, const Type& max) noexcept
|
||||
{
|
||||
this->x = std::clamp(this->x, min, max);
|
||||
this->y = std::clamp(this->y, min, max);
|
||||
@@ -109,65 +111,93 @@ namespace omath
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Vector4 operator-() const
|
||||
constexpr Vector4 operator-() const noexcept
|
||||
{
|
||||
return {-this->x, -this->y, -this->z, -w};
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Vector4 operator+(const Vector4& v) const
|
||||
constexpr Vector4 operator+(const Vector4& other) const noexcept
|
||||
{
|
||||
return {this->x + v.x, this->y + v.y, this->z + v.z, w + v.w};
|
||||
return {this->x + other.x, this->y + other.y, this->z + other.z, w + other.w};
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Vector4 operator-(const Vector4& v) const
|
||||
constexpr Vector4 operator-(const Vector4& other) const noexcept
|
||||
{
|
||||
return {this->x - v.x, this->y - v.y, this->z - v.z, w - v.w};
|
||||
return {this->x - other.x, this->y - other.y, this->z - other.z, w - other.w};
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Vector4 operator*(const Type& scalar) const
|
||||
constexpr Vector4 operator*(const Type& value) const noexcept
|
||||
{
|
||||
return {this->x * scalar, this->y * scalar, this->z * scalar, w * scalar};
|
||||
return {this->x * value, this->y * value, this->z * value, w * value};
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Vector4 operator*(const Vector4& v) const
|
||||
constexpr Vector4 operator*(const Vector4& other) const noexcept
|
||||
{
|
||||
return {this->x * v.x, this->y * v.y, this->z * v.z, w * v.w};
|
||||
return {this->x * other.x, this->y * other.y, this->z * other.z, w * other.w};
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Vector4 operator/(const Type& scalar) const
|
||||
constexpr Vector4 operator/(const Type& value) const noexcept
|
||||
{
|
||||
return {this->x / scalar, this->y / scalar, this->z / scalar, w / scalar};
|
||||
return {this->x / value, this->y / value, this->z / value, w / value};
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Vector4 operator/(const Vector4& v) const
|
||||
constexpr Vector4 operator/(const Vector4& other) const noexcept
|
||||
{
|
||||
return {this->x / v.x, this->y / v.y, this->z / v.z, w / v.w};
|
||||
return {this->x / other.x, this->y / other.y, this->z / other.z, w / other.w};
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Type Sum() const
|
||||
constexpr Type sum() const noexcept
|
||||
{
|
||||
return Vector3<Type>::Sum() + w;
|
||||
return Vector3<Type>::sum() + w;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
bool operator<(const Vector4& other) const noexcept
|
||||
{
|
||||
return length() < other.length();
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
bool operator>(const Vector4& other) const noexcept
|
||||
{
|
||||
return length() > other.length();
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
bool operator<=(const Vector4& other) const noexcept
|
||||
{
|
||||
return length() <= other.length();
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
bool operator>=(const Vector4& other) const noexcept
|
||||
{
|
||||
return length() >= other.length();
|
||||
}
|
||||
|
||||
#ifdef OMATH_IMGUI_INTEGRATION
|
||||
[[nodiscard]]
|
||||
ImVec4 ToImVec4() const
|
||||
ImVec4 to_im_vec4() const noexcept
|
||||
{
|
||||
return
|
||||
{
|
||||
static_cast<float>(this->x),
|
||||
static_cast<float>(this->y),
|
||||
static_cast<float>(this->z),
|
||||
static_cast<float>(w),
|
||||
return {
|
||||
static_cast<float>(this->x),
|
||||
static_cast<float>(this->y),
|
||||
static_cast<float>(this->z),
|
||||
static_cast<float>(w),
|
||||
};
|
||||
}
|
||||
[[nodiscard]]
|
||||
static Vector4<float> from_im_vec4(const ImVec4& other) noexcept
|
||||
{
|
||||
return {static_cast<Type>(other.x), static_cast<Type>(other.y), static_cast<Type>(other.z)};
|
||||
}
|
||||
#endif
|
||||
};
|
||||
}
|
||||
};
|
||||
} // namespace omath
|
||||
|
||||
@@ -12,4 +12,4 @@ namespace omath
|
||||
YawType yaw;
|
||||
RollType roll;
|
||||
};
|
||||
}
|
||||
} // namespace omath
|
||||
|
||||
54
source/3d_primitives/box.cpp
Normal file
54
source/3d_primitives/box.cpp
Normal file
@@ -0,0 +1,54 @@
|
||||
//
|
||||
// Created by Vlad on 4/18/2025.
|
||||
//
|
||||
#include "omath/3d_primitives/box.hpp"
|
||||
|
||||
namespace omath::primitives
|
||||
{
|
||||
std::array<Triangle<Vector3<float>>, 12> create_box(const Vector3<float>& top, const Vector3<float>& bottom,
|
||||
const Vector3<float>& dir_forward,
|
||||
const Vector3<float>& dir_right, const float ratio) noexcept
|
||||
{
|
||||
const auto height = top.distance_to(bottom);
|
||||
const auto side_size = height / ratio;
|
||||
|
||||
// corner layout (0‑3 bottom, 4‑7 top)
|
||||
std::array<Vector3<float>, 8> p;
|
||||
p[0] = bottom + (dir_forward + dir_right) * side_size; // front‑right‑bottom
|
||||
p[1] = bottom + (dir_forward - dir_right) * side_size; // front‑left‑bottom
|
||||
p[2] = bottom + (-dir_forward + dir_right) * side_size; // back‑right‑bottom
|
||||
p[3] = bottom + (-dir_forward - dir_right) * side_size; // back‑left‑bottom
|
||||
p[4] = top + (dir_forward + dir_right) * side_size; // front‑right‑top
|
||||
p[5] = top + (dir_forward - dir_right) * side_size; // front‑left‑top
|
||||
p[6] = top + (-dir_forward + dir_right) * side_size; // back‑right‑top
|
||||
p[7] = top + (-dir_forward - dir_right) * side_size; // back‑left‑top
|
||||
|
||||
std::array<Triangle<Vector3<float>>, 12> poly;
|
||||
|
||||
// bottom face (+Y up ⇒ wind CW when viewed from above)
|
||||
poly[0] = {p[0], p[2], p[3]};
|
||||
poly[1] = {p[0], p[3], p[1]};
|
||||
|
||||
// top face
|
||||
poly[2] = {p[4], p[7], p[6]};
|
||||
poly[3] = {p[4], p[5], p[7]};
|
||||
|
||||
// front face
|
||||
poly[4] = {p[0], p[5], p[1]};
|
||||
poly[5] = {p[0], p[4], p[5]};
|
||||
|
||||
// right face
|
||||
poly[6] = {p[0], p[6], p[2]};
|
||||
poly[7] = {p[0], p[4], p[6]};
|
||||
|
||||
// back face
|
||||
poly[8] = {p[2], p[7], p[3]};
|
||||
poly[9] = {p[2], p[6], p[7]};
|
||||
|
||||
// left face
|
||||
poly[10] = {p[1], p[7], p[5]};
|
||||
poly[11] = {p[1], p[3], p[7]};
|
||||
|
||||
return poly;
|
||||
}
|
||||
} // namespace omath::primitives
|
||||
@@ -1,10 +0,0 @@
|
||||
target_sources(omath PRIVATE
|
||||
matrix.cpp
|
||||
color.cpp
|
||||
)
|
||||
|
||||
add_subdirectory(projectile_prediction)
|
||||
add_subdirectory(pathfinding)
|
||||
add_subdirectory(projection)
|
||||
add_subdirectory(collision)
|
||||
add_subdirectory(engines)
|
||||
@@ -1,3 +0,0 @@
|
||||
target_sources(omath PRIVATE
|
||||
line_tracer.cpp
|
||||
)
|
||||
@@ -5,58 +5,59 @@
|
||||
|
||||
namespace omath::collision
|
||||
{
|
||||
bool LineTracer::CanTraceLine(const Ray& ray, const Triangle<Vector3<float>>& triangle)
|
||||
bool LineTracer::can_trace_line(const Ray& ray, const Triangle<Vector3<float>>& triangle) noexcept
|
||||
{
|
||||
return GetRayHitPoint(ray, triangle) == ray.end;
|
||||
return get_ray_hit_point(ray, triangle) == ray.end;
|
||||
}
|
||||
Vector3<float> Ray::DirectionVector() const
|
||||
Vector3<float> Ray::direction_vector() const noexcept
|
||||
{
|
||||
return end - start;
|
||||
}
|
||||
|
||||
Vector3<float> Ray::DirectionVectorNormalized() const
|
||||
Vector3<float> Ray::direction_vector_normalized() const noexcept
|
||||
{
|
||||
return DirectionVector().Normalized();
|
||||
return direction_vector().normalized();
|
||||
}
|
||||
|
||||
Vector3<float> LineTracer::GetRayHitPoint(const Ray& ray, const Triangle<Vector3<float>>& triangle)
|
||||
Vector3<float> LineTracer::get_ray_hit_point(const Ray& ray, const Triangle<Vector3<float>>& triangle) noexcept
|
||||
{
|
||||
constexpr float kEpsilon = std::numeric_limits<float>::epsilon();
|
||||
constexpr float k_epsilon = std::numeric_limits<float>::epsilon();
|
||||
|
||||
const auto sideA = triangle.SideAVector();
|
||||
const auto sideB = triangle.SideBVector();
|
||||
const auto side_a = triangle.side_a_vector();
|
||||
const auto side_b = triangle.side_b_vector();
|
||||
|
||||
const auto ray_dir = ray.direction_vector();
|
||||
|
||||
const auto rayDir = ray.DirectionVector();
|
||||
const auto p = ray_dir.cross(side_b);
|
||||
const auto det = side_a.dot(p);
|
||||
|
||||
const auto p = rayDir.Cross(sideB);
|
||||
const auto det = sideA.Dot(p);
|
||||
|
||||
|
||||
if (std::abs(det) < kEpsilon)
|
||||
if (std::abs(det) < k_epsilon)
|
||||
return ray.end;
|
||||
|
||||
const auto invDet = 1.0f / det;
|
||||
const auto inv_det = 1.0f / det;
|
||||
const auto t = ray.start - triangle.m_vertex2;
|
||||
const auto u = t.Dot(p) * invDet;
|
||||
const auto u = t.dot(p) * inv_det;
|
||||
|
||||
|
||||
if ((u < 0 && std::abs(u) > kEpsilon) || (u > 1 && std::abs(u - 1) > kEpsilon))
|
||||
if ((u < 0 && std::abs(u) > k_epsilon) || (u > 1 && std::abs(u - 1) > k_epsilon))
|
||||
return ray.end;
|
||||
|
||||
const auto q = t.Cross(sideA);
|
||||
const auto v = rayDir.Dot(q) * invDet;
|
||||
const auto q = t.cross(side_a);
|
||||
// ReSharper disable once CppTooWideScopeInitStatement
|
||||
const auto v = ray_dir.dot(q) * inv_det;
|
||||
|
||||
|
||||
if ((v < 0 && std::abs(v) > kEpsilon) || (u + v > 1 && std::abs(u + v - 1) > kEpsilon))
|
||||
if ((v < 0 && std::abs(v) > k_epsilon) || (u + v > 1 && std::abs(u + v - 1) > k_epsilon))
|
||||
return ray.end;
|
||||
|
||||
const auto tHit = sideB.Dot(q) * invDet;
|
||||
const auto t_hit = side_b.dot(q) * inv_det;
|
||||
|
||||
|
||||
if (tHit <= kEpsilon)
|
||||
if (ray.infinite_length)
|
||||
{
|
||||
if (t_hit <= k_epsilon)
|
||||
return ray.end;
|
||||
}
|
||||
else if (t_hit <= k_epsilon || t_hit > 1.0f - k_epsilon)
|
||||
return ray.end;
|
||||
|
||||
return ray.start + rayDir * tHit;
|
||||
return ray.start + ray_dir * t_hit;
|
||||
}
|
||||
} // namespace omath::collision
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
//
|
||||
// Created by vlad on 2/4/24.
|
||||
//
|
||||
|
||||
#include "omath/color.hpp"
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
|
||||
namespace omath
|
||||
{
|
||||
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
add_subdirectory(source_engine)
|
||||
add_subdirectory(opengl_engine)
|
||||
add_subdirectory(iw_engine)
|
||||
add_subdirectory(unity_engine)
|
||||
@@ -1 +0,0 @@
|
||||
target_sources(omath PRIVATE camera.cpp formulas.cpp)
|
||||
@@ -1,34 +0,0 @@
|
||||
//
|
||||
// Created by Vlad on 3/17/2025.
|
||||
//
|
||||
#include "omath/engines/iw_engine/camera.hpp"
|
||||
#include "omath/engines/iw_engine/formulas.hpp"
|
||||
|
||||
namespace omath::iw_engine
|
||||
{
|
||||
|
||||
Camera::Camera(const Vector3<float>& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort,
|
||||
const Angle<float, 0.f, 180.f, AngleFlags::Clamped>& fov, const float near, const float far) :
|
||||
projection::Camera<Mat4x4, ViewAngles>(position, viewAngles, viewPort, fov, near, far)
|
||||
{
|
||||
}
|
||||
void Camera::LookAt([[maybe_unused]] const Vector3<float>& target)
|
||||
{
|
||||
const float distance = m_origin.DistTo(target);
|
||||
const auto delta = target - m_origin;
|
||||
|
||||
|
||||
m_viewAngles.pitch = PitchAngle::FromRadians(std::asin(delta.z / distance));
|
||||
m_viewAngles.yaw = -YawAngle::FromRadians(std::atan2(delta.y, delta.x));
|
||||
m_viewAngles.roll = RollAngle::FromRadians(0.f);
|
||||
}
|
||||
Mat4x4 Camera::CalcViewMatrix() const
|
||||
{
|
||||
return iw_engine::CalcViewMatrix(m_viewAngles, m_origin);
|
||||
}
|
||||
Mat4x4 Camera::CalcProjectionMatrix() const
|
||||
{
|
||||
return CalcPerspectiveProjectionMatrix(m_fieldOfView.AsDegrees(), m_viewPort.AspectRatio(), m_nearPlaneDistance,
|
||||
m_farPlaneDistance);
|
||||
}
|
||||
} // namespace omath::openg
|
||||
@@ -3,50 +3,49 @@
|
||||
//
|
||||
#include "omath/engines/iw_engine/formulas.hpp"
|
||||
|
||||
|
||||
namespace omath::iw_engine
|
||||
{
|
||||
|
||||
Vector3<float> ForwardVector(const ViewAngles& angles)
|
||||
Vector3<float> forward_vector(const ViewAngles& angles) noexcept
|
||||
{
|
||||
const auto vec = RotationMatrix(angles) * MatColumnFromVector(kAbsForward);
|
||||
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_forward);
|
||||
|
||||
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
|
||||
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
|
||||
}
|
||||
|
||||
Vector3<float> RightVector(const ViewAngles& angles)
|
||||
Vector3<float> right_vector(const ViewAngles& angles) noexcept
|
||||
{
|
||||
const auto vec = RotationMatrix(angles) * MatColumnFromVector(kAbsRight);
|
||||
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_right);
|
||||
|
||||
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
|
||||
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
|
||||
}
|
||||
Vector3<float> UpVector(const ViewAngles& angles)
|
||||
Vector3<float> up_vector(const ViewAngles& angles) noexcept
|
||||
{
|
||||
const auto vec = RotationMatrix(angles) * MatColumnFromVector(kAbsUp);
|
||||
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_up);
|
||||
|
||||
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
|
||||
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
|
||||
}
|
||||
Mat4x4 RotationMatrix(const ViewAngles& angles)
|
||||
Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept
|
||||
{
|
||||
return MatRotationAxisZ(angles.yaw) * MatRotationAxisY(angles.pitch) * MatRotationAxisX(angles.roll);
|
||||
return mat_rotation_axis_z(angles.yaw) * mat_rotation_axis_y(angles.pitch) * mat_rotation_axis_x(angles.roll);
|
||||
}
|
||||
|
||||
Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3<float>& cam_origin)
|
||||
Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept
|
||||
{
|
||||
return MatCameraView(ForwardVector(angles), RightVector(angles), UpVector(angles), cam_origin);
|
||||
return mat_camera_view(forward_vector(angles), right_vector(angles), up_vector(angles), cam_origin);
|
||||
}
|
||||
|
||||
Mat4x4 CalcPerspectiveProjectionMatrix(const float fieldOfView, const float aspectRatio, const float near,
|
||||
const float far)
|
||||
Mat4X4 calc_perspective_projection_matrix(const float field_of_view, const float aspect_ratio, const float near,
|
||||
const float far) noexcept
|
||||
{
|
||||
// NOTE: Need magic number to fix fov calculation, since IW engine inherit Quake proj matrix calculation
|
||||
constexpr auto kMultiplyFactor = 0.75f;
|
||||
constexpr auto k_multiply_factor = 0.75f;
|
||||
|
||||
const float fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2.f) * kMultiplyFactor;
|
||||
const float fov_half_tan = std::tan(angles::degrees_to_radians(field_of_view) / 2.f) * k_multiply_factor;
|
||||
|
||||
return {
|
||||
{1.f / (aspectRatio * fovHalfTan), 0, 0, 0},
|
||||
{0, 1.f / (fovHalfTan), 0, 0},
|
||||
{1.f / (aspect_ratio * fov_half_tan), 0, 0, 0},
|
||||
{0, 1.f / (fov_half_tan), 0, 0},
|
||||
{0, 0, (far + near) / (far - near), -(2.f * far * near) / (far - near)},
|
||||
{0, 0, 1, 0},
|
||||
};
|
||||
|
||||
27
source/engines/iw_engine/traits/camera_trait.cpp
Normal file
27
source/engines/iw_engine/traits/camera_trait.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
//
|
||||
// Created by Vlad on 8/11/2025.
|
||||
//
|
||||
#include "omath/engines/iw_engine/traits/camera_trait.hpp"
|
||||
#include "omath/engines/iw_engine/formulas.hpp"
|
||||
namespace omath::iw_engine
|
||||
{
|
||||
|
||||
ViewAngles CameraTrait::calc_look_at_angle(const Vector3<float>& cam_origin, const Vector3<float>& look_at) noexcept
|
||||
{
|
||||
const auto distance = cam_origin.distance_to(look_at);
|
||||
const auto delta = cam_origin - look_at;
|
||||
|
||||
return {PitchAngle::from_radians(-std::asin(delta.z / distance)),
|
||||
YawAngle::from_radians(std::atan2(delta.y, delta.x)), RollAngle::from_radians(0.f)};
|
||||
}
|
||||
Mat4X4 CameraTrait::calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept
|
||||
{
|
||||
return iw_engine::calc_view_matrix(angles, cam_origin);
|
||||
}
|
||||
Mat4X4 CameraTrait::calc_projection_matrix(const projection::FieldOfView& fov,
|
||||
const projection::ViewPort& view_port, const float near,
|
||||
const float far) noexcept
|
||||
{
|
||||
return calc_perspective_projection_matrix(fov.as_degrees(), view_port.aspect_ratio(), near, far);
|
||||
}
|
||||
} // namespace omath::iw_engine
|
||||
@@ -1 +0,0 @@
|
||||
target_sources(omath PRIVATE camera.cpp formulas.cpp)
|
||||
@@ -1,35 +0,0 @@
|
||||
//
|
||||
// Created by Orange on 12/23/2024.
|
||||
//
|
||||
#include "omath/engines/opengl_engine/camera.hpp"
|
||||
#include "omath/engines/opengl_engine/formulas.hpp"
|
||||
|
||||
|
||||
namespace omath::opengl_engine
|
||||
{
|
||||
|
||||
Camera::Camera(const Vector3<float>& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort,
|
||||
const Angle<float, 0.f, 180.f, AngleFlags::Clamped>& fov, const float near, const float far) :
|
||||
projection::Camera<Mat4x4, ViewAngles>(position, viewAngles, viewPort, fov, near, far)
|
||||
{
|
||||
}
|
||||
void Camera::LookAt([[maybe_unused]] const Vector3<float>& target)
|
||||
{
|
||||
const float distance = m_origin.DistTo(target);
|
||||
const auto delta = target - m_origin;
|
||||
|
||||
|
||||
m_viewAngles.pitch = PitchAngle::FromRadians(std::asin(delta.z / distance));
|
||||
m_viewAngles.yaw = -YawAngle::FromRadians(std::atan2(delta.y, delta.x));
|
||||
m_viewAngles.roll = RollAngle::FromRadians(0.f);
|
||||
}
|
||||
Mat4x4 Camera::CalcViewMatrix() const
|
||||
{
|
||||
return opengl_engine::CalcViewMatrix(m_viewAngles, m_origin);
|
||||
}
|
||||
Mat4x4 Camera::CalcProjectionMatrix() const
|
||||
{
|
||||
return CalcPerspectiveProjectionMatrix(m_fieldOfView.AsDegrees(), m_viewPort.AspectRatio(), m_nearPlaneDistance,
|
||||
m_farPlaneDistance);
|
||||
}
|
||||
} // namespace omath::opengl
|
||||
@@ -3,47 +3,48 @@
|
||||
//
|
||||
#include "omath/engines/opengl_engine/formulas.hpp"
|
||||
|
||||
|
||||
namespace omath::opengl_engine
|
||||
{
|
||||
|
||||
Vector3<float> ForwardVector(const ViewAngles& angles)
|
||||
Vector3<float> forward_vector(const ViewAngles& angles) noexcept
|
||||
{
|
||||
const auto vec = RotationMatrix(angles) * MatColumnFromVector<float, MatStoreType::COLUMN_MAJOR>(kAbsForward);
|
||||
const auto vec
|
||||
= rotation_matrix(angles) * mat_column_from_vector<float, MatStoreType::COLUMN_MAJOR>(k_abs_forward);
|
||||
|
||||
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
|
||||
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
|
||||
}
|
||||
Vector3<float> RightVector(const ViewAngles& angles)
|
||||
Vector3<float> right_vector(const ViewAngles& angles) noexcept
|
||||
{
|
||||
const auto vec = RotationMatrix(angles) * MatColumnFromVector<float, MatStoreType::COLUMN_MAJOR>(kAbsRight);
|
||||
const auto vec
|
||||
= rotation_matrix(angles) * mat_column_from_vector<float, MatStoreType::COLUMN_MAJOR>(k_abs_right);
|
||||
|
||||
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
|
||||
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
|
||||
}
|
||||
Vector3<float> UpVector(const ViewAngles& angles)
|
||||
Vector3<float> up_vector(const ViewAngles& angles) noexcept
|
||||
{
|
||||
const auto vec = RotationMatrix(angles) * MatColumnFromVector<float, MatStoreType::COLUMN_MAJOR>(kAbsUp);
|
||||
const auto vec = rotation_matrix(angles) * mat_column_from_vector<float, MatStoreType::COLUMN_MAJOR>(k_abs_up);
|
||||
|
||||
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
|
||||
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
|
||||
}
|
||||
Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3<float>& cam_origin)
|
||||
Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept
|
||||
{
|
||||
return MatCameraView<float, MatStoreType::COLUMN_MAJOR>(-ForwardVector(angles), RightVector(angles),
|
||||
UpVector(angles), cam_origin);
|
||||
return mat_camera_view<float, MatStoreType::COLUMN_MAJOR>(-forward_vector(angles), right_vector(angles),
|
||||
up_vector(angles), cam_origin);
|
||||
}
|
||||
Mat4x4 RotationMatrix(const ViewAngles& angles)
|
||||
Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept
|
||||
{
|
||||
return MatRotationAxisZ<float, MatStoreType::COLUMN_MAJOR>(angles.roll) *
|
||||
MatRotationAxisY<float, MatStoreType::COLUMN_MAJOR>(-angles.yaw) *
|
||||
MatRotationAxisX<float, MatStoreType::COLUMN_MAJOR>(-angles.pitch);
|
||||
return mat_rotation_axis_x<float, MatStoreType::COLUMN_MAJOR>(-angles.pitch)
|
||||
* mat_rotation_axis_y<float, MatStoreType::COLUMN_MAJOR>(-angles.yaw)
|
||||
* mat_rotation_axis_z<float, MatStoreType::COLUMN_MAJOR>(angles.roll);
|
||||
}
|
||||
Mat4x4 CalcPerspectiveProjectionMatrix(const float fieldOfView, const float aspectRatio, const float near,
|
||||
const float far)
|
||||
Mat4X4 calc_perspective_projection_matrix(const float field_of_view, const float aspect_ratio, const float near,
|
||||
const float far) noexcept
|
||||
{
|
||||
const float fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2.f);
|
||||
const float fov_half_tan = std::tan(angles::degrees_to_radians(field_of_view) / 2.f);
|
||||
|
||||
return {
|
||||
{1.f / (aspectRatio * fovHalfTan), 0, 0, 0},
|
||||
{0, 1.f / (fovHalfTan), 0, 0},
|
||||
{1.f / (aspect_ratio * fov_half_tan), 0, 0, 0},
|
||||
{0, 1.f / (fov_half_tan), 0, 0},
|
||||
{0, 0, -(far + near) / (far - near), -(2.f * far * near) / (far - near)},
|
||||
{0, 0, -1, 0},
|
||||
};
|
||||
|
||||
28
source/engines/opengl_engine/traits/camera_trait.cpp
Normal file
28
source/engines/opengl_engine/traits/camera_trait.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
//
|
||||
// Created by Vlad on 8/11/2025.
|
||||
//
|
||||
#include "omath/engines/opengl_engine/traits/camera_trait.hpp"
|
||||
#include "omath/engines/opengl_engine/formulas.hpp"
|
||||
|
||||
namespace omath::opengl_engine
|
||||
{
|
||||
|
||||
ViewAngles CameraTrait::calc_look_at_angle(const Vector3<float>& cam_origin, const Vector3<float>& look_at) noexcept
|
||||
{
|
||||
const auto distance = cam_origin.distance_to(look_at);
|
||||
const auto delta = cam_origin - look_at;
|
||||
|
||||
return {PitchAngle::from_radians(-std::asin(delta.y / distance)),
|
||||
YawAngle::from_radians(std::atan2(delta.z, delta.x)), RollAngle::from_radians(0.f)};
|
||||
}
|
||||
Mat4X4 CameraTrait::calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept
|
||||
{
|
||||
return opengl_engine::calc_view_matrix(angles, cam_origin);
|
||||
}
|
||||
Mat4X4 CameraTrait::calc_projection_matrix(const projection::FieldOfView& fov,
|
||||
const projection::ViewPort& view_port, const float near,
|
||||
const float far) noexcept
|
||||
{
|
||||
return calc_perspective_projection_matrix(fov.as_degrees(), view_port.aspect_ratio(), near, far);
|
||||
}
|
||||
} // namespace omath::opengl_engine
|
||||
@@ -1 +0,0 @@
|
||||
target_sources(omath PRIVATE camera.cpp formulas.cpp)
|
||||
@@ -1,37 +0,0 @@
|
||||
//
|
||||
// Created by Orange on 12/4/2024.
|
||||
//
|
||||
#include "omath/engines/source_engine/camera.hpp"
|
||||
#include "omath/engines/source_engine/formulas.hpp"
|
||||
|
||||
|
||||
namespace omath::source_engine
|
||||
{
|
||||
|
||||
Camera::Camera(const Vector3<float>& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort,
|
||||
const projection::FieldOfView& fov, const float near, const float far) :
|
||||
projection::Camera<Mat4x4, ViewAngles>(position, viewAngles, viewPort, fov, near, far)
|
||||
{
|
||||
}
|
||||
void Camera::LookAt(const Vector3<float>& target)
|
||||
{
|
||||
const float distance = m_origin.DistTo(target);
|
||||
const auto delta = target - m_origin;
|
||||
|
||||
|
||||
m_viewAngles.pitch = PitchAngle::FromRadians(std::asin(delta.z / distance));
|
||||
m_viewAngles.yaw = -YawAngle::FromRadians(std::atan2(delta.y, delta.x));
|
||||
m_viewAngles.roll = RollAngle::FromRadians(0.f);
|
||||
}
|
||||
|
||||
Mat4x4 Camera::CalcViewMatrix() const
|
||||
{
|
||||
return source_engine::CalcViewMatrix(m_viewAngles, m_origin);
|
||||
}
|
||||
|
||||
Mat4x4 Camera::CalcProjectionMatrix() const
|
||||
{
|
||||
return CalcPerspectiveProjectionMatrix(m_fieldOfView.AsDegrees(), m_viewPort.AspectRatio(), m_nearPlaneDistance,
|
||||
m_farPlaneDistance);
|
||||
}
|
||||
} // namespace omath::source
|
||||
@@ -3,50 +3,49 @@
|
||||
//
|
||||
#include <omath/engines/source_engine/formulas.hpp>
|
||||
|
||||
|
||||
namespace omath::source_engine
|
||||
{
|
||||
Vector3<float> ForwardVector(const ViewAngles& angles)
|
||||
Vector3<float> forward_vector(const ViewAngles& angles) noexcept
|
||||
{
|
||||
const auto vec = RotationMatrix(angles) * MatColumnFromVector(kAbsForward);
|
||||
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_forward);
|
||||
|
||||
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
|
||||
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
|
||||
}
|
||||
|
||||
Mat4x4 RotationMatrix(const ViewAngles& angles)
|
||||
Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept
|
||||
{
|
||||
return MatRotationAxisZ(angles.yaw) * MatRotationAxisY(angles.pitch) * MatRotationAxisX(angles.roll);
|
||||
return mat_rotation_axis_z(angles.yaw) * mat_rotation_axis_y(angles.pitch) * mat_rotation_axis_x(angles.roll);
|
||||
}
|
||||
|
||||
Vector3<float> RightVector(const ViewAngles& angles)
|
||||
Vector3<float> right_vector(const ViewAngles& angles) noexcept
|
||||
{
|
||||
const auto vec = RotationMatrix(angles) * MatColumnFromVector(kAbsRight);
|
||||
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_right);
|
||||
|
||||
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
|
||||
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
|
||||
}
|
||||
Vector3<float> UpVector(const ViewAngles& angles)
|
||||
Vector3<float> up_vector(const ViewAngles& angles) noexcept
|
||||
{
|
||||
const auto vec = RotationMatrix(angles) * MatColumnFromVector(kAbsUp);
|
||||
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_up);
|
||||
|
||||
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
|
||||
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
|
||||
}
|
||||
|
||||
Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3<float>& cam_origin)
|
||||
Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept
|
||||
{
|
||||
return MatCameraView(ForwardVector(angles), RightVector(angles), UpVector(angles), cam_origin);
|
||||
return mat_camera_view(forward_vector(angles), right_vector(angles), up_vector(angles), cam_origin);
|
||||
}
|
||||
|
||||
Mat4x4 CalcPerspectiveProjectionMatrix(const float fieldOfView, const float aspectRatio, const float near,
|
||||
const float far)
|
||||
Mat4X4 calc_perspective_projection_matrix(const float field_of_view, const float aspect_ratio, const float near,
|
||||
const float far) noexcept
|
||||
{
|
||||
// NOTE: Need magic number to fix fov calculation, since source inherit Quake proj matrix calculation
|
||||
constexpr auto kMultiplyFactor = 0.75f;
|
||||
constexpr auto k_multiply_factor = 0.75f;
|
||||
|
||||
const float fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2.f) * kMultiplyFactor;
|
||||
const float fov_half_tan = std::tan(angles::degrees_to_radians(field_of_view) / 2.f) * k_multiply_factor;
|
||||
|
||||
return {
|
||||
{1.f / (aspectRatio * fovHalfTan), 0, 0, 0},
|
||||
{0, 1.f / (fovHalfTan), 0, 0},
|
||||
{1.f / (aspect_ratio * fov_half_tan), 0, 0, 0},
|
||||
{0, 1.f / (fov_half_tan), 0, 0},
|
||||
{0, 0, (far + near) / (far - near), -(2.f * far * near) / (far - near)},
|
||||
{0, 0, 1, 0},
|
||||
};
|
||||
|
||||
27
source/engines/source_engine/traits/camera_trait.cpp
Normal file
27
source/engines/source_engine/traits/camera_trait.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
//
|
||||
// Created by Vlad on 8/11/2025.
|
||||
//
|
||||
#include "omath/engines/source_engine/traits/camera_trait.hpp"
|
||||
#include "omath/engines/source_engine/formulas.hpp"
|
||||
namespace omath::source_engine
|
||||
{
|
||||
|
||||
ViewAngles CameraTrait::calc_look_at_angle(const Vector3<float>& cam_origin, const Vector3<float>& look_at) noexcept
|
||||
{
|
||||
const auto distance = cam_origin.distance_to(look_at);
|
||||
const auto delta = cam_origin - look_at;
|
||||
|
||||
return {PitchAngle::from_radians(-std::asin(delta.z / distance)),
|
||||
YawAngle::from_radians(std::atan2(delta.y, delta.x)), RollAngle::from_radians(0.f)};
|
||||
}
|
||||
Mat4X4 CameraTrait::calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept
|
||||
{
|
||||
return source_engine::calc_view_matrix(angles, cam_origin);
|
||||
}
|
||||
Mat4X4 CameraTrait::calc_projection_matrix(const projection::FieldOfView& fov,
|
||||
const projection::ViewPort& view_port, const float near,
|
||||
const float far) noexcept
|
||||
{
|
||||
return calc_perspective_projection_matrix(fov.as_degrees(), view_port.aspect_ratio(), near, far);
|
||||
}
|
||||
} // namespace omath::source_engine
|
||||
@@ -1 +0,0 @@
|
||||
target_sources(omath PRIVATE formulas.cpp camera.cpp)
|
||||
@@ -1,28 +0,0 @@
|
||||
//
|
||||
// Created by Vlad on 3/22/2025.
|
||||
//
|
||||
#include <omath/engines/unity_engine/camera.hpp>
|
||||
#include <omath/engines/unity_engine/formulas.hpp>
|
||||
|
||||
|
||||
namespace omath::unity_engine
|
||||
{
|
||||
Camera::Camera(const Vector3<float>& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort,
|
||||
const projection::FieldOfView& fov, const float near, const float far) :
|
||||
projection::Camera<Mat4x4, ViewAngles>(position, viewAngles, viewPort, fov, near, far)
|
||||
{
|
||||
}
|
||||
void Camera::LookAt([[maybe_unused]] const Vector3<float>& target)
|
||||
{
|
||||
throw std::runtime_error("Not implemented");
|
||||
}
|
||||
Mat4x4 Camera::CalcViewMatrix() const
|
||||
{
|
||||
return unity_engine::CalcViewMatrix(m_viewAngles, m_origin);
|
||||
}
|
||||
Mat4x4 Camera::CalcProjectionMatrix() const
|
||||
{
|
||||
return CalcPerspectiveProjectionMatrix(m_fieldOfView.AsDegrees(), m_viewPort.AspectRatio(), m_nearPlaneDistance,
|
||||
m_farPlaneDistance);
|
||||
}
|
||||
} // namespace omath::unity_engine
|
||||
@@ -3,47 +3,45 @@
|
||||
//
|
||||
#include "omath/engines/unity_engine/formulas.hpp"
|
||||
|
||||
|
||||
|
||||
namespace omath::unity_engine
|
||||
{
|
||||
Vector3<float> ForwardVector(const ViewAngles& angles)
|
||||
Vector3<float> forward_vector(const ViewAngles& angles) noexcept
|
||||
{
|
||||
const auto vec = RotationMatrix(angles) * MatColumnFromVector(kAbsForward);
|
||||
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_forward);
|
||||
|
||||
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
|
||||
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
|
||||
}
|
||||
Vector3<float> RightVector(const ViewAngles& angles)
|
||||
Vector3<float> right_vector(const ViewAngles& angles) noexcept
|
||||
{
|
||||
const auto vec = RotationMatrix(angles) * MatColumnFromVector(kAbsRight);
|
||||
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_right);
|
||||
|
||||
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
|
||||
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
|
||||
}
|
||||
Vector3<float> UpVector(const ViewAngles& angles)
|
||||
Vector3<float> up_vector(const ViewAngles& angles) noexcept
|
||||
{
|
||||
const auto vec = RotationMatrix(angles) * MatColumnFromVector(kAbsUp);
|
||||
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_up);
|
||||
|
||||
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
|
||||
return {vec.at(0, 0), vec.at(1, 0), vec.at(2, 0)};
|
||||
}
|
||||
Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3<float>& cam_origin)
|
||||
Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept
|
||||
{
|
||||
return MatCameraView<float, MatStoreType::ROW_MAJOR>(ForwardVector(angles), -RightVector(angles),
|
||||
UpVector(angles), cam_origin);
|
||||
return mat_camera_view<float, MatStoreType::ROW_MAJOR>(forward_vector(angles), -right_vector(angles),
|
||||
up_vector(angles), cam_origin);
|
||||
}
|
||||
Mat4x4 RotationMatrix(const ViewAngles& angles)
|
||||
Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept
|
||||
{
|
||||
return MatRotationAxisZ(angles.roll) *
|
||||
MatRotationAxisY(angles.yaw) *
|
||||
MatRotationAxisX(angles.pitch);
|
||||
return mat_rotation_axis_x<float, MatStoreType::ROW_MAJOR>(angles.pitch)
|
||||
* mat_rotation_axis_y<float, MatStoreType::ROW_MAJOR>(angles.yaw)
|
||||
* mat_rotation_axis_z<float, MatStoreType::ROW_MAJOR>(angles.roll);
|
||||
}
|
||||
Mat4x4 CalcPerspectiveProjectionMatrix(const float fieldOfView, const float aspectRatio, const float near,
|
||||
const float far)
|
||||
Mat4X4 calc_perspective_projection_matrix(const float field_of_view, const float aspect_ratio, const float near,
|
||||
const float far) noexcept
|
||||
{
|
||||
const float fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2.f);
|
||||
const float fov_half_tan = std::tan(angles::degrees_to_radians(field_of_view) / 2.f);
|
||||
|
||||
return {
|
||||
{1.f / (aspectRatio * fovHalfTan), 0, 0, 0},
|
||||
{0, 1.f / (fovHalfTan), 0, 0},
|
||||
{1.f / (aspect_ratio * fov_half_tan), 0, 0, 0},
|
||||
{0, 1.f / (fov_half_tan), 0, 0},
|
||||
{0, 0, (far + near) / (far - near), -(2.f * far * near) / (far - near)},
|
||||
{0, 0, -1.f, 0},
|
||||
};
|
||||
|
||||
27
source/engines/unity_engine/traits/camera_trait.cpp
Normal file
27
source/engines/unity_engine/traits/camera_trait.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
//
|
||||
// Created by Vlad on 8/11/2025.
|
||||
//
|
||||
#include "omath/engines/unity_engine/traits/camera_trait.hpp"
|
||||
|
||||
namespace omath::unity_engine
|
||||
{
|
||||
|
||||
ViewAngles CameraTrait::calc_look_at_angle(const Vector3<float>& cam_origin, const Vector3<float>& look_at) noexcept
|
||||
{
|
||||
const auto distance = cam_origin.distance_to(look_at);
|
||||
const auto delta = cam_origin - look_at;
|
||||
|
||||
return {PitchAngle::from_radians(-std::asin(delta.y / distance)),
|
||||
YawAngle::from_radians(std::atan2(delta.z, delta.x)), RollAngle::from_radians(0.f)};
|
||||
}
|
||||
Mat4X4 CameraTrait::calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept
|
||||
{
|
||||
return unity_engine::calc_view_matrix(angles, cam_origin);
|
||||
}
|
||||
Mat4X4 CameraTrait::calc_projection_matrix(const projection::FieldOfView& fov,
|
||||
const projection::ViewPort& view_port, const float near,
|
||||
const float far) noexcept
|
||||
{
|
||||
return calc_perspective_projection_matrix(fov.as_degrees(), view_port.aspect_ratio(), near, far);
|
||||
}
|
||||
} // namespace omath::unity_engine
|
||||
@@ -1,14 +1,13 @@
|
||||
#ifdef OMATH_ENABLE_LEGACY
|
||||
|
||||
#include "omath/matrix.hpp"
|
||||
#include "omath/angles.hpp"
|
||||
#include "omath/vector3.hpp"
|
||||
|
||||
|
||||
#include <complex>
|
||||
#include <format>
|
||||
#include <stdexcept>
|
||||
#include <utility>
|
||||
|
||||
|
||||
namespace omath
|
||||
{
|
||||
Matrix::Matrix(const size_t rows, const size_t columns)
|
||||
@@ -21,7 +20,7 @@ namespace omath
|
||||
|
||||
m_data = std::make_unique<float[]>(m_rows * m_columns);
|
||||
|
||||
Set(0.f);
|
||||
set(0.f);
|
||||
}
|
||||
|
||||
Matrix::Matrix(const std::initializer_list<std::initializer_list<float>>& rows)
|
||||
@@ -29,7 +28,6 @@ namespace omath
|
||||
m_rows = rows.size();
|
||||
m_columns = rows.begin()->size();
|
||||
|
||||
|
||||
for (const auto& row: rows)
|
||||
if (row.size() != m_columns)
|
||||
throw std::invalid_argument("All rows must have the same number of columns.");
|
||||
@@ -41,7 +39,7 @@ namespace omath
|
||||
{
|
||||
size_t j = 0;
|
||||
for (const auto& value: row)
|
||||
At(i, j++) = value;
|
||||
at(i, j++) = value;
|
||||
++i;
|
||||
}
|
||||
}
|
||||
@@ -55,29 +53,28 @@ namespace omath
|
||||
|
||||
for (size_t i = 0; i < m_rows; ++i)
|
||||
for (size_t j = 0; j < m_columns; ++j)
|
||||
At(i, j) = other.At(i, j);
|
||||
at(i, j) = other.at(i, j);
|
||||
}
|
||||
|
||||
Matrix::Matrix(const size_t rows, const size_t columns, const float* pRaw)
|
||||
Matrix::Matrix(const size_t rows, const size_t columns, const float* raw_data)
|
||||
{
|
||||
m_rows = rows;
|
||||
m_columns = columns;
|
||||
|
||||
|
||||
m_data = std::make_unique<float[]>(m_rows * m_columns);
|
||||
|
||||
for (size_t i = 0; i < rows * columns; ++i)
|
||||
At(i / rows, i % columns) = pRaw[i];
|
||||
at(i / rows, i % columns) = raw_data[i];
|
||||
}
|
||||
|
||||
size_t Matrix::RowCount() const noexcept
|
||||
size_t Matrix::row_count() const noexcept
|
||||
{
|
||||
return m_rows;
|
||||
}
|
||||
|
||||
float& Matrix::operator[](const size_t row, const size_t column)
|
||||
{
|
||||
return At(row, column);
|
||||
return at(row, column);
|
||||
}
|
||||
|
||||
Matrix::Matrix(Matrix&& other) noexcept
|
||||
@@ -92,35 +89,35 @@ namespace omath
|
||||
other.m_data = nullptr;
|
||||
}
|
||||
|
||||
size_t Matrix::ColumnsCount() const noexcept
|
||||
size_t Matrix::columns_count() const noexcept
|
||||
{
|
||||
return m_columns;
|
||||
}
|
||||
|
||||
std::pair<size_t, size_t> Matrix::Size() const noexcept
|
||||
std::pair<size_t, size_t> Matrix::size() const noexcept
|
||||
{
|
||||
return {RowCount(), ColumnsCount()};
|
||||
return {row_count(), columns_count()};
|
||||
}
|
||||
|
||||
float& Matrix::At(const size_t iRow, const size_t iCol)
|
||||
float& Matrix::at(const size_t row, const size_t col)
|
||||
{
|
||||
return const_cast<float&>(std::as_const(*this).At(iRow, iCol));
|
||||
return const_cast<float&>(std::as_const(*this).at(row, col));
|
||||
}
|
||||
|
||||
float Matrix::Sum()
|
||||
float Matrix::sum()
|
||||
{
|
||||
float sum = 0;
|
||||
|
||||
for (size_t i = 0; i < RowCount(); i++)
|
||||
for (size_t j = 0; j < ColumnsCount(); j++)
|
||||
sum += At(i, j);
|
||||
for (size_t i = 0; i < row_count(); i++)
|
||||
for (size_t j = 0; j < columns_count(); j++)
|
||||
sum += at(i, j);
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
const float& Matrix::At(const size_t iRow, const size_t iCol) const
|
||||
const float& Matrix::at(const size_t row, const size_t col) const
|
||||
{
|
||||
return m_data[iRow * m_columns + iCol];
|
||||
return m_data[row * m_columns + col];
|
||||
}
|
||||
|
||||
Matrix Matrix::operator*(const Matrix& other) const
|
||||
@@ -128,15 +125,14 @@ namespace omath
|
||||
if (m_columns != other.m_rows)
|
||||
throw std::runtime_error("n != m");
|
||||
|
||||
auto outMat = Matrix(m_rows, other.m_columns);
|
||||
auto out_mat = Matrix(m_rows, other.m_columns);
|
||||
|
||||
for (size_t d = 0; d < m_rows; ++d)
|
||||
for (size_t i = 0; i < other.m_columns; ++i)
|
||||
for (size_t j = 0; j < other.m_rows; ++j)
|
||||
outMat.At(d, i) += At(d, j) * other.At(j, i);
|
||||
out_mat.at(d, i) += at(d, j) * other.at(j, i);
|
||||
|
||||
|
||||
return outMat;
|
||||
return out_mat;
|
||||
}
|
||||
|
||||
Matrix& Matrix::operator*=(const Matrix& other)
|
||||
@@ -150,22 +146,22 @@ namespace omath
|
||||
auto out = *this;
|
||||
for (size_t i = 0; i < m_rows; ++i)
|
||||
for (size_t j = 0; j < m_columns; ++j)
|
||||
out.At(i, j) *= f;
|
||||
out.at(i, j) *= f;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
Matrix& Matrix::operator*=(const float f)
|
||||
{
|
||||
for (size_t i = 0; i < RowCount(); i++)
|
||||
for (size_t j = 0; j < ColumnsCount(); j++)
|
||||
At(i, j) *= f;
|
||||
for (size_t i = 0; i < row_count(); i++)
|
||||
for (size_t j = 0; j < columns_count(); j++)
|
||||
at(i, j) *= f;
|
||||
return *this;
|
||||
}
|
||||
|
||||
void Matrix::Clear()
|
||||
void Matrix::clear()
|
||||
{
|
||||
Set(0.f);
|
||||
set(0.f);
|
||||
}
|
||||
|
||||
Matrix& Matrix::operator=(const Matrix& other)
|
||||
@@ -175,7 +171,7 @@ namespace omath
|
||||
|
||||
for (size_t i = 0; i < m_rows; ++i)
|
||||
for (size_t j = 0; j < m_columns; ++j)
|
||||
At(i, j) = other.At(i, j);
|
||||
at(i, j) = other.at(i, j);
|
||||
|
||||
return *this;
|
||||
}
|
||||
@@ -199,7 +195,7 @@ namespace omath
|
||||
{
|
||||
for (size_t i = 0; i < m_rows; ++i)
|
||||
for (size_t j = 0; j < m_columns; ++j)
|
||||
At(i, j) /= f;
|
||||
at(i, j) /= f;
|
||||
|
||||
return *this;
|
||||
}
|
||||
@@ -209,12 +205,12 @@ namespace omath
|
||||
auto out = *this;
|
||||
for (size_t i = 0; i < m_rows; ++i)
|
||||
for (size_t j = 0; j < m_columns; ++j)
|
||||
out.At(i, j) /= f;
|
||||
out.at(i, j) /= f;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string Matrix::ToString() const
|
||||
std::string Matrix::to_string() const
|
||||
{
|
||||
std::string str;
|
||||
|
||||
@@ -222,7 +218,7 @@ namespace omath
|
||||
{
|
||||
for (size_t j = 0; j < m_columns; ++j)
|
||||
{
|
||||
str += std::format("{:.1f}", At(i, j));
|
||||
str += std::format("{:.1f}", at(i, j));
|
||||
|
||||
if (j == m_columns - 1)
|
||||
str += '\n';
|
||||
@@ -233,89 +229,89 @@ namespace omath
|
||||
return str;
|
||||
}
|
||||
|
||||
float Matrix::Determinant() const
|
||||
float Matrix::determinant() const // NOLINT(*-no-recursion)
|
||||
{
|
||||
if (m_rows + m_columns == 2)
|
||||
return At(0, 0);
|
||||
return at(0, 0);
|
||||
|
||||
if (m_rows == 2 and m_columns == 2)
|
||||
return At(0, 0) * At(1, 1) - At(0, 1) * At(1, 0);
|
||||
return at(0, 0) * at(1, 1) - at(0, 1) * at(1, 0);
|
||||
|
||||
float fDet = 0;
|
||||
float det = 0;
|
||||
for (size_t i = 0; i < m_columns; i++)
|
||||
fDet += AlgComplement(0, i) * At(0, i);
|
||||
det += alg_complement(0, i) * at(0, i);
|
||||
|
||||
return fDet;
|
||||
return det;
|
||||
}
|
||||
|
||||
float Matrix::AlgComplement(const size_t i, const size_t j) const
|
||||
float Matrix::alg_complement(const size_t i, const size_t j) const // NOLINT(*-no-recursion)
|
||||
{
|
||||
const auto tmp = Minor(i, j);
|
||||
const auto tmp = minor(i, j);
|
||||
return ((i + j + 2) % 2 == 0) ? tmp : -tmp;
|
||||
}
|
||||
|
||||
Matrix Matrix::Transpose() const
|
||||
Matrix Matrix::transpose() const
|
||||
{
|
||||
Matrix transposed = {m_columns, m_rows};
|
||||
|
||||
for (size_t i = 0; i < m_rows; ++i)
|
||||
for (size_t j = 0; j < m_columns; ++j)
|
||||
transposed.At(j, i) = At(i, j);
|
||||
transposed.at(j, i) = at(i, j);
|
||||
|
||||
return transposed;
|
||||
}
|
||||
|
||||
Matrix::~Matrix() = default;
|
||||
|
||||
void Matrix::Set(const float val)
|
||||
void Matrix::set(const float val)
|
||||
{
|
||||
for (size_t i = 0; i < m_rows; ++i)
|
||||
for (size_t j = 0; j < m_columns; ++j)
|
||||
At(i, j) = val;
|
||||
at(i, j) = val;
|
||||
}
|
||||
|
||||
Matrix Matrix::Strip(const size_t row, const size_t column) const
|
||||
Matrix Matrix::strip(const size_t row, const size_t column) const
|
||||
{
|
||||
Matrix stripped = {m_rows - 1, m_columns - 1};
|
||||
size_t iStripRowIndex = 0;
|
||||
size_t strip_row_index = 0;
|
||||
|
||||
for (size_t i = 0; i < m_rows; i++)
|
||||
{
|
||||
if (i == row)
|
||||
continue;
|
||||
|
||||
size_t iStripColumnIndex = 0;
|
||||
size_t strip_column_index = 0;
|
||||
for (size_t j = 0; j < m_columns; ++j)
|
||||
{
|
||||
if (j == column)
|
||||
continue;
|
||||
|
||||
stripped.At(iStripRowIndex, iStripColumnIndex) = At(i, j);
|
||||
iStripColumnIndex++;
|
||||
stripped.at(strip_row_index, strip_column_index) = at(i, j);
|
||||
strip_column_index++;
|
||||
}
|
||||
|
||||
iStripRowIndex++;
|
||||
strip_row_index++;
|
||||
}
|
||||
|
||||
return stripped;
|
||||
}
|
||||
|
||||
float Matrix::Minor(const size_t i, const size_t j) const
|
||||
float Matrix::minor(const size_t i, const size_t j) const // NOLINT(*-no-recursion)
|
||||
{
|
||||
return Strip(i, j).Determinant();
|
||||
return strip(i, j).determinant();
|
||||
}
|
||||
|
||||
Matrix Matrix::ToScreenMatrix(const float screenWidth, const float screenHeight)
|
||||
Matrix Matrix::to_screen_matrix(const float screen_width, const float screen_height)
|
||||
{
|
||||
return {
|
||||
{screenWidth / 2.f, 0.f, 0.f, 0.f},
|
||||
{0.f, -screenHeight / 2.f, 0.f, 0.f},
|
||||
{screen_width / 2.f, 0.f, 0.f, 0.f},
|
||||
{0.f, -screen_height / 2.f, 0.f, 0.f},
|
||||
{0.f, 0.f, 1.f, 0.f},
|
||||
{screenWidth / 2.f, screenHeight / 2.f, 0.f, 1.f},
|
||||
{screen_width / 2.f, screen_height / 2.f, 0.f, 1.f},
|
||||
};
|
||||
}
|
||||
|
||||
Matrix Matrix::TranslationMatrix(const Vector3<float>& diff)
|
||||
Matrix Matrix::translation_matrix(const Vector3<float>& diff)
|
||||
{
|
||||
return {
|
||||
{1.f, 0.f, 0.f, 0.f},
|
||||
@@ -325,7 +321,8 @@ namespace omath
|
||||
};
|
||||
}
|
||||
|
||||
Matrix Matrix::OrientationMatrix(const Vector3<float>& forward, const Vector3<float>& right, const Vector3<float>& up)
|
||||
Matrix Matrix::orientation_matrix(const Vector3<float>& forward, const Vector3<float>& right,
|
||||
const Vector3<float>& up)
|
||||
{
|
||||
return {
|
||||
{right.x, up.x, forward.x, 0.f},
|
||||
@@ -335,25 +332,26 @@ namespace omath
|
||||
};
|
||||
}
|
||||
|
||||
Matrix Matrix::ProjectionMatrix(const float fieldOfView, const float aspectRatio, const float near, const float far)
|
||||
Matrix Matrix::projection_matrix(const float field_of_view, const float aspect_ratio, const float near,
|
||||
const float far)
|
||||
{
|
||||
const float fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2.f);
|
||||
const float fov_half_tan = std::tan(angles::degrees_to_radians(field_of_view) / 2.f);
|
||||
|
||||
return {{1.f / (aspectRatio * fovHalfTan), 0.f, 0.f, 0.f},
|
||||
{0.f, 1.f / fovHalfTan, 0.f, 0.f},
|
||||
return {{1.f / (aspect_ratio * fov_half_tan), 0.f, 0.f, 0.f},
|
||||
{0.f, 1.f / fov_half_tan, 0.f, 0.f},
|
||||
{0.f, 0.f, (far + near) / (far - near), 2.f * near * far / (far - near)},
|
||||
{0.f, 0.f, -1.f, 0.f}};
|
||||
}
|
||||
|
||||
const float* Matrix::Raw() const
|
||||
const float* Matrix::raw() const
|
||||
{
|
||||
return m_data.get();
|
||||
}
|
||||
|
||||
void Matrix::SetDataFromRaw(const float* pRawMatrix)
|
||||
void Matrix::set_data_from_raw(const float* raw_matrix)
|
||||
{
|
||||
for (size_t i = 0; i < m_columns * m_rows; ++i)
|
||||
At(i / m_rows, i % m_columns) = pRawMatrix[i];
|
||||
at(i / m_rows, i % m_columns) = raw_matrix[i];
|
||||
}
|
||||
|
||||
Matrix::Matrix()
|
||||
@@ -363,3 +361,4 @@ namespace omath
|
||||
m_data = nullptr;
|
||||
}
|
||||
} // namespace omath
|
||||
#endif
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
target_sources(omath PRIVATE navigation_mesh.cpp a_star.cpp)
|
||||
@@ -2,96 +2,95 @@
|
||||
// Created by Vlad on 28.07.2024.
|
||||
//
|
||||
#include "omath/pathfinding/a_star.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <optional>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
|
||||
|
||||
namespace omath::pathfinding
|
||||
{
|
||||
struct PathNode final
|
||||
{
|
||||
std::optional<Vector3<float>> cameFrom;
|
||||
float gCost = 0.f;
|
||||
std::optional<Vector3<float>> came_from;
|
||||
float g_cost = 0.f;
|
||||
};
|
||||
|
||||
|
||||
std::vector<Vector3<float>> Astar::ReconstructFinalPath(const std::unordered_map<Vector3<float>, PathNode>& closedList,
|
||||
const Vector3<float>& current)
|
||||
std::vector<Vector3<float>>
|
||||
Astar::reconstruct_final_path(const std::unordered_map<Vector3<float>, PathNode>& closed_list,
|
||||
const Vector3<float>& current) noexcept
|
||||
{
|
||||
std::vector<Vector3<float>> path;
|
||||
std::optional currentOpt = current;
|
||||
std::optional current_opt = current;
|
||||
|
||||
while (currentOpt)
|
||||
while (current_opt)
|
||||
{
|
||||
path.push_back(*currentOpt);
|
||||
path.push_back(*current_opt);
|
||||
|
||||
auto it = closedList.find(*currentOpt);
|
||||
auto it = closed_list.find(*current_opt);
|
||||
|
||||
if (it == closedList.end())
|
||||
if (it == closed_list.end())
|
||||
break;
|
||||
|
||||
currentOpt = it->second.cameFrom;
|
||||
current_opt = it->second.came_from;
|
||||
}
|
||||
|
||||
std::ranges::reverse(path);
|
||||
return path;
|
||||
}
|
||||
auto Astar::GetPerfectNode(const std::unordered_map<Vector3<float>, PathNode>& openList, const Vector3<float>& endVertex)
|
||||
auto Astar::get_perfect_node(const std::unordered_map<Vector3<float>, PathNode>& open_list,
|
||||
const Vector3<float>& end_vertex) noexcept
|
||||
{
|
||||
return std::ranges::min_element(openList,
|
||||
[&endVertex](const auto& a, const auto& b)
|
||||
return std::ranges::min_element(open_list,
|
||||
[&end_vertex](const auto& a, const auto& b)
|
||||
{
|
||||
const float fA = a.second.gCost + a.first.DistTo(endVertex);
|
||||
const float fB = b.second.gCost + b.first.DistTo(endVertex);
|
||||
return fA < fB;
|
||||
const float fa = a.second.g_cost + a.first.distance_to(end_vertex);
|
||||
const float fb = b.second.g_cost + b.first.distance_to(end_vertex);
|
||||
return fa < fb;
|
||||
});
|
||||
}
|
||||
|
||||
std::vector<Vector3<float>> Astar::FindPath(const Vector3<float>& start, const Vector3<float>& end, const NavigationMesh& navMesh)
|
||||
std::vector<Vector3<float>> Astar::find_path(const Vector3<float>& start, const Vector3<float>& end,
|
||||
const NavigationMesh& nav_mesh) noexcept
|
||||
{
|
||||
std::unordered_map<Vector3<float>, PathNode> closedList;
|
||||
std::unordered_map<Vector3<float>, PathNode> openList;
|
||||
std::unordered_map<Vector3<float>, PathNode> closed_list;
|
||||
std::unordered_map<Vector3<float>, PathNode> open_list;
|
||||
|
||||
auto maybeStartVertex = navMesh.GetClosestVertex(start);
|
||||
auto maybeEndVertex = navMesh.GetClosestVertex(end);
|
||||
auto maybe_start_vertex = nav_mesh.get_closest_vertex(start);
|
||||
auto maybe_end_vertex = nav_mesh.get_closest_vertex(end);
|
||||
|
||||
if (!maybeStartVertex || !maybeEndVertex)
|
||||
if (!maybe_start_vertex || !maybe_end_vertex)
|
||||
return {};
|
||||
|
||||
const auto startVertex = maybeStartVertex.value();
|
||||
const auto endVertex = maybeEndVertex.value();
|
||||
const auto start_vertex = maybe_start_vertex.value();
|
||||
const auto end_vertex = maybe_end_vertex.value();
|
||||
|
||||
open_list.emplace(start_vertex, PathNode{std::nullopt, 0.f});
|
||||
|
||||
openList.emplace(startVertex, PathNode{std::nullopt, 0.f});
|
||||
|
||||
while (!openList.empty())
|
||||
while (!open_list.empty())
|
||||
{
|
||||
auto currentIt = GetPerfectNode(openList, endVertex);
|
||||
auto current_it = get_perfect_node(open_list, end_vertex);
|
||||
|
||||
const auto current = currentIt->first;
|
||||
const auto currentNode = currentIt->second;
|
||||
const auto current = current_it->first;
|
||||
const auto current_node = current_it->second;
|
||||
|
||||
if (current == endVertex)
|
||||
return ReconstructFinalPath(closedList, current);
|
||||
if (current == end_vertex)
|
||||
return reconstruct_final_path(closed_list, current);
|
||||
|
||||
closed_list.emplace(current, current_node);
|
||||
open_list.erase(current_it);
|
||||
|
||||
closedList.emplace(current, currentNode);
|
||||
openList.erase(currentIt);
|
||||
|
||||
for (const auto& neighbor: navMesh.GetNeighbors(current))
|
||||
for (const auto& neighbor: nav_mesh.get_neighbors(current))
|
||||
{
|
||||
if (closedList.contains(neighbor))
|
||||
if (closed_list.contains(neighbor))
|
||||
continue;
|
||||
|
||||
const float tentativeGCost = currentNode.gCost + neighbor.DistTo(current);
|
||||
const float tentative_g_cost = current_node.g_cost + neighbor.distance_to(current);
|
||||
|
||||
const auto openIt = openList.find(neighbor);
|
||||
// ReSharper disable once CppTooWideScopeInitStatement
|
||||
const auto open_it = open_list.find(neighbor);
|
||||
|
||||
if (openIt == openList.end() || tentativeGCost < openIt->second.gCost)
|
||||
openList[neighbor] = PathNode{current, tentativeGCost};
|
||||
if (open_it == open_list.end() || tentative_g_cost < open_it->second.g_cost)
|
||||
open_list[neighbor] = PathNode{current, tentative_g_cost};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,94 +2,90 @@
|
||||
// Created by Vlad on 28.07.2024.
|
||||
//
|
||||
#include "omath/pathfinding/navigation_mesh.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <stdexcept>
|
||||
namespace omath::pathfinding
|
||||
{
|
||||
std::expected<Vector3<float>, std::string> NavigationMesh::GetClosestVertex(const Vector3<float> &point) const
|
||||
std::expected<Vector3<float>, std::string>
|
||||
NavigationMesh::get_closest_vertex(const Vector3<float>& point) const noexcept
|
||||
{
|
||||
const auto res = std::ranges::min_element(m_verTextMap,
|
||||
[&point](const auto& a, const auto& b)
|
||||
{
|
||||
return a.first.DistTo(point) < b.first.DistTo(point);
|
||||
});
|
||||
const auto res = std::ranges::min_element(m_vertex_map, [&point](const auto& a, const auto& b)
|
||||
{ return a.first.distance_to(point) < b.first.distance_to(point); });
|
||||
|
||||
if (res == m_verTextMap.cend())
|
||||
if (res == m_vertex_map.cend())
|
||||
return std::unexpected("Failed to get clossest point");
|
||||
|
||||
return res->first;
|
||||
}
|
||||
|
||||
const std::vector<Vector3<float>>& NavigationMesh::GetNeighbors(const Vector3<float> &vertex) const
|
||||
const std::vector<Vector3<float>>& NavigationMesh::get_neighbors(const Vector3<float>& vertex) const noexcept
|
||||
{
|
||||
return m_verTextMap.at(vertex);
|
||||
return m_vertex_map.at(vertex);
|
||||
}
|
||||
|
||||
bool NavigationMesh::Empty() const
|
||||
bool NavigationMesh::empty() const
|
||||
{
|
||||
return m_verTextMap.empty();
|
||||
return m_vertex_map.empty();
|
||||
}
|
||||
|
||||
std::vector<uint8_t> NavigationMesh::Serialize() const
|
||||
std::vector<uint8_t> NavigationMesh::serialize() const noexcept
|
||||
{
|
||||
auto dumpToVector =[]<typename T>(const T& t, std::vector<uint8_t>& vec){
|
||||
auto dump_to_vector = []<typename T>(const T& t, std::vector<uint8_t>& vec)
|
||||
{
|
||||
for (size_t i = 0; i < sizeof(t); i++)
|
||||
vec.push_back(*(reinterpret_cast<const uint8_t*>(&t)+i));
|
||||
vec.push_back(*(reinterpret_cast<const uint8_t*>(&t) + i));
|
||||
};
|
||||
|
||||
std::vector<uint8_t> raw;
|
||||
|
||||
|
||||
for (const auto& [vertex, neighbors] : m_verTextMap)
|
||||
for (const auto& [vertex, neighbors]: m_vertex_map)
|
||||
{
|
||||
const auto neighborsCount = neighbors.size();
|
||||
const auto neighbors_count = neighbors.size();
|
||||
|
||||
dumpToVector(vertex, raw);
|
||||
dumpToVector(neighborsCount, raw);
|
||||
dump_to_vector(vertex, raw);
|
||||
dump_to_vector(neighbors_count, raw);
|
||||
|
||||
for (const auto& neighbor : neighbors)
|
||||
dumpToVector(neighbor, raw);
|
||||
for (const auto& neighbor: neighbors)
|
||||
dump_to_vector(neighbor, raw);
|
||||
}
|
||||
return raw;
|
||||
|
||||
}
|
||||
|
||||
void NavigationMesh::Deserialize(const std::vector<uint8_t> &raw)
|
||||
void NavigationMesh::deserialize(const std::vector<uint8_t>& raw) noexcept
|
||||
{
|
||||
auto loadFromVector = [](const std::vector<uint8_t>& vec, size_t& offset, auto& value)
|
||||
auto load_from_vector = [](const std::vector<uint8_t>& vec, size_t& offset, auto& value)
|
||||
{
|
||||
if (offset + sizeof(value) > vec.size())
|
||||
{
|
||||
throw std::runtime_error("Deserialize: Invalid input data size.");
|
||||
}
|
||||
std::copy_n(vec.data() + offset, sizeof(value), (uint8_t*)&value);
|
||||
std::copy_n(vec.data() + offset, sizeof(value), reinterpret_cast<uint8_t*>(&value));
|
||||
offset += sizeof(value);
|
||||
};
|
||||
|
||||
m_verTextMap.clear();
|
||||
m_vertex_map.clear();
|
||||
|
||||
size_t offset = 0;
|
||||
|
||||
while (offset < raw.size())
|
||||
{
|
||||
Vector3<float> vertex;
|
||||
loadFromVector(raw, offset, vertex);
|
||||
load_from_vector(raw, offset, vertex);
|
||||
|
||||
uint16_t neighborsCount;
|
||||
loadFromVector(raw, offset, neighborsCount);
|
||||
uint16_t neighbors_count;
|
||||
load_from_vector(raw, offset, neighbors_count);
|
||||
|
||||
std::vector<Vector3<float>> neighbors;
|
||||
neighbors.reserve(neighborsCount);
|
||||
neighbors.reserve(neighbors_count);
|
||||
|
||||
for (size_t i = 0; i < neighborsCount; ++i)
|
||||
for (size_t i = 0; i < neighbors_count; ++i)
|
||||
{
|
||||
Vector3<float> neighbor;
|
||||
loadFromVector(raw, offset, neighbor);
|
||||
load_from_vector(raw, offset, neighbor);
|
||||
neighbors.push_back(neighbor);
|
||||
}
|
||||
|
||||
m_verTextMap.emplace(vertex, std::move(neighbors));
|
||||
m_vertex_map.emplace(vertex, std::move(neighbors));
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace omath::pathfinding
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
target_sources(omath PRIVATE proj_pred_engine_legacy.cpp projectile.cpp target.cpp proj_pred_engine_avx2.cpp proj_pred_engine.cpp)
|
||||
@@ -1,10 +0,0 @@
|
||||
//
|
||||
// Created by Vlad on 2/23/2025.
|
||||
//
|
||||
#include "omath/projectile_prediction/proj_pred_engine.hpp"
|
||||
|
||||
|
||||
namespace omath::projectile_prediction
|
||||
{
|
||||
|
||||
} // namespace omath::projectile_prediction
|
||||
@@ -14,8 +14,8 @@
|
||||
namespace omath::projectile_prediction
|
||||
{
|
||||
std::optional<Vector3<float>>
|
||||
ProjPredEngineAVX2::MaybeCalculateAimPoint([[maybe_unused]] const Projectile& projectile,
|
||||
[[maybe_unused]] const Target& target) const
|
||||
ProjPredEngineAvx2::maybe_calculate_aim_point([[maybe_unused]] const Projectile& projectile,
|
||||
[[maybe_unused]] const Target& target) const
|
||||
{
|
||||
#if defined(OMATH_USE_AVX2) && defined(__i386__) && defined(__x86_64__)
|
||||
const float bulletGravity = m_gravityConstant * projectile.m_gravityScale;
|
||||
@@ -28,16 +28,16 @@ namespace omath::projectile_prediction
|
||||
|
||||
for (; currentTime <= m_maximumSimulationTime; currentTime += m_simulationTimeStep * SIMD_FACTOR)
|
||||
{
|
||||
const __m256 times =
|
||||
_mm256_setr_ps(currentTime, currentTime + m_simulationTimeStep,
|
||||
currentTime + m_simulationTimeStep * 2, currentTime + m_simulationTimeStep * 3,
|
||||
currentTime + m_simulationTimeStep * 4, currentTime + m_simulationTimeStep * 5,
|
||||
currentTime + m_simulationTimeStep * 6, currentTime + m_simulationTimeStep * 7);
|
||||
const __m256 times
|
||||
= _mm256_setr_ps(currentTime, currentTime + m_simulationTimeStep,
|
||||
currentTime + m_simulationTimeStep * 2, currentTime + m_simulationTimeStep * 3,
|
||||
currentTime + m_simulationTimeStep * 4, currentTime + m_simulationTimeStep * 5,
|
||||
currentTime + m_simulationTimeStep * 6, currentTime + m_simulationTimeStep * 7);
|
||||
|
||||
const __m256 targetX =
|
||||
_mm256_fmadd_ps(_mm256_set1_ps(target.m_velocity.x), times, _mm256_set1_ps(target.m_origin.x));
|
||||
const __m256 targetY =
|
||||
_mm256_fmadd_ps(_mm256_set1_ps(target.m_velocity.y), times, _mm256_set1_ps(target.m_origin.y));
|
||||
const __m256 targetX
|
||||
= _mm256_fmadd_ps(_mm256_set1_ps(target.m_velocity.x), times, _mm256_set1_ps(target.m_origin.x));
|
||||
const __m256 targetY
|
||||
= _mm256_fmadd_ps(_mm256_set1_ps(target.m_velocity.y), times, _mm256_set1_ps(target.m_origin.y));
|
||||
const __m256 timesSq = _mm256_mul_ps(times, times);
|
||||
const __m256 targetZ = _mm256_fmadd_ps(_mm256_set1_ps(target.m_velocity.z), times,
|
||||
_mm256_fnmadd_ps(_mm256_set1_ps(0.5f * m_gravityConstant), timesSq,
|
||||
@@ -112,25 +112,32 @@ namespace omath::projectile_prediction
|
||||
}
|
||||
|
||||
return std::nullopt;
|
||||
#else
|
||||
throw std::runtime_error(
|
||||
std::format("{} AVX2 feature is not enabled!", std::source_location::current().function_name()));
|
||||
#endif
|
||||
}
|
||||
ProjPredEngineAVX2::ProjPredEngineAVX2(const float gravityConstant, const float simulationTimeStep,
|
||||
const float maximumSimulationTime) :
|
||||
m_gravityConstant(gravityConstant), m_simulationTimeStep(simulationTimeStep),
|
||||
m_maximumSimulationTime(maximumSimulationTime)
|
||||
ProjPredEngineAvx2::ProjPredEngineAvx2(const float gravity_constant, const float simulation_time_step,
|
||||
const float maximum_simulation_time)
|
||||
: m_gravity_constant(gravity_constant), m_simulation_time_step(simulation_time_step),
|
||||
m_maximum_simulation_time(maximum_simulation_time)
|
||||
{
|
||||
}
|
||||
std::optional<float> ProjPredEngineAVX2::CalculatePitch(const Vector3<float>& projOrigin,
|
||||
const Vector3<float>& targetPos, const float bulletGravity,
|
||||
const float v0, const float time)
|
||||
std::optional<float> ProjPredEngineAvx2::calculate_pitch([[maybe_unused]] const Vector3<float>& proj_origin,
|
||||
[[maybe_unused]] const Vector3<float>& target_pos,
|
||||
[[maybe_unused]] const float bullet_gravity,
|
||||
[[maybe_unused]] const float v0,
|
||||
[[maybe_unused]] const float time)
|
||||
{
|
||||
#if defined(OMATH_USE_AVX2) && defined(__i386__) && defined(__x86_64__)
|
||||
if (time <= 0.0f)
|
||||
return std::nullopt;
|
||||
|
||||
const Vector3 delta = targetPos - projOrigin;
|
||||
const Vector3 delta = target_pos - proj_origin;
|
||||
const float dSqr = delta.x * delta.x + delta.y * delta.y;
|
||||
const float h = delta.z;
|
||||
|
||||
const float term = h + 0.5f * bulletGravity * time * time;
|
||||
const float term = h + 0.5f * bullet_gravity * time * time;
|
||||
const float requiredV0Sqr = (dSqr + term * term) / (time * time);
|
||||
const float v0Sqr = v0 * v0;
|
||||
|
||||
@@ -140,7 +147,6 @@ namespace omath::projectile_prediction
|
||||
if (dSqr == 0.0f)
|
||||
return term >= 0.0f ? 90.0f : -90.0f;
|
||||
|
||||
|
||||
const float d = std::sqrt(dSqr);
|
||||
const float tanTheta = term / d;
|
||||
return angles::RadiansToDegrees(std::atan(tanTheta));
|
||||
|
||||
@@ -1,68 +0,0 @@
|
||||
#include "omath/projectile_prediction/proj_pred_engine_legacy.hpp"
|
||||
#include <cmath>
|
||||
#include <omath/angles.hpp>
|
||||
|
||||
namespace omath::projectile_prediction
|
||||
{
|
||||
ProjPredEngineLegacy::ProjPredEngineLegacy(const float gravityConstant, const float simulationTimeStep,
|
||||
const float maximumSimulationTime, const float distanceTolerance) :
|
||||
m_gravityConstant(gravityConstant), m_simulationTimeStep(simulationTimeStep),
|
||||
m_maximumSimulationTime(maximumSimulationTime), m_distanceTolerance(distanceTolerance)
|
||||
{
|
||||
}
|
||||
|
||||
std::optional<Vector3<float>> ProjPredEngineLegacy::MaybeCalculateAimPoint(const Projectile& projectile,
|
||||
const Target& target) const
|
||||
{
|
||||
for (float time = 0.f; time < m_maximumSimulationTime; time += m_simulationTimeStep)
|
||||
{
|
||||
const auto predictedTargetPosition = target.PredictPosition(time, m_gravityConstant);
|
||||
|
||||
const auto projectilePitch = MaybeCalculateProjectileLaunchPitchAngle(projectile, predictedTargetPosition);
|
||||
|
||||
if (!projectilePitch.has_value()) [[unlikely]]
|
||||
continue;
|
||||
|
||||
if (!IsProjectileReachedTarget(predictedTargetPosition, projectile, projectilePitch.value(), time))
|
||||
continue;
|
||||
|
||||
const auto delta2d = (predictedTargetPosition - projectile.m_origin).Length2D();
|
||||
const auto height = delta2d * std::tan(angles::DegreesToRadians(projectilePitch.value()));
|
||||
|
||||
return Vector3(predictedTargetPosition.x, predictedTargetPosition.y, projectile.m_origin.z + height);
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<float>
|
||||
ProjPredEngineLegacy::MaybeCalculateProjectileLaunchPitchAngle(const Projectile& projectile,
|
||||
const Vector3<float>& targetPosition) const
|
||||
{
|
||||
const auto bulletGravity = m_gravityConstant * projectile.m_gravityScale;
|
||||
const auto delta = targetPosition - projectile.m_origin;
|
||||
|
||||
const auto distance2d = delta.Length2D();
|
||||
const auto distance2dSqr = distance2d * distance2d;
|
||||
const auto launchSpeedSqr = projectile.m_launchSpeed * projectile.m_launchSpeed;
|
||||
|
||||
float root = launchSpeedSqr * launchSpeedSqr -
|
||||
bulletGravity * (bulletGravity * distance2dSqr + 2.0f * delta.z * launchSpeedSqr);
|
||||
|
||||
if (root < 0.0f) [[unlikely]]
|
||||
return std::nullopt;
|
||||
|
||||
root = std::sqrt(root);
|
||||
const float angle = std::atan((launchSpeedSqr - root) / (bulletGravity * distance2d));
|
||||
|
||||
return angles::RadiansToDegrees(angle);
|
||||
}
|
||||
|
||||
bool ProjPredEngineLegacy::IsProjectileReachedTarget(const Vector3<float>& targetPosition, const Projectile& projectile,
|
||||
const float pitch, const float time) const
|
||||
{
|
||||
const auto yaw = projectile.m_origin.ViewAngleTo(targetPosition).y;
|
||||
const auto projectilePosition = projectile.PredictPosition(pitch, yaw, time, m_gravityConstant);
|
||||
|
||||
return projectilePosition.DistTo(targetPosition) <= m_distanceTolerance;
|
||||
}
|
||||
} // namespace omath::projectile_prediction
|
||||
@@ -1,21 +0,0 @@
|
||||
//
|
||||
// Created by Vlad on 6/9/2024.
|
||||
//
|
||||
|
||||
#include "omath/projectile_prediction/projectile.hpp"
|
||||
|
||||
#include <omath/engines/source_engine/formulas.hpp>
|
||||
|
||||
namespace omath::projectile_prediction
|
||||
{
|
||||
Vector3<float> Projectile::PredictPosition(const float pitch, const float yaw, const float time, const float gravity) const
|
||||
{
|
||||
auto currentPos = m_origin + source_engine::ForwardVector({source_engine::PitchAngle::FromDegrees(-pitch),
|
||||
source_engine::YawAngle::FromDegrees(yaw),
|
||||
source_engine::RollAngle::FromDegrees(0)}) *
|
||||
m_launchSpeed * time;
|
||||
currentPos.z -= (gravity * m_gravityScale) * (time * time) * 0.5f;
|
||||
|
||||
return currentPos;
|
||||
}
|
||||
} // namespace omath::prediction
|
||||
@@ -1,11 +0,0 @@
|
||||
//
|
||||
// Created by Vlad on 6/9/2024.
|
||||
//
|
||||
|
||||
#include "omath/projectile_prediction/projectile.hpp"
|
||||
|
||||
|
||||
namespace omath::prediction
|
||||
{
|
||||
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
target_sources(omath PRIVATE camera.cpp)
|
||||
@@ -1,9 +0,0 @@
|
||||
//
|
||||
// Created by Vlad on 27.08.2024.
|
||||
//
|
||||
#include "omath/projection/camera.hpp"
|
||||
|
||||
|
||||
namespace omath::projection
|
||||
{
|
||||
}
|
||||
@@ -1,40 +1,22 @@
|
||||
enable_testing()
|
||||
|
||||
project(unit-tests)
|
||||
project(unit_tests)
|
||||
|
||||
include(GoogleTest)
|
||||
add_executable(unit-tests
|
||||
general/unit_test_prediction.cpp
|
||||
general/unit_test_matrix.cpp
|
||||
general/unit_test_mat.cpp
|
||||
general/unit_test_a_star.cpp
|
||||
general/unit_test_projection.cpp
|
||||
general/unit_test_vector3.cpp
|
||||
general/unit_test_vector2.cpp
|
||||
general/unit_test_color.cpp
|
||||
general/unit_test_vector4.cpp
|
||||
general/unit_test_line_trace.cpp
|
||||
general/unit_test_angles.cpp
|
||||
general/unit_test_view_angles.cpp
|
||||
general/unit_test_angle.cpp
|
||||
general/unit_test_triangle.cpp
|
||||
|
||||
engines/unit_test_open_gl.cpp
|
||||
engines/unit_test_unity_engine.cpp
|
||||
engines/unit_test_source_engine.cpp
|
||||
engines/unit_test_iw_engine.cpp
|
||||
file(GLOB_RECURSE UNIT_TESTS_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")
|
||||
add_executable(${PROJECT_NAME} ${UNIT_TESTS_SOURCES})
|
||||
|
||||
)
|
||||
|
||||
set_target_properties(unit-tests PROPERTIES
|
||||
set_target_properties(unit_tests PROPERTIES
|
||||
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
|
||||
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
|
||||
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
|
||||
UNITY_BUILD ON
|
||||
UNITY_BUILD_BATCH_SIZE 20
|
||||
CXX_STANDARD 23
|
||||
CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
|
||||
target_link_libraries(unit-tests PRIVATE gtest gtest_main omath::omath)
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE gtest gtest_main omath::omath)
|
||||
|
||||
gtest_discover_tests(unit-tests)
|
||||
gtest_discover_tests(${PROJECT_NAME})
|
||||
@@ -7,71 +7,71 @@
|
||||
#include <omath/engines/iw_engine/formulas.hpp>
|
||||
|
||||
|
||||
TEST(UnitTestIwEngine, ForwardVector)
|
||||
TEST(unit_test_iw_engine, ForwardVector)
|
||||
{
|
||||
const auto forward = omath::iw_engine::ForwardVector({});
|
||||
const auto forward = omath::iw_engine::forward_vector({});
|
||||
|
||||
EXPECT_EQ(forward, omath::iw_engine::kAbsForward);
|
||||
EXPECT_EQ(forward, omath::iw_engine::k_abs_forward);
|
||||
}
|
||||
|
||||
TEST(UnitTestIwEngine, RightVector)
|
||||
TEST(unit_test_iw_engine, RightVector)
|
||||
{
|
||||
const auto right = omath::iw_engine::RightVector({});
|
||||
const auto right = omath::iw_engine::right_vector({});
|
||||
|
||||
EXPECT_EQ(right, omath::iw_engine::kAbsRight);
|
||||
EXPECT_EQ(right, omath::iw_engine::k_abs_right);
|
||||
}
|
||||
|
||||
TEST(UnitTestIwEngine, UpVector)
|
||||
TEST(unit_test_iw_engine, UpVector)
|
||||
{
|
||||
const auto up = omath::iw_engine::UpVector({});
|
||||
EXPECT_EQ(up, omath::iw_engine::kAbsUp);
|
||||
const auto up = omath::iw_engine::up_vector({});
|
||||
EXPECT_EQ(up, omath::iw_engine::k_abs_up);
|
||||
}
|
||||
|
||||
TEST(UnitTestIwEngine, ForwardVectorRotationYaw)
|
||||
TEST(unit_test_iw_engine, ForwardVectorRotationYaw)
|
||||
{
|
||||
omath::iw_engine::ViewAngles angles;
|
||||
|
||||
angles.yaw = omath::iw_engine::YawAngle::FromDegrees(-90.f);
|
||||
angles.yaw = omath::iw_engine::YawAngle::from_degrees(-90.f);
|
||||
|
||||
const auto forward = omath::iw_engine::ForwardVector(angles);
|
||||
EXPECT_NEAR(forward.x, omath::iw_engine::kAbsRight.x, 0.00001f);
|
||||
EXPECT_NEAR(forward.y, omath::iw_engine::kAbsRight.y, 0.00001f);
|
||||
EXPECT_NEAR(forward.z, omath::iw_engine::kAbsRight.z, 0.00001f);
|
||||
const auto forward = omath::iw_engine::forward_vector(angles);
|
||||
EXPECT_NEAR(forward.x, omath::iw_engine::k_abs_right.x, 0.00001f);
|
||||
EXPECT_NEAR(forward.y, omath::iw_engine::k_abs_right.y, 0.00001f);
|
||||
EXPECT_NEAR(forward.z, omath::iw_engine::k_abs_right.z, 0.00001f);
|
||||
}
|
||||
|
||||
TEST(UnitTestIwEngine, ForwardVectorRotationPitch)
|
||||
TEST(unit_test_iw_engine, ForwardVectorRotationPitch)
|
||||
{
|
||||
omath::iw_engine::ViewAngles angles;
|
||||
|
||||
angles.pitch = omath::iw_engine::PitchAngle::FromDegrees(-89.f);
|
||||
angles.pitch = omath::iw_engine::PitchAngle::from_degrees(-89.f);
|
||||
|
||||
const auto forward = omath::iw_engine::ForwardVector(angles);
|
||||
EXPECT_NEAR(forward.x, omath::iw_engine::kAbsUp.x, 0.02f);
|
||||
EXPECT_NEAR(forward.y, omath::iw_engine::kAbsUp.y, 0.01f);
|
||||
EXPECT_NEAR(forward.z, omath::iw_engine::kAbsUp.z, 0.01f);
|
||||
const auto forward = omath::iw_engine::forward_vector(angles);
|
||||
EXPECT_NEAR(forward.x, omath::iw_engine::k_abs_up.x, 0.02f);
|
||||
EXPECT_NEAR(forward.y, omath::iw_engine::k_abs_up.y, 0.01f);
|
||||
EXPECT_NEAR(forward.z, omath::iw_engine::k_abs_up.z, 0.01f);
|
||||
}
|
||||
|
||||
TEST(UnitTestIwEngine, ForwardVectorRotationRoll)
|
||||
TEST(unit_test_iw_engine, ForwardVectorRotationRoll)
|
||||
{
|
||||
omath::iw_engine::ViewAngles angles;
|
||||
|
||||
angles.roll = omath::iw_engine::RollAngle::FromDegrees(90.f);
|
||||
angles.roll = omath::iw_engine::RollAngle::from_degrees(90.f);
|
||||
|
||||
const auto forward = omath::iw_engine::UpVector(angles);
|
||||
EXPECT_NEAR(forward.x, omath::iw_engine::kAbsRight.x, 0.00001f);
|
||||
EXPECT_NEAR(forward.y, omath::iw_engine::kAbsRight.y, 0.00001f);
|
||||
EXPECT_NEAR(forward.z, omath::iw_engine::kAbsRight.z, 0.00001f);
|
||||
const auto forward = omath::iw_engine::up_vector(angles);
|
||||
EXPECT_NEAR(forward.x, omath::iw_engine::k_abs_right.x, 0.00001f);
|
||||
EXPECT_NEAR(forward.y, omath::iw_engine::k_abs_right.y, 0.00001f);
|
||||
EXPECT_NEAR(forward.z, omath::iw_engine::k_abs_right.z, 0.00001f);
|
||||
}
|
||||
|
||||
TEST(UnitTestIwEngine, ProjectTargetMovedFromCamera)
|
||||
TEST(unit_test_iw_engine, ProjectTargetMovedFromCamera)
|
||||
{
|
||||
constexpr auto fov = omath::projection::FieldOfView::FromDegrees(90.f);
|
||||
constexpr auto fov = omath::projection::FieldOfView::from_degrees(90.f);
|
||||
const auto cam = omath::iw_engine::Camera({0, 0, 0}, {}, {1920.f, 1080.f}, fov, 0.01f, 1000.f);
|
||||
|
||||
|
||||
for (float distance = 0.02f; distance < 1000.f; distance += 0.01f)
|
||||
{
|
||||
const auto projected = cam.WorldToScreen({distance, 0, 0});
|
||||
const auto projected = cam.world_to_screen({distance, 0, 0});
|
||||
|
||||
EXPECT_TRUE(projected.has_value());
|
||||
|
||||
@@ -83,23 +83,23 @@ TEST(UnitTestIwEngine, ProjectTargetMovedFromCamera)
|
||||
}
|
||||
}
|
||||
|
||||
TEST(UnitTestIwEngine, CameraSetAndGetFov)
|
||||
TEST(unit_test_iw_engine, CameraSetAndGetFov)
|
||||
{
|
||||
constexpr auto fov = omath::projection::FieldOfView::FromDegrees(90.f);
|
||||
constexpr auto fov = omath::projection::FieldOfView::from_degrees(90.f);
|
||||
auto cam = omath::iw_engine::Camera({0, 0, 0}, {}, {1920.f, 1080.f}, fov, 0.01f, 1000.f);
|
||||
|
||||
EXPECT_EQ(cam.GetFieldOfView().AsDegrees(), 90.f);
|
||||
cam.SetFieldOfView(omath::projection::FieldOfView::FromDegrees(50.f));
|
||||
EXPECT_EQ(cam.get_field_of_view().as_degrees(), 90.f);
|
||||
cam.set_field_of_view(omath::projection::FieldOfView::from_degrees(50.f));
|
||||
|
||||
EXPECT_EQ(cam.GetFieldOfView().AsDegrees(), 50.f);
|
||||
EXPECT_EQ(cam.get_field_of_view().as_degrees(), 50.f);
|
||||
}
|
||||
|
||||
TEST(UnitTestIwEngine, CameraSetAndGetOrigin)
|
||||
TEST(unit_test_iw_engine, CameraSetAndGetOrigin)
|
||||
{
|
||||
auto cam = omath::iw_engine::Camera({0, 0, 0}, {}, {1920.f, 1080.f}, {}, 0.01f, 1000.f);
|
||||
|
||||
EXPECT_EQ(cam.GetOrigin(), omath::Vector3<float>{});
|
||||
cam.SetFieldOfView(omath::projection::FieldOfView::FromDegrees(50.f));
|
||||
EXPECT_EQ(cam.get_origin(), omath::Vector3<float>{});
|
||||
cam.set_field_of_view(omath::projection::FieldOfView::from_degrees(50.f));
|
||||
|
||||
EXPECT_EQ(cam.GetFieldOfView().AsDegrees(), 50.f);
|
||||
EXPECT_EQ(cam.get_field_of_view().as_degrees(), 50.f);
|
||||
}
|
||||
@@ -7,71 +7,71 @@
|
||||
#include <omath/engines/opengl_engine/formulas.hpp>
|
||||
|
||||
|
||||
TEST(UnitTestOpenGL, ForwardVector)
|
||||
TEST(unit_test_opengl, ForwardVector)
|
||||
{
|
||||
const auto forward = omath::opengl_engine::ForwardVector({});
|
||||
EXPECT_EQ(forward, omath::opengl_engine::kAbsForward);
|
||||
const auto forward = omath::opengl_engine::forward_vector({});
|
||||
EXPECT_EQ(forward, omath::opengl_engine::k_abs_forward);
|
||||
}
|
||||
|
||||
TEST(UnitTestOpenGL, RightVector)
|
||||
TEST(unit_test_opengl, RightVector)
|
||||
{
|
||||
const auto right = omath::opengl_engine::RightVector({});
|
||||
EXPECT_EQ(right, omath::opengl_engine::kAbsRight);
|
||||
const auto right = omath::opengl_engine::right_vector({});
|
||||
EXPECT_EQ(right, omath::opengl_engine::k_abs_right);
|
||||
}
|
||||
|
||||
TEST(UnitTestOpenGL, UpVector)
|
||||
TEST(unit_test_opengl, UpVector)
|
||||
{
|
||||
const auto up = omath::opengl_engine::UpVector({});
|
||||
EXPECT_EQ(up, omath::opengl_engine::kAbsUp);
|
||||
const auto up = omath::opengl_engine::up_vector({});
|
||||
EXPECT_EQ(up, omath::opengl_engine::k_abs_up);
|
||||
}
|
||||
|
||||
TEST(UnitTestOpenGL, ForwardVectorRotationYaw)
|
||||
TEST(unit_test_opengl, ForwardVectorRotationYaw)
|
||||
{
|
||||
omath::opengl_engine::ViewAngles angles;
|
||||
|
||||
angles.yaw = omath::opengl_engine::YawAngle::FromDegrees(90.f);
|
||||
angles.yaw = omath::opengl_engine::YawAngle::from_degrees(90.f);
|
||||
|
||||
const auto forward = omath::opengl_engine::ForwardVector(angles);
|
||||
EXPECT_NEAR(forward.x, omath::opengl_engine::kAbsRight.x, 0.00001f);
|
||||
EXPECT_NEAR(forward.y, omath::opengl_engine::kAbsRight.y, 0.00001f);
|
||||
EXPECT_NEAR(forward.z, omath::opengl_engine::kAbsRight.z, 0.00001f);
|
||||
const auto forward = omath::opengl_engine::forward_vector(angles);
|
||||
EXPECT_NEAR(forward.x, omath::opengl_engine::k_abs_right.x, 0.00001f);
|
||||
EXPECT_NEAR(forward.y, omath::opengl_engine::k_abs_right.y, 0.00001f);
|
||||
EXPECT_NEAR(forward.z, omath::opengl_engine::k_abs_right.z, 0.00001f);
|
||||
}
|
||||
|
||||
|
||||
|
||||
TEST(UnitTestOpenGL, ForwardVectorRotationPitch)
|
||||
TEST(unit_test_opengl, ForwardVectorRotationPitch)
|
||||
{
|
||||
omath::opengl_engine::ViewAngles angles;
|
||||
|
||||
angles.pitch = omath::opengl_engine::PitchAngle::FromDegrees(-90.f);
|
||||
angles.pitch = omath::opengl_engine::PitchAngle::from_degrees(-90.f);
|
||||
|
||||
const auto forward = omath::opengl_engine::ForwardVector(angles);
|
||||
EXPECT_NEAR(forward.x, omath::opengl_engine::kAbsUp.x, 0.00001f);
|
||||
EXPECT_NEAR(forward.y, omath::opengl_engine::kAbsUp.y, 0.00001f);
|
||||
EXPECT_NEAR(forward.z, omath::opengl_engine::kAbsUp.z, 0.00001f);
|
||||
const auto forward = omath::opengl_engine::forward_vector(angles);
|
||||
EXPECT_NEAR(forward.x, omath::opengl_engine::k_abs_up.x, 0.00001f);
|
||||
EXPECT_NEAR(forward.y, omath::opengl_engine::k_abs_up.y, 0.00001f);
|
||||
EXPECT_NEAR(forward.z, omath::opengl_engine::k_abs_up.z, 0.00001f);
|
||||
}
|
||||
|
||||
TEST(UnitTestOpenGL, ForwardVectorRotationRoll)
|
||||
TEST(unit_test_opengl, ForwardVectorRotationRoll)
|
||||
{
|
||||
omath::opengl_engine::ViewAngles angles;
|
||||
|
||||
angles.roll = omath::opengl_engine::RollAngle::FromDegrees(-90.f);
|
||||
angles.roll = omath::opengl_engine::RollAngle::from_degrees(-90.f);
|
||||
|
||||
const auto forward = omath::opengl_engine::UpVector(angles);
|
||||
EXPECT_NEAR(forward.x, omath::opengl_engine::kAbsRight.x, 0.00001f);
|
||||
EXPECT_NEAR(forward.y, omath::opengl_engine::kAbsRight.y, 0.00001f);
|
||||
EXPECT_NEAR(forward.z, omath::opengl_engine::kAbsRight.z, 0.00001f);
|
||||
const auto forward = omath::opengl_engine::up_vector(angles);
|
||||
EXPECT_NEAR(forward.x, omath::opengl_engine::k_abs_right.x, 0.00001f);
|
||||
EXPECT_NEAR(forward.y, omath::opengl_engine::k_abs_right.y, 0.00001f);
|
||||
EXPECT_NEAR(forward.z, omath::opengl_engine::k_abs_right.z, 0.00001f);
|
||||
}
|
||||
|
||||
TEST(UnitTestOpenGL, ProjectTargetMovedFromCamera)
|
||||
TEST(unit_test_opengl, ProjectTargetMovedFromCamera)
|
||||
{
|
||||
constexpr auto fov = omath::projection::FieldOfView::FromDegrees(90.f);
|
||||
constexpr auto fov = omath::projection::FieldOfView::from_degrees(90.f);
|
||||
const auto cam = omath::opengl_engine::Camera({0, 0, 0}, {}, {1920.f, 1080.f}, fov, 0.01f, 1000.f);
|
||||
|
||||
|
||||
for (float distance = -10.f; distance > -1000.f; distance -= 0.01f)
|
||||
{
|
||||
const auto projected = cam.WorldToScreen({0, 0, distance});
|
||||
const auto projected = cam.world_to_screen({0, 0, distance});
|
||||
|
||||
EXPECT_TRUE(projected.has_value());
|
||||
|
||||
@@ -83,23 +83,23 @@ TEST(UnitTestOpenGL, ProjectTargetMovedFromCamera)
|
||||
}
|
||||
}
|
||||
|
||||
TEST(UnitTestOpenGL, CameraSetAndGetFov)
|
||||
TEST(unit_test_opengl, CameraSetAndGetFov)
|
||||
{
|
||||
constexpr auto fov = omath::projection::FieldOfView::FromDegrees(90.f);
|
||||
constexpr auto fov = omath::projection::FieldOfView::from_degrees(90.f);
|
||||
auto cam = omath::opengl_engine::Camera({0, 0, 0}, {}, {1920.f, 1080.f}, fov, 0.01f, 1000.f);
|
||||
|
||||
EXPECT_EQ(cam.GetFieldOfView().AsDegrees(), 90.f);
|
||||
cam.SetFieldOfView(omath::projection::FieldOfView::FromDegrees(50.f));
|
||||
EXPECT_EQ(cam.get_field_of_view().as_degrees(), 90.f);
|
||||
cam.set_field_of_view(omath::projection::FieldOfView::from_degrees(50.f));
|
||||
|
||||
EXPECT_EQ(cam.GetFieldOfView().AsDegrees(), 50.f);
|
||||
EXPECT_EQ(cam.get_field_of_view().as_degrees(), 50.f);
|
||||
}
|
||||
|
||||
TEST(UnitTestOpenGL, CameraSetAndGetOrigin)
|
||||
TEST(unit_test_opengl, CameraSetAndGetOrigin)
|
||||
{
|
||||
auto cam = omath::opengl_engine::Camera({0, 0, 0}, {}, {1920.f, 1080.f}, {}, 0.01f, 1000.f);
|
||||
|
||||
EXPECT_EQ(cam.GetOrigin(), omath::Vector3<float>{});
|
||||
cam.SetFieldOfView(omath::projection::FieldOfView::FromDegrees(50.f));
|
||||
EXPECT_EQ(cam.get_origin(), omath::Vector3<float>{});
|
||||
cam.set_field_of_view(omath::projection::FieldOfView::from_degrees(50.f));
|
||||
|
||||
EXPECT_EQ(cam.GetFieldOfView().AsDegrees(), 50.f);
|
||||
EXPECT_EQ(cam.get_field_of_view().as_degrees(), 50.f);
|
||||
}
|
||||
@@ -7,71 +7,71 @@
|
||||
#include <omath/engines/source_engine/formulas.hpp>
|
||||
|
||||
|
||||
TEST(UnitTestSourceEngine, ForwardVector)
|
||||
TEST(unit_test_source_engine, ForwardVector)
|
||||
{
|
||||
const auto forward = omath::source_engine::ForwardVector({});
|
||||
const auto forward = omath::source_engine::forward_vector({});
|
||||
|
||||
EXPECT_EQ(forward, omath::source_engine::kAbsForward);
|
||||
EXPECT_EQ(forward, omath::source_engine::k_abs_forward);
|
||||
}
|
||||
|
||||
TEST(UnitTestSourceEngine, RightVector)
|
||||
TEST(unit_test_source_engine, RightVector)
|
||||
{
|
||||
const auto right = omath::source_engine::RightVector({});
|
||||
const auto right = omath::source_engine::right_vector({});
|
||||
|
||||
EXPECT_EQ(right, omath::source_engine::kAbsRight);
|
||||
EXPECT_EQ(right, omath::source_engine::k_abs_right);
|
||||
}
|
||||
|
||||
TEST(UnitTestSourceEngine, UpVector)
|
||||
TEST(unit_test_source_engine, UpVector)
|
||||
{
|
||||
const auto up = omath::source_engine::UpVector({});
|
||||
EXPECT_EQ(up, omath::source_engine::kAbsUp);
|
||||
const auto up = omath::source_engine::up_vector({});
|
||||
EXPECT_EQ(up, omath::source_engine::k_abs_up);
|
||||
}
|
||||
|
||||
TEST(UnitTestSourceEngine, ForwardVectorRotationYaw)
|
||||
TEST(unit_test_source_engine, ForwardVectorRotationYaw)
|
||||
{
|
||||
omath::source_engine::ViewAngles angles;
|
||||
|
||||
angles.yaw = omath::source_engine::YawAngle::FromDegrees(-90.f);
|
||||
angles.yaw = omath::source_engine::YawAngle::from_degrees(-90.f);
|
||||
|
||||
const auto forward = omath::source_engine::ForwardVector(angles);
|
||||
EXPECT_NEAR(forward.x, omath::source_engine::kAbsRight.x, 0.00001f);
|
||||
EXPECT_NEAR(forward.y, omath::source_engine::kAbsRight.y, 0.00001f);
|
||||
EXPECT_NEAR(forward.z, omath::source_engine::kAbsRight.z, 0.00001f);
|
||||
const auto forward = omath::source_engine::forward_vector(angles);
|
||||
EXPECT_NEAR(forward.x, omath::source_engine::k_abs_right.x, 0.00001f);
|
||||
EXPECT_NEAR(forward.y, omath::source_engine::k_abs_right.y, 0.00001f);
|
||||
EXPECT_NEAR(forward.z, omath::source_engine::k_abs_right.z, 0.00001f);
|
||||
}
|
||||
|
||||
TEST(UnitTestSourceEngine, ForwardVectorRotationPitch)
|
||||
TEST(unit_test_source_engine, ForwardVectorRotationPitch)
|
||||
{
|
||||
omath::source_engine::ViewAngles angles;
|
||||
|
||||
angles.pitch = omath::source_engine::PitchAngle::FromDegrees(-89.f);
|
||||
angles.pitch = omath::source_engine::PitchAngle::from_degrees(-89.f);
|
||||
|
||||
const auto forward = omath::source_engine::ForwardVector(angles);
|
||||
EXPECT_NEAR(forward.x, omath::source_engine::kAbsUp.x, 0.02f);
|
||||
EXPECT_NEAR(forward.y, omath::source_engine::kAbsUp.y, 0.01f);
|
||||
EXPECT_NEAR(forward.z, omath::source_engine::kAbsUp.z, 0.01f);
|
||||
const auto forward = omath::source_engine::forward_vector(angles);
|
||||
EXPECT_NEAR(forward.x, omath::source_engine::k_abs_up.x, 0.02f);
|
||||
EXPECT_NEAR(forward.y, omath::source_engine::k_abs_up.y, 0.01f);
|
||||
EXPECT_NEAR(forward.z, omath::source_engine::k_abs_up.z, 0.01f);
|
||||
}
|
||||
|
||||
TEST(UnitTestSourceEngine, ForwardVectorRotationRoll)
|
||||
TEST(unit_test_source_engine, ForwardVectorRotationRoll)
|
||||
{
|
||||
omath::source_engine::ViewAngles angles;
|
||||
|
||||
angles.roll = omath::source_engine::RollAngle::FromDegrees(90.f);
|
||||
angles.roll = omath::source_engine::RollAngle::from_degrees(90.f);
|
||||
|
||||
const auto forward = omath::source_engine::UpVector(angles);
|
||||
EXPECT_NEAR(forward.x, omath::source_engine::kAbsRight.x, 0.00001f);
|
||||
EXPECT_NEAR(forward.y, omath::source_engine::kAbsRight.y, 0.00001f);
|
||||
EXPECT_NEAR(forward.z, omath::source_engine::kAbsRight.z, 0.00001f);
|
||||
const auto forward = omath::source_engine::up_vector(angles);
|
||||
EXPECT_NEAR(forward.x, omath::source_engine::k_abs_right.x, 0.00001f);
|
||||
EXPECT_NEAR(forward.y, omath::source_engine::k_abs_right.y, 0.00001f);
|
||||
EXPECT_NEAR(forward.z, omath::source_engine::k_abs_right.z, 0.00001f);
|
||||
}
|
||||
|
||||
TEST(UnitTestSourceEngine, ProjectTargetMovedFromCamera)
|
||||
TEST(unit_test_source_engine, ProjectTargetMovedFromCamera)
|
||||
{
|
||||
constexpr auto fov = omath::projection::FieldOfView::FromDegrees(90.f);
|
||||
constexpr auto fov = omath::projection::FieldOfView::from_degrees(90.f);
|
||||
const auto cam = omath::source_engine::Camera({0, 0, 0}, {}, {1920.f, 1080.f}, fov, 0.01f, 1000.f);
|
||||
|
||||
|
||||
for (float distance = 0.02f; distance < 1000.f; distance += 0.01f)
|
||||
{
|
||||
const auto projected = cam.WorldToScreen({distance, 0, 0});
|
||||
const auto projected = cam.world_to_screen({distance, 0, 0});
|
||||
|
||||
EXPECT_TRUE(projected.has_value());
|
||||
|
||||
@@ -83,15 +83,15 @@ TEST(UnitTestSourceEngine, ProjectTargetMovedFromCamera)
|
||||
}
|
||||
}
|
||||
|
||||
TEST(UnitTestSourceEngine, ProjectTargetMovedUp)
|
||||
TEST(unit_test_source_engine, ProjectTargetMovedUp)
|
||||
{
|
||||
constexpr auto fov = omath::projection::FieldOfView::FromDegrees(90.f);
|
||||
constexpr auto fov = omath::projection::FieldOfView::from_degrees(90.f);
|
||||
const auto cam = omath::source_engine::Camera({0, 0, 0}, {}, {1920.f, 1080.f}, fov, 0.01f, 1000.f);
|
||||
|
||||
auto prev = 1080.f;
|
||||
for (float distance = 0.0f; distance < 10.f; distance += 1.f)
|
||||
{
|
||||
const auto projected = cam.WorldToScreen({100.f, 0, distance});
|
||||
const auto projected = cam.world_to_screen({100.f, 0, distance});
|
||||
EXPECT_TRUE(projected.has_value());
|
||||
|
||||
if (!projected.has_value())
|
||||
@@ -103,23 +103,23 @@ TEST(UnitTestSourceEngine, ProjectTargetMovedUp)
|
||||
}
|
||||
}
|
||||
|
||||
TEST(UnitTestSourceEngine, CameraSetAndGetFov)
|
||||
TEST(unit_test_source_engine, CameraSetAndGetFov)
|
||||
{
|
||||
constexpr auto fov = omath::projection::FieldOfView::FromDegrees(90.f);
|
||||
constexpr auto fov = omath::projection::FieldOfView::from_degrees(90.f);
|
||||
auto cam = omath::source_engine::Camera({0, 0, 0}, {}, {1920.f, 1080.f}, fov, 0.01f, 1000.f);
|
||||
|
||||
EXPECT_EQ(cam.GetFieldOfView().AsDegrees(), 90.f);
|
||||
cam.SetFieldOfView(omath::projection::FieldOfView::FromDegrees(50.f));
|
||||
EXPECT_EQ(cam.get_field_of_view().as_degrees(), 90.f);
|
||||
cam.set_field_of_view(omath::projection::FieldOfView::from_degrees(50.f));
|
||||
|
||||
EXPECT_EQ(cam.GetFieldOfView().AsDegrees(), 50.f);
|
||||
EXPECT_EQ(cam.get_field_of_view().as_degrees(), 50.f);
|
||||
}
|
||||
|
||||
TEST(UnitTestSourceEngine, CameraSetAndGetOrigin)
|
||||
TEST(unit_test_source_engine, CameraSetAndGetOrigin)
|
||||
{
|
||||
auto cam = omath::source_engine::Camera({0, 0, 0}, {}, {1920.f, 1080.f}, {}, 0.01f, 1000.f);
|
||||
|
||||
EXPECT_EQ(cam.GetOrigin(), omath::Vector3<float>{});
|
||||
cam.SetFieldOfView(omath::projection::FieldOfView::FromDegrees(50.f));
|
||||
EXPECT_EQ(cam.get_origin(), omath::Vector3<float>{});
|
||||
cam.set_field_of_view(omath::projection::FieldOfView::from_degrees(50.f));
|
||||
|
||||
EXPECT_EQ(cam.GetFieldOfView().AsDegrees(), 50.f);
|
||||
EXPECT_EQ(cam.get_field_of_view().as_degrees(), 50.f);
|
||||
}
|
||||
@@ -5,73 +5,73 @@
|
||||
#include <omath/engines/unity_engine/camera.hpp>
|
||||
#include <omath/engines/unity_engine/constants.hpp>
|
||||
#include <omath/engines/unity_engine/formulas.hpp>
|
||||
#include <print>
|
||||
|
||||
|
||||
TEST(UnitTestUnityEngine, ForwardVector)
|
||||
TEST(unit_test_unity_engine, ForwardVector)
|
||||
{
|
||||
const auto forward = omath::unity_engine::ForwardVector({});
|
||||
const auto forward = omath::unity_engine::forward_vector({});
|
||||
|
||||
EXPECT_EQ(forward, omath::unity_engine::kAbsForward);
|
||||
EXPECT_EQ(forward, omath::unity_engine::k_abs_forward);
|
||||
}
|
||||
|
||||
TEST(UnitTestUnityEngine, ForwardVectorRotationYaw)
|
||||
TEST(unit_test_unity_engine, ForwardVectorRotationYaw)
|
||||
{
|
||||
omath::unity_engine::ViewAngles angles;
|
||||
|
||||
angles.yaw = omath::unity_engine::YawAngle::FromDegrees(90.f);
|
||||
angles.yaw = omath::unity_engine::YawAngle::from_degrees(90.f);
|
||||
|
||||
const auto forward = omath::unity_engine::ForwardVector(angles);
|
||||
EXPECT_NEAR(forward.x, omath::unity_engine::kAbsRight.x, 0.00001f);
|
||||
EXPECT_NEAR(forward.y, omath::unity_engine::kAbsRight.y, 0.00001f);
|
||||
EXPECT_NEAR(forward.z, omath::unity_engine::kAbsRight.z, 0.00001f);
|
||||
const auto forward = omath::unity_engine::forward_vector(angles);
|
||||
EXPECT_NEAR(forward.x, omath::unity_engine::k_abs_right.x, 0.00001f);
|
||||
EXPECT_NEAR(forward.y, omath::unity_engine::k_abs_right.y, 0.00001f);
|
||||
EXPECT_NEAR(forward.z, omath::unity_engine::k_abs_right.z, 0.00001f);
|
||||
}
|
||||
|
||||
TEST(UnitTestUnityEngine, ForwardVectorRotationPitch)
|
||||
TEST(unit_test_unity_engine, ForwardVectorRotationPitch)
|
||||
{
|
||||
omath::unity_engine::ViewAngles angles;
|
||||
|
||||
angles.pitch = omath::unity_engine::PitchAngle::FromDegrees(-90.f);
|
||||
angles.pitch = omath::unity_engine::PitchAngle::from_degrees(-90.f);
|
||||
|
||||
const auto forward = omath::unity_engine::ForwardVector(angles);
|
||||
EXPECT_NEAR(forward.x, omath::unity_engine::kAbsUp.x, 0.00001f);
|
||||
EXPECT_NEAR(forward.y, omath::unity_engine::kAbsUp.y, 0.00001f);
|
||||
EXPECT_NEAR(forward.z, omath::unity_engine::kAbsUp.z, 0.00001f);
|
||||
const auto forward = omath::unity_engine::forward_vector(angles);
|
||||
EXPECT_NEAR(forward.x, omath::unity_engine::k_abs_up.x, 0.00001f);
|
||||
EXPECT_NEAR(forward.y, omath::unity_engine::k_abs_up.y, 0.00001f);
|
||||
EXPECT_NEAR(forward.z, omath::unity_engine::k_abs_up.z, 0.00001f);
|
||||
}
|
||||
|
||||
TEST(UnitTestUnityEngine, ForwardVectorRotationRoll)
|
||||
TEST(unit_test_unity_engine, ForwardVectorRotationRoll)
|
||||
{
|
||||
omath::unity_engine::ViewAngles angles;
|
||||
|
||||
angles.roll = omath::unity_engine::RollAngle::FromDegrees(-90.f);
|
||||
angles.roll = omath::unity_engine::RollAngle::from_degrees(-90.f);
|
||||
|
||||
const auto forward = omath::unity_engine::UpVector(angles);
|
||||
EXPECT_NEAR(forward.x, omath::unity_engine::kAbsRight.x, 0.00001f);
|
||||
EXPECT_NEAR(forward.y, omath::unity_engine::kAbsRight.y, 0.00001f);
|
||||
EXPECT_NEAR(forward.z, omath::unity_engine::kAbsRight.z, 0.00001f);
|
||||
const auto forward = omath::unity_engine::up_vector(angles);
|
||||
EXPECT_NEAR(forward.x, omath::unity_engine::k_abs_right.x, 0.00001f);
|
||||
EXPECT_NEAR(forward.y, omath::unity_engine::k_abs_right.y, 0.00001f);
|
||||
EXPECT_NEAR(forward.z, omath::unity_engine::k_abs_right.z, 0.00001f);
|
||||
}
|
||||
|
||||
TEST(UnitTestUnityEngine, RightVector)
|
||||
TEST(unit_test_unity_engine, RightVector)
|
||||
{
|
||||
const auto right = omath::unity_engine::RightVector({});
|
||||
const auto right = omath::unity_engine::right_vector({});
|
||||
|
||||
EXPECT_EQ(right, omath::unity_engine::kAbsRight);
|
||||
EXPECT_EQ(right, omath::unity_engine::k_abs_right);
|
||||
}
|
||||
|
||||
TEST(UnitTestUnityEngine, UpVector)
|
||||
TEST(unit_test_unity_engine, UpVector)
|
||||
{
|
||||
const auto up = omath::unity_engine::UpVector({});
|
||||
EXPECT_EQ(up, omath::unity_engine::kAbsUp);
|
||||
const auto up = omath::unity_engine::up_vector({});
|
||||
EXPECT_EQ(up, omath::unity_engine::k_abs_up);
|
||||
}
|
||||
|
||||
TEST(UnitTestUnityEngine, ProjectTargetMovedFromCamera)
|
||||
TEST(unit_test_unity_engine, ProjectTargetMovedFromCamera)
|
||||
{
|
||||
constexpr auto fov = omath::projection::FieldOfView::FromDegrees(60.f);
|
||||
constexpr auto fov = omath::projection::FieldOfView::from_degrees(60.f);
|
||||
const auto cam = omath::unity_engine::Camera({0, 0, 0}, {}, {1280.f, 720.f}, fov, 0.01f, 1000.f);
|
||||
|
||||
|
||||
for (float distance = 0.02f; distance < 100.f; distance += 0.01f)
|
||||
{
|
||||
const auto projected = cam.WorldToScreen({0, 0, distance});
|
||||
const auto projected = cam.world_to_screen({0, 0, distance});
|
||||
|
||||
EXPECT_TRUE(projected.has_value());
|
||||
|
||||
@@ -82,32 +82,32 @@ TEST(UnitTestUnityEngine, ProjectTargetMovedFromCamera)
|
||||
EXPECT_NEAR(projected->y, 360, 0.00001f);
|
||||
}
|
||||
}
|
||||
TEST(UnitTestUnityEngine, Project)
|
||||
TEST(unit_test_unity_engine, Project)
|
||||
{
|
||||
constexpr auto fov = omath::projection::FieldOfView::FromDegrees(60.f);
|
||||
constexpr auto fov = omath::projection::FieldOfView::from_degrees(60.f);
|
||||
|
||||
const auto cam = omath::unity_engine::Camera({0, 0, 0}, {}, {1280.f, 720.f}, fov, 0.03f, 1000.f);
|
||||
const auto proj = cam.WorldToScreen({5.f, 3, 10.f});
|
||||
const auto proj = cam.world_to_screen({5.f, 3, 10.f});
|
||||
std::println("{} {}", proj->x, proj->y);
|
||||
}
|
||||
|
||||
TEST(UnitTestUnityEngine, CameraSetAndGetFov)
|
||||
TEST(unit_test_unity_engine, CameraSetAndGetFov)
|
||||
{
|
||||
constexpr auto fov = omath::projection::FieldOfView::FromDegrees(90.f);
|
||||
constexpr auto fov = omath::projection::FieldOfView::from_degrees(90.f);
|
||||
auto cam = omath::unity_engine::Camera({0, 0, 0}, {}, {1920.f, 1080.f}, fov, 0.01f, 1000.f);
|
||||
|
||||
EXPECT_EQ(cam.GetFieldOfView().AsDegrees(), 90.f);
|
||||
cam.SetFieldOfView(omath::projection::FieldOfView::FromDegrees(50.f));
|
||||
EXPECT_EQ(cam.get_field_of_view().as_degrees(), 90.f);
|
||||
cam.set_field_of_view(omath::projection::FieldOfView::from_degrees(50.f));
|
||||
|
||||
EXPECT_EQ(cam.GetFieldOfView().AsDegrees(), 50.f);
|
||||
EXPECT_EQ(cam.get_field_of_view().as_degrees(), 50.f);
|
||||
}
|
||||
|
||||
TEST(UnitTestUnityEngine, CameraSetAndGetOrigin)
|
||||
TEST(unit_test_unity_engine, CameraSetAndGetOrigin)
|
||||
{
|
||||
auto cam = omath::unity_engine::Camera({0, 0, 0}, {}, {1920.f, 1080.f}, {}, 0.01f, 1000.f);
|
||||
|
||||
EXPECT_EQ(cam.GetOrigin(), omath::Vector3<float>{});
|
||||
cam.SetFieldOfView(omath::projection::FieldOfView::FromDegrees(50.f));
|
||||
EXPECT_EQ(cam.get_origin(), omath::Vector3<float>{});
|
||||
cam.set_field_of_view(omath::projection::FieldOfView::from_degrees(50.f));
|
||||
|
||||
EXPECT_EQ(cam.GetFieldOfView().AsDegrees(), 50.f);
|
||||
EXPECT_EQ(cam.get_field_of_view().as_degrees(), 50.f);
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user