2010-09-09 19:41:14 +00:00
|
|
|
/********************************************************************************
|
|
|
|
* ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ *
|
|
|
|
* Copyright (c) 2010 Daniel Chappuis *
|
|
|
|
*********************************************************************************
|
|
|
|
* *
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy *
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal *
|
|
|
|
* in the Software without restriction, including without limitation the rights *
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is *
|
|
|
|
* furnished to do so, subject to the following conditions: *
|
|
|
|
* *
|
|
|
|
* The above copyright notice and this permission notice shall be included in *
|
|
|
|
* all copies or substantial portions of the Software. *
|
|
|
|
* *
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN *
|
|
|
|
* THE SOFTWARE. *
|
|
|
|
********************************************************************************/
|
2009-07-02 20:43:55 +00:00
|
|
|
|
|
|
|
// Libraries
|
|
|
|
#include "OBB.h"
|
2009-11-29 11:10:05 +00:00
|
|
|
#include <vector>
|
2009-07-21 20:29:03 +00:00
|
|
|
#include <GL/freeglut.h> // TODO : Remove this in the final version
|
|
|
|
#include <GL/gl.h> // TODO : Remove this in the final version
|
2009-11-29 11:10:05 +00:00
|
|
|
#include <cassert>
|
2009-07-21 20:29:03 +00:00
|
|
|
|
2009-07-02 20:43:55 +00:00
|
|
|
using namespace reactphysics3d;
|
2010-08-27 10:00:25 +00:00
|
|
|
using namespace std;
|
2009-07-02 20:43:55 +00:00
|
|
|
|
|
|
|
// Constructor
|
2009-07-03 12:15:57 +00:00
|
|
|
OBB::OBB(const Vector3D& center, const Vector3D& axis1, const Vector3D& axis2,
|
2010-08-27 10:00:25 +00:00
|
|
|
const Vector3D& axis3, double extent1, double extent2, double extent3) {
|
2009-07-03 12:15:57 +00:00
|
|
|
this->center = center;
|
2009-07-02 20:43:55 +00:00
|
|
|
|
2009-12-22 16:22:47 +00:00
|
|
|
oldAxis[0] = axis1.getUnit();
|
|
|
|
oldAxis[1] = axis2.getUnit();
|
|
|
|
oldAxis[2] = axis3.getUnit();
|
2009-07-21 20:29:03 +00:00
|
|
|
|
2009-12-22 16:22:47 +00:00
|
|
|
this->axis[0] = oldAxis[0];
|
|
|
|
this->axis[1] = oldAxis[1];
|
|
|
|
this->axis[2] = oldAxis[2];
|
2009-07-03 12:15:57 +00:00
|
|
|
|
|
|
|
this->extent[0] = extent1;
|
|
|
|
this->extent[1] = extent2;
|
|
|
|
this->extent[2] = extent3;
|
|
|
|
}
|
|
|
|
|
2009-07-02 20:43:55 +00:00
|
|
|
// Destructor
|
|
|
|
OBB::~OBB() {
|
|
|
|
|
|
|
|
}
|
2009-07-21 20:29:03 +00:00
|
|
|
|
|
|
|
// Draw the OBB (only for testing purpose)
|
|
|
|
void OBB::draw() const {
|
|
|
|
double e0 = extent[0];
|
|
|
|
double e1 = extent[1];
|
|
|
|
double e2 = extent[2];
|
|
|
|
|
|
|
|
Vector3D s1 = center + (axis[0]*e0) + (axis[1]*e1) - (axis[2]*e2);
|
|
|
|
Vector3D s2 = center + (axis[0]*e0) + (axis[1]*e1) + (axis[2]*e2);
|
|
|
|
Vector3D s3 = center - (axis[0]*e0) + (axis[1]*e1) + (axis[2]*e2);
|
|
|
|
Vector3D s4 = center - (axis[0]*e0) + (axis[1]*e1) - (axis[2]*e2);
|
|
|
|
Vector3D s5 = center + (axis[0]*e0) - (axis[1]*e1) - (axis[2]*e2);
|
|
|
|
Vector3D s6 = center + (axis[0]*e0) - (axis[1]*e1) + (axis[2]*e2);
|
|
|
|
Vector3D s7 = center - (axis[0]*e0) - (axis[1]*e1) + (axis[2]*e2);
|
|
|
|
Vector3D s8 = center - (axis[0]*e0) - (axis[1]*e1) - (axis[2]*e2);
|
|
|
|
|
|
|
|
// Draw in red
|
|
|
|
glColor3f(1.0, 0.0, 0.0);
|
|
|
|
|
|
|
|
// Draw the OBB
|
|
|
|
glBegin(GL_LINES);
|
|
|
|
glVertex3f(s1.getX(), s1.getY(), s1.getZ());
|
|
|
|
glVertex3f(s2.getX(), s2.getY(), s2.getZ());
|
|
|
|
|
|
|
|
glVertex3f(s2.getX(), s2.getY(), s2.getZ());
|
|
|
|
glVertex3f(s3.getX(), s3.getY(), s3.getZ());
|
|
|
|
|
|
|
|
glVertex3f(s3.getX(), s3.getY(), s3.getZ());
|
|
|
|
glVertex3f(s4.getX(), s4.getY(), s4.getZ());
|
|
|
|
|
|
|
|
glVertex3f(s4.getX(), s4.getY(), s4.getZ());
|
|
|
|
glVertex3f(s1.getX(), s1.getY(), s1.getZ());
|
|
|
|
|
|
|
|
glVertex3f(s5.getX(), s5.getY(), s5.getZ());
|
|
|
|
glVertex3f(s6.getX(), s6.getY(), s6.getZ());
|
|
|
|
|
|
|
|
glVertex3f(s6.getX(), s6.getY(), s6.getZ());
|
|
|
|
glVertex3f(s7.getX(), s7.getY(), s7.getZ());
|
|
|
|
|
|
|
|
glVertex3f(s7.getX(), s7.getY(), s7.getZ());
|
|
|
|
glVertex3f(s8.getX(), s8.getY(), s8.getZ());
|
|
|
|
|
|
|
|
glVertex3f(s8.getX(), s8.getY(), s8.getZ());
|
|
|
|
glVertex3f(s5.getX(), s5.getY(), s5.getZ());
|
|
|
|
|
|
|
|
glVertex3f(s1.getX(), s1.getY(), s1.getZ());
|
|
|
|
glVertex3f(s5.getX(), s5.getY(), s5.getZ());
|
|
|
|
|
|
|
|
glVertex3f(s4.getX(), s4.getY(), s4.getZ());
|
|
|
|
glVertex3f(s8.getX(), s8.getY(), s8.getZ());
|
|
|
|
|
|
|
|
glVertex3f(s3.getX(), s3.getY(), s3.getZ());
|
|
|
|
glVertex3f(s7.getX(), s7.getY(), s7.getZ());
|
|
|
|
|
|
|
|
glVertex3f(s2.getX(), s2.getY(), s2.getZ());
|
|
|
|
glVertex3f(s6.getX(), s6.getY(), s6.getZ());
|
|
|
|
|
|
|
|
glVertex3f(center.getX(), center.getY(), center.getZ());
|
2009-11-29 11:10:05 +00:00
|
|
|
glVertex3f(center.getX() + 8.0 * axis[1].getX(), center.getY() + 8.0 * axis[1].getY(), center.getZ() + 8.0 * axis[1].getZ());
|
2009-07-21 20:29:03 +00:00
|
|
|
|
|
|
|
glEnd();
|
|
|
|
}
|
2009-11-29 11:10:05 +00:00
|
|
|
|
|
|
|
// Return all the vertices that are projected at the extreme of the projection of the bouding volume on the axis.
|
2010-02-08 19:06:27 +00:00
|
|
|
// If the extreme vertices are part of a face of the OBB, the returned vertices will be ordered according to the face.
|
2010-08-27 10:00:25 +00:00
|
|
|
vector<Vector3D> OBB::getExtremeVertices(const Vector3D& directionAxis) const {
|
2009-12-23 10:06:10 +00:00
|
|
|
assert(directionAxis.length() != 0);
|
2009-11-29 11:10:05 +00:00
|
|
|
|
2010-08-27 10:00:25 +00:00
|
|
|
vector<Vector3D> extremeVertices;
|
2009-11-29 11:10:05 +00:00
|
|
|
|
2009-12-22 16:22:47 +00:00
|
|
|
// Check if the given axis is parallel to an axis on the OBB
|
2009-12-23 10:06:10 +00:00
|
|
|
if (axis[0].isParallelWith(directionAxis)) {
|
|
|
|
if (axis[0].scalarProduct(directionAxis) >= 0) { // If both axis are in the same direction
|
|
|
|
extremeVertices = getFace(0); // The extreme is the face 0
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
extremeVertices = getFace(1); // The extreme is the face 1
|
|
|
|
}
|
2009-12-22 16:22:47 +00:00
|
|
|
}
|
2009-12-23 10:06:10 +00:00
|
|
|
else if(axis[1].isParallelWith(directionAxis)) {
|
|
|
|
if (axis[1].scalarProduct(directionAxis) >= 0) { // If both axis are in the same direction
|
|
|
|
extremeVertices = getFace(2); // The extreme is the face 2
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
extremeVertices = getFace(3); // The extreme is the face 3
|
|
|
|
}
|
|
|
|
|
2009-12-22 16:22:47 +00:00
|
|
|
}
|
2009-12-23 10:06:10 +00:00
|
|
|
else if(axis[2].isParallelWith(directionAxis)) {
|
|
|
|
if (axis[2].scalarProduct(directionAxis) >= 0) { // If both axis are in the same direction
|
|
|
|
extremeVertices = getFace(4); // The extreme is the face 4
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
extremeVertices = getFace(5); // The extreme is the face 5
|
|
|
|
}
|
2009-12-22 16:22:47 +00:00
|
|
|
}
|
|
|
|
else { // The extreme is made of an unique vertex or an edge
|
2009-12-23 10:06:10 +00:00
|
|
|
double maxProjectionLength = 0.0; // Longest projection length of a vertex onto the projection axis
|
2009-12-22 16:22:47 +00:00
|
|
|
|
|
|
|
// For each vertex of the OBB
|
|
|
|
for (unsigned int i=0; i<8; ++i) {
|
|
|
|
Vector3D vertex = getVertex(i);
|
|
|
|
|
|
|
|
// Compute the projection length of the current vertex onto the projection axis
|
2009-12-23 10:06:10 +00:00
|
|
|
double projectionLength = directionAxis.scalarProduct(vertex-center) / directionAxis.length();
|
2009-12-22 16:22:47 +00:00
|
|
|
|
|
|
|
// If we found a bigger projection length
|
|
|
|
if (projectionLength > maxProjectionLength + EPSILON) {
|
|
|
|
maxProjectionLength = projectionLength;
|
|
|
|
extremeVertices.clear();
|
|
|
|
extremeVertices.push_back(vertex);
|
|
|
|
}
|
|
|
|
else if (approxEqual(projectionLength, maxProjectionLength)) {
|
|
|
|
extremeVertices.push_back(vertex);
|
|
|
|
}
|
2009-11-29 11:10:05 +00:00
|
|
|
}
|
2010-06-16 21:04:04 +00:00
|
|
|
|
|
|
|
assert(extremeVertices.size() == 1 || extremeVertices.size() == 2);
|
2009-11-29 11:10:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// An extreme should be a unique vertex, an edge or a face
|
|
|
|
assert(extremeVertices.size() == 1 || extremeVertices.size() == 2 || extremeVertices.size() == 4);
|
|
|
|
|
2009-12-12 21:28:06 +00:00
|
|
|
// Return the extreme vertices
|
|
|
|
return extremeVertices;
|
2009-11-29 11:10:05 +00:00
|
|
|
}
|
2009-12-21 22:57:36 +00:00
|
|
|
|
2010-08-05 15:06:34 +00:00
|
|
|
// Static method that computes an OBB from a set of vertices. The "center" argument corresponds to the center of the OBB
|
|
|
|
// This method allocates a new OBB object and return a pointer to the new allocated OBB object
|
2010-08-27 10:00:25 +00:00
|
|
|
OBB* OBB::computeFromVertices(const vector<Vector3D>& vertices, const Vector3D& center) {
|
2010-08-05 15:06:34 +00:00
|
|
|
// TODO : Implement this method;
|
|
|
|
return 0;
|
2010-07-19 19:21:33 +00:00
|
|
|
}
|
2010-09-01 16:37:13 +00:00
|
|
|
|
|
|
|
// Return the corresponding AABB
|
|
|
|
AABB* OBB::computeAABB() const {
|
|
|
|
double maxLength[] = {0.0, 0.0, 0.0}; // Maximum length for each of the three x,y and z axis
|
|
|
|
Vector3D vertex;
|
|
|
|
double length;
|
|
|
|
|
|
|
|
// For each vertex of the OBB
|
|
|
|
for (int i = 0; i<8; i++) {
|
|
|
|
vertex = getVertex(i) - center;
|
|
|
|
for (int j=0; j<3; j++) {
|
|
|
|
length = std::abs(vertex.getValue(j));
|
|
|
|
if (length > maxLength[j]) {
|
|
|
|
maxLength[j] = length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create and return the AABB
|
|
|
|
return new AABB(center, maxLength[0], maxLength[1], maxLength[2]);
|
|
|
|
}
|