Annotation of python/pyvtk/lib/Normals.py, revision 1.3

1.1       pearu       1: #!/usr/bin/env python
                      2: """
1.3     ! pearu       3: Normals
        !             4: """
        !             5: """
1.1       pearu       6: 
                      7: Copyright 2001 Pearu Peterson all rights reserved,
                      8: Pearu Peterson <pearu@ioc.ee>          
                      9: Permission to use, modify, and distribute this software is given under the
                     10: terms of the LGPL.  See http://www.fsf.org
                     11: 
                     12: NO WARRANTY IS EXPRESSED OR IMPLIED.  USE AT YOUR OWN RISK.
1.3     ! pearu      13: $Revision: 1.2 $
        !            14: $Date: 2001/05/22 18:29:42 $
1.1       pearu      15: Pearu Peterson
                     16: """
                     17: 
                     18: import DataSetAttr
                     19: import math
1.3     ! pearu      20: import common
1.1       pearu      21: 
                     22: class Normals(DataSetAttr.DataSetAttr):
                     23:     """Holds VTK Normals.
1.3     ! pearu      24:     Usage:
        !            25:       Normals(<sequence of 3-tuples> ,name = <string>)
        !            26:     Attributes:
        !            27:       normals
        !            28:       name
        !            29:     Public methods:
        !            30:       get_size()
        !            31:       to_string(format = 'ascii')
1.1       pearu      32:     """
                     33:     def __init__(self,normals,name=None):
                     34:         self.name = self._get_name(name)
                     35:         seq = []
                     36:         for v in self.get_3_tuple_list(normals,(self.default_value,)*3):
                     37:             n = math.sqrt(v[0]*v[0]+v[1]*v[1]+v[2]*v[2])
1.2       pearu      38:             if n==0:
                     39:                 self.warning('cannot normalize zero vector')
                     40:                 seq.append(v)
                     41:             elif n==1:
1.1       pearu      42:                 seq.append(v)
                     43:             else:
                     44:                 seq.append(tuple([c/n for c in v]))
                     45:         self.normals = seq
                     46:     def to_string(self,format='ascii'):
                     47:         t = self.get_datatype(self.normals)
1.2       pearu      48:         ret = ['NORMALS %s %s'%(self.name,t),
                     49:                self.seq_to_string(self.normals,format,t)]
1.1       pearu      50:         return '\n'.join(ret)
                     51:     def get_size(self):
                     52:         return len(self.normals)
1.3     ! pearu      53: 
        !            54: def normals_fromfile(f,n,sl):
        !            55:     dataname = sl[0]
        !            56:     datatype = sl[1].lower()
        !            57:     assert datatype in ['bit','unsigned_char','char','unsigned_short','short','unsigned_int','int','unsigned_long','long','float','double'],`datatype`
        !            58:     normals = []
        !            59:     while len(normals) < 3*n:
        !            60:         normals += map(eval,common._getline(f).split(' '))
        !            61:     assert len(normals) == 3*n
        !            62:     return Normals(normals,dataname)
1.1       pearu      63: 
                     64: if __name__ == "__main__":
                     65:     print Normals([[3,3],[4,3.],240,3,2]).to_string()

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>