This file is indexed.

/usr/share/pyshared/Scientific/Geometry/Transformation.py is in python-scientific 2.8-4.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
# This module defines classes that represent coordinate translations,
# rotations, and combinations of translation and rotation.
#
# Written by: Konrad Hinsen <hinsen@cnrs-orleans.fr>
# Contributions from Pierre Legrand <pierre.legrand@synchrotron-soleil.fr>
# last revision: 2008-8-22
# 

"""
Linear transformations in 3D space
"""

from Scientific import Geometry
from Scientific import N; Numeric = N
from math import atan2

#
# Abstract base classes
#
class Transformation:

    """
    Linear coordinate transformation.

    Transformation objects represent linear coordinate transformations
    in a 3D space. They can be applied to vectors, returning another vector.
    If t is a transformation and v is a vector, t(v) returns
    the transformed vector.

    Transformations support composition: if t1 and t2 are transformation
    objects, t1*t2 is another transformation object which corresponds
    to applying t1 B{after} t2.

    This class is an abstract base class. Instances can only be created
    of concrete subclasses, i.e. translations or rotations.
    """

    def __call__(self, vector):
        """
        @param vector: the input vector
        @type vector: L{Scientific.Geometry.Vector}
        @returns: the transformed vector
        @rtype: L{Scientific.Geometry.Vector}
        """
        return NotImplementedError

    def inverse(self):
        """
        @returns: the inverse transformation
        @rtype: L{Transformation}
        """
        return NotImplementedError

#
# Rigid body transformations
#
class RigidBodyTransformation(Transformation):

    """
    Combination of translations and rotations
    """

    def rotation(self):
        """
        @returns: the rotational component
        @rtype: L{Rotation}
        """
        pass

    def translation(self):
        """
        @returns: the translational component. In the case of a mixed
                  rotation/translation, this translation is executed
                  B{after} the rotation.
        @rtype: L{Translation}
        """
        pass

    def screwMotion(self):
        """
        @returns: the four parameters
                  (reference, direction, angle, distance)
                  of a screw-like motion that is equivalent to the
                  transformation. The screw motion consists of a displacement
                  of distance (a C{float}) along direction (a normalized
                  L{Scientific.Geometry.Vector}) plus a rotation of
                  angle radians around an axis pointing along
                  direction and passing through the point reference
                  (a L{Scientific.Geometry.Vector}).
        """
        pass

#
# Pure translation
#
class Translation(RigidBodyTransformation):

    """
    Translational transformation
    """

    def __init__(self, vector):
        """
        @param vector: the displacement vector
        @type vector: L{Scientific.Geometry.Vector}
        """
        self.vector = vector

    is_translation = 1

    def asLinearTransformation(self):
        return LinearTransformation(Geometry.delta, self.vector)

    def __mul__(self, other):
        if hasattr(other, 'is_translation'):
            return Translation(self.vector + other.vector)
        elif hasattr(other, 'is_rotation'):
            return RotationTranslation(other.tensor, self.vector)
        elif hasattr(other, 'is_rotation_translation'):
            return RotationTranslation(other.tensor, other.vector+self.vector)
        else:
            return self.asLinearTransformation()*other.asLinearTransformation()

    def __call__(self, vector):
        return self.vector + vector

    def displacement(self):
        """
        @returns: the displacement vector
        """
        return self.vector

    def rotation(self):
        return Rotation(Geometry.ez, 0.)

    def translation(self):
        return self

    def inverse(self):
        return Translation(-self.vector)

    def screwMotion(self):
        l = self.vector.length()
        if l == 0.:
            return Geometry.Vector(0.,0.,0.), \
                   Geometry.Vector(0.,0.,1.), 0., 0.
        else:
            return Geometry.Vector(0.,0.,0.), self.vector/l, 0., l

#
# Pure rotation
#
class Rotation(RigidBodyTransformation):

    """
    Rotational transformation
    """

    def __init__(self, *args):
        """
        There are two calling patterns: 

         - Rotation(tensor), where tensor is a L{Scientific.Geometry.Tensor}
           of rank 2 containing the rotation matrix.

         - Rotation(axis, angle), where axis is a L{Scientific.Geometry.Vector}
           and angle a number (the angle in radians).       
        """
        if len(args) == 1:
            self.tensor = args[0]
            if not Geometry.isTensor(self.tensor):
                self.tensor = Geometry.Tensor(self.tensor)
        elif len(args) == 2:
            axis, angle = args
            axis = axis.normal()
            projector = axis.dyadicProduct(axis)
            self.tensor = projector - \
                          N.sin(angle)*Geometry.epsilon*axis + \
                          N.cos(angle)*(Geometry.delta-projector)
        else:
            raise TypeError('one or two arguments required')

    is_rotation = 1

    def asLinearTransformation(self):
        return LinearTransformation(self.tensor, Geometry.nullVector)

    def __mul__(self, other):
        if hasattr(other, 'is_rotation'):
            return Rotation(self.tensor.dot(other.tensor))
        elif hasattr(other, 'is_translation'):
            return RotationTranslation(self.tensor, self.tensor*other.vector)
        elif hasattr(other, 'is_rotation_translation'):
            return RotationTranslation(self.tensor.dot(other.tensor),
                                       self.tensor*other.vector)
        else:
            return self.asLinearTransformation()*other.asLinearTransformation()

    def __call__(self,other):
        if hasattr(other,'is_vector'):
            return self.tensor*other
        elif hasattr(other, 'is_tensor') and other.rank == 2:
            _rinv=self.tensor.inverse()
            return _rinv.dot(other.dot(self.tensor))
        elif hasattr(other, 'is_tensor') and other.rank == 1:
            return self.tensor.dot(other)
        else:
            raise ValueError('incompatible object')

    def axisAndAngle(self):
        """
        @returns: the axis (a normalized vector) and angle (in radians).
                  The angle is in the interval (-pi, pi]
        @rtype: (L{Scientific.Geometry.Vector}, C{float})
        """
        asym = -self.tensor.asymmetricalPart()
        axis = Geometry.Vector(asym[1,2], asym[2,0], asym[0,1])
        sine = axis.length()
        if abs(sine) > 1.e-10:
            axis = axis/sine
            projector = axis.dyadicProduct(axis)
            cosine = (self.tensor-projector).trace()/(3.-axis*axis)
            angle = angleFromSineAndCosine(sine, cosine)
        else:
            t = 0.5*(self.tensor+Geometry.delta)
            i = N.argmax(t.diagonal().array)
            axis = (t[i]/N.sqrt(t[i,i])).asVector()
            angle = 0.
            if t.trace() < 2.:
                angle = N.pi
        return axis, angle

    def threeAngles(self, e1, e2, e3, tolerance=1e-7):
        """
        Find three angles a1, a2, a3 such that
        Rotation(a1*e1)*Rotation(a2*e2)*Rotation(a3*e3)
        is equal to the rotation object. e1, e2, and
        e3 are non-zero vectors. There are two solutions, both of which
        are computed.

        @param e1: a rotation axis
        @type e1: L{Scientific.Geometry.Vector}
        @param e2: a rotation axis
        @type e2: L{Scientific.Geometry.Vector}
        @param e3: a rotation axis
        @type e3: L{Scientific.Geometry.Vector}
        @returns: a list containing two arrays of shape (3,),
                  each containing the three angles of one solution
        @rtype: C{list} of C{N.array}
        @raise ValueError: if two consecutive axes are parallel
        """

        # Written by Pierre Legrand (pierre.legrand@synchrotron-soleil.fr)
        #
        # Basically this is a reimplementation of the David
        # Thomas's algorithm [1] described by Gerard Bricogne in [2]:
        #
        # [1] "Modern Equations of Diffractometry. Goniometry." D.J. Thomas
        # Acta Cryst. (1990) A46 Page 321-343.
        #
        # [2] "The ECC Cooperative Programming Workshop on Position-Sensitive
        # Detector Software." G. Bricogne,
        # Computational aspect of Protein Crystal Data Analysis,
        # Proceedings of the Daresbury Study Weekend (23-24/01/1987)
        # Page 122-126

        e1 = e1.normal()
        e2 = e2.normal()
        e3 = e3.normal()

        # We are searching for the three angles a1, a2, a3
        # If 2 consecutive axes are parallel: decomposition is not meaningful
        if (e1.cross(e2)).length() < tolerance or \
           (e2.cross(e3)).length() < tolerance :
            raise ValueError('Consecutive parallel axes. Too many solutions')
        w = self(e3)
        
        # Solve the equation : _a.cosx + _b.sinx = _c
        _a = e1*e3 - (e1*e2)*(e2*e3)
        _b = e1*(e2.cross(e3))
        _c = e1*w - (e1*e2)*(e2*e3)
        _norm = (_a**2 + _b**2)**0.5
        
        # Checking for possible errors in initial Rot matrix
        if _norm == 0:
            raise ValueError('FAILURE 1, norm = 0')
        if abs(_c/_norm) > 1+tolerance:
            raise ValueError('FAILURE 2' +
                              'malformed rotation Tensor (non orthogonal?) %.8f'
                              % (_c/_norm))
        #if _c/_norm > 1: raise ValueError('Step1: No solution')
        _th = angleFromSineAndCosine(_b/_norm, _a/_norm)
        _xmth = N.arccos(_c/_norm)

        # a2a and a2b are the two possible solutions to the equation.
        a2a = mod_angle((_th + _xmth), 2*N.pi)
        a2b = mod_angle((_th - _xmth), 2*N.pi)
        
        solutions = []
        # for each solution, find the two other angles (a1, a3).
        for a2 in (a2a, a2b):
            R2 = Rotation(e2, a2)
            v =  R2(e3)
            v1 = v - (v*e1)*e1
            w1 = w - (w*e1)*e1
            norm = ((v1*v1)*(w1*w1))**0.5
            if norm == 0: 
                # in that case rotation 1 and 3 are about the same axis
                # so any solution for rotation 1 is OK
                a1 = 0.
            else:
                cosa1 = (v1*w1)/norm
                sina1 = v1*(w1.cross(e1))/norm
                a1 = mod_angle(angleFromSineAndCosine(sina1, cosa1),
                               2*N.pi)
                
            R3 = Rotation(e2, -1*a2)*Rotation(e1, -1*a1)*self
            # u = normalized test vector perpendicular to e3
            # if e2 and e3 are // we have an exception before.
            # if we take u = e1^e3 then it will not work for
            # Euler and Kappa axes.
            u = (e2.cross(e3)).normal()
            cosa3 = u*R3(u)
            sina3 = u*(R3(u).cross(e3))
            a3 =  mod_angle(angleFromSineAndCosine(sina3, cosa3),
                            2*N.pi)
            
            solutions.append(N.array([a1, a2, a3]))
            
        # Gives the closest solution to 0,0,0 first
        if N.add.reduce(solutions[0]**2) > \
               N.add.reduce(solutions[1]**2):
            solutions = [solutions[1], solutions[0]]
        return solutions

    def asQuaternion(self):
        """
        @returns: a quaternion representing the same rotation
        @rtype: L{Scientific.Geometry.Quaternion.Quaternion}
        """
        from Quaternion import Quaternion
        axis, angle = self.axisAndAngle()
        sin_angle_2 = N.sin(0.5*angle)
        cos_angle_2 = N.cos(0.5*angle)
        return Quaternion(cos_angle_2, sin_angle_2*axis[0],
                          sin_angle_2*axis[1], sin_angle_2*axis[2])

    def rotation(self):
        return self

    def translation(self):
        return Translation(Geometry.Vector(0.,0.,0.))

    def inverse(self):
        return Rotation(self.tensor.transpose())

    def screwMotion(self):
        axis, angle = self.axisAndAngle()
        return Geometry.Vector(0., 0., 0.), axis, angle, 0.

#
# Combined translation and rotation
#
class RotationTranslation(RigidBodyTransformation):

    """
    Combined translational and rotational transformation.

    Objects of this class are not created directly, but can be the
    result of a composition of rotations and translations.
    """

    def __init__(self, tensor, vector):
        self.tensor = tensor
        self.vector = vector

    is_rotation_translation = 1

    def asLinearTransformation(self):
        return LinearTransformation(self.tensor, self.vector)

    def __mul__(self, other):
        if hasattr(other, 'is_rotation'):
            return RotationTranslation(self.tensor.dot(other.tensor),
                                       self.vector)
        elif hasattr(other, 'is_translation'):
            return RotationTranslation(self.tensor,
                                       self.tensor*other.vector+self.vector)
        elif hasattr(other, 'is_rotation_translation'):
            return RotationTranslation(self.tensor.dot(other.tensor),
                                       self.tensor*other.vector+self.vector)
        else:
            return self.asLinearTransformation()*other.asLinearTransformation()

    def __call__(self, vector):
        return self.tensor*vector + self.vector

    def rotation(self):
        return Rotation(self.tensor)

    def translation(self):
        return Translation(self.vector)

    def inverse(self):
        return Rotation(self.tensor.transpose())*Translation(-self.vector)

#    def screwMotion1(self):
#        import Scientific.LA
#        axis, angle = self.rotation().axisAndAngle()
#        d = self.vector*axis
#        x = d*axis-self.vector
#        r0 = N.dot(Scientific.LA.generalized_inverse(
#                            self.tensor.array-N.identity(3)), x.array)
#        return Geometry.Vector(r0), axis, angle, d

    def screwMotion(self):
        axis, angle = self.rotation().axisAndAngle()
        d = self.vector*axis
        x = d*axis-self.vector
        if abs(angle) < 1.e-9:
            r0 = Geometry.Vector(0., 0., 0.)
            angle = 0.
        else:
            r0 = -0.5*((N.cos(0.5*angle)/N.sin(0.5*angle))*axis.cross(x)+x)
        return r0, axis, angle, d

#
# Scaling
#
class Scaling(Transformation):

    """
    Scaling
    """
    def __init__(self, scale_factor):
        """
        @param scale_factor: the scale factor
        @type scale_factor: C{float}
        """
        self.scale_factor = scale_factor

    is_scaling = 1

    def asLinearTransformation(self):
        return LinearTransformation(self.scale_factor*Geometry.delta,
                                    Geometry.nullVector)

    def __call__(self, vector):
        return self.scale_factor*vector

    def __mul__(self, other):
        if hasattr(other, 'is_scaling'):
            return Scaling(self.scale_factor*other.scale_factor)
        else:
            return self.asLinearTransformation()*other.asLinearTransformation()

    def inverse(self):
        return Scaling(1./self.scale_factor)

#
# Inversion is scaling by -1
#
class Inversion(Scaling):

    def __init__(self):
        Scaling.__init__(self, -1.)

#
# Shear
#
class Shear(Transformation):

    def __init__(self, *args):
        if len(args) == 1:
            if Geometry.isTensor(args[0]):
                self.tensor = args[0]
            else:
                self.tensor = Geometry.Tensor(args[0])
                assert self.tensor.rank == 2
        elif len(args) == 3 and Geometry.isVector(args[0]) \
                 and Geometry.isVector(args[1]) and Geometry.isVector(args[2]):
            self.tensor = Geometry.Tensor([args[0].array, args[1].array,
                                           args[2].array]).transpose()

    def asLinearTransformation(self):
        return LinearTransformation(self.tensor, Geometry.nullVector)

    def __mul__(self, other):
        return self.asLinearTransformation()*other

    def __call__(self, vector):
        return self.tensor*vector

    def inverse(self):
        return Shear(self.tensor.inverse())

#
# General linear transformation
#
class LinearTransformation(Transformation):

    """
    General linear transformation.

    Objects of this class are not created directly, but can be the
    result of a composition of transformations.
    """

    def __init__(self, tensor, vector):
        self.tensor = tensor
        self.vector = vector

    def asLinearTransformation(self):
        return self

    def __mul__(self, other):
        other = other.asLinearTransformation()
        return LinearTransformation(self.tensor.dot(other.tensor),
                                    self.tensor*other.vector+self.vector)

    def __call__(self, vector):
        return self.tensor*vector + self.vector

    def inverse(self):
        return LinearTransformation(self.tensor.inverse(), -self.vector)


# Utility functions

def angleFromSineAndCosine(y, x):
    return atan2(y, x)

def mod_angle(angle, mod):
    return (angle + mod/2.) % mod - mod/2


# Test code

if __name__ == '__main__':

    t = Translation(Geometry.Vector(1.,-2.,0))
    r = Rotation(Geometry.Vector(0.1, -2., 0.5), 1.e-10)
    q = r.asQuaternion()
    angles = r.threeAngles(Geometry.Vector(1., 0., 0.),
                           Geometry.Vector(0., 1., 0.),
                           Geometry.Vector(0., 0., 1.))
    c = t*r
    print c.screwMotion()
    s = Scaling(2.)
    all = s*t*r
    print all(Geometry.ex)