From 04b3fcb479f5aaae06d18b315a8bdc8c298f4eae Mon Sep 17 00:00:00 2001 From: admin Date: Tue, 5 Aug 2025 11:28:41 +0200 Subject: removed clustering --- include/glamac/core/glamac_errors.h | 38 +++++++ include/glamac/core/glamacdef.h | 72 +++++++++++++ include/glamac/core/security.h | 41 ++++++++ include/glamac/data/glass_data.h | 56 ++++++++++ include/glamac/graphics/glamac_render.h | 50 +++++++++ include/glamac/graphics/glamac_view.h | 109 ++++++++++++++++++++ include/glamac/input/glamac_events.h | 27 +++++ include/glamac_errors.h | 41 -------- include/glamac_events.h | 27 ----- include/glamac_render.h | 50 --------- include/glamac_view.h | 175 -------------------------------- include/glamacdef.h | 43 -------- include/glass_data.h | 56 ---------- include/glautils/fgla.h | 113 +++++++++++++++++++++ 14 files changed, 506 insertions(+), 392 deletions(-) create mode 100644 include/glamac/core/glamac_errors.h create mode 100644 include/glamac/core/glamacdef.h create mode 100644 include/glamac/core/security.h create mode 100644 include/glamac/data/glass_data.h create mode 100644 include/glamac/graphics/glamac_render.h create mode 100644 include/glamac/graphics/glamac_view.h create mode 100644 include/glamac/input/glamac_events.h delete mode 100644 include/glamac_errors.h delete mode 100644 include/glamac_events.h delete mode 100644 include/glamac_render.h delete mode 100644 include/glamac_view.h delete mode 100644 include/glamacdef.h delete mode 100644 include/glass_data.h create mode 100644 include/glautils/fgla.h (limited to 'include') diff --git a/include/glamac/core/glamac_errors.h b/include/glamac/core/glamac_errors.h new file mode 100644 index 0000000..2065600 --- /dev/null +++ b/include/glamac/core/glamac_errors.h @@ -0,0 +1,38 @@ +/** + * glamac_errors.h - Unified error handling for GlaMaC + * + * Copyright (C) 2025 https://optics-design.com + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * See the COPYING file for the full license text. + */ +#ifndef GLAMAC_ERRORS_H +#define GLAMAC_ERRORS_H + +#include "glamacdef.h" +#include "security.h" + +// Error codes for GlaMaC operations +typedef enum { + GLAMAC_SUCCESS = 0, + GLAMAC_ERROR_MEMORY = -1, + GLAMAC_ERROR_FILE_NOT_FOUND = -2, + GLAMAC_ERROR_FILE_TOO_LARGE = -3, + GLAMAC_ERROR_INVALID_JSON = -4, + GLAMAC_ERROR_INVALID_PATH = -5, + GLAMAC_ERROR_BUFFER_OVERFLOW = -6, + GLAMAC_ERROR_MANUFACTURER_NOT_FOUND = -7, + GLAMAC_ERROR_NO_GLASSES_FOUND = -8, + GLAMAC_ERROR_INVALID_ARGUMENT = -9, + GLAMAC_ERROR_INVALID_GLASS_DATA = -10, + GLAMAC_ERROR_INVALID_MANUFACTURER = -11 +} GlamacResult; + +// Convert error code to human-readable string +const char* glamac_error_string(GlamacResult error); + +#endif /* GLAMAC_ERRORS_H */ \ No newline at end of file diff --git a/include/glamac/core/glamacdef.h b/include/glamac/core/glamacdef.h new file mode 100644 index 0000000..46cbf82 --- /dev/null +++ b/include/glamac/core/glamacdef.h @@ -0,0 +1,72 @@ +/** + * glamacdef.h - header file containing various definitions for the GlaMaC. + * + * Copyright (C) 2025 https://optics-design.com + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * See the COPYING file for the full license text. + */ +#ifndef OPDECDEF_H +#define OPDECDEF_H + +#include +#include +#include +#include +/* Type definitions for consistent sizing across platforms. Idea taken from https://nullprogram.com/blog/2023/10/08/ (archive link: ) */ +typedef uint8_t u8; +typedef char16_t c16; +typedef int32_t b32; +typedef int32_t i32; +typedef uint32_t u32; +typedef uint64_t u64; +typedef float f32; +typedef double f64; +typedef uintptr_t uptr; +#ifndef _WIN32 +typedef char byte; +#else +// On Windows, use unsigned char to match Windows headers +typedef unsigned char byte; +#endif +typedef ptrdiff_t size; +typedef size_t usize; +/* Utility macros */ +#define countof(a) (size)(sizeof(a) / sizeof(*(a))) +#define lengthof(s) (countof(s) - 1) + +/* Safe allocation macros with overflow protection */ +static inline void* safe_malloc(size_t element_size, size_t count) { + // Check for integer overflow in multiplication + if (count == 0 || element_size == 0) { + return NULL; + } + if (count > SIZE_MAX / element_size) { + return NULL; // Would overflow + } + return malloc(element_size * count); +} + +static inline void* safe_calloc(size_t element_size, size_t count) { + // Check for integer overflow - calloc handles this internally but be explicit + if (count == 0 || element_size == 0) { + return NULL; + } + if (count > SIZE_MAX / element_size) { + return NULL; // Would overflow + } + return calloc(count, element_size); +} + +// Safe allocation macros - use these instead of malloc directly +#define new(t, n) ((t*)safe_malloc(sizeof(t), (size_t)(n))) +#define new_zero(t, n) ((t*)safe_calloc(sizeof(t), (size_t)(n))) +#define new_arr(t, arr) ((t*)safe_malloc(sizeof(arr), 1)) + +// Legacy unsafe macro - deprecated, use new() or new_zero() instead +// #define old_new(t, n) (t *)malloc(n*sizeof(t)) // DEPRECATED +#endif /* OPDECDEF_H */ diff --git a/include/glamac/core/security.h b/include/glamac/core/security.h new file mode 100644 index 0000000..6b8c811 --- /dev/null +++ b/include/glamac/core/security.h @@ -0,0 +1,41 @@ +/** + * security.h - Centralized security constants and limits for GlaMaC + * + * Copyright (C) 2025 https://optics-design.com + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * See the COPYING file for the full license text. + */ +#ifndef GLAMAC_SECURITY_H +#define GLAMAC_SECURITY_H + +// File and data size limits +#define MAX_JSON_FILE_SIZE (10 * 1024 * 1024) // 10MB limit for JSON file size +#define MAX_JSON_STRING_LEN 1024 // Max string field length in JSON +#define MAX_PATH_LEN 4096 // Max file path length +#define MAX_MANUFACTURER_NAME_LEN 64 // Max manufacturer name length + +// Glass search utility limits +#define FGLA_MAX_SEARCH_TERM_LEN 256 // Max search term length +#define FGLA_MAX_GLASS_NAME_LEN 256 // Max glass name length +#define FGLA_MAX_CATALOG_COUNT 10 // Max number of catalogs to search +#define FGLA_MAX_NORMALIZED_LEN 512 // Max normalized string length +#define FGLA_GLASS_CODE_LEN 6 // Fixed glass code length + +// Buffer limits for input validation +#define MAX_INPUT_BUFFER_SIZE 8192 // General input buffer limit +#define MAX_COMMAND_LINE_ARGS 32 // Max command line arguments + +// Memory allocation limits +#define MAX_GLASS_ENTRIES 100000 // Max glass entries in database +#define MAX_TEXTURE_CACHE_SIZE 1000 // Max cached textures for rendering + +// Security validation constants +#define MIN_GLASS_CODE_DIGITS 6 // Minimum digits in glass code +#define MAX_GLASS_CODE_DIGITS 6 // Maximum digits in glass code + +#endif /* GLAMAC_SECURITY_H */ \ No newline at end of file diff --git a/include/glamac/data/glass_data.h b/include/glamac/data/glass_data.h new file mode 100644 index 0000000..00f30c0 --- /dev/null +++ b/include/glamac/data/glass_data.h @@ -0,0 +1,56 @@ +/** + * glass_data.h - header file from glass_data.c. + * + * Copyright (C) 2025 https://optics-design.com + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * See the COPYING file for the full license text. + */ +#ifndef GLASS_DATA_H +#define GLASS_DATA_H + +#include "../core/glamacdef.h" // For type definitions +#include "../core/glamac_errors.h" // For error handling + +// Structure to represent an optical glass +typedef struct { + byte name[50]; + f32 abbeNumber; // X-axis (vd) + f32 refractiveIndex; // Y-axis (nd) + byte glass_code[20]; // Glass code from manufacturer + byte manufacturer[20]; // Manufacturer name +} Glass; + +// Get number of glasses in the catalog +u32 get_glass_count(void); + +// Get glass at index +const Glass* get_glass(u32 index); + +// Get glass name +const byte* get_glass_name(u32 index); + +// Find data range in glass catalog +void find_glass_data_range(f32 *minAbbe, f32 *maxAbbe, f32 *minRI, f32 *maxRI); + +// Initialize glass data - call at program start +void initialize_glass_data(void); + +// Load glasses from JSON file +b32 load_glasses_from_json(const byte* json_path, const byte* manufacturer_filter); + +// Cleanup glass data resources +void cleanup_glass_data(void); + +// Catalog management +u32 get_catalog_count(void); +const char* get_catalog_name(u32 catalog_index); +const char* get_current_catalog_name(void); +void set_current_catalog(u32 catalog_index); +void cycle_catalog(i32 direction); // +1 for next, -1 for previous + +#endif /* GLASS_DATA_H */ diff --git a/include/glamac/graphics/glamac_render.h b/include/glamac/graphics/glamac_render.h new file mode 100644 index 0000000..4c20751 --- /dev/null +++ b/include/glamac/graphics/glamac_render.h @@ -0,0 +1,50 @@ +/** + * glamac_render.h - header file from glamac_render.c. + */ +#ifndef GLAMAC_RENDER_H +#define GLAMAC_RENDER_H + +#include +#include +#include "../core/glamacdef.h" +#include "glamac_view.h" + +// Drawing primitives +void draw_text(SDL_Renderer *renderer, TTF_Font *font, const char *text, i32 x, i32 y, SDL_Color color); +void draw_filled_circle(SDL_Renderer *renderer, i32 centerX, i32 centerY, i32 radius); + +// UI element rendering +void draw_axes(SDL_Renderer *renderer, TTF_Font *font, TTF_Font *titleFont, const ViewState* view); +void draw_grid(SDL_Renderer *renderer, const ViewState* view); +void draw_glass_points(SDL_Renderer *renderer, TTF_Font *labelFont, const ViewState* view); +void draw_glass_properties(SDL_Renderer *renderer, TTF_Font *font, TTF_Font *titleFont, const ViewState* view); +void draw_help_window(SDL_Renderer *renderer, TTF_Font *font, TTF_Font *titleFont, const ViewState* view); + +// Main render function +void render(SDL_Renderer *renderer, TTF_Font *font, TTF_Font *titleFont, TTF_Font *labelFont, ViewState* view); + +// Font management +typedef struct { + TTF_Font *regular; + TTF_Font *title; + TTF_Font *label; +} FontSet; + +// Load all required fonts +b32 load_fonts(FontSet *fonts); + +// Load fonts with DPI-aware sizing +b32 load_adaptive_fonts(FontSet *fonts, i32 windowWidth, i32 windowHeight, f32 dpi); + +// Free all fonts +void free_fonts(FontSet *fonts); +void clear_text_cache(void); + +// Simple glass property display helper +void calculate_smart_window_position(i32 glassX, i32 glassY, i32 windowWidth, i32 windowHeight, + const ViewState* view, i32* windowX, i32* windowY); + +// Debug mode check +b32 is_debug_mode(void); + +#endif /* GLAMAC_RENDER_H */ diff --git a/include/glamac/graphics/glamac_view.h b/include/glamac/graphics/glamac_view.h new file mode 100644 index 0000000..58de2b1 --- /dev/null +++ b/include/glamac/graphics/glamac_view.h @@ -0,0 +1,109 @@ +/** + * glamac_view.h - header file from glamac_view.c. + */ +#ifndef GLAMAC_VIEW_H +#define GLAMAC_VIEW_H + +#include +#include "../core/glamacdef.h" + +// Constants for view +#define MIN_PADDING 20 // Minimum padding in pixels +#define MAX_PADDING_PERCENT 0.04f // Maximum padding as percentage of window size +#define PAN_STEP 0.05f // Step size for keyboard panning +#define ZOOM_FACTOR 1.2f // Zoom factor for zoom operations +#define MIN_ZOOM 0.5f // Minimum zoom level +#define MAX_ZOOM 10.0f // Maximum zoom level + +// Glass label positioning constants (adjustable parameters) +#define LABEL_OFFSET_X 12 // Horizontal offset from glass point (pixels) +#define LABEL_OFFSET_Y -8 // Vertical offset from glass point (pixels) +#define LABEL_SIZE_SCALE 1.4f // Scale factor for glass name labels + + + +// State for zooming and panning +typedef struct { + f32 zoomLevel; + f32 offsetX; + f32 offsetY; + i32 windowWidth; + i32 windowHeight; + f32 minAbbe; + f32 maxAbbe; + f32 minRI; + f32 maxRI; + b32 showHelp; // Flag to show/hide help window + b32 gKeyPressed; // Flag to track if 'g' was pressed + u32 gKeyTime; // Time when 'g' was pressed for sequence timing + i32 selectedGlass; // Index of selected glass (-1 if none) +} ViewState; + +// Initialize a view state with default values +void init_view_state(ViewState* view, i32 windowWidth, i32 windowHeight); + +// Refresh view state data range when catalog changes +void refresh_view_data_range(ViewState* view); + +// Helper function to calculate adaptive padding +static inline i32 get_adaptive_padding(const ViewState* view) { + i32 padding = (i32)(view->windowWidth * MAX_PADDING_PERCENT); + return padding > MIN_PADDING ? padding : MIN_PADDING; +} + +// Convert glass data to screen coordinates with zoom and offset +static inline void data_to_screen_coords(f32 abbeNumber, f32 refractiveIndex, + const ViewState* view, i32 *x, i32 *y) { + const i32 padding = get_adaptive_padding(view); + + // Apply zoom and offset transformation + // FLIPPED: Use 1.0f - normalized to flip the Abbe number axis + f32 normalizedX = 1.0f - (abbeNumber - view->minAbbe) / (view->maxAbbe - view->minAbbe); + f32 normalizedY = (refractiveIndex - view->minRI) / (view->maxRI - view->minRI); + + // Transform with zoom and offset + normalizedX = (normalizedX - 0.5f) * view->zoomLevel + 0.5f + view->offsetX; + normalizedY = (normalizedY - 0.5f) * view->zoomLevel + 0.5f + view->offsetY; + + // Convert to screen coordinates + *x = padding + (i32)(normalizedX * (view->windowWidth - 2 * padding)); + *y = view->windowHeight - padding - (i32)(normalizedY * (view->windowHeight - 2 * padding)); +} + +// Convert screen coordinates to data values +static inline void screen_to_data_coords(i32 x, i32 y, const ViewState* view, + f32 *abbeNumber, f32 *refractiveIndex) { + const i32 padding = get_adaptive_padding(view); + + // Convert to normalized coordinates + f32 normalizedX = (f32)(x - padding) / (view->windowWidth - 2 * padding); + f32 normalizedY = (f32)(view->windowHeight - y - padding) / (view->windowHeight - 2 * padding); + + // Reverse transform with zoom and offset + normalizedX = (normalizedX - view->offsetX - 0.5f) / view->zoomLevel + 0.5f; + normalizedY = (normalizedY - view->offsetY - 0.5f) / view->zoomLevel + 0.5f; + + // Convert to data values - FLIPPED axis logic for Abbe + *abbeNumber = view->maxAbbe - normalizedX * (view->maxAbbe - view->minAbbe); + *refractiveIndex = view->minRI + normalizedY * (view->maxRI - view->minRI); +} + +// Find the nearest glass to a given screen position +i32 find_nearest_glass(i32 x, i32 y, const ViewState* view, f32 maxDistance); + +// Calculate visible data range based on current view +void get_visible_data_range(const ViewState* view, f32 *visibleMinAbbe, f32 *visibleMaxAbbe, + f32 *visibleMinRI, f32 *visibleMaxRI); + +// Handle mouse wheel zoom +void handle_mouse_wheel_zoom(i32 wheelY, i32 mouseX, i32 mouseY, ViewState* view); + +// Toggle fullscreen +void toggle_fullscreen(SDL_Window* window); + +// Reset view to default +void reset_view(ViewState* view); + + + +#endif /* GLAMAC_VIEW_H */ diff --git a/include/glamac/input/glamac_events.h b/include/glamac/input/glamac_events.h new file mode 100644 index 0000000..f06a703 --- /dev/null +++ b/include/glamac/input/glamac_events.h @@ -0,0 +1,27 @@ +/** + * glamac_events.h - header file from glamac_events.c. + */ +#ifndef GLAMAC_EVENTS_H +#define GLAMAC_EVENTS_H + +#include +#include "../core/glamacdef.h" +#include "../graphics/glamac_view.h" + +// Process key event +b32 process_key_event(SDL_KeyboardEvent *key, ViewState *view, SDL_Window *window, b32 *quit); + +// Process mouse button event +b32 process_mouse_button(SDL_MouseButtonEvent *button, ViewState *view, i32 *lastMouseX, i32 *lastMouseY, b32 *dragging); + +// Process mouse motion event +b32 process_mouse_motion(SDL_MouseMotionEvent *motion, ViewState *view, i32 *lastMouseX, i32 *lastMouseY, b32 dragging); + +// Process window event +b32 process_window_event(SDL_WindowEvent *window_event, ViewState *view); + +// Process all events on queue +b32 process_events(SDL_Event *event, ViewState *view, SDL_Window *window, + i32 *lastMouseX, i32 *lastMouseY, b32 *dragging, b32 *quit); + +#endif /* GLAMAC_EVENTS_H */ diff --git a/include/glamac_errors.h b/include/glamac_errors.h deleted file mode 100644 index 4e7b9d0..0000000 --- a/include/glamac_errors.h +++ /dev/null @@ -1,41 +0,0 @@ -/** - * glamac_errors.h - Unified error handling for GlaMaC - * - * Copyright (C) 2025 https://optics-design.com - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * See the COPYING file for the full license text. - */ -#ifndef GLAMAC_ERRORS_H -#define GLAMAC_ERRORS_H - -#include "glamacdef.h" - -// Error codes for GlaMaC operations -typedef enum { - GLAMAC_SUCCESS = 0, - GLAMAC_ERROR_MEMORY = -1, - GLAMAC_ERROR_FILE_NOT_FOUND = -2, - GLAMAC_ERROR_FILE_TOO_LARGE = -3, - GLAMAC_ERROR_INVALID_JSON = -4, - GLAMAC_ERROR_INVALID_PATH = -5, - GLAMAC_ERROR_BUFFER_OVERFLOW = -6, - GLAMAC_ERROR_MANUFACTURER_NOT_FOUND = -7, - GLAMAC_ERROR_NO_GLASSES_FOUND = -8, - GLAMAC_ERROR_INVALID_ARGUMENT = -9 -} GlamacResult; - -// Convert error code to human-readable string -const char* glamac_error_string(GlamacResult error); - -// Security limits -#define MAX_JSON_FILE_SIZE (10 * 1024 * 1024) // 10MB limit -#define MAX_JSON_STRING_LEN 1024 // Max string field length -#define MAX_PATH_LEN 4096 // Max file path length -#define MAX_MANUFACTURER_NAME_LEN 64 // Max manufacturer name length - -#endif /* GLAMAC_ERRORS_H */ \ No newline at end of file diff --git a/include/glamac_events.h b/include/glamac_events.h deleted file mode 100644 index c4145c0..0000000 --- a/include/glamac_events.h +++ /dev/null @@ -1,27 +0,0 @@ -/** - * glamac_events.h - header file from glamac_events.c. - */ -#ifndef GLAMAC_EVENTS_H -#define GLAMAC_EVENTS_H - -#include -#include "glamacdef.h" -#include "glamac_view.h" - -// Process key event -b32 process_key_event(SDL_KeyboardEvent *key, ViewState *view, SDL_Window *window, b32 *quit); - -// Process mouse button event -b32 process_mouse_button(SDL_MouseButtonEvent *button, ViewState *view, i32 *lastMouseX, i32 *lastMouseY, b32 *dragging); - -// Process mouse motion event -b32 process_mouse_motion(SDL_MouseMotionEvent *motion, ViewState *view, i32 *lastMouseX, i32 *lastMouseY, b32 dragging); - -// Process window event -b32 process_window_event(SDL_WindowEvent *window_event, ViewState *view); - -// Process all events on queue -b32 process_events(SDL_Event *event, ViewState *view, SDL_Window *window, - i32 *lastMouseX, i32 *lastMouseY, b32 *dragging, b32 *quit); - -#endif /* GLAMAC_EVENTS_H */ diff --git a/include/glamac_render.h b/include/glamac_render.h deleted file mode 100644 index 899702d..0000000 --- a/include/glamac_render.h +++ /dev/null @@ -1,50 +0,0 @@ -/** - * glamac_render.h - header file from glamac_render.c. - */ -#ifndef GLAMAC_RENDER_H -#define GLAMAC_RENDER_H - -#include -#include -#include "glamacdef.h" -#include "glamac_view.h" - -// Drawing primitives -void draw_text(SDL_Renderer *renderer, TTF_Font *font, const char *text, i32 x, i32 y, SDL_Color color); -void draw_filled_circle(SDL_Renderer *renderer, i32 centerX, i32 centerY, i32 radius); - -// UI element rendering -void draw_axes(SDL_Renderer *renderer, TTF_Font *font, TTF_Font *titleFont, const ViewState* view); -void draw_grid(SDL_Renderer *renderer, const ViewState* view); -void draw_glass_points(SDL_Renderer *renderer, TTF_Font *labelFont, const ViewState* view); -void draw_glass_properties(SDL_Renderer *renderer, TTF_Font *font, TTF_Font *titleFont, const ViewState* view); -void draw_help_window(SDL_Renderer *renderer, TTF_Font *font, TTF_Font *titleFont, const ViewState* view); - -// Main render function -void render(SDL_Renderer *renderer, TTF_Font *font, TTF_Font *titleFont, TTF_Font *labelFont, ViewState* view); - -// Font management -typedef struct { - TTF_Font *regular; - TTF_Font *title; - TTF_Font *label; -} FontSet; - -// Load all required fonts -b32 load_fonts(FontSet *fonts); - -// Load fonts with DPI-aware sizing -b32 load_adaptive_fonts(FontSet *fonts, i32 windowWidth, i32 windowHeight, f32 dpi); - -// Free all fonts -void free_fonts(FontSet *fonts); -void clear_text_cache(void); - -// Simple glass property display helper -void calculate_smart_window_position(i32 glassX, i32 glassY, i32 windowWidth, i32 windowHeight, - const ViewState* view, i32* windowX, i32* windowY); - -// Debug mode check -b32 is_debug_mode(void); - -#endif /* GLAMAC_RENDER_H */ diff --git a/include/glamac_view.h b/include/glamac_view.h deleted file mode 100644 index 6ca4b9b..0000000 --- a/include/glamac_view.h +++ /dev/null @@ -1,175 +0,0 @@ -/** - * glamac_view.h - header file from glamac_view.c. - */ -#ifndef GLAMAC_VIEW_H -#define GLAMAC_VIEW_H - -#include -#include "glamacdef.h" - -// Constants for view -#define MIN_PADDING 20 // Minimum padding in pixels -#define MAX_PADDING_PERCENT 0.04f // Maximum padding as percentage of window size -#define PAN_STEP 0.05f // Step size for keyboard panning -#define ZOOM_FACTOR 1.2f // Zoom factor for zoom operations -#define MIN_ZOOM 0.5f // Minimum zoom level -#define MAX_ZOOM 10.0f // Maximum zoom level - -// Glass label positioning constants (adjustable parameters) -#define LABEL_OFFSET_X 12 // Horizontal offset from glass point (pixels) -#define LABEL_OFFSET_Y -8 // Vertical offset from glass point (pixels) -#define LABEL_SIZE_SCALE 1.4f // Scale factor for glass name labels - -// Tight clustering parameters -#define MAX_CLUSTER_SIZE 8 // Maximum glasses per cluster -#define DEFAULT_TIGHT_CLUSTER_ND_THRESHOLD 0.0005f // Default nd difference threshold -#define DEFAULT_TIGHT_CLUSTER_VD_THRESHOLD 0.15f // Default vd difference threshold - -// Loose clustering parameters (zoom-dependent) -#define DEFAULT_LOOSE_CLUSTER_ND_THRESHOLD 0.3f // Default nd base threshold -#define DEFAULT_LOOSE_CLUSTER_VD_THRESHOLD 0.55f // Default vd base threshold -#define DEFAULT_LOOSE_CLUSTER_ND_FRACTION 4.9f // Default nd fraction of visible range -#define DEFAULT_LOOSE_CLUSTER_VD_FRACTION 0.9f // Default vd fraction of visible range - -// Global tight cluster thresholds (adjustable parameters) -extern f32 g_tight_cluster_nd_threshold; -extern f32 g_tight_cluster_vd_threshold; - -// Global loose cluster parameters (adjustable parameters) -extern f32 g_loose_cluster_nd_threshold; // Base threshold -extern f32 g_loose_cluster_vd_threshold; // Base threshold -extern f32 g_loose_cluster_nd_fraction; // Zoom scaling fraction -extern f32 g_loose_cluster_vd_fraction; // Zoom scaling fraction - -// Tight cluster structure -typedef struct { - i32 glassIndices[MAX_CLUSTER_SIZE]; // Indices of glasses in this cluster - i32 count; // Number of glasses in cluster - i32 representativeIndex; // Index of glass with shortest name - f32 avgAbbeNumber; // Average position for reference - f32 avgRefractiveIndex; -} TightCluster; - -// Loose cluster structure (zoom-dependent) -typedef struct { - i32 glassIndices[MAX_CLUSTER_SIZE]; // Indices of glasses in this cluster - i32 count; // Number of glasses in cluster - i32 representativeIndex; // Index of glass with shortest name - f32 avgAbbeNumber; // Average position for reference - f32 avgRefractiveIndex; -} LooseCluster; - - -// State for zooming and panning -typedef struct { - f32 zoomLevel; - f32 offsetX; - f32 offsetY; - i32 windowWidth; - i32 windowHeight; - f32 minAbbe; - f32 maxAbbe; - f32 minRI; - f32 maxRI; - b32 showHelp; // Flag to show/hide help window - b32 gKeyPressed; // Flag to track if 'g' was pressed - u32 gKeyTime; // Time when 'g' was pressed for sequence timing - i32 selectedGlass; // Index of selected glass (-1 if none) - i32 selectedCluster; // Index of selected tight cluster (-1 if none) - - // Tight cluster data (per catalog) - TightCluster* tightClusters; // Array of tight clusters for current catalog - i32 tightClusterCount; // Number of tight clusters - - // Loose cluster data (per catalog, zoom-dependent) - LooseCluster* looseClusters; // Array of loose clusters for current catalog - i32 looseClusterCount; // Number of loose clusters - - // Label recalculation tracking - f32 lastZoomLevel; // Last zoom level for which labels were calculated - i32 lastWindowWidth; // Last window width for labels - i32 lastWindowHeight; // Last window height for labels - f32 lastOffsetX; // Last X offset for labels - f32 lastOffsetY; // Last Y offset for labels -} ViewState; - -// Initialize a view state with default values -void init_view_state(ViewState* view, i32 windowWidth, i32 windowHeight); - -// Refresh view state data range when catalog changes -void refresh_view_data_range(ViewState* view); - -// Helper function to calculate adaptive padding -static inline i32 get_adaptive_padding(const ViewState* view) { - i32 padding = (i32)(view->windowWidth * MAX_PADDING_PERCENT); - return padding > MIN_PADDING ? padding : MIN_PADDING; -} - -// Convert glass data to screen coordinates with zoom and offset -static inline void data_to_screen_coords(f32 abbeNumber, f32 refractiveIndex, - const ViewState* view, i32 *x, i32 *y) { - const i32 padding = get_adaptive_padding(view); - - // Apply zoom and offset transformation - // FLIPPED: Use 1.0f - normalized to flip the Abbe number axis - f32 normalizedX = 1.0f - (abbeNumber - view->minAbbe) / (view->maxAbbe - view->minAbbe); - f32 normalizedY = (refractiveIndex - view->minRI) / (view->maxRI - view->minRI); - - // Transform with zoom and offset - normalizedX = (normalizedX - 0.5f) * view->zoomLevel + 0.5f + view->offsetX; - normalizedY = (normalizedY - 0.5f) * view->zoomLevel + 0.5f + view->offsetY; - - // Convert to screen coordinates - *x = padding + (i32)(normalizedX * (view->windowWidth - 2 * padding)); - *y = view->windowHeight - padding - (i32)(normalizedY * (view->windowHeight - 2 * padding)); -} - -// Convert screen coordinates to data values -static inline void screen_to_data_coords(i32 x, i32 y, const ViewState* view, - f32 *abbeNumber, f32 *refractiveIndex) { - const i32 padding = get_adaptive_padding(view); - - // Convert to normalized coordinates - f32 normalizedX = (f32)(x - padding) / (view->windowWidth - 2 * padding); - f32 normalizedY = (f32)(view->windowHeight - y - padding) / (view->windowHeight - 2 * padding); - - // Reverse transform with zoom and offset - normalizedX = (normalizedX - view->offsetX - 0.5f) / view->zoomLevel + 0.5f; - normalizedY = (normalizedY - view->offsetY - 0.5f) / view->zoomLevel + 0.5f; - - // Convert to data values - FLIPPED axis logic for Abbe - *abbeNumber = view->maxAbbe - normalizedX * (view->maxAbbe - view->minAbbe); - *refractiveIndex = view->minRI + normalizedY * (view->maxRI - view->minRI); -} - -// Find the nearest glass to a given screen position -i32 find_nearest_glass(i32 x, i32 y, const ViewState* view, f32 maxDistance); - -// Calculate visible data range based on current view -void get_visible_data_range(const ViewState* view, f32 *visibleMinAbbe, f32 *visibleMaxAbbe, - f32 *visibleMinRI, f32 *visibleMaxRI); - -// Handle mouse wheel zoom -void handle_mouse_wheel_zoom(i32 wheelY, i32 mouseX, i32 mouseY, ViewState* view); - -// Toggle fullscreen -void toggle_fullscreen(SDL_Window* window); - -// Reset view to default -void reset_view(ViewState* view); - -// Tight clustering functions -void create_tight_clusters(ViewState* view); -void free_tight_clusters(ViewState* view); -i32 find_tight_cluster_for_glass(i32 glassIndex, const ViewState* view); - -// Loose clustering functions (zoom-dependent) -void create_loose_clusters(ViewState* view); -void free_loose_clusters(ViewState* view); -i32 find_loose_cluster_for_glass(i32 glassIndex, const ViewState* view); - -// Combined clustering logic -b32 should_show_glass_label(i32 glassIndex, const ViewState* view); - - -#endif /* GLAMAC_VIEW_H */ diff --git a/include/glamacdef.h b/include/glamacdef.h deleted file mode 100644 index aeba926..0000000 --- a/include/glamacdef.h +++ /dev/null @@ -1,43 +0,0 @@ -/** - * glamacdef.h - header file containing various definitions for the GlaMaC. - * - * Copyright (C) 2025 https://optics-design.com - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * See the COPYING file for the full license text. - */ -#ifndef OPDECDEF_H -#define OPDECDEF_H - -#include -#include -#include -/* Type definitions for consistent sizing across platforms. Idea taken from https://nullprogram.com/blog/2023/10/08/ (archive link: ) */ -typedef uint8_t u8; -typedef char16_t c16; -typedef int32_t b32; -typedef int32_t i32; -typedef uint32_t u32; -typedef uint64_t u64; -typedef float f32; -typedef double f64; -typedef uintptr_t uptr; -#ifndef _WIN32 -typedef char byte; -#else -// On Windows, use unsigned char to match Windows headers -typedef unsigned char byte; -#endif -typedef ptrdiff_t size; -typedef size_t usize; -/* Utility macros */ -#define countof(a) (size)(sizeof(a) / sizeof(*(a))) -#define lengthof(s) (countof(s) - 1) -// #define new(a, t, n) (t *)alloc(a, sizeof(t), _Alignof(t), n) //From Nullprogram, not using currently -#define new(t, n) (t *)malloc(n*sizeof(t)) -#define new_arr(t,arr) (t)malloc(sizeof(arr)) -#endif /* OPDECDEF_H */ diff --git a/include/glass_data.h b/include/glass_data.h deleted file mode 100644 index 56c2c7f..0000000 --- a/include/glass_data.h +++ /dev/null @@ -1,56 +0,0 @@ -/** - * glass_data.h - header file from glass_data.c. - * - * Copyright (C) 2025 https://optics-design.com - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * See the COPYING file for the full license text. - */ -#ifndef GLASS_DATA_H -#define GLASS_DATA_H - -#include "glamacdef.h" // For type definitions -#include "glamac_errors.h" // For error handling - -// Structure to represent an optical glass -typedef struct { - byte name[50]; - f32 abbeNumber; // X-axis (vd) - f32 refractiveIndex; // Y-axis (nd) - byte glass_code[20]; // Glass code from manufacturer - byte manufacturer[20]; // Manufacturer name -} Glass; - -// Get number of glasses in the catalog -u32 get_glass_count(void); - -// Get glass at index -const Glass* get_glass(u32 index); - -// Get glass name -const byte* get_glass_name(u32 index); - -// Find data range in glass catalog -void find_glass_data_range(f32 *minAbbe, f32 *maxAbbe, f32 *minRI, f32 *maxRI); - -// Initialize glass data - call at program start -void initialize_glass_data(void); - -// Load glasses from JSON file -b32 load_glasses_from_json(const byte* json_path, const byte* manufacturer_filter); - -// Cleanup glass data resources -void cleanup_glass_data(void); - -// Catalog management -u32 get_catalog_count(void); -const char* get_catalog_name(u32 catalog_index); -const char* get_current_catalog_name(void); -void set_current_catalog(u32 catalog_index); -void cycle_catalog(i32 direction); // +1 for next, -1 for previous - -#endif /* GLASS_DATA_H */ diff --git a/include/glautils/fgla.h b/include/glautils/fgla.h new file mode 100644 index 0000000..d9a946b --- /dev/null +++ b/include/glautils/fgla.h @@ -0,0 +1,113 @@ +/** + * fgla.h - Find Glass utility header + * + * Copyright (C) 2025 https://optics-design.com + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * See the COPYING file for the full license text. + */ + +#ifndef FGLA_H +#define FGLA_H + +#include "glamac/core/glamacdef.h" +#include "glamac/core/security.h" + +#ifdef __cplusplus +extern "C" { +#endif + +// Output format types +typedef enum { + FGLA_OUTPUT_CSV = 0, + FGLA_OUTPUT_TABLE = 1, + FGLA_OUTPUT_JSON = 2 +} FglaOutputFormat; + +// Error handling +typedef enum { + FGLA_SUCCESS = 0, + FGLA_ERROR_INVALID_ARGS = 1, + FGLA_ERROR_INVALID_SEARCH_TERM = 2, + FGLA_ERROR_NO_DATABASE = 3, + FGLA_ERROR_NO_MATCHES = 4, + FGLA_ERROR_MEMORY = 5 +} FglaResult; + +/** + * @brief Safely converts string to lowercase with length limit + * @param str String to convert (modified in place) + * @param max_len Maximum length to process + */ +void fgla_to_lowercase_safe(char* str, size_t max_len); + +/** + * @brief Safely normalizes string by removing dashes and converting to lowercase + * @param input Input string to normalize + * @param output Buffer for normalized output + * @param output_size Size of output buffer + * @return 0 on success, -1 on error + */ +int fgla_normalize_string_safe(const char* input, char* output, size_t output_size); + +/** + * @brief Safely checks if needle is found in haystack (case-insensitive, dash-insensitive) + * @param haystack String to search in + * @param needle String to search for + * @return 1 if found, 0 if not found or error + */ +int fgla_contains_substring_safe(const char* haystack, const char* needle); + +/** + * @brief Checks if manufacturer matches any of the specified catalogs + * @param manufacturer Manufacturer name to check + * @param catalog_list Array of catalog names to match against + * @param catalog_count Number of catalogs in the list + * @return 1 if matches, 0 if no match + */ +int fgla_matches_catalog(const char* manufacturer, const char* catalog_list[], int catalog_count); + +/** + * @brief Validates search term input for security and format + * @param term Search term to validate + * @return 1 if valid, 0 if invalid + */ +int fgla_validate_search_term(const char* term); + +/** + * @brief Checks if a search term is a glass code pattern + * @param term Search term to check + * @return 1 if glass code pattern, 0 otherwise + */ +int fgla_is_glass_code_pattern(const char* term); + +/** + * @brief Safely checks if a glass code matches a pattern + * @param glass_code Glass code to check + * @param pattern Pattern to match (can contain 'x' wildcards) + * @return 1 if matches, 0 if no match + */ +int fgla_matches_glass_code_pattern_safe(const char* glass_code, const char* pattern); + +/** + * @brief Prints usage information for fgla utility + * @param program_name Name of the program (argv[0]) + */ +void fgla_print_usage(const char* program_name); + +/** + * @brief Prints error message with helpful suggestions + * @param error Error code from FglaResult enum + * @param context Optional context string for the error + */ +void fgla_print_error_with_suggestion(FglaResult error, const char* context); + +#ifdef __cplusplus +} +#endif + +#endif /* FGLA_H */ \ No newline at end of file -- cgit v1.2.3