~ubuntu-branches/ubuntu/raring/png23d/raring

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * Copyright 2011 Vincent Sanders <vince@kyllikki.org>
 *
 * Licenced under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 *
 * This file is part of png23d.
 *
 * Routines to handle meshes
 */

#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>

#include "option.h"
#include "bitmap.h"
#include "mesh.h"
#include "mesh_gen.h"
#include "mesh_index.h"
#include "mesh_simplify.h"
#include "mesh_math.h"

void
debug_mesh_init(struct mesh *mesh, const char* filename)
{
    if (filename == NULL)
        return;

    mesh->dumpfile = fopen(filename, "w");

    if (mesh->dumpfile == NULL)
        return;

    fprintf(mesh->dumpfile,"<html>\n<body>");

}

static void
debug_mesh_fini(struct mesh *mesh, unsigned int start)
{
    unsigned int floop;
    struct vertex *v0 = vertex_from_index(mesh, start);

    if (mesh->dumpfile == NULL)
        return;

    fprintf(mesh->dumpfile, "<h2>Final mesh</h2>");

    fprintf(mesh->dumpfile,
            "<p>Final mesh had %d facets and %d vertexes.</p>\n",
            mesh->fcount, mesh->vcount);

    fprintf(mesh->dumpfile,"<p>Mesh of all facets with common normal</p>\n<svg width=\"%d\" height=\"%d\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n", DUMP_SVG_SIZE, DUMP_SVG_SIZE);

    for (floop = 0; floop < mesh->fcount; floop++) {
        if (same_normal(&mesh->f[floop].n, &v0->facets[0]->n)) {

            fprintf(mesh->dumpfile,
                    "<polygon points=\"%.1f,%.1f %.1f,%.1f %.1f,%.1f\" style=\"fill:lime;stroke:black;stroke-width=1\"/>\n",
                    SVGPX(mesh->f[floop].v[0].x), SVGPY(mesh->f[floop].v[0].y),
                    SVGPX(mesh->f[floop].v[1].x), SVGPY(mesh->f[floop].v[1].y),
                    SVGPX(mesh->f[floop].v[2].x), SVGPY(mesh->f[floop].v[2].y));

            /* label facet at centroid */
            fprintf(mesh->dumpfile,
                    "<text x=\"%.1f\" y=\"%.1f\" fill=\"blue\">%u</text>\n",
                    (SVGPX(mesh->f[floop].v[0].x) +
                     SVGPX(mesh->f[floop].v[1].x) +
                     SVGPX(mesh->f[floop].v[2].x)) / 3,
                    (SVGPY(mesh->f[floop].v[0].y) +
                     SVGPY(mesh->f[floop].v[1].y) +
                     SVGPY(mesh->f[floop].v[2].y)) / 3,
                    floop);


        }
    }

    fprintf(mesh->dumpfile,"</body>\n</html>\n");

    fclose(mesh->dumpfile);

    mesh->dumpfile = NULL;
}



/* exported method documented in mesh.h */
struct mesh *new_mesh(void)
{
    struct mesh *mesh;

    mesh = calloc(1, sizeof(struct mesh));

    return mesh;
}



/* exported method documented in mesh.h */
void free_mesh(struct mesh *mesh)
{
    debug_mesh_fini(mesh, 4);
    free(mesh->bloom_table);
    free(mesh->f);
}