minor changes. started writting own 3d math library and started implementation for linux/mac

This commit is contained in:
AlphaCapella
2025-08-09 14:20:26 +02:00
parent 295444d0e5
commit 2c301b76cf
19 changed files with 291 additions and 13 deletions

53
braindump.md Normal file
View File

@@ -0,0 +1,53 @@
# Math library render implementation
- [ ] mat4
- [ ] functions mat4
- [ ] constructor (identity?)
- [ ] multiply
- [ ] data acces glm::value_prt equivalent
- [ ] translate
- [ ] rotate
- [ ] perspective
- [ ] scale
- [ ] vlookat (?)
- [ ] quaternion to matrix
- [ ] vec3
- [ ] functions vec3
- [x] constructor
- [x] add
- [x] substract
- [x] multiply scalar
- [x] dot product
- [X] cross product
- [x] length
- [x] normalize
- [ ] mat3
- [ ] vec4
- [ ] quaternions to matrix
- [ ] glm::radians
- Connector und Connected-status indicator
- implement calibration ???
- load .obj as model
- make it run on linux/unix
- implement 2nd sensor
- rigid body them together, somehow???
- have a look at rtos, start the move over
- actually print a fucking model
- and most importantly LETS FUCKING GOOOOOOOOOO
- have a look at CAN and wireless and battery. how to implement those actually? PCBs???
- Custom designed PCBs for sensors, connectors and stuff? what will be the eventual brain later on, store bought stm32?
- ziel für diese woche ist endlich eine funktionierende prototypen version zu haben, die genau das macht was sie später soll
Today:
- **stop relying on claude/ai code. the code is shit and you dont know what youre doing half the time. use it to explain shit**
- ~~Laptops bei Ebay reinstellen (Bilder machen, Beschreibung)~~
- ~~Laptops verpacken
- ~~Unraidserver saubermachen; neue Parity Disk einbauen~~
- ~~Tisch saugen, Müll wegwerfen~~
- RTOS projekt anlegen und code migrieren. Läuft alles noch? (turns out dieser approach ist vielleicht von grundauf scheiße, wenn ich eh eigene mcus and die imus bringe und alles wireless mit nem brain verbinde. mal sehen, gut zum testen i guess wie rtos funktioniert)
- 2nd sensor implementation embedded/pc
- design cad model for testrig (2 sensors, hinge)
- print cad model
- use cad model as obj in visualizer

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -1,5 +1,5 @@
[Window][Debug##Default]
Pos=37,29
Pos=22,29
Size=326,393
[Window][Hello, ImGui!]

View File

@@ -1,3 +1,4 @@
#include <cstddef>
#include <cstdint>
#include <handleapi.h>
#include <thread>
@@ -26,7 +27,7 @@
std::mutex g_mutex;
Quaternion g_currQuat = {1.0f, 0.0f, 0.0f, 0.0f};
std::vector<Quaternion> g_quadHistory;
bool g_running = true;
bool g_running = false;
HANDLE g_serialHandle = INVALID_HANDLE_VALUE;
@@ -113,7 +114,7 @@ void serialParserThread() {
// close and exit the thread
CloseHandle(g_serialHandle);
std::cout << "Serial communicatin thread exiting" << std::endl;
std::cout << "Serial communication thread exiting" << std::endl;
}
@@ -293,7 +294,8 @@ int main() {
// Start serial parsing thread
// pass the address of the code in memory so that thread can start its instance of the function
std::thread parserThread(serialParserThread);
//std::thread parserThread(serialParserThread);
std::thread* parserThread = nullptr;
// set rendering state of window
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
@@ -302,7 +304,7 @@ int main() {
// main rendering loop for the ui
while (g_running && !glfwWindowShouldClose(window)) {
while (!glfwWindowShouldClose(window)) {
// process new events from window evevnt queue
glfwPollEvents();
// clear the color buffer == clear the screen / / GL_DEPTH_BUFFER_BIT would be for 3D rendering
@@ -329,7 +331,7 @@ int main() {
// apply MVP
//model = glm::rotate(model, (float)glfwGetTime() * glm::radians(50.0f), glm::vec3(0.5f, 1.0f, 0.0f));
// this need to be deleted and redone
// this needs to be deleted and redone
glm::quat kekus_q(g_currQuat.w, g_currQuat.x, g_currQuat.y, g_currQuat.z);
@@ -379,6 +381,26 @@ int main() {
ImGui::Text("Hello Daddy, I'm here to serve");
ImGui::Text("w: %.4f x: %.4f y: %.4f z: %.4f", lQuad.w, lQuad.x, lQuad.y, lQuad.z);
ImGui::Text("Connection to MCU: ");
ImGui::BeginDisabled(parserThread != nullptr);
if (ImGui::Button("Connect")) {
g_running = true;
parserThread = new std::thread(serialParserThread);
}
ImGui::EndDisabled();
ImGui::SameLine();
ImGui::BeginDisabled(parserThread == nullptr);
if (ImGui::Button("Disconnect")) {
g_running = false;
parserThread -> join();
delete parserThread;
parserThread = nullptr;
}
ImGui::EndDisabled();
// create scrolling section for last 200 measurements
ImGui::BeginChild("History", ImVec2(0, 300), true, ImGuiWindowFlags_HorizontalScrollbar);
@@ -405,10 +427,10 @@ int main() {
}
// set the parsing thread to exit and wait for it
g_running = false;
if (parserThread.joinable()) {
parserThread.join();
}
// g_running = false;
//if (parserThread.joinable()) {
// parserThread.join();
//}
// clean up ImGui
ImGui_ImplOpenGL3_Shutdown();

96
math3d.cpp Normal file
View File

@@ -0,0 +1,96 @@
#include <math.h>
struct Vec3 {
float x, y, z;
};
Vec3 create_Vec3 (float x, float y, float z) {
Vec3 result = {x, y, z};
return result;
}
Vec3 Vec3_add (const Vec3& a, const Vec3& b) {
Vec3 result = {a.x + b.x, a.y + b.y, a.z + b.z};
return result;
}
Vec3 Vec3_subtract (const Vec3& a, const Vec3& b) {
Vec3 result = {a.x - b.x, a.y - b.y, a.z - b.z};
return result;
}
Vec3 Vec3_scalar (const Vec3& a, const float scalar) {
Vec3 result = {a.x*scalar, a.y*scalar, a.z*scalar};
return result;
}
float Vec3_dot (const Vec3& a, const Vec3& b) {
float result = a.x*b.x + a.y*b.y + a.z*b.z;
return result;
}
Vec3 Vec3_cross (const Vec3& a, const Vec3& b) {
Vec3 result = {a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x};
return result;
}
float Vec3_length (const Vec3& a) {
float result = sqrt(a.x*a.x + a.y*a.y + a.z*a.z);
return result;
}
Vec3 Vec3_normalize (const Vec3& a) {
Vec3 result;
float len = sqrt(a.x*a.x + a.y*a.y + a.z*a.z);
if (len > 0.0f) {
return result = {a.x/len, a.y/len, a.z/len};
} else {
return a;
}
}
struct Mat4{
float m4[16];
};
Mat4 Mat4_createMatrix (float InitialValue) {
Mat4 result;
for (int i = 0; i < 16; i++) {
result.m4[i] = {InitialValue};
}
return result;
}
Mat4 Mat4_createZeroes () {
Mat4 result = {0};
return result;
}
Mat4 Mat4_createIdentity () {
Mat4 result = {0};
result.m4[0] = 1;
result.m4[5] = 1;
result.m4[10] = 1;
result.m4[15] = 1;
return result;
}
Mat4 Mat4_createTranslation (float x, float y, float z) {
Mat4 result = {0};
result.m4[0] = 1;
result.m4[5] = 1;
result.m4[10] = 1;
result.m4[15] = 1;
result.m4[12] = x;
result.m4[13] = y;
result.m4[14] = z;
return result;
}
//translate
//rotate
//scale
//perspective

0
math3d.hpp Normal file
View File

View File

@@ -1,4 +1,13 @@
#include <windows.h>
#ifdef _WIN32
#include <windows.h>
#elif defined(__APPLE__)
#elif defined(__linux__)
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <unistd.h>
#endif
#include <iostream>
#include <string>
#include <regex>
@@ -6,7 +15,8 @@
#include <thread>
#include "serialcomm.hpp"
HANDLE initSerialPort (const char* portName, DWORD baudRate) {
#ifdef _WIN32
HANDLE initSerialPort (const char* portName, DWORD baudRate) {
// open serial port
HANDLE hSerial = CreateFile(portName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
@@ -62,6 +72,54 @@ bool readSerialData (HANDLE hSerial, char* buffer, DWORD bufferSize, DWORD& byte
return true;
}
#elif defined(__APPLE__)
#elif defined(__linux__)
int initSerialPort(){
struct termios tty;
int linux_serialPort = open("/dev/ttyUSB0", 0_RDWR);
// read in existing struct settings and handle errors
if (tcgetattr(linux_serialPort, &tty) !0 0) {
std::cout << "Error %i opening serial port: %s\n", errno, strerror(errno) << std::endl;
return 1;
}
// set the serial settings
tty.c_flag &= ~PARENB;
tty.c_flag &= ~CSTOPB;
tty.c_flag &= ~CSIZE;
tty.c_flag |= CS8;
tty.c_flag &= ~CRTSCTS;
tty.c_flag |= CREAD | CLOCAL;
tty.c_cc[VTIME] = 10; // Wait for up to 1s (10 deciseconds), returning as soon as any data is received.
tty.c_cc[VMIN] = 0;
// Set in/out baud rate to be 9600
cfsetispeed(&tty, B115200);
cfsetospeed(&tty, B115200);
// save the settings and check for errors
if (tcsetattr(linux_serialPort, TCSANOW, &tty) != 0 ) {
std::cout << "Error %i saving serial port settings: %s\n", errno, strerror(errno) << std::endl;
return 1;
}
return linux_serialPort;
}
bool linux_readSerialData (int serialPort, char* buffer) {
if (read(serialPort, &buffer, sizeof(buffer)) < 0 ) {
std::cout << "Error reading serial port data: %s", strerror(errno)) << std::endl;
return 1;
}
}
#endif
bool parseQuaternion(const std::string& data, Quaternion& quat) {
// regex pattern to match the floats in the uart data stream from stm32
std::regex pattern("qw: ([+-]?\\d*\\.?\\d+) qx: ([+-]?\\d*\\.?\\d+) qy: ([+-]?\\d*\\.?\\d+) qz: ([+-]?\\d*\\.?\\d+)");

View File

@@ -7,4 +7,7 @@ struct Quaternion {
HANDLE initSerialPort (const char* portName, DWORD baudRate);
bool readSerialData (HANDLE hSerial, char* buffer, DWORD bufferSize, DWORD& bytesRead);
bool parseQuaternion(const std::string& data, Quaternion& quat);
bool parseQuaternion(const std::string& data, Quaternion& quat);
int linux_initSerialPort();
bool linux_readSerialData(int serialPort, char* buffer);

46
uff.cpp Normal file
View File

@@ -0,0 +1,46 @@
class Vec3 {
public:
float x, y, z;
Vec3(float x, float y, float z) {
this->x = x;
this->y = y;
this->z = z;
}
Vec3 add (Vec3 other) {
Vec3 result (x + other.x, y + other.y, z + other.z);
return result;
}
Vec3 subtract (Vec3 other) {
Vec3 result (x - other.x, y - other.y, z - other.z);
return result;
}
Vec3 scalar (float scalar) {
Vec3 result (x * scalar, y * scalar, z * scalar);
return result;
}
float dot (Vec3 other) {
float result (x * other.x + y * other.y + z * other.z);
return result;
}
Vec3 cross (Vec3 other) {
Vec3 result (y*other.z - z*other.y, z*other.x - x*other.z, x*other.y - y*other.x);
return result;
}
float length () {
float result = sqrt(x*x + y*y + z*z);
return result;
}
Vec3 normalize () {
float len = sqrt(x*x + y*y + z*z);
Vec3 result (x / len, y / len, z / len);
return result;
}
};