00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef _MBMESHGEOMETRY_H
00024 #define _MBMESHGEOMETRY_H
00025
00026 #ifdef _WIN32
00027 #pragma warning(push)
00028 #pragma warning(disable:4250)
00029 #pragma warning(disable:4251)
00030 #endif
00031
00032 #include <MeshVizXLM/mesh/geometry/MiGeometryI.h>
00033 #include <geometry/MbGeometry.h>
00034 #include <MeshVizXLM/MbVec3.h>
00035
00036 #include <Inventor/STL/vector>
00037
00046 template <typename _Vec3T=MbVec3d>
00047 class MbMeshGeometry : public MiGeometryI, public MbGeometry
00048 {
00049 public:
00050 MbMeshGeometry()
00051 #ifdef MBMESH_DONT_USE_STL
00052 : m_coords(NULL), m_coordsSize(0)
00053 #endif
00054 {}
00055
00057 template <typename _Vec3Iter>
00058 MbMeshGeometry(_Vec3Iter begin, _Vec3Iter end)
00059 #ifndef MBMESH_DONT_USE_STL
00060 : m_coords(begin,end)
00061 #endif
00062 {
00063 #ifdef MBMESH_DONT_USE_STL
00064 m_coordsSize = end-begin;
00065 m_coords = new _Vec3T[m_coordsSize];
00066 _Vec3Iter it = begin;
00067 size_t i;
00068 for (it=begin, i=0; it!=end; ++it,++i)
00069 m_coords[i] = *it;
00070 #endif
00071 }
00072
00073 #ifdef MBMESH_DONT_USE_STL
00074 ~MbMeshGeometry();
00075 #endif
00076
00082 template <typename _Vec3Iter>
00083 void assign(_Vec3Iter begin, _Vec3Iter end)
00084 {
00085 #ifndef MBMESH_DONT_USE_STL
00086 m_coords.assign(begin,end);
00087 #else
00088 delete [] m_coords;
00089 m_coordsSize = end-begin;
00090 m_coords = new _Vec3T[m_coordsSize];
00091 _Vec3Iter it = begin;
00092 size_t i;
00093 for (it=begin, i=0; it!=end; ++it,++i)
00094 m_coords[i] = *it;
00095 #endif
00096 m_timeStamp = MxTimeStamp::getTimeStamp();
00097 }
00098
00102 virtual MbVec3d getMin() const;
00106 virtual MbVec3d getMax() const;
00107
00111 virtual MbVec3d getCoord(size_t i) const
00112 {
00113 return (MbVec3d)m_coords[i];
00114 }
00115
00116 _Vec3T& operator [](size_t i)
00117 {
00118 return m_coords[i];
00119 }
00120
00121 const _Vec3T& operator [](size_t i) const
00122 {
00123 return m_coords[i];
00124 }
00125
00126 MbVec3d getCenter() const
00127 {
00128 return (getMin() + getMax())/2.0f;
00129 }
00130
00131 size_t getSize() const;
00132
00133 void rotate(double angle, size_t rotaxis, const _Vec3T* rotationCenter = NULL);
00134
00135 template <typename _FunctorT>
00136 void transform(const _FunctorT& func);
00137
00138 private:
00139 #ifndef MBMESH_DONT_USE_STL
00140 std::vector<_Vec3T> m_coords;
00141 #else
00142 _Vec3T* m_coords;
00143 size_t m_coordsSize;
00144 #endif
00145 };
00146
00147 #ifdef MBMESH_DONT_USE_STL
00148
00149 template <typename _Vec3T>
00150 MbMeshGeometry<_Vec3T>::~MbMeshGeometry()
00151 {
00152 delete [] m_coords;
00153 }
00154 #endif
00155
00156 template <typename _Vec3T>
00157 inline size_t
00158 MbMeshGeometry<_Vec3T>::getSize() const
00159 {
00160 #ifndef MBMESH_DONT_USE_STL
00161 return m_coords.size();
00162 #else
00163 return m_coordsSize;
00164 #endif
00165 }
00166
00167
00168 template <typename _Vec3T>
00169 inline MbVec3d
00170 MbMeshGeometry<_Vec3T>::getMin() const
00171 {
00172 double xm = m_coords[0][0];
00173 double ym = m_coords[0][1];
00174 double zm = m_coords[0][2];
00175 double cx,cy,cz;
00176
00177 #ifndef MBMESH_DONT_USE_STL
00178 for (size_t i=1; i<m_coords.size(); ++i)
00179 #else
00180 for (size_t i=1; i<m_coordsSize; ++i)
00181 #endif
00182 {
00183 cx = m_coords[i][0];
00184 cy = m_coords[i][1];
00185 cz = m_coords[i][2];
00186 if (cx < xm)
00187 xm = cx;
00188 if (cy < ym)
00189 ym = cy;
00190 if (cz < zm)
00191 zm = cz;
00192 }
00193 return MbVec3d(xm,ym,zm);
00194 }
00195
00196
00197
00198 template <typename _Vec3T>
00199 inline MbVec3d
00200 MbMeshGeometry<_Vec3T>::getMax() const
00201 {
00202 double xm = m_coords[0][0];
00203 double ym = m_coords[0][1];
00204 double zm = m_coords[0][2];
00205 double cx,cy,cz;
00206
00207 #ifndef MBMESH_DONT_USE_STL
00208 for (size_t i=1; i<m_coords.size(); ++i)
00209 #else
00210 for (size_t i=1; i<m_coordsSize; ++i)
00211 #endif
00212 {
00213 cx = m_coords[i][0];
00214 cy = m_coords[i][1];
00215 cz = m_coords[i][2];
00216 if (cx > xm)
00217 xm = cx;
00218 if (cy > ym)
00219 ym = cy;
00220 if (cz > zm)
00221 zm = cz;
00222 }
00223 return MbVec3d(xm,ym,zm);
00224 }
00225
00226
00227 template < typename _Vec3T>
00228 void
00229 MbMeshGeometry<_Vec3T>::rotate(double angle, size_t rotaxis, const _Vec3T* rotationCenter)
00230 {
00231 _Vec3T shift;
00232 typedef typename _Vec3T::ValueType T;
00233 if (rotationCenter != NULL)
00234 shift = *rotationCenter;
00235 else
00236 shift = _Vec3T(this->getCenter());
00237 if (angle != 0.0)
00238 {
00239 T cosa = (T)cos(angle);
00240 T sina = (T)sin(angle);
00241 _Vec3T tcoord;
00242 _Vec3T* it = &m_coords[0];
00243 size_t numCoord = this->getSize();
00244 switch(rotaxis)
00245 {
00246 case 0:
00247 for (size_t i = 0; i < numCoord; ++i, ++it)
00248 {
00249 tcoord = *it - shift;
00250 it->setValue( tcoord[0],
00251 tcoord[1]*cosa - tcoord[2]*sina,
00252 tcoord[1]*sina + tcoord[2]*cosa);
00253 *it += shift;
00254 }
00255 break;
00256 case 1:
00257 for (size_t i = 0; i < numCoord; ++i, ++it)
00258 {
00259 tcoord = *it - shift;
00260 it->setValue( tcoord[0]*cosa - tcoord[2]*sina,
00261 tcoord[1],
00262 tcoord[0]*sina + tcoord[2]*cosa);
00263 *it += shift;
00264 }
00265 break;
00266 case 2:
00267 for (size_t i = 0; i < numCoord; ++i, ++it)
00268 {
00269 tcoord = *it - shift;
00270 it->setValue( tcoord[0]*cosa - tcoord[1]*sina,
00271 tcoord[0]*sina + tcoord[1]*cosa,
00272 tcoord[2]);
00273 *it += shift;
00274 }
00275 break;
00276 }
00277 this->touch();
00278 }
00279 }
00280
00281
00282 template < typename _Vec3T>
00283 template < typename _FunctorT>
00284 void
00285 MbMeshGeometry<_Vec3T>::transform(const _FunctorT& func)
00286 {
00287 size_t numCoord = this->getSize();
00288 for (size_t i = 0; i < numCoord; ++i)
00289 m_coords[i] = func(m_coords[i]);
00290 this->touch();
00291 }
00292
00293 #ifdef _WIN32
00294 #pragma warning(pop)
00295 #endif
00296
00297 #endif
00298
00299
00300
00301
00302
00303
00304
00305