csg 0.0.0 → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. data/src/bsp.c +14 -8
  2. data/src/bsp.h +11 -0
  3. data/src/export.c +0 -1
  4. metadata +2 -2
data/src/bsp.c CHANGED
@@ -307,7 +307,8 @@ klist_t(poly) *bsp_clip_polygon_array(bsp_node_t *node, poly_t **polygons, size_
307
307
  poly_t *p = NULL;
308
308
  int rc = -1;
309
309
 
310
- poly_t **poly_buffer = NULL;
310
+ poly_t *static_poly_buffer[STATIC_POLY_BUFFER_SIZE];
311
+ poly_t **poly_buffer = static_poly_buffer;
311
312
  poly_t **front_array = NULL;
312
313
  poly_t **back_array = NULL;
313
314
  int n_front = 0;
@@ -317,7 +318,9 @@ klist_t(poly) *bsp_clip_polygon_array(bsp_node_t *node, poly_t **polygons, size_
317
318
  if(n_polys == 0) return result;
318
319
 
319
320
  if(node->divider != NULL) {
320
- check_mem(poly_buffer = malloc((sizeof(poly_t*) * n_polys) * 2));
321
+ if((n_polys * 2) > STATIC_POLY_BUFFER_SIZE) {
322
+ check_mem(poly_buffer = malloc((sizeof(poly_t*) * n_polys) * 2));
323
+ }
321
324
  front_array = poly_buffer;
322
325
  back_array = poly_buffer + n_polys;
323
326
  // Sort this node's polygons into the front or back
@@ -350,7 +353,7 @@ klist_t(poly) *bsp_clip_polygon_array(bsp_node_t *node, poly_t **polygons, size_
350
353
  check(result != NULL, "Failed to clip back tree");
351
354
  }
352
355
 
353
- if(poly_buffer) free(poly_buffer);
356
+ if(poly_buffer != static_poly_buffer) free(poly_buffer);
354
357
  // Clean up the result halves, now that they're copied into `result`
355
358
  }
356
359
  else {
@@ -363,7 +366,7 @@ klist_t(poly) *bsp_clip_polygon_array(bsp_node_t *node, poly_t **polygons, size_
363
366
 
364
367
  return result;
365
368
  error:
366
- if(poly_buffer) free(poly_buffer);
369
+ if(poly_buffer != static_poly_buffer) free(poly_buffer);
367
370
  if(result) kl_destroy(poly, result);
368
371
  return NULL;
369
372
  }
@@ -374,7 +377,8 @@ klist_t(poly) *bsp_clip_polygons(bsp_node_t *node, klist_t(poly) *polygons, klis
374
377
  poly_t *p = NULL;
375
378
  int rc = -1;
376
379
 
377
- poly_t **poly_buffer = NULL;
380
+ poly_t *static_poly_buffer[STATIC_POLY_BUFFER_SIZE];
381
+ poly_t **poly_buffer = static_poly_buffer;
378
382
  poly_t **front_array = NULL;
379
383
  poly_t **back_array = NULL;
380
384
  int n_front = 0;
@@ -384,7 +388,9 @@ klist_t(poly) *bsp_clip_polygons(bsp_node_t *node, klist_t(poly) *polygons, klis
384
388
  if(polygons->size == 0) return result;
385
389
 
386
390
  if(node->divider != NULL) {
387
- check_mem(poly_buffer = malloc(sizeof(poly_t*) * polygons->size * 2));
391
+ if((polygons->size * 2) > STATIC_POLY_BUFFER_SIZE) {
392
+ check_mem(poly_buffer = malloc(sizeof(poly_t*) * polygons->size * 2));
393
+ }
388
394
  front_array = poly_buffer;
389
395
  back_array = poly_buffer + polygons->size;
390
396
  // Sort this node's polygons into the front or back
@@ -416,7 +422,7 @@ klist_t(poly) *bsp_clip_polygons(bsp_node_t *node, klist_t(poly) *polygons, klis
416
422
  check(result != NULL, "Failed to clip back tree");
417
423
  }
418
424
 
419
- if(poly_buffer) free(poly_buffer);
425
+ if(poly_buffer != static_poly_buffer) free(poly_buffer);
420
426
  // Clean up the result halves, now that they're copied into `result`
421
427
  }
422
428
  else {
@@ -429,7 +435,7 @@ klist_t(poly) *bsp_clip_polygons(bsp_node_t *node, klist_t(poly) *polygons, klis
429
435
 
430
436
  return result;
431
437
  error:
432
- if(poly_buffer) free(poly_buffer);
438
+ if(poly_buffer != static_poly_buffer) free(poly_buffer);
433
439
  if(result) kl_destroy(poly, result);
434
440
  return NULL;
435
441
  }
data/src/bsp.h CHANGED
@@ -4,6 +4,17 @@
4
4
  #ifndef __BSP_H
5
5
  #define __BSP_H
6
6
 
7
+ // This many polygon pointers are allocated
8
+ // on the stack during bsp_clip_polygons and bsp_clip_polygon_array
9
+ // Only when more than this is requested do we reach
10
+ // into the heap for more polygon pointers.
11
+ // Setting this to zero disables the optimization.
12
+ // Exceptionally large values will limit the recursion
13
+ // limit.
14
+ #ifndef STATIC_POLY_BUFFER_SIZE
15
+ #define STATIC_POLY_BUFFER_SIZE 200
16
+ #endif
17
+
7
18
  typedef struct s_bsp_node {
8
19
  klist_t(poly) *polygons;
9
20
  poly_t *divider;
data/src/export.c CHANGED
@@ -26,7 +26,6 @@ stl_object *bsp_to_stl(bsp_node_t *tree) {
26
26
 
27
27
  polys = bsp_to_polygons(tree, 1, NULL);
28
28
  check(polys != NULL, "Failed to generate polygons from bsp_node_t(%p)", tree);
29
- check(polys->size > 0, "No polygons returned from tree(%p)", tree);
30
29
 
31
30
  stl = stl_from_polys(polys);
32
31
  check(stl != NULL, "Failed to build stl from %zd polygons", polys->size);
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: csg
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-06-11 00:00:00.000000000 Z
13
+ date: 2013-07-05 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: ffi