三角形库是否适用于 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 用户函数中
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 函数
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”,代码就可以工作了。
关于c++ - 带有 VS2015 x64 的三角形库,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44558793/