forked from natikgadzhi/XNMaths
-
Notifications
You must be signed in to change notification settings - Fork 1
/
XNVector.m
181 lines (137 loc) · 3.74 KB
/
XNVector.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
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
//
// XNVector.m
// Assignation 3.2
//
// XNVector class
// N-dimentional vector of double.
//
// Created by Нат Гаджибалаев on 15.11.09.
// Copyright 2009 Нат Гаджибалаев. All rights reserved.
//
#pragma mark -
#pragma mark Imports
#import "XNVector.h"
#pragma mark -
#pragma mark XNVector class implementation
@implementation XNVector
@synthesize capacity;
#pragma mark -
#pragma mark Class init methods
+ (XNVector *) vectorWithMutableArray: (NSMutableArray *) vertices
{
return [[XNVector alloc] initWithMutableArray: vertices];
}
+ (XNVector *) vectorWithCapacity: (NSUInteger) newCapacity filledWith: (CGFloat *) newCArray
{
return [[XNVector alloc] initWithCapacity: newCapacity filledWith: newCArray];
}
+ (XNVector *) vectorWithCapacity: (NSUInteger) newCapacity
{
return [[XNVector alloc] initWithCapacity: newCapacity];
}
#pragma mark -
#pragma mark Instance initialization methods
// Init with just a capacity (really just allocates)
- (XNVector *) initWithCapacity: (NSUInteger) newCapacity
{
// init
self = [super init];
// copy capacity
capacity = newCapacity;
// allocate data
data = calloc(capacity, sizeof( CGFloat ));
// clear data
for( NSUInteger i = 0; i < capacity; i++){
data[i] = 0;
}
return self;
}
- (XNVector *) initWithCapacity: (NSUInteger) newCapacity filledWith: (CGFloat *) newCArray
{
// init
self = [super init];
// copy capacity
capacity = newCapacity;
// allocate data
data = calloc(capacity, sizeof( CGFloat ));
// copy data
for( NSUInteger i = 0; i < capacity; i++){
data[i] = newCArray[i];
}
return self;
}
// Initilize the vector with array of double
- (XNVector *) initWithMutableArray: (NSMutableArray *) vertices
{
self = [self initWithCapacity: vertices.count];
for( NSUInteger i = 0; i < vertices.count; i++){
[self setValue: [[vertices objectAtIndex: i] floatValue] atIndex: i];
}
return self;
}
- (XNVector *) substract: (XNVector *) vector
{
if( capacity != vector.capacity){
[NSException raise: @"Vector operations error." format: @"Can't substract [%d] vector from [%d] vector.", vector.capacity, capacity];
}
XNVector *result = [[XNVector alloc] initWithCapacity: capacity];
for( NSInteger i = 0; i < capacity; i++){
[result setValue: [self valueAtIndex: i] - [vector valueAtIndex: i] atIndex: i];
}
return [result autorelease];
}
- (CGFloat) norm
{
CGFloat norm = 0.0f;
for( NSInteger i = 0; i < capacity; i++ ){
norm += powf([self valueAtIndex: i], 2);
}
return sqrtf(norm);
}
#pragma mark -
#pragma mark Getters and setters
- (CGFloat) valueAtIndex: (NSUInteger) index
{
if( index >= capacity ){
[NSException raise:@"Trying to get not existing vertice from vector"
format:@"Trying to get vertice at index %d", index];
}
return data[index];
}
- (void) setValue: (CGFloat) value atIndex: (NSUInteger) index
{
if( index >= capacity ){
[NSException raise:@"Trying to get not existing vertice from vector"
format:@"Trying to get vertice at index %d", index];
}
data[index] = value;
}
- (XNVector *) copy
{
XNVector *copiedVector = [[XNVector alloc] initWithCapacity: capacity];
for( NSInteger i = 0; i < capacity; i++){
[copiedVector setValue: [self valueAtIndex:i] atIndex: i];
}
return copiedVector;
}
#pragma mark -
#pragma mark Debuging
- (void) printToLog
{
NSLog(@"Vector if %d elements", capacity);
NSMutableString *logFormatForRow = [NSMutableString stringWithCapacity: 30];
for( NSInteger i = 0; i < capacity; i++ ){
[logFormatForRow appendString: [NSMutableString stringWithFormat: @"%1.2f, ", data[i]]];
}
NSLog(@"[%@]", logFormatForRow);
}
#pragma mark -
#pragma mark Dealloc
- (void) dealloc
{
// free the vertices array.
free(data);
data = NULL;
[super dealloc];
}
@end