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

« back to all changes in this revision

Viewing changes to source/blender/blenkernel/intern/mball.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:
6
6
 *  
7
7
 * texture coordinates are patched within the displist
8
8
 * 
9
 
 * $Id: mball.c,v 1.28 2006/07/07 11:10:53 jiri Exp $
 
9
 * $Id: mball.c 14883 2008-05-18 13:57:47Z ton $
10
10
 *
11
 
 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
 
11
 * ***** BEGIN GPL LICENSE BLOCK *****
12
12
 *
13
13
 * This program is free software; you can redistribute it and/or
14
14
 * modify it under the terms of the GNU General Public License
15
15
 * as published by the Free Software Foundation; either version 2
16
 
 * of the License, or (at your option) any later version. The Blender
17
 
 * Foundation also sells licenses for use in proprietary software under
18
 
 * the Blender License.  See http://www.blender.org/BL/ for information
19
 
 * about this.
 
16
 * of the License, or (at your option) any later version.
20
17
 *
21
18
 * This program is distributed in the hope that it will be useful,
22
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32
29
 *
33
30
 * Contributor(s): Jiri Hnidek <jiri.hnidek@vslib.cz>.
34
31
 *
35
 
 * ***** END GPL/BL DUAL LICENSE BLOCK *****
 
32
 * ***** END GPL LICENSE BLOCK *****
36
33
 */
37
34
 
38
35
#include <stdio.h>
40
37
#include <math.h>
41
38
#include <stdlib.h>
42
39
#include <ctype.h>
 
40
#include <float.h>
43
41
#include "MEM_guardedalloc.h"
44
42
 
45
43
#include "DNA_material_types.h"
74
72
float thresh= 0.6f;
75
73
int totelem=0;
76
74
MetaElem **mainb;
77
 
octal_tree *metaball_tree;
 
75
octal_tree *metaball_tree = NULL;
78
76
/* Functions */
79
77
 
80
78
void unlink_mball(MetaBall *mb)
99
97
        if(mb->disp.first) freedisplist(&mb->disp);
100
98
}
101
99
 
102
 
MetaBall *add_mball()
 
100
MetaBall *add_mball(char *name)
103
101
{
104
102
        MetaBall *mb;
105
103
        
106
 
        mb= alloc_libblock(&G.main->mball, ID_MB, "Meta");
 
104
        mb= alloc_libblock(&G.main->mball, ID_MB, name);
107
105
        
108
106
        mb->size[0]= mb->size[1]= mb->size[2]= 1.0;
109
107
        mb->texflag= MB_AUTOSPACE;
232
230
        boundbox_set_from_min_max(bb, min, max);
233
231
}
234
232
 
235
 
void make_orco_mball(Object *ob)
 
233
float *make_orco_mball(Object *ob)
236
234
{
237
235
        BoundBox *bb;
238
236
        DispList *dl;
239
 
        float *data;
 
237
        float *data, *orco, *orcodata;
240
238
        float loc[3], size[3];
241
239
        int a;
242
 
        
 
240
 
243
241
        /* restore size and loc */
244
242
        bb= ob->bb;
245
243
        loc[0]= (bb->vec[0][0]+bb->vec[4][0])/2.0f;
250
248
        size[2]= bb->vec[1][2]-loc[2];
251
249
 
252
250
        dl= ob->disp.first;
 
251
        orcodata= MEM_mallocN(sizeof(float)*3*dl->nr, "MballOrco");
 
252
 
253
253
        data= dl->verts;
 
254
        orco= orcodata;
254
255
        a= dl->nr;
255
256
        while(a--) {
256
 
                data[0]= (data[0]-loc[0])/size[0];
257
 
                data[1]= (data[1]-loc[1])/size[1];
258
 
                data[2]= (data[2]-loc[2])/size[2];
 
257
                orco[0]= (data[0]-loc[0])/size[0];
 
258
                orco[1]= (data[1]-loc[1])/size[1];
 
259
                orco[2]= (data[2]-loc[2])/size[2];
259
260
 
260
261
                data+= 3;
 
262
                orco+= 3;
261
263
        }
 
264
 
 
265
        return orcodata;
262
266
}
263
267
/** \brief Test, if Object *ob is basic MetaBall.
264
268
 *
301
305
                                /* if bob object is in edit mode, then dynamic list of all MetaElems
302
306
                                 * is stored in editelems */
303
307
                                if(ob==G.obedit) ml= editelems.first;
 
308
                                /* keep track of linked data too! */
 
309
                                else if(G.obedit && G.obedit->data==ob->data) ml= editelems.first;
304
310
                                /* if bob object is in object mode */
305
311
                                else ml= ((MetaBall*)ob->data)->elems.first;
306
312
                        }
369
375
#define RTF     7  /* right top far corner     */
370
376
 
371
377
/* the LBN corner of cube (i, j, k), corresponds with location
372
 
 * (start.x+(i-0.5)*size, start.y+(j-0.5)*size, start.z+(k-0.5)*size) */
 
378
 * (i-0.5)*size, (j-0.5)*size, (k-0.5)*size) */
373
379
 
374
380
#define HASHBIT     (5)
375
381
#define HASHSIZE    (size_t)(1<<(3*HASHBIT))   /*! < hash table size (32768) */
569
575
 
570
576
/* ******************************************** */
571
577
 
572
 
int *indices=0;
 
578
int *indices=NULL;
573
579
int totindex, curindex;
574
580
 
575
581
 
591
597
        
592
598
        cur= indices+4*curindex;
593
599
 
594
 
        /* prevent zero codes for faces indices */
595
 
        if(i3==0) {
596
 
                if(i4) {
597
 
                        i3= i4;
598
 
                        i4= i1;
599
 
                        i1= i2;
600
 
                        i2= 0;
601
 
                }
602
 
                else {
603
 
                        i3= i2;
604
 
                        i2= i1;
605
 
                        i1= 0;
606
 
                }
607
 
        }
 
600
        /* diplists now support array drawing, we treat trias as fake quad */
608
601
        
609
602
        cur[0]= i1;
610
603
        cur[1]= i2;
611
604
        cur[2]= i3;
612
 
        cur[3]= i4;
 
605
        if(i4==0)
 
606
                cur[3]= i3;
 
607
        else 
 
608
                cur[3]= i4;
613
609
        
614
610
        curindex++;
615
611
 
840
836
        c = (CORNER *) new_pgn_element(sizeof(CORNER));
841
837
 
842
838
        c->i = i; 
843
 
        c->x = p->start.x+((float)i-0.5f)*p->size;
 
839
        c->x = ((float)i-0.5f)*p->size;
844
840
        c->j = j; 
845
 
        c->y = p->start.y+((float)j-0.5f)*p->size;
 
841
        c->y = ((float)j-0.5f)*p->size;
846
842
        c->k = k; 
847
 
        c->z = p->start.z+((float)k-0.5f)*p->size;
 
843
        c->z = ((float)k-0.5f)*p->size;
848
844
        c->value = p->function(c->x, c->y, c->z);
849
845
        
850
846
        c->next = p->corners[index];
1218
1214
                p->y = neg.y;
1219
1215
                p->z = neg.z;
1220
1216
                while (1) {
 
1217
                        if (i++ == RES) return;
1221
1218
                        p->x = 0.5f*(pos.x + neg.x);
1222
 
                        if (i++ == RES) return;
1223
1219
                        if ((function(p->x,p->y,p->z)) > 0.0)   pos.x = p->x; else neg.x = p->x; 
1224
1220
                }
1225
1221
        }
1228
1224
                p->x = neg.x;
1229
1225
                p->z = neg.z;
1230
1226
                while (1) {
 
1227
                        if (i++ == RES) return;
1231
1228
                        p->y = 0.5f*(pos.y + neg.y);
1232
 
                        if (i++ == RES) return;
1233
1229
                        if ((function(p->x,p->y,p->z)) > 0.0)   pos.y = p->y; else neg.y = p->y;
1234
1230
                }
1235
1231
        }
1238
1234
                p->x = neg.x;
1239
1235
                p->y = neg.y;
1240
1236
                while (1) {
 
1237
                        if (i++ == RES) return;
1241
1238
                        p->z = 0.5f*(pos.z + neg.z);
1242
 
                        if (i++ == RES) return;
1243
1239
                        if ((function(p->x,p->y,p->z)) > 0.0)   pos.z = p->z; else neg.z = p->z;
1244
1240
                }
1245
1241
        }
1304
1300
        int index[3]={1,0,-1};
1305
1301
        float f =0.0f;
1306
1302
        float in_v, out_v;
 
1303
        MB_POINT workp;
 
1304
        float tmp_v, workp_v, max_len, len, dx, dy, dz, nx, ny, nz, MAXN;
1307
1305
 
1308
1306
        ml = mainb[a];
1309
1307
 
1364
1362
 
1365
1363
                                        out_v = mbproc->function(out.x, out.y, out.z);
1366
1364
 
1367
 
                                        /* find "first point" on Implicit Surface of MetaElemnt ml */
1368
 
                                        converge(&in, &out, in_v, out_v, mbproc->function, &mbproc->start, mb, 0);
1369
 
        
1370
 
                                        /* indexes of CUBE, which includes "first point" */
1371
 
                                        c_i= (int)floor(mbproc->start.x/mbproc->size );
1372
 
                                        c_j= (int)floor(mbproc->start.y/mbproc->size );
1373
 
                                        c_k= (int)floor(mbproc->start.z/mbproc->size );
1374
 
                
1375
 
                                        mbproc->start.x= mbproc->start.y= mbproc->start.z= 0.0;
 
1365
                                        /* find "first points" on Implicit Surface of MetaElemnt ml */
 
1366
                                        workp.x = in.x;
 
1367
                                        workp.y = in.y;
 
1368
                                        workp.z = in.z;
 
1369
                                        workp_v = in_v;
 
1370
                                        max_len = sqrt((out.x-in.x)*(out.x-in.x) + (out.y-in.y)*(out.y-in.y) + (out.z-in.z)*(out.z-in.z));
1376
1371
 
1377
 
                                        /* add CUBE (with indexes c_i, c_j, c_k) to the stack,
1378
 
                                         * this cube includes found point of Implicit Surface */
1379
 
                                        if (ml->flag & MB_NEGATIVE)
1380
 
                                                add_cube(mbproc, c_i, c_j, c_k, 2);
1381
 
                                        else
1382
 
                                                add_cube(mbproc, c_i, c_j, c_k, 1);
1383
 
                                                
 
1372
                                        nx = abs((out.x - in.x)/mbproc->size);
 
1373
                                        ny = abs((out.y - in.y)/mbproc->size);
 
1374
                                        nz = abs((out.z - in.z)/mbproc->size);
1384
1375
                                        
 
1376
                                        MAXN = MAX3(nx,ny,nz);
 
1377
                                        if(MAXN!=0.0f) {
 
1378
                                                dx = (out.x - in.x)/MAXN;
 
1379
                                                dy = (out.y - in.y)/MAXN;
 
1380
                                                dz = (out.z - in.z)/MAXN;
 
1381
 
 
1382
                                                len = 0.0;
 
1383
                                                while(len<=max_len) {
 
1384
                                                        workp.x += dx;
 
1385
                                                        workp.y += dy;
 
1386
                                                        workp.z += dz;
 
1387
                                                        /* compute value of implicite function */
 
1388
                                                        tmp_v = mbproc->function(workp.x, workp.y, workp.z);
 
1389
                                                        /* add cube to the stack, when value of implicite function crosses zero value */
 
1390
                                                        if((tmp_v<0.0 && workp_v>=0.0)||(tmp_v>0.0 && workp_v<=0.0)) {
 
1391
 
 
1392
                                                                /* indexes of CUBE, which includes "first point" */
 
1393
                                                                c_i= (int)floor(workp.x/mbproc->size);
 
1394
                                                                c_j= (int)floor(workp.y/mbproc->size);
 
1395
                                                                c_k= (int)floor(workp.z/mbproc->size);
 
1396
                                                                
 
1397
                                                                /* add CUBE (with indexes c_i, c_j, c_k) to the stack,
 
1398
                                                                 * this cube includes found point of Implicit Surface */
 
1399
                                                                if (ml->flag & MB_NEGATIVE)
 
1400
                                                                        add_cube(mbproc, c_i, c_j, c_k, 2);
 
1401
                                                                else
 
1402
                                                                        add_cube(mbproc, c_i, c_j, c_k, 1);
 
1403
                                                        }
 
1404
                                                        len = sqrt((workp.x-in.x)*(workp.x-in.x) + (workp.y-in.y)*(workp.y-in.y) + (workp.z-in.z)*(workp.z-in.z));
 
1405
                                                        workp_v = tmp_v;
 
1406
 
 
1407
                                                }
 
1408
                                        }
1385
1409
                                }
1386
1410
                        }
1387
1411
                }
1686
1710
 *  +------+------+
1687
1711
 *  
1688
1712
 */
1689
 
void subdivide_metaball_octal_node(octal_node *node, float *size, short depth)
 
1713
void subdivide_metaball_octal_node(octal_node *node, float size_x, float size_y, float size_z, short depth)
1690
1714
{
1691
1715
        MetaElem *ml;
1692
1716
        ml_pointer *ml_p;
1693
1717
        float x,y,z;
1694
1718
        int a,i;
1695
1719
 
1696
 
        if(depth==0) return;
1697
 
 
1698
1720
        /* create new nodes */
1699
1721
        for(a=0;a<8;a++){
1700
1722
                node->nodes[a]= MEM_mallocN(sizeof(octal_node),"octal_node");
1708
1730
                node->nodes[a]->pos= 0;
1709
1731
        }
1710
1732
 
1711
 
        size[0]/=2; size[1]/=2; size[2]/=2;
 
1733
        size_x /= 2;
 
1734
        size_y /= 2;
 
1735
        size_z /= 2;
1712
1736
        
1713
1737
        /* center of node */
1714
 
        node->x= x= node->x_min + size[0];
1715
 
        node->y= y= node->y_min + size[1];
1716
 
        node->z= z= node->z_min + size[2];
 
1738
        node->x = x = node->x_min + size_x;
 
1739
        node->y = y = node->y_min + size_y;
 
1740
        node->z = z = node->z_min + size_z;
1717
1741
 
1718
1742
        /* setting up of border points of new nodes */
1719
 
        node->nodes[0]->x_min= node->x_min;
1720
 
        node->nodes[0]->y_min= node->y_min;
1721
 
        node->nodes[0]->z_min= node->z_min;
1722
 
        
1723
 
        node->nodes[1]->x_min= x;
1724
 
        node->nodes[1]->y_min= node->y_min;
1725
 
        node->nodes[1]->z_min= node->z_min;
1726
 
 
1727
 
        node->nodes[2]->x_min= x;
1728
 
        node->nodes[2]->y_min= y;
1729
 
        node->nodes[2]->z_min= node->z_min;
1730
 
 
1731
 
        node->nodes[3]->x_min= node->x_min;
1732
 
        node->nodes[3]->y_min= y;
1733
 
        node->nodes[3]->z_min= node->z_min;
1734
 
 
1735
 
        node->nodes[4]->x_min= node->x_min;
1736
 
        node->nodes[4]->y_min= node->y_min;
1737
 
        node->nodes[4]->z_min= z;
1738
 
        
1739
 
        node->nodes[5]->x_min= x;
1740
 
        node->nodes[5]->y_min= node->y_min;
1741
 
        node->nodes[5]->z_min= z;
1742
 
 
1743
 
        node->nodes[6]->x_min= x;
1744
 
        node->nodes[6]->y_min= y;
1745
 
        node->nodes[6]->z_min= z;
1746
 
 
1747
 
        node->nodes[7]->x_min= node->x_min;
1748
 
        node->nodes[7]->y_min= y;
1749
 
        node->nodes[7]->z_min= z;
 
1743
        node->nodes[0]->x_min = node->x_min;
 
1744
        node->nodes[0]->y_min = node->y_min;
 
1745
        node->nodes[0]->z_min = node->z_min;
 
1746
        node->nodes[0]->x = node->nodes[0]->x_min + size_x/2;
 
1747
        node->nodes[0]->y = node->nodes[0]->y_min + size_y/2;
 
1748
        node->nodes[0]->z = node->nodes[0]->z_min + size_z/2;
 
1749
        
 
1750
        node->nodes[1]->x_min = x;
 
1751
        node->nodes[1]->y_min = node->y_min;
 
1752
        node->nodes[1]->z_min = node->z_min;
 
1753
        node->nodes[1]->x = node->nodes[1]->x_min + size_x/2;
 
1754
        node->nodes[1]->y = node->nodes[1]->y_min + size_y/2;
 
1755
        node->nodes[1]->z = node->nodes[1]->z_min + size_z/2;
 
1756
 
 
1757
        node->nodes[2]->x_min = x;
 
1758
        node->nodes[2]->y_min = y;
 
1759
        node->nodes[2]->z_min = node->z_min;
 
1760
        node->nodes[2]->x = node->nodes[2]->x_min + size_x/2;
 
1761
        node->nodes[2]->y = node->nodes[2]->y_min + size_y/2;
 
1762
        node->nodes[2]->z = node->nodes[2]->z_min + size_z/2;
 
1763
 
 
1764
        node->nodes[3]->x_min = node->x_min;
 
1765
        node->nodes[3]->y_min = y;
 
1766
        node->nodes[3]->z_min = node->z_min;
 
1767
        node->nodes[3]->x = node->nodes[3]->x_min + size_x/2;
 
1768
        node->nodes[3]->y = node->nodes[3]->y_min + size_y/2;
 
1769
        node->nodes[3]->z = node->nodes[3]->z_min + size_z/2;
 
1770
 
 
1771
        node->nodes[4]->x_min = node->x_min;
 
1772
        node->nodes[4]->y_min = node->y_min;
 
1773
        node->nodes[4]->z_min = z;
 
1774
        node->nodes[4]->x = node->nodes[4]->x_min + size_x/2;
 
1775
        node->nodes[4]->y = node->nodes[4]->y_min + size_y/2;
 
1776
        node->nodes[4]->z = node->nodes[4]->z_min + size_z/2;
 
1777
        
 
1778
        node->nodes[5]->x_min = x;
 
1779
        node->nodes[5]->y_min = node->y_min;
 
1780
        node->nodes[5]->z_min = z;
 
1781
        node->nodes[5]->x = node->nodes[5]->x_min + size_x/2;
 
1782
        node->nodes[5]->y = node->nodes[5]->y_min + size_y/2;
 
1783
        node->nodes[5]->z = node->nodes[5]->z_min + size_z/2;
 
1784
 
 
1785
        node->nodes[6]->x_min = x;
 
1786
        node->nodes[6]->y_min = y;
 
1787
        node->nodes[6]->z_min = z;
 
1788
        node->nodes[6]->x = node->nodes[6]->x_min + size_x/2;
 
1789
        node->nodes[6]->y = node->nodes[6]->y_min + size_y/2;
 
1790
        node->nodes[6]->z = node->nodes[6]->z_min + size_z/2;
 
1791
 
 
1792
        node->nodes[7]->x_min = node->x_min;
 
1793
        node->nodes[7]->y_min = y;
 
1794
        node->nodes[7]->z_min = z;
 
1795
        node->nodes[7]->x = node->nodes[7]->x_min + size_x/2;
 
1796
        node->nodes[7]->y = node->nodes[7]->y_min + size_y/2;
 
1797
        node->nodes[7]->z = node->nodes[7]->z_min + size_z/2;
1750
1798
 
1751
1799
        ml_p= node->elems.first;
1752
1800
        
1913
1961
        if(depth>0){
1914
1962
                for(a=0;a<8;a++){
1915
1963
                        if(node->nodes[a]->count > 0) /* if node is not empty, then it is subdivided */
1916
 
                                subdivide_metaball_octal_node(node->nodes[a], size, depth);
 
1964
                                subdivide_metaball_octal_node(node->nodes[a], size_x, size_y, size_z, depth);
1917
1965
                }
1918
1966
        }
1919
1967
}
1926
1974
                if(node->nodes[a]!=NULL) free_metaball_octal_node(node->nodes[a]);
1927
1975
        }
1928
1976
        BLI_freelistN(&node->elems);
1929
 
        if(node) MEM_freeN(node);
 
1977
        MEM_freeN(node);
1930
1978
}
1931
1979
 
1932
1980
/* If scene include more then one MetaElem, then octree is used */
1952
2000
        for(a=0;a<8;a++)
1953
2001
                node->nodes[a]=NULL;
1954
2002
 
1955
 
        node->x_min= node->y_min= node->z_min= 10000000.0;
1956
 
        node->x_max= node->y_max= node->z_max= -10000000.0;
 
2003
        node->x_min= node->y_min= node->z_min= FLT_MAX;
 
2004
        node->x_max= node->y_max= node->z_max= -FLT_MAX;
1957
2005
 
1958
2006
        /* size of octal tree scene */
1959
2007
        for(a=0;a<totelem;a++) {
1985
2033
        size[2]= node->z_max - node->z_min;
1986
2034
 
1987
2035
        /* first node is subdivided recursively */
1988
 
        subdivide_metaball_octal_node(node, size, metaball_tree->depth);
 
2036
        subdivide_metaball_octal_node(node, size[0], size[1], size[2], metaball_tree->depth);
1989
2037
}
1990
2038
 
1991
2039
void metaball_polygonize(Object *ob)
2016
2064
        if(metaball_tree){
2017
2065
                free_metaball_octal_node(metaball_tree->first);
2018
2066
                MEM_freeN(metaball_tree);
 
2067
                metaball_tree= NULL;
2019
2068
        }
2020
2069
 
2021
2070
        /* if scene includes more then one MetaElem, then octal tree optimalisation is used */