forked from pixmeo/osirix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOSIGeometry.m
130 lines (99 loc) · 3.59 KB
/
OSIGeometry.m
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
/*=========================================================================
Program: OsiriX
Copyright (c) OsiriX Team
All rights reserved.
Distributed under GNU - LGPL
See http://www.osirix-viewer.com/copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.
=========================================================================*/
#import "OSIGeometry.h"
OSISlab OSISlabMake(N3Plane plane, CGFloat thickness)
{
OSISlab slab;
slab.plane = plane;
slab.thickness = thickness;
return slab;
}
bool OSISlabEqualTo(OSISlab slab1, OSISlab slab2)
{
return N3PlaneEqualToPlane(slab1.plane, slab2.plane) && slab1.thickness == slab2.thickness;
}
bool OSISlabIsCoincidentToSlab(OSISlab slab1, OSISlab slab2)
{
return N3PlaneIsCoincidentToPlane(slab1.plane, slab2.plane) && slab1.thickness == slab2.thickness;
}
bool OSISlabContainsVector(OSISlab slab, N3Vector vector)
{
return N3VectorDistanceToPlane(vector, slab.plane) - slab.thickness/2.0 <= (CGFLOAT_MIN * 1E5);
}
bool OSISlabContainsPlane(OSISlab slab, N3Plane plane)
{
return N3PlaneIsParallelToPlane(slab.plane, plane) && OSISlabContainsVector(slab, plane.point);
}
OSISlab OSISlabApplyTransform(OSISlab slab, N3AffineTransform transform)
{
OSISlab transformedSlab;
N3Vector topPoint;
N3Vector transformedTopPoint;
topPoint = N3VectorAdd(slab.plane.point, N3VectorScalarMultiply(N3VectorNormalize(slab.plane.normal), slab.thickness));
transformedTopPoint = N3VectorApplyTransform(topPoint, transform);
transformedSlab.plane = N3PlaneApplyTransform(slab.plane, transform);
transformedSlab.thickness = N3VectorDistance(transformedSlab.plane.point, transformedTopPoint);
return transformedSlab;
}
NSString *NSStringFromOSISlab(OSISlab slab)
{
return [NSString stringWithFormat:@"{%@, %f}", NSStringFromN3Plane(slab.plane), slab.thickness];
}
CFDictionaryRef OSISlabCreateDictionaryRepresentation(OSISlab slab)
{
CFDictionaryRef planeDict;
NSNumber *thicknessNumber;
CFDictionaryRef slabDict;
planeDict = N3PlaneCreateDictionaryRepresentation(slab.plane);
thicknessNumber = [NSNumber numberWithDouble:slab.thickness];
slabDict = (CFDictionaryRef)[[NSDictionary alloc] initWithObjectsAndKeys:(id)planeDict, @"plane", thicknessNumber, @"thickness", nil];
CFRelease(planeDict);
return slabDict;
}
bool OSISlabMakeWithDictionaryRepresentation(CFDictionaryRef dict, OSISlab *slab)
{
OSISlab tempSlab;
CFDictionaryRef planeDict;
CFNumberRef thicknessNumber;
if (dict == NULL) {
return false;
}
planeDict = CFDictionaryGetValue(dict, @"plane");
thicknessNumber = CFDictionaryGetValue(dict, @"thickness");
if (planeDict == NULL || CFGetTypeID(planeDict) != CFDictionaryGetTypeID() ||
thicknessNumber == NULL || CFGetTypeID(thicknessNumber) != CFNumberGetTypeID()) {
return false;
}
if (N3PlaneMakeWithDictionaryRepresentation(planeDict, &(tempSlab.plane)) == false) {
return false;
}
CFNumberGetValue(thicknessNumber, kCFNumberCGFloatType, &(tempSlab.thickness));
// if (CFNumberGetValue(thicknessNumber, kCFNumberCGFloatType, &(tempSlab.thickness)) == false) {
// return false;
// }
if (slab) {
*slab = tempSlab;
}
return true;
}
@implementation NSValue (OSIGeometryAdditions)
+ (NSValue *)valueWithOSISlab:(OSISlab)slab
{
return [NSValue valueWithBytes:&slab objCType:@encode(OSISlab)];
}
- (OSISlab)OSISlabValue
{
OSISlab slab;
assert(strcmp([self objCType], @encode(OSISlab)) == 0);
[self getValue:&slab];
return slab;
}
@end