forked from DocOtak/not_tanaka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Course.m
122 lines (102 loc) · 2.78 KB
/
Course.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
//
// Course.m
// nottanaka
//
// Created by Alex Dodge on 8/4/10.
// Copyright 2010 Roke Foundries. All rights reserved.
//
#import "Course.h"
/*
typedef struct {
char type;
float x,y,z;
float dx,dy,dz;
float r,a;
} TanakaCourseSegment;
*/
@implementation Course
@synthesize segments,nsegments,totalDistance;
-(id) init {
self = [super init];
nsegments = 0;
segments = malloc(0);
return self;
}
-(int) getSegmentIdForDistance:(float)distance{
for(int i=0;i<nsegments;i++){
if(segments[i].dfromstart > distance) return i;
}
return nsegments-1;
}
-(void) circularTrackWithRadius:(float)radius andSegments:(int)n {
free(segments);
nsegments = n;
segments = malloc(sizeof(TanakaCourseSegment)*n);
for(int i=0;i<n;i++){
TanakaCourseSegment seg;
seg.type = 0;
seg.y = seg.dy = 0;
seg.x = cos(((float)i/n)*2*M_PI)*radius;
seg.z = sin(((float)i/n)*2*M_PI)*radius;
seg.dx = -sin(((float)i/n)*2*M_PI);
seg.dz = cos(((float)i/n)*2*M_PI);
seg.r = 3;
seg.a = 0;
segments[i] = seg;
}
[self calculateMatrices];
[self calculateDistances];
}
float * matrixFromHeadingPosition(vector heading, vector position){
float * matrix = malloc(sizeof(float)*16);
memset(matrix, 0, sizeof(float)*15);
matrix[15] = 1;
vector right = normalize(cross(cvector(0,1,0), heading));
vector backwards = normalize(cross(right, cvector(0,1,0)));
vector up = cross(backwards, right);
memcpy(&matrix[0], &right, sizeof(vector));
memcpy(&matrix[4], &up, sizeof(vector));
memcpy(&matrix[8], &backwards, sizeof(vector));
memcpy(&matrix[12], &position, sizeof(vector));
return matrix;
}
-(void) calculateMatrices {
for(int i=0;i<nsegments;i++){
TanakaCourseSegment * s = &segments[i];
float * matrix = matrixFromHeadingPosition(cvector(s->dx, s->dy, s->dz), cvector(s->x, s->y, s->z));
memcpy(s->matrix, matrix, sizeof(float)*16);
if(i != nsegments-1){
TanakaCourseSegment * s2 = &segments[i+1];
float * facingmatrix = matrixFromHeadingPosition(
normalize(sub(
cvector(s->x, s->y, s->z),
cvector(s2->x, s2->y, s2->z)
)),
cvector(s->x, s->y, s->z));
memcpy(s->facingmatrix, facingmatrix, sizeof(float)*16);
free(facingmatrix);
}else{
memcpy(s->facingmatrix, matrix, sizeof(float)*16);
}
free(matrix);
}
}
-(void) calculateDistances {
float d = 0;
for(int i=0;i<nsegments-1;i++){
TanakaCourseSegment * s1 = &segments[i];
TanakaCourseSegment * s2 = &segments[i+1];
float dx = s2->x - s1->x;
float dy = s2->y - s1->y;
float dz = s2->z - s1->z;
s1->d = sqrt(dx*dx+dy*dy+dz*dz);
s1->dfromstart = d;
d += s1->d;
}
segments[nsegments-1].d = 0;
segments[nsegments-1].dfromstart = totalDistance = d;
}
-(TanakaCourseSegment*) segment:(int)i {
return &segments[i];
}
@end