File: PCVContext.cpp

package info (click to toggle)
cloudcompare 2.10.1-2
  • links: PTS
  • area: main
  • in suites: buster
  • size: 55,916 kB
  • sloc: cpp: 219,837; ansic: 29,944; makefile: 67; sh: 45
file content (355 lines) | stat: -rw-r--r-- 8,893 bytes parent folder | download | duplicates (2)
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
//##########################################################################
//#                                                                        #
//#                                PCV                                     #
//#                                                                        #
//#  This program is free software; you can redistribute it and/or modify  #
//#  it under the terms of the GNU Library General Public License as       #
//#  published by the Free Software Foundation; version 2 or later of the License.  #
//#                                                                        #
//#  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.                          #
//#                                                                        #
//#          COPYRIGHT: EDF R&D / TELECOM ParisTech (ENST-TSI)             #
//#                                                                        #
//##########################################################################

#include "PCVContext.h"

//CCLib
#include <CCMiscTools.h>
#include <GenericTriangle.h>

//Qt
#include <QGLPixelBuffer>

//OpenGL
#ifdef __APPLE__
#include <OpenGL/glu.h>
#else
#include <GL/glu.h>
#endif

//system
#include <assert.h>

//type-less glVertex3Xv call (X=f,d)
static inline void glVertex3v(const float* v) { glVertex3fv(v); }
static inline void glVertex3v(const double* v) { glVertex3dv(v); }

//type-less glTranslateX call (X=f,d)
static inline void glTranslate(float x, float y, float z) { glTranslatef(x, y, z); }
static inline void glTranslate(double x, double y, double z) { glTranslated(x, y, z); }

//type-less glScaleX call (X=f,d)
static inline void glScale(float x, float y, float z) { glScalef(x, y, z); }
static inline void glScale(double x, double y, double z) { glScaled(x, y, z); }

using namespace CCLib;

#ifndef ZTWIST
#define ZTWIST 1e-3f
#endif

PCVContext::PCVContext()
	: m_vertices(0)
	, m_mesh(0)
	, m_zoom(1)
	, m_pixBuffer(nullptr)
	, m_width(0)
	, m_height(0)
	, m_snapZ(nullptr)
	, m_snapC(nullptr)
	, m_meshIsClosed(false)
{
	memset(m_viewMat, 0, sizeof(float)*OPENGL_MATRIX_SIZE);
}

PCVContext::~PCVContext()
{
	if (m_pixBuffer)
		delete m_pixBuffer;

	if (m_snapZ)
		delete[] m_snapZ;
	if (m_snapC)
		delete[] m_snapC;
}

bool PCVContext::init(unsigned W,
					  unsigned H,
					  CCLib::GenericCloud* cloud,
					  CCLib::GenericMesh* mesh/*=0*/,
					  bool closedMesh/*=true*/)
{
	if (!QGLPixelBuffer::hasOpenGLPbuffers())
		return false;

	assert(!m_pixBuffer);

	m_pixBuffer = new QGLPixelBuffer(W, H);
	if (!m_pixBuffer || !m_pixBuffer->isValid())
		return false;

	unsigned size = W*H;
	m_snapZ = new float[size];
	if (!m_snapZ)
	{
		delete m_pixBuffer;
		m_pixBuffer = 0;
		return false;
	}

	m_meshIsClosed = (closedMesh || !mesh);
	if (!m_meshIsClosed)
	{
		//buffer for color
		m_snapC = new unsigned char[4*size];
		if (!m_snapC)
		{
			delete m_pixBuffer;
			m_pixBuffer = 0;
			delete[] m_snapZ;
			m_snapZ = 0;
			return false;
		}
	}

	m_width = W;
	m_height = H;

	associateToEntity(cloud, mesh);

	glInit();

	return true;
}

void PCVContext::associateToEntity(GenericCloud* cloud, GenericMesh* mesh)
{
	assert(cloud);
	if (!cloud)
		return;

	m_vertices = cloud;
	m_mesh = mesh;

	//we get cloud bounding box
	CCVector3 bbMin, bbMax;
	m_vertices->getBoundingBox(bbMin, bbMax);

	//we compute bbox diagonal
	PointCoordinateType maxD = (bbMax - bbMin).norm();

	//we deduce default zoom
	m_zoom = (maxD > ZERO_TOLERANCE ? static_cast<PointCoordinateType>(std::min(m_width, m_height)) / maxD : PC_ONE);

	//as well as display center
	m_viewCenter = (bbMax+bbMin)/2;
}

void PCVContext::glInit()
{
	if (!m_pixBuffer || !m_pixBuffer->isValid())
		return;

	m_pixBuffer->makeCurrent();

	glClearColor(0.0, 0.0, 0.0, 0.0);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_CULL_FACE);
	glDepthMask(GL_TRUE);
	glDisable(GL_LIGHTING);

	glPixelStorei(GL_PACK_ROW_LENGTH, 0);
	glPixelStorei(GL_PACK_ALIGNMENT, 1);

	//model view matrix initialization
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glGetFloatv(GL_MODELVIEW_MATRIX, m_viewMat);
	glPushMatrix();

	//projection matrix initialization
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	float w2 = 0.5f * m_width;
	float h2 = 0.5f * m_height;
	float maxD = static_cast<float>(std::max(m_width, m_height));
	glOrtho(-w2, w2, -h2, h2, -maxD, maxD);
	glPushMatrix();
}

void PCVContext::setViewDirection(const CCVector3& V)
{
	if (!m_pixBuffer || !m_pixBuffer->isValid())
		return;

	m_pixBuffer->makeCurrent();

	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadIdentity();

	CCVector3 U(0, 0, 1);
	if (1 - fabs(V.dot(U)) < 1.0e-4)
	{
		U.y = 1;
		U.z = 0;
	}

	gluLookAt(-V.x, -V.y, -V.z, 0.0, 0.0, 0.0, U.x, U.y, U.z);
	glGetFloatv(GL_MODELVIEW_MATRIX, m_viewMat);
	glPopMatrix();
}

void PCVContext::drawEntity()
{
	assert(m_vertices);

	glMatrixMode(GL_MODELVIEW);
	glLoadMatrixf(m_viewMat);
	glScale(m_zoom, m_zoom, m_zoom);
	glTranslate(-m_viewCenter.x, -m_viewCenter.y, -m_viewCenter.z);

	glColor3ub(255, 255, 0); //yellow by default

	if (m_mesh)
	{
		unsigned nTri = m_mesh->size();
		m_mesh->placeIteratorAtBeginning();

		glBegin(GL_TRIANGLES);
		for (unsigned i = 0; i < nTri; ++i)
		{
			const GenericTriangle* t = m_mesh->_getNextTriangle();
			glVertex3v(t->_getA()->u);
			glVertex3v(t->_getB()->u);
			glVertex3v(t->_getC()->u);
		}
		glEnd();
	}
	else
	{
		unsigned nPts = m_vertices->size();
		m_vertices->placeIteratorAtBeginning();

		glBegin(GL_POINTS);
		for (unsigned i = 0; i < nPts; ++i)
			glVertex3v(m_vertices->getNextPoint()->u);
		glEnd();
	}
}

void openGLSnapshot(GLenum format, GLenum type, void* buffer)
{
	assert(buffer);

	int vp[4];
	glGetIntegerv(GL_VIEWPORT, vp);

	glReadPixels(vp[0], vp[1], vp[2], vp[3], format, type, buffer);
}

//The method below is inspired from ShadeVis' "GLAccumPixel" (Cignoni et al.)
/****************************************************************************
* VCGLib                                                            o o     *
* Visual and Computer Graphics Library                            o     o   *
*                                                                _   O  _   *
* Copyright(C) 2004                                                \/)\/    *
* Visual Computing Lab                                            /\/|      *
* ISTI - Italian National Research Council                           |      *
*****************************************************************************/
int PCVContext::GLAccumPixel(std::vector<int>& visibilityCount)
{
	if (!m_pixBuffer || !m_pixBuffer->isValid())
		return -1;
	if (!m_vertices)
		return -1;
	if (m_vertices->size() != visibilityCount.size())
		return -1;

	assert(m_snapZ);

	m_pixBuffer->makeCurrent();

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glDepthRange(2.0f*ZTWIST, 1.0f);

	if (m_meshIsClosed)
		glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
	else
		glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);

	//display 3D entity (front)
	glCullFace(GL_BACK);
	drawEntity();

	if (!m_meshIsClosed)
	{
		//display it again (back)
		glCullFace(GL_FRONT);
		drawEntity();

		assert(m_snapC);
		openGLSnapshot(GL_RGBA, GL_UNSIGNED_BYTE, m_snapC);
	}
	openGLSnapshot(GL_DEPTH_COMPONENT, GL_FLOAT, m_snapZ);

	if (m_meshIsClosed)
		glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);

	glDepthRange(0, 1.0f - 2.0f*ZTWIST);
	double MM[OPENGL_MATRIX_SIZE];
	glGetDoublev(GL_MODELVIEW_MATRIX, MM);
	double MP[OPENGL_MATRIX_SIZE];
	glGetDoublev(GL_PROJECTION_MATRIX, MP);
	int VP[4];
	glGetIntegerv(GL_VIEWPORT, VP);

	int count = 0;
	int sx4 = (m_width << 2);

	unsigned nVert = m_vertices->size();
	m_vertices->placeIteratorAtBeginning();
	for (unsigned i = 0; i < nVert; ++i)
	{
		const CCVector3* P = m_vertices->getNextPoint();

		double tx, ty, tz;
		gluProject(P->x, P->y, P->z, MM, MP, VP, &tx, &ty, &tz);

		int txi = static_cast<int>(floor(tx));
		int tyi = static_cast<int>(floor(ty));
		if (txi >= 0 && txi < static_cast<int>(m_width)
			&& tyi >= 0 && tyi < static_cast<int>(m_height))
		{
			int dec = txi + tyi*static_cast<int>(m_width);
			int col = 1;

			if (!m_meshIsClosed)
			{
				//int c1 = std::max(m_snapC[dec],m_snapC[dec<<2+4]);
				//int c2 = std::max(m_snapC[dec<<2+sx4],m_snapC[dec<<2+sx4+4]);
				const unsigned char* pix = m_snapC + (dec << 2);
				int c1 = std::max(pix[0], pix[4]);
				pix += sx4;
				int c2 = std::max(pix[0], pix[4]);
				col = std::max(c1, c2);
			}

			if (col != 0)
			{
				if (tz < static_cast<double>(m_snapZ[dec]))
				{
					assert(i < visibilityCount.size());
					++visibilityCount[i];
					++count;
				}
			}
		}
	}

	return count;
}