-/*
- * Write the triangle data to the cache.
- */
-static void _group_write_cache(const char* filename, int count, list_t* l)
-{
- char* cachename = mem_strcat(".", filename);
- FILE* file = fopen(cachename, "wb");
- if (file == NULL) {
- fprintf(stderr, "Cannot write %s: %s\n", cachename, strerror(errno));
- return;
- }
-
- _CHECK_IO(fwrite(&count, sizeof(count), 1, file));
- for (list_t* i = l; i; i = i->link) {
- tri_t* t = (tri_t*)i->val;
- _CHECK_IO(fwrite(&t->a.v.x, sizeof(float), 1, file));
- _CHECK_IO(fwrite(&t->a.v.y, sizeof(float), 1, file));
- _CHECK_IO(fwrite(&t->a.v.z, sizeof(float), 1, file));
- _CHECK_IO(fwrite(&t->b.v.x, sizeof(float), 1, file));
- _CHECK_IO(fwrite(&t->b.v.y, sizeof(float), 1, file));
- _CHECK_IO(fwrite(&t->b.v.z, sizeof(float), 1, file));
- _CHECK_IO(fwrite(&t->c.v.x, sizeof(float), 1, file));
- _CHECK_IO(fwrite(&t->c.v.y, sizeof(float), 1, file));
- _CHECK_IO(fwrite(&t->c.v.z, sizeof(float), 1, file));
- _CHECK_IO(fwrite(&t->a.n.x, sizeof(float), 1, file));
- _CHECK_IO(fwrite(&t->a.n.y, sizeof(float), 1, file));
- _CHECK_IO(fwrite(&t->a.n.z, sizeof(float), 1, file));
- _CHECK_IO(fwrite(&t->b.n.x, sizeof(float), 1, file));
- _CHECK_IO(fwrite(&t->b.n.y, sizeof(float), 1, file));
- _CHECK_IO(fwrite(&t->b.n.z, sizeof(float), 1, file));
- _CHECK_IO(fwrite(&t->c.n.x, sizeof(float), 1, file));
- _CHECK_IO(fwrite(&t->c.n.y, sizeof(float), 1, file));
- _CHECK_IO(fwrite(&t->c.n.z, sizeof(float), 1, file));
- }
-
-fail:
- mem_free(cachename);
-}
-
-#undef _CHECK_IO
-
-
-/*
- * Allocate a group by reading raw triangle coordinates from a file.
- */
-static _group_t* _group_alloc(const char* filename)
-{
- _group_t* g = (_group_t*)mem_alloc(sizeof(_group_t));
- g->triangles = NULL;
- g->model = MAT_IDENTITY;
- g->name = mem_strdup(filename);
- g->count = 0;
-
-#if PRE_NORMALS == 3
- int r = _group_try_read_cache(filename, &g->triangles);
- if (0 < r) {
- g->count = r;
- return g;
- }
-#endif
-
- FILE* file = fopen(filename, "r");
- if (file == NULL) {
- fprintf(stderr, "Cannot read %s: %s\n", filename, strerror(errno));
- return NULL;
- }
-
-#if PRE_NORMALS >= 2
- map_t* m = map_vnorm_alloc();
-#endif
-
- double x1, y1, z1, x2, y2, z2, x3, y3, z3;
- while (fscanf(file, " %lf %lf %lf %lf %lf %lf %lf %lf %lf",
- &x1, &y1, &z1, &x2, &y2, &z2, &x3, &y3, &z3) == 9) {
- tri_t* t = tri_alloc(
- vert_new2((scal_t)x1, (scal_t)y1, (scal_t)z1),
- vert_new2((scal_t)x2, (scal_t)y2, (scal_t)z2),
- vert_new2((scal_t)x3, (scal_t)y3, (scal_t)z3)
- );
- list_push2(&g->triangles, t, mem_free);
- ++g->count;
-
-#if PRE_NORMALS == 1
- vec_t n = vec_normalize(tri_normal(*t));
- t->a.n = n;
- t->b.n = n;
- t->c.n = n;
-#elif PRE_NORMALS >= 2
- _find_normals_add_triangle(m, t);
-#endif
- }
-
-#if PRE_NORMALS >= 2
- map_vnorm_call(m, _find_normals_average);
- rbtree_destroy(m);
-#if PRE_NORMALS == 3
- list_reverse(&g->triangles);
- _group_write_cache(filename, g->count, g->triangles);
-#endif
-#endif
-
- fclose(file);
-
- if (g->triangles == NULL) {
- fprintf(stderr, "No triangles coordinates read from %s\n", filename);
- mem_free(g);
- return NULL;
- }
-
- return g;
-}
-
-/*
- * Destroy a group.
- */
-static void _group_destroy(_group_t* g)
-{
- mem_free(g->name);
- list_destroy(g->triangles);
- mem_free(g);
-}
-
-
-/*
- * Set the colors of the triangles in the group as defined in a file.
- */
-static int _group_set_colors(_group_t* g, FILE* file)
-{
- double r1, g1, b1, r2, g2, b2, r3, g3, b3;
- if (fscanf(file, " %lf %lf %lf %lf %lf %lf %lf %lf %lf",
- &r1, &g1, &b1, &r2, &g2, &b2, &r3, &g3, &b3) != 9) {
- fprintf(stderr, "Cannot read color values from scene file.\n");
- return -1;
- }
-
- for (list_t* i = g->triangles; i; i = i->link) {
- tri_t* t = (tri_t*)i->val;
- t->a.c = color_new((colorchan_t)r1, (colorchan_t)g1, (colorchan_t)b1, S(1.0));
- t->b.c = color_new((colorchan_t)r2, (colorchan_t)g2, (colorchan_t)b2, S(1.0));
- t->c.c = color_new((colorchan_t)r3, (colorchan_t)g3, (colorchan_t)b3, S(1.0));
- }
- return 0;
-}
-
-/*
- * Concat a translation matrix to the transformation as defined in a file.
- */
-static int _group_add_translate(_group_t* g, FILE* file)
-{
- double tx, ty, tz;
- if (fscanf(file, " %lf %lf %lf", &tx, &ty, &tz) != 3) {
- fprintf(stderr, "Cannot read translate coordinates from scene file.\n");
- return -1;
- }
- g->model = mat_mult(g->model, MAT_TRANSLATE((scal_t)tx, (scal_t)ty, (scal_t)tz));
- return 0;
-}
-
-/*
- * Concat a rotation matrix to the transformation as defined in a file.
- */
-static int _group_add_rotate(_group_t* g, FILE* file)
-{
- double theta, ax, ay, az;
- if (fscanf(file, " %lf %lf %lf %lf", &theta, &ax, &ay, &az) != 4) {
- fprintf(stderr, "Cannot read rotation angle from scene file.\n");
- return -1;
- }
- g->model = mat_mult(g->model, MAT_ROTATE((scal_t)theta, (scal_t)ax, (scal_t)ay, (scal_t)az));
- return 0;
-}
-
-/*
- * Concat a scale matrix to the transformation as defined in a file.
- */
-static int _group_add_scale(_group_t* g, FILE* file)
-{
- double sx, sy, sz;
- if (fscanf(file, " %lf %lf %lf", &sx, &sy, &sz) != 3) {
- fprintf(stderr, "Cannot read scale factors from scene file.\n");
- return -1;
- }
- g->model = mat_mult(g->model, MAT_SCALE((scal_t)sx, (scal_t)sy, (scal_t)sz));
- return 0;
-}