Thread Subject:
Cell array creation for Voronoi cells in C++

Subject: Cell array creation for Voronoi cells in C++

From: Anthony Lamb

Date: 29 Aug, 2010 20:35:04

Message: 1 of 3

Hi All,

I'm preparing an interface to a C++ Voronoi library and I've gotten to the point where I have the vertices for each cell. Since I have a varying number of vertices for each Voronoi cell I've opted for the Cell array structure to store the data.
So here is where I am...for each Voronoi cell I have the coordinates stored in Vpts which is initialized using the following lines:

Vpts = new double *[nVert];
for(i=0; i< nVert ; i++) {Vpts[i] = new double [2];}

where nVert is the number of vertices for each Voronoi Cell. I fill Vpts with the coordinates of each vertex with the following lines:

for (vPt=0;vPt<nVert;vPt++){
Vpts[vPt][1]= x;
Vpts[vPt][2]=y; }

My question here is how do I create and store these values into a Cell structure to send back to Matlab. The syntax I used for my Matlab function is:

[vIndex nID] = nodes;

I need vIndex in the form of a Matlab cell array, where vIndex{i}(n,2) represents the coordinates of the n vertices for the Voronoi cell i.

Any help you can offer will be greatly appreciated.

Many thanks in advance,

Tony

Subject: Cell array creation for Voronoi cells in C++

From: James Tursa

Date: 30 Aug, 2010 02:26:03

Message: 2 of 3

"Anthony Lamb" <alamb@mathworks.com> wrote in message <i5eg9o$k49$1@fred.mathworks.com>...
> Hi All,
>
> I'm preparing an interface to a C++ Voronoi library and I've gotten to the point where I have the vertices for each cell. Since I have a varying number of vertices for each Voronoi cell I've opted for the Cell array structure to store the data.
> So here is where I am...for each Voronoi cell I have the coordinates stored in Vpts which is initialized using the following lines:
>
> Vpts = new double *[nVert];
> for(i=0; i< nVert ; i++) {Vpts[i] = new double [2];}
>
> where nVert is the number of vertices for each Voronoi Cell. I fill Vpts with the coordinates of each vertex with the following lines:
>
> for (vPt=0;vPt<nVert;vPt++){
> Vpts[vPt][1]= x;
> Vpts[vPt][2]=y; }

Error. Your indexing above should be 0 and 1, not 1 and 2.
>
> My question here is how do I create and store these values into a Cell structure to send back to Matlab. The syntax I used for my Matlab function is:
>
> [vIndex nID] = nodes;
>
> I need vIndex in the form of a Matlab cell array, where vIndex{i}(n,2) represents the coordinates of the n vertices for the Voronoi cell i.
>
> Any help you can offer will be greatly appreciated.
>
> Many thanks in advance,
>
> Tony

Here is an outline code to get you started. Call it with no arguments and it will return a cell array that I think matches your requested format.

#include "mex.h"
#define NVERT 10
#define NCELL 5
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
int j = 1;
    int vPt, nVert = NVERT;
double Vpts[NVERT][2];
double *pr;
mxArray *mx;

    for( vPt=0; vPt<nVert; vPt++ ){ // fill some sample data
        Vpts[vPt][0] = j++;
        Vpts[vPt][1] = j++;
}
plhs[0] = mxCreateCellMatrix(NCELL, 1); // create return cell array
    for( j=0; j<NCELL; j++ ){
mx = mxCreateDoubleMatrix(nVert, 2, mxREAL);
pr = mxGetPr(mx);
for( vPt=0; vPt<nVert; vPt++ ) { // fill in return cell
*pr++ = Vpts[vPt][0];
}
for( vPt=0; vPt<nVert; vPt++ ) { // fill in return cell
*pr++ = Vpts[vPt][1];
}
mxSetCell(plhs[0], j, mx); // attach it to cell array
}
}


James Tursa

Subject: Cell array creation for Voronoi cells in C++

From: Anthony Lamb

Date: 30 Aug, 2010 14:41:06

Message: 3 of 3

Thanks James!!!

It worked like a charm...
I opted for the following format to store the vertex nodes
*pr++ = x+0.5*vCell.pts[2*k];
*pr++ = y+0.5*vCell.pts[2*k+1];
which saved me the additional "for loop" but gives the vIndex in the form [2,nPts].
I've sent you a separate email on another stumbling point in my code...

Best wishes,
Tony

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
cellarray voronoi ... Anthony Lamb 29 Aug, 2010 16:39:04
rssFeed for this Thread

Contact us