FreeCAD vector math library: Difference between revisions

From FreeCAD Documentation
No edit summary
(Module to Workbench renaming.)
 
(16 intermediate revisions by 7 users not shown)
Line 1: Line 1:
<languages/>
This is a python file containing a couple of useful functions to manipulate freecad vectors. Just paste the following code in a python file, and import that file in your python script in order to use them. This library is included in the [[Draft Module]] and can be accessed like this from the python interpreter:
<translate>


== Introduction == <!--T:4-->
from draftlibs import fcvec


<!--T:1-->
This is a [[Python|Python]] module containing a couple of useful functions to manipulate vectors. This library is included in the [[Draft_Workbench]] and can be accessed like this from the Python interpreter:
</translate>
{{Code|code=
import DraftVecUtils
}}
<translate>

<!--T:5-->
Please note that this module was created a long time ago, when the {{incode|Vector}} class didn't have many of its methods. Now these operations can be done by the Vector class itself.

<!--T:6-->
Although the {{incode|DraftVecUtils}} module still exists, and it is still used inside the [[Draft_Workbench|Draft Workbench]], it is probably better to use the {{incode|Vector}} methods directly for new developments.

== Functions == <!--T:7-->

<!--T:2-->
Vectors are the building bricks of almost all 3D geometric operations, so it is useful to know a bit about them to understand how these functions can be useful to you. A couple of good pages to learn the basics of vector math:
Vectors are the building bricks of almost all 3D geometric operations, so it is useful to know a bit about them to understand how these functions can be useful to you. A couple of good pages to learn the basics of vector math:
* http://en.wikipedia.org/wiki/Vector_space
* http://quark.planetquake.gamespy.com/infobase/src.math.vectors.html
* http://www.brainycreatures.org/maths/vector.asp
* http://maths-wiki.wikispaces.com/Vectors
* http://www.geocities.com/SiliconValley/2151/math3d.html
* http://darksleep.com/player/opengl_coordinate_system_and_matrix_math.html


</translate>
Vector math library for FreeCAD"
{{Code|code=
"""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)))
<translate>

def project(first, other):
</translate>
"project(Vector,Vector): projects the first vector onto the second one"
{{Powerdocnavi{{#translation:}}}}
if isinstance(first,FreeCAD.Vector) and isinstance(other,FreeCAD.Vector):
[[Category:Developer Documentation{{#translation:}}]]
return scale(other, dotproduct(first,other)/dotproduct(other,other))
[[Category:Python Code{{#translation:}}]]
{{clear}}

Latest revision as of 20:24, 24 August 2021

Introduction

This is a Python module containing a couple of useful functions to manipulate vectors. This library is included in the Draft_Workbench and can be accessed like this from the Python interpreter:

import DraftVecUtils

Please note that this module was created a long time ago, when the Vector class didn't have many of its methods. Now these operations can be done by the Vector class itself.

Although the DraftVecUtils module still exists, and it is still used inside the Draft Workbench, it is probably better to use the Vector methods directly for new developments.

Functions

Vectors are the building bricks of almost all 3D geometric operations, so it is useful to know a bit about them to understand how these functions can be useful to you. A couple of good pages to learn the basics of vector math:

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