00001 #ifndef OPENTISSUE_DYNAMICS_EDM_EDM_OBJECT_H 00002 #define OPENTISSUE_DYNAMICS_EDM_EDM_OBJECT_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 #include <OpenTissue/core/geometry/geometry_base_shape.h> 00012 #include <OpenTissue/core/function/function_signed_distance_function.h> 00013 00014 #include <cassert> 00015 00016 00017 namespace OpenTissue 00018 { 00019 00020 namespace edm 00021 { 00022 00026 template<typename edm_types> 00027 class Object 00028 : public edm_types::object_traits 00029 { 00030 public: 00031 00032 typedef typename edm_types::value_traits value_traits; 00033 typedef typename edm_types::math_types math_types; 00034 typedef typename edm_types::real_type real_type; 00035 typedef typename edm_types::vector3_type vector3_type; 00036 typedef geometry::BaseShape<math_types> base_shape; 00037 typedef function::SignedDistanceFunction<math_types> sdf_type; 00038 00039 protected: 00040 00041 bool m_visible; 00042 real_type m_scale; 00043 base_shape * m_shape; 00044 sdf_type const * m_sdf; 00045 00046 public: 00047 00048 Object() 00049 : m_visible(true) 00050 , m_scale(value_traits::one()) 00051 , m_shape(0) 00052 , m_sdf(0) 00053 {} 00054 00055 virtual ~Object() 00056 { 00057 delete m_shape; 00058 } 00059 00060 public: 00061 00062 template<typename shape_type> 00063 shape_type& create_shape() 00064 { 00065 shape_type* shape = new shape_type; 00066 // 2009-03-11 kenny: why dynamic cast? could we not do without? 00067 m_shape = dynamic_cast<base_shape*>(shape); 00068 assert(m_shape || !"shape_type does not support the ShapeBase interface!"); 00069 m_sdf = dynamic_cast<sdf_type*>(m_shape); 00070 assert(m_sdf || !"shape_type does not support the SignedDistanceFunction interface!"); 00071 return *static_cast<shape_type*>(m_shape); 00072 } 00073 00074 base_shape const * get_shape() const 00075 { 00076 return m_shape; 00077 } 00078 00079 Object & set_scale(real_type const & scale) 00080 { 00081 m_scale = scale; 00082 return *this; 00083 } 00084 00085 real_type const & get_scale() const 00086 { 00087 return m_scale; 00088 } 00089 00090 Object & set_visibility(bool visible) 00091 { 00092 m_visible = visible; 00093 return *this; 00094 } 00095 00096 bool get_visibility() const 00097 { 00098 return m_visible; 00099 } 00100 00101 public: 00102 00103 real_type eval(vector3_type const & x) const 00104 { 00105 // 2009-03-11 kenny: assert without proper message 00106 assert(m_sdf); 00107 return real_type(m_sdf->evaluate(x)); 00108 } 00109 00110 vector3_type normal(vector3_type const & x) const 00111 { 00112 // 2009-03-11 kenny: assert without proper message 00113 assert(m_sdf); 00114 return vector3_type(m_sdf->normal(x)); 00115 } 00116 00117 real_type dist(vector3_type const & x) const 00118 { 00119 using std::fabs; 00120 // 2009-03-11 kenny: assert without proper message 00121 assert(m_sdf); 00122 return real_type(fabs(m_sdf->signed_distance(x))); 00123 } 00124 00125 }; 00126 00127 } // namespace edm 00128 00129 } // namespace OpenTissue 00130 00131 // OPENTISSUE_DYNAMICS_EDM_EDM_OBJECT_H 00132 #endif