+/* fix the cw list and append to ccw_list */
+void fix_cw_list(int *list, int list_ptr) {
+ int i, j, len;
+
+ if ( list_ptr < 3 ) {
+ printf("List is empty ... skipping\n");
+ return;
+ }
+
+ printf("Fixing cw list, size = %d\n", list_ptr);
+
+ i = 0;
+ while ( i < list_ptr ) {
+ /* do next strip */
+
+ /* find length */
+ len = 0;
+ /* scan rest of strip (until -1) */
+ while ( ((i+len) < list_ptr) && (list[i+len] != -1) ) {
+ // printf("len = %d item = %d\n", len, list[i+len] );
+ len++;
+ }
+ // printf(" Final length = %d\n", len);
+
+ if ( (len % 2) != 0 ) {
+ /* if length is odd, just reverse order of nodes to reverse
+ winding */
+ if ( ccw_list_ptr ) {
+ list_add(ccw_list, &ccw_list_ptr, -1);
+ }
+ for ( j = i + len - 1; j >= i; j-- ) {
+ // printf(" odd -> item = %d\n", list[j] );
+ list_add(ccw_list, &ccw_list_ptr, list[j]);
+ }
+ } else {
+ /* if length is even, reverse order of (n-1) nodes to
+ reverse winding, and create an orphan triangle for the
+ last "nth" node */
+ if ( ccw_list_ptr ) {
+ list_add(ccw_list, &ccw_list_ptr, -1);
+ }
+ for ( j = i + len - 2; j >= i; j-- ) {
+ // printf(" even -> item = %d\n", list[j] );
+ list_add(ccw_list, &ccw_list_ptr, list[j]);
+ }
+
+ // printf(" even bonus -> item = %d\n", list[i + len - 1] );
+ // printf(" even bonus -> item = %d\n", list[i + len - 2] );
+ // printf(" even bonus -> item = %d\n", list[i + len - 3] );
+ list_add(ccw_list, &ccw_list_ptr, -1);
+ list_add(ccw_list, &ccw_list_ptr, list[i + len - 3]);
+ list_add(ccw_list, &ccw_list_ptr, list[i + len - 2]);
+ list_add(ccw_list, &ccw_list_ptr, list[i + len - 1]);
+ }
+
+ i += len + 1;
+ }
+}
+
+
+// Calculate distance between (0,0,0) and the specified point
+static double calc_dist(double x, double y, double z) {
+ return ( sqrt(x*x + y*y + z*z) );
+}
+
+
+void dump_global_bounds( void ) {
+ double dist, radius;
+ int i;
+
+ radius = 0.0;
+
+ fprintf(out, "\n");
+
+ for ( i = 1; i < ncount; i++ ) {
+
+ dist = calc_dist(nodes[i][0] - refx, nodes[i][1] - refy,
+ nodes[i][2] - refz);
+ // printf("node = %.2f %.2f %.2f dist = %.2f\n",
+ // nodes[i][0], nodes[i][1], nodes[i][2],
+ // dist);
+
+ if ( dist > radius ) {
+ radius = dist;
+ }
+
+ }
+
+ fprintf(out, "gb %.4f %.4f %.4f %.2f\n", refx, refy, refz, radius);
+}
+
+