-
Notifications
You must be signed in to change notification settings - Fork 0
/
FeatureView.m
1610 lines (1425 loc) · 48.9 KB
/
FeatureView.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// FeatureView.m
//
// Created by Grogee on 9/24/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "FeatureView.h"
#import "readFeature.h"
@implementation FeatureView
@synthesize indexset;
@synthesize highlightedPoints,highlightedClusterPoints;
@synthesize showFrame;
-(BOOL) acceptsFirstResponder
{
return YES;
}
-(void) awakeFromNib
{
minmax = calloc(6,sizeof(float));
minmax[0] = -1;
minmax[1] = 1;
minmax[2] = -1;
minmax[3] = 1;
minmax[4] = -1;
minmax[5] = 1;
rotatex = 0.0;
rotatey = 0.0;
rotatez = 0.0;
originx = 0.0;
originy = 0.0;
originz = -2.0;
scale = 1.0;
base_color[0] = 1.0f;
base_color[1] = 0.85f;
base_color[2] = 0.35f;
drawAxesLabels = NO;
appendHighlights = NO;
NSOpenGLPixelFormatAttribute attrs[] =
{
NSOpenGLPFAAllRenderers,YES,
NSOpenGLPFAColorSize, 24,
NSOpenGLPFAAlphaSize, 8,
NSOpenGLPFADepthSize, 16,
};
//_pixelFormat =[[[NSOpenGLPixelFormat alloc] initWithAttributes:attrs] retain];
/*_pixelFormat = [[FeatureView defaultPixelFormat] retain];
[[NSNotificationCenter defaultCenter] addObserver: self
selector:@selector(_surfaceNeedsUpdate:)
name: NSViewGlobalFrameDidChangeNotification object: self];
*/
//[self setOpenGLContext: [[NSOpenGLContext alloc] initWithFormat:[NSOpenGLView defaultPixelFormat] shareContext:nil]];
//register for defaults updates
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotification:) name:NSUserDefaultsDidChangeNotification object:nil];
picked = NO;
selectedClusters = [[NSMutableArray arrayWithCapacity:10] retain];
}
- (id) initWithFrame:(NSRect)frameRect
{
/*self = [super initWithFrame:frameRect];
if(self == nil)
{
return nil;
}
/*[self setOpenGLContext: [[NSOpenGLContext alloc] initWithFormat:[NSOpenGLView defaultPixelFormat] shareContext:nil]];
[[self openGLContext] makeCurrentContext];*/
return [self initWithFrame:frameRect pixelFormat: [FeatureView defaultPixelFormat]];
}
-(id) initWithFrame:(NSRect)frameRect pixelFormat:(NSOpenGLPixelFormat*)format
{
self = [super initWithFrame:frameRect];
if( self != nil)
{
_pixelFormat = [format retain];
[self setOpenGLContext: [[NSOpenGLContext alloc] initWithFormat:format shareContext:nil]];
[[self openGLContext] makeCurrentContext];
[[NSNotificationCenter defaultCenter] addObserver: self
selector:@selector(_surfaceNeedsUpdate:)
name: NSViewGlobalFrameDidChangeNotification object: self];
//receive notification about change in highlight
}
return self;
}
-(void) _surfaceNeedsUpdate:(NSNotification*)notification
{
[self reshape];
[self update];
}
-(void) lockFocus
{
NSOpenGLContext *context = [self openGLContext];
[super lockFocus];
if( [context view] != self )
{
[context setView:self];
}
[context makeCurrentContext];
}
-(BOOL) isOpaque
{
return YES;
}
-(void) setOpenGLContext:(NSOpenGLContext *)context
{
_oglContext = [context retain];
}
+(NSOpenGLPixelFormat*) defaultPixelFormat
{
NSOpenGLPixelFormatAttribute attrs[] =
{
NSOpenGLPFAAllRenderers,YES,
NSOpenGLPFADoubleBuffer, YES,
NSOpenGLPFAColorSize, 24,
NSOpenGLPFAAlphaSize, 8,
NSOpenGLPFADepthSize, 32,
0
};
NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
return pixelFormat;
//return [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
}
-(NSOpenGLContext*)openGLContext
{
return _oglContext;
}
-(void)clearGLContext
{
[[self openGLContext] makeCurrentContext];
[NSOpenGLContext clearCurrentContext];
//[[self openGLContext] release];
}
-(void)setPixelFormat:(NSOpenGLPixelFormat *)pixelFormat
{
_pixelFormat = [pixelFormat retain];
}
-(NSOpenGLPixelFormat*)pixelFormat
{
return _pixelFormat;
}
-(void)update
{
if( [[self openGLContext] view] == self)
{
[[self openGLContext] update];
}
}
- (void) loadVertices: (NSURL*)url
{
char *path = [[url path] cStringUsingEncoding:NSASCIIStringEncoding];
header H;
//H = *readFeatureHeader("../../test2.hdf5", &H);
H = *readFeatureHeader(path, &H);
//check if vertices has already been allocated
vertices = malloc(H.rows*H.cols*sizeof(GLfloat));
//vertices = readFeatureFile("../../test2.hdf5", vertices);
vertices = readFeatureFile(path, vertices);
minmax = realloc(minmax,2*H.cols*sizeof(float));
int c;
for(c=0;c<H.cols;c++)
{
minmax[2*c] = -1;
minmax[2*c+1] = 1;
}
//minmax = getMinMax(minmax, vertices, H.rows, H.cols);
draw_dims[0] = 0;
draw_dims[1] = 1;
draw_dims[2] = 3;
rows = H.rows;
cols = H.cols;
//cluster indices
//cids = malloc((rows+1)*sizeof(unsigned int));
///cids = readClusterIds("../../a101g0001waveforms.clu.1", cids);
//use_colors = malloc(cids[0]*3*sizeof(GLfloat));
//create colors to use; highly simplistic, need to change this, but let's use it for now
/*
for(c=0;c<cids[0];c++)
{
use_colors[3*c] = ((float)rand())/RAND_MAX;
use_colors[3*c + 1] = ((float)rand())/RAND_MAX;
use_colors[3*c + 2] = ((float)rand())/RAND_MAX;
}
*/
ndraw_dims = 3;
nindices = rows;
NSRange range;
range.location = 0;
range.length = rows;
indexset = [[NSMutableIndexSet indexSetWithIndexesInRange:range] retain];
use_vertices = malloc(rows*ndraw_dims*sizeof(GLfloat));
indices = malloc(nindices*sizeof(GLuint));
colors = malloc(nindices*3*sizeof(GLfloat));
[[self openGLContext] makeCurrentContext];
modifyVertices(use_vertices);
modifyIndices(indices);
modifyColors(colors);
pushVertices();
[self setNeedsDisplay:YES];
//[self selectDimensions];
}
-(void) createVertices: (NSData*)vertex_data withRows: (NSUInteger)r andColumns: (NSUInteger)c
{
rows = r;
cols = c;
nindices = rows;
//create an indexset
NSRange rng;
rng.location = 0;
rng.length = rows;
indexset = [[NSMutableIndexSet indexSetWithIndexesInRange:rng] retain];
if(dataloaded)
{
//data has already been loaded, i.e. we are requesting to draw another set of features
dataloaded = NO;
free(vertices);
free(use_vertices);
free(indices);
free(colors);
}
vertices = malloc(rows*cols*sizeof(GLfloat));
[vertex_data getBytes:vertices length: rows*cols*sizeof(float)];
minmax = realloc(minmax,2*cols*sizeof(float));
int cl;
//this only works if we have rescaled the axis
for(cl=0;cl<cols;cl++)
{
minmax[2*cl] = -1.5;
minmax[2*cl+1] = 1.5;
}
scale = 1.0;
draw_dims[0] = 0;
draw_dims[1] = 1;
draw_dims[2] = 3;
ndraw_dims = 3;
use_vertices = malloc(rows*ndraw_dims*sizeof(GLfloat));
indices = malloc(nindices*sizeof(GLuint));
colors = malloc(nindices*3*sizeof(GLfloat));
[[self openGLContext] makeCurrentContext];
modifyVertices(use_vertices);
modifyIndices(indices);
modifyColors(colors);
pushVertices();
[self setNeedsDisplay:YES];
}
-(NSData*)getVertexData
{
NSData *data = [NSData dataWithBytesNoCopy:vertices length:rows*cols*sizeof(float)];
return data;
}
-(void) selectDimensions:(NSDictionary*)dims
{
int dim = [[dims valueForKey: @"dim"] intValue];
int f = [[dims valueForKey:@"dim_data"] intValue];
int dim_data = f;
if( ((dim >= 0) && (dim < ndraw_dims)) && ((dim_data>=0) && (dim_data<cols)))
{
draw_dims[dim] = dim_data;
//set openGL viewport based on vertices
//make sure we are in the current context
[[self openGLContext] makeCurrentContext];
//glMatrixMode(GL_PROJECTION);
//glLoadIdentity();
//glOrtho(1.1*minmax[2*draw_dims[0]], 1.1*minmax[2*draw_dims[0]+1], 1.1*minmax[2*draw_dims[1]], 1.1*minmax[2*draw_dims[1]+1], 1.1*minmax[2*draw_dims[2]], 1.1*minmax[2*draw_dims[2]+1]);
//get the data from the GPU
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
GLfloat *vertex_pointer = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
modifyVertices(vertex_pointer);
//compute min/max
/*float max,min,l;
//find max
vDSP_maxv(vertex_pointer,1,&max,rows*3);
//find min
vDSP_minv(vertex_pointer,1,&min,rows*3);
l = max-min;
int j;
for(j=0;j<rows*3;j++)
{
vertex_pointer[j] = 2*(vertex_pointer[j]-min)/l-1;
}*/
//
glUnmapBuffer(GL_ARRAY_BUFFER);
/*
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
GLuint *index_pointer = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER,GL_WRITE_ONLY);
modifyIndices(index_pointer);
glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);*/
[self setNeedsDisplay: TRUE];
}
}
-(void) showCluster: (Cluster *)cluster
{
currentCluster = cluster;
float *_color = (float*)[[cluster color] bytes];
if([selectedClusters containsObject:cluster] == NO)
{
[selectedClusters addObject: cluster];
}
unsigned int new_size = [[cluster npoints] intValue];
/*
if( nindices + new_size > rows )
{
return;
}
*/
[[self openGLContext] makeCurrentContext];
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
GLuint *tmp_indices = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY);
//
GLuint error = glGetError();
const GLbyte *errstr = gluErrorString(error);
//
if(tmp_indices!=NULL)
{
//new_size = [cluster.indices count];
//tmp_indices = realloc(tmp_indices, new_size*sizeof(GLuint));
int j = 0;
[indexset addIndexes: [cluster indices]];
nindices = [indexset count];
NSUInteger *_indices = malloc(nindices*sizeof(NSUInteger));
[indexset getIndexes:_indices maxCount:nindices inIndexRange:nil];
for(j=0;j<nindices;j++)
{
tmp_indices[j] = (unsigned int)_indices[j];
}
free(_indices);
glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
/*for(i=0;i<rows;i++)
{
if(cids[i+1]==cid)
{
tmp_indices[j] = i;
j+=1;
}
}*/
//float *centroid = NSZoneMalloc([self zone], 3*sizeof(float));
unsigned int *points = (unsigned int*)[[cluster points] bytes];
float *cluster_minmax = NSZoneCalloc([self zone], 6,sizeof(float));
for(j=0;j<new_size;j++)
{
//tmp_indices[nindices+j] = points[j];
//compute centroid
//centroid[0] += vertices[points[j]*cols + draw_dims[0]];
//centroid[1] += vertices[points[j]*cols + draw_dims[1]];
//centroid[2] += vertices[points[j]*cols + draw_dims[2]];
//compute min/max
cluster_minmax[0] = MIN(cluster_minmax[0],vertices[points[j]*cols + draw_dims[0]]);
cluster_minmax[1] = MAX(cluster_minmax[1],vertices[points[j]*cols + draw_dims[0]]);
cluster_minmax[2] = MIN(cluster_minmax[2],vertices[points[j]*cols + draw_dims[1]]);
cluster_minmax[3] = MAX(cluster_minmax[3],vertices[points[j]*cols + draw_dims[1]]);
cluster_minmax[4] = MIN(cluster_minmax[4],vertices[points[j]*cols + draw_dims[2]]);
cluster_minmax[5] = MAX(cluster_minmax[5],vertices[points[j]*cols + draw_dims[2]]);
}
//colors
/*
scale = 1.0;
for(j=0;j<3;j++)
{
scale = MAX(scale,1.0/(cluster_minmax[2*j+1]-cluster_minmax[2*j]));
}
scale = 1.0/scale;
*/
//this does not work for a 64 bit application, as NSUInteger is then 64 bit, while the tm_indices is 32 bit.
//int count = [indexset getIndexes:(NSUInteger*)tmp_indices maxCount:nindices+new_size inIndexRange:nil];
//glBufferData(GL_ELEMENT_ARRAY_BUFFER, new_size*sizeof(GLuint), tmp_indices, GL_DYNAMIC_DRAW);
//nindices += new_size;
//normalize
//centroid[0]/=new_size;
//centroid[1]/=new_size;
//centroid[2]/=new_size;
//originx = -centroid[0];
//originy = -centroid[1];
//originz = -centroid[1];
//NSZoneFree([self zone], centroid);
//calculate the centroid of the cluster in the current space
//do colors
[self setClusterColors:_color forIndices:(unsigned int*)[[cluster points] bytes] length:[[cluster npoints] unsignedIntValue]];
//
[self changeZoom];
[self setNeedsDisplay:YES];
}
}
-(void) hideCluster: (Cluster *)cluster
{
//first check it the cluster is even shown
if( [cluster isKindOfClass:[Cluster class]] == NO )
{
return;
}
if( [selectedClusters containsObject:cluster] == NO )
{
//if not, do nothing
return;
}
[selectedClusters removeObject:cluster];
if( [selectedClusters count] >= 1 )
{
currentCluster = [selectedClusters lastObject];
}
unsigned int new_size = [[cluster npoints] intValue];
//first check if hiding this cluster takes away all indices, if so, simply set nindices to 0
if(nindices - new_size ==0)
{
nindices = 0;
[indexset removeAllIndexes];
//also remove highlights
[[self highlightedPoints] setLength:0];
[self setNeedsDisplay:YES];
return;
}
if(new_size>0)
{
[[self openGLContext] makeCurrentContext];
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
GLuint *tmp_indices = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_READ_WRITE);
if( tmp_indices != NULL)
{
unsigned int *points = (unsigned int*)[[cluster points] bytes];
//unsigned int *_points = (unsigned int*)malloc((nindices-new_size)*sizeof(unsigned int));
//new_size = [[cluster indices] count];
int i,j,k,found;
i = 0;
//TODO: The following is a very naiv way of doing intersection. Should fix this
// One way to fix make it more efficient is to make sure the indices are sorted. This can
// be done by maintaining a heap, for instance.
//dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//dispatch_apply(nindices, queue, ^(size_t j)
/*
for(j=0;j<nindices;j++)
{
//int i,k,found;
found = 0;
//i = 0;
for(k=0;k<new_size;k++)
{
if(tmp_indices[j]==points[k])
{
found=1;
//once we've found a match, abort
break;
}
}
if(found==0)
{
indices[i] = tmp_indices[j];
i+=1;
}
}//);
*/
//alternative to the above: Use IndexSets
//NSMutableIndexSet *tmp = [NSMutableIndexSet
[indexset removeIndexes: [cluster indices]];
j = 0;
if( [[self highlightedPoints] length] >0 )
{
//check if the currentl highltighted points belong to this cluster
unsigned int *_points = (unsigned int*)[[self highlightedPoints] bytes];
unsigned int _npoints = [[self highlightedPoints] length]/sizeof(unsigned int);
//new array to hold the higlighted points we keep
unsigned int* newpoints = malloc(_npoints*sizeof(unsigned int ));
for(i=0;i<_npoints;i++)
{
if([ [cluster indices] containsIndex:(NSUInteger)_points[i]] == NO )
{
newpoints[j]=_points[i];
j++;
}
}
[[self highlightedPoints] setData:[NSData dataWithBytes:newpoints length:j*sizeof(unsigned int)]];
free(newpoints);
}
NSRange range;
range.location = 0;
range.length =nindices;
//count = [indexset getIndexes: tmp_indices maxCount: nindices-new_size inIndexRange: nil];
//nindices-=new_size;
//if(nindices<0)
// nindices=0;
nindices = [indexset count];
NSUInteger *_index = malloc(nindices*sizeof(NSUInteger));
[indexset getIndexes: _index maxCount:nindices*sizeof(NSUInteger) inIndexRange:nil];
for(i=0;i<nindices;i++)
{
indices[i] = (unsigned int)_index[i];
tmp_indices[i] = (GLuint) _index[i];
}
//push the new indices
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
//glBufferData(GL_ELEMENT_ARRAY_BUFFER, nindices*sizeof(unsigned int), indices, GL_DYNAMIC_DRAW);
//free(_points);
//reset origin
originx = 0.0;
originy = 0.0;
originz = -2.0;
glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
[self setNeedsDisplay:YES];
}
}
}
-(void) hideAllClusters
{
[selectedClusters removeAllObjects];
nindices = 0;
[indexset removeAllIndexes];
[self setNeedsDisplay:YES];
}
-(void) showAllClusters
{
nindices = rows;
[[self openGLContext] makeCurrentContext];
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
GLuint *index = malloc(rows*sizeof(unsigned int));
//could make this more efficient, e.g. using range
unsigned int i;
for(i=0;i<rows;i++)
{
index[i] = i;
}
NSRange rng;
rng.location = 0;
rng.length = rows;
[indexset addIndexesInRange:rng];
glBufferData(GL_ELEMENT_ARRAY_BUFFER, rows*sizeof(GLuint), index, GL_DYNAMIC_DRAW);
free(index);
[self setNeedsDisplay:YES];
}
-(void) highlightPoints:(NSDictionary*)params inCluster:(Cluster*)cluster
{
//draw selected points in complementary color
NSData *points = [params objectForKey: @"points"];
unsigned int* _points = (unsigned int*)[points bytes];
unsigned int _npoints = (unsigned int)([points length]/sizeof(unsigned int));
int i;
//store the points such that multiple selections will work
NSMutableIndexSet *hcp = [NSMutableIndexSet indexSet];
for(i=0;i<_npoints;i++)
{
[hcp addIndex:(NSUInteger)_points[i]];
}
//[self setHighlightedClusterPoints:points];
[self setHighlightedClusterPoints:hcp];
//NSData *color = [params objectForKey:@"color"];
//need to make this work when cluster is nil
NSData *color;
unsigned int* _clusterPoints;
unsigned int _nclusterPoints = 0;
unsigned int offset = 0;
if( cluster != nil )
{
color = [cluster color];
_clusterPoints = (unsigned int*)[[cluster points] bytes];
_nclusterPoints = [[cluster npoints] unsignedIntValue];
if(_nclusterPoints == 0)
_clusterPoints = NULL;
//we have to determine the offset of the wfidx by finding the index of the cluster in the array of currently selected clusters
NSEnumerator *_clusterEnumerator = [selectedClusters objectEnumerator];
Cluster *_clu = [_clusterEnumerator nextObject];
while( (_clu) && ([_clu isEqualTo:cluster]==NO))
{
offset+=[[_clu npoints] unsignedIntValue];
_clu = [_clusterEnumerator nextObject];
}
}
else
{
color = [NSData dataWithBytes:base_color length:3*sizeof(GLfloat)];
//if no cluster is given, just use an index
_clusterPoints = NULL;
}
//get the indices to redraw
[[self openGLContext] makeCurrentContext];
GLuint *idx = malloc(_npoints*sizeof(GLuint));
if( _clusterPoints != NULL )
{
for(i=0;i<_npoints;i++)
{
//idx[i] = tmp_idx[_clusterPoints[_points[i]]];
idx[i] = _clusterPoints[_points[i]-offset];
//idx[i] = _points[i];
}
}
else
{
//if no cluster is given, just use the raw indiex
for(i=0;i<_npoints;i++)
{
//idx[i] = tmp_idx[_clusterPoints[_points[i]]];
idx[i] = _points[i];
}
}
//glUnmapBuffer(GL_ARRAY_BUFFER);
//GLfloat *_color = (GLfloat*)[color bytes];
glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
GLfloat *_colors = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
//reset the colors before doing the new colors
if( (highlightedPoints != NULL ) && (appendHighlights == NO) )
{
unsigned int* _hpoints = (unsigned int*)[highlightedPoints bytes];
unsigned int _nhpoints = (unsigned int)([highlightedPoints length]/sizeof(unsigned int));
//GLfloat *_bcolor = (GLfloat*)[bcolor bytes];
for(i=0;i<_nhpoints;i++)
{
_colors[_hpoints[i]*3] = 1-_colors[_hpoints[i]*3];
_colors[_hpoints[i]*3+1] = 1-_colors[_hpoints[i]*3+1];
_colors[_hpoints[i]*3+2] =1-_colors[_hpoints[i]*3+2];
}
}
for(i=0;i<_npoints;i++)
{
_colors[idx[i]*3] = 1-_colors[idx[i]*3];
_colors[idx[i]*3+1] = 1-_colors[idx[i]*3+1];
_colors[idx[i]*3+2] = 1- _colors[idx[i]*3+2];
}
glUnmapBuffer(GL_ARRAY_BUFFER);
if( highlightedPoints == NULL)
{
[self setHighlightedPoints: [NSMutableData dataWithBytes: idx length: _npoints*sizeof(GLuint)]];
}
else
{
[[self highlightedPoints] setData: [NSData dataWithBytes: idx length: _npoints*sizeof(GLuint)]];
}
free(idx);
//update the menu
[[[self menu] itemWithTitle:@"Add points to cluster"] setEnabled:YES];
[[[self menu] itemWithTitle:@"Remove points from cluster"] setEnabled:YES];
[self setNeedsDisplay:YES];
}
-(void) rotateY
{
//glRotated(5, 0, 1, 0);
[self setNeedsDisplay:YES];
}
-(void) rotateX
{
//glRotated(5, 1, 0, 0);
[self setNeedsDisplay:YES];
}
-(void) rotateZ
{
//glRotated(5, 0, 0, 1);
[self setNeedsDisplay:YES];
}
-(void) setClusterColors: (GLfloat*)cluster_colors forIndices: (GLuint*)cluster_indices length:(NSUInteger)length
{
glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
GLfloat *tmp_colors = glMapBuffer(GL_ARRAY_BUFFER,GL_WRITE_ONLY);
int i;
if( cluster_indices != NULL)
for(i=0;i<length;i++)
{
tmp_colors[3*cluster_indices[i]] = cluster_colors[0];//[3*i];
tmp_colors[3*cluster_indices[i]+1] = cluster_colors[1];//[3*i+1];
tmp_colors[3*cluster_indices[i]+2] = cluster_colors[2];//[3*i+2];
}
else
{
//if cluster_indices is NULL, assume we are changing everthing
for(i=0;i<rows;i++)
{
tmp_colors[3*i] = cluster_colors[3*i];
tmp_colors[3*i+1] = cluster_colors[3*i+1];
tmp_colors[3*i+2] = cluster_colors[3*i+2];
}
}
//make sure we give the buffer back
glUnmapBuffer(GL_ARRAY_BUFFER);
[self setNeedsDisplay:YES];
}
static void modifyVertices(GLfloat *vertex_data)
{
//create vertices for drawing
int i,j;
//int ndraw_dims = 3;
//nindices = rows;
for(i=0;i<rows;i++)
{
//indices[i] = i;
for(j=0;j<ndraw_dims;j++)
{
//indices[i*ndraw_dims+j] = i*ndraw_dims +j;
vertex_data[i*ndraw_dims+j] = vertices[i*cols+draw_dims[j]];
}
}
}
static void modifyIndices(GLuint *index_data)
{
int i;
for(i=0;i<rows;i++)
{
index_data[i] = i;
}
}
static void modifyColors(GLfloat *color_data)
{
int i;
for(i=0;i<rows;i++)
{
color_data[3*i] = 1.0f;//use_colors[3*cids[i+1]];
color_data[3*i+1] = 0.85f;//use_colors[3*cids[i+1]+1];
color_data[3*i+2] = 0.35f;//use_colors[3*cids[i+1]+2];
}
}
static void pushVertices()
{
//set up index buffer
int k = 0;
//glMatrixMode(GL_PROJECTION);
//glLoadIdentity();
//glOrtho(1.1*minmax[2*draw_dims[0]], 1.1*minmax[2*draw_dims[0]+1], 1.1*minmax[2*draw_dims[1]], 1.1*minmax[2*draw_dims[1]+1], 1.1*minmax[2*draw_dims[2]], 1.1*minmax[2*draw_dims[2]+1]);
//glOrtho(1.1*minmax[2*draw_dims[0]], 1.1*minmax[2*draw_dims[0]+1], 1.1*minmax[2*draw_dims[1]], 1.1*minmax[2*draw_dims[1]+1], 1.1*minmax[2*draw_dims[2]], 1.1*minmax[2*draw_dims[2]+1]);
glGenBuffers(1,&indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, nindices*sizeof(GLuint),indices, GL_DYNAMIC_DRAW);
//generate 1 buffer of type vertexBuffer
glGenBuffers(1,&colorBuffer);
glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
glBufferData(GL_ARRAY_BUFFER, nindices*3*sizeof(GLfloat),colors,GL_DYNAMIC_DRAW);
//bind vertexBuffer
glGenBuffers(1,&vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
//push data to the current buffer
glBufferData(GL_ARRAY_BUFFER, nindices*3*sizeof(GLfloat), use_vertices, GL_DYNAMIC_DRAW);
dataloaded = YES;
}
static void drawFrame()
{
//finally connect the corners
glColor4f(0.5f,0.85f,0.35f,1.0f);
glLineWidth(1.0);
int i;
float d = 0;
for(i=0;i<=10;i++)
{
d = 0.2*i;
glBegin(GL_LINES);
//y
glVertex3f(-.99+d, -.99, -.99);
glVertex3f(-.99+d, 1.01, -.99);
glVertex3f(-.99, -.99, -.99+d);
glVertex3f(-.99, 1.01, -.99+d);
//z
glVertex3f(-.99+d, -.99, -0.99);
glVertex3f(-.99+d, -.99, 1.01);
glVertex3f(-.99, -.99+d, -0.99);
glVertex3f(-.99, -.99+d, 1.01);
//x
glVertex3f(-.99, -0.99, -.99+d);
glVertex3f(1.01, -.99, -.99+d);
glVertex3f(-.99, -.99+d, -0.99);
glVertex3f(1.01, -.99+d, -.99);
/*glVertex3f(1.0, 1.0, 1.0);
glVertex3f(1.0, 1.0, -1.0);*/
glEnd();
}
//glColor4f(0.5f,0.85f,0.35f,0.1f);
//front size
//glBegin(GL_LINE_LOOP);
/*glBegin(GL_QUADS);
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f(1.0,-1.0,1.0);
glVertex3f(1.0,1.0,1.0);
glVertex3f(-1.0,1.0,1.0);
glEnd();*/
//back side
/*
glBegin(GL_QUADS);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f(1.0,-1.0,-1.0);
glVertex3f(1.0,1.0,-1.0);
glVertex3f(-1.0,1.0,-1.0);
glEnd();
//floor
glBegin(GL_QUADS);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f(1.0,-1.0,-1.0);
glVertex3f(1.0,-1.0,1.0);
glVertex3f(-1.0,-1.0,1.0);
glEnd();
//side
glBegin(GL_QUADS);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(-1.0, -1.0, 1.0);
glEnd();*/
}
static void drawAnObject()
{
//glColor3f(1.0f,0.85f,0.35f);
//activate the dynamicbuffer
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glEnableClientState(GL_VERTEX_ARRAY);
//bind the vertexbuffer
//glVertexPointer(2, GL_FLOAT, sizeof(vertexStatic), (void*)offsetof(vertexStatic,position));
//set the vertex pointer
//offsetof
//activate vertex point; 3 points per vertex, each point of type GL_FLOAT, stride of 0 (i.e. tightly pakced), and use existing
//vetex data, i.e. vertexBuffer activated above.
glVertexPointer(3, GL_FLOAT, 0, (void*)0);
glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(3, GL_FLOAT, 0, (void*)0);
//bind the indices
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
//draw eveything for now
//glDrawArrays(GL_POINT, 0, nvertices/3);
//draw using the indices
//Draw nindices elements of type GL_POINTS, use the loaded indexBuffer
glDrawElements(GL_POINTS, nindices, GL_UNSIGNED_INT,(void*)0);
//glDrawRangeElement
}
-(void)drawLabels
{
if( (glabelX == nil ) || (glabelY == nil) || (glabelZ == nil ) )
{
NSMutableDictionary *normal9Attribs = [NSMutableDictionary dictionary];
[normal9Attribs setObject: [NSFont fontWithName: @"Helvetica" size: 12.0f] forKey: NSFontAttributeName];
NSAttributedString *labelX,*labelY,*labelZ;
labelX = [[[NSAttributedString alloc] initWithString:[NSString stringWithFormat: @"Axis 1"] attributes:normal9Attribs] autorelease];
labelY = [[[NSAttributedString alloc] initWithString:[NSString stringWithFormat: @"Axis 2"] attributes:normal9Attribs] autorelease];
labelZ = [[[NSAttributedString alloc] initWithString:[NSString stringWithFormat: @"Axis 3"] attributes:normal9Attribs] autorelease];
//GLString *glabel;
glabelX = [[[GLString alloc] initWithAttributedString:labelX
withTextColor:[NSColor colorWithDeviceRed:1.0f green:0.0f blue:0.56f alpha:1.0f] withBoxColor:[NSColor colorWithDeviceRed:1.0f green:0.0f blue:0.0f alpha:1.0f] withBorderColor:[NSColor colorWithDeviceRed:0.0f green:0.0f blue:0.0f alpha:1.0f]] retain];
glabelY = [[[GLString alloc] initWithAttributedString:labelY
withTextColor:[NSColor colorWithDeviceRed:1.0f green:0.0f blue:0.56f alpha:1.0f] withBoxColor:[NSColor colorWithDeviceRed:1.0f green:0.0f blue:0.0f alpha:1.0f] withBorderColor:[NSColor colorWithDeviceRed:0.0f green:0.0f blue:0.0f alpha:1.0f]] retain];
glabelZ = [[[GLString alloc] initWithAttributedString:labelZ
withTextColor:[NSColor colorWithDeviceRed:1.0f green:0.0f blue:0.56f alpha:1.0f] withBoxColor:[NSColor colorWithDeviceRed:1.0f green:0.0f blue:0.0f alpha:1.0f] withBorderColor:[NSColor colorWithDeviceRed:0.0f green:0.0f blue:0.0f alpha:1.0f]] retain];
}
GLfloat width = [self bounds].size.width;
GLfloat height = [self bounds].size.height;
/*glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();*/
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glTranslatef(originx, originy, originz);
glRotatef(rotatey,0, 1, 0);
glRotatef(rotatez, 0, 0,1);
//X-label
glPushMatrix();
glTranslatef(0.0, 1.0, 1.1);
//glTranslatef(1.0, 0, 0);
glScalef (2.0f / width, -2.0f / height, 1.0f);
//glTranslatef (-width / 2.0f, -height / 2.0f, 0.0f);
[glabelX drawAtPoint:NSMakePoint (10.0f, height - [glabelX frameSize].height - 10.0f)];
glPopMatrix();
//Y-label
glPushMatrix();
//glRotatef(90,0,1,0);
glRotatef(90, 0, 0, 1);
glTranslatef(1.5, 1.7, 1.1);
glScalef (2.0f / width, -2.0f / height, 1.0f);
glTranslatef (-width / 2.0f, -height / 2.0f, 0.0f);
[glabelY drawAtPoint:NSMakePoint (10.0f, height - [glabelY frameSize].height - 10.0f)];
glPopMatrix();
//Z-label
/*glPushMatrix();
//glRotatef(90,1,0,0);
glScalef (2.0f / width, -2.0f / height, 1.0f);
glTranslatef (-width / 2.0f, -height / 2.0f, 0.0f);
//rotate
glRotatef(90, 0, 0, 1);
[glabelZ drawAtPoint:NSMakePoint (10.0f, height - [glabelZ frameSize].height - 10.0f)];
glPopMatrix();*/
GLenum err = glGetError();
NSLog(@"glError: %s" ,(char *) gluErrorString (err));
/*
glMatrixMode(GL_PROJECTION);
glPopMatrix();*/
}
-(void) drawRect :(NSRect) bounds
{
NSOpenGLContext *context = [self openGLContext];
[context makeCurrentContext];
//if(bounds != [self bounds] )
//{
//glViewport(bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height);
//glDepthRange(minmax[4],minmax[5]);
//}
glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT);