Etude (Learning of) des extension de Inkscape : cspsubdiv.py

Asked by Papy Octet

Bonjour,
Je me suis lancé dans l'étude des extensions de Inkscape avec comme idée de créer mes propres extensions en rapport avec l'utilisation très particulière que je fais de ce logiciel.

I am learning how to create my own extentions for Inkscape and in this gaol, I learn the existing extentions. Actually, I learn cspsubdiv.py but I have several problems to understand how it run.

Actuellement, j'essaye de comprendre le fonctionnement de l'extension : cspsubdiv.py.

Voici le code complet de cette extension :
Here is the complete code of this extention :

#!/usr/bin/env python
from bezmisc import * # pour la fonction beziersplitatt()
from ffgeom import * # pour les fonctions Point(), Segment()

def maxdist(((p0x,p0y),(p1x,p1y),(p2x,p2y),(p3x,p3y))):
    p0 = Point(p0x,p0y)
    p1 = Point(p1x,p1y)
    p2 = Point(p2x,p2y)
    p3 = Point(p3x,p3y)

    s1 = Segment(p0,p3)
    return max(s1.distanceToPoint(p1),s1.distanceToPoint(p2))

def cspsubdiv(csp,flat):
    for sp in csp:
        subdiv(sp,flat)

def subdiv(sp,flat,i=1):
    p0 = sp[i-1][1]
    p1 = sp[i-1][2]
    p2 = sp[i][0]
    p3 = sp[i][1]

    b = (p0,p1,p2,p3)
    m = maxdist(b)
    if m <= flat:
        try:
            subdiv(sp,flat,i+1)
        except IndexError:
            pass
    else:
        one, two = beziersplitatt(b,0.5)
        sp[i-1][2] = one[1]
        sp[i][0] = two[2]
        p = [one[2],one[3],two[1]]
        sp[i:1] = [p]
        subdiv(sp,flat,i)

Ce qui me pose problème ici :
What don't Y understand here :

Dans ces parties de code :
In this parts of the code :

1.
def maxdist(((p0x,p0y),(p1x,p1y),(p2x,p2y),(p3x,p3y))):
    p0 = Point(p0x,p0y)
    p1 = Point(p1x,p1y)
    p2 = Point(p2x,p2y)
    p3 = Point(p3x,p3y)

==> Que sont ces quatre points passés en paramètre de la fonction ?
         What are these four points passed as parameters to the function ?

2.
def cspsubdiv(csp,flat):
    for sp in csp:
        subdiv(sp,flat)

==> Qu'est que c'est cette variable "flat" ?
         What is the variable "flat" ?
==> Que représente-t-elle ?
         What does she represent ?
==> Comment est-elle passée à la fonction ?
         How do we pass this variable to the function ?

Merci de m'aider à comprendre et désolé si mon anglais n'est pas très bon.
Thank you for your help and sorry for my bad english.

Question information

Language:
French Edit question
Status:
Solved
For:
Inkscape Edit question
Assignee:
No assignee Edit question
Solved by:
Papy Octet
Solved:
Last query:
Last reply:
Revision history for this message
su_v (suv-lp) said :
#1

> Here is the complete code of this extention
'cspsubdiv.py' isn't an extension of its own - it contains helper functions which are loaded and called by other python scripts or extensions.

This python function library you are studying was written by Aaron Spike - he has a comprehensive but outdated ("This information remains as a historical reference") documentation on his website:
  <http://www.ekips.org/comp/inkscape/>
Maybe this helps to untangle the net of parameter and variables used by the helper functions.

Did you read the available information about the extension system in the Inkscape Wiki?
<http://wiki.inkscape.org/wiki/index.php/Extension_subsystem>
<http://wiki.inkscape.org/wiki/index.php/Category:Extensions>

You might get more specific answers if you explain the goals of your new extension and what doesn't work in your current implementation.

hth, ~suv

Revision history for this message
Papy Octet (guy-wallon) said :
#2

Yes, of course I read this pages but I do'nt find the answers to my 2 questions.

But I continue my explorations and I think I have founded an answer to my first question :
maxDist is working on Bezier cuves and we need 4 points to define un curve : the two endpoints and the controle point for each endpoint. That is why it is defined 4 points. Two points (p0 and p1) for the startpoint and two others points (p2 and p3) for the endpoint of the curve.
in the code " return max(s1.distanceToPoint(p1),s1.distanceToPoint(p2))", the instruction "distanceToPoint" come from the file "bezmisc.py" whitch can manipulate Bezier curves.

Am I in the write way whith this explanation ?

********************************
But I'm still question :
what is that variable "flat" passed as parameter in the function "cspsubdiv(csp,flat):"
I do'nt find any explanation to what does this parameter represent.
********************************

You ask me for the goals of my future extension.
I wil create several special extensions in Inkscape that get in accord with my needs for drawing lace. (Some examples of my lace creations in my blog : http://le-dentellier.skynetblogs.be/)
For example, I need to put on a segment (line, bezier curve, polyline, cercle, ...), in an other layer, "points" (I meen there, for example a cross wher the intersection of the two lines of the cross is the point where I can "attach" a point of the new polyline I draw) where the distance between two conscutive points increases (or decreases or both) progressively by an arithmetic progression (or geometric progression, logarithmic progression, ...). The parameters of that increase of the distance have to be introduce by the user before the "effect" is calculated and applicated.
(I hope you understand what I will do. For example, look here "http://easylace.co.uk/samples.htm" or there "http://www.xs4all.nl/~falkink/lace/SoftAlg-FR.html").

That is a first goal. Later, I will be able to create other types of extensions at therefore, I have to understant how the existing extensions function. Step by step, I begin to understant but some functions remain obscure for me like this I do not understant here.

A+

Revision history for this message
su_v (suv-lp) said :
#3

Thank you for the background information! Fascinating - will you report back on your progress?

About cspsubdiv(csp,flat): see the extension 'Extension > Modify Path > Flatten Beziers'. It calls the function cspsubdiv() with the selected path and its parameter 'Flatness' which is entered by the user in the extensions dialog window.
<http://inkscape.svn.sourceforge.net/viewvc/inkscape/inkscape/trunk/share/extensions/flatten.inx?view=markup>
<http://inkscape.svn.sourceforge.net/viewvc/inkscape/inkscape/trunk/share/extensions/flatten.py?view=markup>

In the python script you can read a minimal description of the parameter 'Flatness':
 "Minimum flatness of the subdivided curves"

hth, ~suv

p.s. I don't know which OS you use. On OS X (or linux), I open a terminal (shell), change to the directory where Inkscape's installed extensions are (${install_path_of_inkscape}/share/extensions) and search all python scripts for e.g. the name of a function:

   grep cspsubdiv *.py

which yields:

cspsubdiv.py:def cspsubdiv(csp,flat):
flatten.py:import inkex, cubicsuperpath, simplepath, cspsubdiv
flatten.py: cspsubdiv.cspsubdiv(p, self.options.flat)
hpgl_output.py:import inkex, cubicsuperpath, simplepath, simplestyle, cspsubdiv
hpgl_output.py: cspsubdiv.cspsubdiv(p, self.options.flat)

Revision history for this message
Papy Octet (guy-wallon) said :
#4

Thnak you for you answer.

Feed back for my progress : yes, naturally. I am so kind to work with the linux softwares that I will return all what I can to Linux and all the softwares I use. It is normally, for me to do so.
I have already tranlated several documentations from english to french and I give them on the web.

I look for the informations you give to me.
On my computer, I work whith Ubuntu Jaunty (I have forgotten to say it first !).

When I call : grep cspsubdiv*.py, I get :

cspsubdiv.py:def cspsubdiv(csp,flat):
flatten.py:import inkex, cubicsuperpath, simplepath, cspsubdiv
flatten.py: cspsubdiv.cspsubdiv(p, self.options.flat)

The files you have "hpgl_output.py" are not on my system. I suppose they are specials for your OS.

Thank you very very much for your help.

Revision history for this message
su_v (suv-lp) said :
#5

These links just illustrate the flatten effect, I could not find more references to the underlying math...

<http://wiki.inkscape.org/wiki/index.php/Effect_reference#Flatten_Beziers>

"A new Python effect, Flatten Path, flattens paths in the current selection, approximating each path with a polyline whose segments meet the specified criteria for flatness. "
<http://wiki.inkscape.org/wiki/index.php/Release_notes/0.44#Extension_effects>

"This effect converts selected Bezier curves to an approximation composed of straight-line paths. The number of line segments used is determined by the Flatness parameter. The smaller the Flatness, the more line segments are used. "
<http://tavmjong.free.fr/INKSCAPE/MANUAL/html/Effects-ModifyPath.html#Effects-FlattenBezier>

I often resort to a search in the archives of the inkscape-devel mailing list which can reveal a lot about the inner workings of Inkscape's features or at least have given me many links and clues to pursue:
<http://news.gmane.org/gmane.comp.graphics.inkscape.devel>

… the 'hpgl_output' extension is part of the 0.46+devel version of Inkscape and will be included in 0.47.
<http://inkscape.svn.sourceforge.net/viewvc/inkscape/inkscape/trunk/share/extensions/hpgl_output.inx?view=markup>
<http://inkscape.svn.sourceforge.net/viewvc/inkscape/inkscape/trunk/share/extensions/hpgl_output.py?view=markup>

Revision history for this message
Papy Octet (guy-wallon) said :
#6

Once more : thank you very very much for youyr help.
A+

Revision history for this message
su_v (suv-lp) said :
#7

Maybe another interesting resource for you (though the extensions were not included into the Inkscape distribution): pLAySVG
<http://article.gmane.org/gmane.comp.graphics.inkscape.devel/20332>
<http://article.gmane.org/gmane.comp.graphics.inkscape.devel/20839>

"pLAySVG is a python library for the development and manipulation of SVG vector graphics through scripting. pLAySVG makes it easy to create graphics based on geometric relationships. It's intended for educational and artistic use."
<http://sourceforge.net/projects/play-svg/>

Revision history for this message
Papy Octet (guy-wallon) said :
#8

Thank you.

I shall take a look to these files. I shall learne how using them.
It's very kind of you sending me these informations.

A+