File: ccInnerRect2DFinder.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 (183 lines) | stat: -rw-r--r-- 5,252 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
//##########################################################################
//#                                                                        #
//#                              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 "ccInnerRect2DFinder.h"

//qCC_db
#include <ccLog.h>

//system
#include <assert.h>

ccInnerRect2DFinder::ccInnerRect2DFinder()
	: m_maxArea(0)
	, m_cloud(0)
	, m_X(0)
	, m_Y(1)
{}

ccBox* ccInnerRect2DFinder::process( ccGenericPointCloud* cloud, unsigned char zDim/*=2*/ )
{
	if (!init(cloud,zDim))
		return 0;

	//Find the 'biggest' rectangle
	m_maxRect = Rect();
	m_maxArea = 0;
	findBiggestRect(m_boundingRect,0);

	ccBox* resultBox = 0;
	if (m_maxArea > 0)
	{
		ccBBox bbox = cloud->getOwnBB();
		assert(bbox.isValid());

		//box size
		CCVector3 boxDim = bbox.getDiagVec();
		boxDim.u[m_X] = static_cast<PointCoordinateType>(m_maxRect.width());
		boxDim.u[m_Y] = static_cast<PointCoordinateType>(m_maxRect.height());

		CCVector3 boxCenter = bbox.getCenter();
		boxCenter.u[m_X] = static_cast<PointCoordinateType>((m_maxRect.x0 + m_maxRect.x1)/2);
		boxCenter.u[m_Y] = static_cast<PointCoordinateType>((m_maxRect.y0 + m_maxRect.y1)/2);

		ccGLMatrix shiftMat;
		shiftMat.setTranslation(boxCenter);
		resultBox = new ccBox(boxDim,&shiftMat,"Biggest rect");
	}

	return resultBox;
}

bool ccInnerRect2DFinder::init(ccGenericPointCloud* cloud, unsigned char zDim)
{
	if (!cloud || cloud->size() == 0)
	{
		ccLog::Error("[ccInnerRect2DFinder] Invalid input cloud");
		return false;
	}

	ccBBox bbox = cloud->getOwnBB();
	if (!bbox.isValid())
	{
		ccLog::Error("[ccInnerRect2DFinder] Invalid input cloud");
		return false;
	}

	if (zDim > 2)
	{
		ccLog::Error("[ccInnerRect2DFinder] Invalid input parameter (zDim)");
		return false;
	}

	unsigned char Z = zDim;
	unsigned char X = ((Z+1) % 3);
	unsigned char Y = ((X+1) % 3);

	m_cloud = cloud;
	m_X = X;
	m_Y = Y;

	//init bounding rectangle
	{
		const CCVector3* P0 = m_cloud->getPoint(0);
		m_boundingRect = Rect(P0->u[m_X],P0->u[m_Y],P0->u[m_X],P0->u[m_Y]);

		unsigned pointCloud = m_cloud->size();
		for (unsigned i=1; i<pointCloud; ++i)
		{
			const CCVector3* P = m_cloud->getPoint(i);
			if (P->u[m_X] < m_boundingRect.x0)
				m_boundingRect.x0 = P->u[m_X];
			else if (P->u[m_X] > m_boundingRect.x1)
				m_boundingRect.x1 = P->u[m_X];

			if (P->u[m_Y] < m_boundingRect.y0)
				m_boundingRect.y0 = P->u[m_Y];
			else if (P->u[m_Y] > m_boundingRect.y1)
				m_boundingRect.y1 = P->u[m_Y];
		}
	}

	return true;
}

void ccInnerRect2DFinder::findBiggestRect(const Rect& rect, unsigned startIndex)
{
	assert(m_cloud);

	//test if at least one point falls inside the input rectangle
	const CCVector3* Pinside = 0;
	{
		unsigned pointCount = m_cloud->size();
		double minSquareDistToCenter = -1.0;
		double xc = (rect.x0+rect.x1)/2;
		double yc = (rect.y0+rect.y1)/2;
		for (unsigned i=startIndex; i<pointCount; ++i)
		{
			const CCVector3* P = m_cloud->getPoint(i);
			if (	P->u[m_X] > rect.x0 && P->u[m_X] < rect.x1	//strict inequalities!
				&&	P->u[m_Y] > rect.y0 && P->u[m_Y] < rect.y1 )
			{
				double dist2 = (xc - P->u[m_X])*(xc - P->u[m_X]) + (yc - P->u[m_Y])*(yc - P->u[m_Y]);
				if (minSquareDistToCenter < 0)
				{
					Pinside = P;
					minSquareDistToCenter = dist2;
					startIndex = i;
				}
				else if (dist2 < minSquareDistToCenter)
				{
					Pinside = P;
					minSquareDistToCenter = dist2;
				}
				//break;
			}
		}
	}

	//do we have an empty rectangle?
	if (!Pinside)
	{
		//we remember it only if its size is bigger
		double surf = rect.area();
		if (surf > m_maxArea)
		{
			m_maxArea = surf;
			m_maxRect = rect;
		}
	}
	else //otherwise we test the 4 sub-rectangles
	{
		//left sub-rectangle
		Rect r(rect.x0,rect.y0,Pinside->u[m_X],rect.y1);
		if (r.area() > m_maxArea)
			findBiggestRect(r,startIndex);
		//right sub-rectangle
		r = Rect(Pinside->u[m_X],rect.y0,rect.x1,rect.y1);
		if (r.area() > m_maxArea)
			findBiggestRect(r,startIndex);
		//upper sub-rectangle
		r = Rect(rect.x0,rect.y0,rect.x1,Pinside->u[m_Y]);
		if (r.area() > m_maxArea)
			findBiggestRect(r,startIndex);
		//lower sub-rectangle
		r = Rect(rect.x0,Pinside->u[m_Y],rect.x1,rect.y1);
		if (r.area() > m_maxArea)
			findBiggestRect(r,startIndex);
	}
}