113
119
if isinstance(first,Vector) and isinstance(other,Vector):
114
120
dp=dotproduct(normalized(first),normalized(other))
115
121
if (dp >= -1) and (dp <= 1):
116
angle = math.acos(dotproduct(normalized(first),normalized(other)))
117
#angle = math.atan2(normalized(crossproduct(first,other)),dotproduct(first,other));
118
# next line works only for 2D at the moment
119
if ((first.x * other.y - first.y * other.x) < 0): angle = -angle
122
#angle = math.atan2(normalized(crossproduct(first,other)),dotproduct(first,other))
123
ang = math.acos(dotproduct(normalized(first),normalized(other)))
124
# next line works only for 2D at the moment. Only used by DXF importer...
125
if ((first.x * other.y - first.y * other.x) < 0): ang = -ang
124
130
def project(first, other):
125
131
"project(Vector,Vector): projects the first vector onto the second one"
126
132
if isinstance(first,Vector) and isinstance(other,Vector):
127
return scale(other, dotproduct(first,other)/dotproduct(other,other))
133
dp = dotproduct(other,other)
135
return scale(other, dotproduct(first,other)/dotproduct(other,other))
129
def rotate(first,angle):
130
"rotate(Vector,Float): rotates the first Vector around the origin Z axis, at the given angle."
131
if isinstance(first,Vector):
132
return Vector(math.cos(angle)*first.x-math.sin(angle)*first.y,math.sin(angle)*first.x+math.cos(angle)*first.y,first.z)
138
def rotate(first,rotangle,axis=Vector(0,0,1)):
139
'''rotate(Vector,Float,axis=Vector): rotates the first Vector
140
around the given axis, at the given angle.
141
If axis is omitted, the rotation is made on the xy plane.'''
142
if rotangle == 0: return first
143
if isinstance(first,Vector) and isinstance(axis,Vector):
144
axisxy = Vector(axis.x,axis.y,0)
145
mat1 = FreeCAD.Matrix()
146
if not isNull(axisxy):
147
mat1.rotateZ(angle(axisxy))
148
axisrot1 = mat1.multiply(axis)
149
axisxz = Vector(axisrot1.x,axisrot1.z,0)
150
mat2 = FreeCAD.Matrix()
151
if not isNull(axisxz):
152
mat2.rotateY(angle(axisxz)-math.pi/2)
153
axisrot2 = mat2.multiply(axisrot1)
154
vec = mat1.multiply(first)
155
vec = mat2.multiply(vec)
156
vec = Vector(math.cos(rotangle)*vec.x-math.sin(rotangle)*vec.y,math.sin(rotangle)*vec.x+math.cos(rotangle)*vec.y,vec.z)
157
vec = mat2.inverse().multiply(vec)
158
vec = mat1.inverse().multiply(vec)
134
161
def isNull(vector):
135
162
'''isNull(vector): Tests if a vector is nul vector'''
147
174
if equals(vector,v):
178
def rounded(vector,pr=PREC):
179
'''rounded(vector,int) - returns a rounded vector to given precision, or is
180
precision is not specified, built-in precision is used.'''
181
if isinstance(vector,Vector) and isinstance(pr,int):
182
return Vector(round(vector.x,pr),round(vector.y,pr),round(vector.z,pr))