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
|
#include "AdunKernel/AdunSphericalBox.h"
@implementation AdSphericalBox
- (void) _initialiseDependants
{
id obj;
sphereVolume = 4*M_PI*pow(sphereRadius, 3)/3;
obj = [NSArray arrayWithObjects:
[NSNumber numberWithDouble: sphereRadius],
[NSNumber numberWithDouble: -sphereRadius],
nil];
[sphereExtremes release];
sphereExtremes = [NSArray arrayWithObjects: obj, [obj copy], [obj copy], nil];
[sphereExtremes retain];
NSDebugLLog(@"SphericalBox", @"SphereVolume %lf. SphereExtremes %@.",
sphereVolume, sphereExtremes);
}
- (id) init
{
return [self initWithRadius: 10.0];
}
- (id) initWithRadius: (double) rad
{
return [self initWithCavityCentre: nil
radius: rad];
}
- (id) initWithCavityCentre: (NSArray*) array
radius: (double) rad
{
if((self = [super init]))
{
if(rad <= 0)
[NSException raise: NSInvalidArgumentException
format: @"Radius must be greater than 0"];
sphereRadius = rad;
sphereExtremes = 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];
sphereCentre = Ad3DVectorFromNSArray(centre);
[self _initialiseDependants];
}
return self;
}
- (void) dealloc
{
[centre release];
[sphereExtremes release];
[super dealloc];
}
- (NSString*) description
{
NSMutableString* description = [NSMutableString string];
[description appendFormat: @"%@. Radius: %8.3lf. Centre: (%8.3lf, %8.3lf %8.3lf)\n",
NSStringFromClass([self class]), sphereRadius,
sphereCentre.vector[0], sphereCentre.vector[1], sphereCentre.vector[2]];
return description;
}
- (double) cavityVolume
{
return sphereVolume;
}
- (BOOL) isPointInCavity: (double*) point
{
int j;
Vector3D seperation;
//return yes if its in the sphere otherwise no
for(j=0; j<3; j++)
seperation.vector[j] = point[j] - sphereCentre.vector[j];
Ad3DVectorLength(&seperation);
if(seperation.length < sphereRadius)
return YES;
return NO;
}
- (Vector3D*) cavityCentre
{
return &sphereCentre;
}
- (void) setCavityCentre: (NSArray*) array
{
Vector3D newCentre;
newCentre = Ad3DVectorFromNSArray(array);
[centre release];
centre = [array retain];
sphereCentre = newCentre;
}
- (NSArray*) cavityExtremes
{
return sphereExtremes;
}
- (double) radius
{
return sphereRadius;
}
- (void) setRadius: (double) value
{
if(value <= 0)
[NSException raise: NSInvalidArgumentException
format: @"Sphere radius must be greater than 0"];
sphereRadius = value;
[self _initialiseDependants];
}
- (id) initWithCoder: (NSCoder*) decoder
{
if([decoder allowsKeyedCoding])
{
centre = [decoder decodeObjectForKey: @"Centre"];
sphereRadius = [decoder decodeDoubleForKey: @"SphereRadius"];
[centre retain];
sphereCentre = 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: sphereRadius forKey: @"SphereRadius"];
[encoder encodeObject: centre forKey: @"Centre"];
}
else
[NSException raise: NSInvalidArgumentException
format: @"%@ class does not support non keyed coding", [self class]];
}
@end
|