File: ccExtru.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 (190 lines) | stat: -rw-r--r-- 5,235 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
//##########################################################################
//#                                                                        #
//#                              CLOUDCOMPARE                              #
//#                                                                        #
//#  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; 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 "ccExtru.h"

//qCC_db
#include "ccPointCloud.h"
#include "ccNormalVectors.h"

//CCLib
#include <Delaunay2dMesh.h>

//system
#include <string.h>

ccExtru::ccExtru(const std::vector<CCVector2>& profile,
				 PointCoordinateType height,
				 const ccGLMatrix* transMat /*= 0*/,
				 QString name/*="Extrusion"*/)
	: ccGenericPrimitive(name,transMat)
	, m_height(height)
	, m_profile(profile)
{
	assert(m_profile.size() > 2);

	updateRepresentation();
}

ccExtru::ccExtru(QString name/*="Plane"*/)
	: ccGenericPrimitive(name)
	, m_height(0)
{
}

ccGenericPrimitive* ccExtru::clone() const
{
	return finishCloneJob(new ccExtru(m_profile,m_height,&m_transformation,getName()));
}

bool ccExtru::buildUp()
{
	unsigned count = static_cast<unsigned>(m_profile.size());
	if (count < 3)
		return false;

	CCLib::Delaunay2dMesh mesh;

	//DGM: we check that last vertex is different from the first one!
	//(yes it happens ;)
	if (m_profile.back().x == m_profile.front().x &&  m_profile.back().y == m_profile.front().y)
		--count;

	char errorStr[1024];
	if (!mesh.buildMesh(m_profile,count,errorStr))
	{
		ccLog::Warning(QString("[ccPlane::buildUp] Profile triangulation failed (CClib said: '%1'").arg(errorStr));
		return false;
	}

	unsigned numberOfTriangles = mesh.size();
	int* triIndexes = mesh.getTriangleVertIndexesArray();

	if (numberOfTriangles == 0)
		return false;

	//vertices
	unsigned vertCount = 2*count;
	//faces
	unsigned faceCount = 2*numberOfTriangles+2*count;
	//faces normals
	unsigned faceNormCount = 2+count;

	if (!init(vertCount,false,faceCount,faceNormCount))
	{
		ccLog::Error("[ccPlane::buildUp] Not enough memory");
		return false;
	}

	ccPointCloud* verts = vertices();
	assert(verts);
	assert(m_triNormals);

	//bottom & top faces normals
	m_triNormals->addElement(ccNormalVectors::GetNormIndex(CCVector3(0.0,0.0,-1.0).u));
	m_triNormals->addElement(ccNormalVectors::GetNormIndex(CCVector3(0.0,0.0,1.0).u));

	//add profile vertices & normals
	for (unsigned i=0;i<count;++i)
	{
		const CCVector2& P = m_profile[i];
		verts->addPoint(CCVector3(P.x,P.y,0));
		verts->addPoint(CCVector3(P.x,P.y,m_height));

		const CCVector2& PNext = m_profile[(i+1)%count];
		CCVector2 N(-(PNext.y-P.y),PNext.x-P.x);
		N.normalize();
		m_triNormals->addElement(ccNormalVectors::GetNormIndex(CCVector3(N.x,N.y,0.0).u));
	}

	//add faces
	{
		//side faces
		{
			const int* _triIndexes = triIndexes;
			for (unsigned i=0;i<numberOfTriangles;++i,_triIndexes+=3)
			{
				addTriangle(_triIndexes[0]*2,_triIndexes[2]*2,_triIndexes[1]*2);
				addTriangleNormalIndexes(0,0,0);
				addTriangle(_triIndexes[0]*2+1,_triIndexes[1]*2+1,_triIndexes[2]*2+1);
				addTriangleNormalIndexes(1,1,1);
			}
		}

		//thickness
		{
			for (unsigned i=0;i<count;++i)
			{
				unsigned iNext = ((i+1)%count);
				addTriangle(i*2,i*2+1,iNext*2);
				addTriangleNormalIndexes(2+i,2+i,2+i);
				addTriangle(iNext*2,i*2+1,iNext*2+1);
				addTriangleNormalIndexes(2+i,2+i,2+i);
			}
		}
	}

	return true;
}

bool ccExtru::toFile_MeOnly(QFile& out) const
{
	if (!ccGenericPrimitive::toFile_MeOnly(out))
		return false;

	//parameters (dataVersion>=21)
	QDataStream outStream(&out);
	outStream << m_height;
	//profile size
	outStream << (qint32)m_profile.size();
	//profile points (2D)
	for (unsigned i=0; i<m_profile.size(); ++i)
	{
		outStream << m_profile[i].x;
		outStream << m_profile[i].y;
	}

	return true;
}

bool ccExtru::fromFile_MeOnly(QFile& in, short dataVersion, int flags)
{
	if (!ccGenericPrimitive::fromFile_MeOnly(in, dataVersion, flags))
		return false;

	//parameters (dataVersion>=21)
	QDataStream inStream(&in);
	ccSerializationHelper::CoordsFromDataStream(inStream,flags,&m_height);
	//profile size
	qint32 vertCount;
	inStream >> vertCount;
	if (vertCount)
	{
		m_profile.resize(vertCount);
		//profile points (2D)
		for (unsigned i=0; i<m_profile.size(); ++i)
		{
			ccSerializationHelper::CoordsFromDataStream(inStream,flags,m_profile[i].u,2);
		}
	}
	else
	{
		return false;
	}

	return true;
}