Skip to content

Instantly share code, notes, and snippets.

View kennyalive's full-sized avatar
🥕

Artem Kharytoniuk kennyalive

🥕
View GitHub Profile
@kennyalive
kennyalive / inverse_of_TRS_matrix.cpp
Last active July 10, 2022 04:16
Inverse of translate+rotate+scale matrix
// This implementation does not support shear transform.
// Not-uniform scale is supported.
//
// If you need to support shear then 'true' matrix inversion algorithm
// shoudl be used, for example, gaussian elimination will work.
//
Matrix3x4 get_inverse_transform(const Matrix3x4& m)
{
Vector3 scale = get_scale_from_transform(m);
Vector3 inv_scale = Vector3(1) / scale;
@kennyalive
kennyalive / intersect_triangle_watertight.cpp
Last active November 6, 2021 17:55
watertight triangle intersection with ray origin adjustment
constexpr bool perform_expensive_numerical_analysis_to_prevent_intersection_behind_ray_origin = true; // could noticeable slowdown algorithm (like 2x)
//
// Sven Woop, Carsten Benthin, and Ingo Wald, Watertight Ray/Triangle Intersection,
// Journal of Computer Graphics Techniques (JCGT), vol. 2, no. 1, 65-82, 2013
// http://jcgt.org/published/0002/01/05/
//
float intersect_triangle_watertight(const Ray& ray, const Vector3& p0, const Vector3& p1, const Vector3& p2, Vector3* barycentrics)
{
const int kz = ray.direction.abs().max_dimension();
@kennyalive
kennyalive / gist:b3271612452fb3f746d47185fdba99eb
Created September 13, 2021 21:03
Do not optimize out benchmark helpers
#pragma optimize("", off)
void dummy_modify_variable(void* p) {}
void dummy_use_variable(const void *p) {}
#pragma optimize("", on)
template <typename T>
void make_unpredictable_for_compiler(T& var) {
dummy_modify_variable(&var);
}
#include <cstdio>
#include <chrono>
struct Timestamp {
Timestamp() : t(std::chrono::steady_clock::now()) {}
std::chrono::time_point<std::chrono::steady_clock> t;
};
double elapsed_milliseconds(Timestamp t) {
auto duration = std::chrono::steady_clock::now() - t.t;
auto nanoseconds = std::chrono::duration_cast<std::chrono::nanoseconds>(duration).count();
#include <cstdio>
#include <cstring>
#include <cassert>
int main(int argc, char** argv) {
if (argc < 2) return -1;
FILE* f = fopen(argv[1], "r+b");
if (!f) return -2;
fseek(f, 0x33, SEEK_SET);
int vertex_count = 0;
@kennyalive
kennyalive / Text_File_Parser.cpp
Created September 21, 2019 22:08
Text file parsing.
struct Text_File_Lines {
std::string text;
std::vector<size_t> line_start_positions; // the last element is a position at the end of the file
};
struct Text_File_Parser {
private:
size_t pos = 0;
int line = -1;
Text_File_Lines* text_file = nullptr;