forked from ScrollPrize/villa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvesuvius-c.h
5013 lines (4275 loc) · 163 KB
/
vesuvius-c.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef VESUVIUS_H
#define VESUVIUS_H
// Vesuvius-c notes:
// - in order to use Vesuvius-c, define VESUVIUS_IMPL in one .c file and then #include "vesuvius-c.h"
// - when passing pointers to a _new function in order to fill out fields in the struct (e.g. vs_mesh_new)
// the struct will take ownership of the pointer and the pointer shall be cleaned up in the _free function.
// The caller loses ownership of the pointer. This does NOT include char* for strings like paths or URLs
// - e.g. vs_vol_new(char* cache, char* url) does _not_ subsume either pointer. If they are not literals
// then the caller is responsible for cleaning up the char*
// - index order is in Z Y X order
// - a 0 return code indicates success for functions that do NOT return a pointer
// - a non zero return code _can_ indicate failure
// - this is often on a case by case basis, but is quite often the case for functions that take out parameters
// or are otherwise side-effect-ful
// - a NULL pointer indicates failure for functions that return a pointer
// - It is the caller's responsibility to clean up pointers returned by Vesuvius APIs
// - some structures, such as chunk and volume, have custom _free functions which should be called
// which will free any pointers contained within the structure that have been allocated, f.ex. in _new
// AND will also free the pointer itself
// - this applies to both function return values and out parameters passed as pointer pointers
// - for pointers to primitive types the caller should just call free() on the pointer
#include <ctype.h>
#include <limits.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include <json-c/json.h>
#include <blosc2.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <float.h>
// Buffer size for metadata JSON and URL
#define BUFFER_SIZE 4096
#define URL_SIZE 256
#define CACHE_CAPACITY 100 // Define the LRU cache capacity
#define CACHE_DIR ".vesuvius-cache"
// Struct for scroll volume regions
typedef struct {
int x_start;
int x_width;
int y_start;
int y_height;
int z_start;
int z_depth;
} RegionOfInterest;
typedef struct {
unsigned char *data;
size_t size;
} MemoryChunk;
typedef struct LRUNode {
int chunk_x;
int chunk_y;
int chunk_z;
MemoryChunk chunk;
struct LRUNode *prev;
struct LRUNode *next;
} LRUNode;
typedef struct {
LRUNode *head;
LRUNode *tail;
LRUNode *cache[CACHE_CAPACITY];
int count;
} LRUCache;
typedef struct {
float x, y, z;
} Vertex;
typedef struct {
int v1, v2, v3; // Indices of the triangle's vertices
} Triangle;
typedef struct {
Vertex *vertices;
Triangle *triangles;
size_t vertex_count;
size_t triangle_count;
} TriangleMesh;
// Function prototypes
static size_t write_callback(void *ptr, size_t size, size_t nmemb, void *stream);
static int fetch_metadata(const char *url, char *buffer);
static int parse_metadata(const char *buffer);
int load_shape_and_chunksize(void);
void init_vesuvius(const char *scroll_id, int energy, double resolution);
LRUCache *init_cache();
LRUNode *get_cache(LRUCache *cache, int chunk_x, int chunk_y, int chunk_z);
void put_cache(LRUCache *cache, int chunk_x, int chunk_y, int chunk_z, MemoryChunk chunk);
void move_to_head(LRUCache *cache, LRUNode *node);
void evict_from_cache(LRUCache *cache);
int hash_key(int chunk_x, int chunk_y, int chunk_z);
size_t write_data(void *ptr, size_t size, size_t nmemb, MemoryChunk *chunk);
int fetch_zarr_chunk(int chunk_x, int chunk_y, int chunk_z, MemoryChunk *chunk);
int get_volume_voxel(int x, int y, int z, unsigned char *value);
int get_volume_roi(RegionOfInterest region, unsigned char *volume);
int get_volume_slice(RegionOfInterest region, unsigned char *slice);
int write_bmp(const char *filename, unsigned char *image, int width, int height);
int create_directories(const char *path);
int write_chunk_to_disk(int chunk_x, int chunk_y, int chunk_z, MemoryChunk *chunk);
int read_chunk_from_disk(int chunk_x, int chunk_y, int chunk_z, MemoryChunk *chunk);
char *get_cache_path(int chunk_x, int chunk_y, int chunk_z);
char *get_obj_cache_path(const char *id);
int download_obj_file(const char *id, const char *cache_path);
int fetch_obj_file(const char *id, char **obj_file_path);
int parse_obj_file(const char *file_path, TriangleMesh *mesh);
int get_triangle_mesh(const char *id, TriangleMesh *mesh);
int write_trianglemesh_to_obj(const char *filename, const TriangleMesh *mesh);
RegionOfInterest get_mesh_bounding_box(const TriangleMesh *mesh);
void reset_mesh_origin_to_roi(TriangleMesh *mesh, const RegionOfInterest *roi);
#ifdef VESUVIUS_IMPL
// Global cache
LRUCache *cache;
// Global variable to store the dynamically constructed Zarr URL
char ZARR_URL[URL_SIZE] = {0}; // Initially empty
// Variables to hold Zarr's chunk sizes and shape, initially set to -1 to indicate uninitialized
int CHUNK_SIZE_X = -1, CHUNK_SIZE_Y = -1, CHUNK_SIZE_Z = -1;
int SHAPE_X = -1, SHAPE_Y = -1, SHAPE_Z = -1;
// Internal function to write data fetched by cURL
static size_t write_callback(void *ptr, size_t size, size_t nmemb, void *stream) {
strncat((char *)stream, (char *)ptr, size * nmemb);
return size * nmemb;
}
// Fetches the metadata JSON from the specified Zarr directory URL
static int fetch_metadata(const char *url, char *buffer) {
CURL *curl;
CURLcode res;
// Construct the full URL to the `.zarray` metadata file
char metadata_url[URL_SIZE];
snprintf(metadata_url, URL_SIZE, "%s.zarray", url);
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, metadata_url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, buffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
return -1;
}
} else {
fprintf(stderr, "Failed to initialize curl\n");
return -1;
}
return 0;
}
// Parses the metadata JSON to retrieve chunk sizes and shape
static int parse_metadata(const char *buffer) {
struct json_object *parsed_json, *chunks, *shape;
parsed_json = json_tokener_parse(buffer);
if (parsed_json == NULL) {
fprintf(stderr, "Failed to parse JSON\n");
return -1;
}
// Extract "chunks" and "shape" arrays from JSON
if (!json_object_object_get_ex(parsed_json, "chunks", &chunks) ||
!json_object_object_get_ex(parsed_json, "shape", &shape)) {
fprintf(stderr, "Missing 'chunks' or 'shape' in metadata\n");
json_object_put(parsed_json);
return -1;
}
// Set chunk sizes from "chunks" array
CHUNK_SIZE_Z = json_object_get_int(json_object_array_get_idx(chunks, 0));
CHUNK_SIZE_Y = json_object_get_int(json_object_array_get_idx(chunks, 1));
CHUNK_SIZE_X = json_object_get_int(json_object_array_get_idx(chunks, 2));
// Set shape sizes from "shape" array
SHAPE_Z = json_object_get_int(json_object_array_get_idx(shape, 0));
SHAPE_Y = json_object_get_int(json_object_array_get_idx(shape, 1));
SHAPE_X = json_object_get_int(json_object_array_get_idx(shape, 2));
json_object_put(parsed_json); // Free JSON object
return 0;
}
// Public function to initialize chunk sizes and shape
int load_shape_and_chunksize() {
char buffer[BUFFER_SIZE] = {0};
if (fetch_metadata(ZARR_URL, buffer) != 0) {
fprintf(stderr, "Failed to fetch metadata\n");
return -1;
}
if (parse_metadata(buffer) != 0) {
fprintf(stderr, "Failed to parse metadata\n");
return -1;
}
return 0;
}
// Function to write data to memory (used by curl)
size_t write_data(void *ptr, size_t size, size_t nmemb, MemoryChunk *chunk) {
size_t realsize = size * nmemb;
chunk->data = (unsigned char *)realloc(chunk->data, chunk->size + realsize);
if (chunk->data == NULL) {
fprintf(stderr, "Not enough memory (realloc returned NULL)\n");
return 0;
}
memcpy(&(chunk->data[chunk->size]), ptr, realsize);
chunk->size += realsize;
return realsize;
}
// Initialize the vesuvius library with dynamic URL construction
void init_vesuvius(const char *scroll_id, int energy, double resolution) {
// Construct the ZARR_URL based on the provided parameters, stopping at the directory
snprintf(ZARR_URL, URL_SIZE,
"https://dl.ash2txt.org/other/dev/scrolls/%s/volumes/%dkeV_%.2fum.zarr/0/",
scroll_id, energy, resolution);
// Load shape and chunk size from the dynamically constructed ZARR_URL
if (load_shape_and_chunksize() != 0) {
fprintf(stderr, "Failed to load shape and chunk size\n");
exit(EXIT_FAILURE); // Exit if metadata loading fails
}
// Print shape and chunk size to verify
printf("Loaded Zarr metadata from: %s\n", ZARR_URL);
printf("Shape: X=%d, Y=%d, Z=%d\n", SHAPE_X, SHAPE_Y, SHAPE_Z);
printf("Chunk Size: X=%d, Y=%d, Z=%d\n", CHUNK_SIZE_X, CHUNK_SIZE_Y, CHUNK_SIZE_Z);
// Initialize cache (assuming init_cache() initializes the cache system)
cache = init_cache();
}
// Initialize the LRU cache
LRUCache *init_cache() {
LRUCache *cache = (LRUCache *)malloc(sizeof(LRUCache));
cache->head = NULL;
cache->tail = NULL;
cache->count = 0;
// Initialize all cache entries to NULL
for (int i = 0; i < CACHE_CAPACITY; i++) {
cache->cache[i] = NULL;
}
return cache;
}
// Hash function to generate a key for the cache
int hash_key(int chunk_x, int chunk_y, int chunk_z) {
// Ensure the hash key is non-negative and within the bounds of CACHE_CAPACITY
return abs((chunk_x * 73856093) ^ (chunk_y * 19349663) ^ (chunk_z * 83492791)) % CACHE_CAPACITY;
}
// Get path for disk cache based on chunk coordinates
char *get_cache_path(int chunk_x, int chunk_y, int chunk_z) {
char *path = (char *)malloc(512 * sizeof(char));
snprintf(path, 512, "%s/other/dev/scrolls/1/volumes/54keV_7.91um.zarr/0/%d/%d/%d", CACHE_DIR, chunk_z, chunk_y, chunk_x);
return path;
}
// Helper function to create directories recursively
int create_directories(const char *path) {
char temp_path[512];
snprintf(temp_path, sizeof(temp_path), "%s", path);
for (char *p = temp_path + 1; *p; p++) {
if (*p == '/') {
*p = '\0'; // Temporarily terminate the string
if (mkdir(temp_path, 0755) != 0 && errno != EEXIST) {
return -1; // Failed to create directory
}
*p = '/'; // Restore the original slash
}
}
// Create the last directory in the path
if (mkdir(temp_path, 0755) != 0 && errno != EEXIST) {
return -1; // Failed to create final directory
}
return 0; // Success
}
int write_chunk_to_disk(int chunk_x, int chunk_y, int chunk_z, MemoryChunk *chunk) {
char *path = get_cache_path(chunk_x, chunk_y, chunk_z);
// Ensure the directory structure is created recursively
char *dir = strdup(path);
char *last_slash = strrchr(dir, '/');
if (last_slash) {
*last_slash = '\0'; // Remove the file name, keeping only the directory path
if (create_directories(dir) != 0) {
fprintf(stderr, "Failed to create directory: %s\n", dir);
free(dir);
free(path);
return -1;
}
}
free(dir);
// Write chunk data to disk
FILE *file = fopen(path, "wb");
if (!file) {
fprintf(stderr, "Failed to open file: %s\n", path);
free(path);
return -1;
}
fwrite(chunk->data, sizeof(unsigned char), chunk->size, file);
fclose(file);
free(path);
return 0;
}
// Read a chunk from disk cache
int read_chunk_from_disk(int chunk_x, int chunk_y, int chunk_z, MemoryChunk *chunk) {
char *path = get_cache_path(chunk_x, chunk_y, chunk_z);
FILE *file = fopen(path, "rb");
if (!file) {
free(path);
return -1; // File does not exist
}
fseek(file, 0, SEEK_END);
long file_size = ftell(file);
fseek(file, 0, SEEK_SET);
chunk->data = (unsigned char *)malloc(file_size);
if (chunk->data == NULL) {
fclose(file);
free(path);
return -1;
}
fread(chunk->data, sizeof(unsigned char), file_size, file);
chunk->size = file_size;
fclose(file);
free(path);
return 0;
}
// Get chunk from the cache
LRUNode *get_cache(LRUCache *cache, int chunk_x, int chunk_y, int chunk_z) {
int key = hash_key(chunk_x, chunk_y, chunk_z);
LRUNode *node = cache->cache[key];
// Check if the node matches the requested chunk coordinates
if (node && node->chunk_x == chunk_x && node->chunk_y == chunk_y && node->chunk_z == chunk_z) {
move_to_head(cache, node); // Move the node to the head (most recently used)
return node;
}
return NULL;
}
// Put a chunk into the cache
void put_cache(LRUCache *cache, int chunk_x, int chunk_y, int chunk_z, MemoryChunk chunk) {
int key = hash_key(chunk_x, chunk_y, chunk_z);
LRUNode *node = (LRUNode *)malloc(sizeof(LRUNode));
node->chunk_x = chunk_x;
node->chunk_y = chunk_y;
node->chunk_z = chunk_z;
node->chunk = chunk;
node->prev = NULL;
node->next = cache->head;
if (cache->head != NULL) {
cache->head->prev = node;
}
cache->head = node;
if (cache->tail == NULL) {
cache->tail = node;
}
// If the cache is full, evict the least recently used node
if (cache->count == CACHE_CAPACITY) {
evict_from_cache(cache);
} else {
cache->count++;
}
cache->cache[key] = node;
}
// Move a node to the head of the LRU cache (most recently used)
void move_to_head(LRUCache *cache, LRUNode *node) {
if (node == cache->head) return;
if (node->prev) {
node->prev->next = node->next;
}
if (node->next) {
node->next->prev = node->prev;
}
if (node == cache->tail) {
cache->tail = node->prev;
}
node->prev = NULL;
node->next = cache->head;
if (cache->head != NULL) {
cache->head->prev = node;
}
cache->head = node;
}
// Evict the least recently used node from the cache
void evict_from_cache(LRUCache *cache) {
if (cache->tail == NULL) return;
LRUNode *node = cache->tail;
if (cache->tail->prev) {
cache->tail->prev->next = NULL;
}
cache->tail = cache->tail->prev;
int key = hash_key(node->chunk_x, node->chunk_y, node->chunk_z);
cache->cache[key] = NULL;
free(node->chunk.data);
free(node);
}
// Get chunk from the cache, disk, or fetch it
int fetch_zarr_chunk(int chunk_x, int chunk_y, int chunk_z, MemoryChunk *chunk) {
LRUNode *cached_node = get_cache(cache, chunk_x, chunk_y, chunk_z);
if (cached_node) {
*chunk = cached_node->chunk;
return 0;
}
// Try reading from disk cache
if (read_chunk_from_disk(chunk_x, chunk_y, chunk_z, chunk) == 0) {
put_cache(cache, chunk_x, chunk_y, chunk_z, *chunk); // Store in memory cache
return 0;
}
// Fetch the chunk from the server
CURL *curl;
CURLcode res;
char url[512];
snprintf(url, sizeof(url), "%s%d/%d/%d", ZARR_URL, chunk_z, chunk_y, chunk_x);
chunk->data = (unsigned char *)malloc(1);
chunk->size = 0;
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)chunk);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
curl_easy_cleanup(curl);
return -1;
}
curl_easy_cleanup(curl);
// Decompress the chunk using Blosc
unsigned char *decompressed_data = (unsigned char *)malloc(CHUNK_SIZE_Z * CHUNK_SIZE_Y * CHUNK_SIZE_X);
int decompressed_size = blosc2_decompress(chunk->data, chunk->size, decompressed_data, CHUNK_SIZE_Z * CHUNK_SIZE_Y * CHUNK_SIZE_X);
if (decompressed_size < 0) {
fprintf(stderr, "Blosc2 decompression failed: %d\n", decompressed_size);
free(chunk->data);
free(decompressed_data);
return -1;
}
// Free the compressed data and update the chunk with the decompressed data
free(chunk->data);
chunk->data = decompressed_data;
chunk->size = decompressed_size;
// Store in memory and disk cache
put_cache(cache, chunk_x, chunk_y, chunk_z, *chunk);
write_chunk_to_disk(chunk_x, chunk_y, chunk_z, chunk);
}
return 0;
}
// Function to retrieve the value at a specific (x, y, z) index
int get_volume_voxel(int x, int y, int z, unsigned char *value) {
// Calculate the corresponding chunk indices
int chunk_x = x / CHUNK_SIZE_X;
int chunk_y = y / CHUNK_SIZE_Y;
int chunk_z = z / CHUNK_SIZE_Z;
// Calculate the local indices within the chunk
int local_x = x % CHUNK_SIZE_X;
int local_y = y % CHUNK_SIZE_Y;
int local_z = z % CHUNK_SIZE_Z;
// Fetch the chunk data
MemoryChunk chunk = {0};
if (fetch_zarr_chunk(chunk_x, chunk_y, chunk_z, &chunk) != 0) {
fprintf(stderr, "Failed to fetch Zarr chunk\n");
return -1;
}
// Retrieve the value from the chunk data
*value = chunk.data[local_z * CHUNK_SIZE_X * CHUNK_SIZE_Y + local_y * CHUNK_SIZE_X + local_x];
return 0;
}
// Function to fill a 3D volume from the Zarr data
int get_volume_roi(RegionOfInterest region, unsigned char *volume) {
// Validate boundaries
if (region.x_start < 0 || region.x_start + region.x_width > SHAPE_X ||
region.y_start < 0 || region.y_start + region.y_height > SHAPE_Y ||
region.z_start < 0 || region.z_start + region.z_depth > SHAPE_Z) {
fprintf(stderr, "Invalid boundaries for the volume\n");
return -1;
}
// Determine the range of chunks needed for the volume
int chunk_start_x = region.x_start / CHUNK_SIZE_X;
int chunk_end_x = (region.x_start + region.x_width - 1) / CHUNK_SIZE_X;
int chunk_start_y = region.y_start / CHUNK_SIZE_Y;
int chunk_end_y = (region.y_start + region.y_height - 1) / CHUNK_SIZE_Y;
int chunk_start_z = region.z_start / CHUNK_SIZE_Z;
int chunk_end_z = (region.z_start + region.z_depth - 1) / CHUNK_SIZE_Z;
// Loop over all chunks that cover the volume
for (int chunk_z = chunk_start_z; chunk_z <= chunk_end_z; ++chunk_z) {
for (int chunk_y = chunk_start_y; chunk_y <= chunk_end_y; ++chunk_y) {
for (int chunk_x = chunk_start_x; chunk_x <= chunk_end_x; ++chunk_x) {
// Fetch the chunk data
MemoryChunk chunk = {0};
if (fetch_zarr_chunk(chunk_x, chunk_y, chunk_z, &chunk) != 0) {
fprintf(stderr, "Failed to fetch Zarr chunk (%d, %d, %d)\n", chunk_x, chunk_y, chunk_z);
return -1;
}
// Calculate local boundaries within the chunk
int local_start_x = (chunk_x == chunk_start_x) ? region.x_start % CHUNK_SIZE_X : 0;
int local_end_x = (chunk_x == chunk_end_x) ? (region.x_start + region.x_width - 1) % CHUNK_SIZE_X : CHUNK_SIZE_X - 1;
int local_start_y = (chunk_y == chunk_start_y) ? region.y_start % CHUNK_SIZE_Y : 0;
int local_end_y = (chunk_y == chunk_end_y) ? (region.y_start + region.y_height - 1) % CHUNK_SIZE_Y : CHUNK_SIZE_Y - 1;
int local_start_z = (chunk_z == chunk_start_z) ? region.z_start % CHUNK_SIZE_Z : 0;
int local_end_z = (chunk_z == chunk_end_z) ? (region.z_start + region.z_depth - 1) % CHUNK_SIZE_Z : CHUNK_SIZE_Z - 1;
// Copy the relevant data from the chunk to the volume
for (int z = local_start_z; z <= local_end_z; ++z) {
for (int y = local_start_y; y <= local_end_y; ++y) {
memcpy(&volume[((chunk_z * CHUNK_SIZE_Z + z - region.z_start) * region.y_height +
(chunk_y * CHUNK_SIZE_Y + y - region.y_start)) * region.x_width +
(chunk_x * CHUNK_SIZE_X + local_start_x - region.x_start)],
&chunk.data[z * CHUNK_SIZE_X * CHUNK_SIZE_Y + y * CHUNK_SIZE_X + local_start_x],
local_end_x - local_start_x + 1);
}
}
}
}
}
return 0;
}
int get_volume_slice(RegionOfInterest region, unsigned char *slice) {
// Validate boundaries
if (region.x_start < 0 || region.x_start + region.x_width > SHAPE_X ||
region.y_start < 0 || region.y_start + region.y_height > SHAPE_Y ||
region.z_start < 0 || region.z_start + region.z_depth > SHAPE_Z) {
fprintf(stderr, "Invalid boundaries for the volume\n");
return -1;
}
// Validate depth 1
if (region.z_depth != 1) {
fprintf(stderr, "Slice must have z_depth of 1\n");
return -1;
}
// Fetch the volume data for the slice
unsigned char *volume = (unsigned char *)malloc(region.x_width * region.y_height);
if (get_volume_roi(region, volume) != 0) {
fprintf(stderr, "Failed to fetch volume data for slice\n");
return -1;
}
// Copy the slice data from the volume
for (int y = 0; y < region.y_height; y++) {
for (int x = 0; x < region.x_width; x++) {
slice[y * region.x_width + x] = volume[y * region.x_width + x];
}
}
free(volume);
return 0;
}
// BMP Header Structures
#pragma pack(push, 1) // Ensure no padding
typedef struct {
uint16_t bfType;
uint32_t bfSize;
uint16_t bfReserved1;
uint16_t bfReserved2;
uint32_t bfOffBits;
} BMPFileHeader;
typedef struct {
uint32_t biSize;
int32_t biWidth;
int32_t biHeight;
uint16_t biPlanes;
uint16_t biBitCount;
uint32_t biCompression;
uint32_t biSizeImage;
int32_t biXPelsPerMeter;
int32_t biYPelsPerMeter;
uint32_t biClrUsed;
uint32_t biClrImportant;
} BMPInfoHeader;
#pragma pack(pop)
// Function to write the image slice to a BMP file
int write_bmp(const char *filename, unsigned char *image, int width, int height) {
FILE *file = fopen(filename, "wb");
if (!file) {
fprintf(stderr, "Failed to open file for writing: %s\n", filename);
return -1;
}
// BMP file and info headers
BMPFileHeader file_header;
BMPInfoHeader info_header;
// Calculate the size of each row including padding
int rowSize = (width + 3) & ~3; // Round up to the nearest multiple of 4
int imageSize = rowSize * height;
// BMP file header
file_header.bfType = 0x4D42; // 'BM'
file_header.bfOffBits = sizeof(BMPFileHeader) + sizeof(BMPInfoHeader) + 256 * 4; // File header + Info header + Palette
file_header.bfSize = file_header.bfOffBits + imageSize;
file_header.bfReserved1 = 0;
file_header.bfReserved2 = 0;
// BMP info header
info_header.biSize = sizeof(BMPInfoHeader);
info_header.biWidth = width;
info_header.biHeight = -height; // Negative height to indicate top-down row order
info_header.biPlanes = 1;
info_header.biBitCount = 8; // 8 bits per pixel (grayscale)
info_header.biCompression = 0;
info_header.biSizeImage = imageSize;
info_header.biXPelsPerMeter = 2835; // 72 DPI
info_header.biYPelsPerMeter = 2835; // 72 DPI
info_header.biClrUsed = 256;
info_header.biClrImportant = 256;
// Write BMP file header
fwrite(&file_header, sizeof(BMPFileHeader), 1, file);
// Write BMP info header
fwrite(&info_header, sizeof(BMPInfoHeader), 1, file);
// Write the grayscale palette (256 shades of gray)
for (int i = 0; i < 256; ++i) {
unsigned char color[4] = {i, i, i, 0}; // R, G, B, Reserved
fwrite(color, sizeof(unsigned char), 4, file);
}
// Write the pixel data with padding
unsigned char *row = (unsigned char *)malloc(rowSize);
if (!row) {
fprintf(stderr, "Memory allocation failed\n");
fclose(file);
return -1;
}
for (int y = 0; y < height; ++y) {
// Copy the image row and pad it
memcpy(row, image + (height - 1 - y) * width, width);
memset(row + width, 0, rowSize - width); // Pad the row
// Write the padded row
fwrite(row, sizeof(unsigned char), rowSize, file);
}
free(row);
fclose(file);
return 0;
}
char *get_obj_cache_path(const char *id) {
char *path = (char *)malloc(512 * sizeof(char));
snprintf(path, 512, "%s/full-scrolls/Scroll1/PHercParis4.volpkg/paths/%s/%s.obj", CACHE_DIR, id, id);
return path;
}
int download_obj_file(const char *id, const char *cache_path) {
CURL *curl;
CURLcode res;
char url[512];
snprintf(url, sizeof(url), "https://dl.ash2txt.org/full-scrolls/Scroll1/PHercParis4.volpkg/paths/%s/%s.obj", id, id);
FILE *file = fopen(cache_path, "wb");
if (!file) {
fprintf(stderr, "Failed to open file: %s\n", cache_path);
return -1;
}
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
fclose(file);
curl_easy_cleanup(curl);
return -1;
}
curl_easy_cleanup(curl);
}
fclose(file);
return 0;
}
int fetch_obj_file(const char *id, char **obj_file_path) {
char *cache_path = get_obj_cache_path(id);
// Check if file exists in cache
FILE *file = fopen(cache_path, "r");
if (file) {
fclose(file);
*obj_file_path = cache_path;
return 0; // File already cached
}
// Create directory structure and download the file
char *dir = strdup(cache_path);
char *last_slash = strrchr(dir, '/');
if (last_slash) {
*last_slash = '\0';
if (create_directories(dir) != 0) {
fprintf(stderr, "Failed to create directory: %s\n", dir);
free(dir);
free(cache_path);
return -1;
}
}
free(dir);
// Download the file
if (download_obj_file(id, cache_path) != 0) {
free(cache_path);
return -1;
}
*obj_file_path = cache_path;
return 0;
}
int parse_obj_file(const char *file_path, TriangleMesh *mesh) {
FILE *file = fopen(file_path, "r");
if (!file) {
fprintf(stderr, "Failed to open .obj file: %s\n", file_path);
return -1;
}
// Initial capacities for vertices and triangles
size_t vertex_capacity = 100;
size_t triangle_capacity = 100;
// Allocate memory for vertices and triangles
mesh->vertices = (Vertex *)malloc(vertex_capacity * sizeof(Vertex));
mesh->triangles = (Triangle *)malloc(triangle_capacity * sizeof(Triangle));
mesh->vertex_count = 0;
mesh->triangle_count = 0;
if (!mesh->vertices || !mesh->triangles) {
fprintf(stderr, "Failed to allocate memory for mesh.\n");
fclose(file);
return -1;
}
char line[256];
while (fgets(line, sizeof(line), file)) {
// Parse vertex line (v)
if (strncmp(line, "v ", 2) == 0) {
// Resize the vertex array if capacity is exceeded
if (mesh->vertex_count >= vertex_capacity) {
vertex_capacity *= 2;
mesh->vertices = (Vertex *)realloc(mesh->vertices, vertex_capacity * sizeof(Vertex));
if (!mesh->vertices) {
fprintf(stderr, "Failed to reallocate memory for vertices.\n");
fclose(file);
return -1;
}
}
// Read vertex coordinates
sscanf(line + 2, "%f %f %f", &mesh->vertices[mesh->vertex_count].x,
&mesh->vertices[mesh->vertex_count].y, &mesh->vertices[mesh->vertex_count].z);
mesh->vertex_count++;
}
// Detect and skip vertex normal line (vn)
else if (strncmp(line, "vn ", 3) == 0) {
continue;
}
// Detect and skip texture coordinate line (vt)
else if (strncmp(line, "vt ", 3) == 0) {
continue;
}
// Parse face (f) line
else if (strncmp(line, "f ", 2) == 0) {
// Resize the triangle array if capacity is exceeded
if (mesh->triangle_count >= triangle_capacity) {
triangle_capacity *= 2;
mesh->triangles = (Triangle *)realloc(mesh->triangles, triangle_capacity * sizeof(Triangle));
if (!mesh->triangles) {
fprintf(stderr, "Failed to reallocate memory for triangles.\n");
fclose(file);
return -1;
}
}
// Parse face with vertex indices (ignoring texture and normal indices)
int v1, v2, v3;
sscanf(line + 2, "%d/%*d/%*d %d/%*d/%*d %d/%*d/%*d", &v1, &v2, &v3);
mesh->triangles[mesh->triangle_count].v1 = v1 - 1; // Convert to 0-based indexing
mesh->triangles[mesh->triangle_count].v2 = v2 - 1;
mesh->triangles[mesh->triangle_count].v3 = v3 - 1;
mesh->triangle_count++;
}
}
fclose(file);
return 0;
}
int get_triangle_mesh(const char *id, TriangleMesh *mesh) {
char *obj_file_path = NULL;
if (fetch_obj_file(id, &obj_file_path) != 0) {
fprintf(stderr, "Failed to fetch .obj file for ID: %s\n", id);
return -1;
}
if (parse_obj_file(obj_file_path, mesh) != 0) {
fprintf(stderr, "Failed to parse .obj file: %s\n", obj_file_path);
free(obj_file_path);
return -1;
}
free(obj_file_path);
return 0;
}
int write_trianglemesh_to_obj(const char *filename, const TriangleMesh *mesh) {
FILE *file = fopen(filename, "w");
if (!file) {
fprintf(stderr, "Error: Could not open file for writing: %s\n", filename);
return -1;
}
// Write vertices
for (size_t i = 0; i < mesh->vertex_count; ++i) {
const Vertex *v = &mesh->vertices[i];
fprintf(file, "v %f %f %f\n", v->x, v->y, v->z);
}
// Write faces (triangles)
for (size_t i = 0; i < mesh->triangle_count; ++i) {
const Triangle *t = &mesh->triangles[i];
// OBJ uses 1-based indexing, so increment the vertex indices by 1
fprintf(file, "f %d %d %d\n", t->v1 + 1, t->v2 + 1, t->v3 + 1);
}
fclose(file);
return 0;
}
RegionOfInterest get_mesh_bounding_box(const TriangleMesh *mesh) {
// Initialize the bounding box to extreme values
float min_x = FLT_MAX, min_y = FLT_MAX, min_z = FLT_MAX;
float max_x = -FLT_MAX, max_y = -FLT_MAX, max_z = -FLT_MAX;
// Traverse through all vertices to find the bounding box
for (size_t i = 0; i < mesh->vertex_count; ++i) {
const Vertex *v = &mesh->vertices[i];
if (v->x < min_x) min_x = v->x;
if (v->y < min_y) min_y = v->y;
if (v->z < min_z) min_z = v->z;
if (v->x > max_x) max_x = v->x;
if (v->y > max_y) max_y = v->y;
if (v->z > max_z) max_z = v->z;
}
// Calculate the dimensions of the bounding box
RegionOfInterest roi;
roi.x_start = min_x;
roi.y_start = min_y;
roi.z_start = min_z;
roi.x_width = max_x - min_x;
roi.y_height = max_y - min_y;
roi.z_depth = max_z - min_z;
return roi;
}
void reset_mesh_origin_to_roi(TriangleMesh *mesh, const RegionOfInterest *roi) {
// Subtract the ROI origin from each vertex in the mesh
for (size_t i = 0; i < mesh->vertex_count; ++i) {
mesh->vertices[i].x -= roi->x_start;
mesh->vertices[i].y -= roi->y_start;
mesh->vertices[i].z -= roi->z_start;
}
}
#endif
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdint.h>
#include <math.h>
#include <float.h>
#include <stdbool.h>
#include <assert.h>
#include <sys/stat.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <curl/curl.h>
#include <blosc2.h>
#if defined(__linux__) || defined(__GLIBC__)
#include <execinfo.h>
#endif
#ifdef NDEBUG
#define ASSERT(expr, msg, ...) ((void)0)
#else
#define ASSERT(expr, msg, ...) do{if(!(expr)){fprintf(stderr,msg __VA_OPT__(,)#__VA_ARGS__); vs__assert_fail_with_backtrace(#expr, __FILE__, __LINE__, __func__);}}while(0)
#endif
#ifdef MAX
#undef MAX
#endif
#define MAX(a,b) (a > b ? a : b)
#ifdef MIN
#undef MIN
#endif
#define MIN(a,b) (a < b ? a : b)
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef int8_t s8;
typedef int16_t s16;
typedef int32_t s32;
typedef int64_t s64;
typedef float f32;