[Tutor] Triangle structure for triangulation

Alan Gauld alan.gauld at btinternet.com
Thu Aug 30 23:26:59 CEST 2007


"János Juhász" <janos.juhasz at VELUX.com> wrote

>> > ## I can translate it into python in this way
>> > class Triangle:
>> >     def __init__(self, points, neighbours):
>> >     def TOR(self, direction):
>> >     def ROT(self, direction):
>> >     def RIGHT(self, direction):

>> and store it as an attribute. But it sounds like you need
>> to add some methods too. What do you do with these
>> triangles?

> The most important function is CalcZ(point(x, y)), that interpolates 
> the
> elevation on the surface of a triangle.
> For this interpolation I have to find the corresponding triangle of 
> the
> point, that can be made the fastest by walking from triangle to 
> triangle.
> This is the first reason I need the neighbours.

OK, That suggests to me that your triangle should have a predicate
method contains(aPoint) where aPoint is a tuple of x,y coords and
it returns a boolean. The iteration could then become a list
comprehension:

candidates = [t for t in triangles if t.contains(myPoint)]

candidates will be a list of all triangles containing the point
(hopefully a very short list!!)

And the calcZ function could be a method of the collection of
triangles if the collection were made into a class
- a TriangualtedSurface maybe?

> I also need to extend and update the triangulation, when a new point
> inserted into it.

OK That sounds like a job for the Surface class too, but with a fairt
bit of help from, the triangles within it. A lot of the adoption of 
OOP
is about rethinking how procedural style functionscan be split up with
the various responsibilities parceled out between the various objects.
The end result tends to be a lot of small methods within the various
classes. And the top level method either simply orchestrates the
execution of the helper methods in the component classes or starts
a cascade of method calls as each component does something then
delegates to its components (this is usually how GUIs work). I suspect
your surface model would be a candidate for the orchestration 
technique.

> I feel that, the best would be to strore 3 separate triangles for 
> A,B,C
> points,
>
> Triangulation.Append(Triangle(A,B,C))
> Triangulation.Append(Triangle(B,C,A))
> Triangulation.Append(Triangle(C,A,B))
> And register the topological relations after it. It could be OO, and
> simple.

OO is usually simpler that traditiona;l procedural once you get the 
knack.
Its because you delegate the functions to where the data is so all
processing tends to be simplified. The big down side is that you can
end up with very deep call stacks, but in practice this is rarely as
big a problem as traditionalists expected when OOP was introduced.

> As I wrote, I made it in C with structures, so it wasn't OO, but 
> fast.

OO doesn't need to be slow, especially in C++ where some OOP programs
have been shown to be faster than their non OOP equiva;lents. But in
Python speed of execution is almost never the primary aim, "fast 
enough"
and easy to build and maintain are the usual goals.

> Except now, when the algorithm based strongly on pointers and 
> indeces :(

Real low level pointer arithmetic can be tricky to convert 
pythonically
but normal array indexing is nt usually an issue. Either a while loop
or a for loop will do the job. But an OOP approach can often render
both unnecessary, or at least greatly simplify things.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list