473,326 Members | 2,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

help on python SWIG C++ extension

RLC
Hello

I am new to python SWIG. Recently I wrote a small program trying to
import collada files(using colladadom) into python so I could use
python cgkit to render them. However, during the progressing, I got
some problems. Every time I quit from Python, I get a segmentation
fault, although the main program runs well. I suspect it is because I
wrapped std::vector objects in C struct and I did not release the
memory properly. Below is the code. I am working on FreeBSD 7.0, GCC
4.2.1. I will be very appreciated if you could help me. Thanks a lot.

the error I have got
Segmentation fault: 11 (core dumped)
////////////////////////
///Header file
////////////////////////

#ifndef TDIMPORT_H
#define TDIMPORT_H
#include <string>
#include <vector>
#include "math.h"
#include "float.h"

#define WORDINVALIDVALUE ULONG_MAX
#define FLOATINVALIDVALUE FLT_MAX

typedef unsigned long WORD;

typedef struct
{
double x,y,z;
} Vertex;

extern Vertex *new_Vertex(double x, double y, double z);
extern void delete_Vertex(Vertex *v);
extern double Vertex_length(Vertex *v);

static const Vertex UNDEFINEDVERTEX = {FLT_MAX,FLT_MAX,FLT_MAX};

typedef struct
{
double u,v;
} UV;

extern UV *new_UV(double u, double v);
extern void delete_UV(UV *uv);

static const UV UNDEFINEDUV = {FLT_MAX,FLT_MAX};

typedef struct
{
double red,green,blue,alpha;
} Color;

extern Color *new_Color(double red, double green, double blue, double
alpha);
extern void delete_Color(Color *color);

static const Color BLACK = {0.0, 0.0, 0.0, 1.0};
static const Color WHITE = {1.0, 1.0, 1.0, 1.0};

typedef struct
{
int type;
Color color;
std::string texture;
} Material;

typedef struct
{
std::vector<Vertex vertices;

std::vector<unsigned long polygonNbVertices;

std::vector<unsigned long polygonStartVertexIndex;

std::vector<unsigned long polygonVerticesIndices;

std::vector<UV uvs;

std::vector<unsigned long polygonNbUVs;

std::vector<unsigned long polygonStartUVIndex;

std::vector<unsigned long polygonUVsIndices;

Material material;
} PolygonMesh;

extern PolygonMesh *new_PolygonMesh();
extern void delete_PolygonMesh(PolygonMesh *p);
extern unsigned long PolygonMesh_countvertices(PolygonMesh *p);
extern unsigned long PolygonMesh_countpolygons(PolygonMesh *p);
extern void PolygonMesh_appendvertex(PolygonMesh *p, Vertex *vertex);
extern Vertex PolygonMesh_getvertex(PolygonMesh *p, unsigned long
vertexIndex);
extern void PolygonMesh_appendpolygon(PolygonMesh *p, const
std::vector<unsigned long>& verticesIndices);
extern long PolygonMesh_getpolygonverticescount(PolygonMesh *p,
unsigned long polygonIndex);
extern std::vector<unsigned long>
PolygonMesh_getpolygonverticesindices(PolygonMesh *p, unsigned long
polygonIndex);

#endif
//////////////////////////
//// implementation
//////////////////////////

#include "tdimport.h"

Vertex *new_Vertex(double x, double y, double z)
{
Vertex *v;
v = (Vertex *)malloc(sizeof(Vertex));
v->x = x;
v->y = y;
v->z = z;
return v;
}

void delete_Vertex(Vertex *v)
{
free(v);
}

double Vertex_length(Vertex *v)
{
return sqrt(v->x*v->x+v->y*v->y+v->z*v->z);
}

UV *new_UV(double u, double v)
{
UV *uv;
uv = (UV *)malloc(sizeof(UV));
uv->u = u;
uv->v = v;
return uv;
}

void delete_UV(UV *uv)
{
free(uv);
}

Color *new_Color(double r, double g, double b, double a)
{
Color *color;
color = (Color *)malloc(sizeof(Color));
color->red = r;
color->green = g;
color->blue = b;
color->alpha = a;
return color;
}

void delete_Color(Color *color)
{
free(color);
}

PolygonMesh *new_PolygonMesh()
{
PolygonMesh *p;
p = (PolygonMesh *)malloc(sizeof(PolygonMesh));
p->vertices.clear();
p->polygonNbVertices.clear();
p->polygonStartVertexIndex.clear();
p->polygonVerticesIndices.clear();
p->uvs.clear();
p->polygonNbUVs.clear();
p->polygonStartUVIndex.clear();
p->polygonUVsIndices.clear();
return p;
}

void delete_PolygonMesh(PolygonMesh *p)
{
free(p);
}

unsigned long PolygonMesh_countvertices(PolygonMesh *p)
{
return (unsigned long)p->vertices.size();
}

unsigned long PolygonMesh_countpolygons(PolygonMesh *p)
{
return (unsigned long)p->polygonNbVertices.size();
}

void PolygonMesh_appendvertex(PolygonMesh *p, Vertex *vertex)
{
p->vertices.push_back(*vertex);
}

void PolygonMesh_appendpolygon(PolygonMesh *p, const
std::vector<unsigned long>& verticesIndices)
{
unsigned int i;
for ( i = 0 ; i < verticesIndices.size() ; i++ )
p->polygonVerticesIndices.push_back(verticesIndices. at(i));
p->polygonStartVertexIndex.push_back(p-
>polygonVerticesIndices.size()-verticesIndices.size());
p->polygonNbVertices.push_back(verticesIndices.size( ));
}

Vertex PolygonMesh_getvertex(PolygonMesh *p, unsigned long
vertexIndex)
{
if (vertexIndex < 0 || vertexIndex>=p->vertices.size() )
{
return UNDEFINEDVERTEX;
}
else
{
return p->vertices.at(vertexIndex);
}
}

long PolygonMesh_getpolygonverticescount(PolygonMesh *p, unsigned long
polygonIndex)
{
if ((polygonIndex < 0) || (polygonIndex >= p-
>polygonStartVertexIndex.size()))
{
return (long)-1;
}
else
{
return (long)p->polygonNbVertices.at(polygonIndex);
}
}

std::vector<unsigned long>
PolygonMesh_getpolygonverticesindices(PolygonMesh *p, unsigned long
polygonIndex)
{
std::vector<unsigned longtmp;
tmp.clear();
if ((polygonIndex < 0) || (polygonIndex >= p-
>polygonStartVertexIndex.size()))
{
return tmp;
}
else
{
unsigned long count = p->polygonNbVertices.at(polygonIndex);
unsigned long start = p->polygonStartVertexIndex.at(polygonIndex);
for (unsigned long i=0; i<count; i++)
tmp.push_back(p->polygonVerticesIndices.at(i+start));
return tmp;
}
}

/////////////////////////
////SWIG interface
/////////////////////////
%module tdimport
%{
#include "tdimport.h"
%}
%include "std_string.i"
%include "std_vector.i"

namespace std {
%template(IntVector) vector<int>;
%template(UIVector) vector<unsigned long>;
%template(VertexVector) vector<Vertex>;
%template(UVVector) vector<UV>;
}

using namespace std;

typedef struct
{
double x,y,z;
%extend
{
Vertex (double,double,double);
~Vertex();
double length();
}
} Vertex;

typedef struct
{
double u,v;
%extend
{
UV (double,double);
~UV();
}
} UV;

typedef struct
{
double red,green,blue,alpha;
%extend
{
Color (double,double,double,double);
~Color();
}
} Color;

%apply const std::string& {std::string* texture};

typedef struct
{
int type;
Color color;
std::string texture;
} Material;

typedef struct
{

std::vector<Vertex vertices;

std::vector<unsigned long polygonNbVertices;

std::vector<unsigned long polygonStartVertexIndex;

std::vector<unsigned long polygonVerticesIndices;

std::vector<UV uvs;

std::vector<unsigned long polygonNbUVs;

std::vector<unsigned long polygonStartUVIndex;

std::vector<unsigned long polygonUVsIndices;

Material material;

%extend
{
PolygonMesh();
~PolygonMesh();
unsigned long countvertices();
unsigned long countpolygons();
void appendvertex(Vertex*);
Vertex getvertex (unsigned long) ;
void appendpolygon(const std::vector<unsigned long>&);
long getpolygonverticescount(unsigned long);
std::vector<unsigned longgetpolygonverticesindices(unsigned
long);
}
} PolygonMesh;
/////////////////////////
////python file
/////////////////////////
#!/usr/local/bin/python

import tdimport

a = tdimport.PolygonMesh()

a.appendvertex(tdimport.Vertex(1.0,0.0,0.0))
a.appendvertex(tdimport.Vertex(1.0,1.0,0.0))
a.appendvertex(tdimport.Vertex(1.0,1.0,1.0))

a.appendpolygon([1,2,3])

b = a.getpolygonverticescount(0)

print b

del a

////////////////////////////
///compile command
////////////////////////////
swig -c++ -python tdimport.i
g++ -c tdimport.cpp
g++ -c tdimport_wrap.cxx -I/usr/local/include/python2.5
g++ -shared tdimport.o tdimport_wrap.o -o _tdimport.so


Sep 16 '08 #1
0 1027

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Phil Schmidt | last post by:
I'm attempting to follow the instructions at http://sebsauvage.net/python/mingw.html, without luck. I have Python 2.3, MinGW 3.2.3, ans SWIG 1.3.19. I get an export error on "initexample" when...
0
by: Helmut Zeisel | last post by:
I want to build a static extension of Python using SWIG and VC++ 6.0 as described in http://www.swig.org/Doc1.3/Python.html#n8 for gcc. My file is testerl.i: ========================= %module...
13
by: Roy Smith | last post by:
I've got a C library with about 50 calls in it that I want to wrap in Python. I know I could use some tool like SWIG, but that will give me a too-literal translation; I want to make some...
0
by: John Ling | last post by:
Hello, I have been trying to install an application which requires Python: http://chip.dfci.harvard.edu/~wli/MAT/ My environment is AIX 5.2. Below is the section of the setup that has failed,...
6
by: matey | last post by:
I am have version 2.3.4. I want to write a python script to access a secure HTTPS. I tried the following: import urllib urllib.urlopen("https://somesecuresite.com") s = f.read() f.close()
9
by: bressert | last post by:
Hi Everyone, I'm considering about generating some Python Bindings for C++ libraries. What are considered the best tools for doing something like this? I know that there are SWIG, SIP,...
6
by: Maxim Veksler | last post by:
Hello, I wish to do some low level network stuff using python. I've googled somewhat and came up with pylibpcap, trouble is I can't compile it on my Ubuntu 6.10 workstation. Can someone please...
3
by: Soren | last post by:
Hi, I went through the SWIG tutorial for the example named "simple". I managed to get to the first step, creating example_wrap.c using swig, and doing: "gcc -fpic -c example_wrap.c...
3
by: RLC | last post by:
Hello I am new to python SWIG. Recently I wrote a small program trying to import collada files(using colladadom) into python so I could use python cgkit to render them. However, during the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.