bool InitOCL(const unsigned int oclPlatformID, const unsigned int oclDeviceID, OCLHelper& oclHelper, std::ostringstream& msg)
{

    bool res = true;
    vector<string> info;
    res = oclHelper.GetPlatformsInfo(info, "\t\t");
    if (!res)
        return res;

    const size_t numPlatforms = info.size();
    msg << "\t Number of OpenCL platforms: " << numPlatforms << endl;
    for (size_t i = 0; i < numPlatforms; ++i) {
        msg << "\t OpenCL platform [" << i << "]" << endl;
        msg << info[i];
    }
    msg << "\t Using OpenCL platform [" << oclPlatformID << "]" << endl;
    res = oclHelper.InitPlatform(oclPlatformID);
    if (!res)
        return res;

    info.clear();
    res = oclHelper.GetDevicesInfo(info, "\t\t");
    if (!res)
        return res;

    const size_t numDevices = info.size();
    msg << "\t Number of OpenCL devices: " << numDevices << endl;
    for (size_t i = 0; i < numDevices; ++i) {
        msg << "\t OpenCL device [" << i << "]" << endl;
        msg << info[i];
    }
    msg << "\t Using OpenCL device [" << oclDeviceID << "]" << endl;
    res = oclHelper.InitDevice(oclDeviceID);
    return res;
}
int main(int argc, char* argv[])
{
    // --input camel.off --output camel_acd.wrl --log log.txt --resolution 1000000 --depth 20 --concavity 0.0025 --planeDownsampling 4 --convexhullDownsampling 4 --alpha 0.05 --beta 0.05 --gamma 0.00125 --pca 0 --mode 0 --maxNumVerticesPerCH 256 --minVolumePerCH 0.0001 --convexhullApproximation 1 --oclDeviceID 2
    {
        // set parameters
        Parameters params;
        ParseParameters(argc, argv, params);
        MyCallback myCallback;
        MyLogger myLogger(params.m_fileNameLog);
        params.m_paramsVHACD.m_logger = &myLogger;
        params.m_paramsVHACD.m_callback = &myCallback;
        Usage(params);
        if (!params.m_run) {
            return 0;
        }

        std::ostringstream msg;
#ifdef CL_VERSION_1_1
        msg << "+ OpenCL (ON)" << std::endl;
        OCLHelper oclHelper;
        if (params.m_paramsVHACD.m_oclAcceleration) {
            bool res = InitOCL(params.m_oclPlatformID,
                               params.m_oclDeviceID,
                               oclHelper,
                               msg);
            if (!res) {
                myLogger.Log(msg.str().c_str());
                return -1;
            }
        }
#else //CL_VERSION_1_1
        msg << "+ OpenCL (OFF)" << std::endl;
#endif //CL_VERSION_1_1

#ifdef _OPENMP
        msg << "+ OpenMP (ON)" << std::endl;
#else
        msg << "+ OpenMP (OFF)" << std::endl;
#endif
        msg << "+ Parameters" << std::endl;
        msg << "\t input                                       " << params.m_fileNameIn << endl;
        msg << "\t resolution                                  " << params.m_paramsVHACD.m_resolution << endl;
        msg << "\t max. depth                                  " << params.m_paramsVHACD.m_depth << endl;
        msg << "\t max. concavity                              " << params.m_paramsVHACD.m_concavity << endl;
        msg << "\t plane down-sampling                         " << params.m_paramsVHACD.m_planeDownsampling << endl;
        msg << "\t convex-hull down-sampling                   " << params.m_paramsVHACD.m_convexhullDownsampling << endl;
        msg << "\t alpha                                       " << params.m_paramsVHACD.m_alpha << endl;
        msg << "\t beta                                        " << params.m_paramsVHACD.m_beta << endl;
        msg << "\t maxhulls                                    " << params.m_paramsVHACD.m_maxConvexHulls << endl;
        msg << "\t pca                                         " << params.m_paramsVHACD.m_pca << endl;
        msg << "\t mode                                        " << params.m_paramsVHACD.m_mode << endl;
        msg << "\t max. vertices per convex-hull               " << params.m_paramsVHACD.m_maxNumVerticesPerCH << endl;
        msg << "\t min. volume to add vertices to convex-hulls " << params.m_paramsVHACD.m_minVolumePerCH << endl;
        msg << "\t convex-hull approximation                   " << params.m_paramsVHACD.m_convexhullApproximation << endl;
        msg << "\t OpenCL acceleration                         " << params.m_paramsVHACD.m_oclAcceleration << endl;
        msg << "\t OpenCL platform ID                          " << params.m_oclPlatformID << endl;
        msg << "\t OpenCL device ID                            " << params.m_oclDeviceID << endl;
        msg << "\t output                                      " << params.m_fileNameOut << endl;
        msg << "\t log                                         " << params.m_fileNameLog << endl;
        msg << "+ Load mesh" << std::endl;
        myLogger.Log(msg.str().c_str());

        cout << msg.str().c_str();

        // load mesh
        vector<float> points;
        vector<int> triangles;
        string fileExtension;
        GetFileExtension(params.m_fileNameIn, fileExtension);
        //cout<<"params.m_fileNameIn="<<params.m_fileNameIn<<" and fileExtension="<<fileExtension<<endl;
        if (fileExtension == ".OFF") {
            if (!LoadOFF(params.m_fileNameIn, points, triangles, myLogger)) {
                cout<<"load OFF file error"<<endl;
                return -1;
            }
        }
        else if (fileExtension == ".OBJ") {
            if (!LoadOBJ(params.m_fileNameIn, points, triangles, myLogger)) {
                cout<<"load OBJ file error"<<endl;
                return -1;
            }
        }
        else {
            myLogger.Log("Format not supported!\n");
            return -1;
        }

        // run V-HACD
        IVHACD* interfaceVHACD = CreateVHACD();

#ifdef CL_VERSION_1_1
        if (params.m_paramsVHACD.m_oclAcceleration) {
            bool res = interfaceVHACD->OCLInit(oclHelper.GetDevice(), &myLogger);
            if (!res) {
                params.m_paramsVHACD.m_oclAcceleration = false;
            }
        }
#endif //CL_VERSION_1_1
        bool res = interfaceVHACD->Compute(&points[0], 3, (unsigned int)points.size() / 3,
                &triangles[0], 3, (unsigned int)triangles.size() / 3, params.m_paramsVHACD);
        if (res) {
            std::string ext;
            if (params.m_fileNameOut.length() > 4) {
                ext = params.m_fileNameOut.substr(params.m_fileNameOut.length()-4);
            }
            std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
            if (ext != ".obj") {
                // save output
                unsigned int nConvexHulls = interfaceVHACD->GetNConvexHulls();
                msg.str("");
                msg << "+ Generate output: " << nConvexHulls << " convex-hulls " << endl;
                myLogger.Log(msg.str().c_str());
                ofstream foutCH(params.m_fileNameOut.c_str());
                IVHACD::ConvexHull ch;
                if (foutCH.is_open()) {
                    /******* Phuc : Save nb of convex part decomposition *******/
                    ofstream outFile;
                    std::string filename;
                    filename=params.m_fileNameOut.substr(0,params.m_fileNameOut.length()-4);//../../results/egea_acd_0.dat
                    ///filename=params.m_fileNameIn.substr(0,params.m_fileNameOut.length()-4);
                    filename=filename+".txt";
                    outFile.open(filename.c_str());
                    if (!outFile.is_open()) {
                        cerr<<"! ERROR: can NOT open file : "<<filename<<endl;
                        exit(1);
                    }
                    outFile<<nConvexHulls<<endl;
                    outFile.close();
                    /******* Phuc : Save nb of convex part decomposition *******/
                    Material mat;
                    for (unsigned int p = 0; p < nConvexHulls; ++p) {
                        interfaceVHACD->GetConvexHull(p, ch);
                        ComputeRandomColor(mat);
                        SaveVRML2(foutCH, ch.m_points, ch.m_triangles, ch.m_nPoints, ch.m_nTriangles, mat, myLogger);
                        msg.str("");
                        msg << "\t CH[" << setfill('0') << setw(5) << p << "] " << ch.m_nPoints << " V, " << ch.m_nTriangles << " T" << endl;
                        myLogger.Log(msg.str().c_str());
#ifdef SAVE_PART
                        /****** Phuc : Save convex-part decomposition *********/
                        filename=params.m_fileNameOut.substr(0,params.m_fileNameOut.length()-4);//../../results/egea_acd_0.dat
                        ///filename=params.m_fileNameIn.substr(0,params.m_fileNameIn.length()-4);//../../data/egea_0.dat
                        filename=filename+"_"+std::to_string(p)+".dat";
                        //cout<<filename<<endl;
                        ofstream fpart(filename.c_str());
                        if (fpart.is_open()) {
                            size_t nV = ch.m_nPoints * 3;
                            for (size_t v = 0; v < nV; v += 3) {
                                fpart << ch.m_points[v + 0] << " "
                                                            << ch.m_points[v + 1] << " "
                                                            << ch.m_points[v + 2] << std::endl;
                            }
                            fpart.close();
                        }
                        else
                            std::cout<<"Can't open file\n"<<std::endl;
                        /****** Phuc : Save convex-part decomposition *********/
#endif
                    }
                    foutCH.close();
                }
            }
            else {
                unsigned int nConvexHulls = interfaceVHACD->GetNConvexHulls();
                msg.str("");
                msg << "+ Generate output: " << nConvexHulls << " convex-hulls " << endl;
                myLogger.Log(msg.str().c_str());
                ofstream foutCH(params.m_fileNameOut.c_str());
                IVHACD::ConvexHull ch;
                if (foutCH.is_open()) {
                    /******* Phuc : Save nb of convex part decomposition *******/
                    ofstream outFile;
                    std::string filename;
                    filename=params.m_fileNameOut.substr(0,params.m_fileNameOut.length()-4);//../../results/egea_acd_0.dat
                    ///filename=params.m_fileNameIn.substr(0,params.m_fileNameIn.length()-4);
                    filename=filename+".txt";
                    outFile.open(filename.c_str());
                    if (!outFile.is_open()) {
                        cerr<<"! ERROR: can NOT open file : "<<filename<<endl;
                        exit(1);
                    }
                    outFile<<nConvexHulls<<endl;
                    outFile.close();
                    /******* Phuc : Save nb of convex part decomposition *******/

                    Material mat;
                    int vertexOffset = 1;//obj wavefront starts counting at 1...
                    for (unsigned int p = 0; p < nConvexHulls; ++p) {
                        interfaceVHACD->GetConvexHull(p, ch);
                        SaveOBJ(foutCH, ch.m_points, ch.m_triangles, ch.m_nPoints, ch.m_nTriangles, mat, myLogger, p, vertexOffset);
                        vertexOffset+=ch.m_nPoints;
                        msg.str("");
                        msg << "\t CH[" << setfill('0') << setw(5) << p << "] " << ch.m_nPoints << " V, " << ch.m_nTriangles << " T" << endl;
                        myLogger.Log(msg.str().c_str());
#ifdef SAVE_PART
                        /****** Phuc : Save convex-part decomposition *********/
                        filename=params.m_fileNameOut.substr(0,params.m_fileNameOut.length()-4);//../../results/egea_acd_0.dat
                        ///filename=params.m_fileNameIn.substr(0,params.m_fileNameIn.length()-4);//../../data/egea_0.dat
                        filename=filename+"_"+std::to_string(p)+".dat";
                        ofstream fpart(filename.c_str());
                        if (fpart.is_open()) {
                            size_t nV = ch.m_nPoints * 3;
                            for (size_t v = 0; v < nV; v += 3) {
                                fpart << ch.m_points[v + 0] << " "
                                                            << ch.m_points[v + 1] << " "
                                                            << ch.m_points[v + 2] << std::endl;
                            }
                            fpart.close();
                        }
                        else
                            std::cout<<"Can't open file\n"<<std::endl;
                        /****** Phuc : Save convex-part decomposition *********/
#endif
                    }
                    foutCH.close();
                }
            }
        }
        else {
            myLogger.Log("Decomposition cancelled by user!\n");
        }

#ifdef CL_VERSION_1_1
        if (params.m_paramsVHACD.m_oclAcceleration) {
            bool res = interfaceVHACD->OCLRelease(&myLogger);
            if (!res) {
                assert(-1);
            }
        }
#endif //CL_VERSION_1_1

        interfaceVHACD->Clean();
        interfaceVHACD->Release();
    }
#ifdef _CRTDBG_MAP_ALLOC
    _CrtDumpMemoryLeaks();
#endif // _CRTDBG_MAP_ALLOC
    return 0;
}
Ejemplo n.º 3
0
int main(int argc, char * argv[])
{
    // --input camel.off --output camel_acd.wrl --log log.txt --resolution 1000000 --depth 20 --concavity 0.0025 --planeDownsampling 4 --convexhullDownsampling 4 --alpha 0.05 --beta 0.05 --gamma 0.00125 --pca 0 --mode 0 --maxNumVerticesPerCH 256 --minVolumePerCH 0.0001 --convexhullApproximation 1 --oclDeviceID 2
    {
        // set parameters
        Parameters params;
        ParseParameters(argc, argv, params);
        MyCallback myCallback;
        MyLogger   myLogger(params.m_fileNameLog);
        params.m_paramsVHACD.m_logger = &myLogger;
        params.m_paramsVHACD.m_callback = &myCallback;
        Usage(params);
        if (!params.m_run)
        {
            return 0;
        }

        std::ostringstream msg;
#ifdef CL_VERSION_1_1
        msg << "+ OpenCL (ON)" << std::endl;
        OCLHelper    oclHelper;
        if (params.m_paramsVHACD.m_oclAcceleration)
        {
            bool res = InitOCL(params.m_oclPlatformID, 
                               params.m_oclDeviceID, 
                               oclHelper, 
                               msg);
            if (!res)
            {
                myLogger.Log(msg.str().c_str());
                return -1;
            }
        }
#else //CL_VERSION_1_1
        msg << "+ OpenCL (OFF)" << std::endl;
#endif //CL_VERSION_1_1

#ifdef _OPENMP
        msg << "+ OpenMP (ON)" << std::endl;
#else
        msg << "+ OpenMP (OFF)" << std::endl;
#endif
        msg << "+ Parameters" << std::endl;
        msg << "\t input                                       " << params.m_fileNameIn                            << endl;
        msg << "\t resolution                                  " << params.m_paramsVHACD.m_resolution              << endl;
        msg << "\t max. depth                                  " << params.m_paramsVHACD.m_depth                   << endl;
        msg << "\t max. concavity                              " << params.m_paramsVHACD.m_concavity               << endl;
        msg << "\t plane down-sampling                         " << params.m_paramsVHACD.m_planeDownsampling       << endl;
        msg << "\t convex-hull down-sampling                   " << params.m_paramsVHACD.m_convexhullDownsampling  << endl;
        msg << "\t alpha                                       " << params.m_paramsVHACD.m_alpha                   << endl;
        msg << "\t beta                                        " << params.m_paramsVHACD.m_beta                    << endl;
        msg << "\t gamma                                       " << params.m_paramsVHACD.m_gamma                   << endl;
        msg << "\t pca                                         " << params.m_paramsVHACD.m_pca                     << endl;
        msg << "\t mode                                        " << params.m_paramsVHACD.m_mode                    << endl;
        msg << "\t max. vertices per convex-hull               " << params.m_paramsVHACD.m_maxNumVerticesPerCH     << endl;
        msg << "\t min. volume to add vertices to convex-hulls " << params.m_paramsVHACD.m_minVolumePerCH          << endl;
        msg << "\t convex-hull approximation                   " << params.m_paramsVHACD.m_convexhullApproximation << endl;
        msg << "\t OpenCL acceleration                         " << params.m_paramsVHACD.m_oclAcceleration         << endl;
        msg << "\t OpenCL platform ID                          " << params.m_oclPlatformID                         << endl;
        msg << "\t OpenCL device ID                            " << params.m_oclDeviceID                           << endl;
        msg << "\t output                                      " << params.m_fileNameOut                           << endl;
        msg << "\t log                                         " << params.m_fileNameLog                           << endl;
        msg << "+ Load mesh" << std::endl;
        myLogger.Log(msg.str().c_str());

        cout << msg.str().c_str();

        // load mesh
        vector<float > points;
        vector<int > triangles;
        string fileExtension;
        GetFileExtension(params.m_fileNameIn, fileExtension);
        if (fileExtension == ".OFF")
        {
            if (!LoadOFF(params.m_fileNameIn, points, triangles, myLogger))
            {
                return -1;
            }
        }
        else if (fileExtension == ".OBJ")
        {
            if (!LoadOBJ(params.m_fileNameIn, points, triangles, myLogger))
            {
                return -1;
            }
        }
        else
        {
            myLogger.Log("Format not supported!\n");
            return -1;
        }

        // run V-HACD
        IVHACD * interfaceVHACD = CreateVHACD();

#ifdef CL_VERSION_1_1
        if (params.m_paramsVHACD.m_oclAcceleration)
        {
            bool res = interfaceVHACD->OCLInit(oclHelper.GetDevice(), &myLogger);
            if (!res)
            {
                params.m_paramsVHACD.m_oclAcceleration = false;
            }
        }
#endif //CL_VERSION_1_1
        bool res = interfaceVHACD->Compute(&points[0]   , 3, (unsigned int)points.size()    / 3,
                                           &triangles[0], 3, (unsigned int)triangles.size() / 3, params.m_paramsVHACD);
        if (res)
        {
            // save output
            unsigned int nConvexHulls = interfaceVHACD->GetNConvexHulls();
            msg.str("");
            msg << "+ Generate output: " << nConvexHulls << " convex-hulls " << endl;
            myLogger.Log(msg.str().c_str());
            ofstream foutCH(params.m_fileNameOut.c_str());
            IVHACD::ConvexHull ch;
            if (foutCH.is_open())
            {
                Material mat;
                for (unsigned int p = 0; p < nConvexHulls; ++p)
                {
                    interfaceVHACD->GetConvexHull(p, ch);
                    ComputeRandomColor(mat);
                    SaveVRML2(foutCH, ch.m_points, ch.m_triangles, ch.m_nPoints, ch.m_nTriangles, mat, myLogger);
                    msg.str("");
                    msg << "\t CH[" << setfill('0') << setw(5) << p << "] " << ch.m_nPoints << " V, " << ch.m_nTriangles << " T" << endl;
                    myLogger.Log(msg.str().c_str());
                }
                foutCH.close();
            }
        }
        else
        {
            myLogger.Log("Decomposition cancelled by user!\n");
        }

#ifdef CL_VERSION_1_1
        if (params.m_paramsVHACD.m_oclAcceleration)
        {
            bool res = interfaceVHACD->OCLRelease(&myLogger);
            if (!res)
            {
                assert(-1);
            }
        }
#endif //CL_VERSION_1_1

        interfaceVHACD->Clean();
        interfaceVHACD->Release();
    }
#ifdef _CRTDBG_MAP_ALLOC
    _CrtDumpMemoryLeaks();
#endif // _CRTDBG_MAP_ALLOC
    return 0;
}