~ubuntu-branches/ubuntu/maverick/freecad/maverick

« back to all changes in this revision

Viewing changes to src/Mod/Draft/draftlibs/fcvec.py

  • Committer: Bazaar Package Importer
  • Author(s): Adam C. Powell, IV
  • Date: 2010-01-11 08:48:33 UTC
  • mfrom: (3.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20100111084833-4g9vgdqbkw8u34zb
Tags: 0.9.2646.5-1
* New upstream version (closes: #561696).
* Added swig to Build-Depends (closes: #563523, #563772).
* Removed python-opencv from Build-Depends and Recommends (closes: #560768).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
 
2
2
#***************************************************************************
3
3
#*                                                                         *
 
4
#*   Copyright (c) 2009 Yorik van Havre <yorik@gmx.fr>                     *  
 
5
#*                                                                         *
4
6
#*   This program is free software; you can redistribute it and/or modify  *
5
7
#*   it under the terms of the GNU General Public License (GPL)            *
6
8
#*   as published by the Free Software Foundation; either version 2 of     *
19
21
#*                                                                         *
20
22
#***************************************************************************
21
23
 
 
24
__title__="FreeCAD Draft Workbench - Vector library"
 
25
__author__ = "Yorik van Havre <yorik@gmx.fr>, Werner Mayer, Martin Burbaum"
 
26
__url__ = ["http://yorik.orgfree.com","http://free-cad.sourceforge.net"]
 
27
 
22
28
"a vector math library for FreeCAD"
23
29
 
24
30
import math,FreeCAD
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
120
 
                        return 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                    
 
126
                        return ang
121
127
                else:
122
128
                        return 0
123
129
 
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)
 
134
                if dp:
 
135
                        return scale(other, dotproduct(first,other)/dotproduct(other,other))
 
136
        return None
128
137
 
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)
 
159
                return vec
133
160
 
134
161
def isNull(vector):
135
162
        '''isNull(vector): Tests if a vector is nul vector'''
147
174
                        if equals(vector,v):
148
175
                                return i
149
176
        return None
 
177
 
 
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))