File: CSPrimPolyhedron.cpp

package info (click to toggle)
openems 0.0.35%2Bdfsg.1-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 6,288 kB
  • sloc: cpp: 40,259; yacc: 580; lex: 350; makefile: 258; sh: 169; ruby: 19
file content (357 lines) | stat: -rw-r--r-- 10,348 bytes parent folder | download
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
356
357
/*
*	Copyright (C) 2008-2012 Thorsten Liebig (Thorsten.Liebig@gmx.de)
*
*	This program is free software: you can redistribute it and/or modify
*	it under the terms of the GNU Lesser General Public License as published
*	by the Free Software Foundation, either version 3 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 Lesser General Public License for more details.
*
*	You should have received a copy of the GNU Lesser General Public License
*	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <sstream>
#include <iostream>
#include <limits>
#include "tinyxml.h"
#include "stdint.h"

#include "CSPrimPolyhedron.h"
#include "CSPrimPolyhedron_p.h"
#include "CSProperties.h"
#include "CSUseful.h"

void Polyhedron_Builder::operator()(HalfedgeDS &hds)
{
	// Postcondition: `hds' is a valid polyhedral surface.
	CGAL::Polyhedron_incremental_builder_3<HalfedgeDS> B( hds, true);
	B.begin_surface( m_polyhedron->m_Vertices.size(), m_polyhedron->m_Faces.size());
	typedef HalfedgeDS::Vertex   Vertex;
	typedef Vertex::Point Point;
	for (size_t n=0;n<m_polyhedron->m_Vertices.size();++n)
		B.add_vertex( Point( m_polyhedron->m_Vertices.at(n).coord[0], m_polyhedron->m_Vertices.at(n).coord[1], m_polyhedron->m_Vertices.at(n).coord[2]));

	for (size_t f=0;f<m_polyhedron->m_Faces.size();++f)
	{
		m_polyhedron->m_Faces.at(f).valid=false;
		int *first = m_polyhedron->m_Faces.at(f).vertices, *beyond = first+m_polyhedron->m_Faces.at(f).numVertex;
		if (B.test_facet(first, beyond))
		{
			B.add_facet(first, beyond);
			if (B.error())
			{
				std::cerr << "Polyhedron_Builder::operator(): Error in polyhedron construction" << std::endl;
				break;
			}
			m_polyhedron->m_Faces.at(f).valid=true;
		}
		else
		{
			std::cerr << "Polyhedron_Builder::operator(): Face " << f << ": Trying reverse order... ";
			int help[m_polyhedron->m_Faces.at(f).numVertex];
			for (unsigned int n=0;n<m_polyhedron->m_Faces.at(f).numVertex;++n)
				help[n]=m_polyhedron->m_Faces.at(f).vertices[m_polyhedron->m_Faces.at(f).numVertex-1-n];
			first = help;
			beyond = first+m_polyhedron->m_Faces.at(f).numVertex;
			if (B.test_facet(first, beyond))
			{
				B.add_facet(first, beyond);
				if (B.error())
				{
					std::cerr << "Polyhedron_Builder::operator(): Error in polyhedron construction" << std::endl;
					break;
				}
				std::cerr << "success" << std::endl;
				m_polyhedron->m_Faces.at(f).valid=true;
			}
			else
			{
				std::cerr << "failed" << std::endl;
				++m_polyhedron->m_InvalidFaces;
			}
		}
	}
	B.end_surface();
}

/*********************CSPrimPolyhedron********************************************************************/
CSPrimPolyhedron::CSPrimPolyhedron(unsigned int ID, ParameterSet* paraSet, CSProperties* prop) : CSPrimitives(ID,paraSet,prop), d_ptr(new CSPrimPolyhedronPrivate)
{
	Type = POLYHEDRON;
	PrimTypeName = "Polyhedron";
	d_ptr->m_PolyhedronTree = NULL;
	m_InvalidFaces = 0;
}

CSPrimPolyhedron::CSPrimPolyhedron(CSPrimPolyhedron* primPolyhedron, CSProperties *prop) : CSPrimitives(primPolyhedron,prop), d_ptr(new CSPrimPolyhedronPrivate)
{
	Type = POLYHEDRON;
	PrimTypeName = "Polyhedron";
	d_ptr->m_PolyhedronTree = NULL;
	m_InvalidFaces = 0;

	//copy all vertices
	for (size_t n=0;n<primPolyhedron->m_Vertices.size();++n)
		AddVertex(primPolyhedron->m_Vertices.at(n).coord);
	//copy all faces
	for (size_t n=0;n<primPolyhedron->m_Faces.size();++n)
		AddFace(primPolyhedron->m_Faces.at(n).numVertex,primPolyhedron->m_Faces.at(n).vertices);
}

CSPrimPolyhedron::CSPrimPolyhedron(ParameterSet* paraSet, CSProperties* prop) : CSPrimitives(paraSet,prop), d_ptr(new CSPrimPolyhedronPrivate)
{
	Type = POLYHEDRON;
	PrimTypeName = "Polyhedron";
	d_ptr->m_PolyhedronTree = NULL;
	m_InvalidFaces = 0;
}

CSPrimPolyhedron::~CSPrimPolyhedron()
{
	Reset();
	delete d_ptr;
}

void CSPrimPolyhedron::Reset()
{
	m_Vertices.clear();
	for (size_t n=0;n<m_Faces.size();++n)
	{
		delete[] m_Faces.at(n).vertices;
		m_Faces.at(n).vertices=NULL;
	}
	m_Faces.clear();
	d_ptr->m_Polyhedron.clear();
	d_ptr->m_PolyhedronTree = NULL;
	m_InvalidFaces = 0;
}

void CSPrimPolyhedron::AddVertex(float px, float py, float pz)
{
	vertex nv;
	nv.coord[0]=px;nv.coord[1]=py;nv.coord[2]=pz;
	m_Vertices.push_back(nv);
}

float* CSPrimPolyhedron::GetVertex(unsigned int n)
{
	if (n<m_Vertices.size())
		return m_Vertices.at(n).coord;
	return NULL;
}

void CSPrimPolyhedron::AddFace(face f)
{
	m_Faces.push_back(f);
}

void CSPrimPolyhedron::AddFace(int numVertex, int* vertices)
{
	face f;
	f.numVertex=numVertex;
	f.vertices=new int[numVertex];
	for (int n=0;n<numVertex;++n)
		f.vertices[n]=vertices[n];
	m_Faces.push_back(f);
}

void CSPrimPolyhedron::AddFace(std::vector<int> vertices)
{
	face f;
	f.numVertex=vertices.size();
	if (f.numVertex>3)
		std::cerr << __func__ << ": Warning, faces other than triangles are currently not supported for discretization, expect false results!!!" << std::endl;
	f.vertices=new int[f.numVertex];
	for (unsigned int n=0;n<f.numVertex;++n)
		f.vertices[n]=vertices.at(n);
	m_Faces.push_back(f);
}

bool CSPrimPolyhedron::BuildTree()
{
	Polyhedron_Builder builder(this);
	d_ptr->m_Polyhedron.delegate(builder);

	if (d_ptr->m_Polyhedron.is_closed())
		m_Dimension = 3;
	else
	{
		m_Dimension = 2;

		//if structure is not closed due to invalid faces, mark it as 3D
		if (m_InvalidFaces>0)
		{
			m_Dimension = 3;
			std::cerr << "CSPrimPolyhedron::BuildTree: Warning, found polyhedron has invalid faces and is not a closed surface, setting to 3D solid anyway!" << std::endl;
		}
	}

	//build tree
	delete d_ptr->m_PolyhedronTree;
#if CGAL_VERSION_NR >= CGAL_VERSION_NUMBER(4,6,0)
    d_ptr->m_PolyhedronTree = new CGAL::AABB_tree< Traits >(faces(d_ptr->m_Polyhedron).first,faces(d_ptr->m_Polyhedron).second,d_ptr->m_Polyhedron);
#else
    d_ptr->m_PolyhedronTree = new CGAL::AABB_tree< Traits >(d_ptr->m_Polyhedron.facets_begin(),d_ptr->m_Polyhedron.facets_end());
#endif

	//update local bounding box
	GetBoundBox(m_BoundBox);
	double p[3] = {m_BoundBox[1]*(1.0+(double)rand()/RAND_MAX),m_BoundBox[3]*(1.0+(double)rand()/RAND_MAX),m_BoundBox[5]*(1.0+(double)rand()/RAND_MAX)};
	d_ptr->m_RandPt = Point(p[0],p[1],p[2]);
	return true;
}

int* CSPrimPolyhedron::GetFace(unsigned int n, unsigned int &numVertices)
{
	numVertices = 0;
	if (n<m_Faces.size())
	{
		numVertices = m_Faces.at(n).numVertex;
		return m_Faces.at(n).vertices;
	}
	return NULL;
}

bool CSPrimPolyhedron::GetBoundBox(double dBoundBox[6], bool PreserveOrientation)
{
	UNUSED(PreserveOrientation); //has no orientation or preserved anyways
	m_BoundBox_CoordSys=CARTESIAN;

	if (m_Vertices.size()==0)
		return true;

	dBoundBox[0]=dBoundBox[1]=m_Vertices.at(0).coord[0];
	dBoundBox[2]=dBoundBox[3]=m_Vertices.at(0).coord[1];
	dBoundBox[4]=dBoundBox[5]=m_Vertices.at(0).coord[2];

	for (size_t n=0;n<m_Vertices.size();++n)
	{
		dBoundBox[0]=std::min(dBoundBox[0],(double)m_Vertices.at(n).coord[0]);
		dBoundBox[2]=std::min(dBoundBox[2],(double)m_Vertices.at(n).coord[1]);
		dBoundBox[4]=std::min(dBoundBox[4],(double)m_Vertices.at(n).coord[2]);
		dBoundBox[1]=std::max(dBoundBox[1],(double)m_Vertices.at(n).coord[0]);
		dBoundBox[3]=std::max(dBoundBox[3],(double)m_Vertices.at(n).coord[1]);
		dBoundBox[5]=std::max(dBoundBox[5],(double)m_Vertices.at(n).coord[2]);
	}
	return true;
}

bool CSPrimPolyhedron::IsInside(const double* Coord, double /*tol*/)
{
	if (m_Dimension<3)
		return false;

	double pos[3];
	//transform incoming coordinates into cartesian coords
	TransformCoordSystem(Coord,pos,m_MeshType,CARTESIAN);
	if (m_Transform)
		m_Transform->InvertTransform(pos,pos);

	for (unsigned int n=0;n<3;++n)
	{
		if ((m_BoundBox[2*n]>pos[n]) || (m_BoundBox[2*n+1]<pos[n])) return false;
	}

	Point p(pos[0], pos[1], pos[2]);
	Segment segment_query(p,d_ptr->m_RandPt);
	// return true for an odd number of intersections
	if ((d_ptr->m_PolyhedronTree->number_of_intersected_primitives(segment_query)%2)==1)
		return true;
	return false;
}


bool CSPrimPolyhedron::Update(std::string *ErrStr)
{
	//update local bounding box
	m_BoundBoxValid = GetBoundBox(m_BoundBox);
	return CSPrimitives::Update(ErrStr);
}

bool CSPrimPolyhedron::Write2XML(TiXmlElement &elem, bool parameterised)
{
	if (CSPrimitives::Write2XML(elem,parameterised)==false)
		return false;

	for (size_t n=0;n<m_Vertices.size();++n)
	{
		TiXmlElement vertex("Vertex");
		TiXmlText text(CombineArray2String(m_Vertices.at(n).coord,3,','));
		vertex.InsertEndChild(text);
		elem.InsertEndChild(vertex);
	}
	for (size_t n=0;n<m_Faces.size();++n)
	{
		TiXmlElement face("Face");
		TiXmlText text(CombineArray2String(m_Faces.at(n).vertices,m_Faces.at(n).numVertex,','));
		face.InsertEndChild(text);
		elem.InsertEndChild(face);
	}
	return true;
}

bool CSPrimPolyhedron::ReadFromXML(TiXmlNode &root)
{
	if (!CSPrimitives::ReadFromXML(root)) return false;
	TiXmlNode* FN=NULL;
	TiXmlText* Text=NULL;

	// read vertices
	std::vector<double> coords;
	TiXmlElement* vertex = root.FirstChildElement("Vertex");
	while (vertex)
	{
		coords.clear();
		FN = vertex->FirstChild();
		if (FN!=NULL)
		{
			Text = FN->ToText();
			if (Text!=NULL)
				coords = SplitString2Double(std::string(Text->Value()), ',');
			else
				return false;
			if (coords.size()!=3)
				return false;
			AddVertex(coords.at(0),coords.at(1),coords.at(2));
		}
		else
			return false;
		vertex = vertex->NextSiblingElement("Vertex");
	}

	// read faces
	std::vector<int> vertices;
	TiXmlElement* face = root.FirstChildElement("Face");
	while (face)
	{
		coords.clear();
		FN = face->FirstChild();
		if (FN!=NULL)
		{
			Text = FN->ToText();
			if (Text!=NULL)
				vertices = SplitString2Int(std::string(Text->Value()), ',');
			else
				return false;
			AddFace(vertices);
		}
		else
			return false;
		face = face->NextSiblingElement("Face");
	}
	return BuildTree();
}


void CSPrimPolyhedron::ShowPrimitiveStatus(std::ostream& stream)
{
	CSPrimitives::ShowPrimitiveStatus(stream);
	stream << " Number of Vertices: " << m_Vertices.size() << std::endl;
	stream << " Number of Faces: " << m_Faces.size() << std::endl;
	stream << " Number of invalid Faces: " << m_InvalidFaces << std::endl;
}