$search
00001 00002 /* 00003 * Software License Agreement (BSD License) 00004 * 00005 * Copyright (c) 2011, Willow Garage, Inc. 00006 * All rights reserved. 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions 00010 * are met: 00011 * 00012 * * Redistributions of source code must retain the above copyright 00013 * notice, this list of conditions and the following disclaimer. 00014 * * Redistributions in binary form must reproduce the above 00015 * copyright notice, this list of conditions and the following 00016 * disclaimer in the documentation and/or other materials provided 00017 * with the distribution. 00018 * * Neither the name of Willow Garage, Inc. nor the names of its 00019 * contributors may be used to endorse or promote products derived 00020 * from this software without specific prior written permission. 00021 * 00022 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00023 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00024 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00025 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00026 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00027 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00028 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00029 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00030 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00031 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00032 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00033 * POSSIBILITY OF SUCH DAMAGE. 00034 */ 00035 00038 #ifndef COLLISION_CHECKING_BV_FITTER_H 00039 #define COLLISION_CHECKING_BV_FITTER_H 00040 00041 #include "collision_checking/BVH_defs.h" 00042 #include "collision_checking/primitive.h" 00043 #include "collision_checking/vec_3f.h" 00044 #include "collision_checking/obb.h" 00045 #include "collision_checking/rss.h" 00046 #include <iostream> 00047 00049 namespace collision_checking 00050 { 00051 00053 template<typename BV> 00054 class BVFitter 00055 { 00056 public: 00057 00059 static BV fit(Vec3f* ps, int n) 00060 { 00061 BV bv; 00062 for(int i = 0; i < n; ++i) 00063 { 00064 bv += ps[i]; 00065 } 00066 00067 return bv; 00068 } 00069 00071 void set(Vec3f* vertices_, Triangle* tri_indices_, BVHModelType type_) 00072 { 00073 vertices = vertices_; 00074 prev_vertices = NULL; 00075 tri_indices = tri_indices_; 00076 type = type_; 00077 } 00078 00080 void set(Vec3f* vertices_, Vec3f* prev_vertices_, Triangle* tri_indices_, BVHModelType type_) 00081 { 00082 vertices = vertices_; 00083 prev_vertices = prev_vertices_; 00084 tri_indices = tri_indices_; 00085 type = type_; 00086 } 00087 00091 BV fit(unsigned int* primitive_indices, int num_primitives) 00092 { 00093 BV bv; 00094 00095 if(type == BVH_MODEL_TRIANGLES) /* The primitive is triangle */ 00096 { 00097 for(int i = 0; i < num_primitives; ++i) 00098 { 00099 Triangle t = tri_indices[primitive_indices[i]]; 00100 bv += vertices[t[0]]; 00101 bv += vertices[t[1]]; 00102 bv += vertices[t[2]]; 00103 00104 if(prev_vertices) /* When fitting both current and previous frame */ 00105 { 00106 bv += prev_vertices[t[0]]; 00107 bv += prev_vertices[t[1]]; 00108 bv += prev_vertices[t[2]]; 00109 } 00110 } 00111 } 00112 else if(type == BVH_MODEL_POINTCLOUD) /* The primitive is point */ 00113 { 00114 for(int i = 0; i < num_primitives; ++i) 00115 { 00116 bv += vertices[primitive_indices[i]]; 00117 00118 if(prev_vertices) /* When fitting both current and previous frame */ 00119 { 00120 bv += prev_vertices[primitive_indices[i]]; 00121 } 00122 } 00123 } 00124 00125 return bv; 00126 } 00127 00129 void clear() 00130 { 00131 vertices = NULL; 00132 prev_vertices = NULL; 00133 tri_indices = NULL; 00134 type = BVH_MODEL_UNKNOWN; 00135 } 00136 00137 private: 00138 00139 Vec3f* vertices; 00140 Vec3f* prev_vertices; 00141 Triangle* tri_indices; 00142 BVHModelType type; 00143 }; 00144 00145 00147 template<> 00148 class BVFitter<OBB> 00149 { 00150 public: 00151 00153 static OBB fit(Vec3f* ps, int n); 00154 00156 void set(Vec3f* vertices_, Triangle* tri_indices_, BVHModelType type_) 00157 { 00158 vertices = vertices_; 00159 prev_vertices = NULL; 00160 tri_indices = tri_indices_; 00161 type = type_; 00162 } 00163 00165 void set(Vec3f* vertices_, Vec3f* prev_vertices_, Triangle* tri_indices_, BVHModelType type_) 00166 { 00167 vertices = vertices_; 00168 prev_vertices = prev_vertices_; 00169 tri_indices = tri_indices_; 00170 type = type_; 00171 } 00172 00176 OBB fit(unsigned int* primitive_indices, int num_primitives); 00177 00179 void clear() 00180 { 00181 vertices = NULL; 00182 prev_vertices = NULL; 00183 tri_indices = NULL; 00184 type = BVH_MODEL_UNKNOWN; 00185 } 00186 00187 private: 00188 00189 Vec3f* vertices; 00190 Vec3f* prev_vertices; 00191 Triangle* tri_indices; 00192 BVHModelType type; 00193 00195 static inline OBB fit1(Vec3f* ps); 00196 00198 static inline OBB fit2(Vec3f* ps); 00199 00201 static inline OBB fit3(Vec3f* ps); 00202 00204 static inline OBB fit6(Vec3f* ps); 00205 00207 static OBB fitn(Vec3f* ps, int n); 00208 }; 00209 00210 00212 template<> 00213 class BVFitter<RSS> 00214 { 00215 public: 00216 00218 static RSS fit(Vec3f* ps, int n); 00219 00221 void set(Vec3f* vertices_, Triangle* tri_indices_, BVHModelType type_) 00222 { 00223 vertices = vertices_; 00224 prev_vertices = NULL; 00225 tri_indices = tri_indices_; 00226 type = type_; 00227 } 00228 00230 void set(Vec3f* vertices_, Vec3f* prev_vertices_, Triangle* tri_indices_, BVHModelType type_) 00231 { 00232 vertices = vertices_; 00233 prev_vertices = prev_vertices_; 00234 tri_indices = tri_indices_; 00235 type = type_; 00236 } 00237 00241 RSS fit(unsigned int* primitive_indices, int num_primitives); 00242 00244 void clear() 00245 { 00246 vertices = NULL; 00247 prev_vertices = NULL; 00248 tri_indices = NULL; 00249 type = BVH_MODEL_UNKNOWN; 00250 } 00251 00252 private: 00253 00254 Vec3f* vertices; 00255 Vec3f* prev_vertices; 00256 Triangle* tri_indices; 00257 BVHModelType type; 00258 00260 static RSS fit1(Vec3f* ps); 00261 00262 static RSS fit2(Vec3f* ps); 00263 00264 static RSS fit3(Vec3f* ps); 00265 00267 static RSS fitn(Vec3f* ps, int n); 00268 00269 }; 00270 00271 } 00272 00273 #endif