-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsimarrange.c
712 lines (661 loc) · 25.9 KB
/
simarrange.c
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
/*Copyright (c) 2013 Kliment Yanev
This file is part of simarrange.
Simarrange 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.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <admesh/stl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include "utlist.h"
#include <dirent.h>
#include <argtable2.h>
#include <ctype.h>
#include <string.h>
#include <limits.h>
#include <math.h>
#include <omp.h>
//#define PARALLEL
#if defined PARALLEL
#include <omp.h>
#endif
#define FILENAME_LEN 350
inline int max ( int a, int b ) { return a > b ? a : b; }
inline int min ( int a, int b ) { return a < b ? a : b; }
typedef struct img_list{
IplImage *image;
long area;
int count;
int done; // counts the number of copies already plated
int countinplate; // counts the number of copies in the latest plate
int *x;
int *y;
int *rotangle;
int *plate;
char filename[FILENAME_LEN];
stl_file *stl;
struct img_list *prev,*next;
} img_list;
void sqspiral(int n, int *i, int *j)
{
double x = sqrt(n+1);
int xi = floor(x);
int xisq = xi * xi;
if (xi % 2 == 0) // even
{
int xi2 = xi / 2;
if (x == xi)
{
*i = -xi2 + 1;
*j = xi2;
}
// top left number is xisq - xi - 1, bottom left n - xisq - 1
else if (n <= xisq + xi + 1)
{
*i = -xi2;
*j = xi2 - (n - xisq - 1);
}
else
{
*i = -xi2 + (n - xisq - xi - 1);
*j = -xi2;
}
}
else // odd
{
if (x == xi)
{
int xi2 = (xi-1)/2;
*i = xi2;
*j = -xi2;
}
else
{
int xi2 = (xi + 1) / 2;
if (n <= xisq + xi + 1)
{
*i = xi2;
*j = -xi2 + (n - xisq);
}
else
{
*i = xi2 - (n - xisq - xi - 1);
*j = xi2;
}
}
}
}
void add_stl(char *filename, int count, int width, int height, img_list **shapes){
img_list *elt;
DL_FOREACH(*shapes,elt) {
if (strncmp(filename, elt->filename, FILENAME_LEN) == 0) {
elt->count += count;
elt->x = (int*) realloc(elt->x, sizeof(int)*elt->count);
elt->y = (int*) realloc(elt->y, sizeof(int)*elt->count);
elt->rotangle = (int*) realloc(elt->rotangle, sizeof(int)*elt->count);
elt->plate = (int*) realloc(elt->plate, sizeof(int)*elt->count);
return;
}
}
stl_file *s=(stl_file *)malloc(sizeof(stl_file));
memset(s,0,sizeof(stl_file));
stl_open(s,filename);
stl_exit_on_error(s);
stl_check_facets_exact(s);
s->stats.facets_w_1_bad_edge = (s->stats.connected_facets_2_edge - s->stats.connected_facets_3_edge);
s->stats.facets_w_2_bad_edge = (s->stats.connected_facets_1_edge - s->stats.connected_facets_2_edge);
s->stats.facets_w_3_bad_edge = (s->stats.number_of_facets - s->stats.connected_facets_1_edge);
height=(int)(2*height);
width=(int)(2*width);
stl_translate(s, ((float)width/2)-(s->stats.max.x-s->stats.min.x)/2.0, ((float)height/2)-(s->stats.max.y-s->stats.min.y)/2.0, 0.0);
IplImage* img = NULL;
img=cvCreateImage(cvSize(width,height), IPL_DEPTH_8U, 1);
cvZero(img);
unsigned int i;
for (i = 0; i < s->stats.number_of_facets; i++) {
CvPoint points[3]={cvPoint((int)(s->facet_start[i].vertex[0].x),(int)(s->facet_start[i].vertex[0].y)),
cvPoint((int)(s->facet_start[i].vertex[1].x),(int)(s->facet_start[i].vertex[1].y)),
cvPoint((int)(s->facet_start[i].vertex[2].x),(int)(s->facet_start[i].vertex[2].y))
};
cvFillConvexPoly(img,points,3,cvScalarAll(127),8,0);
}
img_list *e=(img_list*)malloc(sizeof(img_list));
e->image=img;
e->x=(int*)malloc(sizeof(int)*count);
e->y=(int*)malloc(sizeof(int)*count);
e->rotangle=(int*)malloc(sizeof(int)*count);
e->plate=(int*)malloc(sizeof(int)*count);
for (i = 0; i < count; ++i)
e->plate[i] = -1;
e->stl=s;
e->count=count;
e->done = 0;
e->area=cvCountNonZero(e->image);
strcpy(e->filename,filename);
DL_APPEND(*shapes,e);
}
int areacmp(img_list *a, img_list *b) {
return (b->area)-(a->area);
}
int dl_count(img_list *l){
int c=0;
img_list *elt;
DL_FOREACH(l,elt){
c += elt->count - elt->done;
}
return c;
}
int add_files(struct arg_file *arg, int w, int h, img_list **shapes, int withrepeat) {
int ifile;
char *indir=NULL;
struct stat filestat;
for(ifile = 0; ifile < arg->count; ifile++){
char filename[FILENAME_LEN];
strncpy(filename, arg->filename[ifile], FILENAME_LEN);
int copies = 1;
if (withrepeat) {
int filename_len = strlen(filename);
int k;
for (k = filename_len - 1; k >= 0; --k) {
if (filename[k] == '+') {
copies = strtol(filename + k + 1, NULL, 10);
filename[k] = '\0';
break;
}
}
}
if( stat(filename, &filestat) == 0 ){
if( filestat.st_mode & S_IFREG ){
char d[FILENAME_LEN];
strcpy(d,filename);
int i;
for(i = 0; d[i]; i++)
d[i] = tolower(d[i]);
if(strstr(d,".stl")!=0){
add_stl(filename, copies, w, h, shapes);
}
}
if( filestat.st_mode & S_IFDIR ){
indir= filename;
DIR *dir;
struct dirent *ent;
if ((dir = opendir (indir)) != NULL) {
while ((ent = readdir (dir)) != NULL) {
int i;
char d[FILENAME_LEN];
strcpy(d,ent->d_name);
for(i = 0; d[i]; i++)
d[i] = tolower(d[i]);
if(strstr(d,".stl")!=0){
char f[FILENAME_LEN];
f[0]=0;
strcat(f,indir);
strcat(f+strlen(f),"/");
strcat(f+strlen(f),ent->d_name);
add_stl(f, copies, w, h, shapes);
}
//printf ("%s\n", ent->d_name);
}
closedir (dir);
} else {
fprintf(stderr, "Input directory not found %s\n",indir);
return EXIT_FAILURE;
}
}
}else{
fprintf(stderr, "Could not access %s\n",filename);
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
int search(int rotangle, int posstep, int w, int h, int firstpassed, int middle, int *minxpos, int *minypos, int *minrotangle, int *mincentricords,CvPoint2D32f center, IplImage *itmp, IplImage *rpatch, IplImage *grpatch, IplImage *img,IplImage *testfit,CvMat *rot){
int xpos,ypos,placed=0;
cvWarpAffine(itmp,rpatch,cv2DRotationMatrix(center, rotangle, 1.0, rot),CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS, cvScalarAll(0) );
if(firstpassed && middle){
int centricords=0;
for(centricords=0;centricords<*mincentricords;centricords++){
xpos=ypos=0;
sqspiral(centricords, &xpos, &ypos);
xpos*=posstep;
ypos*=posstep;
xpos=max(0,min(xpos+w/2,w-1));
ypos=max(0,min(ypos+h/2,h-1));
cvSetImageROI(rpatch, cvRect(w-xpos,h-ypos,w,h));
cvAnd(rpatch, img, testfit, NULL);
if(!cvCountNonZero(testfit)){
int prec=cvCountNonZero(img);
cvAdd(rpatch, img, testfit, NULL);
if(prec!=cvCountNonZero(testfit)){
#if defined PARALLEL
#pragma omp critical
#endif
{
if(centricords<*mincentricords){
*minxpos=xpos;
*minypos=ypos;
*minrotangle=rotangle;
*mincentricords=centricords;
cvResetImageROI(rpatch);
if(grpatch)
cvCopy(rpatch,grpatch,NULL);
placed=1;
}
}
}
}
cvResetImageROI(rpatch);
}
}else{
for(ypos=1;ypos<*minypos;ypos+=posstep){
for(xpos=1;xpos<*minxpos;xpos+=posstep){
cvSetImageROI(rpatch, cvRect(w-xpos,h-ypos,w,h));
cvAnd(rpatch, img, testfit, NULL);
if(!cvCountNonZero(testfit)){
int prec=cvCountNonZero(img);
cvAdd(rpatch, img, testfit, NULL);
if(prec!=cvCountNonZero(testfit)){
#if defined PARALLEL
#pragma omp critical
#endif
{
if (xpos+ypos < (*minxpos+*minypos))
{
*minxpos=xpos;
*minypos=ypos;
*minrotangle=rotangle;
cvResetImageROI(rpatch);
if(grpatch)
cvCopy(rpatch,grpatch,NULL);
placed=1;
}
}
}
}
cvResetImageROI(rpatch);
}
}
}
return placed;
}
int main(int argc, char** argv){
int w=200,h=200;
int spacing=1;
int rotstep=10;
int posstep=5;
int quiet=0;
int c;
int maxplates=0;
struct arg_int *aw = arg_int0("x","width",NULL, "plate width in mm (default is 200)");
struct arg_int *ah = arg_int0("y","height",NULL, "plate height in mm (default is 200)");
struct arg_int *as = arg_int0("s","spacing",NULL, "spacing between parts in mm (default is 1)");
struct arg_int *ar = arg_int0("r","rotstep",NULL, "rotation step when searching (default 10 degrees)");
struct arg_int *ap = arg_int0("p","posstep",NULL, "positional step when searching (default 5mm)");
struct arg_lit *ac = arg_lit0("c","circle", "circular print area with diameter given by -x");
struct arg_lit *acorigin = arg_lit0("m","middle", "place objects from middle of build area out");
#ifdef PARALLEL
struct arg_int *athreads = arg_int0("j","threads",NULL, "number of threads (default is to use as much as possible, set to 1 to disable multithreading)");
#endif
struct arg_lit *adryrun = arg_lit0("d","dryrun", "only do a dry run, computing placement but not producing any output file");
struct arg_lit *ahelp = arg_lit0("h","help", "display this help message");
struct arg_lit *aquiet = arg_lit0("q","quiet", "silence information messages");
struct arg_int *alimit = arg_int0("l","limit",NULL, "maximum number of plates to fill, stop after this number of plates is done");
struct arg_str *aodir = arg_str0("o","outputdir",NULL, "output directory (default .)");
struct arg_file *arepeat = arg_filen("n","repeat",NULL,0,argc+2, "add a given number of copies of the input file or dir by specifying filepath+count");
struct arg_file *ainfile = arg_filen(NULL,NULL,NULL,0,argc+2, "input file or dir (any number allowed)");
struct arg_end *end = arg_end(20);
void* argtable[] = {aw,ah,as,ar,ap,ac,acorigin,aodir,ainfile,arepeat,
#ifdef PARALLEL
athreads,
#endif
adryrun,aquiet,alimit,ahelp,end};
int nerrors;
nerrors = arg_parse(argc,argv,argtable);
if(ahelp->count){
printf("Usage: %s", argv[0]);
arg_print_syntax(stdout, argtable, "\n");
printf("Options:\n");
arg_print_glossary(stdout, argtable, " %-25s %s\n");
return EXIT_SUCCESS;
}
if (nerrors > 0)
{
/* Display the error details contained in the arg_end struct.*/
arg_print_errors(stderr,end,argv[0]);
return EXIT_FAILURE;
}
if (arepeat->count == 0 && ainfile->count == 0) {
fprintf(stderr, "%s: please specify one or more input file or directory\n", argv[0]);
return EXIT_FAILURE;
}
if(alimit->count){
maxplates=alimit->ival[0];
}
if(aw->count){
w=aw->ival[0];
}
if(ah->count){
h=ah->ival[0];
}
if(ac->count){
h=w;
}
if(as->count){
spacing=as->ival[0];
}
if(ar->count){
rotstep=ar->ival[0];
}
if(ap->count){
posstep=ap->ival[0];
}
if(aquiet->count){
quiet = 1;
}
if(adryrun->count && !quiet){
printf("Running in dry run mode (no output file will be produced)\n");
}
#ifdef PARALLEL
if(athreads->count) {
if (athreads->ival[0] > 0){
int numthreads = athreads->ival[0];
omp_set_dynamic(0);
omp_set_num_threads(numthreads);
} else {
fprintf(stderr, "%s: number of threads cannot be lower than 0\n", argv[0]);
return EXIT_FAILURE;
}
}
#endif
img_list *shapes=NULL;
img_list *curplate=NULL;
int platecount=0;
int ret;
if(ainfile->count){
ret = add_files(ainfile, w, h, &shapes, 0);
if (ret != EXIT_SUCCESS)
return ret;
}
if(arepeat->count){
ret = add_files(arepeat, w, h, &shapes, 1);
if (ret != EXIT_SUCCESS)
return ret;
}
DL_SORT(shapes, areacmp);
img_list *elt,*tmp;
int plate=0;
IplImage* img = NULL;
img=cvCreateImage(cvSize(w,h), IPL_DEPTH_8U, 1);
CvMat* rot = NULL;
rot=cvCreateMat(2,3, CV_32FC1);
IplImage* rpatch = NULL;
rpatch=cvCreateImage(cvSize((w*2),(h*2)), IPL_DEPTH_8U, 1);
IplImage* testfit = NULL;
testfit=cvCreateImage(cvSize(w,h), IPL_DEPTH_8U, 1);
IplImage* itmp = NULL;
itmp=cvCreateImage(cvSize(2*w,2*h), IPL_DEPTH_8U, 1);
CvPoint2D32f center=cvPoint2D32f((w),(h));
unsigned copy;
while(dl_count(shapes)){
if (!quiet) printf("Generating plate %d\n",plate);
cvZero(img);
cvLine(img, cvPoint(0,0), cvPoint(w-1,0), cvScalarAll(127), 1, 8, 0);
cvLine(img, cvPoint(w-1,0), cvPoint(w-1,h-1), cvScalarAll(127), 1, 8, 0);
cvLine(img, cvPoint(w-1,h-1), cvPoint(0,h-1), cvScalarAll(127), 1, 8, 0);
cvLine(img, cvPoint(0,h-1), cvPoint(0,0), cvScalarAll(127), 1, 8, 0);
if(ac->count){
cvRectangle(img, cvPoint(0,0), cvPoint(w-1,h-1), cvScalarAll(127), CV_FILLED, 8, 0);
cvCircle(img, cvPoint(w/2,h/2), (w/2)-1, cvScalarAll(0), CV_FILLED, 8, 0);
}
cvZero(rpatch);
cvZero(testfit);
cvZero(itmp);
int firstpassed=0, placed=0;
DL_FOREACH(shapes,elt) {
for (copy = elt->done; copy < elt->count; copy++) {
placed=0;
//printf("File: %s\n",elt->filename);
cvCopy(img, testfit, NULL);
int xpos=1, ypos=1, rotangle=0, minxpos=w-1, minypos=h-1, minrotangle=0, mincentricords=max(w-1,h-1)*max(w-1,h-1)/(posstep*posstep);
cvDilate(elt->image,itmp,NULL,spacing);
#ifdef PARALLEL
#pragma omp parallel for
for(rotangle=0;rotangle<360;rotangle+=rotstep){
//rpatch,rot,testfit
CvMat* rot = NULL;
rot=cvCreateMat(2,3, CV_32FC1);
IplImage* prpatch = NULL;
prpatch=cvCreateImage(cvSize((w*2),(h*2)), IPL_DEPTH_8U, 1);
IplImage* testfit = NULL;
testfit=cvCreateImage(cvSize(w,h), IPL_DEPTH_8U, 1);
placed|=search(rotangle, posstep, w, h, firstpassed, acorigin->count, &minxpos, &minypos, &minrotangle, &mincentricords,center, itmp, prpatch, rpatch, img,testfit,rot);
cvReleaseImage(&testfit);
cvReleaseImage(&prpatch);
cvReleaseMat(&rot);
}
#else
for(rotangle=0;rotangle<360;rotangle+=rotstep){
placed|=search(rotangle, posstep, w, h, firstpassed, acorigin->count, &minxpos, &minypos, &minrotangle, &mincentricords,center, itmp, rpatch, NULL, img,testfit,rot);
}
#endif
if(!firstpassed && acorigin->count){
int centricords=0;
for(centricords=0;centricords<max(w-1,h-1)*max(w-1,h-1);centricords++){
xpos=ypos=0;
sqspiral(centricords, &xpos, &ypos);
xpos=max(0,min(xpos+w/2,w-1));
ypos=max(0,min(ypos+h/2,h-1));
cvSetImageROI(rpatch, cvRect(w-xpos,h-ypos,w,h));
cvAnd(rpatch, img, testfit, NULL);
if(!cvCountNonZero(testfit)){
int prec=cvCountNonZero(img);
cvAdd(rpatch, img, testfit, NULL);
if(prec!=cvCountNonZero(testfit)){
minxpos=xpos;
minypos=ypos;
placed=1;
cvResetImageROI(rpatch);
break;
}
}
}
}
if(placed){
if (!quiet) printf("File: %s minx: %d, miny: %d, minrot: %d\n",elt->filename, minxpos, minypos, minrotangle);
cvWarpAffine(elt->image,rpatch,cv2DRotationMatrix(center, minrotangle, 1.0, rot),CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS, cvScalarAll(0) );
cvSetImageROI(rpatch, cvRect(w-minxpos,h-minypos,w,h));
cvAdd(rpatch,img,testfit,NULL);
cvCopy(testfit,img,NULL);
cvResetImageROI(rpatch);
elt->x[elt->done]=minxpos;
elt->y[elt->done]=minypos;
elt->rotangle[elt->done]=minrotangle;
elt->plate[elt->done]=plate;
elt->done++;
platecount++;
}else{
if (!quiet) printf("SKIP: %s skipped for this plate\n",elt->filename);
if (!firstpassed) {
fprintf(stderr, "Could not fit this file as the first item of the plate in any tested orientation! It might be too large for the print area.\n");
return EXIT_FAILURE;
}
break;
}
firstpassed=1;
}
} // end of the DL_FOREACH(shapes,elt) { loop
if(!platecount){
fprintf(stderr, "The files skipped in the last stage do not fit on plate in any tested orientation! They might be too large for the print area.\n");
return EXIT_FAILURE;
}
int maxextraplates = INT_MAX;
DL_FOREACH(shapes,elt){
elt->countinplate = 0;
for (copy = 0; copy < elt->done; ++copy)
{
if (elt->plate[copy] == plate) {
elt->countinplate++;
}
}
if (elt->countinplate > 0) {
int extraplates = (elt->count - elt->done) / elt->countinplate;
maxextraplates = min(maxextraplates, extraplates);
}
}
if (maxextraplates > 0) {
if (!quiet) printf("Making %d duplicates of plate %d\n", maxextraplates, plate);
DL_FOREACH(shapes,elt){
elt->done += elt->countinplate * maxextraplates;
}
}
if (!adryrun->count)
{
char tmpfn[512], outdir[512];
char imagefn[1024], stlfn[1024];
outdir[0]=0;
if(aodir->count){
mkdir(aodir->sval[0],0777);
strcpy(imagefn,aodir->sval[0]);
strcat(imagefn,"/");
strcpy(outdir,aodir->sval[0]);
strcat(outdir,"/");
}
sprintf(tmpfn,"plate%02d.png",plate);
imagefn[0]=0;
strcat(imagefn,outdir);
strcat(imagefn,tmpfn);
cvFlip(img,NULL,0);
cvSaveImage(imagefn,img,0);
sprintf(tmpfn,"plate%02d.stl",plate);
stlfn[0]=0;
strcat(stlfn,outdir);
strcat(stlfn,tmpfn);
FILE *fp;
int i;
fp = fopen(stlfn, "w");
if(fp == NULL){
fprintf(stderr, "Could not open output file %s\n",stlfn);
return EXIT_FAILURE;
}
fprintf(fp, "%s", stlfn);
for(i = strlen(stlfn); i < LABEL_SIZE; i++) putc(0, fp);
fseek(fp, LABEL_SIZE, SEEK_SET);
int totalfacets=0;
DL_FOREACH(shapes,elt){
for (copy = 0; copy < elt->done; ++copy)
{
if(elt->plate[copy]==plate){
totalfacets+=elt->stl->stats.number_of_facets;
}
}
}
stl_put_little_int(fp, totalfacets);
DL_FOREACH(shapes,elt){
if (elt->countinplate == 0)
continue;
for (copy = 0; copy < elt->done; ++copy)
{
if(elt->plate[copy]==plate){
stl_file *s=elt->stl;
stl_translate(s,0-(s->stats.max.x-s->stats.min.x)/2.0,0-(s->stats.max.y-s->stats.min.y)/2.0,0);
stl_rotate_z(s,-elt->rotangle[copy]);
stl_translate_relative(s,elt->x[copy],elt->y[copy], 0);
stl_write_binary_block(s,fp);
if (elt->count > 1) { // Reset object state
stl_translate_relative(s,-elt->x[copy],-elt->y[copy], 0);
stl_rotate_z(s,elt->rotangle[copy]);
}
}
}
}
fclose(fp);
stl_file stl_in;
int last_edges_fixed = 0;
int iterations = 2;
stl_open(&stl_in, stlfn);
stl_exit_on_error(&stl_in);
stl_check_facets_exact(&stl_in);
stl_in.stats.facets_w_1_bad_edge =
(stl_in.stats.connected_facets_2_edge -
stl_in.stats.connected_facets_3_edge);
stl_in.stats.facets_w_2_bad_edge =
(stl_in.stats.connected_facets_1_edge -
stl_in.stats.connected_facets_2_edge);
stl_in.stats.facets_w_3_bad_edge =
(stl_in.stats.number_of_facets -
stl_in.stats.connected_facets_1_edge);
float tolerance = stl_in.stats.shortest_edge;
float increment = stl_in.stats.bounding_diameter / 10000.0;
if(stl_in.stats.connected_facets_3_edge < stl_in.stats.number_of_facets)
{
for(i = 0; i < iterations; i++)
{
if(stl_in.stats.connected_facets_3_edge <
stl_in.stats.number_of_facets)
{
stl_check_facets_nearby(&stl_in, tolerance);
last_edges_fixed = stl_in.stats.edges_fixed;
tolerance += increment;
}
}
stl_remove_unconnected_facets(&stl_in);
stl_fill_holes(&stl_in);
}
stl_fix_normal_directions(&stl_in);
stl_fix_normal_values(&stl_in);
stl_verify_neighbors(&stl_in);
stl_generate_shared_vertices(&stl_in);
stl_write_binary(&stl_in, stlfn, stlfn);
stl_exit_on_error(&stl_in);
if (maxextraplates > 0)
{
int k;
for (k = 0; k < maxextraplates; ++k) {
int platek = plate + k + 1;
sprintf(tmpfn,"plate%02d.png", platek);
imagefn[0]=0;
strcat(imagefn,outdir);
strcat(imagefn,tmpfn);
cvSaveImage(imagefn,img,0);
sprintf(tmpfn,"plate%02d.stl", platek);
stlfn[0]=0;
strcat(stlfn,outdir);
strcat(stlfn,tmpfn);
stl_write_binary(&stl_in, stlfn, stlfn);
stl_exit_on_error(&stl_in);
}
}
stl_close(&stl_in);
}
plate += 1 + maxextraplates;
if(maxplates>0 && plate >= maxplates){
if (!quiet) printf("Stopping at limit of plate %d\n", maxplates);
break;
}
platecount=0;
}
DL_FOREACH_SAFE(shapes,elt,tmp) {
cvReleaseImage(&(elt->image));
free(elt->stl);
free(elt->x);
free(elt->y);
free(elt->rotangle);
free(elt->plate);
DL_DELETE(shapes,elt);
free(elt);
}
cvReleaseImage(&itmp);
cvReleaseImage(&img);
cvReleaseImage(&rpatch);
cvReleaseImage(&testfit);
return EXIT_SUCCESS;
}