Doubts on how to do a c wrapper for python using SWIG

42 views Asked by At

Context

I have a function implemented in c that I would like to test in python environment. After some reads, I decided to use SWIG tool. A just did an simple example presented here, but I am still planty of doubts and so I decided to come here. The c project has the following files

  • ransac2Dline.h
  • ransac2Dline.c
  • svd.h
  • svd.c

and the fuction that I want to use is

int ransac_2Dline(float **data, int n, int maxT, float threshold,
                    float *bestModel, int *bestInliers, int verbose);

which is declared in the header ransac2Dline.h with other auxiliary functions. The header contents are

#ifndef RANSAC2DLINE_H
#define RANSAC2DLINE_H

#include <time.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "svd.hpp"

int ransac_2Dline(float **data, int n, int maxT, float threshold,
                    float *bestModel, int *bestInliers, int verbose);

int randomSelect(float **sel, int nsel, float **data, int *ndata);

int fitModel_line(float *point, float *l, float threshold);

void estimateModel_line(float *l, float **P, int n);

void twoPointsLine(float *l, float **P);

#endif /* RANSAC2DLINE_H */

.

Questions

i) I would like to know how exactly should I define the SWIG module .i. Since I want an interface just for ransac_2Dline function I guess the SWIG module needs to handle it. I mean someything like this

/* File: ransac.i */
%module ransac

%{
#define SWIG_FILE_WITH_INIT
#include "ransac2Dline.h"
%}

int ransac_2Dline(float **data, int n, int maxT, float threshold,
                      float *bestModel, int *bestInliers, int verbose);

. Is this right? Or should I do something different?

ii) The most import data computed by the ransac_2Dline function it is not returned by it (the returned int value is just an indicator of the function success in accomplish its task) but it is turned available in the outer scope by the point *bestModel. Can an interface be created by SWIG to simulated in python environment this approach?

iii) Is this approach a good solution or should I go with ctypes or anything else?

More infos

I am working with an Ubuntu 14.04.5 LTS, using g++ compiler and the python vesion 2.7.6.

I will really appreciate any help or tip (this subject is a little hard for me since I am a mechanical engineer that had the stupid idea of work with robotcs. :)).

1

There are 1 answers

0
John Greene On

Use gcc (and not g++). Also use swig -python ransac_2Dline.i (but without the -c++ option)