bounding_box.cpp
Go to the documentation of this file.
00001 #include "tango-gl/bounding_box.h"
00002 
00003 namespace tango_gl {
00004 BoundingBox::BoundingBox(const std::vector<float>& vertices) {
00005   // Set min and max to the first vertice.
00006   bounding_min_ = glm::vec3(vertices[0], vertices[1], vertices[2]);
00007   bounding_max_ = bounding_min_;
00008   size_t vertices_count = vertices.size() / 3;
00009   for (size_t i = 1; i < vertices_count; i += 3) {
00010     bounding_min_.x = std::min(vertices[i * 3], bounding_min_.x);
00011     bounding_min_.y = std::min(vertices[i * 3 + 1], bounding_min_.y);
00012     bounding_min_.z = std::min(vertices[i * 3 + 2], bounding_min_.z);
00013 
00014     bounding_max_.x = std::max(vertices[i * 3], bounding_max_.x);
00015     bounding_max_.y = std::max(vertices[i * 3 + 1], bounding_max_.y);
00016     bounding_max_.z = std::max(vertices[i * 3 + 2], bounding_max_.z);
00017   }
00018 }
00019 
00020 bool BoundingBox::IsIntersecting(const Segment& segment,
00021                                  const glm::quat& rotation,
00022                                  const glm::mat4& transformation) {
00023   // The current bounding box.
00024   glm::vec3 min, max;
00025 
00026   // If the mesh has been rotated, we need to derive a new bounding box
00027   // based on the original one, if it just been translated or scaled,
00028   // we can still use the original one with current model matrix applied.
00029   if (rotation == glm::quat(1.0f, 0.0f, 0.0f, 0.0f)) {
00030     min = util::ApplyTransform(transformation, bounding_min_);
00031     max = util::ApplyTransform(transformation, bounding_max_);
00032   } else {
00033     std::vector<glm::vec3> box;
00034     // Derive 8 vertices of the new bounding box from original min and max.
00035     box.push_back(bounding_min_);
00036     box.push_back(bounding_max_);
00037 
00038     box.push_back(glm::vec3(bounding_min_.x, bounding_max_.y, bounding_max_.z));
00039     box.push_back(glm::vec3(bounding_max_.x, bounding_min_.y, bounding_min_.z));
00040 
00041     box.push_back(glm::vec3(bounding_min_.x, bounding_min_.y, bounding_max_.z));
00042     box.push_back(glm::vec3(bounding_max_.x, bounding_max_.y, bounding_min_.z));
00043 
00044     box.push_back(glm::vec3(bounding_max_.x, bounding_min_.y, bounding_max_.z));
00045     box.push_back(glm::vec3(bounding_min_.x, bounding_max_.y, bounding_min_.z));
00046 
00047     min = util::ApplyTransform(transformation, bounding_min_);
00048     max = min;
00049     for (size_t i = 1; i < box.size(); i++) {
00050       glm::vec3 temp = util::ApplyTransform(transformation, box[i]);
00051       min.x = std::min(temp.x, min.x);
00052       min.y = std::min(temp.y, min.y);
00053       min.z = std::min(temp.z, min.z);
00054 
00055       max.x = std::max(temp.x, max.x);
00056       max.y = std::max(temp.y, max.y);
00057       max.z = std::max(temp.z, max.z);
00058     }
00059   }
00060   return util::SegmentAABBIntersect(min, max, segment.start, segment.end);
00061 }
00062 }  // namespace tango_gl


rtabmap
Author(s): Mathieu Labbe
autogenerated on Sat Jul 23 2016 11:44:15