15 #include "boundingHexahedron.h" 16 #include "boundingSphere.h" 17 #include "boundingBox.h" 18 #include "config_mathutil.h" 31 BoundingHexahedron(
const LFrustum &frustum,
bool is_ortho,
32 CoordinateSystem cs) {
33 if (cs == CS_default) {
34 cs = get_default_coordinate_system();
37 PN_stdfloat fs = 1.0f;
39 fs = frustum._ffar / frustum._fnear;
45 _points[0].set(frustum._l * fs, frustum._ffar, frustum._b * fs);
46 _points[1].set(frustum._r * fs, frustum._ffar, frustum._b * fs);
47 _points[2].set(frustum._r * fs, frustum._ffar, frustum._t * fs);
48 _points[3].set(frustum._l * fs, frustum._ffar, frustum._t * fs);
49 _points[4].set(frustum._l, frustum._fnear, frustum._b);
50 _points[5].set(frustum._r, frustum._fnear, frustum._b);
51 _points[6].set(frustum._r, frustum._fnear, frustum._t);
52 _points[7].set(frustum._l, frustum._fnear, frustum._t);
57 if (cs == CS_zup_right) {
110 for (i = 1; i < num_points; i++) {
111 m.set(min(m[0], _points[i][0]),
112 min(m[1], _points[i][1]),
113 min(m[2], _points[i][2]));
129 for (i = 1; i < num_points; i++) {
130 m.set(max(m[0], _points[i][0]),
131 max(m[1], _points[i][1]),
132 max(m[2], _points[i][2]));
143 get_approx_center()
const {
154 void BoundingHexahedron::
157 for (
int i = 0; i < num_points; i++) {
158 _points[i] = _points[i] * mat;
170 void BoundingHexahedron::
171 output(ostream &out)
const {
173 out <<
"bhexahedron, empty";
175 out <<
"bhexahedron, infinite";
177 out <<
"bhexahedron, min " << get_min() <<
" max " << get_max();
186 void BoundingHexahedron::
187 write(ostream &out,
int indent_level)
const {
189 indent(out, indent_level) <<
"bhexahedron, empty\n";
191 out <<
"bhexahedron, infinite\n";
193 indent(out, indent_level)
194 <<
"bhexahedron, min " << get_min() <<
" max " << get_max() <<
":\n";
196 for (i = 0; i < num_points; i++) {
197 indent(out, indent_level + 2) << _points[i] <<
"\n";
199 indent(out, indent_level + 2) <<
"centroid is " << _centroid <<
"\n";
220 bool BoundingHexahedron::
222 return other->extend_by_hexahedron(
this);
230 bool BoundingHexahedron::
234 return other->around_hexahedrons(first, last);
242 int BoundingHexahedron::
244 return other->contains_hexahedron(
this);
252 int BoundingHexahedron::
253 contains_point(
const LPoint3 &point)
const {
255 return IF_no_intersection;
258 return IF_possible | IF_some | IF_all;
263 for (
int i = 0; i < num_planes; i++) {
264 const LPlane &p = _planes[i];
265 if (p.dist_to_plane(point) > 0.0f) {
266 return IF_no_intersection;
269 return IF_possible | IF_some | IF_all;
278 int BoundingHexahedron::
281 return IF_no_intersection;
284 return IF_possible | IF_some | IF_all;
289 for (
int i = 0; i < num_planes; i++) {
290 const LPlane &p = _planes[i];
291 if (p.dist_to_plane(a) > 0.0f ||
292 p.dist_to_plane(b) > 0.0f) {
293 return IF_no_intersection;
309 int BoundingHexahedron::
315 const LPoint3 ¢er = sphere->get_center();
316 PN_stdfloat radius = sphere->get_radius();
318 int result = IF_possible | IF_some | IF_all;
320 for (
int i = 0; i < num_planes; i++) {
321 const LPlane &p = _planes[i];
322 PN_stdfloat dist = p.dist_to_plane(center);
327 return IF_no_intersection;
329 }
else if (dist > -radius) {
344 int BoundingHexahedron::
352 LPoint3 center = (min + max) * 0.5f;
353 PN_stdfloat radius2 = (max - center).length_squared();
355 int result = IF_possible | IF_some | IF_all;
357 for (
int i = 0; i < num_planes; i++) {
358 const LPlane &p = _planes[i];
359 PN_stdfloat dist = p.dist_to_plane(center);
360 PN_stdfloat dist2 = dist * dist;
362 if (dist2 <= radius2) {
369 for (
int i = 0; i < 8 && (all_in || all_out) ; ++i) {
370 if (p.dist_to_plane(box->
get_point(i)) < 0.0f) {
380 return IF_no_intersection;
381 }
else if (!all_in) {
385 }
else if (dist >= 0.0f) {
387 return IF_no_intersection;
399 int BoundingHexahedron::
402 nassertr(!hexahedron->
is_empty(), 0);
405 LPoint3 min = hexahedron->get_min();
406 LPoint3 max = hexahedron->get_max();
407 LPoint3 center = (min + max) * 0.5f;
408 PN_stdfloat radius2 = (max - center).length_squared();
410 int result = IF_possible | IF_some | IF_all;
412 for (
int i = 0; i < num_planes; i++) {
413 const LPlane &p = _planes[i];
414 PN_stdfloat dist = p.dist_to_plane(center);
415 PN_stdfloat dist2 = dist * dist;
417 if (dist >= 0.0f && dist2 > radius2) {
420 return IF_no_intersection;
427 unsigned points_out = 0;
428 for (
int i = 0; i < 8; ++i) {
429 if (p.dist_to_plane(hexahedron->
get_point(i)) > 0.0f) {
435 if (points_out != 0) {
436 if (points_out == 8) {
437 return IF_no_intersection;
452 void BoundingHexahedron::
454 _planes[0] = LPlane(_points[0], _points[3], _points[2]);
460 if (_planes[0].dist_to_plane(_centroid) > 0) {
463 _planes[0] = LPlane(_points[0], _points[2], _points[3]);
464 _planes[1] = LPlane(_points[0], _points[5], _points[1]);
465 _planes[2] = LPlane(_points[1], _points[6], _points[2]);
466 _planes[3] = LPlane(_points[2], _points[7], _points[3]);
467 _planes[4] = LPlane(_points[3], _points[4], _points[0]);
468 _planes[5] = LPlane(_points[4], _points[7], _points[6]);
472 _planes[1] = LPlane(_points[0], _points[1], _points[5]);
473 _planes[2] = LPlane(_points[1], _points[2], _points[6]);
474 _planes[3] = LPlane(_points[2], _points[3], _points[7]);
475 _planes[4] = LPlane(_points[3], _points[0], _points[4]);
476 _planes[5] = LPlane(_points[4], _points[6], _points[7]);
482 nassertv(_planes[0].dist_to_plane(_centroid) <= 0.001);
483 nassertv(_planes[1].dist_to_plane(_centroid) <= 0.001);
484 nassertv(_planes[2].dist_to_plane(_centroid) <= 0.001);
485 nassertv(_planes[3].dist_to_plane(_centroid) <= 0.001);
486 nassertv(_planes[4].dist_to_plane(_centroid) <= 0.001);
487 nassertv(_planes[5].dist_to_plane(_centroid) <= 0.001);
496 void BoundingHexahedron::
499 for (
int i = 1; i < num_points; i++) {
502 _centroid = net / (PN_stdfloat)num_points;
LPoint3 get_point(int n) const
Returns the nth vertex of the hexahedron.
An axis-aligned bounding box; that is, a minimum and maximum coordinate triple.
bool is_empty() const
Any kind of volume might be empty.
This defines a bounding sphere, consisting of a center and a radius.
bool is_infinite() const
The other side of the empty coin is an infinite volume.
This is a three-component point in space (as opposed to a three-component vector, which represents a ...
static const LMatrix4f & convert_mat(CoordinateSystem from, CoordinateSystem to)
Returns a matrix that transforms from the indicated coordinate system to the indicated coordinate sys...
This is an abstract class for any volume in any sense which can be said to define the locality of ref...
This is a 4-by-4 transform matrix.
const LPoint3 & get_maxq() const
An inline accessor for the maximum value.
LPoint3 get_point(int n) const
Returns the nth vertex of the rectangular solid.
virtual const BoundingHexahedron * as_bounding_hexahedron() const
Virtual downcast method.
TypeHandle is the identifier used to differentiate C++ class types.
This defines a bounding convex hexahedron.
const LPoint3 & get_minq() const
An inline accessor for the minimum value.