Windows下編譯Triangle

地质狼發表於2024-08-02

前言

將我幾年前還是“小小小白”時(目前將自己定義為“小白”),編譯Triangle庫時的場景記錄了下來,那時候算是才開始接觸開源專案,很多東西都不懂,本著一顆不會不懂就瞎折騰的精神,一路走來,之前在cnblog自己的另一個賬號也記錄過,現在這個新註冊的號,買了會員,手動搬了過來。僅以此祭奠下這幾年逝去的時光。

下載原始碼

http://www.cs.cmu.edu/~quake/triangle.html

解壓

說明

showme是基於LINUX下的X11開發的,在windows下無法正常執行使,所以實際上用到的,也就是如下兩個檔案

編譯

環境

win10
vs2017

新建解決方案

新增現有項,並刪除原來項

修改專案屬性

初次編譯,報錯

修改標頭檔案

#ifndef _TRIANGLE_HEADER_  
#define _TRIANGLE_HEADER_
#ifdef _cplusplus
extern "C" {
#endif

#define REAL double  
#define ANSI_DECLARATORS  
#define VOID int
#include "triangle.h"

//_____________________________________________________________
//原來程式碼
//_____________________________________________________________

#ifdef _cplusplus
}
#endif//_cplusplus

#endif//_TRIANGLE_HEADER_

再次編譯

修改原始檔

再次編譯

新增前處理器

再次執行

修改原始檔

再次編譯

至此debug編譯成功

編譯release

修改release下的專案屬性

複製複製lib和標頭檔案

複製標頭檔案至C:\Shared_Folders\VCPKG_201912_installed_x64\include\triangle,注意此時複製的是修改後的標頭檔案

複製C:\Users\Administrator\Documents\Visual Studio 2017\Projects\libTriangle\x64\Debug\libTriangle.lib

C:\Shared_Folders\VCPKG_201912_installed_x64\debug\lib\libTriangled.lib
(注意重新命名,加以區別release與debug)
複製C:\Users\Administrator\Documents\Visual Studio 2017\Projects\libTriangle\x64\Release\libTriangle.lib

C:\Shared_Folders\VCPKG_201912_installed_x64\lib\libTriangle.lib

在目標專案裡呼叫編譯好的靜態庫

執行呼叫測試程式碼

    struct triangulateio in, mid, out, vorout;

    /* Define input points. */

    in.numberofpoints = 4;
    in.numberofpointattributes = 1;
    in.pointlist = (REAL *)malloc(in.numberofpoints * 2 * sizeof(REAL));
    in.pointlist[0] = 0.0;
    in.pointlist[1] = 0.0;
    in.pointlist[2] = 1.0;
    in.pointlist[3] = 0.0;
    in.pointlist[4] = 1.0;
    in.pointlist[5] = 10.0;
    in.pointlist[6] = 0.0;
    in.pointlist[7] = 10.0;
    in.pointattributelist = (REAL *)malloc(in.numberofpoints *
        in.numberofpointattributes *
        sizeof(REAL));
    in.pointattributelist[0] = 0.0;
    in.pointattributelist[1] = 1.0;
    in.pointattributelist[2] = 11.0;
    in.pointattributelist[3] = 10.0;
    in.pointmarkerlist = (int *)malloc(in.numberofpoints * sizeof(int));
    in.pointmarkerlist[0] = 0;
    in.pointmarkerlist[1] = 2;
    in.pointmarkerlist[2] = 0;
    in.pointmarkerlist[3] = 0;

    in.numberofsegments = 0;
    in.numberofholes = 0;
    in.numberofregions = 1;
    in.regionlist = (REAL *)malloc(in.numberofregions * 4 * sizeof(REAL));
    in.regionlist[0] = 0.5;
    in.regionlist[1] = 5.0;
    in.regionlist[2] = 7.0;            /* Regional attribute (for whole mesh). */
    in.regionlist[3] = 0.1;          /* Area constraint that will not be used. */

    printf("Input point set:\n\n");
    //report(&in, 1, 0, 0, 0, 0, 0);

    /* Make necessary initializations so that Triangle can return a */
    /*   triangulation in `mid' and a voronoi diagram in `vorout'.  */

    mid.pointlist = (REAL *)NULL;            /* Not needed if -N switch used. */
    /* Not needed if -N switch used or number of point attributes is zero: */
    mid.pointattributelist = (REAL *)NULL;
    mid.pointmarkerlist = (int *)NULL; /* Not needed if -N or -B switch used. */
    mid.trianglelist = (int *)NULL;          /* Not needed if -E switch used. */
    /* Not needed if -E switch used or number of triangle attributes is zero: */
    mid.triangleattributelist = (REAL *)NULL;
    mid.neighborlist = (int *)NULL;         /* Needed only if -n switch used. */
    /* Needed only if segments are output (-p or -c) and -P not used: */
    mid.segmentlist = (int *)NULL;
    /* Needed only if segments are output (-p or -c) and -P and -B not used: */
    mid.segmentmarkerlist = (int *)NULL;
    mid.edgelist = (int *)NULL;             /* Needed only if -e switch used. */
    mid.edgemarkerlist = (int *)NULL;   /* Needed if -e used and -B not used. */

    vorout.pointlist = (REAL *)NULL;        /* Needed only if -v switch used. */
    /* Needed only if -v switch used and number of attributes is not zero: */
    vorout.pointattributelist = (REAL *)NULL;
    vorout.edgelist = (int *)NULL;          /* Needed only if -v switch used. */
    vorout.normlist = (REAL *)NULL;         /* Needed only if -v switch used. */

    /* Triangulate the points.  Switches are chosen to read and write a  */
    /*   PSLG (p), preserve the convex hull (c), number everything from  */
    /*   zero (z), assign a regional attribute to each element (A), and  */
    /*   produce an edge list (e), a Voronoi diagram (v), and a triangle */
    /*   neighbor list (n).                                              */

    triangulate("pczAevn", &in, &mid, &vorout);

    printf("Initial triangulation:\n\n");
    //report(&mid, 1, 1, 1, 1, 1, 0);
    printf("Initial Voronoi diagram:\n\n");
    //report(&vorout, 0, 0, 0, 0, 1, 1);

    /* Attach area constraints to the triangles in preparation for */
    /*   refining the triangulation.                               */

    /* Needed only if -r and -a switches used: */
    mid.trianglearealist = (REAL *)malloc(mid.numberoftriangles * sizeof(REAL));
    mid.trianglearealist[0] = 3.0;
    mid.trianglearealist[1] = 1.0;

    /* Make necessary initializations so that Triangle can return a */
    /*   triangulation in `out'.                                    */

    out.pointlist = (REAL *)NULL;            /* Not needed if -N switch used. */
    /* Not needed if -N switch used or number of attributes is zero: */
    out.pointattributelist = (REAL *)NULL;
    out.trianglelist = (int *)NULL;          /* Not needed if -E switch used. */
    /* Not needed if -E switch used or number of triangle attributes is zero: */
    out.triangleattributelist = (REAL *)NULL;

    /* Refine the triangulation according to the attached */
    /*   triangle area constraints.                       */

    triangulate("prazBP", &mid, &out, (struct triangulateio *) NULL);

    printf("Refined triangulation:\n\n");
    //report(&out, 0, 1, 0, 0, 0, 0);

    /* Free all allocated arrays, including those allocated by Triangle. */

    free(in.pointlist);
    free(in.pointattributelist);
    free(in.pointmarkerlist);
    free(in.regionlist);
    free(mid.pointlist);
    free(mid.pointattributelist);
    free(mid.pointmarkerlist);
    free(mid.trianglelist);
    free(mid.triangleattributelist);
    free(mid.trianglearealist);
    free(mid.neighborlist);
    free(mid.segmentlist);
    free(mid.segmentmarkerlist);
    free(mid.edgelist);
    free(mid.edgemarkerlist);
    free(vorout.pointlist);
    free(vorout.pointattributelist);
    free(vorout.edgelist);
    free(vorout.normlist);
    free(out.pointlist);
    free(out.pointattributelist);
    free(out.trianglelist);
    free(out.triangleattributelist);

    QMessageBox::information(NULL, "INFO", "Finish", QMessageBox::Ok, QMessageBox::Ok);
    //return 0;
執行目標程式測試

我之前一直以為是自己編的測試資料不符合條件,所以才報錯。
後來發現用Triangle庫本身所提供的程式碼測試,仍然報錯。就懷疑是編譯除了問題。
但是編譯時成功。
無意間發現了一個部落格上提到的一個問題

https://blog.csdn.net/u010750137/article/details/106028070

抱著試一試的態度,修改了下。最終問題得到了解決。
重新編譯,生產靜態庫後再次替換。
開源開發很多都是坑啊~~~~~~

相關文章