/* procedure tries all combinarion of leaving out i of the 3D points (because not enough markers where specified) plus the points leaveout1 and leavout2 (if they are passed as non-negative) */ char *recursive_loop (Desktop * d, char omit[], int from, int to, int i, double tol, \ int leaveout1, int leaveout2, int calc_distortion) { int j; double e; static double emin = 10e90; static char *omitmin = 0; if (!omit) { emin = 10e90; destroy ((void *) &omitmin); return 0; } if (i) { for (j = from; j <= to - i; j++) { if (stop) break; omit[j] = 1; recursive_loop (d, omit, j + 1, to, i - 1, tol, leaveout1, leaveout2, \ calc_distortion); omit[j] = 0; } } else { Vec *v; double *x, *y; int leave_out = (leaveout1 >= 0) + (leaveout2 >= 0); int k; int n; for (i = 0, j = 0; j < to; i += omit[j++]); n = to - i - leave_out; v = Cmalloc (sizeof (Vec) * n); x = Cmalloc (sizeof (double) * n); y = Cmalloc (sizeof (double) * n); i = 0; k = 0; for (j = 0; j < to; j++) { printf ("%d", (int) omit[j]); if (!omit[j]) { if (k != leaveout1 && k != leaveout2) { v[i] = d->cal_points[j]; x[i] = d->view[d->current_view].mark[k].x; y[i] = d->view[d->current_view].mark[k].y; i++; } k++; } } printf ("\n"); e = findcameraposition (x, y, v, n, \ &(d->view[d->current_view].cam), tol, 1, calc_distortion); if (e < emin) { emin = e; destroy ((void *) &omitmin); omitmin = Cmalloc (to); memcpy (omitmin, omit, to); } free (x); free (y); } return omitmin; }
void Cdraw3d_roundplate (const char *ident, double x, double y, double z, double a, double b, double c, double r) { TD_Point *p = Cmalloc ((GridDensity * 4 + 1) * 2 * sizeof (TD_Point)); double alpha = 0; Vec r1; Vec r2; Vec rv; Vec A; Vec X; int i = 0; A.x = a; A.y = b; A.z = c; X.x = x; X.y = y; X.z = z; orth_vectors (X, &r1, &r2, r); while (alpha < (2 * PI + 0.001)) { rv = plus (plus (times (r1, cos (alpha)), times (r2, sin (alpha))), A); assignTD (&p[i], rv); i++; assignTD (&p[i], A); i++; alpha += (2 * PI) / (GridDensity * 4); } Cinit_surf_points (ident, 2, GridDensity * 4 + 1, p); free (p); }
void Cdraw3d_cone (const char *ident, double x, double y, double z, double a, double b, double c, double ra, double rb) { int g = 4 * GridDensity / 3; TD_Point *p = Cmalloc ((g + 1) * 3 * sizeof (TD_Point)); Vec r1; Vec r2; Vec A, X; double f = rb / ra; A.x = a; A.y = b; A.z = c; X.x = x; X.y = y; X.z = z; orth_vectors (X, &r1, &r2, ra); third_cyl (0, p, A, X, r1, r2, g, f); Cinit_surf_points (ident, 3, g + 1, p); third_cyl (2 * PI / 3, p, A, X, r1, r2, g, f); Cinit_surf_points (ident, 3, g + 1, p); third_cyl (4 * PI / 3, p, A, X, r1, r2, g, f); Cinit_surf_points (ident, 3, g + 1, p); free (p); }
void draw3d_surface(const char *ident, int w, int h, Vec *v) { int i; TD_Point *p = Cmalloc (w * h * sizeof (TD_Point)); for(i=0; i<w*h; i++) assignTD(&p[i], v[i]); Cinit_surf_points (ident, w, h, p); free (p); }
int load_calibration_file (Desktop * d, const char *filename) { FILE *f; int n, i = 0; Vec *v; f = fopen (filename, "r"); if (!f) { Cerrordialog (0, 0, 0, " Load Calibration File ", \ get_sys_error (" Error trying to load file. ")); return 1; } v = Cmalloc (MAX_CAL_POINTS * sizeof (Vec)); while (!feof (f)) { n = fscanf (f, "%lf %lf %lf ", &v[i].x, &v[i].y, &v[i].z); if (n == 3) { i++; } else { Cerrordialog (0, 0, 0, " Load Calibration File ", \ " Format error in calibration file "); i = 0; break; } } fclose (f); if (i) { d->num_cal_points = i; destroy ((void *) &(d->cal_points)); d->cal_points = Cmalloc (i * sizeof (Vec)); memcpy (d->cal_points, v, i * sizeof (Vec)); destroy ((void *) &(d->cal_file)); d->cal_file = strdup (filename); } /* else { cal points not changed */ destroy ((void *) &v); /* fclose (f); TLD 2/6/08 */ return !i; }
static char *strread (int f) { int len; char *s; read (f, (char *) &len, sizeof (int)); if (len < 0) { return 0; } else { s = Cmalloc (len + 1); read (f, s, len); s[len] = 0; return s; } }
/* Takes a newline seperated list of files, returns a null seperated list of complete path names by prepending dnd_directory to each file name. returns 0 if no files in list. result must always be free'd. returns l as the total length of data. returns num_files as the number of files in the list. Alters t */ char *CDndFileList (char *t, int *l, int *num_files) { char *p, *q, *r, *result; int i; int path_len, len; /* strip leading newlines */ while (*t == '\n') t++; /* strip trailing newlines */ striptrailing (t, '\n'); if (!*t) return 0; /* count files */ for (i = 1, p = t; *p; p++) if (*p == '\n') i++; *num_files = i; len = (unsigned long) p - (unsigned long) t; path_len = strlen (dnd_directory); result = Cmalloc ((path_len + 2) * i + len + 2); r = result; p = t; for (;;) { q = strchr (p, '\n'); if (!q) q = t + len; *q = 0; strcpy (r, dnd_directory); r += path_len; *r++ = '/'; strcpy (r, p); r += (unsigned long) q - (unsigned long) p; *r++ = 0; if ((unsigned long) q == (unsigned long) t + len) break; p = ++q; } *r = 0; *l = (unsigned long) r - (unsigned long) result; return result; }
int do_load_desktop (Desktop * d, char *filename) { int f; if ((f = open (filename, O_RDONLY)) >= 0) { int i, x = 40, y = 40; char *sign; sign = strread (f); if (strcmp (sign, "stereo\n - saved desktop\n\n")) { close (f); Cerrordialogue (CMain, 20, 20, " Load Desktop ", \ " This is not a desktop file "); free (sign); return 1; } free (sign); for (i = 0; i < d->num_views; i++) destroy_view (&(d->view[i])); clear (d, Desktop); read (f, d, sizeof (Desktop)); destroy ((void *) &d->cal_points); d->cal_points = Cmalloc (d->num_cal_points * sizeof (Vec)); read (f, d->cal_points, d->num_cal_points * sizeof (Vec)); d->cal_file = strread (f); d->temp_dir = strread (f); d->image_dir = strread (f); if (d->num_views) for (i = 0; i < d->num_views; i++) { char *v; v = strread (f); if (v) { setup_view (d, v, x += 20, y += 20, i); d->view[i].filename = v; } } draw_markers (d); close (f); return 0; } else { Cerrordialogue (CMain, 20, 20, " Load Desktop ", \ get_sys_error (" Error trying to save file. ")); } return 1; }
void Cdraw3d_surface(const char *ident, int w, int h,...) { va_list pa; int i; TD_Point *p = Cmalloc (w * h * sizeof (TD_Point)); va_start(pa, h); for(i = 0; i < w * h; i++) { p[i].x = va_arg(pa, double); p[i].y = va_arg(pa, double); p[i].z = va_arg(pa, double); p[i].dirx = 0; p[i].diry = 0; p[i].dirz = 0; } va_end(pa); Cinit_surf_points (ident, w, h, p); free (p); }
void Cdraw3d_ellipsoid (const char *ident, double x, double y, double z, double a, double b, double c, double f) { int w = GridDensity / 2; int g = 2 * w + 1; TD_Point *p = Cmalloc (g * g * sizeof (TD_Point)); initellipsoidpart (p, x, y, z, a, b, c, w, 0, f); Cinit_surf_points (ident, g, g, p); initellipsoidpart (p, x, y, z, a, b, c, w, 1, f); Cinit_surf_points (ident, g, g, p); initellipsoidpart (p, x, y, z, a, b, c, w, 2, f); Cinit_surf_points (ident, g, g, p); initellipsoidpart (p, x, y, z, a, b, c, w, 3, f); Cinit_surf_points (ident, g, g, p); initellipsoidpart (p, x, y, z, a, b, c, w, 4, f); Cinit_surf_points (ident, g, g, p); initellipsoidpart (p, x, y, z, a, b, c, w, 5, f); Cinit_surf_points (ident, g, g, p); free (p); }
/* returns -1 on error, zero on success */ int Cdraw3d_from_text (const char *ident, const char *text) { char *p = (char *) text; int line = 1; double x, y, z, a, b, c, r, r2; Vec *v; int w, h, i, k; do { p += strspn(p, " \t\r"); if(!*p) break; if (*p == '#' || *p == '\n') { /* comment, do nothing */ ; } else if (!strncmp (p, "scale ", 6)) { if (sscanf (p, "scale %lf", &x) == 1) Cdraw3d_scale (ident, x); else { textformaterror (line, ident); return -1; } } else if (!strncmp (p, "offset ", 7)) { if (sscanf (p, "offset %lf %lf %lf", &x, &y, &z) == 3) Cdraw3d_offset (ident, x, y, z); else { textformaterror (line, ident); return -1; } } else if (!strncmp (p, "density ", 7)) { if (sscanf (p, "density %lf", &x) == 1) Cdraw3d_density (ident, x); else { textformaterror (line, ident); return -1; } } else if (!strncmp (p, "cylinder ", 8)) { if (sscanf (p, "cylinder %lf %lf %lf %lf %lf %lf %lf", &x, &y, &z, &a, &b, &c, &r) == 7) Cdraw3d_cylinder (ident, x, y, z, a, b, c, r); else { textformaterror (line, ident); return -1; } } else if (!strncmp (p, "roundplate ", 11)) { if (sscanf (p, "roundplate %lf %lf %lf %lf %lf %lf %lf", &x, &y, &z, &a, &b, &c, &r) == 7) { Cdraw3d_roundplate (ident, x, y, z, a, b, c, r); Cdraw3d_roundplate (ident, -x, -y, -z, a, b, c, r); } else { textformaterror (line, ident); return -1; } } else if (!strncmp (p, "cone ", 5)) { if (sscanf (p, "cone %lf %lf %lf %lf %lf %lf %lf %lf", &x, &y, &z, &a, &b, &c, &r, &r2) == 8) Cdraw3d_cone (ident, x, y, z, a, b, c, r, r2); else { textformaterror (line, ident); return -1; } } else if (!strncmp (p, "cappedcone ", 11)) { if (sscanf (p, "cappedcone %lf %lf %lf %lf %lf %lf %lf %lf", &x, &y, &z, &a, &b, &c, &r, &r2) == 8) Cdraw3d_cappedcone (ident, x, y, z, a, b, c, r, r2); else { textformaterror (line, ident); return -1; } } else if (!strncmp (p, "cappedcylinder ", 15)) { if (sscanf (p, "cappedcylinder %lf %lf %lf %lf %lf %lf %lf", &x, &y, &z, &a, &b, &c, &r) == 7) { Cdraw3d_cappedcylinder (ident, x, y, z, a, b, c, r); } else { textformaterror (line, ident); return -1; } } else if (!strncmp (p, "ellipsoid ", 10)) { if (sscanf (p, "ellipsoid %lf %lf %lf %lf %lf %lf %lf", &x, &y, &z, &a, &b, &c, &r) == 7) { Cdraw3d_ellipsoid (ident, x, y, z, a, b, c, r); } else { textformaterror (line, ident); return -1; } } else if (!strncmp (p, "rectangle ", 10)) { if (sscanf (p, "rectangle %lf %lf %lf %lf %lf %lf", &x, &y, &z, &a, &b, &c) == 6) { Cdraw3d_rectangle (ident, x, y, z, a, b, c); } else { textformaterror (line, ident); return -1; } } else if (!strncmp (p, "sphere ", 7)) { if (sscanf (p, "sphere %lf %lf %lf %lf ", &x, &y, &z, &r) == 4) { Cdraw3d_sphere (ident, x, y, z, r); } else { textformaterror (line, ident); return -1; } } else if (!strncmp (p, "surface ", 8)) { if (sscanf (p, "surface %d %d %n", &w, &h, &i) == 2) { v = Cmalloc(w * h * sizeof(Vec)); for(k = 0; k < w * h; k++) { p += i; if(sscanf(p, "%lf %lf %lf %n", &(v[k].x), &(v[k].y), &(v[k].z), &i) != 3) { textformaterror (line, ident); free(v); return -1; } } draw3d_surface(ident, w, h, v); free(v); } else { textformaterror (line, ident); return -1; } } else { textformaterror (line, ident); return -1; } while (*p != '\n' && *p) p++; line++; } while (*(p++)); Credraw3dobject(ident, 1); return 0; }
return 2; } if (omit < 0) { Cerrordialog (0, 0, 0, " Calibrate ", \ " You have more markers than calibration points "); return 2; } if (d->view[d->current_view].num_marks < MIN_MARKS_NEEDED_TO_CALIBRATE) { Cerrordialog (0, 0, 0, " Calibrate ", \ " You have at least %d markers to calibrate ", \ MIN_MARKS_NEEDED_TO_CALIBRATE); return 2; } } omitted = Cmalloc (d->num_cal_points); memset (omitted, 0, d->num_cal_points); recursive_loop (0, 0, 0, 0, 0, 0, 0, 0, 0); /* reset */ /* try omitting every combination of calibration points without markers and see which gives the minimum error */ /* Here we are not yet leaving out markers: we are first trying to find out which markers correspond to which calibration point */ printf ("Which markers are which?: Trying all combinations:\n"); omitmin = recursive_loop (d, omitted, 0, d->num_cal_points, omit, \ ROUGH_TOL, -1, -1, calc_distortion); if (stop) return 1; /* save the result, there is a 1 in the array if that calibration point is \