File: test.cpp

package info (click to toggle)
mrpt 1%3A2.5.8%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 87,448 kB
  • sloc: cpp: 551,662; ansic: 38,702; xml: 3,914; python: 2,547; sh: 404; makefile: 237
file content (243 lines) | stat: -rw-r--r-- 7,852 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
/* +------------------------------------------------------------------------+
   |                     Mobile Robot Programming Toolkit (MRPT)            |
   |                          https://www.mrpt.org/                         |
   |                                                                        |
   | Copyright (c) 2005-2023, Individual contributors, see AUTHORS file     |
   | See: https://www.mrpt.org/Authors - All rights reserved.               |
   | Released under BSD License. See: https://www.mrpt.org/License          |
   +------------------------------------------------------------------------+ */

#include <mrpt/gui/CDisplayWindow3D.h>
#include <mrpt/math/TObject3D.h>
#include <mrpt/opengl/CAngularObservationMesh.h>
#include <mrpt/opengl/CAxis.h>
#include <mrpt/opengl/CGridPlaneXY.h>
#include <mrpt/opengl/CPolyhedron.h>

#include <iostream>

using namespace std;
using namespace mrpt;
using namespace mrpt::gui;
using namespace mrpt::opengl;
using namespace mrpt::poses;
using namespace mrpt::math;
using mrpt::opengl::CAngularObservationMesh;
using namespace mrpt::system;

const double GRID_R = 1.0;
const double GRID_G = 1.0;
const double GRID_B = 1.0;

// Comment to increase randomness of poses
#define PAIRED_RANDOM_POSES

#define USE_THREADS

inline double MYRAND1(size_t prec = 64)
{
	return static_cast<double>(rand() % prec) / static_cast<double>(prec - 1);
}

void randomColor(const CRenderizable::Ptr& obj, double alpha)
{
	obj->setColor(MYRAND1(), MYRAND1(), MYRAND1(), alpha);
}

#ifdef USE_THREADS
class PIThreadParam
{
   public:
	const pair<CPolyhedron::Ptr, CPolyhedron::Ptr>* polys{nullptr};
	vector<TSegment3D> intersection;
	PIThreadParam(const pair<CPolyhedron::Ptr, CPolyhedron::Ptr>& p)
		: polys(&p), intersection()
	{
	}
	PIThreadParam() : intersection() {}
	inline static PIThreadParam createObject(
		const pair<CPolyhedron::Ptr, CPolyhedron::Ptr>& p)
	{
		return PIThreadParam(p);
	}
};

void piThreadFunction(PIThreadParam& p)
{
	vector<TObject3D> ints;
	CPolyhedron::getIntersection(p.polys->first, p.polys->second, ints);
	for (const auto& o : ints)
		if (o.isSegment()) p.intersection.emplace_back(o.getAs<TSegment3D>());
}

inline std::thread piCreateThread(PIThreadParam& p)
{
	return std::thread(&piThreadFunction, std::ref(p));
}

class AggregatorFunctor
{
   public:
	vector<TSegment3D>& sgms;
	AggregatorFunctor(vector<TSegment3D>& s) : sgms(s) {}
	inline void operator()(const PIThreadParam& p)
	{
		sgms.insert(sgms.end(), p.intersection.begin(), p.intersection.end());
	}
};
#endif

CSetOfLines::Ptr getIntersections(
	const vector<pair<CPolyhedron::Ptr, CPolyhedron::Ptr>>& v)
{
	vector<TSegment3D> sgms;
#ifdef USE_THREADS
	vector<PIThreadParam> pars(v.size());
	vector<std::thread> threads(v.size());
	transform(v.begin(), v.end(), pars.begin(), &PIThreadParam::createObject);
	transform(pars.begin(), pars.end(), threads.begin(), &piCreateThread);
	for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
	for_each(pars.begin(), pars.end(), AggregatorFunctor(sgms));
#else
	vector<TObject3D> ints, TMP;
	for (vector<pair<CPolyhedron::Ptr, CPolyhedron::Ptr>>::const_iterator it =
			 v.begin();
		 it != v.end(); ++it)
	{
		CPolyhedron::getIntersection(it->first, it->second, TMP);
		ints.insert(ints.end(), TMP.begin(), TMP.end());
	}
	TObject3D::getSegments(ints, sgms);
#endif
	CSetOfLines::Ptr lns = CSetOfLines::Create(sgms);
	lns->setLineWidth(9);
	randomColor(lns, 1.0);
	return lns;
}

inline double randomAngle(size_t prec = 64) { return MYRAND1(prec) * M_PI; }
inline double randomZ(double space = 25, size_t prec = 64)
{
	return space * (MYRAND1(prec) - 0.5);
}

pair<CPolyhedron::Ptr, CPolyhedron::Ptr> addPairOfPolys(
	CPolyhedron::Ptr p1, CPolyhedron::Ptr p2, CSetOfObjects::Ptr& objs,
	double x, double y)
{
	p1->makeConvexPolygons();
	p2->makeConvexPolygons();
#ifdef PAIRED_RANDOM_POSES
	CPose3D pose =
		CPose3D(x, y, randomZ(), randomAngle(), randomAngle(), randomAngle());
	p1->setPose(pose);
	p2->setPose(pose);
#else
	CPose3D pose1 =
		CPose3D(x, y, randomZ(), randomAngle(), randomAngle(), randomAngle());
	CPose3D pose2 =
		CPose3D(x, y, randomZ(), randomAngle(), randomAngle(), randomAngle());
	p1->setPose(pose1);
	p2->setPose(pose2);
#endif
	randomColor(p1, 0.5);
	randomColor(p2, 0.5);
	objs->insert(p1);
	objs->insert(p2);
	return make_pair(p1, p2);
}

void display()
{
	CDisplayWindow3D window("Polyhedra Intersection demo", 640, 480);
	window.resize(640, 480);
	COpenGLScene::Ptr scene1 = COpenGLScene::Create();
	opengl::CGridPlaneXY::Ptr plane1 =
		CGridPlaneXY::Create(-25, 25, -25, 25, 0, 1);
	plane1->setColor(GRID_R, GRID_G, GRID_B);
	scene1->insert(plane1);
	scene1->insert(CAxis::Create(-5, -5, -5, 5, 5, 5, 2.5, 3, true));
	CSetOfObjects::Ptr objs = CSetOfObjects::Create();
	vector<pair<CPolyhedron::Ptr, CPolyhedron::Ptr>> polys;
	polys.reserve(16);
	// Addition of polyhedra. Add more polyhedra at wish, but try to avoid
	// intersections with other pairs, for better visualization.
	polys.push_back(addPairOfPolys(
		CPolyhedron::CreateHexahedron(10), CPolyhedron::CreateOctahedron(10),
		objs, -12.5, -12.5));
	polys.push_back(addPairOfPolys(
		CPolyhedron::CreateIcosahedron(10), CPolyhedron::CreateDodecahedron(10),
		objs, -12.5, 12.5));
	polys.push_back(addPairOfPolys(
		CPolyhedron::CreateRhombicuboctahedron(10),
		CPolyhedron::CreateCuboctahedron(10), objs, 12.5, 12.5));
	polys.push_back(addPairOfPolys(
		CPolyhedron::CreateArchimedeanRegularAntiprism(4, 9),
		CPolyhedron::CreateRegularDoublePyramid(9, 10, 15, 6), objs, 12.5,
		-12.5));
	polys.push_back(addPairOfPolys(
		CPolyhedron::CreateCuboctahedron(10),
		CPolyhedron::CreateRhombicDodecahedron(10), objs, -37.5, -37.5));
	polys.push_back(addPairOfPolys(
		CPolyhedron::CreateRhombicuboctahedron(10),
		CPolyhedron::CreateDeltoidalIcositetrahedron(10), objs, -37.5, -12.5));
	polys.push_back(addPairOfPolys(
		CPolyhedron::CreateIcosidodecahedron(10),
		CPolyhedron::CreateRhombicTriacontahedron(10), objs, -37.5, 12.5));
	polys.push_back(addPairOfPolys(
		CPolyhedron::CreateRhombicosidodecahedron(10),
		CPolyhedron::CreateDeltoidalHexecontahedron(10), objs, -37.5, 37.5));
	polys.push_back(addPairOfPolys(
		CPolyhedron::CreateTruncatedTetrahedron(10),
		CPolyhedron::CreateTriakisTetrahedron(10), objs, -12.5, -37.5));
	polys.push_back(addPairOfPolys(
		CPolyhedron::CreateTruncatedHexahedron(10),
		CPolyhedron::CreateTriakisOctahedron(10), objs, -12.5, 37.5));
	polys.push_back(addPairOfPolys(
		CPolyhedron::CreateTruncatedOctahedron(10),
		CPolyhedron::CreateTetrakisHexahedron(10), objs, 12.5, -37.5));
	polys.push_back(addPairOfPolys(
		CPolyhedron::CreateTruncatedDodecahedron(10),
		CPolyhedron::CreateTriakisIcosahedron(10), objs, 12.5, 37.5));
	polys.push_back(addPairOfPolys(
		CPolyhedron::CreateTruncatedIcosahedron(10),
		CPolyhedron::CreatePentakisDodecahedron(10), objs, 37.5, -37.5));
	polys.push_back(addPairOfPolys(
		CPolyhedron::CreateRandomPolyhedron(10),
		CPolyhedron::CreateRandomPolyhedron(10), objs, 37.5, -12.5));
	polys.push_back(addPairOfPolys(
		CPolyhedron::CreateDodecahedron(10),
		CPolyhedron::CreateDeltoidalHexecontahedron(10), objs, 37.5, 12.5));
	polys.push_back(addPairOfPolys(
		CPolyhedron::CreateTriakisIcosahedron(10),
		CPolyhedron::CreatePentakisDodecahedron(10), objs, 37.5, 37.5));
	objs << getIntersections(polys);

	scene1->insert(objs);
	window.get3DSceneAndLock() = scene1;
	window.unlockAccess3DScene();
	window.setCameraElevationDeg(25.0f);
	window.forceRepaint();
	window.waitForKey();
}

int main()
{
	srand((unsigned int)mrpt::system::extractDayTimeFromTimestamp(
		mrpt::system::now()));
	try
	{
		display();
		return 0;
	}
	catch (exception& e)
	{
		cout << "Error: " << e.what() << '.' << endl;
		return -1;
	}
	catch (...)
	{
		cout << "Unknown Error.\n";
		return -1;
	}
}