source: branches/blt4/packages/vizservers/nanovis/vrmath/BBox.cpp @ 3892

Last change on this file since 3892 was 3892, checked in by gah, 11 years ago
File size: 3.4 KB
Line 
1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 * Copyright (c) 2004-2013  HUBzero Foundation, LLC
4 *
5 * Author: Insoo Woo <iwoo@purdue.edu>
6 */
7
8#include <float.h>
9
10#include <vrmath/BBox.h>
11#include <vrmath/Matrix4x4d.h>
12#include <vrmath/Vector3f.h>
13#include <vrmath/Vector4f.h>
14
15using namespace vrmath;
16
17BBox::BBox()
18{
19    makeEmpty();
20}
21
22BBox::BBox(const BBox& other) :
23    min(other.min),
24    max(other.max)
25{
26}
27
28BBox::BBox(const Vector3f& minv, const Vector3f& maxv) :
29    min(minv),
30    max(maxv)
31{
32}
33
34void BBox::makeEmpty()
35{
36    min.set(FLT_MAX, FLT_MAX, FLT_MAX);
37    max.set(-FLT_MAX, -FLT_MAX, -FLT_MAX);
38}
39
40bool BBox::isEmpty()
41{
42    return (isEmptyX() && isEmptyY() && isEmptyZ());
43}
44
45bool BBox::isEmptyX()
46{
47    return (min.x > max.x);
48}
49
50bool BBox::isEmptyY()
51{
52    return (min.y > max.y);
53}
54
55bool BBox::isEmptyZ()
56{
57    return (min.z > max.z);
58}
59
60void BBox::make(const Vector3f& center, const Vector3f& size)
61{
62    float halfX = size.x * 0.5f, halfY = size.y * 0.5f, halfZ = size.z * 0.5f;
63
64    min.x = center.x - halfX;
65    max.x = center.x + halfX;
66
67    min.y = center.y - halfY;
68    max.y = center.y + halfY;
69
70    min.z = center.z - halfZ;
71    max.z = center.z + halfZ;
72}
73
74void BBox::extend(const Vector3f& point)
75{
76    if (min.x > point.x) {
77        min.x = point.x;
78    } else if (max.x < point.x) {
79        max.x = point.x;
80    }
81
82    if (min.y > point.y) {
83        min.y = point.y;
84    } else if (max.y < point.y) {
85        max.y = point.y;
86    }
87
88    if (min.z > point.z) {
89        min.z = point.z;
90    } else if (max.z < point.z) {
91        max.z = point.z;
92    }
93}
94
95void BBox::extend(const BBox& box)
96{
97    if (min.x > box.min.x) min.x = box.min.x;
98    if (min.y > box.min.y) min.y = box.min.y;
99    if (min.z > box.min.z) min.z = box.min.z;
100
101    if (max.x < box.max.x) max.x = box.max.x;
102    if (max.y < box.max.y) max.y = box.max.y;
103    if (max.z < box.max.z) max.z = box.max.z;
104}
105
106void BBox::transform(const BBox& box, const Matrix4x4d& mat)
107{
108    float x0 = box.min.x;
109    float y0 = box.min.y;
110    float z0 = box.min.z;
111    float x1 = box.max.x;
112    float y1 = box.max.y;
113    float z1 = box.max.z;
114
115    Vector4f points[8];
116
117    points[0].set(x0, y0, z0, 1);
118    points[1].set(x1, y0, z0, 1);
119    points[2].set(x0, y1, z0, 1);
120    points[3].set(x0, y0, z1, 1);
121    points[4].set(x1, y1, z0, 1);
122    points[5].set(x1, y0, z1, 1);
123    points[6].set(x0, y1, z1, 1);
124    points[7].set(x1, y1, z1, 1);
125
126    float minX = FLT_MAX, minY = FLT_MAX, minZ = FLT_MAX;
127    float maxX = -FLT_MAX, maxY = -FLT_MAX, maxZ = -FLT_MAX;
128
129    for (int i = 0; i < 8; i++) {
130        points[i] = mat.transform(points[i]);
131
132        if (points[i].x > maxX) maxX = points[i].x;
133        if (points[i].x < minX) minX = points[i].x;
134        if (points[i].y > maxY) maxY = points[i].y;
135        if (points[i].y < minY) minY = points[i].y;
136        if (points[i].z > maxZ) maxZ = points[i].z;
137        if (points[i].z < minZ) minZ = points[i].z;
138    }
139
140    min.set(minX, minY, minZ);
141    max.set(maxX, maxY, maxZ);
142}
143
144bool BBox::intersect(const BBox& box)
145{
146    if (min.x > box.max.x || max.x < box.min.x) return false;
147    if (min.y > box.max.y || max.y < box.min.y) return false;
148    if (min.z > box.max.z || max.z < box.min.z) return false;
149
150    return true;
151}
152
153bool BBox::contains(const Vector3f& point)
154{
155    if ((point.x < min.x) || (point.x > max.x)) return false;
156    if ((point.y < min.y) || (point.y > max.y)) return false;
157    if ((point.z < min.z) || (point.z > max.z)) return false;
158
159    return true;
160}
Note: See TracBrowser for help on using the repository browser.