Revision: 55309
          http://sourceforge.net/p/brlcad/code/55309
Author:   d_rossberg
Date:     2013-05-06 14:03:59 +0000 (Mon, 06 May 2013)
Log Message:
-----------
refactoring of the voxelize functions
- fixed memory leaks
- filter air voxels
- catched a file error

Modified Paths:
--------------
    brlcad/trunk/include/analyze.h
    brlcad/trunk/src/conv/g-voxel.c
    brlcad/trunk/src/libanalyze/voxels.c
    brlcad/trunk/src/libged/voxelize.c

Modified: brlcad/trunk/include/analyze.h
===================================================================
--- brlcad/trunk/include/analyze.h      2013-05-06 12:19:18 UTC (rev 55308)
+++ brlcad/trunk/include/analyze.h      2013-05-06 14:03:59 UTC (rev 55309)
@@ -84,7 +84,7 @@
  */
 
 struct voxelRegion {
-    const char *regionName;
+    char *regionName;
     fastf_t regionDistance;
     struct voxelRegion *nextRegion;
 };

Modified: brlcad/trunk/src/conv/g-voxel.c
===================================================================
--- brlcad/trunk/src/conv/g-voxel.c     2013-05-06 12:19:18 UTC (rev 55308)
+++ brlcad/trunk/src/conv/g-voxel.c     2013-05-06 14:03:59 UTC (rev 55309)
@@ -27,9 +27,15 @@
     fastf_t *threshold = (fastf_t *)callBackData;
     FILE *fp;
 
-    fp = fopen("voxels1.txt","a");
-    fprintf(fp, "%f\t(%4d,%4d,%4d)\t%s\t%f\n", *threshold, x, y, z, a, fill);
-    fclose(fp);
+    if (a != NULL) {
+       fp = fopen("voxels1.txt","a");
+
+       if (fp != NULL) {
+           fprintf(fp, "%f\t(%4d,%4d,%4d)\t%s\t%f\n", *threshold, x, y, z, a, 
fill);
+           fclose(fp);
+       }
+    }
+    /* else this voxel is air */
 }
 
 

Modified: brlcad/trunk/src/libanalyze/voxels.c
===================================================================
--- brlcad/trunk/src/libanalyze/voxels.c        2013-05-06 12:19:18 UTC (rev 
55308)
+++ brlcad/trunk/src/libanalyze/voxels.c        2013-05-06 14:03:59 UTC (rev 
55309)
@@ -41,13 +41,13 @@
 
     BU_ASSERT(regionName != NULL);
 
-    if(head->regionName == NULL) { /* the first region on this voxel */
+    if (head->regionName == NULL) { /* the first region on this voxel */
        head->regionName = bu_strdup(regionName);
        ret = head;
     }
     else {
-       while(head->nextRegion != NULL) {
-           if(bu_strcmp(head->regionName, regionName) == 0){
+       while (head->nextRegion != NULL) {
+           if (bu_strcmp(head->regionName, regionName) == 0) {
                ret = head;
                break;
            }
@@ -56,7 +56,7 @@
        }
 
        if (ret == NULL) { /* not found until here */
-           if(bu_strcmp(head->regionName ,regionName) == 0) /* is it the last 
one on the list? */
+           if (bu_strcmp(head->regionName ,regionName) == 0) /* is it the last 
one on the list? */
                ret = head;
            else {
                BU_ALLOC(ret, struct voxelRegion);
@@ -83,7 +83,7 @@
  * The 'segs' segment list is unused in this example.
  */
 int
-hit_voxelize(struct application *ap, struct partition *PartHeadp, struct 
seg*UNUSED(segs))
+hit_voxelize(struct application *ap, struct partition *PartHeadp, struct seg 
*UNUSED(segs))
 {
     struct partition *pp            = PartHeadp->pt_forw;
     struct rayInfo   *voxelHits     = (struct rayInfo*) ap->a_uptr;
@@ -101,8 +101,8 @@
 
        struct hit *hitInp      = pp->pt_inhit;
        struct hit *hitOutp     = pp->pt_outhit;
-       fastf_t     hitDistIn   = hitInp->hit_dist - 1.0;
-       fastf_t     hitDistOut  = hitOutp->hit_dist - 1.0;
+       fastf_t     hitDistIn   = hitInp->hit_dist - 1.;
+       fastf_t     hitDistOut  = hitOutp->hit_dist - 1.;
        int         voxelNumIn  = (int)(hitDistIn / sizeVoxel);
        int         voxelNumOut = (int)(hitDistOut / sizeVoxel);
 
@@ -145,117 +145,119 @@
  * voxelize function takes raytrace instance and user parameters as inputs
  */
 void
-voxelize(struct rt_i *rtip, fastf_t sizeVoxel[3], int levelOfDetail, void 
(*create_boxes)(genptr_t callBackData, int x, int y, int z, const char 
*regionName, fastf_t percentageFill), genptr_t callBackData){
-
-    struct application ap;
+voxelize(struct rt_i *rtip, fastf_t sizeVoxel[3], int levelOfDetail, void 
(*create_boxes)(genptr_t callBackData, int x, int y, int z, const char 
*regionName, fastf_t percentageFill), genptr_t callBackData)
+{
     struct rayInfo voxelHits;
-    struct voxelRegion *tmp, *old;
+    int            numVoxel[3];
+    int            yMin;
+    int            zMin;
+    fastf_t        *voxelArray;
+    fastf_t        rayTraceDistance;
+    fastf_t        effectiveDistance;
 
-    int i, j, k, numVoxel[3], yMin, zMin, rayNum;
-    fastf_t *voxelArray, rayTraceDistance, effectiveDistance;
-
     /* get bounding box values etc. */
     rt_prep_parallel(rtip, 1);
 
-
     /* calculate number of voxels in each dimension */
     numVoxel[0] = (int)(((rtip->mdl_max)[0] - 
(rtip->mdl_min)[0])/sizeVoxel[0]) + 1;
     numVoxel[1] = (int)(((rtip->mdl_max)[1] - 
(rtip->mdl_min)[1])/sizeVoxel[1]) + 1;
     numVoxel[2] = (int)(((rtip->mdl_max)[2] - 
(rtip->mdl_min)[2])/sizeVoxel[2]) + 1;
 
-    if(EQUAL(numVoxel[0] - 1, (((rtip->mdl_max)[0] - 
(rtip->mdl_min)[0])/sizeVoxel[0]))) {
+    if (EQUAL(numVoxel[0] - 1, (((rtip->mdl_max)[0] - 
(rtip->mdl_min)[0])/sizeVoxel[0])))
        numVoxel[0] -=1;
-    }
-    if(EQUAL(numVoxel[1] - 1, (((rtip->mdl_max)[1] - 
(rtip->mdl_min)[1])/sizeVoxel[1]))) {
+
+    if (EQUAL(numVoxel[1] - 1, (((rtip->mdl_max)[1] - 
(rtip->mdl_min)[1])/sizeVoxel[1])))
        numVoxel[1] -=1;
-    }
-    if(EQUAL(numVoxel[2] - 1, (((rtip->mdl_max)[2] - 
(rtip->mdl_min)[2])/sizeVoxel[2]))) {
+
+    if (EQUAL(numVoxel[2] - 1, (((rtip->mdl_max)[2] - 
(rtip->mdl_min)[2])/sizeVoxel[2])))
        numVoxel[2] -=1;
-    }
 
     voxelHits.sizeVoxel = sizeVoxel[0];
 
-    /* voxelArray stores the distance in path of ray inside a voxel which is 
filled*/
-    voxelArray = bu_calloc(numVoxel[0], sizeof(fastf_t), "voxelArray");
+    /* voxelArray stores the distance in path of ray inside a voxel which is 
filled
+     * initialize with 0s */
+    voxelArray = bu_calloc(numVoxel[0], sizeof(fastf_t), 
"voxelize:voxelArray");
 
-    /* regionList holds the names of voxels inside the voxels*/
-    voxelHits.regionList = bu_calloc(numVoxel[0], sizeof(struct voxelRegion), 
"regionList");
+    /* regionList holds the names of voxels inside the voxels
+     * initialize with NULLs */
+    voxelHits.regionList = bu_calloc(numVoxel[0], sizeof(struct voxelRegion), 
"voxelize:regionList");
 
-    /* initialize values of region, fillDistances etc. */
-    for(k = 0; k < numVoxel[0]; k++) {
-       voxelArray[k] = 0.0;
-       voxelHits.regionList[k].regionName = NULL;
-       voxelHits.regionList[k].nextRegion = NULL;
-    }
-
     /* minimum value of bounding box in Y and Z directions */
     yMin = (int)((rtip->mdl_min)[1]);
     zMin = (int)((rtip->mdl_min)[2]);
 
+    BU_ASSERT_LONG(levelOfDetail, >, 0);
     /* 1.0 / (levelOfDetail + 1) and effectiveDistance have to be used 
multiple times in the following loops */
-    rayTraceDistance = 1.0 / (levelOfDetail + 1);
+    rayTraceDistance  = 1. / levelOfDetail;
     effectiveDistance = levelOfDetail * levelOfDetail * sizeVoxel[0];
 
     /* start shooting */
-    for (i = 0; i < numVoxel[2]; i++) {
-       for (j = 0; j < numVoxel[1]; j++) {
+    for (int i = 0; i < numVoxel[2]; ++i) {
+       for (int j = 0; j < numVoxel[1]; ++j) {
+           struct application ap;
 
            RT_APPLICATION_INIT(&ap);
-           ap.a_rt_i = rtip;
+           ap.a_rt_i   = rtip;
            ap.a_onehit = 0;
-           VSET(ap.a_ray.r_dir, 1.0, 0.0, 0.0);
+           VSET(ap.a_ray.r_dir, 1., 0., 0.);
 
-           ap.a_hit = hit_voxelize;
+           ap.a_hit  = hit_voxelize;
            ap.a_miss = NULL;
            ap.a_uptr = &voxelHits;
 
            voxelHits.fillDistances = voxelArray;
 
-           for (rayNum = 1; rayNum <= levelOfDetail; rayNum++) {
-               for (k = 1; k <= levelOfDetail; k++) {
+           for (int rayNum = 0; rayNum < levelOfDetail; ++rayNum) {
+               for (int k = 0; k < levelOfDetail; ++k) {
 
                    /* ray is hit through evenly spaced points of the unit 
sized voxels */
-                   VSET(ap.a_ray.r_pt, (rtip->mdl_min)[0] - 1.0, yMin + (j + k 
* rayTraceDistance) * sizeVoxel[1], zMin + (i + rayNum * rayTraceDistance) * 
sizeVoxel[2]);
+                   VSET(ap.a_ray.r_pt, (rtip->mdl_min)[0] - 1.,
+                                       yMin + (j + (k + 0.5) * 
rayTraceDistance) * sizeVoxel[1],
+                                       zMin + (i + (rayNum + 0.5) * 
rayTraceDistance) * sizeVoxel[2]);
                    rt_shootray(&ap);
                }
            }
 
-           /*print results into file name supplied by user*/
-           for (k = 0; k < numVoxel[0]; k++) {
+           /* output results via a call-back supplied by user*/
+           for (int k = 0; k < numVoxel[0]; ++k) {
+               if(voxelHits.regionList[k].regionName == NULL)
+                   /* an air voxel */
+                   create_boxes(callBackData, k, j, i, NULL, 0.);
+               else {
+                   struct voxelRegion *tmp = voxelHits.regionList + k;
+                   struct voxelRegion *old = tmp->nextRegion;
 
-                   if(voxelHits.regionList[k].regionName==NULL){
+                   create_boxes(callBackData, k, j, i, tmp->regionName, 
tmp->regionDistance / effectiveDistance);
 
-                       create_boxes(callBackData, k, j, i, "air", 0.0);
+                   if (tmp->regionName != 0)
+                       bu_free(tmp->regionName, 
"voxelize:voxelRegion:regionName");
 
-                   } else {
-
-                       tmp = voxelHits.regionList + k;
+                   while (old != NULL) {
+                       tmp = old;
                        create_boxes(callBackData, k, j, i, tmp->regionName, 
tmp->regionDistance / effectiveDistance);
-
                        old = tmp->nextRegion;
-
-                       while(old != NULL) {
-                           tmp = old;
-                           create_boxes(callBackData, k, j, i, 
tmp->regionName, tmp->regionDistance / effectiveDistance);
-                           old = tmp->nextRegion;
-                           /* free the space allocated for new regions */
-                           bu_free(tmp, "");
-                        }
-
+                       
+                       /* free the space allocated for new regions */
+                       if (tmp->regionName != 0)
+                           bu_free(tmp->regionName, 
"voxelize:voxelRegion:regionName");
+                       
+                       BU_FREE(tmp, struct voxelRegion);
                    }
+               }
 
-               voxelArray[k] = 0.0;
-               voxelHits.regionList[k].regionName = NULL;
-               voxelHits.regionList[k].nextRegion = NULL;
-               voxelHits.regionList[k].regionDistance = 0.0;
-               }
+               voxelArray[k] = 0.;
+               voxelHits.regionList[k].regionName     = NULL;
+               voxelHits.regionList[k].nextRegion     = NULL;
+               voxelHits.regionList[k].regionDistance = 0.;
            }
        }
+    }
 
-    bu_free(voxelArray, "");
-    bu_free(voxelHits.regionList, "");
+    bu_free(voxelArray, "voxelize:voxelArray");
+    bu_free(voxelHits.regionList, "voxelize:regionList");
 }
 
+
 /*
  * Local Variables:
  * tab-width: 8

Modified: brlcad/trunk/src/libged/voxelize.c
===================================================================
--- brlcad/trunk/src/libged/voxelize.c  2013-05-06 12:19:18 UTC (rev 55308)
+++ brlcad/trunk/src/libged/voxelize.c  2013-05-06 14:03:59 UTC (rev 55309)
@@ -48,41 +48,45 @@
 };
 
 HIDDEN void
-create_boxes(genptr_t callBackData, int x, int y, int z, const char 
*UNUSED(a), fastf_t fill)
+create_boxes(genptr_t callBackData, int x, int y, int z, const char *a, 
fastf_t fill)
 {
-    fastf_t min[3], max[3];
+    if (a != NULL) {
+       fastf_t min[3], max[3];
 
-    struct bu_vls *vp;
-    char bufx[50], bufy[50], bufz[50];
-    char *nameDestination;
+       struct bu_vls *vp;
+       char bufx[50], bufy[50], bufz[50];
+       char *nameDestination;
 
-    struct voxelizeData *dataValues = (struct voxelizeData *)callBackData;
+       struct voxelizeData *dataValues = (struct voxelizeData *)callBackData;
 
-    sprintf(bufx, "%d", x);
-    sprintf(bufy, "%d", y);
-    sprintf(bufz, "%d", z);
-    if(dataValues->threshold <= fill) {
-       vp = bu_vls_vlsinit();
-       bu_vls_strcat(vp, dataValues->newname);
-       bu_vls_strcat(vp, ".x");
-       bu_vls_strcat(vp, bufx);
-       bu_vls_strcat(vp, "y");
-       bu_vls_strcat(vp, bufy);
-       bu_vls_strcat(vp, "z");
-       bu_vls_strcat(vp, bufz);
-       bu_vls_strcat(vp, ".s");
+       sprintf(bufx, "%d", x);
+       sprintf(bufy, "%d", y);
+       sprintf(bufz, "%d", z);
 
-       min[0] = (dataValues->bbMin)[0] + (x * (dataValues->sizeVoxel)[0]);
-       min[1] = (dataValues->bbMin)[1] + (y * (dataValues->sizeVoxel)[1]);
-       min[2] = (dataValues->bbMin)[2] + (z * (dataValues->sizeVoxel)[2]);
-       max[0] = (dataValues->bbMin)[0] + ( (x + 1.0) * 
(dataValues->sizeVoxel)[0]);
-       max[1] = (dataValues->bbMin)[1] + ( (y + 1.0) * 
(dataValues->sizeVoxel)[1]);
-       max[2] = (dataValues->bbMin)[2] + ( (z + 1.0) * 
(dataValues->sizeVoxel)[2]);
+       if (dataValues->threshold <= fill) {
+           vp = bu_vls_vlsinit();
+           bu_vls_strcat(vp, dataValues->newname);
+           bu_vls_strcat(vp, ".x");
+           bu_vls_strcat(vp, bufx);
+           bu_vls_strcat(vp, "y");
+           bu_vls_strcat(vp, bufy);
+           bu_vls_strcat(vp, "z");
+           bu_vls_strcat(vp, bufz);
+           bu_vls_strcat(vp, ".s");
 
-       nameDestination = bu_vls_strgrab(vp);
-       mk_rpp(dataValues->wdbp,nameDestination, min, max);
-       mk_addmember(nameDestination, &dataValues->content.l, 0, WMOP_UNION);
+           min[0] = (dataValues->bbMin)[0] + (x * (dataValues->sizeVoxel)[0]);
+           min[1] = (dataValues->bbMin)[1] + (y * (dataValues->sizeVoxel)[1]);
+           min[2] = (dataValues->bbMin)[2] + (z * (dataValues->sizeVoxel)[2]);
+           max[0] = (dataValues->bbMin)[0] + ( (x + 1.0) * 
(dataValues->sizeVoxel)[0]);
+           max[1] = (dataValues->bbMin)[1] + ( (y + 1.0) * 
(dataValues->sizeVoxel)[1]);
+           max[2] = (dataValues->bbMin)[2] + ( (z + 1.0) * 
(dataValues->sizeVoxel)[2]);
+
+           nameDestination = bu_vls_strgrab(vp);
+           mk_rpp(dataValues->wdbp,nameDestination, min, max);
+           mk_addmember(nameDestination, &dataValues->content.l, 0, 
WMOP_UNION);
+       }
     }
+    /* else this voxel is air */
 }
 
 int

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
Introducing AppDynamics Lite, a free troubleshooting tool for Java/.NET
Get 100% visibility into your production application - at no cost.
Code-level diagnostics for performance bottlenecks with <2% overhead
Download for free and get started troubleshooting in minutes.
http://p.sf.net/sfu/appdyn_d2d_ap1
_______________________________________________
BRL-CAD Source Commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/brlcad-commits

Reply via email to