chrisidefix / cgal-bindings

Automatically exported from code.google.com/p/cgal-bindings
Boost Software License 1.0
0 stars 0 forks source link

Hash function of a Point_2 #9

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi,

I noticed that a the hash given by the hash method of a Point_2 changes even if 
the Point have the same coordinates.

p1 = Point_2(1,1)
p2 = Point_2(1,1)

p1 == p2 returns True
p1.__hash__() == p2.__hash__() returns False

I don't know if this is a bug or not, but I think it would be better to have 
the same hash.

Thx

Original issue reported on code.google.com by francois...@gmail.com on 16 Feb 2012 at 3:07

GoogleCodeExporter commented 9 years ago
This is SWIG default.
What do you propose to implement that?

Original comment by sloriot...@gmail.com on 16 Feb 2012 at 6:10

GoogleCodeExporter commented 9 years ago
Mmm, I don't have any idea because I really don't know SWIG. 
Will think about that later, for the moment I avoid recording points in a 
python dict.

Original comment by francois...@gmail.com on 22 Feb 2012 at 3:47

GoogleCodeExporter commented 9 years ago
My question was more python related. If you know how to hash a pair of double 
in python then I can call that function from SWIG using the Python C/C++ API. 
I'll check later if you have no idea.

Original comment by sloriot...@gmail.com on 23 Feb 2012 at 9:32

GoogleCodeExporter commented 9 years ago
Hi, I'm not sure if this is a good idea to have a hash function for Point_2 
object. 
In fact, I was thinking about this solution because when I create a 
triangulations with some points, I have some data associated with these points, 
so what I finally want is more to be able to add an "id" to each point I insert 
in the triangulation. Is it possible to add extra data to a point in a 
triangulation in python ?
Thx

Original comment by francois...@gmail.com on 18 Apr 2012 at 9:45

GoogleCodeExporter commented 9 years ago
IIRC, the hash need to be an integer, so we can't do that anyway.
Usually, we add ids to vertices but you can for example add an id to a point by 
adding a public id member in the class Point_2 (SWIG_CGAL/Kernel/Point_2.h). 
Then recompile (cmake will not relaunch SWIG, so you probably need to fake a 
modification in CGAL_Kernel.i to force it).

Original comment by sloriot...@gmail.com on 18 Apr 2012 at 12:43

GoogleCodeExporter commented 9 years ago
I think I was not clear, I think also it is better to define an id for a 
vertice, it's not so relevant to define ones for a Point_2. In fact, I would 
prefer to add an id to the vertice in the triangulation (that's what I meant 
when I said "add extra data to a point in a triangulation", sorry).
So is it possible to add an id to a vertice in python ?
Thx

Original comment by francois...@gmail.com on 18 Apr 2012 at 1:00

GoogleCodeExporter commented 9 years ago
Yes. Actually this done for Java.
You need two things:
change the typedef for CGAL_DT2 in SWIG_CGAL/Triangulation_2/typedefs.h so that 
the cpp vertex type contains the info you want (line 34). It should look like 
something above.

Then on the SWIG side, you need to say that you want to extend the Vertex_class.
Modify CGAL_Triangulation_2.i and add line 23
#define i_Vertex_handle_DT2 
SWIG_Triangulation_2::CGAL_Vertex_handle<CGAL_DT2,Point_2>
%extend i_Vertex_handle_DT2 {
  int info(){
    return $self->get_data()->info();
  }
  void set_info(int i){
    $self->get_data()->info()=i;
  }
}
where int is the type of the id.

Original comment by sloriot...@gmail.com on 18 Apr 2012 at 1:23