473,782 Members | 2,458 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 WORDINVALIDVALU E ULONG_MAX
#define FLOATINVALIDVAL UE FLT_MAX

typedef unsigned long WORD;

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

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

static const Vertex UNDEFINEDVERTEX = {FLT_MAX,FLT_MA X,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_MA X};

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

extern Color *new_Color(doub le red, double green, double blue, double
alpha);
extern void delete_Color(Co lor *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<Ver tex vertices;

std::vector<uns igned long polygonNbVertic es;

std::vector<uns igned long polygonStartVer texIndex;

std::vector<uns igned long polygonVertices Indices;

std::vector<UV uvs;

std::vector<uns igned long polygonNbUVs;

std::vector<uns igned long polygonStartUVI ndex;

std::vector<uns igned long polygonUVsIndic es;

Material material;
} PolygonMesh;

extern PolygonMesh *new_PolygonMes h();
extern void delete_PolygonM esh(PolygonMesh *p);
extern unsigned long PolygonMesh_cou ntvertices(Poly gonMesh *p);
extern unsigned long PolygonMesh_cou ntpolygons(Poly gonMesh *p);
extern void PolygonMesh_app endvertex(Polyg onMesh *p, Vertex *vertex);
extern Vertex PolygonMesh_get vertex(PolygonM esh *p, unsigned long
vertexIndex);
extern void PolygonMesh_app endpolygon(Poly gonMesh *p, const
std::vector<uns igned long>& verticesIndices );
extern long PolygonMesh_get polygonvertices count(PolygonMe sh *p,
unsigned long polygonIndex);
extern std::vector<uns igned long>
PolygonMesh_get polygonvertices indices(Polygon Mesh *p, unsigned long
polygonIndex);

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

#include "tdimport.h "

Vertex *new_Vertex(dou ble 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(V ertex *v)
{
free(v);
}

double Vertex_length(V ertex *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(doub le 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(Co lor *color)
{
free(color);
}

PolygonMesh *new_PolygonMes h()
{
PolygonMesh *p;
p = (PolygonMesh *)malloc(sizeof (PolygonMesh));
p->vertices.clear ();
p->polygonNbVerti ces.clear();
p->polygonStartVe rtexIndex.clear ();
p->polygonVertice sIndices.clear( );
p->uvs.clear();
p->polygonNbUVs.c lear();
p->polygonStartUV Index.clear();
p->polygonUVsIndi ces.clear();
return p;
}

void delete_PolygonM esh(PolygonMesh *p)
{
free(p);
}

unsigned long PolygonMesh_cou ntvertices(Poly gonMesh *p)
{
return (unsigned long)p->vertices.size( );
}

unsigned long PolygonMesh_cou ntpolygons(Poly gonMesh *p)
{
return (unsigned long)p->polygonNbVerti ces.size();
}

void PolygonMesh_app endvertex(Polyg onMesh *p, Vertex *vertex)
{
p->vertices.push_ back(*vertex);
}

void PolygonMesh_app endpolygon(Poly gonMesh *p, const
std::vector<uns igned long>& verticesIndices )
{
unsigned int i;
for ( i = 0 ; i < verticesIndices .size() ; i++ )
p->polygonVertice sIndices.push_b ack(verticesInd ices.at(i));
p->polygonStartVe rtexIndex.push_ back(p-
>polygonVertice sIndices.size()-verticesIndices .size());
p->polygonNbVerti ces.push_back(v erticesIndices. size());
}

Vertex PolygonMesh_get vertex(PolygonM esh *p, unsigned long
vertexIndex)
{
if (vertexIndex < 0 || vertexIndex>=p->vertices.size( ) )
{
return UNDEFINEDVERTEX ;
}
else
{
return p->vertices.at(ve rtexIndex);
}
}

long PolygonMesh_get polygonvertices count(PolygonMe sh *p, unsigned long
polygonIndex)
{
if ((polygonIndex < 0) || (polygonIndex >= p-
>polygonStartVe rtexIndex.size( )))
{
return (long)-1;
}
else
{
return (long)p->polygonNbVerti ces.at(polygonI ndex);
}
}

std::vector<uns igned long>
PolygonMesh_get polygonvertices indices(Polygon Mesh *p, unsigned long
polygonIndex)
{
std::vector<uns igned longtmp;
tmp.clear();
if ((polygonIndex < 0) || (polygonIndex >= p-
>polygonStartVe rtexIndex.size( )))
{
return tmp;
}
else
{
unsigned long count = p->polygonNbVerti ces.at(polygonI ndex);
unsigned long start = p->polygonStartVe rtexIndex.at(po lygonIndex);
for (unsigned long i=0; i<count; i++)
tmp.push_back(p->polygonVertice sIndices.at(i+s tart));
return tmp;
}
}

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

namespace std {
%template(IntVe ctor) vector<int>;
%template(UIVec tor) vector<unsigned long>;
%template(Verte xVector) vector<Vertex>;
%template(UVVec tor) 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<Ver tex vertices;

std::vector<uns igned long polygonNbVertic es;

std::vector<uns igned long polygonStartVer texIndex;

std::vector<uns igned long polygonVertices Indices;

std::vector<UV uvs;

std::vector<uns igned long polygonNbUVs;

std::vector<uns igned long polygonStartUVI ndex;

std::vector<uns igned long polygonUVsIndic es;

Material material;

%extend
{
PolygonMesh();
~PolygonMesh();
unsigned long countvertices() ;
unsigned long countpolygons() ;
void appendvertex(Ve rtex*);
Vertex getvertex (unsigned long) ;
void appendpolygon(c onst std::vector<uns igned long>&);
long getpolygonverti cescount(unsign ed long);
std::vector<uns igned longgetpolygonv erticesindices( unsigned
long);
}
} PolygonMesh;
/////////////////////////
////python file
/////////////////////////
#!/usr/local/bin/python

import tdimport

a = tdimport.Polygo nMesh()

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.getpolygonver ticescount(0)

print b

del a

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


Sep 16 '08 #1
0 1051

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

Similar topics

0
2092
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 attempting the example. Do I need some extra switches to get SWIG to create that function? Or is it something else? Here's my example build:
0
2506
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 testerl extern int hz(int i);
13
3861
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 modifications along the way to make the interface more Pythonic. For example, all of these functions return an error code (typically just errno passed along, but not always). They all accept as one of their arguments a pointer to someplace to store...
0
1382
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, which BTW works fine on Fedora 5. I suspect it has to do with Python on AIX and/or the setup.py script. Has anyone experienced this before? MAT-2.04182006python setup.py install Found make and swig.
6
3972
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
1896
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, Boost.Python, and GCC_XML. Thanks!
6
3062
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 suggest a way to read some bits from random ports? I'm looking to write a simple pen-testing tool that would try from one side connecting to ports and from the other side sniff traffic to see on what ports traffic is coming.
3
3695
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 -IC:\python24\include " to create example_wrap.o But when I needed to compile the library file using:
3
1709
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 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...
0
9641
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9944
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.