Welcome to mirror list, hosted at ThFree Co, Russian Federation.

dualcon_c_api.cpp « intern « dualcon « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 92f8b0cfd1a002e03ed6813b305f6d5eba57a8c9 (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
/*
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Contributor(s): Nicholas Bishop
 *
 * ***** END GPL LICENSE BLOCK *****
 */

#include <cassert>
#include "dualcon.h"
#include "ModelReader.h"
#include "octree.h"

#include <cstdio>
#include <float.h>

#if defined(_WIN32)
#define isnan(n) _isnan(n)
#endif

static void veccopy(float dst[3], const float src[3])
{
	dst[0] = src[0];
	dst[1] = src[1];
	dst[2] = src[2];
}

#define GET_TRI(_mesh, _n) \
	(*(DualConTri)(((char *)(_mesh)->looptri) + ((_n) * (_mesh)->tri_stride)))

#define GET_CO(_mesh, _n) \
	(*(DualConCo)(((char *)(_mesh)->co) + ((_n) * (_mesh)->co_stride)))

#define GET_LOOP(_mesh, _n) \
	(*(DualConLoop)(((char *)(_mesh)->mloop) + ((_n) * (_mesh)->loop_stride)))

class DualConInputReader : public ModelReader
{
private:
const DualConInput *input_mesh;
int tottri, curtri;
float min[3], max[3], maxsize;
float scale;
public:
DualConInputReader(const DualConInput *mesh, float _scale)
	: input_mesh(mesh), scale(_scale)
{
	reset();
}

void reset()
{
	curtri = 0;
	maxsize = 0;
	tottri = input_mesh->tottri;

	veccopy(min, input_mesh->min);
	veccopy(max, input_mesh->max);

	/* initialize maxsize */
	for (int i = 0; i < 3; i++) {
		float d = max[i] - min[i];
		if (d > maxsize)
			maxsize = d;
	}

	/* redo the bounds */
	for (int i = 0; i < 3; i++)
	{
		min[i] = (max[i] + min[i]) / 2 - maxsize / 2;
		max[i] = (max[i] + min[i]) / 2 + maxsize / 2;
	}

	for (int i = 0; i < 3; i++)
		min[i] -= maxsize * (1 / scale - 1) / 2;
	maxsize *= 1 / scale;
}

Triangle *getNextTriangle()
{
	if (curtri == input_mesh->tottri)
		return NULL;

	Triangle *t = new Triangle();

	unsigned int *tr = GET_TRI(input_mesh, curtri);
	veccopy(t->vt[0], GET_CO(input_mesh, GET_LOOP(input_mesh, tr[0])));
	veccopy(t->vt[1], GET_CO(input_mesh, GET_LOOP(input_mesh, tr[1])));
	veccopy(t->vt[2], GET_CO(input_mesh, GET_LOOP(input_mesh, tr[2])));

	curtri++;

	/* remove triangle if it contains invalid coords */
	for (int i = 0; i < 3; i++) {
		const float *co = t->vt[i];
		if (isnan(co[0]) || isnan(co[1]) || isnan(co[2])) {
			delete t;
			return getNextTriangle();
		}
	}

	return t;
}

int getNextTriangle(int t[3])
{
	if (curtri == input_mesh->tottri)
		return 0;

	unsigned int *tr = GET_TRI(input_mesh, curtri);
	t[0] = tr[0];
	t[1] = tr[1];
	t[2] = tr[2];

	curtri++;

	return 1;
}

int getNumTriangles()
{
	return tottri;
}

int getNumVertices()
{
	return input_mesh->totco;
}

float getBoundingBox(float origin[3])
{
	veccopy(origin, min);
	return maxsize;
}

/* output */
void getNextVertex(float /*v*/[3])
{
	/* not used */
}

/* stubs */
void printInfo() {
}
int getMemory() {
	return sizeof(DualConInputReader);
}

#ifdef WITH_CXX_GUARDEDALLOC
	MEM_CXX_CLASS_ALLOC_FUNCS("DUALCON:DualConInputReader")
#endif

};

void *dualcon(const DualConInput *input_mesh,
              /* callbacks for output */
              DualConAllocOutput alloc_output,
              DualConAddVert add_vert,
              DualConAddQuad add_quad,

              DualConFlags flags,
              DualConMode mode,
              float threshold,
              float hermite_num,
              float scale,
              int depth)
{
	DualConInputReader r(input_mesh, scale);
	Octree o(&r, alloc_output, add_vert, add_quad,
	         flags, mode, depth, threshold, hermite_num);
	o.scanConvert();
	return o.getOutputMesh();
}