Home > Enterprise >  C function not getting called
C function not getting called

Time:05-05

I have a function that takes 2d-vector and outputs a 2d-vector. For some reason, the function is not getting called.
Here is the link to reproduce the issue: Google Colab.
In the link to check for correctness, I have added another code that uses the exact same function but doesn't take a 2d-vector array as an argument instead it runs on static input.
mycode.cpp:

#include <vector>
#include "geomutils.h"
#include "mycode.h"
#include <iostream>

using namespace std;

vector< vector<double> > customComputeConvexHull(vector< vector<double> > i_matrix){
  cout <<"\nDone1.1";
  Polygon custompts, customhull;
  for (int r = 0; r < i_matrix.size(); r  ){
    custompts.push_back(Point(i_matrix[r][0], i_matrix[r][1]));
  }
  computeConvexHull(custompts, customhull);
  // vector< vector<double> > res;
  vector<vector<double>> res( customhull.size() , vector<double> (2));
  for(int i = 0;i < customhull.size();i   ) {
        res[i][0] = customhull[i].x;
        res[i][1] = customhull[i].y;
  }
  return res;
}

void print_polygon(Polygon &h, int name){
    std::cout << "\nHull in "<< name << ": \n"<<"[";
    for(int i = 0;i < h.size();i   ) {
        std::cout << "("<< h[i].x<< ", "<< h[i].y<<"), ";
    }
    std::cout <<"]\n";
}

void get_convex_hull_custom(){
    Polygon custompts;
    Polygon customhull;
    custompts.push_back(Point(0,0));
    custompts.push_back(Point(4.58,7.14));
    custompts.push_back(Point(0,7.14));
    computeConvexHull(custompts, customhull);
    print_polygon(customhull, -99999);
}


int main()
{
    // Create an empty vector
    vector< vector<double> > mat,mat2;
    
    vector<double> myRow1(0,0);
    mat.push_back(myRow1);

    vector<double> myRow2(7.61,9.48);
    mat.push_back(myRow2);
    
    vector<double> myRow3(0,9.48);
    mat.push_back(myRow3);
    cout <<"Done1\n";

    get_convex_hull_custom();

    mat2 = customComputeConvexHull(mat);
    cout <<"Done2";

    return 0;
}

mycode.h:

#ifndef _code
#define _code

#include <vector>

std::vector< std::vector<double> > customComputeConvexHull (std::vector< std::vector<double> > i_matrix);

#endif

geomutils.cpp:

#include "geomutils.h"

#include <iostream>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/adapted/boost_tuple.hpp>

BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)


void computeConvexHull(Polygon &pts, Polygon &chull) {
    chull.clear();
    if(pts.size() == 1) {
        chull.push_back(pts[0]);
        chull.push_back(pts[0]);
        return;
    } else if(pts.size() == 2) {
        chull.push_back(pts[0]);
        chull.push_back(pts[1]);
        chull.push_back(pts[0]);
        return;
    }

    typedef boost::tuple<double, double> point;
    typedef boost::geometry::model::multi_point<point> mpoints;
    typedef boost::geometry::model::polygon<point> polygon;

    mpoints mpts;

    for(int i = 0;i < pts.size();i   ) {
        boost::geometry::append(mpts,point(pts[i].x,pts[i].y));
    }
    polygon hull;

    // Polygon is closed
    boost::geometry::convex_hull(mpts, hull);
    for(auto pt : hull.outer()) {
        chull.push_back(Point(pt.get<0>(), pt.get<1>()));
    }
}

geomutils.h:

#ifndef GEOMUTILS_H
#define GEOMUTILS_H

#include <vector>

struct Point {
    double x,y;

    Point(){}
    Point(double x, double y):x(x),y(y){}
};

typedef std::vector<Point> Polygon;

void computeConvexHull(Polygon &pts, Polygon &chull);

#endif // GEOMUTILS_H

When I compile the code and try to run it. Only Done1 gets printed on the console. It neither gives any error nor any message. Output:

Done1

Hull in -99999: 
[(0, 0), (0, 7.14), (4.58, 7.14), (0, 0), ]

CodePudding user response:

There's some issue on your code. First of all in main to correctly initialize the vector you have to use the {} syntax. Further in customComputeConvexHull you are setting values inside the res vector which are not yet present. You have to use push_back to populate res. Below a version of your code which works (I put everything into one cpp file for semplicity.

#include <vector>
//#include "geomutils.h"
//#include "mycode.h"
#include <iostream>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/adapted/boost_tuple.hpp>

using namespace std;

struct Point {
    double x, y;

    Point() {}
    Point(double x, double y) :x(x), y(y) {}
};

typedef std::vector<Point> Polygon;

BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian);


void computeConvexHull(Polygon& pts, Polygon& chull) {
    chull.clear();
    if (pts.size() == 1) {
        chull.push_back(pts[0]);
        chull.push_back(pts[0]);
        return;
    }
    else if (pts.size() == 2) {
        chull.push_back(pts[0]);
        chull.push_back(pts[1]);
        chull.push_back(pts[0]);
        return;
    }

    typedef boost::tuple<double, double> point;
    typedef boost::geometry::model::multi_point<point> mpoints;
    typedef boost::geometry::model::polygon<point> polygon;

    mpoints mpts;

    for (int i = 0; i < pts.size(); i  ) {
        boost::geometry::append(mpts, point(pts[i].x, pts[i].y));
    }
    polygon hull;

    // Polygon is closed
    boost::geometry::convex_hull(mpts, hull);
    for (auto pt : hull.outer()) {
        chull.push_back(Point(pt.get<0>(), pt.get<1>()));
    }
}

vector< vector<double> > customComputeConvexHull(vector< vector<double> > i_matrix) {
    cout << "\nDone1.1";
    Polygon custompts, customhull;
    for (int r = 0; r < i_matrix.size(); r  ) {
        custompts.push_back(Point(i_matrix[r][0], i_matrix[r][1]));
    }
    computeConvexHull(custompts, customhull);
    vector< vector<double> > res;
    for (int i = 0; i < 3; i  ) 
    {
        vector<double> v1{ customhull[i].x,  customhull[i].y };
        res.push_back(v1);
    }
    return res;
}

int main()
{
    // Create an empty vector
    vector< vector<double> > mat, mat2;

    vector<double> myRow1{0, 0};
    mat.push_back(myRow1);

    vector<double> myRow2{ 7.61, 9.48 };
    mat.push_back(myRow2);

    vector<double> myRow3{ 0, 9.48 };
    mat.push_back(myRow3);
    cout << "Done1";

    mat2 = customComputeConvexHull(mat);
    cout << "Done2";

    return 0;
}
  •  Tags:  
  • c
  • Related