style cleanup
[blender.git] / source / blender / editors / space_info / info_stats.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Blender Foundation
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/editors/space_info/info_stats.c
24  *  \ingroup spinfo
25  */
26
27
28 #include <stdio.h>
29 #include <string.h>
30
31 #include "MEM_guardedalloc.h"
32
33 #include "DNA_armature_types.h"
34 #include "DNA_curve_types.h"
35 #include "DNA_group_types.h"
36 #include "DNA_lattice_types.h"
37 #include "DNA_mesh_types.h"
38 #include "DNA_meta_types.h"
39 #include "DNA_scene_types.h"
40
41 #include "BLI_utildefines.h"
42
43 #include "BKE_anim.h"
44 #include "BKE_blender.h"
45 #include "BKE_curve.h"
46 #include "BKE_displist.h"
47 #include "BKE_DerivedMesh.h"
48 #include "BKE_key.h"
49 #include "BKE_mesh.h"
50 #include "BKE_particle.h"
51 #include "BKE_tessmesh.h"
52
53 #include "ED_info.h"
54 #include "ED_armature.h"
55 #include "ED_mesh.h"
56
57
58 typedef struct SceneStats {
59         int totvert, totvertsel;
60         int totedge, totedgesel;
61         int totface, totfacesel;
62         int totbone, totbonesel;
63         int totobj, totobjsel;
64         int totmesh, totlamp, totcurve;
65
66         char infostr[512];
67 } SceneStats;
68
69 static void stats_object(Object *ob, int sel, int totob, SceneStats *stats)
70 {
71         switch(ob->type) {
72         case OB_MESH: {
73                 /* we assume derivedmesh is already built, this strictly does stats now. */
74                 DerivedMesh *dm= ob->derivedFinal;
75                 int totvert, totedge, totface;
76
77                 stats->totmesh +=totob;
78
79                 if(dm) {
80                         totvert = dm->getNumVerts(dm);
81                         totedge = dm->getNumEdges(dm);
82                         totface = dm->getNumPolys(dm);
83
84                         stats->totvert += totvert*totob;
85                         stats->totedge += totedge*totob;
86                         stats->totface += totface*totob;
87
88                         if(sel) {
89                                 stats->totvertsel += totvert;
90                                 stats->totfacesel += totface;
91                         }
92                 }
93                 break;
94         }
95         case OB_LAMP:
96                 stats->totlamp += totob;
97                 break;
98         case OB_SURF:
99         case OB_CURVE:
100         case OB_FONT: {
101                 int tot= 0, totf= 0;
102
103                 stats->totcurve += totob;
104
105                 if(ob->disp.first)
106                         count_displist(&ob->disp, &tot, &totf);
107
108                 tot *= totob;
109                 totf *= totob;
110
111                 stats->totvert+= tot;
112                 stats->totface+= totf;
113
114                 if(sel) {
115                         stats->totvertsel += tot;
116                         stats->totfacesel += totf;
117                 }
118                 break;
119         }
120         case OB_MBALL: {
121                 int tot= 0, totf= 0;
122
123                 count_displist(&ob->disp, &tot, &totf);
124
125                 tot *= totob;
126                 totf *= totob;
127
128                 stats->totvert += tot;
129                 stats->totface += totf;
130
131                 if(sel) {
132                         stats->totvertsel += tot;
133                         stats->totfacesel += totf;
134                 }
135                 break;
136         }
137         }
138 }
139
140 static void stats_object_edit(Object *obedit, SceneStats *stats)
141 {
142         if(obedit->type==OB_MESH) {
143                 BMEditMesh *em = BMEdit_FromObject(obedit);
144
145                 stats->totvert = em->bm->totvert;
146                 stats->totvertsel = em->bm->totvertsel;
147                 
148                 stats->totedge = em->bm->totedge;
149                 stats->totedgesel = em->bm->totedgesel;
150                 
151                 stats->totface = em->bm->totface;
152                 stats->totfacesel = em->bm->totfacesel;
153         }
154         else if(obedit->type==OB_ARMATURE){
155                 /* Armature Edit */
156                 bArmature *arm= obedit->data;
157                 EditBone *ebo;
158
159                 for(ebo=arm->edbo->first; ebo; ebo=ebo->next){
160                         stats->totbone++;
161                         
162                         if((ebo->flag & BONE_CONNECTED) && ebo->parent)
163                                 stats->totvert--;
164                         
165                         if(ebo->flag & BONE_TIPSEL)
166                                 stats->totvertsel++;
167                         if(ebo->flag & BONE_ROOTSEL)
168                                 stats->totvertsel++;
169                         
170                         if(ebo->flag & BONE_SELECTED) stats->totbonesel++;
171
172                         /* if this is a connected child and it's parent is being moved, remove our root */
173                         if((ebo->flag & BONE_CONNECTED)&& (ebo->flag & BONE_ROOTSEL) && ebo->parent && (ebo->parent->flag & BONE_TIPSEL))
174                                 stats->totvertsel--;
175
176                         stats->totvert+=2;
177                 }
178         }
179         else if ELEM(obedit->type, OB_CURVE, OB_SURF) { /* OB_FONT has no cu->editnurb */
180                 /* Curve Edit */
181                 Curve *cu= obedit->data;
182                 Nurb *nu;
183                 BezTriple *bezt;
184                 BPoint *bp;
185                 int a;
186                 ListBase *nurbs= curve_editnurbs(cu);
187
188                 for(nu=nurbs->first; nu; nu=nu->next) {
189                         if(nu->type == CU_BEZIER) {
190                                 bezt= nu->bezt;
191                                 a= nu->pntsu;
192                                 while(a--) {
193                                         stats->totvert+=3;
194                                         if(bezt->f1) stats->totvertsel++;
195                                         if(bezt->f2) stats->totvertsel++;
196                                         if(bezt->f3) stats->totvertsel++;
197                                         bezt++;
198                                 }
199                         }
200                         else {
201                                 bp= nu->bp;
202                                 a= nu->pntsu*nu->pntsv;
203                                 while(a--) {
204                                         stats->totvert++;
205                                         if(bp->f1 & SELECT) stats->totvertsel++;
206                                         bp++;
207                                 }
208                         }
209                 }
210         }
211         else if(obedit->type==OB_MBALL) {
212                 /* MetaBall Edit */
213                 MetaBall *mball= obedit->data;
214                 MetaElem *ml;
215                 
216                 for(ml= mball->editelems->first; ml; ml=ml->next) {
217                         stats->totvert++;
218                         if(ml->flag & SELECT) stats->totvertsel++;
219                 }
220         }
221         else if(obedit->type==OB_LATTICE) {
222                 /* Lattice Edit */
223                 Lattice *lt= obedit->data;
224                 Lattice *editlatt= lt->editlatt->latt;
225                 BPoint *bp;
226                 int a;
227
228                 bp= editlatt->def;
229                 
230                 a= editlatt->pntsu*editlatt->pntsv*editlatt->pntsw;
231                 while(a--) {
232                         stats->totvert++;
233                         if(bp->f1 & SELECT) stats->totvertsel++;
234                         bp++;
235                 }
236         }
237 }
238
239 static void stats_object_pose(Object *ob, SceneStats *stats)
240 {
241         if(ob->pose) {
242                 bArmature *arm= ob->data;
243                 bPoseChannel *pchan;
244
245                 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
246                         stats->totbone++;
247                         if(pchan->bone && (pchan->bone->flag & BONE_SELECTED))
248                                 if(pchan->bone->layer & arm->layer)
249                                         stats->totbonesel++;
250                 }
251         }
252 }
253
254 static void stats_object_paint(Object *ob, SceneStats *stats)
255 {
256         if(ob->type == OB_MESH) {
257                 Mesh *me= ob->data;
258
259                 stats->totface= me->totface;
260                 stats->totvert= me->totvert;
261         }
262 }
263
264 static void stats_dupli_object(Base *base, Object *ob, SceneStats *stats)
265 {
266         if(base->flag & SELECT) stats->totobjsel++;
267
268         if(ob->transflag & OB_DUPLIPARTS) {
269                 /* Dupli Particles */
270                 ParticleSystem *psys;
271                 ParticleSettings *part;
272
273                 for(psys=ob->particlesystem.first; psys; psys=psys->next){
274                         part=psys->part;
275
276                         if(part->draw_as==PART_DRAW_OB && part->dup_ob){
277                                 int tot=count_particles(psys);
278                                 stats_object(part->dup_ob, 0, tot, stats);
279                         }
280                         else if(part->draw_as==PART_DRAW_GR && part->dup_group){
281                                 GroupObject *go;
282                                 int tot, totgroup=0, cur=0;
283                                 
284                                 for(go= part->dup_group->gobject.first; go; go=go->next)
285                                         totgroup++;
286
287                                 for(go= part->dup_group->gobject.first; go; go=go->next) {
288                                         tot=count_particles_mod(psys,totgroup,cur);
289                                         stats_object(go->ob, 0, tot, stats);
290                                         cur++;
291                                 }
292                         }
293                 }
294                 
295                 stats_object(ob, base->flag & SELECT, 1, stats);
296                 stats->totobj++;
297         }
298         else if(ob->parent && (ob->parent->transflag & (OB_DUPLIVERTS|OB_DUPLIFACES))) {
299                 /* Dupli Verts/Faces */
300                 int tot= count_duplilist(ob->parent);
301                 stats->totobj+=tot;
302                 stats_object(ob, base->flag & SELECT, tot, stats);
303         }
304         else if(ob->transflag & OB_DUPLIFRAMES) {
305                 /* Dupli Frames */
306                 int tot= count_duplilist(ob);
307                 stats->totobj+=tot;
308                 stats_object(ob, base->flag & SELECT, tot, stats);
309         }
310         else if((ob->transflag & OB_DUPLIGROUP) && ob->dup_group) {
311                 /* Dupli Group */
312                 int tot= count_duplilist(ob);
313                 stats->totobj+=tot;
314                 stats_object(ob, base->flag & SELECT, tot, stats);
315         }
316         else {
317                 /* No Dupli */
318                 stats_object(ob, base->flag & SELECT, 1, stats);
319                 stats->totobj++;
320         }
321 }
322
323 /* Statistics displayed in info header. Called regularly on scene changes. */
324 static void stats_update(Scene *scene)
325 {
326         SceneStats stats= {0};
327         Object *ob= (scene->basact)? scene->basact->object: NULL;
328         Base *base;
329         
330         if(scene->obedit) {
331                 /* Edit Mode */
332                 stats_object_edit(scene->obedit, &stats);
333         }
334         else if(ob && (ob->mode & OB_MODE_POSE)) {
335                 /* Pose Mode */
336                 stats_object_pose(ob, &stats);
337         }
338         else if(ob && (ob->flag & OB_MODE_ALL_PAINT)) {
339                 /* Sculpt and Paint Mode */
340                 stats_object_paint(ob, &stats);
341         }
342         else {
343                 /* Objects */
344                 for(base= scene->base.first; base; base=base->next)
345                         if(scene->lay & base->lay)
346                                 stats_dupli_object(base, base->object, &stats);
347         }
348
349         if(!scene->stats)
350                 scene->stats= MEM_callocN(sizeof(SceneStats), "SceneStats");
351
352         *(scene->stats)= stats;
353 }
354
355 static void stats_string(Scene *scene)
356 {
357         SceneStats *stats= scene->stats;
358         Object *ob= (scene->basact)? scene->basact->object: NULL;
359         uintptr_t mem_in_use, mmap_in_use;
360         char memstr[64];
361         char *s;
362
363         mem_in_use= MEM_get_memory_in_use();
364         mmap_in_use= MEM_get_mapped_memory_in_use();
365
366         /* get memory statistics */
367         s= memstr + sprintf(memstr, " | Mem:%.2fM", (double)((mem_in_use-mmap_in_use)>>10)/1024.0);
368         if(mmap_in_use)
369                 sprintf(s, " (%.2fM)", (double)((mmap_in_use)>>10)/1024.0);
370
371         s= stats->infostr;
372         
373         s+= sprintf(s, "%s | ", versionstr);
374
375         if(scene->obedit) {
376                 if(ob_get_keyblock(scene->obedit))
377                         s+= sprintf(s, "(Key) ");
378
379                 if(scene->obedit->type==OB_MESH) {
380                         if(scene->toolsettings->selectmode & SCE_SELECT_VERTEX)
381                                 s+= sprintf(s, "Ve:%d-%d | Ed:%d-%d | Fa:%d-%d",
382                                                 stats->totvertsel, stats->totvert, stats->totedgesel, stats->totedge, stats->totfacesel, stats->totface);
383                         else if(scene->toolsettings->selectmode & SCE_SELECT_EDGE)
384                                 s+= sprintf(s, "Ed:%d-%d | Fa:%d-%d",
385                                                 stats->totedgesel, stats->totedge, stats->totfacesel, stats->totface);
386                         else
387                                 s+= sprintf(s, "Fa:%d-%d", stats->totfacesel, stats->totface);
388                 }
389                 else if(scene->obedit->type==OB_ARMATURE) {
390                         s+= sprintf(s, "Ve:%d-%d | Bo:%d-%d", stats->totvertsel, stats->totvert, stats->totbonesel, stats->totbone);
391                 }
392                 else {
393                         s+= sprintf(s, "Ve:%d-%d", stats->totvertsel, stats->totvert);
394                 }
395
396                 strcat(s, memstr);
397         }
398         else if(ob && (ob->mode & OB_MODE_POSE)) {
399                 s += sprintf(s, "Bo:%d-%d %s",
400                                         stats->totbonesel, stats->totbone, memstr);
401         }
402         else {
403                 s += sprintf(s, "Ve:%d | Fa:%d | Ob:%d-%d | La:%d%s",
404                         stats->totvert, stats->totface, stats->totobjsel, stats->totobj, stats->totlamp, memstr);
405         }
406
407         if(ob)
408                 sprintf(s, " | %s", ob->id.name+2);
409 }
410
411 void ED_info_stats_clear(Scene *scene)
412 {
413         if(scene->stats) {
414                 MEM_freeN(scene->stats);
415                 scene->stats= NULL;
416         }
417 }
418
419 const char *ED_info_stats_string(Scene *scene)
420 {
421         if(!scene->stats)
422                 stats_update(scene);
423         stats_string(scene);
424
425         return scene->stats->infostr;
426 }
427