~ubuntu-branches/ubuntu/intrepid/blender/intrepid-updates

« back to all changes in this revision

Viewing changes to source/blender/python/api2_2x/Mathutils.c

  • Committer: Bazaar Package Importer
  • Author(s): Cyril Brulebois
  • Date: 2008-08-08 02:45:40 UTC
  • mfrom: (12.1.14 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080808024540-kkjp7ekfivzhuw3l
Tags: 2.46+dfsg-4
* Fix python syntax warning in import_dxf.py, which led to nasty output
  in installation/upgrade logs during byte-compilation, using a patch
  provided by the script author (Closes: #492280):
   - debian/patches/45_fix_python_syntax_warning

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* 
2
 
 * $Id: Mathutils.c,v 1.30 2006/07/02 15:28:28 campbellbarton Exp $
 
2
 * $Id: Mathutils.c 14444 2008-04-16 22:40:48Z hos $
3
3
 *
4
 
 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
 
4
 * ***** BEGIN GPL LICENSE BLOCK *****
5
5
 *
6
6
 * This program is free software; you can redistribute it and/or
7
7
 * modify it under the terms of the GNU General Public License
8
8
 * as published by the Free Software Foundation; either version 2
9
 
 * of the License, or (at your option) any later version. The Blender
10
 
 * Foundation also sells licenses for use in proprietary software under
11
 
 * the Blender License.  See http://www.blender.org/BL/ for information
12
 
 * about this.
 
9
 * of the License, or (at your option) any later version.
13
10
 *
14
11
 * This program is distributed in the hope that it will be useful,
15
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27
24
 *
28
25
 * Contributor(s): Joseph Gilbert, Campbell Barton
29
26
 *
30
 
 * ***** END GPL/BL DUAL LICENSE BLOCK *****
 
27
 * ***** END GPL LICENSE BLOCK *****
31
28
 */
32
29
 
33
30
#include "Mathutils.h"
59
56
static char M_Mathutils_OrthoProjectionMatrix_doc[] = "() - construct a orthographic projection matrix from a selected plane";
60
57
static char M_Mathutils_ShearMatrix_doc[] = "() - construct a shearing matrix from a plane of shear and a shear factor";
61
58
static char M_Mathutils_CopyMat_doc[] = "() - create a copy of a matrix";
62
 
static char M_Mathutils_TranslationMatrix_doc[] = "() - create a translation matrix from a vector";
 
59
static char M_Mathutils_TranslationMatrix_doc[] = "(vec) - create a translation matrix from a vector";
63
60
static char M_Mathutils_CopyQuat_doc[] = "() - copy quatB to quatA";
64
61
static char M_Mathutils_CopyEuler_doc[] = "() - copy eulB to eultA";
65
62
static char M_Mathutils_CrossQuats_doc[] = "() - return the mutliplication of two quaternions";
88
85
        {"RotationMatrix", (PyCFunction) M_Mathutils_RotationMatrix, METH_VARARGS, M_Mathutils_RotationMatrix_doc},
89
86
        {"ScaleMatrix", (PyCFunction) M_Mathutils_ScaleMatrix, METH_VARARGS, M_Mathutils_ScaleMatrix_doc},
90
87
        {"ShearMatrix", (PyCFunction) M_Mathutils_ShearMatrix, METH_VARARGS, M_Mathutils_ShearMatrix_doc},
91
 
        {"TranslationMatrix", (PyCFunction) M_Mathutils_TranslationMatrix, METH_VARARGS, M_Mathutils_TranslationMatrix_doc},
 
88
        {"TranslationMatrix", (PyCFunction) M_Mathutils_TranslationMatrix, METH_O, M_Mathutils_TranslationMatrix_doc},
92
89
        {"CopyMat", (PyCFunction) M_Mathutils_CopyMat, METH_VARARGS, M_Mathutils_CopyMat_doc},
93
90
        {"OrthoProjectionMatrix", (PyCFunction) M_Mathutils_OrthoProjectionMatrix,  METH_VARARGS, M_Mathutils_OrthoProjectionMatrix_doc},
94
91
        {"MatMultVec", (PyCFunction) M_Mathutils_MatMultVec, METH_VARARGS, M_Mathutils_MatMultVec_doc},
117
114
        //seed the generator for the rand function
118
115
        BLI_srand((unsigned int) (PIL_check_seconds_timer() *
119
116
                                      0x7FFFFFFF));
120
 
 
 
117
        
 
118
        /* needed for getseters */
 
119
        if( PyType_Ready( &vector_Type ) < 0 )
 
120
                return NULL;
 
121
        if( PyType_Ready( &matrix_Type ) < 0 )
 
122
                return NULL;    
 
123
        if( PyType_Ready( &euler_Type ) < 0 )
 
124
                return NULL;
 
125
        if( PyType_Ready( &quaternion_Type ) < 0 )
 
126
                return NULL;
 
127
        
121
128
        submodule = Py_InitModule3("Blender.Mathutils",
122
129
                                    M_Mathutils_methods, M_Mathutils_doc);
123
130
        return (submodule);
138
145
        if(mat->rowSize != vec->size){
139
146
                if(mat->rowSize == 4 && vec->size != 3){
140
147
                        return EXPP_ReturnPyObjError(PyExc_AttributeError,
141
 
                                "matrix * vector: matrix row size and vector size must be the same\n");
 
148
                                "matrix * vector: matrix row size and vector size must be the same");
142
149
                }else{
143
 
                        vecCopy[3] = 0.0f;
 
150
                        vecCopy[3] = 1.0f;
144
151
                }
145
152
        }
146
153
 
197
204
{
198
205
        float vecNew[4], vecCopy[4];
199
206
        double dot = 0.0f;
200
 
        int x, y, z = 0, size;
 
207
        int x, y, z = 0, vec_size = vec->size;
201
208
 
202
 
        if(mat->colSize != vec->size){
203
 
                if(mat->rowSize == 4 && vec->size != 3){
 
209
        if(mat->colSize != vec_size){
 
210
                if(mat->rowSize == 4 && vec_size != 3){
204
211
                        return EXPP_ReturnPyObjError(PyExc_AttributeError, 
205
 
                                "vector * matrix: matrix column size and the vector size must be the same\n");
 
212
                                "vector * matrix: matrix column size and the vector size must be the same");
206
213
                }else{
207
 
                        vecCopy[3] = 0.0f;
 
214
                        vecCopy[3] = 1.0f;
208
215
                }
209
216
        }
210
 
        size = vec->size;
211
 
        for(x = 0; x < vec->size; x++){
 
217
        
 
218
        for(x = 0; x < vec_size; x++){
212
219
                vecCopy[x] = vec->vec[x];
213
220
        }
214
221
 
220
227
                vecNew[z++] = (float)dot;
221
228
                dot = 0.0f;
222
229
        }
223
 
        return newVectorObject(vecNew, size, Py_NEW);
 
230
        return newVectorObject(vecNew, vec_size, Py_NEW);
224
231
}
225
232
//This is a helper for the point class
226
233
PyObject *row_point_multiplication(PointObject* pt, MatrixObject * mat)
450
457
 
451
458
        if(!PyArg_ParseTuple(args, "O!O!", &vector_Type, &vec1, &vector_Type, &vec2))
452
459
                return EXPP_ReturnPyObjError(PyExc_TypeError, 
453
 
                        "Mathutils.DotVec(): expects (2) vector objects of the same size\n");
 
460
                        "Mathutils.DotVecs(): expects (2) vector objects of the same size\n");
454
461
        if(vec1->size != vec2->size)
455
462
                return EXPP_ReturnPyObjError(PyExc_AttributeError, 
456
 
                        "Mathutils.DotVec(): expects (2) vector objects of the same size\n");
 
463
                        "Mathutils.DotVecs(): expects (2) vector objects of the same size\n");
457
464
 
458
465
        for(x = 0; x < vec1->size; x++) {
459
466
                dot += vec1->vec[x] * vec2->vec[x];
490
497
        }
491
498
        dot /= (sqrt(test_v1) * sqrt(test_v2));
492
499
 
493
 
        if (dot < -1.0f || dot > 1.0f) {
494
 
                CLAMP(dot,-1.0f,1.0f);
495
 
        }
496
 
        angleRads = (double)acos(dot);
 
500
        angleRads = (double)saacos(dot);
497
501
 
498
502
        return PyFloat_FromDouble(angleRads * (180/ Py_PI));
499
503
 
656
660
                return EXPP_ReturnPyObjError (PyExc_TypeError, 
657
661
                        "Mathutils.RotationMatrix(): expected float int and optional string and vector\n");
658
662
        }
659
 
        if(angle < -360.0f || angle > 360.0f)
660
 
                return EXPP_ReturnPyObjError(PyExc_AttributeError,
661
 
                        "Mathutils.RotationMatrix(): angle size not appropriate\n");
 
663
        
 
664
        /* Clamp to -360:360 */
 
665
        while (angle<-360.0f)
 
666
                angle+=360.0;
 
667
        while (angle>360.0f)
 
668
                angle-=360.0;
 
669
        
662
670
        if(matSize != 2 && matSize != 3 && matSize != 4)
663
671
                return EXPP_ReturnPyObjError(PyExc_AttributeError,
664
672
                        "Mathutils.RotationMatrix(): can only return a 2x2 3x3 or 4x4 matrix\n");
759
767
}
760
768
//----------------------------------Mathutils.TranslationMatrix() -------
761
769
//creates a translation matrix
762
 
PyObject *M_Mathutils_TranslationMatrix(PyObject * self, PyObject * args)
 
770
PyObject *M_Mathutils_TranslationMatrix(PyObject * self, VectorObject * vec)
763
771
{
764
 
        VectorObject *vec = NULL;
765
772
        float mat[16] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
766
773
                0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f};
767
 
 
768
 
        if(!PyArg_ParseTuple(args, "O!", &vector_Type, &vec)) {
 
774
        
 
775
        if(!VectorObject_Check(vec)) {
769
776
                return EXPP_ReturnPyObjError(PyExc_TypeError,
770
777
                                                "Mathutils.TranslationMatrix(): expected vector\n");
771
778
        }
1051
1058
                        }
1052
1059
                }else{
1053
1060
                        listObject = PySequence_GetItem(args, 1);
1054
 
                        if (PySequence_Check(listObject)) {
 
1061
                        if (size>1 && PySequence_Check(listObject)) {
1055
1062
                                size = PySequence_Length(listObject);
1056
1063
                                if (size != 3) { 
1057
1064
                                        // invalid args/size
1375
1382
        VECCOPY(v3, vec3->vec);
1376
1383
 
1377
1384
        VECCOPY(dir, ray->vec);
1378
 
        Normalise(dir);
 
1385
        Normalize(dir);
1379
1386
 
1380
1387
        VECCOPY(orig, ray_off->vec);
1381
1388
 
1471
1478
                VecSubf(b, v4, v3);
1472
1479
 
1473
1480
                VECCOPY(dir1, a);
1474
 
                Normalise(dir1);
 
1481
                Normalize(dir1);
1475
1482
                VECCOPY(dir2, b);
1476
 
                Normalise(dir2);
 
1483
                Normalize(dir2);
1477
1484
                d = Inpf(dir1, dir2);
1478
1485
                if (d == 1.0f || d == -1.0f) {
1479
1486
                        /* colinear */
1563
1570
        VecSubf(e2, v3, v2);
1564
1571
 
1565
1572
        Crossf(n1, e2, e1);
1566
 
        Normalise(n1);
 
1573
        Normalize(n1);
1567
1574
 
1568
1575
        /* find vectors for two edges sharing v4 */
1569
1576
        VecSubf(e1, v3, v4);
1570
1577
        VecSubf(e2, v1, v4);
1571
1578
 
1572
1579
        Crossf(n2, e2, e1);
1573
 
        Normalise(n2);
 
1580
        Normalize(n2);
1574
1581
 
1575
1582
        /* adding and averaging the normals of both triangles */
1576
1583
        VecAddf(n1, n2, n1);
1577
 
        Normalise(n1);
 
1584
        Normalize(n1);
1578
1585
 
1579
1586
        return newVectorObject(n1, 3, Py_NEW);
1580
1587
}
1606
1613
        VecSubf(e2, v3, v2);
1607
1614
 
1608
1615
        Crossf(n, e2, e1);
1609
 
        Normalise(n);
 
1616
        Normalize(n);
1610
1617
 
1611
1618
        return newVectorObject(n, 3, Py_NEW);
1612
1619
}
1658
1665
PyObject *M_Mathutils_CopyMat(PyObject * self, PyObject * args)
1659
1666
{
1660
1667
        PyObject *matrix = NULL;
1661
 
 
1662
 
        printf("Mathutils.CopyMat(): Deprecated :use Mathutils.Matrix() to copy matrices\n");
 
1668
        static char warning = 1;
 
1669
 
 
1670
        if( warning ) {
 
1671
                printf("Mathutils.CopyMat(): deprecated :use Mathutils.Matrix() to copy matrices\n");
 
1672
                --warning;
 
1673
        }
 
1674
 
1663
1675
        matrix = M_Mathutils_Matrix(self, args);
1664
1676
        if(matrix == NULL)
1665
1677
                return NULL; //error string already set if we get here
1671
1683
PyObject *M_Mathutils_CopyVec(PyObject * self, PyObject * args)
1672
1684
{
1673
1685
        PyObject *vec = NULL;
1674
 
 
1675
 
        printf("Mathutils.CopyVec(): Deprecated: use Mathutils.Vector() to copy vectors\n");
 
1686
        static char warning = 1;
 
1687
 
 
1688
        if( warning ) {
 
1689
                printf("Mathutils.CopyVec(): Deprecated: use Mathutils.Vector() to copy vectors\n");
 
1690
                --warning;
 
1691
        }
 
1692
 
1676
1693
        vec = M_Mathutils_Vector(self, args);
1677
1694
        if(vec == NULL)
1678
1695
                return NULL; //error string already set if we get here
1684
1701
PyObject *M_Mathutils_CopyQuat(PyObject * self, PyObject * args)
1685
1702
{
1686
1703
        PyObject *quat = NULL;
1687
 
 
1688
 
        printf("Mathutils.CopyQuat(): Deprecated:use Mathutils.Quaternion() to copy vectors\n");
 
1704
        static char warning = 1;
 
1705
 
 
1706
        if( warning ) {
 
1707
                printf("Mathutils.CopyQuat(): Deprecated: use Mathutils.Quaternion() to copy vectors\n");
 
1708
                --warning;
 
1709
        }
 
1710
 
1689
1711
        quat = M_Mathutils_Quaternion(self, args);
1690
1712
        if(quat == NULL)
1691
1713
                return NULL; //error string already set if we get here
1697
1719
PyObject *M_Mathutils_CopyEuler(PyObject * self, PyObject * args)
1698
1720
{
1699
1721
        PyObject *eul = NULL;
1700
 
 
1701
 
        printf("Mathutils.CopyEuler(): Deprecated:use Mathutils.Euler() to copy vectors\n");
 
1722
        static char warning = 1;
 
1723
 
 
1724
        if( warning ) {
 
1725
                printf("Mathutils.CopyEuler(): deprecated:use Mathutils.Euler() to copy vectors\n");
 
1726
                --warning;
 
1727
        }
 
1728
 
1702
1729
        eul = M_Mathutils_Euler(self, args);
1703
1730
        if(eul == NULL)
1704
1731
                return NULL; //error string already set if we get here
1713
1740
        EulerObject *Eul = NULL;
1714
1741
        float angle;
1715
1742
        char *axis;
 
1743
        static char warning = 1;
 
1744
 
 
1745
        if( warning ) {
 
1746
                printf("Mathutils.RotateEuler(): Deprecated:use Euler.rotate() to rotate a euler\n");
 
1747
                --warning;
 
1748
        }
1716
1749
 
1717
1750
        if(!PyArg_ParseTuple(args, "O!fs", &euler_Type, &Eul, &angle, &axis))
1718
1751
                return EXPP_ReturnPyObjError(PyExc_TypeError,
1719
1752
                           "Mathutils.RotateEuler(): expected euler type & float & string");
1720
1753
 
1721
 
        printf("Mathutils.RotateEuler(): Deprecated:use Euler.rotate() to rotate a euler\n");
1722
1754
        Euler_Rotate(Eul, Py_BuildValue("fs", angle, axis));
1723
 
        return EXPP_incr_ret(Py_None);
 
1755
        Py_RETURN_NONE;
1724
1756
}
1725
1757
//----------------------------------Mathutils.MatMultVec() --------------
1726
1758
//COLUMN VECTOR Multiplication (Matrix X Vector)
1728
1760
{
1729
1761
        MatrixObject *mat = NULL;
1730
1762
        VectorObject *vec = NULL;
 
1763
        static char warning = 1;
 
1764
 
 
1765
        if( warning ) {
 
1766
                printf("Mathutils.MatMultVec(): Deprecated: use matrix * vec to perform column vector multiplication\n");
 
1767
                --warning;
 
1768
        }
1731
1769
 
1732
1770
        //get pyObjects
1733
1771
        if(!PyArg_ParseTuple(args, "O!O!", &matrix_Type, &mat, &vector_Type, &vec))
1734
1772
                return EXPP_ReturnPyObjError(PyExc_TypeError, 
1735
1773
                        "Mathutils.MatMultVec(): MatMultVec() expects a matrix and a vector object - in that order\n");
1736
1774
 
1737
 
        printf("Mathutils.MatMultVec(): Deprecated: use matrix * vec to perform column vector multiplication\n");
1738
1775
        return column_vector_multiplication(mat, vec);
1739
1776
}
1740
1777
//----------------------------------Mathutils.VecMultMat() ---------------
1743
1780
{
1744
1781
        MatrixObject *mat = NULL;
1745
1782
        VectorObject *vec = NULL;
 
1783
        static char warning = 1;
 
1784
 
 
1785
        if( warning ) {
 
1786
                printf("Mathutils.VecMultMat(): Deprecated: use vec * matrix to perform row vector multiplication\n");
 
1787
                --warning;
 
1788
        }
1746
1789
 
1747
1790
        //get pyObjects
1748
1791
        if(!PyArg_ParseTuple(args, "O!O!", &vector_Type, &vec, &matrix_Type, &mat))
1749
1792
                return EXPP_ReturnPyObjError(PyExc_TypeError, 
1750
1793
                        "Mathutils.VecMultMat(): VecMultMat() expects a vector and matrix object - in that order\n");
1751
1794
 
1752
 
        printf("Mathutils.VecMultMat(): Deprecated: use vec * matrix to perform row vector multiplication\n");
1753
1795
        return row_vector_multiplication(vec, mat);
1754
1796
}
1755
1797
//#######################################################################