Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelpmish committed Dec 10, 2024
0 parents commit cb42965
Show file tree
Hide file tree
Showing 207 changed files with 56,234 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
cmake-build-*
build/*
14 changes: 14 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.21)
set(CMAKE_CXX_STANDARD 17)
project(metal_cpp_try)

set(CMAKE_CXX_STANDARD 20)

option(METAL_CPP_BUILD_EXAMPLES "Build examples" ON)

add_subdirectory(metal-cmake) # Library definition

if(METAL_CPP_BUILD_EXAMPLES)
add_subdirectory(examples) # Add targets
endif(METAL_CPP_BUILD_EXAMPLES)

Binary file added data/ball_bdr_vertices.bin
Binary file not shown.
Binary file added data/ball_connectivity.bin
Binary file not shown.
Binary file added data/ball_coords.bin
Binary file not shown.
16 changes: 16 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Get all project dir
FILE(GLOB examples ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)

# For each source file, make an executable
FOREACH(example ${examples})

# Get project name and all sources
get_filename_component(example-name ${example} NAME_WE)

# Create executable and link target
add_executable(${example-name} ${example})
target_link_libraries(${example-name} METAL_CPP_COMPUTE)

message(STATUS "adding target: ${example}")

ENDFOREACH()
41 changes: 41 additions & 0 deletions examples/common/binary_io.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once

#include <vector>
#include <string>
#include <fstream>
#include <iostream>

template <typename T>
std::vector<T> read_binary(std::string filename) {
std::vector<T> buffer;
std::ifstream infile(filename, std::ios::binary);

if (infile) {
// std::cout << "file found: " << filename << std::endl;
infile.seekg(0, std::ios::end);
std::streampos filesize = infile.tellg();
infile.seekg(0, std::ios::beg);

buffer = std::vector<T>(filesize / sizeof(T));
infile.read((char*)&buffer[0], filesize);
} else {
std::cout << "file not found: " << filename << std::endl;
}

infile.close();

return buffer;
}

template <typename T>
void write_binary(const std::vector<T>& buffer, std::string filename) {
std::ofstream outfile(filename, std::ios::binary);

if (outfile) {
outfile.write((char*)&buffer[0], sizeof(T) * buffer.size());
} else {
std::cout << "file not found: " << filename << std::endl;
}

outfile.close();
}
62 changes: 62 additions & 0 deletions examples/common/math.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#pragma once

#include <array>
#include <iostream>

using float3 = std::array<float, 3>;
using float4x3 = std::array<float3, 4>;
using uint4 = std::array<uint32_t, 4>;

float3 operator-(const float3 & x, const float3 & y) {
return {x[0] - y[0], x[1] - y[1], x[2] - y[2]};
}

float4x3 operator-(const float4x3 & x, const float4x3 & y) {
return {x[0] - y[0], x[1] - y[1], x[2] - y[2], x[3] - y[3]};
}

float3 operator+(const float3 & x, const float3 & y) {
return {x[0] + y[0], x[1] + y[1], x[2] + y[2]};
}

float4x3 operator+(const float4x3 & x, const float4x3 & y) {
return {x[0] + y[0], x[1] + y[1], x[2] + y[2], x[3] + y[3]};
}

void operator+=(float3 & x, const float3 & y) {
x[0] += y[0];
x[1] += y[1];
x[2] += y[2];
}

float3 operator*(const float & scale, const float3 & y) {
return {scale * y[0], scale * y[1], scale * y[2]};
}

float4x3 operator*(const float & scale, const float4x3 & A) {
return {scale * A[0], scale * A[1], scale * A[2], scale * A[3]};
}

float3 operator/(const float3 & x, const float & scale) {
return {x[0] / scale, x[1] / scale, x[2] / scale};
}

float4x3 operator/(const float4x3 & A, const float & scale) {
return {A[0] / scale, A[1] / scale, A[2] / scale, A[3] / scale};
}

float dot(const float3 & x, const float3 & y) {
return x[0] * y[0] + x[1] * y[1] + x[2] * y[2];
}

float det(const float3 & A0, const float3 & A1, const float3 & A2) {
return A0[0] * A1[1] * A2[2] + A0[1] * A1[2] * A2[0] +
A0[2] * A1[0] * A2[1] - A0[0] * A1[2] * A2[1] -
A0[1] * A1[0] * A2[2] - A0[2] * A1[1] * A2[0];
}


std::ostream& operator<<(std::ostream & out, const float3 & x) {
out << "{" << x[0] << " " << x[1] << " " << x[2] << "}";
return out;
}
Loading

0 comments on commit cb42965

Please sign in to comment.