Commit 91589064aa61ecd4eed1267301f12347716b9c27
1 parent
7d01bb90
added a global matlab mat4 function
Showing
1 changed file
with
43 additions
and
7 deletions
Show diff stats
stim/math/matrix.h
... | ... | @@ -4,6 +4,7 @@ |
4 | 4 | //#include "rts/vector.h" |
5 | 5 | #include <string.h> |
6 | 6 | #include <iostream> |
7 | +#include <fstream> | |
7 | 8 | #include <stim/math/vector.h> |
8 | 9 | #include <stim/math/vec3.h> |
9 | 10 | //#include <stim/cuda/cudatools/callable.h> |
... | ... | @@ -31,6 +32,36 @@ namespace stim{ |
31 | 32 | default: return 0; |
32 | 33 | } |
33 | 34 | } |
35 | + | |
36 | + void save_mat4(char* data, std::string filename, std::string varname, size_t sx, size_t sy, mat4Format format){ | |
37 | + //save the matrix file here (use the mat4 function above) | |
38 | + //data format: https://maxwell.ict.griffith.edu.au/spl/matlab-page/matfile_format.pdf (page 32) | |
39 | + | |
40 | + int MOPT = 0; //initialize the MOPT type value to zero | |
41 | + int m = 0; //little endian | |
42 | + int o = 0; //reserved, always 0 | |
43 | + int p = format; | |
44 | + int t = 0; | |
45 | + MOPT = m * 1000 + o * 100 + p * 10 + t; //calculate the type value | |
46 | + int mrows = sx; | |
47 | + int ncols = sy; | |
48 | + int imagf = 0; //assume real (for now) | |
49 | + varname.push_back('\0'); //add a null to the string | |
50 | + int namlen = varname.size(); //calculate the name size | |
51 | + | |
52 | + size_t bytes = sx * sy * mat4Format_size(format); | |
53 | + std::ofstream outfile(filename, std::ios::binary); | |
54 | + outfile.write((char*)&MOPT, 4); | |
55 | + outfile.write((char*)&mrows, 4); | |
56 | + outfile.write((char*)&ncols, 4); | |
57 | + outfile.write((char*)&imagf, 4); | |
58 | + outfile.write((char*)&namlen, 4); | |
59 | + outfile.write((char*)&varname[0], namlen); | |
60 | + outfile.write((char*)data, bytes); //write the matrix data | |
61 | + outfile.close(); | |
62 | + | |
63 | + } | |
64 | + | |
34 | 65 | template <class T> |
35 | 66 | class matrix { |
36 | 67 | //the matrix will be stored in column-major order (compatible with OpenGL) |
... | ... | @@ -380,8 +411,17 @@ public: |
380 | 411 | } |
381 | 412 | |
382 | 413 | // saves the matrix as a Level-4 MATLAB file |
383 | - void mat4(std::string filename, std::string name = std::string("unknown"), mat4Format = mat4_float) { | |
384 | - int MOPT = 0; //initialize the MOPT type value to zero | |
414 | + void mat4(std::string filename, std::string name = std::string("unknown"), mat4Format format = mat4_float) { | |
415 | + if (format == mat4_float) { | |
416 | + if (sizeof(T) == 4) format = mat4_float32; | |
417 | + else if (sizeof(T) == 8) format = mat4_float64; | |
418 | + else { | |
419 | + std::cout << "stim::matrix ERROR - incorrect format specified" << std::endl; | |
420 | + exit(1); | |
421 | + } | |
422 | + } | |
423 | + stim::save_mat4((char*)M, filename, name, rows(), cols(), format); | |
424 | + /*int MOPT = 0; //initialize the MOPT type value to zero | |
385 | 425 | int m = 0; //little endian |
386 | 426 | int o = 0; //reserved, always 0 |
387 | 427 | int p = mat4_float; |
... | ... | @@ -409,14 +449,10 @@ public: |
409 | 449 | outfile.write((char*)&namlen, 4); |
410 | 450 | outfile.write((char*)&name[0], namlen); |
411 | 451 | outfile.write((char*)M, bytes()); //write the matrix data |
412 | - outfile.close(); | |
452 | + outfile.close();*/ | |
413 | 453 | } |
414 | 454 | }; |
415 | 455 | |
416 | -void save_mat4(char* data, std::string filename, std::string varname, size_t sx, size_t sy, mat4Format format){ | |
417 | - //save the matrix file here (use the mat4 function above) | |
418 | - //data format: https://maxwell.ict.griffith.edu.au/spl/matlab-page/matfile_format.pdf (page 32) | |
419 | -} | |
420 | 456 | } //end namespace rts |
421 | 457 | |
422 | 458 | ... | ... |