本文结合5 API接口阅读。

向量基本知识

向量大小和归一化(vector magnitude & normalization)、向量范数(vector norm)、标量/向量/矩阵/张量
https://blog.csdn.net/liveshow021_jxb/article/details/113978699
向量归一化即将向量的方向保持不变,大小归一化到1

数学基础——向量
向量没有位置,只有大小和方向
任何一个点都可以看作是从原点出发的向量

向量的基本运算(加减、数乘)——1.2 坐标系视角下的加减(常用)
https://zhuanlan.zhihu.com/p/615940607
向量叉乘
https://zhuanlan.zhihu.com/p/148780358
向量的点乘运算(内积)
https://zhuanlan.zhihu.com/p/616207329

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

矢量(向量)Vector和placement

https://wiki.freecad.org/Python_scripting_tutorial
https://wiki.freecad.org/Manual:A_gentle_introduction#Vectors_and_Placements

矢量(向量)和展示位置
矢量(向量)是任何 3D 应用中的基本概念。它是 3 个数字(x、y 和 z)的列表,描述 3D 空间中的点或位置。很多事情都可以用向量来完成,例如加法、减法、投影等等。在FreeCAD中,向量是这样的:

myvec = FreeCAD.Vector(2,0,0)
print(myvec)
print(myvec.x)
print(myvec.y)
othervec = FreeCAD.Vector(0,3,0)
sumvec = myvec.add(othervec)

FreeCAD对象的另一个共同特征是它们的放置。正如我们在前面的章节中看到的,每个对象都有一个 Placement 属性,其中包含对象的位置(基础)和方向(旋转)。这些属性很容易从 Python 中操作,例如移动我们的对象:

print(box.Placement)
print(box.Placement.Base)
box.Placement.Base = sumvec
otherpla = FreeCAD.Placement()
otherpla.Base = FreeCAD.Vector(5,5,0)
box.Placement = otherpla
作者:秦晓川  创建时间:2023-05-21 14:53
最后编辑:秦晓川  更新时间:2024-11-14 14:45