summaryrefslogtreecommitdiff
path: root/modules/graphic_objects/src/cpp/DecompositionUtils.cpp
blob: 5c0e3c06ee7c045c18eebc949e498ea9e1c09e8f (plain)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
 *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 *  Copyright (C) 2011 - DIGITEO - Manuel Juliachs
 *
 *  This file must be used under the terms of the CeCILL.
 *  This source file is licensed as described in the file COPYING, which
 *  you should have received as part of this distribution.  The terms
 *  are also available at
 *  http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
 *
 */

#include "DecompositionUtils.hxx"

extern "C"
{
#include <math.h>
#include <float.h>
#ifdef _MSC_VER
#define isnan _isnan
    // isinf(x)
    // | +%inf -> 1
    // | -%inf -> -1
    // | _ -> 0
#define isinf(x) (_fpclass(x)==_FPCLASS_PINF?1:(_fpclass(x)==_FPCLASS_NINF?-1:0))
#endif
}

int DecompositionUtils::isANumber(double x)
{
    if (isnan(x))
    {
        return 0;
    }
    else
    {
        return 1;
    }
}

int DecompositionUtils::isFinite(double x)
{
    if (isinf(x))
    {
        return 0;
    }
    else
    {
        return 1;
    }
}

int DecompositionUtils::isValid(double x)
{
    if (isnan(x) || isinf(x))
    {
        return 0;
    }
    else
    {
        return 1;
    }
}

int DecompositionUtils::isValid(double x, double y, double z)
{
    if (isnan(x) || isnan(y) || isnan(z) || isinf(x) || isinf(y) || isinf(z))
    {
        return 0;
    }
    else
    {
        return 1;
    }
}

int DecompositionUtils::isValid(double x, double y)
{
    if (isnan(x) || isnan(y) || isinf(x) || isinf(y))
    {
        return 0;
    }
    else
    {
        return 1;
    }
}

double DecompositionUtils::getLog10Value(double value)
{
    return log10(value);
}

int DecompositionUtils::isLogValid(double x)
{
    if (x > 0.0)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

int DecompositionUtils::isLogValid(double x, double y, double z, int logMask)
{
    int valid = 1;

    if (logMask & 0x1)
    {
        valid &= (x > 0.0);
    }

    if (logMask & 0x2)
    {
        valid &= (y > 0.0);
    }

    if (logMask & 0x4)
    {
        valid &= (z > 0.0);
    }

    return valid;
}

int DecompositionUtils::isLogValid(double x, double y, int logMask)
{
    int valid = 1;

    if (logMask & 0x1)
    {
        valid &= (x > 0.0);
    }

    if (logMask & 0x2)
    {
        valid &= (y > 0.0);
    }

    return valid;
}

double DecompositionUtils::getMaxDoubleValue(void)
{
    return DBL_MAX;
}

double DecompositionUtils::getMinDoubleValue(void)
{
    return DBL_MIN;
}

double DecompositionUtils::getAbsoluteValue(double value)
{
    return fabs(value);
}

double DecompositionUtils::getSquareRoot(double value)
{
    return sqrt(value);
}

/*
 * Decomposes a rectangle into two adjacent triangles.
 * The rectangle's vertices are supposed to be specified in
 * counter-clockwise order, with 0 corresponding to the former's lower-left vertex.
 * The two output triangles' vertex indices are also specified in
 * counter-clockwise order.
 */
void DecompositionUtils::getDecomposedRectangleTriangleIndices(int* indices)
{
    indices[0] = 0;
    indices[1] = 1;
    indices[2] = 2;
    indices[3] = 0;
    indices[4] = 2;
    indices[5] = 3;
}

/* To do: use a Vector3d class to perform vector operations. */
void DecompositionUtils::getDecomposedQuadTriangleIndices(double vertices[4][3], int* facetVertexIndices, int* triangleVertexIndices)
{
    /* The two decompositions' midpoints */
    double mid0[3];
    double mid1[3];

    /* The vectors from one midpoint to its opposite vertices */
    double mo0[3];
    double mo1[3];

    double nmo0 = 0.;
    double nmo1 = 0.;

    double dot0 = 0.;
    double dot1 = 0.;

    double denom = 0.;

    /*
     * The input vertices are given in counter-clockwise order, from v0 to v3.
     * Two decompositions are possible: either (v0,v1,v2) and (v0,v2,v3) or (v1,v2,v3) and (v1,v3,v0).
     * The best one is the one that yields the most coplanar triangles. To estimate this, for each configuration,
     * we compute the midpoint of the triangles' shared edge, which are respectively mid0 and mid1.
     * The angles (v1 mid0 v3) and (v2 mid1 v0) give an approximation of the angles between the two triangles' planes
     * for respectively the first and second condigurations.
     */

    mid0[0] = 0.5 * (vertices[0][0] + vertices[2][0]);
    mid0[1] = 0.5 * (vertices[0][1] + vertices[2][1]);
    mid0[2] = 0.5 * (vertices[0][2] + vertices[2][2]);
    mid1[0] = 0.5 * (vertices[1][0] + vertices[3][0]);
    mid1[1] = 0.5 * (vertices[1][1] + vertices[3][1]);
    mid1[2] = 0.5 * (vertices[1][2] + vertices[3][2]);

    /* 1st decomposition */

    /* mo0 = v1 - mid0 */
    mo0[0] = vertices[1][0] - mid0[0];
    mo0[1] = vertices[1][1] - mid0[1];
    mo0[2] = vertices[1][2] - mid0[2];

    /* mo1 = v3 - mid0 */
    mo1[0] = vertices[3][0] - mid0[0];
    mo1[1] = vertices[3][1] - mid0[1];
    mo1[2] = vertices[3][2] - mid0[2];

    nmo0 = mo0[0] * mo0[0] + mo0[1] * mo0[1] + mo0[2] * mo0[2];
    nmo1 = mo1[0] * mo1[0] + mo1[1] * mo1[1] + mo1[2] * mo1[2];

    if (nmo0 * nmo1 > 0.0)
    {
        denom = DecompositionUtils::getSquareRoot(nmo0 * nmo1);
    }
    else
    {
        denom = 1.0;
    }

    dot0 = (mo0[0] * mo1[0] + mo0[1] * mo1[1] + mo0[2] * mo1[2]) / denom;

    /* 2nd decomposition */

    /* mo0 = v2 - mid1 */
    mo0[0] = vertices[2][0] - mid1[0];
    mo0[1] = vertices[2][1] - mid1[1];
    mo0[2] = vertices[2][2] - mid1[2];

    /* mo1 = v0 - mid1 */
    mo1[0] = vertices[0][0] - mid1[0];
    mo1[1] = vertices[0][1] - mid1[1];
    mo1[2] = vertices[0][2] - mid1[2];

    nmo0 = mo0[0] * mo0[0] + mo0[1] * mo0[1] + mo0[2] * mo0[2];
    nmo1 = mo1[0] * mo1[0] + mo1[1] * mo1[1] + mo1[2] * mo1[2];

    if (nmo0 * nmo1 > 0.0)
    {
        denom = getSquareRoot(nmo0 * nmo1);
    }
    else
    {
        denom = 1.0;
    }

    dot1 = (mo0[0] * mo1[0] + mo0[1] * mo1[1] + mo0[2] * mo1[2]) / denom;

    /* The lower the dot product, the closer to -1, and the more coplanar the triangles are. */
    if (dot0 <= dot1)
    {
        triangleVertexIndices[0] = facetVertexIndices[0];
        triangleVertexIndices[1] = facetVertexIndices[1];
        triangleVertexIndices[2] = facetVertexIndices[2];
        triangleVertexIndices[3] = facetVertexIndices[0];
        triangleVertexIndices[4] = facetVertexIndices[2];
        triangleVertexIndices[5] = facetVertexIndices[3];
    }
    else
    {
        triangleVertexIndices[0] = facetVertexIndices[1];
        triangleVertexIndices[1] = facetVertexIndices[2];
        triangleVertexIndices[2] = facetVertexIndices[3];
        triangleVertexIndices[3] = facetVertexIndices[1];
        triangleVertexIndices[4] = facetVertexIndices[3];
        triangleVertexIndices[5] = facetVertexIndices[0];
    }
}