File: export_heightmap.cpp

package info (click to toggle)
antimony 0.9.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,476 kB
  • sloc: cpp: 42,596; ansic: 28,661; python: 1,093; yacc: 128; lex: 114; sh: 90; makefile: 10
file content (126 lines) | stat: -rw-r--r-- 3,551 bytes parent folder | download | duplicates (3)
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
115
116
117
118
119
120
121
122
123
124
125
126
#include <Python.h>

#include <QFileDialog>
#include <QMessageBox>

#include "export/export_heightmap.h"

#include "dialog/resolution.h"

#include "fab/util/region.h"
#include "fab/tree/render.h"
#include "fab/formats/png.h"

////////////////////////////////////////////////////////////////////////////////

void ExportHeightmapWorker::run()
{
    // Sanity-check bounds
    if (std::isinf(bounds.xmin) || std::isinf(bounds.xmax) ||
        std::isinf(bounds.ymin) || std::isinf(bounds.ymax))
    {
        QMessageBox::critical(NULL, "Export error",
                "<b>Export error:</b><br>"
                "Target shape has invalid (infinite) bounds");
        return;
    }

    if (resolution == -1)
    {
        auto resolution_dialog = new ResolutionDialog(
                bounds, RESOLUTION_DIALOG_2D, HAS_UNITS);
        if (!resolution_dialog->exec())
            return;
        _resolution = resolution_dialog->getResolution();
        _mm_per_unit = resolution_dialog->getMMperUnit();
        delete resolution_dialog;
    }
    else
    {
        _resolution = resolution;
        _mm_per_unit = mm_per_unit;
    }

    if (_resolution == 0)
    {
        QMessageBox::critical(NULL, "Export error",
                "<b>Export error:</b><br>"
                "Resolution cannot be set to 0");
        return;
    }

    if (filename.isEmpty())
        _filename = QFileDialog::getSaveFileName(
                NULL, "Export .png", "", "*.png");
    else
        _filename = filename;
    if (_filename.isEmpty())
        return;

    if (checkWritable())
        runAsync();

    /*
     *  Check return code from async call
     */
    if (!success)
    {
        QMessageBox::critical(NULL, "Export error",
                            "<b>Writing to png file failed</b><br>"
                            "Check logs for error message with details.");
    }
}

////////////////////////////////////////////////////////////////////////////////

void ExportHeightmapWorker::async()
{
    Region r =  (Region){
        .imin=0, .jmin=0, .kmin=0,
        .ni=uint32_t((bounds.xmax - bounds.xmin) * _resolution),
        .nj=uint32_t((bounds.ymax - bounds.ymin) * _resolution),
        .nk=1
    };

    if (!std::isinf(bounds.zmin) && !std::isinf(bounds.zmax))
        r.nk = uint32_t((bounds.zmax - bounds.zmin) * _resolution);

    build_arrays(
            &r, bounds.xmin, bounds.ymin, bounds.zmin,
                bounds.xmax, bounds.ymax, bounds.zmax);

    uint16_t* d16(new uint16_t[r.ni * r.nj]);
    uint16_t** d16_rows(new uint16_t*[r.nj]);

    for (unsigned i=0; i < r.nj; ++i)
        d16_rows[i] = &d16[r.ni * i];

    memset(d16, 0, r.ni * r.nj * sizeof(uint16_t));
    render16(shape.tree.get(), r, d16_rows, &halt, NULL);

    // These bounds will be stored to give the .png real-world units.
    float bounds[6] = {
        r.X[0] * _mm_per_unit,
        r.Y[0] * _mm_per_unit,
        r.Z[0] * _mm_per_unit,
        r.X[r.ni] * _mm_per_unit,
        r.Y[r.nj] * _mm_per_unit,
        r.Z[r.nk] * _mm_per_unit};

    // Flip rows before saving image
    for (unsigned i=0; i < r.nj; ++i)
        d16_rows[r.nj - i - 1] = d16 + (r.ni * i);

    // If the operation has been cancelled, then mark it as a sucess;
    // otherwise, attempt to write the file and check the return code.
    if (halt)
        success = true;
    else
        success = save_png16L(_filename.toStdString().c_str(), r.ni, r.nj,
                              bounds, d16_rows);

    free_arrays(&r);
    delete [] d16;
    delete [] d16_rows;
}