FreeCAD vector math library/it: Difference between revisions

From FreeCAD Documentation
No edit summary
(Created page with "== Funzioni ==")
 
(15 intermediate revisions by 4 users not shown)
Line 1: Line 1:
<languages/>
=Libreria di matematica vettoriale di FreeCAD=


<span id="Introduction"></span>
Questo è un file python che contiene un paio di funzioni utili per manipolare i vettori di FreeCAD. Per usarle, basta incollare il seguente codice in un file di python, e importare il file nel vostro script python. Questa libreria è inclusa nel [[Draft Module/it|Modulo Draft]] e vi si può accedere tramite l'interprete python in questo modo:
==Introduzione==


Questo è un modulo [[Python/it|Python]] contenente un paio di funzioni utili per manipolare i vettori. Questa libreria è inclusa in [[Draft_Workbench/it|Draft]] ed è possibile accedervi in questo modo dall'interprete Python:
from draftlibs import fcvec
{{Code|code=
import DraftVecUtils
}}

Tenere presente che questo modulo è stato creato molto tempo fa, quando la classe {{incode|Vector}} non aveva molti dei suoi metodi. Ora queste operazioni possono essere eseguite dalla stessa classe Vector.

Sebbene il modulo {{incode|DraftVecUtils}} esista ancora e sia ancora utilizzato all'interno di [[Draft_Workbench|Draft]], è probabilmente meglio utilizzare i metodi {{incode|Vector}} direttamente per i nuovi sviluppi.

<span id="Functions"></span>
== Funzioni ==


I vettori sono i mattoni costitutivi di quasi tutte le operazioni geometriche 3D, quindi è utile conoscerli un po' per capire come queste funzioni possono essere utili. Ecco alcune valide pagine per imparare le basi della matematica vettoriale:
I vettori sono i mattoni costitutivi di quasi tutte le operazioni geometriche 3D, quindi è utile conoscerli un po' per capire come queste funzioni possono essere utili. Ecco alcune valide pagine per imparare le basi della matematica vettoriale:
Line 10: Line 21:
* http://darksleep.com/player/opengl_coordinate_system_and_matrix_math.html
* http://darksleep.com/player/opengl_coordinate_system_and_matrix_math.html


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

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

[[Category:Poweruser_Documentation/it]]


{{Powerdocnavi{{#translation:}}}}
{{languages/it | {{en|FreeCAD_vector_math_library}} {{es|FreeCAD_vector_math_library/es}} {{fr|FreeCAD_vector_math_library/fr}} }}
[[Category:Developer Documentation{{#translation:}}]]
[[Category:Python Code{{#translation:}}]]
{{clear}}

Latest revision as of 20:57, 5 May 2023

Introduzione

Questo è un modulo Python contenente un paio di funzioni utili per manipolare i vettori. Questa libreria è inclusa in Draft ed è possibile accedervi in questo modo dall'interprete Python:

import DraftVecUtils

Tenere presente che questo modulo è stato creato molto tempo fa, quando la classe Vector non aveva molti dei suoi metodi. Ora queste operazioni possono essere eseguite dalla stessa classe Vector.

Sebbene il modulo DraftVecUtils esista ancora e sia ancora utilizzato all'interno di Draft, è probabilmente meglio utilizzare i metodi Vector direttamente per i nuovi sviluppi.

Funzioni

I vettori sono i mattoni costitutivi di quasi tutte le operazioni geometriche 3D, quindi è utile conoscerli un po' per capire come queste funzioni possono essere utili. Ecco alcune valide pagine per imparare le basi della matematica vettoriale:

"""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))