2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * Contributor(s): Nicholas Bishop
20 * ***** END GPL LICENSE BLOCK *****
25 #include "ModelReader.h"
32 #define isnan(n) _isnan(n)
35 static void veccopy(float dst[3], const float src[3])
42 #define GET_TRI(_mesh, _n) \
43 (*(DualConTri)(((char *)(_mesh)->looptri) + ((_n) * (_mesh)->tri_stride)))
45 #define GET_CO(_mesh, _n) \
46 (*(DualConCo)(((char *)(_mesh)->co) + ((_n) * (_mesh)->co_stride)))
48 #define GET_LOOP(_mesh, _n) \
49 (*(DualConLoop)(((char *)(_mesh)->mloop) + ((_n) * (_mesh)->loop_stride)))
51 class DualConInputReader : public ModelReader
54 const DualConInput *input_mesh;
56 float min[3], max[3], maxsize;
59 DualConInputReader(const DualConInput *mesh, float _scale)
60 : input_mesh(mesh), scale(_scale)
69 tottri = input_mesh->tottri;
71 veccopy(min, input_mesh->min);
72 veccopy(max, input_mesh->max);
74 /* initialize maxsize */
75 for (int i = 0; i < 3; i++) {
76 float d = max[i] - min[i];
82 for (int i = 0; i < 3; i++)
84 min[i] = (max[i] + min[i]) / 2 - maxsize / 2;
85 max[i] = (max[i] + min[i]) / 2 + maxsize / 2;
88 for (int i = 0; i < 3; i++)
89 min[i] -= maxsize * (1 / scale - 1) / 2;
93 Triangle *getNextTriangle()
95 if (curtri == input_mesh->tottri)
98 Triangle *t = new Triangle();
100 unsigned int *tr = GET_TRI(input_mesh, curtri);
101 veccopy(t->vt[0], GET_CO(input_mesh, GET_LOOP(input_mesh, tr[0])));
102 veccopy(t->vt[1], GET_CO(input_mesh, GET_LOOP(input_mesh, tr[1])));
103 veccopy(t->vt[2], GET_CO(input_mesh, GET_LOOP(input_mesh, tr[2])));
107 /* remove triangle if it contains invalid coords */
108 for (int i = 0; i < 3; i++) {
109 const float *co = t->vt[i];
110 if (isnan(co[0]) || isnan(co[1]) || isnan(co[2])) {
112 return getNextTriangle();
119 int getNextTriangle(int t[3])
121 if (curtri == input_mesh->tottri)
124 unsigned int *tr = GET_TRI(input_mesh, curtri);
134 int getNumTriangles()
141 return input_mesh->totco;
144 float getBoundingBox(float origin[3])
146 veccopy(origin, min);
151 void getNextVertex(float /*v*/[3])
160 return sizeof(DualConInputReader);
163 #ifdef WITH_CXX_GUARDEDALLOC
164 MEM_CXX_CLASS_ALLOC_FUNCS("DUALCON:DualConInputReader")
169 void *dualcon(const DualConInput *input_mesh,
170 /* callbacks for output */
171 DualConAllocOutput alloc_output,
172 DualConAddVert add_vert,
173 DualConAddQuad add_quad,
182 DualConInputReader r(input_mesh, scale);
183 Octree o(&r, alloc_output, add_vert, add_quad,
184 flags, mode, depth, threshold, hermite_num);
186 return o.getOutputMesh();