Source

Pleasant3D_OpenSource / ToolPlugins / Slice / SliceKernel.m

Full commit
  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
//
//  SliceKernel.m
//  Slice
//
//  Created by Eberhard Rensch on 12.08.09.
//  Copyright 2009 Pleasant Software. All rights reserved.
//

#import "SliceKernel.h"

#include "sliceVerticesOpenCLSource.h" // Will be generated from sliceVertices.cl during build time (processOpenCL.pl Build Rule)

#define __verbose 0

const size_t kSTLFacetSize=sizeof(STLVertex)*4+sizeof(cl_half); // STL-Data is not aligned!
const size_t kSlicedEdgeSize=sizeof(SlicedEdge);
const size_t kSlicedEdgeLayerReferenceSize=sizeof(SlicedEdgeLayerReference);
const size_t kLineOffsetSize=sizeof(LineOffset);
const size_t kFacetEdgeSize=sizeof(FacetEdge);
const size_t kEdgeIndexSize=sizeof(EdgeIndex);
const size_t kInsetLoopCornerSize=sizeof(InsetLoopCorner);

static void inverseSlicedEdge(SlicedEdge* edge)
{
	cl_uint endEdge = edge->parentEndEdge;
	edge->parentEndEdge = edge->parentStartEdge;
	edge->parentStartEdge = endEdge;
	cl_float2 p;
	p[0] = edge->startPoint[0];
	p[1] = edge->startPoint[1];
	edge->startPoint[0] = edge->endPoint[0];
	edge->startPoint[1] = edge->endPoint[1];
	edge->endPoint[0] = p[0];
	edge->endPoint[1] = p[1];
}


@implementation SliceKernel
@synthesize extrusionHeight, extrusionWidth;

- (BOOL)prepareOpelCL
{
	int err;
	int gpu = 0;
	err = clGetDeviceIDs(NULL, gpu ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU, 1, &device_id, NULL);
	if (err != CL_SUCCESS) {
		PSErrorLog(@"clGetDeviceIDs returns %d",err);
		return NO;
	}
	
	context = clCreateContext(0, 1, &device_id, NULL, NULL, &err);
	if (!context) {
		PSErrorLog(@"clCreateContext returns %d",err);
		return NO;
	}
	
	queue = clCreateCommandQueue(context, device_id, 0, &err);
	if (!queue) {
		PSErrorLog(@"clCreateCommandQueue returns %d",err);
		return NO;
	}
	
	program = clCreateProgramWithSource(context, sliceVerticesSourceCodeCount,	sliceVerticesSourceCode, NULL, &err);
	if (!program) {
		PSErrorLog(@"clCreateProgramWithSource returns %d",err);
		return NO;
	}
	
	err = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
	if (err != CL_SUCCESS)
	{
		size_t len;
		char buffer[2048];
		
		PSErrorLog(@"Error: Failed to build program executable");
		clGetProgramBuildInfo(program, device_id, CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, &len);
		PSErrorLog(@"%s\nn***", buffer);
		return NO;
	}
	
	kernelCalcLineOffsets = clCreateKernel(program, "calcLineOffsets", &err);
    if (!kernelCalcLineOffsets || err != CL_SUCCESS) {
		PSErrorLog(@"clCreateKernel kernelCalcLineOffsets returns %d",err);
		return NO;
	}
	
	kernelSliceTriangles = clCreateKernel(program, "sliceTriangles", &err);
    if (!kernelSliceTriangles || err != CL_SUCCESS) {
		PSErrorLog(@"clCreateKernel kernelSliceTriangles returns %d",err);
		return NO;
	}
	
	kernelInsetLoop = clCreateKernel(program, "insetLoop", &err);
    if (!kernelInsetLoop || err != CL_SUCCESS) {
		PSErrorLog(@"clCreateKernel kernelInsetLoop returns %d",err);
		return NO;
	}
	
	kernelOptimizeCornerPoints = clCreateKernel(program, "optimizeCornerPoints", &err);
    if (!kernelOptimizeCornerPoints || err != CL_SUCCESS) {
		PSErrorLog(@"clCreateKernel kernelOptimizeCornerPoints returns %d",err);
		return NO;
	}
	
	kernelOptimizeConnections = clCreateKernel(program, "optimizeConnections", &err);
    if (!kernelOptimizeConnections || err != CL_SUCCESS) {
		PSErrorLog(@"clCreateKernel kernelOptimizeConnections returns %d",err);
		return NO;
	}
	
	return YES;
}

- (void)cleanupOpenCL
{
	PSLog(@"Slice",5,@"PSSlice: Cleaning up OpenCL");
	if(kernelCalcLineOffsets) clReleaseKernel(kernelCalcLineOffsets);
	if(kernelSliceTriangles) clReleaseKernel(kernelSliceTriangles);
	if(kernelInsetLoop) clReleaseKernel(kernelInsetLoop);
	if(kernelOptimizeCornerPoints) clReleaseKernel(kernelOptimizeCornerPoints);
	if(kernelOptimizeConnections) clReleaseKernel(kernelOptimizeConnections);
	if(program) clReleaseProgram(program);
	if(queue) clReleaseCommandQueue(queue);
	if(context) clReleaseContext(context);	
	context=nil; // This marks the whole OpenCL setup as "cleaned up"	
}


- (id) init
{
	self = [super init];
	if (self != nil) {
		extrusionHeight = .4;
		extrusionWidth = 1.5*extrusionHeight;
	}
	return self;
}

- (void)finalize
{
	if(context)
		[self cleanupOpenCL];
	[super finalize];
}

- (P3DLoops*)slice:(IndexedSTLModel*)indexedModel
{
	NSMutableArray* layerLoops=nil;
	InsetLoopCorner* insetLoops=nil;
	NSUInteger totalCorners=0;
	if(context || [self prepareOpelCL])
	{
		cl_int err;	
		STLBinaryHead* stl = [indexedModel.stlModel stlHead];
			
	#pragma mark Step 1: Calc Line Offsets. Calculate the buffer sizes for slicing
		
		cl_float zStart = (cl_float)indexedModel.stlModel.cornerMinimum.z;
		NSUInteger totalLayers = (NSUInteger)((indexedModel.stlModel.cornerMaximum.z-indexedModel.stlModel.cornerMinimum.z)/extrusionHeight);
		LineOffset* lineOffsets = (LineOffset*)malloc(stl->numberOfFacets*kLineOffsetSize);
		cl_mem inputFacets = clCreateBuffer(context, CL_MEM_READ_ONLY|CL_MEM_USE_HOST_PTR,  kSTLFacetSize*stl->numberOfFacets, firstFacet(stl), &err);
		if (!inputFacets || err != CL_SUCCESS)
		{
			PSErrorLog(@"clCreateBuffer inputFacets failed: %d",err);
			return nil;
		}
		
		cl_mem inOutLineOffsets = clCreateBuffer(context, CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR, kLineOffsetSize*stl->numberOfFacets, lineOffsets, &err);
		if (!inOutLineOffsets || err != CL_SUCCESS)
		{
			PSErrorLog(@"clCreateBuffer inOutLineOffsets failed: %d",err);
			return nil;
		}
			
	//    err = clEnqueueWriteBuffer(queue, inputFacets, CL_FALSE, 0, kSTLFacetSize*stl->numberOfFacets, firstFacet(stl), 0, NULL, NULL);
	//    if (err != CL_SUCCESS) {
	//		PSErrorLog(@"clEnqueueWriteBuffer returns %d",err);
	//		return nil;
	//	}
		
		cl_float layerHeight = (cl_float)extrusionHeight;
		err = 0;
		err |= clSetKernelArg(kernelCalcLineOffsets, 0, sizeof(cl_mem), &inOutLineOffsets);
		err |= clSetKernelArg(kernelCalcLineOffsets, 1, sizeof(cl_mem), &inputFacets);
		err |= clSetKernelArg(kernelCalcLineOffsets, 2, sizeof(cl_float), &zStart);
		err |= clSetKernelArg(kernelCalcLineOffsets, 3, sizeof(cl_float), &layerHeight);
		if (err != CL_SUCCESS)
		{
			PSErrorLog(@"clSetKernelArg kernelCalcLineOffsets returns %d",err);
			return nil;
		}
		
		size_t global = stl->numberOfFacets;
		err = clEnqueueNDRangeKernel(queue, kernelCalcLineOffsets, 1, NULL, &global, NULL, 0, NULL, NULL);
		if (err)
		{
			PSErrorLog(@"clEnqueueNDRangeKernel returns %d",err);
			return nil;
		}
		
		err = clEnqueueReadBuffer( queue, inOutLineOffsets, CL_TRUE, 0, kLineOffsetSize*stl->numberOfFacets, lineOffsets, 0, NULL, NULL );  
		if (err != CL_SUCCESS)
		{
			PSErrorLog(@"clEnqueueReadBuffer returns %d",err);
			return nil;
		}
		
		//
		// Calculating the number of Sliced Lines from the intermed results
		// This step changes the meaning of the "offset" field
		// before it was the number of layers affecting the facet.
		// after it'll be the start index in the global slicedPoints array for this facet
		//
		NSUInteger totalSlices=0;
		for(NSInteger fIndex=0; fIndex<stl->numberOfFacets; fIndex++)
		{
			NSUInteger count=lineOffsets[fIndex].offset;
			if(count>0)
				lineOffsets[fIndex].offset = totalSlices;
			else
				lineOffsets[fIndex].offset = (cl_uint)-1; // Nothing to slice for this one
			totalSlices += count;
		}
			
	#pragma mark Step 2: Slice the triangles in the model
	#ifdef __DEBUG__
		NSDate* startOfSliceTriangles = [NSDate date];
	#endif

		// 
		// Create buffer for the endresults
		//
		
		// This array holds all sliced points
		SlicedEdge* slicedEdges = (SlicedEdge*)malloc(totalSlices*kSlicedEdgeSize);
		// This array holds an index of "layerIndex" <-> "slicedPointIndex"
		SlicedEdgeLayerReference* slicedEdgeLayerReferences = (SlicedEdgeLayerReference*)malloc(totalSlices*kSlicedEdgeLayerReferenceSize);
		
		cl_mem inOutSlicedEdges;
		inOutSlicedEdges = clCreateBuffer(context, CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR, totalSlices*kSlicedEdgeSize,slicedEdges, &err);
		if (!inOutSlicedEdges || err != CL_SUCCESS)
		{
			PSErrorLog(@"clCreateBuffer inOutSlicedEdges failed: %d", err);
			return nil;
		}
		cl_mem inOutSlicedEdgeLayerReferences;
		inOutSlicedEdgeLayerReferences = clCreateBuffer(context, CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR, totalSlices*kSlicedEdgeLayerReferenceSize,slicedEdgeLayerReferences, &err);
		if (!inOutSlicedEdgeLayerReferences || err != CL_SUCCESS)
		{
			PSErrorLog(@"clCreateBuffer inOutSlicedEdgeLayerReferences failed: %d", err);
			return nil;
		}
		
		err = clSetKernelArg(kernelSliceTriangles, 0, sizeof(cl_mem), &inOutSlicedEdges);
		err |= clSetKernelArg(kernelSliceTriangles, 1, sizeof(cl_mem), &inOutSlicedEdgeLayerReferences);
		err |= clSetKernelArg(kernelSliceTriangles, 2, sizeof(cl_mem), &inputFacets);
		err |= clSetKernelArg(kernelSliceTriangles, 3, sizeof(cl_mem), &inOutLineOffsets);
		err |= clSetKernelArg(kernelSliceTriangles, 4, sizeof(cl_float), &zStart);
		err |= clSetKernelArg(kernelSliceTriangles, 5, sizeof(cl_float), &layerHeight);
		if (err != CL_SUCCESS)
		{
			PSErrorLog(@"clSetKernelArg kernelSliceTriangles returns %d",err);
			return nil;
		}		
		
		global = stl->numberOfFacets;
		err = clEnqueueNDRangeKernel(queue, kernelSliceTriangles, 1, NULL, &global, NULL, 0, NULL, NULL);
		if (err)
		{
			PSErrorLog(@"clEnqueueNDRangeKernel returns %d",err);
			return nil;
		}
			
		err = clEnqueueReadBuffer( queue, inOutSlicedEdges, CL_TRUE, 0, totalSlices*kSlicedEdgeSize, slicedEdges, 0, NULL, NULL );  
		if (err != CL_SUCCESS)
		{
			PSErrorLog(@"clEnqueueReadBuffer inOutSlicedPoints returns %d",err);
			return nil;
		}
		
		err = clEnqueueReadBuffer( queue, inOutSlicedEdgeLayerReferences, CL_TRUE, 0, totalSlices*kSlicedEdgeLayerReferenceSize, slicedEdgeLayerReferences, 0, NULL, NULL );  
		if (err != CL_SUCCESS)
		{
			PSErrorLog(@"clEnqueueReadBuffer inOutSlicedPointLayerReferences returns %d",err);
			return nil;
		}
		
		PSLog(@"Timer",5,@"Time for executing OpenCL kernel SliceTriangles: %1.2f", -[startOfSliceTriangles timeIntervalSinceNow]);
	#if __verbose
		PSLog(@"Slice",5,@"Sliced Edges:");
		for(int i = 0; i<totalSlices; i++)
			PSLog(@"Slice",5,@"#%d Layer %d  Facet %d (%f|%f) [Edge %d] -> (%f|%f) [Edge %d]", i, slicedEdges[i].layerIndex, slicedEdges[i].parentFacet, 
				  slicedEdges[i].startPoint[0], slicedEdges[i].startPoint[1], slicedEdges[i].parentStartEdge,
				  slicedEdges[i].endPoint[0], slicedEdges[i].endPoint[1], slicedEdges[i].parentEndEdge);
	#endif
		
	#pragma mark Step 3: Sort and index the results from Step 2 for further processing
		
	#ifdef __DEBUG__
		NSDate* startSortAndIndex = [NSDate date];
	#endif	
		// Sort the Layer references
		qsort_b(slicedEdgeLayerReferences, totalSlices, kSlicedEdgeLayerReferenceSize, ^(const void *arg1, const void *arg2){
			return (int)(((SlicedEdgeLayerReference*)arg1)->layerIndex-((SlicedEdgeLayerReference*)arg2)->layerIndex);
		});

		// We can now access the sliced edges sorted by layerIndex
		
		// The layer catalog contains an Index from layerIndex -> slicedPointLayerReference (-> slicedPointIndex)
		// With help of this catalog, we have fast access on the first slicedPoint of a layer
		cl_uint* layerCatalog = (cl_uint*)malloc((totalLayers+1)*sizeof(cl_uint));
		NSUInteger lastLayerIndex = 0;
		NSUInteger layerCatalogLength = 0;
		layerCatalog[layerCatalogLength++]=0;
		for(NSUInteger catRefIdx=0;catRefIdx<totalSlices; catRefIdx++)
		{
			if(lastLayerIndex<slicedEdgeLayerReferences[catRefIdx].layerIndex)
			{
				lastLayerIndex=slicedEdgeLayerReferences[catRefIdx].layerIndex;
				do
				{
					layerCatalog[layerCatalogLength++]=catRefIdx;
				} while(layerCatalogLength<lastLayerIndex);
			}
		}
		layerCatalog[layerCatalogLength++]=totalSlices; // Add a last (virtual) index for boundarie calculations
		
		PSLog(@"Timer",5,@"Time for executing SortAndIndex: %1.2f", -[startSortAndIndex timeIntervalSinceNow]);

	#if __verbose
		PSLog(@"Slice",5,@"Sorted EdgeLayerReferences:");
		for(int i = 0; i<totalSlices; i++)
			PSLog(@"Slice",5,@"#%d Layer# %d -> Edge# %d", i, slicedEdgeLayerReferences[i].layerIndex, slicedEdgeLayerReferences[i].slicedEdgeIndex);
		
		
		PSLog(@"Slice",5,@"LayerCatalog:");
		for(int i = 0; i<layerCatalogLength-1; i++)
			PSLog(@"Slice",5,@"Layer# %d from %u to %u", i, layerCatalog[i], layerCatalog[i+1]-1);
	#endif
		
	#pragma mark Step 4: Connect the loops on each layer

	#ifdef __DEBUG__
		NSDate* startConnectLayerLoops = [NSDate date];
	#endif

		NSUInteger __block danglingEdges=0;
		
	#if !__disable_gcd	
		
		dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
		dispatch_group_t dpGroup = dispatch_group_create();
	#endif
		
		for(NSInteger layerIndex = 0; layerIndex<totalLayers; layerIndex++)
		{
	#if !__disable_gcd	
		
			dispatch_group_async(dpGroup, globalQueue, ^{
	#endif		
			NSUInteger startIndex = layerCatalog[layerIndex];
			NSUInteger endIndex = layerCatalog[layerIndex+1];

			for(NSUInteger refIdx = startIndex; refIdx<endIndex; refIdx++)
			{
				NSUInteger currentEdgeIndex = slicedEdgeLayerReferences[refIdx].slicedEdgeIndex;
				if(slicedEdges[currentEdgeIndex].connectsTo==(cl_uint)-1) // Not yet handled
				{
					BOOL stillInLoop=YES;
					while(stillInLoop)
					{
						NSUInteger connectionIndex = slicedEdges[currentEdgeIndex].parentFacet*3+slicedEdges[currentEdgeIndex].parentEndEdge;
						if(connectionIndex>=indexedModel.edgeIndex.numberOfIndexedEdges)
						{
							PSErrorLog(@"Corrupted Data in indexedEdges");
							stillInLoop=NO;
							break;
						}
						EdgeIndex connectionInfo = indexedModel.edgeIndex.indexedEdges[connectionIndex];
						
						BOOL conectionFound=NO;
						NSUInteger connectingRefIdx;
						for(connectingRefIdx = startIndex; connectingRefIdx<endIndex; connectingRefIdx++)
						{
							if(/*(slicedEdges[slicedEdgeLayerReferences[connectingRefIdx].slicedEdgeIndex].connectsTo==(cl_uint)-1 ||
								slicedEdgeLayerReferences[refIdx].slicedEdgeIndex == currentEdgeIndex) &&*/
							   slicedEdges[slicedEdgeLayerReferences[connectingRefIdx].slicedEdgeIndex].parentFacet == connectionInfo.touchesFacetIndex &&
							   (slicedEdges[slicedEdgeLayerReferences[connectingRefIdx].slicedEdgeIndex].parentStartEdge == connectionInfo.touchesEdgeIndex ||
							   slicedEdges[slicedEdgeLayerReferences[connectingRefIdx].slicedEdgeIndex].parentEndEdge == connectionInfo.touchesEdgeIndex))
							{
								if(slicedEdges[slicedEdgeLayerReferences[connectingRefIdx].slicedEdgeIndex].parentEndEdge == connectionInfo.touchesEdgeIndex)
								{
									inverseSlicedEdge(&(slicedEdges[slicedEdgeLayerReferences[connectingRefIdx].slicedEdgeIndex]));
								}
								
								slicedEdges[currentEdgeIndex].connectsTo = slicedEdgeLayerReferences[connectingRefIdx].slicedEdgeIndex;
								currentEdgeIndex = slicedEdgeLayerReferences[connectingRefIdx].slicedEdgeIndex;
								conectionFound=YES;
								if(slicedEdgeLayerReferences[refIdx].slicedEdgeIndex == currentEdgeIndex) // The Loop is closed!
								{
									stillInLoop=NO;
								}
								break;
							}
						}					
						if(!conectionFound)
						{
							stillInLoop=NO;
							danglingEdges++;
							break;
						}
					}
				}
			}
			
	#if !__disable_gcd	
			}); // dispatch
	#endif		
		}
	#if !__disable_gcd	
		dispatch_group_wait(dpGroup, DISPATCH_TIME_FOREVER);
		dispatch_release(dpGroup);
	#endif	
		if(danglingEdges>0)
			PSErrorLog(@"Could't recover %d dangling edges",danglingEdges);
		PSLog(@"Timer",5,@"Time for executing ConnectLayerLoops: %1.2f", -[startConnectLayerLoops timeIntervalSinceNow]);
			
	#if __verbose
		PSLog(@"Slice",5,@"Sliced Connected Edges:");
		for(int i = 0; i<totalSlices; i++)
			PSLog(@"Slice",5,@"#%d Layer %d Facet %d (%f|%f) [Edge %d] -> (%f|%f) [Edge %d] Connects To %d", i, slicedEdges[i].layerIndex, slicedEdges[i].parentFacet,
				  slicedEdges[i].startPoint[0], slicedEdges[i].startPoint[1], slicedEdges[i].parentStartEdge,
				  slicedEdges[i].endPoint[0], slicedEdges[i].endPoint[1], slicedEdges[i].parentEndEdge,
				  slicedEdges[i].connectsTo);
	#endif	

		
	#pragma mark Step 5: Optimize Loops - Mark all uneccessary points in the loops

	#ifdef __DEBUG__
		NSDate* startOfOptimize = [NSDate date];
	#endif
		
		// 
		// Create buffer for the endresults
		//
		
		// This array holds all sliced points
		cl_uint* optimizedConnections = (cl_uint*)malloc(totalSlices*sizeof(cl_uint));
		
		cl_mem inOutOptimizedConnections;
		inOutOptimizedConnections = clCreateBuffer(context, CL_MEM_READ_WRITE|CL_MEM_USE_HOST_PTR, totalSlices*sizeof(cl_uint), optimizedConnections, &err);
		if (!inOutOptimizedConnections || err != CL_SUCCESS)
		{
			PSErrorLog(@"clCreateBuffer inOutOptimizedConnections failed: %d", err);
			return nil;
		}
		
		// TODO: Necessary?
		err = clEnqueueWriteBuffer(queue, inOutSlicedEdges, CL_FALSE, 0, totalSlices*kSlicedEdgeSize, slicedEdges, 0, NULL, NULL);
		if (err != CL_SUCCESS) {
			PSErrorLog(@"clEnqueueWriteBuffer inOutSlicedEdges returns %d",err);
			return nil;
		}
		
		// Phase 1: Gather Information
		err = clSetKernelArg(kernelOptimizeCornerPoints, 0, sizeof(cl_mem), &inOutOptimizedConnections);
		err = clSetKernelArg(kernelOptimizeCornerPoints, 1, sizeof(cl_mem), &inOutSlicedEdges);
		if (err != CL_SUCCESS)
		{
			PSErrorLog(@"clSetKernelArg kernelOptimizeCornerPoints returns %d",err);
			return nil;
		}		
		
		global = totalSlices;
		err = clEnqueueNDRangeKernel(queue, kernelOptimizeCornerPoints, 1, NULL, &global, NULL, 0, NULL, NULL);
		if (err)
		{
			PSErrorLog(@"clEnqueueNDRangeKernel kernelOptimizeCornerPoints returns %d",err);
			return nil;
		}
				
		// Phase 2: Process Information		
		err = clSetKernelArg(kernelOptimizeConnections, 0, sizeof(cl_mem), &inOutOptimizedConnections);
		err = clSetKernelArg(kernelOptimizeConnections, 1, sizeof(cl_mem), &inOutSlicedEdges);
		if (err != CL_SUCCESS)
		{
			PSErrorLog(@"clSetKernelArg kernelOptimizeCornerPoints returns %d",err);
			return nil;
		}		
		global = totalSlices;
		err = clEnqueueNDRangeKernel(queue, kernelOptimizeConnections, 1, NULL, &global, NULL, 0, NULL, NULL);
		if (err)
		{
			PSErrorLog(@"clEnqueueNDRangeKernel kernelOptimizeCornerPoints returns %d",err);
			return nil;
		}
		
		err = clEnqueueReadBuffer( queue, inOutSlicedEdges, CL_TRUE, 0, totalSlices*kSlicedEdgeSize, slicedEdges, 0, NULL, NULL );  
		if (err != CL_SUCCESS)
		{
			PSErrorLog(@"clEnqueueReadBuffer inOutSlicedPoints returns %d",err);
			return nil;
		}
		
		
		clReleaseMemObject(inOutOptimizedConnections);
		free(optimizedConnections);
		PSLog(@"Timer",5,@"Time for executing OpenCL kernel OptimizeCornerPoints: %1.2f", -[startOfOptimize timeIntervalSinceNow]);
		
		layerLoops = [[NSMutableArray alloc] initWithCapacity:totalLayers];
		for(NSUInteger layerIndex = 0; layerIndex<totalLayers; layerIndex++)
			[layerLoops addObject:[[NSMutableArray alloc] init]];
		totalCorners=0;
		for(NSUInteger sliceIndex = 0; sliceIndex<totalSlices; sliceIndex++)
		{
			if(slicedEdges[sliceIndex].connectsTo!=(cl_uint)-1 && slicedEdges[sliceIndex].optimizedIndex==(cl_uint)-1) // relevant and not yet handled
			{
				PSMutableIntegerArray* loop = [[PSMutableIntegerArray alloc] init];
				[loop addInteger:totalCorners];
				slicedEdges[sliceIndex].optimizedIndex = totalCorners++;

				NSUInteger currentSliceIndex = sliceIndex;
				
				while(slicedEdges[currentSliceIndex].connectsTo!=(cl_uint)-1)
				{
					//NSUInteger thisIndex = currentSliceIndex;
					currentSliceIndex=slicedEdges[currentSliceIndex].connectsTo;
					//slicedEdges[thisIndex].connectsTo=(cl_uint)-1;
					if(currentSliceIndex==sliceIndex) // Loop closed!
					{
						[loop addInteger:slicedEdges[sliceIndex].optimizedIndex];
						[[layerLoops objectAtIndex:slicedEdges[currentSliceIndex].layerIndex] addObject:loop];
						loop=nil;
						break;
					}
//					if(slicedEdges[currentSliceIndex].optimizedIndex!=(cl_uint)-1)
//					{
//						NSLog(@"Loopbreak");
//						break;
//					}
						
					
					[loop addInteger:totalCorners];
					slicedEdges[currentSliceIndex].optimizedIndex = totalCorners++;
				}
				if([loop count]>1)	// Dangling edge (worth to try...)
				{
					[[layerLoops objectAtIndex:slicedEdges[currentSliceIndex].layerIndex] addObject:loop];
				}
			}
		}
		PSLog(@"Timer",5,@"Time for executing OptimizeCornerPoints: %1.2f", -[startOfOptimize timeIntervalSinceNow]);
#if __verbose
		PSLog(@"Slice",5,@"Optimized Edges:");
		for(int i = 0; i<totalSlices; i++)
			if(slicedEdges[i].connectsTo!=(cl_uint)-1)
				PSLog(@"Slice",5,@"#%d Layer %d Facet %d (%f|%f) [Edge %d] -> (%f|%f) [Edge %d] Connects To %d Optimized Index: %d", i, slicedEdges[i].layerIndex, slicedEdges[i].parentFacet,
				  slicedEdges[i].startPoint[0], slicedEdges[i].startPoint[1], slicedEdges[i].parentStartEdge,
				  slicedEdges[i].endPoint[0], slicedEdges[i].endPoint[1], slicedEdges[i].parentEndEdge,
				  slicedEdges[i].connectsTo, slicedEdges[i].optimizedIndex);
		PSLog(@"Slice",5,@"Invalidated slices:");
		for(int i = 0; i<totalSlices; i++)
			if(slicedEdges[i].connectsTo==(cl_uint)-1)
				PSLog(@"Slice",5,@"(#%d Layer %d Facet %d (%f|%f) [Edge %d] -> (%f|%f) [Edge %d] Connects To %d Optimized Index: %d)", i, slicedEdges[i].layerIndex, slicedEdges[i].parentFacet,
				  slicedEdges[i].startPoint[0], slicedEdges[i].startPoint[1], slicedEdges[i].parentStartEdge,
				  slicedEdges[i].endPoint[0], slicedEdges[i].endPoint[1], slicedEdges[i].parentEndEdge,
				  slicedEdges[i].connectsTo, slicedEdges[i].optimizedIndex);
#endif	
		
	#pragma mark Step 6: Inset the Loops

	#ifdef __DEBUG__
		NSDate* startOfInsetLoops = [NSDate date];
	#endif
		
		// 
		// Create buffer for the endresults
		//
		
		// This array holds all sliced points
		insetLoops = (InsetLoopCorner*)malloc(totalCorners*kInsetLoopCornerSize);
		
		
		// SKALAR:
		float inset = extrusionWidth;
		
	//	char* facetPtr = (char*)firstFacet(stl);
	//	for(unsigned int globalId = 0; globalId< totalSlices;globalId++)
	//	{
	//	
	//		SlicedEdge thisEdge = slicedEdges[globalId];
	//		
	//		if(thisEdge.connectsTo!=(unsigned int)-1)
	//		{
	//			SlicedEdge nextEdge= slicedEdges[thisEdge.connectsTo];
	//
	//			Vector2d* thisEdgeNormal2d = [[Vector2d alloc] initWithX:thisEdge.normal[0] Y:thisEdge.normal[1]];
	//			Vector2d* thisEndPoint = [[Vector2d alloc] initWithX:thisEdge.endPoint[0] Y:thisEdge.endPoint[1]];
	//
	//			Vector2d* nextEdgeNormal2d = [[Vector2d alloc] initWithX:nextEdge.normal[0] Y:nextEdge.normal[1]];
	//			
	//			// Calculate the cornerpoint's normal
	//			
	//			float rad1 = atan2(thisEdgeNormal2d.y, thisEdgeNormal2d.x);
	//			float rad2 = atan2(nextEdgeNormal2d.y, nextEdgeNormal2d.x);
	//			float drad1=rad1-rad2;
	//			if(drad1<0.) drad1+=2.*M_PI;
	//			float drad2=rad2-rad1;
	//			if(drad2<0.) drad2+=2.*M_PI;		
	//			float prad;
	//			if(fabsf(drad1)>fabsf(drad2))
	//				prad = rad1+drad2/2.;
	//			else
	//				prad = rad2+drad1/2.;
	//		//	NSLog(@"#%d this: %1.1f° next: %1.1f° Sum: %1.1f°",globalId, rad1/M_PI*180., rad2/M_PI*180., prad/M_PI*180.);
	//			
	//			insetLoops[globalId].normal[0] = cosf(prad);
	//			insetLoops[globalId].normal[1] = sinf(prad);
	//
	//			insetLoops[globalId].point[0] = thisEndPoint.x-insetLoops[globalId].normal[0]*inset;
	//			insetLoops[globalId].point[1] = thisEndPoint.y-insetLoops[globalId].normal[1]*inset;
	//		}
	//		else
	//		{
	//			insetLoops[globalId].point[0] = -INFINITY;
	//			insetLoops[globalId].point[1] = -INFINITY;
	//			insetLoops[globalId].normal[0] = 0.;
	//			insetLoops[globalId].normal[1] = 0.;
	//		}
	//	}

	//	for(int i=0; i< totalSlices;i++)
	//		PSLog(@"Slice",5,@"Inset Corner: #%d P(%1.2f|%1.2f) N(%1.2f|%1.2f)", i, insetLoops[i].point[0], insetLoops[i].point[1], insetLoops[i].normal[0], insetLoops[i].normal[1]);
	//
	//	NSInteger ln=0;
	//	for(PSMutableIntegerArray* loop in layerLoops)
	//	{
	//		PSLog(@"Slice",5,@"Loop %d: %@",ln++, [loop description]);
	//	}
		
		// OpenCL:
		cl_mem outInsetLoops;
		outInsetLoops = clCreateBuffer(context, CL_MEM_WRITE_ONLY|CL_MEM_USE_HOST_PTR, totalCorners*kInsetLoopCornerSize,insetLoops, &err);
		if (!outInsetLoops || err!=CL_SUCCESS)
		{
			PSErrorLog(@"clCreateBuffer outInsetLoops failed: %d",err);
			return nil;
		}
		
		err = clEnqueueWriteBuffer(queue, inOutSlicedEdges, CL_FALSE, 0, totalSlices*kSlicedEdgeSize, slicedEdges, 0, NULL, NULL);
		if (err != CL_SUCCESS) {
			PSErrorLog(@"clEnqueueWriteBuffer inOutSlicedEdges returns %d",err);
			return nil;
		}
				
		err = clSetKernelArg(kernelInsetLoop, 0, sizeof(cl_mem), &outInsetLoops);
		err = clSetKernelArg(kernelInsetLoop, 1, sizeof(cl_mem), &inOutSlicedEdges);
		err |= clSetKernelArg(kernelInsetLoop, 2, sizeof(float), &extrusionWidth);
		if (err != CL_SUCCESS)
		{
			PSErrorLog(@"clSetKernelArg kernelInsetLoop returns %d",err);
			return nil;
		}		
		global = totalSlices;
		err = clEnqueueNDRangeKernel(queue, kernelInsetLoop, 1, NULL, &global, NULL, 0, NULL, NULL);
		if (err)
		{
			PSErrorLog(@"clEnqueueNDRangeKernel kernelInsetLoop returns %d",err);
			return nil;
		}
		
		err = clEnqueueReadBuffer( queue, outInsetLoops, CL_TRUE, 0, totalCorners*kInsetLoopCornerSize, insetLoops, 0, NULL, NULL );  
		if (err != CL_SUCCESS)
		{
			PSErrorLog(@"clEnqueueReadBuffer kernelInsetLoop returns %d",err);
			return nil;
		}
			
		PSLog(@"Timer",5,@"Time for executing OpenCL kernel InsetLoop: %1.2f", -[startOfInsetLoops timeIntervalSinceNow]);
	#if __verbose
		PSLog(@"Slice",5,@"Inset Loop Corners:");
		for(int i = 0; i<totalCorners; i++)
			PSLog(@"Slice",5,@"Corner (%f|%f) Normal (%f|%f)", insetLoops[i].point[0], insetLoops[i].point[1], insetLoops[i].normal[0], insetLoops[i].normal[1]);
	#endif	
		
		
	#pragma mark Cleaning up

		clReleaseMemObject(inOutSlicedEdges);
		clReleaseMemObject(inOutSlicedEdgeLayerReferences);
		clReleaseMemObject(inputFacets);
		clReleaseMemObject(inOutLineOffsets);
		clReleaseMemObject(outInsetLoops);
		free(lineOffsets);
		free(slicedEdgeLayerReferences);
		free(layerCatalog);
		free(slicedEdges);
		// insetLoops will be free'd by the following NSData object when done...
	}
	
	P3DLoops* result = nil;
	if(insetLoops)
	{
		result = [[P3DLoops alloc] initWithLoopCornerData:[[NSData alloc] initWithBytesNoCopy:insetLoops length:totalCorners*kInsetLoopCornerSize freeWhenDone:YES]];
		result.layers = layerLoops;
		result.extrusionHeight = extrusionHeight;
		result.extrusionWidth = extrusionWidth;
		result.cornerMaximum = indexedModel.stlModel.cornerMaximum;
		result.cornerMinimum = indexedModel.stlModel.cornerMinimum;
	}
		
	return result;
}	
@end