File: vec.rst

package info (click to toggle)
openstructure 2.9.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 205,228 kB
  • sloc: cpp: 188,129; python: 35,361; ansic: 34,298; fortran: 3,275; sh: 286; xml: 146; makefile: 29
file content (189 lines) | stat: -rw-r--r-- 4,935 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
Vectors
================================================================================
.. currentmodule:: ost.geom

The :class:`Vec2`, :class:`Vec3`, :class:`Vec4` classes implement vectors in 2, 
3 and four dimensions. They support basic arithmetic via overloaded operators. 
Essentially, the following basic operations are available:

 * adding and subtracting two vectors
 * negation
 * multiplying and dividing by scalar value

This is shown in the following example:

.. code-block:: python
   
   vec_a=geom.Vec2(1, 0)
   vec_b=geom.Vec2(0, 1)
   print(vec_a, vec_b)
   print(vec_a+vec_b)
   print(vec_a*3-vec_b)

The standard vector operations are implemented as :ref:`free standing functions 
<vector-functions>`:


.. code-block:: python
   
  vec_a=geom.Vec3(1, 0, 0)
  vec_b=geom.Vec3(0, 1, 0)
  
  print(geom.Dot(vec_a, vec_b))
  print(geom.Cross(vec_a, vec_b))
  
  print(geom.Normalize(geom.Vec3(1, 1, 0)))
  
  print(geom.Length(geom.Vec3(1, 1, 1)))


Vector Classes
--------------------------------------------------------------------------------
.. class:: Vec2(x=0.0, y=0.0, z=0.0)
           Vec2(vec)
           
   Real-valued vector in 2 dimensions.
   
   :param   x: x coordinate
   :type    x: float or int   
   :param   y: y coordinate
   :type    y: float or int
   :param vec: the coordinates are set to the coordinates of vec. If vec is a
               :class:`Vec2`, the coordinates are copied directly, If vec is a
               :class:`Vec3`, the x and y coordinates are set to the
               coordinates of vec and z is silently swallowed. If vec is of
               type :class:`Vec4`, x and y are divided by the homogenous
               coordinate w, raising a DivideByZeroException when w is zero.
   :type  vec: Vec2, Vec3 or Vec4
   
   .. attribute:: x

    The x-coordinate of the vector.

    :type: float

   .. attribute:: y

     The y-coordinate of the vector.
.. class:: Vec3(x=0.0, y=0.0, z=0.0)
           Vec3(vec)
    
    Real-valued vector in 3 dimensions.
        
    :param   x: x coordinate
    :param   y: y coordinate
    :param   z: z coordinate
    :param vec: the coordinates are set to the coordinates of vec. If vec is a
                :class:`Vec3`, the coordinates are copied directly, If vec is a
                :class:`Vec2`, the x and y coordinates are set to the
                coordinates of vec and z is initialized to zero. If vec is of
                type :class:`Vec4`, x, y and z are divided by homogenous
                coordinate w, raising a DivideByZeroException when w is zero.
    :type  vec: Vec2, Vec3 or Vec4
    
    .. attribute:: x
    
     The x-coordinate of the vector.
    
     :type: float or int
     
    .. attribute:: y
      
      The y-coordinate of the vector.
    
      :type: float or int
      
    .. attribute:: z
    
      The z-coordinate of the vector.
      
      :type: float or int

.. class:: Vec4(x=0.0, y=0.0, z=0.0, w=1.0)
           Vec4(vec)

    Real-valued vector in 4 dimensions.

    :param   x: x coordinate
    :type    x: float or int
    :param   y: y coordinate
    :type    y: float or int
    :param   z: z coordinate
    :type    z: float or int       
    :param   w: w (homogenous) coordinate
    :type    w: float or int       
    :param vec: the coordinates are set to the coordinates of vec. If vec is a
                :class:`Vec4`, the coordinates are copied directly, If vec is a
                :class:`Vec2`, the x and y coordinates are set to the
                coordinates of vec and z and w are initialized to 0 and 1,
                respectively. If vec is of type :class:`Vec4`, x, y and z are
                divided by homogenous coordinate w, raising a
                DivideByZeroException when w is zero.

    .. attribute:: x

     The x-coordinate of the vector.

     :type: float or int

    .. attribute:: y

      The y-coordinate of the vector.

      :type: float or int

    .. attribute:: z

      The z-coordinate of the vector.

      :type: float or int
  
    .. attribute:: w

      The homogenous coordinate.

      :type: float or int


.. _vector-functions:

Functions Operating on Vectors     
--------------------------------------------------------------------------------

.. function:: Cross(vec_a, vec_b)

   Cross product of `vec_a` and `vec_b`

   :type vec_a: Vec3
   :type vec_b: Vec3

.. function:: Dot(vec_a, vec_b)

   Dot (scalar) product of `vec_a` and `vec_b`

   :param vec_a: first vector
   :type  vec_a: Vec3
   :param vec_b: second vector
   :type  vec_b: Vec3

.. function:: Length(vec)

   Length of vector

   :param vec:
   :type  vec: Vec2, Vec3 or Vec4

.. function:: Length2(vec)

  Returns the squared length of `vec`

  :param vec:
  :type  vec: Vec2, Vec3 or Vec4

.. function:: Normalize(vec)

   Returns a normalized version of `vec`

   :param vec:  Vector to be normalized
   :type  vec: Vec2, Vec3 or Vec4