Follow this link to skip to the main content

bounding_shape.cc

Go to the documentation of this file.
00001 // -*-c++-*-
00002 //---------------------------< /-/ CLARAty /-/ >------------------------------
00003 /**
00004  * @file  bounding_shape.cc
00005  *
00006  * The Bounding_Shape class models the collision parameters of a
00007  * three-dimensionsl object. It is to be used in collision detection
00008  * algorithms.
00009  *
00010  * <br>@b Designer(s):  Hari Das Nayar, Issa Nesnas
00011  * <br>@b Author(s):    Hari Das Nayar
00012  * <br>@b Date:         August 4, 2005
00013  *
00014  * <b>Software License:</b><br>
00015  * <code> http://claraty.jpl.nasa.gov/license/open_src/  or 
00016  *        file: license/open_src.txt </code> 
00017  *
00018  * &copy; 2006, Jet Propulsion Laboratory, California Institute of Technology<br>
00019  *
00020  * $Revision: 1.7 $
00021  */
00022 //-----------------------------------------------------------------------------
00023 
00024 #include "claraty/bounding_shape.h"
00025 
00026 using namespace std;
00027 
00028 namespace claraty {
00029 
00030 //-------------------------------------------------------------------------
00031 //---------------------<< Bounding_Shape Class >>--------------------------
00032 //-------------------------------------------------------------------------
00033 /**
00034  * Default constructor
00035  *
00036  * @ingroup mech_model_pkg
00037  */
00038 Bounding_Shape::Bounding_Shape()
00039   : _name("root"), 
00040     _parent_name(""), 
00041     _is_container(false), 
00042     _is_aligned(false), 
00043     _ignore_collisions(false), 
00044     _tol(0.0)
00045 {
00046 }
00047 
00048 //----------------------------------------------------------------------------
00049 /**
00050  * Copy constructor
00051  *
00052  * @param[in] bounding_shape  Reference to the bounding shape to be copied.
00053  *
00054  * @ingroup bounding_shape_pkg
00055  */
00056 
00057 Bounding_Shape::Bounding_Shape(const Bounding_Shape & bounding_shape)
00058 {
00059   _name              = bounding_shape._name;
00060   _parent_name       = bounding_shape._parent_name;
00061   _is_container      = bounding_shape._is_container;
00062   _is_aligned        = bounding_shape._is_aligned;
00063   _ignore_collisions = bounding_shape._ignore_collisions;
00064   _tol               = bounding_shape._tol;
00065 
00066   _transform         = bounding_shape._transform;
00067   _three_d_object    = bounding_shape._three_d_object;
00068 }
00069 
00070 
00071 //----------------------------------------------------------------------------
00072 /**
00073  * Destructor for a Bounding_Shape object.
00074  *
00075  * @ingroup bounding_shape_pkg
00076  */
00077 Bounding_Shape::~Bounding_Shape()
00078 {
00079 }
00080 
00081 //----------------------------------------------------------------------------
00082 /**
00083  * Set name parameter 
00084  *
00085  * @param[in]  name The name of the bounding shape.
00086  * @return     void
00087  *
00088  * @ingroup bounding_shape_pkg
00089  */
00090 void 
00091 Bounding_Shape::set_name(std::string name)
00092 {
00093   _name = name;
00094 }
00095 
00096 //----------------------------------------------------------------------------
00097 /**
00098  * Get name parameter of the bouding shape.
00099  *
00100  * @return Bounding shape name
00101  *
00102  * @ingroup bounding_shape_pkg
00103  */
00104 
00105 std::string 
00106 Bounding_Shape::get_name()
00107 {
00108   return _name;
00109 }
00110 
00111 //----------------------------------------------------------------------------
00112 /**
00113  * Set parent name parameter 
00114  *
00115  * @param[in] parent_name The name of the parent.
00116  *
00117  * @ingroup bounding_shape_pkg
00118  */
00119 void 
00120 Bounding_Shape::set_parent_name(std::string parent_name)
00121 {
00122   _parent_name = parent_name;
00123 }
00124 
00125 //----------------------------------------------------------------------------
00126 /**
00127  * Get parent name parameter 
00128  *
00129  * @return enum type
00130  *
00131  * @ingroup bounding_shape_pkg
00132  */
00133 std::string 
00134 Bounding_Shape::get_parent_name()
00135 {
00136   return _parent_name;
00137 }
00138 
00139 //----------------------------------------------------------------------------
00140 /**
00141  * Set containment parameter 
00142  *
00143  * @param[in] containment
00144  *
00145  * @return enum type
00146  *
00147  * @ingroup bounding_shape_pkg
00148  */
00149 void 
00150 Bounding_Shape::set_containment(bool containment)
00151 {
00152   _is_container = containment;
00153 }
00154 
00155 //----------------------------------------------------------------------------
00156 /**
00157  * Get containment parameter 
00158  *
00159  * @return A boolean if _is_containiner returns true.
00160  *
00161  * @ingroup bounding_shape_pkg
00162  */
00163 bool 
00164 Bounding_Shape::get_containment()
00165 {
00166   return _is_container;
00167 }
00168 
00169 //----------------------------------------------------------------------------
00170 /**
00171  * Set alignment parameter 
00172  *
00173  * @param[in] alignment
00174  *
00175  * @ingroup bounding_shape_pkg
00176  */
00177 void 
00178 Bounding_Shape::set_alignment(bool alignment)
00179 {
00180   _is_aligned = alignment;
00181 }
00182 
00183 //----------------------------------------------------------------------------
00184 /**
00185  * Get alignment parameter 
00186  *
00187  * @return booliean indicating alignment.
00188  *
00189  * @ingroup bounding_shape_pkg
00190  */
00191 bool 
00192 Bounding_Shape::get_alignment()
00193 {
00194   return _is_aligned;
00195 }
00196 
00197 //----------------------------------------------------------------------------
00198 /**
00199  * Set collision parameter 
00200  *
00201  * @param[in] collision Set boolean value.
00202  *
00203  * @ingroup bounding_shape_pkg
00204  */
00205 void 
00206 Bounding_Shape::set_collision(bool collision)
00207 {
00208   _ignore_collisions = collision;
00209 }
00210 
00211 //----------------------------------------------------------------------------
00212 /**
00213  * Get collision parameter 
00214  *
00215  * @return Collision boolean.
00216  *
00217  * @ingroup bounding_shape_pkg
00218  */
00219 bool 
00220 Bounding_Shape::get_collision()
00221 {
00222   return _ignore_collisions;
00223 }
00224 
00225 //----------------------------------------------------------------------------
00226 /**
00227  * Set tollerance parameter 
00228  *
00229  * @param[in] tolerance
00230  *
00231  * @ingroup bounding_shape_pkg
00232  */
00233 void 
00234 Bounding_Shape::set_tolerance(double tolerance)
00235 {
00236   _tol = tolerance;
00237 }
00238 
00239 //----------------------------------------------------------------------------
00240 /**
00241  * Get tollerance parameter 
00242  *
00243  * @return Tolerance.
00244  *
00245  * @ingroup bounding_shape_pkg
00246  */
00247 double & 
00248 Bounding_Shape::get_tolerance()
00249 {
00250   return _tol;
00251 }
00252 
00253 //----------------------------------------------------------------------------
00254 /**
00255  * Set transform
00256  *
00257  * @param[in] transform
00258  *
00259  * @ingroup bounding_shape_pkg
00260  */
00261 void 
00262 Bounding_Shape::set(Transform & transform)
00263 {
00264   _transform = transform;
00265 }
00266 
00267 //----------------------------------------------------------------------------
00268 /**
00269  * Get transform
00270  *
00271  * @return Transform.
00272  *
00273  * @ingroup bounding_shape_pkg
00274  */
00275 Transform & 
00276 Bounding_Shape::get_transform()
00277 {
00278   return _transform;
00279 }
00280 
00281 //----------------------------------------------------------------------------
00282 /**
00283  * Set shape object
00284  *
00285  * @param[in] three_d_object
00286  *
00287  * @ingroup bounding_shape_pkg
00288  */
00289 void 
00290 Bounding_Shape::set(N_3D_Object & three_d_object)
00291 {
00292   _three_d_object = three_d_object;
00293 }
00294 //----------------------------------------------------------------------------
00295 /**
00296  * Get shape object
00297  *
00298  * @return Shape
00299  *
00300  * @ingroup bounding_shape_pkg
00301  */
00302 N_3D_Object & 
00303 Bounding_Shape::get_3d_object()
00304 {
00305   return _three_d_object;
00306 }
00307 
00308 //----------------------------------------------------------------------------
00309 /**
00310  * Output to ostream
00311  *
00312  * @param[out] os  ostream to put output on
00313  * @param[in]  bounding_shape  Bounding_Shape object to be output
00314  * @return ostream with output on it
00315  *
00316  * @ingroup mechanism_model_io_pkg
00317  */
00318 std::ostream& operator<< (std::ostream& os, Bounding_Shape & bounding_shape)
00319 {
00320   std::string spacing = "";
00321   std::string child_spacing = "";
00322   spacing.append(XML_Out::get_indentation_space(), ' ');
00323   child_spacing.append(XML_Out::get_indentation_space()
00324                        + XML_Out::get_tab_space(), ' ');
00325 
00326   os << spacing;
00327   os << "<Bounding_Shape ";
00328   os << "name = \""     << bounding_shape.get_name() << "\" ";
00329   os << "parent = \""   << bounding_shape.get_parent_name() << "\" ";
00330   os << "is_container = \""
00331      << (bounding_shape.get_containment()   ?  "true" : "false")  << "\" ";;
00332   os << "is_aligned = \""
00333      << (bounding_shape.get_alignment() ? "true" : "false")  << "\" ";;
00334   os << "ignore_collisions = \""
00335      << (bounding_shape.get_collision()   ?  "true" : "false")  << "\" ";;
00336   os << "tol = \"" << bounding_shape.get_tolerance() << "\" ";
00337   os << ">\n";
00338 
00339 #ifdef DEBUG
00340   cout << "In Bounding_Shape, about to output transform_io" << endl;
00341 #endif
00342   os << child_spacing;
00343   os << "<Transform>\n";
00344   XML_Out::increment_indentation_level();
00345   XML_Out::increment_indentation_level();
00346   os << XML_Out::enable << bounding_shape.get_transform() << XML_Out::disable;
00347   XML_Out::decrement_indentation_level();
00348   XML_Out::decrement_indentation_level();
00349   os << child_spacing;
00350   os << "</Transform>\n";
00351   
00352 #ifdef DEBUG
00353   cout << "In Bounding_Shape, about to output bounding_shape_io" << endl;
00354 #endif
00355   XML_Out::increment_indentation_level();
00356   os << bounding_shape.get_3d_object();
00357   XML_Out::decrement_indentation_level();
00358 
00359   os << spacing;
00360   os << "</Bounding_Shape> \n";
00361 
00362   return os;
00363 
00364 }
00365 
00366 //-------------------------------------------------------------------------
00367 //----------------<< Composite_Bounding_Shape Class >>---------------------
00368 //-------------------------------------------------------------------------
00369 //----------------------------------------------------------------------------
00370 /**
00371  * Insert a bounding_shape object to the bounding_shape tree
00372  *
00373  * @param[in] bounding_shape Shape to be inserted into the tree.
00374  *
00375  * @ingroup bounding_shape_pkg
00376  */
00377 void 
00378 Composite_Bounding_Shape::insert(Bounding_Shape * bounding_shape)
00379 {
00380   bool attached_new_node = false;
00381 
00382   Tree<Bounding_Shape>::Pre_Order_Iterator p_shape_tree_itr
00383     = _bounding_shape_tree.begin_pre_order();
00384   Tree<Bounding_Shape>::Pre_Order_Iterator p_end_shape_tree_itr
00385     = _bounding_shape_tree.end_pre_order();
00386   for (; p_shape_tree_itr < p_end_shape_tree_itr; ++p_shape_tree_itr)
00387     {
00388       if (p_shape_tree_itr->get_name() == bounding_shape->get_parent_name())
00389         {
00390           _bounding_shape_tree.append_child(p_shape_tree_itr, bounding_shape);
00391           attached_new_node = true;
00392           break;
00393         }
00394     }
00395   if ((!attached_new_node)||
00396       (bounding_shape->get_parent_name().length() == 0))
00397     // If parent name wasn't found or name string length is zero, attach to root
00398     {
00399       Tree<Bounding_Shape>::Pre_Order_Iterator p_shape_tree_itr
00400         = _bounding_shape_tree.begin_pre_order();
00401       _bounding_shape_tree.append_child(p_shape_tree_itr, bounding_shape);
00402       attached_new_node = true;
00403     }
00404 }
00405 //----------------------------------------------------------------------------
00406 /**
00407  * Insert a bounding_shape object at the root of the bounding_shape tree
00408  *
00409  * @param[in] bounding_shape Object to be inserted at the root of the tree.
00410  *
00411  * @ingroup bounding_shape_pkg
00412  */
00413 void 
00414 Composite_Bounding_Shape::insert_at_root(Bounding_Shape * bounding_shape)
00415 {
00416   Tree<Bounding_Shape>::Pre_Order_Iterator p_shape_tree_itr
00417     = _bounding_shape_tree.begin_pre_order();
00418   _bounding_shape_tree.append_child(p_shape_tree_itr, bounding_shape);
00419 }
00420 
00421 //----------------------------------------------------------------------------
00422 /**
00423  * Copy of the tree to this bounding_shape tree
00424  *
00425  * @param[in] composite_bounding_shape
00426  *
00427  * @ingroup bounding_shape_pkg
00428  */
00429 void 
00430 Composite_Bounding_Shape::copy(Composite_Bounding_Shape & composite_bounding_shape)
00431 {
00432   _bounding_shape_tree.copy(composite_bounding_shape._bounding_shape_tree);
00433 }
00434 
00435 //----------------------------------------------------------------------------
00436 /**
00437  * Return true if tree is empty, false if not
00438  *
00439  * @return True if tree is empty and false if it is not empty.
00440  *
00441  * @ingroup bounding_shape_pkg
00442  */
00443 bool 
00444 Composite_Bounding_Shape::empty()
00445 {
00446   return _bounding_shape_tree.empty();
00447 }
00448 //----------------------------------------------------------------------------
00449 /**
00450  * Clear the tree
00451  *
00452  * @ingroup bounding_shape_pkg
00453  */
00454 void 
00455 Composite_Bounding_Shape::clear_tree()
00456 {
00457   return _bounding_shape_tree.clear_tree();
00458 }
00459 //----------------------------------------------------------------------------
00460 /**
00461  * Get the tree
00462  *
00463  * @return The tree.
00464  *
00465  * @ingroup bounding_shape_pkg
00466  */
00467 Tree<Bounding_Shape> & 
00468 Composite_Bounding_Shape::get_shape_tree()
00469 {
00470   return _bounding_shape_tree;
00471 }
00472 //----------------------------------------------------------------------------
00473 /**
00474  * Output to ostream
00475  *
00476  * @param[out] os  ostream to put output on.
00477  * @param[in]  composite_bounding_shape The object to be output.
00478  * @return ostream with output on it.
00479  *
00480  * @ingroup mechanism_model_io_pkg
00481  */
00482 std::ostream& operator<< (std::ostream& os,
00483                           Composite_Bounding_Shape & composite_bounding_shape)
00484 {
00485   Tree<Bounding_Shape>::Pre_Order_Iterator p_shape_tree_itr
00486     = composite_bounding_shape.get_shape_tree().begin_pre_order();
00487   Tree<Bounding_Shape>::Pre_Order_Iterator p_end_shape_tree_itr
00488     = composite_bounding_shape.get_shape_tree().end_pre_order();
00489   
00490   ++p_shape_tree_itr; // Skip the root object of the object tree
00491   
00492   for (; p_shape_tree_itr < p_end_shape_tree_itr; ++p_shape_tree_itr)
00493     {
00494       os << *p_shape_tree_itr;
00495     }
00496   return os;
00497 }
00498 //----------------------------------------------------------------------------
00499 /**
00500  * Return the top-level bounding_shape object in the composite
00501  *
00502  * @return ostream with the level bounding shape.
00503  *
00504  * @ingroup mechanism_model_io_pkg
00505  */
00506 Bounding_Shape & Composite_Bounding_Shape::_get_top_level_bounding_shape()
00507 {
00508   Tree<Bounding_Shape>::Pre_Order_Iterator p_shape_tree_itr
00509     = get_shape_tree().begin_pre_order();
00510 
00511   ++p_shape_tree_itr;
00512   return *p_shape_tree_itr;
00513 
00514 }//----------------------------------------------------------------------------
00515 
00516 
00517 } // namespace claraty