@@ -42,6 +42,7 @@ struct TypedIndex
42
42
{
43
43
flann_index_t index;
44
44
flann_datatype_t type;
45
+ void * dataset;
45
46
};
46
47
47
48
@@ -332,6 +333,15 @@ static void _index_find_nn(int nOutArray, mxArray* OutArray[], int nInArray, con
332
333
}
333
334
334
335
336
+ template <typename T>
337
+ T* copy_array (const mxArray* array)
338
+ {
339
+ size_t mem_size = mxGetN (array)*mxGetM (array)*sizeof (T);
340
+ void * data = malloc (mem_size);
341
+ memcpy (data,mxGetData (array),mem_size);
342
+ return (T*)data;
343
+ }
344
+
335
345
/* *
336
346
* Input arguments: dataset (matrix), params (struct)
337
347
* Output arguments: index (pointer to index), params (struct), speedup(double)
@@ -349,6 +359,8 @@ static void _build_index(int nOutArray, mxArray* OutArray[], int nInArray, const
349
359
const mxArray* datasetMat = InArray[0 ];
350
360
check_allowed_type (datasetMat);
351
361
362
+
363
+
352
364
int dcount = mxGetN (datasetMat);
353
365
int length = mxGetM (datasetMat);
354
366
@@ -364,24 +376,28 @@ static void _build_index(int nOutArray, mxArray* OutArray[], int nInArray, const
364
376
TypedIndex* typedIndex = new TypedIndex ();
365
377
366
378
if (mxIsSingle (datasetMat)) {
367
- float * dataset = ( float *) mxGetData (datasetMat);
379
+ float * dataset = copy_array< float > (datasetMat);
368
380
typedIndex->index = flann_build_index_float (dataset,dcount,length, &speedup, &p);
369
381
typedIndex->type = FLANN_FLOAT32;
382
+ typedIndex->dataset = dataset;
370
383
}
371
384
else if (mxIsDouble (datasetMat)) {
372
- double * dataset = ( double *) mxGetData (datasetMat);
385
+ double * dataset = copy_array< double > (datasetMat);
373
386
typedIndex->index = flann_build_index_double (dataset,dcount,length, &speedup, &p);
374
387
typedIndex->type = FLANN_FLOAT64;
388
+ typedIndex->dataset = dataset;
375
389
}
376
390
else if (mxIsUint8 (datasetMat)) {
377
- unsigned char * dataset = ( unsigned char *) mxGetData (datasetMat);
391
+ unsigned char * dataset = copy_array< unsigned char > (datasetMat);
378
392
typedIndex->index = flann_build_index_byte (dataset,dcount,length, &speedup, &p);
379
393
typedIndex->type = FLANN_UINT8;
394
+ typedIndex->dataset = dataset;
380
395
}
381
396
else if (mxIsInt32 (datasetMat)) {
382
- int * dataset = ( int *) mxGetData (datasetMat);
397
+ int * dataset = copy_array< int > (datasetMat);
383
398
typedIndex->index = flann_build_index_int (dataset,dcount,length, &speedup, &p);
384
399
typedIndex->type = FLANN_INT32;
400
+ typedIndex->dataset = dataset;
385
401
}
386
402
387
403
mxClassID classID;
@@ -432,6 +448,7 @@ static void _free_index(int nOutArray, mxArray* OutArray[], int nInArray, const
432
448
else if (typedIndex->type ==FLANN_INT32) {
433
449
flann_free_index_int (typedIndex->index , NULL );
434
450
}
451
+ free (typedIndex->dataset );
435
452
delete typedIndex;
436
453
}
437
454
@@ -543,24 +560,28 @@ static void _load_index(int nOutArray, mxArray* OutArray[], int nInArray, const
543
560
TypedIndex* typedIndex = new TypedIndex ();
544
561
545
562
if (mxIsSingle (datasetMat)) {
546
- float * dataset = ( float *) mxGetData (datasetMat);
563
+ float * dataset = copy_array< float > (datasetMat);
547
564
typedIndex->index = flann_load_index_float (filename, dataset,dcount,length);
548
565
typedIndex->type = FLANN_FLOAT32;
566
+ typedIndex->dataset = dataset;
549
567
}
550
568
else if (mxIsDouble (datasetMat)) {
551
- double * dataset = ( double *) mxGetData (datasetMat);
569
+ double * dataset = copy_array< double > (datasetMat);
552
570
typedIndex->index = flann_load_index_double (filename, dataset,dcount,length);
553
571
typedIndex->type = FLANN_FLOAT64;
572
+ typedIndex->dataset = dataset;
554
573
}
555
574
else if (mxIsUint8 (datasetMat)) {
556
- unsigned char * dataset = ( unsigned char *) mxGetData (datasetMat);
575
+ unsigned char * dataset = copy_array< unsigned char > (datasetMat);
557
576
typedIndex->index = flann_load_index_byte (filename, dataset,dcount,length);
558
577
typedIndex->type = FLANN_UINT8;
578
+ typedIndex->dataset = dataset;
559
579
}
560
580
else if (mxIsInt32 (datasetMat)) {
561
- int * dataset = ( int *) mxGetData (datasetMat);
581
+ int * dataset = copy_array< int > (datasetMat);
562
582
typedIndex->index = flann_load_index_int (filename, dataset,dcount,length);
563
583
typedIndex->type = FLANN_INT32;
584
+ typedIndex->dataset = dataset;
564
585
}
565
586
566
587
mxClassID classID;
0 commit comments