00001 #ifndef _VTX_PRIMITIVE_ACCUMULATOR_H_
00002 #define _VTX_PRIMITIVE_ACCUMULATOR_H_
00003
00004 #include "fv_assert.h"
00005 #include "BBox3D.h"
00006 #include "fv_inc.h"
00007 #include "RenderParams.h"
00008 #include "VtxAccumulator.h"
00009 #include <string>
00010 #include <vector>
00011
00012 namespace FemViewer {
00013 class PrimitiveAccumulator;
00014
00015
00016
00017
00018 class VtxPrimitiveAccumulator
00019 {
00020 public:
00021
00022 VtxPrimitiveAccumulator (VtxAccumulator& pVertexes);
00023 ~VtxPrimitiveAccumulator();
00024
00025 bool addLine (const int pVertexIndex1,
00026 const int pVertexIndex2);
00027
00028 bool addPoint (const int pVertexIndex);
00029
00030 bool addQuad (const int pVertexIndex1,
00031 const int pVertexIndex2,
00032 const int pVertexIndex3,
00033 const int pVertexIndex4);
00034
00035 bool addTriangle (const int pVertexIndex1,
00036 const int pVertexIndex2,
00037 const int pVertexIndex3);
00038
00039 void dumpCharacteristics(std::ostream& pOstream,
00040 const std::string& pIndentation,
00041 const Matrix<float>& pTransformation);
00042
00043 const BBox3D& getBBox3D() const;
00044
00045 void render (const RenderParams& pParams);
00046
00047 private:
00048
00049 typedef VtxAccumulator::Vertices Vertices;
00050 typedef VtxAccumulator::Normals Normals;
00051
00052 VtxPrimitiveAccumulator(const VtxPrimitiveAccumulator&);
00053 VtxPrimitiveAccumulator& operator=(const VtxPrimitiveAccumulator&);
00054
00055 struct Line {
00056 int aP[2];
00057
00058 Vec3D getBarycenter(const Vertices& pVertices) const {
00059 FV_ASSERT(aP[0] >= 0 && aP[0] < static_cast<int>(pVertices.size()));
00060 FV_ASSERT(aP[1] >= 0 && aP[1] < static_cast<int>(pVertices.size()));
00061 return 0.5*Vec3D((pVertices[aP[0]].position + pVertices[aP[1]].position).v);
00062 }
00063 };
00064
00065 struct Point {
00066 int aP;
00067
00068 Vec3D getBarycenter(const Vertices& pVertices) const {
00069 FV_ASSERT(aP >= 0 && aP < static_cast<int>(pVertices.size()));
00070 return Vec3D(pVertices[aP].position.v);
00071 }
00072 };
00073
00074 struct Quad {
00075 int aP[4];
00076
00077 Vec3D getBarycenter(const Vertices& pVertices) const {
00078 FV_ASSERT(aP[0] >= 0 && aP[0] < static_cast<int>(pVertices.size()));
00079 FV_ASSERT(aP[1] >= 0 && aP[1] < static_cast<int>(pVertices.size()));
00080 FV_ASSERT(aP[2] >= 0 && aP[2] < static_cast<int>(pVertices.size()));
00081 FV_ASSERT(aP[3] >= 0 && aP[3] < static_cast<int>(pVertices.size()));
00082 return 0.25*Vec3D((pVertices[aP[0]].position
00083 + pVertices[aP[1]].position
00084 + pVertices[aP[2]].position
00085 + pVertices[aP[3]].position).v);
00086 }
00087 Vec3D getNormal(const Vertices& pVertices) const {
00088 FV_ASSERT(aP[0] >= 0 && aP[0] < static_cast<int>(pVertices.size()));
00089 FV_ASSERT(aP[1] >= 0 && aP[1] < static_cast<int>(pVertices.size()));
00090 FV_ASSERT(aP[3] >= 0 && aP[3] < static_cast<int>(pVertices.size()));
00091 const Vec3D& lV0 = Vec3D(pVertices[aP[0]].position.v);
00092 const Vec3D& lV1 = Vec3D(pVertices[aP[1]].position.v);
00093 const Vec3D& lV2 = Vec3D(pVertices[aP[3]].position.v);
00094 return Vec3D(lV1-lV0) % (lV2-lV0);
00095 };
00096 };
00097
00098 struct Triangle {
00099 int aP[3];
00100
00101 Vec3D getBarycenter(const Vertices& pVertices) const {
00102 FV_ASSERT(aP[0] >= 0 && aP[0] < static_cast<int>(pVertices.size()));
00103 FV_ASSERT(aP[1] >= 0 && aP[1] < static_cast<int>(pVertices.size()));
00104 FV_ASSERT(aP[2] >= 0 && aP[2] < static_cast<int>(pVertices.size()));
00105 return (1.0/3.0)*Vec3D((pVertices[aP[0]].position + pVertices[aP[1]].position + pVertices[aP[2]].position).v);
00106 }
00107 Vec3D getNormal(const Vertices& pVertices) const {
00108 FV_ASSERT(aP[0] >= 0 && aP[0] < static_cast<int>(pVertices.size()));
00109 FV_ASSERT(aP[1] >= 0 && aP[1] < static_cast<int>(pVertices.size()));
00110 FV_ASSERT(aP[2] >= 0 && aP[2] < static_cast<int>(pVertices.size()));
00111 const Vec3D& lV0 = Vec3D(pVertices[aP[0]].position.v);
00112 const Vec3D& lV1 = Vec3D(pVertices[aP[1]].position.v);
00113 const Vec3D& lV2 = Vec3D(pVertices[aP[2]].position.v);
00114 return Vec3D(lV1-lV0) % (lV2-lV0);
00115 };
00116 };
00117
00118 typedef std::vector<Line> Lines;
00119 typedef std::vector<Point> Points;
00120 typedef std::vector<Quad> Quads;
00121 typedef std::vector<Triangle> Triangles;
00122 typedef Lines::size_type SizeType;
00123
00124
00125 void computeNormals ();
00126 void constructSimplified_Points();
00127 void constructSimplified_Heuristics();
00128 void renderFacetsFrame (const RenderParams& pParams);
00129 void renderFull (const RenderParams& pParams);
00130 void renderLines ();
00131 void renderLinesColored ();
00132 void renderPoints ();
00133 void renderPointsColored ();
00134 void renderQuads ();
00135 void renderQuadsColored ();
00136 void renderSimplified (const RenderParams& pParams);
00137 void renderTriangles ();
00138 void renderTrianglesColored();
00139
00140
00141 mutable BBox3D aBBox3D;
00142 const VtxAccumulator::Colors& aColors;
00143 Lines aLines;
00144 mutable SizeType aLinesBBoxCounter;
00145 Points aPoints;
00146 mutable SizeType aPointsBBoxCounter;
00147 Quads aQuads;
00148 mutable SizeType aQuadsBBoxCounter;
00149 VtxAccumulator::Normals& aNormals;
00150
00151 PrimitiveAccumulator* aSimplified;
00152 bool aSimplifiedDirty;
00153 bool aSimplifiedSelf;
00154 Triangles aTriangles;
00155 mutable SizeType aTrianglesBBoxCounter;
00156 const VtxAccumulator::Vertices& aVertices;
00157 int aPrimitiveOptimizerValue;
00158
00159 const VtxAccumulator& aVtxAccumulator;
00160 };
00161
00162 }
00163
00164
00165 #endif
00166