$search
00001 /* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2011, Willow Garage, Inc. 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of Willow Garage, Inc. nor the names of its 00018 * contributors may be used to endorse or promote products derived 00019 * from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 00033 */ 00034 00037 #include "collision_checking/BVH_split_rule.h" 00038 00039 namespace collision_checking 00040 { 00041 00042 void BVSplitRule<OBB>::computeRule_bvcenter(const OBB& bv, unsigned int* primitive_indices, int num_primitives) 00043 { 00044 Vec3f center = bv.center(); 00045 split_vector = bv.axis[0]; 00046 split_value = center[0]; 00047 } 00048 00049 void BVSplitRule<OBB>::computeRule_mean(const OBB& bv, unsigned int* primitive_indices, int num_primitives) 00050 { 00051 split_vector = bv.axis[0]; 00052 BVH_REAL sum = 0; 00053 if(type == BVH_MODEL_TRIANGLES) 00054 { 00055 for(int i = 0; i < num_primitives; ++i) 00056 { 00057 const Triangle& t = tri_indices[primitive_indices[i]]; 00058 const Vec3f& p1 = vertices[t[0]]; 00059 const Vec3f& p2 = vertices[t[1]]; 00060 const Vec3f& p3 = vertices[t[2]]; 00061 Vec3f centroid((p1[0] + p2[0] + p3[0]) / 3, 00062 (p1[1] + p2[1] + p3[1]) / 3, 00063 (p1[2] + p2[2] + p3[2]) / 3); 00064 00065 sum += centroid.dot(split_vector); 00066 } 00067 } 00068 else if(type == BVH_MODEL_POINTCLOUD) 00069 { 00070 for(int i = 0; i < num_primitives; ++i) 00071 { 00072 const Vec3f& p = vertices[primitive_indices[i]]; 00073 Vec3f v(p[0], p[1], p[2]); 00074 sum += v.dot(split_vector); 00075 } 00076 } 00077 00078 split_value = sum / num_primitives; 00079 } 00080 00081 00082 void BVSplitRule<OBB>::computeRule_median(const OBB& bv, unsigned int* primitive_indices, int num_primitives) 00083 { 00084 split_vector = bv.axis[0]; 00085 std::vector<BVH_REAL> proj(num_primitives); 00086 00087 if(type == BVH_MODEL_TRIANGLES) 00088 { 00089 for(int i = 0; i < num_primitives; ++i) 00090 { 00091 const Triangle& t = tri_indices[primitive_indices[i]]; 00092 const Vec3f& p1 = vertices[t[0]]; 00093 const Vec3f& p2 = vertices[t[1]]; 00094 const Vec3f& p3 = vertices[t[2]]; 00095 Vec3f centroid((p1[0] + p2[0] + p3[0]) / 3, 00096 (p1[1] + p2[1] + p3[1]) / 3, 00097 (p1[2] + p2[2] + p3[2]) / 3); 00098 00099 proj[i] = centroid.dot(split_vector); 00100 } 00101 } 00102 else if(type == BVH_MODEL_POINTCLOUD) 00103 { 00104 for(int i = 0; i < num_primitives; ++i) 00105 { 00106 const Vec3f& p = vertices[primitive_indices[i]]; 00107 Vec3f v(p[0], p[1], p[2]); 00108 proj[i] = v.dot(split_vector); 00109 } 00110 } 00111 00112 std::sort(proj.begin(), proj.end()); 00113 00114 if(num_primitives % 2 == 1) 00115 { 00116 split_value = proj[(num_primitives - 1) / 2]; 00117 } 00118 else 00119 { 00120 split_value = (proj[num_primitives / 2] + proj[num_primitives / 2 - 1]) / 2; 00121 } 00122 } 00123 00124 }