00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef opengl_CPolyhedron_H
00029 #define opengl_CPolyhedron_H
00030
00031 #include <mrpt/opengl/CRenderizable.h>
00032 #include <mrpt/poses/CPoint3D.h>
00033 #include <mrpt/utils/stl_extensions.h>
00034
00035 namespace mrpt {
00036 namespace opengl {
00037 using namespace mrpt::utils;
00038 using namespace mrpt::poses;
00039 using namespace std;
00040 using mrpt::poses::CPoint3D;
00041
00042 class MRPTDLLIMPEXP CPolyhedron;
00043
00044
00045
00046
00047 DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE(CPolyhedron,CRenderizable)
00051 class MRPTDLLIMPEXP CPolyhedron:public CRenderizable {
00052 DEFINE_SERIALIZABLE(CPolyhedron)
00053 public:
00054 struct TPolyhedronEdge {
00055 uint32_t v1;
00056 uint32_t v2;
00057 TPolyhedronEdge():v1(0),v2(0) {}
00058 bool operator==(const TPolyhedronEdge &e) const {
00059 if (e.v1==v1&&e.v2==v2) return true;
00060 else return e.v1==v2&&e.v2==v1;
00061 }
00062 float length(const vector_serializable<CPoint3D> &vertices) const;
00063 ~TPolyhedronEdge() {}
00064 };
00065 struct TPolyhedronFace {
00066 vector_serializable<uint32_t> vertices;
00067 float normal[3];
00068 TPolyhedronFace():vertices() {}
00069 ~TPolyhedronFace() {}
00070 float area(const vector_serializable<CPoint3D> &vertices) const;
00071 };
00072 protected:
00073 vector_serializable<CPoint3D> mVertices;
00074 vector_serializable<TPolyhedronEdge> mEdges;
00075 vector_serializable<TPolyhedronFace> mFaces;
00076 bool mWireframe;
00077 float mLineWidth;
00078 public:
00079 static CPolyhedronPtr Create(const vector<CPoint3D> &vertices,const vector<vector<uint32_t> > &faces) {
00080 vector<TPolyhedronFace> aux;
00081 for (vector<vector<uint32_t> >::const_iterator it=faces.begin();it!=faces.end();it++) {
00082 TPolyhedronFace f;
00083 f.vertices=*it;
00084 aux.push_back(f);
00085 }
00086 return Create(vertices,aux);
00087 }
00088 static CPolyhedronPtr Create(const vector<CPoint3D> &vertices,const vector<TPolyhedronFace> &faces) {
00089 return CPolyhedronPtr(new CPolyhedron(vertices,faces,true));
00090 }
00094
00095 static CPolyhedronPtr CreateTetrahedron(const float radius);
00096 static CPolyhedronPtr CreateHexahedron(const float radius);
00097 static CPolyhedronPtr CreateOctahedron(const float radius);
00098 static CPolyhedronPtr CreateDodecahedron(const float radius);
00099 static CPolyhedronPtr CreateIcosahedron(const float radius);
00100
00101
00102 static CPolyhedronPtr CreateTruncatedTetrahedron(const float radius);
00103 static CPolyhedronPtr CreateCuboctahedron(const float radius);
00104 static CPolyhedronPtr CreateTruncatedHexahedron(const float radius);
00105 static CPolyhedronPtr CreateTruncatedOctahedron(const float radius);
00106 static CPolyhedronPtr CreateRhombicuboctahedron(const float radius);
00107 static CPolyhedronPtr CreateIcosidodecahedron(const float radius);
00108
00109
00110
00111
00112 static CPolyhedronPtr CreateCubicPrism(const float x1,const float x2,const float y1,const float y2,const float z1,const float z2);
00113 static CPolyhedronPtr CreatePyramid(const vector<CPoint2D> &baseVertices,const float height);
00114 static CPolyhedronPtr CreateDoublePyramid(const vector<CPoint2D> &baseVertices,const float height1,const float height2);
00115 static CPolyhedronPtr CreateTruncatedPyramid(const vector<CPoint2D> &baseVertices,const float height,const float ratio);
00116 static CPolyhedronPtr CreateCustomPrism(const vector<CPoint2D> &baseVertices,const float height);
00117 static CPolyhedronPtr CreateRegularAntiprism(const uint32_t numBaseEdges,const float baseRadius,const float height);
00118 static CPolyhedronPtr CreateRegularPrism(const uint32_t numbaseEdges,const float baseRadius,const float height);
00119 static CPolyhedronPtr CreateCustomAntiprism(const vector<CPoint2D> &bottomBase,const vector<CPoint2D> &topBase,const float height);
00120 static CPolyhedronPtr CreateParallelepiped(const CPoint3D &base,const CPoint3D &v1,const CPoint3D &v2,const CPoint3D &v3);
00121 static CPolyhedronPtr CreateRegularPyramid(const uint32_t numBaseEdges,const float baseRadius,const float height);
00122 static CPolyhedronPtr CreateRegularDoublePyramid(const uint32_t numBaseEdges,const float baseRadius,const float height1,const float height2);
00123 static CPolyhedronPtr CreateArchimedeanRegularPrism(const uint32_t numBaseEdges,const float baseRadius);
00124 static CPolyhedronPtr CreateArchimedeanRegularAntiprism(const uint32_t numBaseEdges,const float baseRadius);
00125 static CPolyhedronPtr CreateRegularTruncatedPyramid(const uint32_t numBaseEdges,const float baseRadius,const float height,const float ratio);
00126
00129 void render() const;
00132 virtual bool traceRay(const mrpt::poses::CPose3D &o,float &dist) const;
00133 void getVertices(vector<CPoint3D> &vertices) const {
00134 vertices=mVertices;
00135 }
00136 void getEdges(vector<TPolyhedronEdge> &edges) const {
00137 edges=mEdges;
00138 }
00139 void getFaces(vector<TPolyhedronFace> &faces) const {
00140 faces=mFaces;
00141 }
00142 uint32_t getNumberOfVertices() const {
00143 return mVertices.size();
00144 }
00145 uint32_t getNumberOfEdges() const {
00146 return mEdges.size();
00147 }
00148 uint32_t getNumberOfFaces() const {
00149 return mFaces.size();
00150 }
00151 void getEdgesLength(vector<float> &lengths) const;
00152 void getFacesArea(vector<float> &areas) const;
00153 float getVolume() const;
00154 inline bool isWireframe() const {
00155 return mWireframe;
00156 }
00157 inline void setWireframe(bool enabled=true) {
00158 mWireframe=enabled;
00159 }
00160 inline float getLineWidth() const {
00161 return mLineWidth;
00162 }
00163 inline void setLineWidth(float lineWidth) {
00164 mLineWidth=lineWidth;
00165 }
00166 private:
00167 bool setNormal(TPolyhedronFace &f,bool doCheck=true);
00168 void addEdges(const TPolyhedronFace &e);
00169 static bool checkConsistence(const vector<CPoint3D> &vertices,const vector<TPolyhedronFace> &faces);
00170 CPolyhedron():mVertices(),mEdges(),mFaces(),mWireframe(false),mLineWidth(1) {}
00171 CPolyhedron(const vector<CPoint3D> &vertices,const vector<TPolyhedronFace> &faces,bool doCheck=true):mVertices(vertices),mEdges(),mFaces(faces),mWireframe(false),mLineWidth(1) {
00172 if (doCheck) if (!checkConsistence(vertices,faces)) throw std::logic_error("Face list accesses a vertex out of range");
00173 for (vector<TPolyhedronFace>::iterator it=mFaces.begin();it!=mFaces.end();it++) {
00174 if (!setNormal(*it,doCheck)) throw std::logic_error("Bad face specification");
00175 addEdges(*it);
00176 }
00177 }
00178 static CPolyhedronPtr CreateNoCheck(const vector<CPoint3D> &vertices,const vector<TPolyhedronFace> &faces) {
00179 return CPolyhedronPtr(new CPolyhedron(vertices,faces,false));
00180 }
00181 static CPolyhedronPtr CreateEmpty() {
00182 return CPolyhedronPtr(new CPolyhedron());
00183 }
00184 virtual ~CPolyhedron() {}
00185 };
00186 MRPTDLLIMPEXP mrpt::utils::CStream& operator>>(mrpt::utils::CStream& in,CPolyhedron::TPolyhedronEdge &o);
00187 MRPTDLLIMPEXP mrpt::utils::CStream& operator<<(mrpt::utils::CStream& out,const CPolyhedron::TPolyhedronEdge &o);
00188 MRPTDLLIMPEXP mrpt::utils::CStream& operator>>(mrpt::utils::CStream& in,CPolyhedron::TPolyhedronFace &o);
00189 MRPTDLLIMPEXP mrpt::utils::CStream& operator<<(mrpt::utils::CStream& out,const CPolyhedron::TPolyhedronFace &o);
00190 }
00191 }
00192 #endif