1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
  
     | 
    
      #include "Vlib.h"
#include <math.h>
void
VComputeObjectExtent(VObject * obj)
{
	VPoint    sum;
	register int i, j, npts = 0;
	register double d;
	obj->extent = 0.0;
	sum.x = 0.0;
	sum.y = 0.0;
	sum.z = 0.0;
/*
 *  Add the xyz components of each point in the object so that we can
 *  determine the average location (i.e. the center).
 */
	for (i = 0; i < obj->numPolys; ++i) {
		for (j = 0; j < obj->polygon[i]->numVtces; ++j) {
			sum.x += obj->polygon[i]->vertex[j].x;
			sum.y += obj->polygon[i]->vertex[j].y;
			sum.z += obj->polygon[i]->vertex[j].z;
			++npts;
		}
	}
	if (npts != 0) {
		obj->center.x = sum.x / npts;
		obj->center.y = sum.y / npts;
		obj->center.z = sum.z / npts;
/*
 *   Determine the most distant point from the center of the object
 */
		for (i = 0; i < obj->numPolys; ++i) {
			for (j = 0; j < obj->polygon[i]->numVtces; ++j) {
				sum.x = obj->polygon[i]->vertex[j].x -
					obj->center.x;
				sum.y = obj->polygon[i]->vertex[j].y -
					obj->center.y;
				sum.z = obj->polygon[i]->vertex[j].z -
					obj->center.z;
				d = sqrt(sum.x * sum.x + sum.y * sum.y +
						 sum.z * sum.z);
				if (d > obj->extent)
					obj->extent = d;
			}
		}
	}
	else {
		obj->center.x = obj->center.y = obj->center.z = 0.0;
	}
}
 
     |