File: AdunEllipsoidBox.m

package info (click to toggle)
adun.app 0.8.2-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 6,824 kB
  • ctags: 713
  • sloc: objc: 49,683; ansic: 4,680; sh: 523; python: 79; makefile: 67; cpp: 33
file content (227 lines) | stat: -rw-r--r-- 4,891 bytes parent folder | download | duplicates (8)
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
#include "AdunKernel/AdunEllipsoidBox.h"

@implementation AdEllipsoidBox

- (void) _initialiseDependants
{
	id obj1, obj2, obj3;

	ellipsoidVolume = 4*M_PI*axisALength*axisBLength*axisCLength/3;
	
	obj1 = [NSArray arrayWithObjects:
			[NSNumber numberWithDouble: axisALength], 	
			[NSNumber numberWithDouble: -axisALength],
			nil]; 	
	obj2 = [NSArray arrayWithObjects:
			[NSNumber numberWithDouble: axisBLength], 	
			[NSNumber numberWithDouble: -axisBLength],
			nil]; 	
	obj3 = [NSArray arrayWithObjects:
			[NSNumber numberWithDouble: axisCLength],
			[NSNumber numberWithDouble: -axisCLength],
			nil]; 	

	[ellipsoidExtremes release];
	ellipsoidExtremes = [NSArray arrayWithObjects: obj1, obj2, obj3, nil];
	[ellipsoidExtremes retain];

	NSDebugLLog(@"AdEllipsoidBox", @"Ellispoid Volume %lf. Elipsoid Extremes %@.",
				 ellipsoidVolume, ellipsoidExtremes);
}

- (id) init
{
	return [self initWithALength: 10.0
		bLength: 10.0
		cLength: 10.0];
}		

- (id) initWithALength: (double) l1
	bLength: (double) l2
	cLength: (double) l3;
{
	return [self initWithCavityCentre: nil
		aLength: l1
		bLength: l2
		cLength: l3];
}

- (id) initWithCavityCentre: (NSArray*) array
	aLength: (double) l1
	bLength: (double) l2
	cLength: (double) l3;
{
	if((self = [super init]))
	{
		if(l1 <= 0)
			[NSException raise: NSInvalidArgumentException
				format: @"A axis length must be greater than 0"];
		else if(l2 <= 0)
			[NSException raise: NSInvalidArgumentException
				format: @"B axis length must be greater than 0"];
		else if(l3 <= 0)
			[NSException raise: NSInvalidArgumentException
				format: @"C axis length must be greater than 0"];
				
		axisALength = l1;
		axisBLength = l2;
		axisCLength = l3;
		ellipsoidExtremes = nil;
		
		if(array == nil)
		{
			centre = [NSMutableArray arrayWithObjects:
					 [NSNumber numberWithDouble: 0.0],
					 [NSNumber numberWithDouble: 0.0],
					 [NSNumber numberWithDouble: 0.0],
					 nil];
		}
		else
			centre = array;

		[centre retain];
		ellipsoidCentre = Ad3DVectorFromNSArray(centre);
		
		[self _initialiseDependants];
	}

	return self;
}

- (void) dealloc
{
	[centre release];
	[ellipsoidExtremes release];
	[super dealloc];
}

- (double) cavityVolume
{
	return ellipsoidVolume;
}

- (BOOL) isPointInCavity: (double*) point
{
	int j;
	double check;
	Vector3D separation;
	
	//return yes if its in the ellipsoid otherwise no

	for(j=0; j<3; j++)
		separation.vector[j] = point[j] - ellipsoidCentre.vector[j];

	check = pow(separation.vector[0], 2)/(axisALength*axisALength);
	check += pow(separation.vector[1], 2)/(axisBLength*axisBLength);
	check += pow(separation.vector[2], 2)/(axisCLength*axisCLength);

	if(check <= 1)
		return YES;
	
	return NO;
}

- (Vector3D*) cavityCentre
{
	return &ellipsoidCentre;
}

- (void) setCavityCentre: (NSArray*) array
{
	Vector3D newCentre;

	newCentre = Ad3DVectorFromNSArray(array);
	[centre release];
	centre = [array retain];
	ellipsoidCentre = newCentre;
}

- (NSArray*) cavityExtremes
{
	return ellipsoidExtremes;
}

- (double) aLength
{
	return axisALength;
}

- (void) setALength: (double) value
{
	if(value <= 0)
		[NSException raise: NSInvalidArgumentException
			format: @"Ellipsoid axis must be greater than 0"];

	axisALength = value;
	[self _initialiseDependants];
}

- (double) bLength
{

	return axisBLength;
}

- (void) setBLength: (double) value
{
	if(value <= 0)
		[NSException raise: NSInvalidArgumentException
			format: @"Ellipsoid axis must be greater than 0"];

	axisBLength = value;
	[self _initialiseDependants];
}

- (double) cLength
{
	return axisCLength;
}

- (void) setCLength: (double) value
{
	if(value <= 0)
		[NSException raise: NSInvalidArgumentException
			format: @"Ellipsoid axis must be greater than 0"];

	axisCLength = value;
	[self _initialiseDependants];
}

- (id) initWithCoder: (NSCoder*) decoder
{
	if([decoder allowsKeyedCoding])
	{
		centre = [decoder decodeObjectForKey: @"Centre"];
		axisALength = [decoder decodeDoubleForKey: @"AxisALength"];
		axisBLength = [decoder decodeDoubleForKey: @"AxisBLength"];
		axisCLength = [decoder decodeDoubleForKey: @"AxisCLength"];
		[centre retain];
		
		ellipsoidCentre = Ad3DVectorFromNSArray(centre);
		[self _initialiseDependants];
	}
	else
		[NSException raise: NSInvalidArgumentException 
			format: @"%@ does not support non keyed coding", [self classDescription]];

	return self;
}

- (void) encodeWithCoder: (NSCoder*) encoder
{

	if([encoder allowsKeyedCoding])
	{
		NSDebugLLog(@"Encode", @"Encoding %@", [self description]);
		[encoder encodeDouble: axisALength forKey: @"AxisALength"];
		[encoder encodeDouble: axisBLength forKey: @"AxisBLength"];
		[encoder encodeDouble: axisCLength forKey: @"AxisCLength"];
		[encoder encodeObject: centre forKey: @"Centre"];
	}
	else
		[NSException raise: NSInvalidArgumentException
			format: @"%@ class does not support non keyed coding", [self class]];
}


@end