You received this message because you are subscribed to a topic in the Google Groups "MathGL" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mathgl/05QjQS44VhE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mathgl+un...@googlegroups.com.
Dear Yvonne,
Most probably yours C# construction correspond to C++ code:
double arr[2][5] = {{1,2,3,4,5},{1,3,5,7,9}};
If yes then compiler place it as array of {5 doubles}, i.e. just as 2*5=10 double numbers. And arr is just a pointer to this array which should be used as double*. It is not double** since compiler avoid creating unnecessary array of pointers. So, for this code you need to use
mgl_data_set_double(f,(double*)arr,5,2);
Contrary, if you create double array manually as
double **arr2=new double*[2];
for(int i=0;i<2;i++) arr2[i] = new double[5];
...
then you need to use function
mgl_data_set_double2(f,arr2,2,5);
The matter is that no one can guarantee what arr2[1] will start just after arr2[0] (and so on). And MathGL have to use true pointers to pointers.
2013/7/1 Yvonne Tan <tyfy...@gmail.com>Dear Alexey,I met another problem. During debugging, when it comes to the function "mgl_data_set_double2(...)", it popped out an "AccessViolationException wasunhandled" dialog window, saying "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." I have noidea how to solve this problem.I defined and initialized the 2d array as: static double[,] arr = new double[2,5] { { 1, 2, 3, 4, 5 }, { 1, 3, 5, 7, 9 } };Imported the function from libmgl.dll as: [DllImport("libmgl.dll")]private static extern void mgl_data_set_double2(IntPtr dat, double[,] A, int N1, int N2);Then I called the function as :IntPtr f = mgl_create_data();mgl_data_set_double2(f,arr,2,5);Where did I do wrong? I've been stuck in here for days. Although I know you don't know C#, can you at least give me an example of how to use this function in C and C++? And how to call this function from *.dll using C++?And FYI, I've tested the "mgl_data_set_double(...)" function from my program and it works well. So I'm thinking does this problem has anything to do with C# passing 2d array to the C *.dll?Thanks!Yvonne
namespace BitmapTest{public partial class Form1 : Form{private static double[,] arr = new double[50, 40];static int row = arr.GetUpperBound(0) + 1;static int col = arr.GetUpperBound(1) + 1;void Initialize2DArray(){for (int i = 0; i < row; i++){for (int j = 0; j < col; j++){double x = i / (double)(row - 1);double y = j / (double)(col - 1);arr[i, j] = (double)(0.6 * Math.Sin(2 * Math.PI * x) * Math.Sin(3 * Math.PI * y) + 0.4 * Math.Cos(3 * Math.PI * x * y));}}}public Form1(){InitializeComponent();Initialize2DArray();}[DllImport("libmgl.dll")]private static extern void mgl_data_set_value(IntPtr dat, double v, int i, int j, int k);[DllImport("libmgl.dll")]private static extern IntPtr mgl_create_data_size(int nx, int ny, int nz);[DllImport("libmgl.dll")]private static extern void mgl_rotate(IntPtr gr, double TetX, double TetZ, double TetY);[DllImport("libmgl.dll")]private static extern void mgl_delete_data(IntPtr dat);[DllImport("libmgl.dll")]private static extern void mgl_box(IntPtr gr, int ticks);[DllImport("libmgl.dll")]private static extern void mgl_set_light(IntPtr gr, int enable);[DllImport("libmgl.dll")]private static extern void mgl_surf(IntPtr gr, IntPtr z, string sch, string opt);[DllImport("libmgl.dll")]private static extern string mgl_get_rgb(IntPtr gr);[DllImport("libmgl.dll")]private static extern IntPtr mgl_create_graph(int width, int height);static int Sample2D(IntPtr gr, IntPtr p){IntPtr mgldt2d = mgl_create_data_size(row, col, 1);for (int i = 0; i < row; i++){for (int j = 0; j < col; j++){double dt = arr[i, j];mgl_data_set_value(mgldt2d, dt, i, j, 0);}}mgl_rotate(gr, 60, 40, 0);mgl_box(gr, 1);mgl_set_light(gr, 1);mgl_surf(gr, mgldt2d, "", "");mgl_delete_data(mgldt2d);return 0;}private void button1_Click(object sender, EventArgs e){IntPtr gr = mgl_create_graph(row,col);Sample2D(gr, (IntPtr)null);string buf = mgl_get_rgb(gr);Bitmap bitmap = new Bitmap(50, 40);for (int i = 0; i < 50; i++)for (int j = 0; j < 40; j++){int r = (int)(byte)char.GetNumericValue(buf[3 * 50 * i + 3 * j + 1]);int g = (int)(byte)char.GetNumericValue(buf[3 * 50 * i + 3 * j + 2]);int b = (int)(byte)char.GetNumericValue(buf[3 * 50 * i + 3 * j + 3]);Color color = Color.FromArgb(r,g,b);bitmap.SetPixel(i, j, color);}pictureBox1.Image = bitmap;pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;}}}
And the string of RGB values is also with wrong values. It contains only blank spaces. And all the respectively extracted RGB values are 255. Below are my codes. What's wrong with it?! (I think more attention should be focused on the red part.)
IntPtr gr = mgl_create_graph(row,col);
Sample2D(gr, (IntPtr)null);string buf = mgl_get_rgb(gr);
Bitmap bitmap = new Bitmap(50, 40);
for (int i = 0; i < 50; i++)for (int j = 0; j < 40; j++){int r = (int)(byte)char.GetNumericValue(buf[3 * 50 * i + 3 * j + 1]);
int g = (int)(byte)char.GetNumericValue(buf[3 * 50 * i + 3 * j + 2]);int b = (int)(byte)char.GetNumericValue(buf[3 * 50 * i + 3 * j + 3]);Color color = Color.FromArgb(r,g,b);bitmap.SetPixel(i, j, color);
--
You received this message because you are subscribed to the Google Groups "MathGL" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mathgl+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Dear Alexey,I met another problem. During debugging, when it comes to the function "mgl_data_set_double2(...)", it popped out an "AccessViolationException wasunhandled" dialog window, saying "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." I have noidea how to solve this problem.I defined and initialized the 2d array as: static double[,] arr = new double[2,5] { { 1, 2, 3, 4, 5 }, { 1, 3, 5, 7, 9 } };Imported the function from libmgl.dll as: [DllImport("libmgl.dll")]private static extern void mgl_data_set_double2(IntPtr dat, double[,] A, int N1, int N2);Then I called the function as :IntPtr f = mgl_create_data();mgl_data_set_double2(f,arr,2,5);Where did I do wrong? I've been stuck in here for days. Although I know you don't know C#, can you at least give me an example of how to use this function in C and C++? And how to call this function from *.dll using C++?And FYI, I've tested the "mgl_data_set_double(...)" function from my program and it works well. So I'm thinking does this problem has anything to do with C# passing 2d array to the C *.dll?Thanks!Yvonne