00001 #ifndef OPENTISSUE_COLLISION_BVH_BOTTOM_UP_CONSTRUCTOR_BVH_BOTTOMUP_CONSTRUCTOR_H 00002 #define OPENTISSUE_COLLISION_BVH_BOTTOM_UP_CONSTRUCTOR_BVH_BOTTOMUP_CONSTRUCTOR_H 00003 // 00004 // OpenTissue Template Library 00005 // - A generic toolbox for physics-based modeling and simulation. 00006 // Copyright (C) 2008 Department of Computer Science, University of Copenhagen. 00007 // 00008 // OTTL is licensed under zlib: http://opensource.org/licenses/zlib-license.php 00009 // 00010 #include <OpenTissue/configuration.h> 00011 00012 #include <OpenTissue/collision/bvh/bottom_up_constructor/bvh_graph.h> 00013 #include <OpenTissue/collision/bvh/bottom_up_constructor/bvh_default_priority_bottom_up_policy.h> 00014 00015 #include <boost/shared_ptr.hpp> // needed for boost::static_pointer_cast 00016 00017 #include <iostream> 00018 00019 namespace OpenTissue 00020 { 00021 namespace bvh 00022 { 00023 00038 template < 00039 typename bvh_type, 00040 typename bottom_up_policy = DefaultPriorityBottomUpPolicy<bvh_type> 00041 > 00042 class BottomUpConstructor : public bottom_up_policy 00043 { 00044 public: 00045 00046 //--- Convenience stuff for better readability 00047 typedef typename bvh_type::volume_type volume_type; 00048 typedef typename bvh_type::geometry_type geometry_type; 00049 typedef typename bvh_type::bv_ptr bv_ptr; 00050 typedef typename bvh_type::annotated_bv_ptr annotated_bv_ptr; 00051 typedef typename bvh_type::annotated_bv_type annotated_bv_type; 00052 typedef BVHGraph<bvh_type> graph_type; 00053 typedef typename graph_type::edge_ptr_type edge_ptr; 00054 typedef typename graph_type::node_ptr_type node_ptr; 00055 typedef typename graph_type::node_iterator node_iterator; 00056 typedef typename graph_type::volume_container volume_container; 00057 00058 public: 00059 00069 void run(graph_type & graph, bvh_type & bvh) 00070 { 00071 bvh.clear(); 00072 //--- Create leaf BVs in BVH 00073 00074 node_iterator node = graph.node_begin(); 00075 node_iterator end = graph.node_end(); 00076 for(;node!=end;++node) 00077 { 00078 if(node->coverage().empty()) 00079 { 00080 node->create_bv(bvh); 00081 } 00082 else 00083 { 00084 node->create_bv(bvh,true); 00085 annotated_bv_ptr bv = boost::static_pointer_cast<annotated_bv_type>(node->bv()); 00086 00087 bv->insert(node->coverage()); 00088 00089 volume_container volumes; //--- empty! 00090 00091 bv->volume() = fit(node->coverage().begin(),node->coverage().end(),volumes.begin(),volumes.end()); 00092 } 00093 } 00094 00095 init(graph);//--- from policy 00096 00097 //--- Begin to merge leaves into parents, until only a single root exist 00098 while(this->has_more_edges()) 00099 { 00100 //--- pick an graph edge and collapse it 00101 edge_ptr edge = this->get_next_edge();//--- from policy 00102 node_ptr node = graph.collapse(edge); 00103 //--- Fit a volume to the new graph node 00104 volume_container volumes; 00105 node->get_volumes(volumes); 00106 node->volume() = fit(node->coverage().begin(),node->coverage().end(),volumes.begin(),volumes.end()); 00107 //--- Test if a BV node should be created for the new graph node 00108 if( should_create_bv(node) ) //--- from policy 00109 { 00110 node->create_bv(bvh); 00111 graph.remove_sub_nodes(node); 00112 } 00113 update(node);//--- from policy 00114 } 00115 graph.clear(); 00116 std::cout << "BVHBottomUpConstructor::run(): " << bvh.size() << " nodes created." << std::endl; 00117 } 00118 00119 }; 00120 00121 } // namespace bvh 00122 00123 } // namespace OpenTissue 00124 00125 // OPENTISSUE_COLLISION_BVH_BOTTOM_UP_CONSTRUCTOR_BVH_BOTTOMUP_CONSTRUCTOR_H 00126 #endif