三角形库是否适用于VS2015 x64?
我正在研究Linux并致力于转换为VS2015 x64的Windows,但由于poolalloc func中的内存问题,我收到了错误。
但我不知道问题是什么。
void OptimizeMatches(MatrixXf& feat, MatrixXf& feat_ref, MatrixXi& match_idx, MatrixXf& match_dist, std::vector<Matrix3f>& trans_mat, std::vector<Matrix3f>& inv_mat, std::vector<Matrix3f>& trans_mat_ref, std::vector<Matrix3f>& inv_mat_ref,
MatrixXf* belief_ptr, MatrixXi* label_ptr, std::vector<match_list>* match_ptr, std::vector<triangle_segment>* triangle_ptr ) {
int num_nodes = (int)feat.cols();
int num_matches = (int)match_idx.rows();
// build graph using delaunay triangulation
std::vector<support_pt> p_support;
p_support.resize(num_nodes);
for(int i = 0; i < num_nodes; i++){
p_support[i].x = feat(0,i);
p_support[i].y = feat(1,i);
}
std::vector<triangle_segment>& T = *triangle_ptr;
computeDelaunayTriangulation(p_support, &T);
............
}
在trangle.cpp用户func中
void computeDelaunayTriangulation (vector<support_pt> p_support, vector<triangle_segment>* tri_ptr){
// input/output structure for triangulation
struct triangulateio in, out;
int k;
// inputs
in.numberofpoints = p_support.size();
in.pointlist = (float*)malloc(in.numberofpoints*2*sizeof(float));
k=0;
for (int i=0; i< p_support.size(); i++){
in.pointlist[k++] = p_support[i].x;
in.pointlist[k++] = p_support[i].y;
}
in.numberofpointattributes = 0;
in.pointattributelist = NULL;
in.pointmarkerlist = NULL;
in.numberofsegments = 0;
in.numberofholes = 0;
in.numberofregions = 0;
in.regionlist = NULL;
// outputs
out.pointlist = NULL;
out.pointattributelist = NULL;
out.pointmarkerlist = NULL;
out.trianglelist = NULL;
out.triangleattributelist = NULL;
out.neighborlist = NULL;
out.segmentlist = NULL;
out.segmentmarkerlist = NULL;
out.edgelist = NULL;
out.edgemarkerlist = NULL;
// do triangulation (z=zero-based, n=neighbors, Q=quiet, B=no boundary markers)
char parameters[] = "zQB";
//printf("triangulate\n");
triangulate(parameters, &in, &out, NULL);
.....
}
triangle.cpp中的三角形主函数
void triangulate(char *triswitches, struct triangulateio *in,
struct triangulateio *out, struct triangulateio *vorout)
{
struct mesh m;
struct behavior b;
float *holearray; /* Array of holes. */
float *regionarray; /* Array of regional attributes and area constraints. */
triangleinit(&m);
parsecommandline(1, &triswitches, &b);
m.steinerleft = b.steiner;
transfernodes(&m, &b, in->pointlist, in->pointattributelist,
in->pointmarkerlist, in->numberofpoints,
in->numberofpointattributes);
.....
}
在此函数结束时,poolalloc函数为顶点循环分配一个内存地址。此内存地址无效,并发生访问冲突。
void transfernodes(struct mesh *m, struct behavior *b, float *pointlist,
float *pointattriblist, int *pointmarkerlist,
int numberofpoints, int numberofpointattribs)
{
vertex vertexloop;
float x, y;
int i, j;
int coordindex;
int attribindex;
m->invertices = numberofpoints;
m->mesh_dim = 2;
m->nextras = numberofpointattribs;
m->readnodefile = 0;
if (m->invertices < 3) {
printf("Error: Input must have at least three input vertices.\n");
triexit(1);
}
if (m->nextras == 0) {
b->weighted = 0;
}
initializevertexpool(m, b);
/* Read the vertices. */
coordindex = 0;
attribindex = 0;
for (i = 0; i < m->invertices; i++) {
vertexloop = (vertex) poolalloc(&m->vertices);
//vertexloop = (vertex) &m->vertices;
/* Read the vertex coordinates. */
x = vertexloop[0] = pointlist[coordindex++];
y = vertexloop[1] = pointlist[coordindex++];
....
}
这是poolalloc func
int *poolalloc(struct memorypool *pool)
{
int *newitem;
int **newblock;
unsigned long alignptr;
/* First check the linked list of dead items. If the list is not */
/* empty, allocate an item from the list rather than a fresh one. */
if (pool->deaditemstack != (int *)NULL) {
newitem = pool->deaditemstack; /* Take first item in list. */
pool->deaditemstack = *(int **)pool->deaditemstack;
}
else {
/* Check if there are any free items left in the current block. */
if (pool->unallocateditems == 0) {
/* Check if another block must be allocated. */
if (*(pool->nowblock) == (int *)NULL) {
/* Allocate a new block of items, pointed to by the previous block. */
newblock = (int **)trimalloc(pool->itemsperblock * pool->itembytes +
(int) sizeof(int *) +
pool->alignbytes);
*(pool->nowblock) = (int *)newblock;
/* The next block pointer is NULL. */
*newblock = (int *)NULL;
}
/* Move to the new block. */
pool->nowblock = (int **) *(pool->nowblock);
/* Find the first item in the block. */
/* Increment by the size of (int *). */
alignptr = (unsigned long) (pool->nowblock + 1);
/* Align the item on an `alignbytes'-byte boundary. */
pool->nextitem = (int *)
(alignptr + (unsigned long) pool->alignbytes -
(alignptr % (unsigned long) pool->alignbytes));
/* There are lots of unallocated items left in this block. */
pool->unallocateditems = pool->itemsperblock;
}
/* Allocate a new item. */
newitem = pool->nextitem;
/* Advance `nextitem' pointer to next free item in block. */
pool->nextitem = (int *)((char *)pool->nextitem + pool->itembytes);
pool->unallocateditems--;
pool->maxitems++;
}
pool->items++;
return newitem;
}
好的,我已经解决了同样的问题。
triangle.c中的代码假定sizeof(long)等于8,并使用unsigned long作为指针的类型。但是sizeof(long)在VS中等于4,所以unsigned long不能是x64中指针的类型。
我只是在triangle.c中用“__int64”替换所有“long”,代码可以工作。