/
SparseMat.cs
977 lines (865 loc) · 31.7 KB
/
SparseMat.cs
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
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using OpenCvSharp.Internal;
namespace OpenCvSharp;
/// <summary>
/// Sparse matrix class.
/// </summary>
public class SparseMat : DisposableCvObject
{
#region Init & Disposal
/// <summary>
/// Creates from native cv::SparseMat* pointer
/// </summary>
/// <param name="ptr"></param>
public SparseMat(IntPtr ptr)
{
if (ptr == IntPtr.Zero)
throw new OpenCvSharpException("Native object address is NULL");
this.ptr = ptr;
}
/// <summary>
/// Creates empty SparseMat
/// </summary>
public SparseMat()
{
NativeMethods.HandleException(
NativeMethods.core_SparseMat_new1(out ptr));
}
/// <summary>
/// constructs n-dimensional sparse matrix
/// </summary>
/// <param name="sizes">Array of integers specifying an n-dimensional array shape.</param>
/// <param name="type">Array type. Use MatType.CV_8UC1, ..., CV_64FC4 to create 1-4 channel matrices,
/// or MatType. CV_8UC(n), ..., CV_64FC(n) to create multi-channel matrices.</param>
[SuppressMessage("Maintainability", "CA1508: Avoid dead conditional code")]
public SparseMat(IEnumerable<int> sizes, MatType type)
{
if (sizes is null)
throw new ArgumentNullException(nameof(sizes));
var sizesArray = sizes as int[] ?? sizes.ToArray();
NativeMethods.HandleException(
NativeMethods.core_SparseMat_new2(sizesArray.Length, sizesArray, type, out ptr));
}
/// <summary>
/// converts old-style CvMat to the new matrix; the data is not copied by default
/// </summary>
/// <param name="m">cv::Mat object</param>
public SparseMat(Mat m)
{
if (m is null)
throw new ArgumentNullException(nameof(m));
m.ThrowIfDisposed();
NativeMethods.HandleException(
NativeMethods.core_SparseMat_new3(m.CvPtr, out ptr));
GC.KeepAlive(m);
if (ptr == IntPtr.Zero)
throw new OpenCvSharpException();
}
/// <summary>
/// Releases the resources
/// </summary>
public void Release()
{
Dispose();
}
/// <summary>
/// Releases unmanaged resources
/// </summary>
protected override void DisposeUnmanaged()
{
NativeMethods.HandleException(
NativeMethods.core_SparseMat_delete(ptr));
base.DisposeUnmanaged();
}
/// <summary>
/// Create SparseMat from Mat
/// </summary>
/// <param name="mat"></param>
/// <returns></returns>
public static SparseMat FromMat(Mat mat)
{
return new SparseMat(mat);
}
#endregion
#region Public Methods
/// <summary>
/// Assignment operator. This is O(1) operation, i.e. no data is copied
/// </summary>
/// <param name="m"></param>
/// <returns></returns>
public SparseMat AssignFrom(SparseMat m)
{
ThrowIfDisposed();
if(m is null)
throw new ArgumentNullException(nameof(m));
NativeMethods.HandleException(
NativeMethods.core_SparseMat_operatorAssign_SparseMat(ptr, m.CvPtr));
GC.KeepAlive(this);
GC.KeepAlive(m);
return this;
}
/// <summary>
/// Assignment operator. equivalent to the corresponding constructor.
/// </summary>
/// <param name="m"></param>
/// <returns></returns>
public SparseMat AssignFrom(Mat m)
{
ThrowIfDisposed();
if (m is null)
throw new ArgumentNullException(nameof(m));
NativeMethods.HandleException(
NativeMethods.core_SparseMat_operatorAssign_Mat(ptr, m.CvPtr));
GC.KeepAlive(this);
GC.KeepAlive(m);
return this;
}
/// <summary>
/// creates full copy of the matrix
/// </summary>
/// <returns></returns>
public SparseMat Clone()
{
ThrowIfDisposed();
NativeMethods.HandleException(
NativeMethods.core_SparseMat_clone(ptr, out var p));
GC.KeepAlive(this);
return new SparseMat(p);
}
/// <summary>
/// copies all the data to the destination matrix. All the previous content of m is erased.
/// </summary>
/// <param name="m"></param>
public void CopyTo(SparseMat m)
{
if (m is null)
throw new ArgumentNullException(nameof(m));
ThrowIfDisposed();
NativeMethods.HandleException(
NativeMethods.core_SparseMat_copyTo_SparseMat(ptr, m.CvPtr));
GC.KeepAlive(this);
GC.KeepAlive(m);
}
/// <summary>
/// converts sparse matrix to dense matrix.
/// </summary>
/// <param name="m"></param>
public void CopyTo(Mat m)
{
if (m is null)
throw new ArgumentNullException(nameof(m));
ThrowIfDisposed();
NativeMethods.HandleException(
NativeMethods.core_SparseMat_copyTo_Mat(ptr, m.CvPtr));
GC.KeepAlive(this);
GC.KeepAlive(m);
}
/// <summary>
/// multiplies all the matrix elements by the specified scale factor alpha and converts the results to the specified data type
/// </summary>
/// <param name="m"></param>
/// <param name="rtype"></param>
/// <param name="alpha"></param>
public void ConvertTo(SparseMat m, int rtype, double alpha = 1)
{
if (m is null)
throw new ArgumentNullException(nameof(m));
ThrowIfDisposed();
NativeMethods.HandleException(
NativeMethods.core_SparseMat_convertTo_SparseMat(ptr, m.CvPtr, rtype, alpha));
GC.KeepAlive(this);
GC.KeepAlive(m);
}
/// <summary>
/// converts sparse matrix to dense n-dim matrix with optional type conversion and scaling.
/// </summary>
/// <param name="m"></param>
/// <param name="rtype">The output matrix data type. When it is =-1, the output array will have the same data type as (*this)</param>
/// <param name="alpha">The scale factor</param>
/// <param name="beta">The optional delta added to the scaled values before the conversion</param>
public void ConvertTo(Mat m, int rtype, double alpha = 1, double beta = 0)
{
if (m is null)
throw new ArgumentNullException(nameof(m));
ThrowIfDisposed();
NativeMethods.HandleException(
NativeMethods.core_SparseMat_convertTo_Mat(ptr, m.CvPtr, rtype, alpha, beta));
GC.KeepAlive(this);
GC.KeepAlive(m);
}
/// <summary>
/// not used now
/// </summary>
/// <param name="m"></param>
/// <param name="type"></param>
public void AssignTo(SparseMat m, int type = -1)
{
if (m is null)
throw new ArgumentNullException(nameof(m));
ThrowIfDisposed();
NativeMethods.HandleException(
NativeMethods.core_SparseMat_assignTo(ptr, m.CvPtr, type));
GC.KeepAlive(this);
GC.KeepAlive(m);
}
/// <summary>
/// Reallocates sparse matrix.
/// If the matrix already had the proper size and type,
/// it is simply cleared with clear(), otherwise,
/// the old matrix is released (using release()) and the new one is allocated.
/// </summary>
/// <param name="type"></param>
/// <param name="sizes"></param>
[SuppressMessage("Maintainability", "CA1508: Avoid dead conditional code")]
public void Create(MatType type, params int[] sizes)
{
ThrowIfDisposed();
if (sizes is null)
throw new ArgumentNullException(nameof(sizes));
if (sizes.Length == 1)
throw new ArgumentException("sizes is empty");
NativeMethods.HandleException(
NativeMethods.core_SparseMat_create(ptr, sizes.Length, sizes, type));
GC.KeepAlive(this);
}
/// <summary>
/// sets all the sparse matrix elements to 0, which means clearing the hash table.
/// </summary>
public void Clear()
{
ThrowIfDisposed();
NativeMethods.HandleException(
NativeMethods.core_SparseMat_clear(ptr));
GC.KeepAlive(this);
}
/// <summary>
/// manually increments the reference counter to the header.
/// </summary>
public void AddRef()
{
ThrowIfDisposed();
NativeMethods.HandleException(
NativeMethods.core_SparseMat_addref(ptr));
GC.KeepAlive(this);
}
/// <summary>
/// returns the size of each element in bytes (not including the overhead - the space occupied by SparseMat::Node elements)
/// </summary>
/// <returns></returns>
public int ElemSize()
{
ThrowIfDisposed();
NativeMethods.HandleException(
NativeMethods.core_SparseMat_elemSize(ptr, out var ret));
GC.KeepAlive(this);
return ret;
}
/// <summary>
/// returns elemSize()/channels()
/// </summary>
/// <returns></returns>
public int ElemSize1()
{
ThrowIfDisposed();
NativeMethods.HandleException(
NativeMethods.core_SparseMat_elemSize1(ptr, out var ret));
GC.KeepAlive(this);
return ret;
}
/// <summary>
/// Returns the type of sparse matrix element.
/// </summary>
/// <returns></returns>
public MatType Type()
{
ThrowIfDisposed();
NativeMethods.HandleException(
NativeMethods.core_SparseMat_type(ptr, out var ret));
GC.KeepAlive(this);
return ret;
}
/// <summary>
/// Returns the depth of sparse matrix element.
/// </summary>
/// <returns></returns>
public int Depth()
{
ThrowIfDisposed();
NativeMethods.HandleException(
NativeMethods.core_SparseMat_depth(ptr, out var ret));
GC.KeepAlive(this);
return ret;
}
/// <summary>
/// Returns the matrix dimensionality
/// </summary>
public int Dims()
{
ThrowIfDisposed();
NativeMethods.HandleException(
NativeMethods.core_SparseMat_dims(ptr, out var ret));
GC.KeepAlive(this);
return ret;
}
/// <summary>
/// Returns the number of sparse matrix channels.
/// </summary>
/// <returns></returns>
public int Channels()
{
ThrowIfDisposed();
NativeMethods.HandleException(
NativeMethods.core_SparseMat_channels(ptr, out var ret));
GC.KeepAlive(this);
return ret;
}
/// <summary>
/// Returns the array of sizes, or null if the matrix is not allocated
/// </summary>
/// <returns></returns>
public int[] Size()
{
ThrowIfDisposed();
NativeMethods.HandleException(
NativeMethods.core_SparseMat_size1(ptr, out var sizePtr));
if (sizePtr == IntPtr.Zero)
throw new OpenCvSharpException("core_SparseMat_size1 == IntPtr.Zero");
var length = Dims();
var size = new int[length];
Marshal.Copy(sizePtr, size, 0, length);
GC.KeepAlive(this);
return size;
}
/// <summary>
/// Returns the size of i-th matrix dimension (or 0)
/// </summary>
/// <param name="dim"></param>
/// <returns></returns>
public int Size(int dim)
{
ThrowIfDisposed();
NativeMethods.HandleException(
NativeMethods.core_SparseMat_size2(ptr, dim, out var ret));
GC.KeepAlive(this);
return ret;
}
/// <summary>
/// returns the number of non-zero elements (=the number of hash table nodes)
/// </summary>
/// <returns></returns>
public long NzCount()
{
ThrowIfDisposed();
NativeMethods.HandleException(
NativeMethods.core_SparseMat_nzcount(ptr, out var ret));
GC.KeepAlive(this);
return ret.ToInt64();
}
#region Hash
/// <summary>
/// Computes the element hash value (1D case)
/// </summary>
/// <param name="i0">Index along the dimension 0</param>
/// <returns></returns>
public long Hash(int i0)
{
ThrowIfDisposed();
NativeMethods.HandleException(
NativeMethods.core_SparseMat_hash_1d(ptr, i0, out var ret));
GC.KeepAlive(this);
return ret.ToInt64();
}
/// <summary>
/// Computes the element hash value (2D case)
/// </summary>
/// <param name="i0">Index along the dimension 0</param>
/// <param name="i1">Index along the dimension 1</param>
/// <returns></returns>
public long Hash(int i0, int i1)
{
ThrowIfDisposed();
NativeMethods.HandleException(
NativeMethods.core_SparseMat_hash_2d(ptr, i0, i1, out var ret));
GC.KeepAlive(this);
return ret.ToInt64();
}
/// <summary>
/// Computes the element hash value (3D case)
/// </summary>
/// <param name="i0">Index along the dimension 0</param>
/// <param name="i1">Index along the dimension 1</param>
/// <param name="i2">Index along the dimension 2</param>
/// <returns></returns>
public long Hash(int i0, int i1, int i2)
{
ThrowIfDisposed();
NativeMethods.HandleException(NativeMethods.core_SparseMat_hash_3d(ptr, i0, i1, i2, out var ret));
GC.KeepAlive(this);
return ret.ToInt64();
}
/// <summary>
/// Computes the element hash value (nD case)
/// </summary>
/// <param name="idx">Array of Mat::dims indices.</param>
/// <returns></returns>
public long Hash(params int[] idx)
{
ThrowIfDisposed();
NativeMethods.HandleException(
NativeMethods.core_SparseMat_hash_nd(ptr, idx, out var ret));
GC.KeepAlive(this);
return ret.ToInt64();
}
#endregion
#region Ptr
/// <summary>
/// Low-level element-access function.
/// </summary>
/// <param name="i0">Index along the dimension 0</param>
/// <param name="createMissing">Create new element with 0 value if it does not exist in SparseMat.</param>
/// <param name="hashVal">If hashVal is not null, the element hash value is not computed but hashval is taken instead.</param>
/// <returns></returns>
public unsafe IntPtr Ptr(int i0, bool createMissing, long? hashVal = null)
{
IntPtr ret;
//ThrowIfDisposed();
if (hashVal.HasValue)
{
var hashVal0 = (ulong)hashVal.Value;
NativeMethods.HandleException(
NativeMethods.core_SparseMat_ptr_1d(
ptr, i0, createMissing ? 1 : 0, &hashVal0, out ret));
}
else
NativeMethods.HandleException(
NativeMethods.core_SparseMat_ptr_1d(
ptr, i0, createMissing ? 1 : 0, null, out ret));
GC.KeepAlive(this);
return ret;
}
/// <summary>
/// Low-level element-access function.
/// </summary>
/// <param name="i0">Index along the dimension 0</param>
/// <param name="i1">Index along the dimension 1</param>
/// <param name="createMissing">Create new element with 0 value if it does not exist in SparseMat.</param>
/// <param name="hashVal">If hashVal is not null, the element hash value is not computed but hashval is taken instead.</param>
/// <returns></returns>
public unsafe IntPtr Ptr(int i0, int i1, bool createMissing, long? hashVal = null)
{
IntPtr ret;
//ThrowIfDisposed();
if (hashVal.HasValue)
{
var hashVal0 = (ulong)hashVal.Value;
NativeMethods.HandleException(
NativeMethods.core_SparseMat_ptr_2d(
ptr, i0, i1, createMissing ? 1 : 0, &hashVal0, out ret));
}
else
NativeMethods.HandleException(
NativeMethods.core_SparseMat_ptr_2d(
ptr, i0, i1, createMissing ? 1 : 0, null, out ret));
GC.KeepAlive(this);
return ret;
}
/// <summary>
/// Low-level element-access function.
/// </summary>
/// <param name="i0">Index along the dimension 0</param>
/// <param name="i1">Index along the dimension 1</param>
/// <param name="i2">Index along the dimension 2</param>
/// <param name="createMissing">Create new element with 0 value if it does not exist in SparseMat.</param>
/// <param name="hashVal">If hashVal is not null, the element hash value is not computed but hashval is taken instead.</param>
/// <returns></returns>
public unsafe IntPtr Ptr(int i0, int i1, int i2, bool createMissing, long? hashVal = null)
{
IntPtr ret;
//ThrowIfDisposed();
if (hashVal.HasValue)
{
var hashVal0 = (ulong)hashVal.Value;
NativeMethods.HandleException(
NativeMethods.core_SparseMat_ptr_3d(
ptr, i0, i1, i2, createMissing ? 1 : 0, &hashVal0, out ret));
}
else
NativeMethods.HandleException(
NativeMethods.core_SparseMat_ptr_3d(
ptr, i0, i1, i2, createMissing ? 1 : 0, null, out ret));
GC.KeepAlive(this);
return ret;
}
/// <summary>
/// Low-level element-access function.
/// </summary>
/// <param name="idx">Array of Mat::dims indices.</param>
/// <param name="createMissing">Create new element with 0 value if it does not exist in SparseMat.</param>
/// <param name="hashVal">If hashVal is not null, the element hash value is not computed but hashval is taken instead.</param>
/// <returns></returns>
public unsafe IntPtr Ptr(int[] idx, bool createMissing, long? hashVal = null)
{
IntPtr ret;
//ThrowIfDisposed();
if (hashVal.HasValue)
{
var hashVal0 = (ulong)hashVal.Value;
NativeMethods.HandleException(
NativeMethods.core_SparseMat_ptr_nd(
ptr, idx, createMissing ? 1 : 0, &hashVal0, out ret));
}
else
NativeMethods.HandleException(
NativeMethods.core_SparseMat_ptr_nd(
ptr, idx, createMissing ? 1 : 0, null, out ret));
GC.KeepAlive(this);
return ret;
}
#endregion
#region Find
/// <summary>
/// Return pthe specified sparse matrix element if it exists; otherwise, null.
/// </summary>
/// <param name="i0">Index along the dimension 0</param>
/// <param name="hashVal">If hashVal is not null, the element hash value is not computed but hashval is taken instead.</param>
/// <returns></returns>
public T? Find<T>(int i0, long? hashVal = null)
where T : struct
{
var p = Ptr(i0, false, hashVal);
if (p == IntPtr.Zero)
return null;
return Marshal.PtrToStructure<T>(p);
}
/// <summary>
/// Return pthe specified sparse matrix element if it exists; otherwise, null.
/// </summary>
/// <param name="i0">Index along the dimension 0</param>
/// <param name="i1">Index along the dimension 1</param>
/// <param name="hashVal">If hashVal is not null, the element hash value is not computed but hashval is taken instead.</param>
/// <returns></returns>
public T? Find<T>(int i0, int i1, long? hashVal = null)
where T : struct
{
var p = Ptr(i0, i1, false, hashVal);
if (p == IntPtr.Zero)
return null;
return Marshal.PtrToStructure<T>(p);
}
/// <summary>
/// Return pthe specified sparse matrix element if it exists; otherwise, null.
/// </summary>
/// <param name="i0">Index along the dimension 0</param>
/// <param name="i1">Index along the dimension 1</param>
/// <param name="i2">Index along the dimension 2</param>
/// <param name="hashVal">If hashVal is not null, the element hash value is not computed but hashval is taken instead.</param>
/// <returns></returns>
public T? Find<T>(int i0, int i1, int i2, long? hashVal = null)
where T : struct
{
var p = Ptr(i0, i1, i2, false, hashVal);
if (p == IntPtr.Zero)
return null;
return Marshal.PtrToStructure<T>(p);
}
/// <summary>
/// Return pthe specified sparse matrix element if it exists; otherwise, null.
/// </summary>
/// <param name="idx">Array of Mat::dims indices.</param>
/// <param name="hashVal">If hashVal is not null, the element hash value is not computed but hashval is taken instead.</param>
/// <returns></returns>
public T? Find<T>(int[] idx, long? hashVal = null)
where T : struct
{
var p = Ptr(idx, false, hashVal);
if (p == IntPtr.Zero)
return null;
return Marshal.PtrToStructure<T>(p);
}
#endregion
#region Value
/// <summary>
/// Return pthe specified sparse matrix element if it exists; otherwise, default(T).
/// </summary>
/// <param name="i0">Index along the dimension 0</param>
/// <param name="hashVal">If hashVal is not null, the element hash value is not computed but hashval is taken instead.</param>
/// <returns></returns>
public T Value<T>(int i0, long? hashVal = null)
where T : struct
{
var p = Ptr(i0, false, hashVal);
if (p == IntPtr.Zero)
return default;
return Marshal.PtrToStructure<T>(p);
}
/// <summary>
/// Return pthe specified sparse matrix element if it exists; otherwise, default(T).
/// </summary>
/// <param name="i0">Index along the dimension 0</param>
/// <param name="i1">Index along the dimension 1</param>
/// <param name="hashVal">If hashVal is not null, the element hash value is not computed but hashval is taken instead.</param>
/// <returns></returns>
public T Value<T>(int i0, int i1, long? hashVal = null)
where T : struct
{
var p = Ptr(i0, i1, false, hashVal);
if (p == IntPtr.Zero)
return default;
return Marshal.PtrToStructure<T>(p);
}
/// <summary>
/// Return pthe specified sparse matrix element if it exists; otherwise, default(T).
/// </summary>
/// <param name="i0">Index along the dimension 0</param>
/// <param name="i1">Index along the dimension 1</param>
/// <param name="i2">Index along the dimension 2</param>
/// <param name="hashVal">If hashVal is not null, the element hash value is not computed but hashval is taken instead.</param>
/// <returns></returns>
public T Value<T>(int i0, int i1, int i2, long? hashVal = null)
where T : struct
{
var p = Ptr(i0, i1, i2, false, hashVal);
if (p == IntPtr.Zero)
return default;
return Marshal.PtrToStructure<T>(p);
}
/// <summary>
/// Return pthe specified sparse matrix element if it exists; otherwise, default(T).
/// </summary>
/// <param name="idx">Array of Mat::dims indices.</param>
/// <param name="hashVal">If hashVal is not null, the element hash value is not computed but hashval is taken instead.</param>
/// <returns></returns>
public T Value<T>(int[] idx, long? hashVal = null)
where T : struct
{
var p = Ptr(idx, false, hashVal);
if (p == IntPtr.Zero)
return default;
return Marshal.PtrToStructure<T>(p);
}
#endregion
#region Element Indexer
#pragma warning disable CA1034
/// <summary>
/// Mat Indexer
/// </summary>
/// <typeparam name="T"></typeparam>
public sealed class Indexer<T> : SparseMatIndexer<T> where T : struct
{
internal Indexer(SparseMat parent)
: base(parent)
{
}
/// <summary>
/// 1-dimensional indexer
/// </summary>
/// <param name="i0">Index along the dimension 0</param>
/// <param name="hashVal">If hashVal is not null, the element hash value is not computed but hashval is taken instead.</param>
/// <returns>A value to the specified array element.</returns>
public override T this[int i0, long? hashVal = null]
{
get
{
var p = Parent.Ptr(i0, true, hashVal);
return Marshal.PtrToStructure<T>(p);
}
set
{
var p = Parent.Ptr(i0, true, hashVal);
Marshal.StructureToPtr(value, p, false);
}
}
/// <summary>
/// 2-dimensional indexer
/// </summary>
/// <param name="i0">Index along the dimension 0</param>
/// <param name="i1">Index along the dimension 1</param>
/// <param name="hashVal">If hashVal is not null, the element hash value is not computed but hashval is taken instead.</param>
/// <returns>A value to the specified array element.</returns>
public override T this[int i0, int i1, long? hashVal = null]
{
get
{
var p = Parent.Ptr(i0, i1, true, hashVal);
return Marshal.PtrToStructure<T>(p);
}
set
{
var p = Parent.Ptr(i0, i1, true, hashVal);
Marshal.StructureToPtr(value, p, false);
}
}
/// <summary>
/// 3-dimensional indexer
/// </summary>
/// <param name="i0">Index along the dimension 0</param>
/// <param name="i1">Index along the dimension 1</param>
/// <param name="i2"> Index along the dimension 2</param>
/// <param name="hashVal">If hashVal is not null, the element hash value is not computed but hashval is taken instead.</param>
/// <returns>A value to the specified array element.</returns>
public override T this[int i0, int i1, int i2, long? hashVal = null]
{
get
{
var p = Parent.Ptr(i0, i1, i2, true, hashVal);
return Marshal.PtrToStructure<T>(p);
}
set
{
var p = Parent.Ptr(i0, i1, i2, true, hashVal);
Marshal.StructureToPtr(value, p, false);
}
}
/// <summary>
/// n-dimensional indexer
/// </summary>
/// <param name="idx">Array of Mat::dims indices.</param>
/// <param name="hashVal">If hashVal is not null, the element hash value is not computed but hashval is taken instead.</param>
/// <returns>A value to the specified array element.</returns>
public override T this[int[] idx, long? hashVal = null]
{
get
{
var p = Parent.Ptr(idx, true, hashVal);
return Marshal.PtrToStructure<T>(p);
}
set
{
var p = Parent.Ptr(idx, true, hashVal);
Marshal.StructureToPtr(value, p, false);
}
}
}
/// <summary>
/// Gets a type-specific indexer.
/// The indexer has getters/setters to access each matrix element.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public Indexer<T> Ref<T>() where T : struct
{
return new Indexer<T>(this);
}
/// <summary>
/// Gets a type-specific indexer.
/// The indexer has getters/setters to access each matrix element.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public Indexer<T> GetIndexer<T>() where T : struct
{
return new Indexer<T>(this);
}
#endregion
#region Get/Set
/// <summary>
/// Returns a value to the specified array element.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="i0">Index along the dimension 0</param>
/// <param name="hashVal">If hashVal is not null, the element hash value is not computed but hashval is taken instead.</param>
/// <returns>A value to the specified array element.</returns>
public T Get<T>(int i0, long? hashVal = null) where T : struct
{
return new Indexer<T>(this)[i0, hashVal];
}
/// <summary>
/// Returns a value to the specified array element.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="i0">Index along the dimension 0</param>
/// <param name="i1">Index along the dimension 1</param>
/// <param name="hashVal">If hashVal is not null, the element hash value is not computed but hashval is taken instead.</param>
/// <returns>A value to the specified array element.</returns>
public T Get<T>(int i0, int i1, long? hashVal = null) where T : struct
{
return new Indexer<T>(this)[i0, i1, hashVal];
}
/// <summary>
/// Returns a value to the specified array element.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="i0">Index along the dimension 0</param>
/// <param name="i1">Index along the dimension 1</param>
/// <param name="i2">Index along the dimension 2</param>
/// <param name="hashVal">If hashVal is not null, the element hash value is not computed but hashval is taken instead.</param>
/// <returns>A value to the specified array element.</returns>
public T Get<T>(int i0, int i1, int i2, long? hashVal = null) where T : struct
{
return new Indexer<T>(this)[i0, i1, i2, hashVal];
}
/// <summary>
/// Returns a value to the specified array element.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="idx">Array of Mat::dims indices.</param>
/// <param name="hashVal">If hashVal is not null, the element hash value is not computed but hashval is taken instead.</param>
/// <returns>A value to the specified array element.</returns>
public T Get<T>(int[] idx, long? hashVal = null) where T : struct
{
return new Indexer<T>(this)[idx, hashVal];
}
/// <summary>
/// Set a value to the specified array element.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="i0">Index along the dimension 0</param>
/// <param name="value"></param>
/// <param name="hashVal"></param>
public void Set<T>(int i0, T value, long? hashVal = null) where T : struct
{
(new Indexer<T>(this))[i0, hashVal] = value;
}
/// <summary>
/// Set a value to the specified array element.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="i0">Index along the dimension 0</param>
/// <param name="i1">Index along the dimension 1</param>
/// <param name="value"></param>
/// <param name="hashVal">If hashVal is not null, the element hash value is not computed but hashval is taken instead.</param>
public void Set<T>(int i0, int i1, T value, long? hashVal = null) where T : struct
{
(new Indexer<T>(this))[i0, i1, hashVal] = value;
}
/// <summary>
/// Set a value to the specified array element.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="i0">Index along the dimension 0</param>
/// <param name="i1">Index along the dimension 1</param>
/// <param name="i2">Index along the dimension 2</param>
/// <param name="value"></param>
/// <param name="hashVal">If hashVal is not null, the element hash value is not computed but hashval is taken instead.</param>
public void Set<T>(int i0, int i1, int i2, T value, long? hashVal = null) where T : struct
{
(new Indexer<T>(this)[i0, i1, i2, hashVal]) = value;
}
/// <summary>
/// Set a value to the specified array element.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="idx">Array of Mat::dims indices.</param>
/// <param name="value"></param>
/// <param name="hashVal">If hashVal is not null, the element hash value is not computed but hashval is taken instead.</param>
public void Set<T>(int[] idx, T value, long? hashVal = null) where T : struct
{
(new Indexer<T>(this)[idx, hashVal]) = value;
}
#endregion
#region ToString
/// <summary>
/// Returns a string that represents this Mat.
/// </summary>
/// <returns></returns>
public override string ToString()
{
return "Mat [ " +
"Dims=" + Dims() +
"Type=" + Type().ToString() +
" ]";
}
#endregion
#endregion
}