summaryrefslogtreecommitdiff
path: root/cad/plugins/NanoVision-1/src/Plugins/RenderingEngines/OpenGL/GLT/bbox.cpp
blob: 3483bdd8cb32fc4bcbdcd06edca64be53693c867 (plain)
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
#include "glt_bbox.h"

/*! \file 
	\ingroup Math
*/

#include <iostream>
using namespace std;

#include "glt_matrix4.h"
#include "glt_viewport.h"

BoundingBox::BoundingBox()
: _defined(false)
{
}

BoundingBox::BoundingBox(const Vector &min,const Vector &max)
: _defined(true), _min(min), _max(max)
{
}

      bool   &BoundingBox::defined()       { return _defined; }
const bool    BoundingBox::defined() const { return _defined; }

      Vector &BoundingBox::min()           { return _min;     }
const Vector &BoundingBox::min() const     { return _min;     }
      Vector &BoundingBox::max()           { return _max;     }
const Vector &BoundingBox::max() const     { return _max;     }

Vector 
BoundingBox::center() const
{
	return (_min+_max)*0.5;
}

real BoundingBox::width()  const { return _max[0] - _min[0]; }
real BoundingBox::height() const { return _max[1] - _min[1]; }
real BoundingBox::depth()  const { return _max[2] - _min[2]; }

void 
BoundingBox::points(std::vector<Vector> &p) const
{
	p.clear();
	p.reserve(8);
	p.push_back(Vector(_min.x(), _min.y(), _min.z()));
	p.push_back(Vector(_min.x(), _min.y(), _max.z()));
	p.push_back(Vector(_min.x(), _max.y(), _min.z()));
	p.push_back(Vector(_min.x(), _max.y(), _max.z()));
	p.push_back(Vector(_max.x(), _min.y(), _min.z()));
	p.push_back(Vector(_max.x(), _min.y(), _max.z()));
	p.push_back(Vector(_max.x(), _max.y(), _min.z()));
	p.push_back(Vector(_max.x(), _max.y(), _max.z()));
}

void 
BoundingBox::reset()
{
	_defined = false;
}

BoundingBox &
BoundingBox::operator+=(const Vector &p)
{
	if (!_defined)
	{
		_min = _max = p;
		_defined = true;
	}
	else
	{
		_min.vmin(p);
		_max.vmax(p);
	}

	return *this;
}

BoundingBox &
BoundingBox::operator+=(const vector<Vector> &p)
{
	if (!p.size())
		return *this;

	uint32 i = 0;

	if (!_defined)
	{
		_min = _max = p.front();
		_defined = true;
		i = 1;
	}

	for (; i<p.size(); i++)
	{
		_min.vmin(p[i]);
		_max.vmax(p[i]);
	}

	return *this;
}

BoundingBox &
BoundingBox::operator+=(const BoundingBox &box)
{
	if (!_defined && box._defined)
	{
		_min = box._min;
		_max = box._max;
		_defined = true;
		return *this;
	}

	if (_defined && box._defined)
	{
		_min.vmin(box._min);
		_max.vmax(box._max);
	}

	return *this;
}

BoundingBox &
BoundingBox::operator*=(const BoundingBox &box)
{
	if (_defined && box._defined)
	{
		_min.vmax(box._min);
		_max.vmin(box._max);
		
		if 
		(
			_min.x()>_max.x() ||
			_min.y()>_max.y() ||
			_min.z()>_max.z()
		)
			_defined = false;
	}
	else
		_defined = false;

	return *this;
}

bool 
BoundingBox::operator==(const BoundingBox &box) const
{
	if (!_defined && !box._defined)
		return true;

	if (_defined && box._defined)
		return _min==box._min && _max==box._max;
	else
		return false;
}

bool 
BoundingBox::inside(const Vector &pos) const
{
	if (pos.x()<_min.x() || pos.x()>_max.x()) return false;
	if (pos.y()<_min.y() || pos.y()>_max.y()) return false;
	if (pos.z()<_min.z() || pos.z()>_max.z()) return false;

	return true;
}

bool 
BoundingBox::intersects(const BoundingBox &box) const
{
	if (!_defined || !box._defined)
		return false;

	if (_min.x() > box._max.x()) return false;
	if (_min.y() > box._max.y()) return false;
	if (_min.z() > box._max.z()) return false;

	if (_max.x() < box._min.x()) return false;
	if (_max.y() < box._min.y()) return false;
	if (_max.z() < box._min.z()) return false;

	return true;
}

bool
BoundingBox::project(const GltMatrix &model,const GltMatrix &proj,const GltViewport &view)
{
	bool ok = true;

	vector<Vector> pnt;
	points(pnt);

	reset();
        for (int j=0; j<(int)pnt.size(); j++)
	{
		ok &= pnt[j].project(model,proj,view);
		operator+=(pnt[j]);
	}

	return ok;
}

ostream &
operator<<(ostream &os, const BoundingBox &b)
{
	os << b.min() << " - " << b.max();
	return os;
}

BoundingBox sum(const BoundingBox &a,const BoundingBox &b)
{
	BoundingBox tmp(a);
	tmp += b;
	return tmp;
}

BoundingBox intersection(const BoundingBox &a,const BoundingBox &b)
{
	BoundingBox tmp(a);
	tmp *= b;
	return tmp;
}