FreeCAD vector math library/fr: Difference between revisions

From FreeCAD Documentation
(Updating to match new version of source page)
No edit summary
 
(16 intermediate revisions by 3 users not shown)
Line 1: Line 1:
<languages/>
Ceci est un fichier Python contenant une série de fonctions utiles pour manipuler des vecteurs dans FreeCAD. Il suffit de coller le code suivant dans un fichier Python, et d'importer ce fichier dans votre script Python dans le but de l'utiliser. Cette bibliothèque est incluse dans [[Draft Module/fr|Draft Module]] et peut être consulté à partir de l'interpréteur python :
from draftlibs import DraftVecUtils
Les vecteurs sont les fondations de pratiquement toutes les formes géométriques 3D, il est donc utile de connaître un peu plus et comprendre comment ces fonctions peuvent vous être utiles. Une série de bonnes adresses pour apprendre les bases des vecteur mathématiques :


== Introduction ==
* http://en.wikipedia.org/wiki/Vector_space

* http://fr.wikipedia.org/wiki/Espace_vectoriel
Il s'agit d'un module [[Python/fr|Python]] contenant quelques fonctions utiles pour manipuler les vecteurs. Cette bibliothèque est incluse dans l'[[Draft_Workbench/fr|atelier Draft]] et est accessible comme ceci depuis l'interpréteur Python :
{{Code|code=
import DraftVecUtils
}}

Veuillez noter que ce module a été créé il y a longtemps lorsque la classe {{incode|Vector}} n'avait pas beaucoup de ses méthodes. Désormais, ces opérations peuvent être effectuées par la classe Vector elle-même.

Bien que le module {{incode|DraftVecUtils}} existe toujours, et qu'il soit toujours utilisé dans l'[[Draft_Workbench/fr|atelier Draft]], il est probablement préférable d'utiliser les méthodes {{incode|Vector}} directement pour les nouveaux développements.

==Les Fonctions==

Les vecteurs sont les fondations de pratiquement toutes les formes géométriques 3D, il est donc utile de connaître un peu plus et comprendre comment ces fonctions peuvent vous être utiles. Une série de bonnes adresses pour apprendre les bases des vecteur mathématiques :
* https://fr.wikipedia.org/wiki/Espace_vectoriel
* https://en.wikipedia.org/wiki/Vector_space
* http://maths-wiki.wikispaces.com/Vectors
* http://maths-wiki.wikispaces.com/Vectors
* http://darksleep.com/player/opengl_coordinate_system_and_matrix_math.html
* http://darksleep.com/player/opengl_coordinate_system_and_matrix_math.html
<pre>
"Vector math library for FreeCAD"


{{Code|code=
import math,FreeCAD
"""Vector math library for FreeCAD"""

import math
import FreeCAD
def add(first, other):
def add(first, other):
"add(Vector,Vector) - adds two vectors"
"""add(Vector,Vector) - adds two vectors"""
if isinstance(first,FreeCAD.Vector) and isinstance(other,FreeCAD.Vector):
if isinstance(first,FreeCAD.Vector) and isinstance(other,FreeCAD.Vector):
return FreeCAD.Vector(first.x+other.x, first.y+other.y, first.z+other.z)
return FreeCAD.Vector(first.x+other.x, first.y+other.y, first.z+other.z)
def sub(first, other):
def sub(first, other):
"sub(Vector,Vector) - subtracts second vector from first one"
"""sub(Vector,Vector) - subtracts second vector from first one"""
if isinstance(first,FreeCAD.Vector) and isinstance(other,FreeCAD.Vector):
if isinstance(first,FreeCAD.Vector) and isinstance(other,FreeCAD.Vector):
return FreeCAD.Vector(first.x-other.x, first.y-other.y, first.z-other.z)
return FreeCAD.Vector(first.x-other.x, first.y-other.y, first.z-other.z)
def scale(first,scalar):
def scale(first,scalar):
"scale(Vector,Float) - scales (multiplies) a vector by a factor"
"""scale(Vector,Float) - scales (multiplies) a vector by a factor"""
if isinstance(first,FreeCAD.Vector):
if isinstance(first,FreeCAD.Vector):
return FreeCAD.Vector(first.x*scalar, first.y*scalar, first.z*scalar)
return FreeCAD.Vector(first.x*scalar, first.y*scalar, first.z*scalar)
def length(first):
def length(first):
"lengh(Vector) - gives vector length"
"""lengh(Vector) - gives vector length"""
if isinstance(first,FreeCAD.Vector):
if isinstance(first,FreeCAD.Vector):
return math.sqrt(first.x*first.x + first.y*first.y + first.z*first.z)
return math.sqrt(first.x*first.x + first.y*first.y + first.z*first.z)
def dist(first, other):
def dist(first, other):
"dist(Vector,Vector) - returns the distance between both points/vectors"
"""dist(Vector,Vector) - returns the distance between both points/vectors"""
if isinstance(first,FreeCAD.Vector) and isinstance(other,FreeCAD.Vector):
if isinstance(first,FreeCAD.Vector) and isinstance(other,FreeCAD.Vector):
return length(sub(first,other))
return length(sub(first,other))
def normalized(first):
def normalized(first):
"normalized(Vector) - returns a unit vector"
"""normalized(Vector) - returns a unit vector"""
if isinstance(first,FreeCAD.Vector):
if isinstance(first,FreeCAD.Vector):
l=length(first)
l=length(first)
return FreeCAD.Vector(first.x/l, first.y/l, first.z/l)
return FreeCAD.Vector(first.x/l, first.y/l, first.z/l)
def dotproduct(first, other):
def dotproduct(first, other):
"dotproduct(Vector,Vector) - returns the dot product of both vectors"
"""dotproduct(Vector,Vector) - returns the dot product of both vectors"""
if isinstance(first,FreeCAD.Vector) and isinstance(other,FreeCAD.Vector):
if isinstance(first,FreeCAD.Vector) and isinstance(other,FreeCAD.Vector):
return (first.x*other.x + first.y*other.y + first.z*other.z)
return (first.x*other.x + first.y*other.y + first.z*other.z)
def crossproduct(first, other=FreeCAD.Vector(0,0,1)):
def crossproduct(first, other=FreeCAD.Vector(0,0,1)):
"crossproduct(Vector,Vector) - returns the cross product of both vectors.
"""crossproduct(Vector,Vector) - returns the cross product of both vectors.
If only one is specified, cross product is made with vertical axis, thus returning its perpendicular in XY plane"
If only one is specified, cross product is made with vertical axis, thus returning its perpendicular in XY plane"""
if isinstance(first,FreeCAD.Vector) and isinstance(other,FreeCAD.Vector):
if isinstance(first,FreeCAD.Vector) and isinstance(other,FreeCAD.Vector):
return FreeCAD.Vector(first.y*other.z - first.z*other.y, first.z*other.x - first.x*other.z, first.x*other.y - first.y*other.x)
return FreeCAD.Vector(first.y*other.z - first.z*other.y, first.z*other.x - first.x*other.z, first.x*other.y - first.y*other.x)
def angle(first, other=FreeCAD.Vector(1,0,0)):
def angle(first, other=FreeCAD.Vector(1,0,0)):
"angle(Vector,Vector) - returns the angle in radians between the two vectors.
"""angle(Vector,Vector) - returns the angle in radians between the two vectors.
If only one is given, angle is between the vector and the horizontal East direction"
If only one is given, angle is between the vector and the horizontal East direction"""
if isinstance(first,FreeCAD.Vector) and isinstance(other,FreeCAD.Vector):
if isinstance(first,FreeCAD.Vector) and isinstance(other,FreeCAD.Vector):
return math.acos(dotproduct(normalized(first),normalized(other)))
return math.acos(dotproduct(normalized(first),normalized(other)))
def project(first, other):
def project(first, other):
"project(Vector,Vector): projects the first vector onto the second one"
"""project(Vector,Vector): projects the first vector onto the second one"""
if isinstance(first,FreeCAD.Vector) and isinstance(other,FreeCAD.Vector):
if isinstance(first,FreeCAD.Vector) and isinstance(other,FreeCAD.Vector):
return scale(other, dotproduct(first,other)/dotproduct(other,other))
return scale(other, dotproduct(first,other)/dotproduct(other,other))
}}
</pre>
[[Category:Poweruser Documentation/fr]]


{{Powerdocnavi{{#translation:}}}}
[[Category:Developer Documentation{{#translation:}}]]
[[Category:Python Code{{#translation:}}]]
{{clear}}
{{clear}}
<languages/>

Latest revision as of 09:52, 28 August 2021

Introduction

Il s'agit d'un module Python contenant quelques fonctions utiles pour manipuler les vecteurs. Cette bibliothèque est incluse dans l'atelier Draft et est accessible comme ceci depuis l'interpréteur Python :

import DraftVecUtils

Veuillez noter que ce module a été créé il y a longtemps lorsque la classe Vector n'avait pas beaucoup de ses méthodes. Désormais, ces opérations peuvent être effectuées par la classe Vector elle-même.

Bien que le module DraftVecUtils existe toujours, et qu'il soit toujours utilisé dans l'atelier Draft, il est probablement préférable d'utiliser les méthodes Vector directement pour les nouveaux développements.

Les Fonctions

Les vecteurs sont les fondations de pratiquement toutes les formes géométriques 3D, il est donc utile de connaître un peu plus et comprendre comment ces fonctions peuvent vous être utiles. Une série de bonnes adresses pour apprendre les bases des vecteur mathématiques :

"""Vector math library for FreeCAD"""

import math
import FreeCAD
 
def add(first, other):
    """add(Vector,Vector) - adds two vectors"""
    if isinstance(first,FreeCAD.Vector) and isinstance(other,FreeCAD.Vector):
        return FreeCAD.Vector(first.x+other.x, first.y+other.y, first.z+other.z)
 
def sub(first, other): 
    """sub(Vector,Vector) - subtracts second vector from first one"""
    if isinstance(first,FreeCAD.Vector) and isinstance(other,FreeCAD.Vector):
        return FreeCAD.Vector(first.x-other.x, first.y-other.y, first.z-other.z)
 
def scale(first,scalar):
    """scale(Vector,Float) - scales (multiplies) a vector by a factor"""
    if isinstance(first,FreeCAD.Vector):
        return FreeCAD.Vector(first.x*scalar, first.y*scalar, first.z*scalar)
 
def length(first):
    """lengh(Vector) - gives vector length"""
    if isinstance(first,FreeCAD.Vector):
        return math.sqrt(first.x*first.x + first.y*first.y + first.z*first.z)
 
def dist(first, other):
    """dist(Vector,Vector) - returns the distance between both points/vectors"""
    if isinstance(first,FreeCAD.Vector) and isinstance(other,FreeCAD.Vector):
        return length(sub(first,other))
 
def normalized(first):
    """normalized(Vector) - returns a unit vector"""
    if isinstance(first,FreeCAD.Vector):
        l=length(first)
        return FreeCAD.Vector(first.x/l, first.y/l, first.z/l)
 
def dotproduct(first, other):
    """dotproduct(Vector,Vector) - returns the dot product of both vectors"""
    if isinstance(first,FreeCAD.Vector) and isinstance(other,FreeCAD.Vector):
        return (first.x*other.x + first.y*other.y + first.z*other.z)
 
def crossproduct(first, other=FreeCAD.Vector(0,0,1)):
    """crossproduct(Vector,Vector) - returns the cross product of both vectors. 
    If only one is specified, cross product is made with vertical axis, thus returning its perpendicular in XY plane"""
    if isinstance(first,FreeCAD.Vector) and isinstance(other,FreeCAD.Vector):
        return FreeCAD.Vector(first.y*other.z - first.z*other.y, first.z*other.x - first.x*other.z, first.x*other.y - first.y*other.x)
 
def angle(first, other=FreeCAD.Vector(1,0,0)):
    """angle(Vector,Vector) - returns the angle in radians between the two vectors. 
    If only one is given, angle is between the vector and the horizontal East direction"""
    if isinstance(first,FreeCAD.Vector) and isinstance(other,FreeCAD.Vector):
        return math.acos(dotproduct(normalized(first),normalized(other)))
 
def project(first, other):
    """project(Vector,Vector): projects the first vector onto the second one"""
    if isinstance(first,FreeCAD.Vector) and isinstance(other,FreeCAD.Vector):
        return scale(other, dotproduct(first,other)/dotproduct(other,other))