32 #ifndef PolygonSimplifier_INCLUDE_ONCE 33 #define PolygonSimplifier_INCLUDE_ONCE 81 a2 = w * n.
x() * n.
x();
82 ab = w * n.
x() * n.
y();
83 ac = w * n.
x() * n.
z();
85 b2 = w * n.
y() * n.
y();
86 bc = w * n.
y() * n.
z();
88 c2 = w * n.
z() * n.
z();
106 return dvec3( ad, bd, cd );
116 return v.
x()*v.
x()*a2 + 2*v.
x()*v.
y()*ab + 2*v.
x()*v.
z()*ac + 2*v.
x()*ad
117 + v.
y()*v.
y()*b2 + 2*v.
y()*v.
z()*bc + 2*v.
y()*bd
118 + v.
z()*v.
z()*c2 + 2*v.
z()*cd
129 v = -(Ainv*vector());
132 double A = c2*b2-bc*bc;
133 double B = bc*ac-c2*ab;
134 double C = bc*ab-b2*ac;
135 double det = a2*(A)+ab*(B)+ac*(C);
136 if (fabs(det) < 0.0000001)
140 double inv_det = 1.0 / det;
141 dmat3 Ainv( A*inv_det, B*inv_det, C*inv_det,
142 (ac*bc-c2*ab)*inv_det, (c2*a2-ac*ac)*inv_det, (ab*ac-bc*a2)*inv_det,
143 (bc*ab-ac*b2)*inv_det, (ac*ab-bc*a2)*inv_det, (b2*a2-ab*ab)*inv_det );
144 v = Ainv *
dvec3( -ad, -bd, -cd );
183 double a2, ab, ac,
ad;
204 inline void replaceVertex(
Vertex* oldv,
Vertex* newv );
205 inline void computeNormal();
206 inline float computeArea()
const;
207 inline float computePotentialArea(
const Vertex* oldv,
const Vertex* newv)
const;
208 inline fvec3 computePotentialNormal(
const Vertex* oldv,
const Vertex* newv)
const;
209 inline bool hasVertex(
const Vertex*v)
const;
210 inline bool checkTriangle()
const;
212 inline QErr computeQErr()
const;
244 Vertex(): mCollapseCost(0.0f), mOriginalIndex(-1) , mSimplifiedIndex(-1), mRemoveOrder(-1),
245 mRemoved(false), mProtected(false), mAlreadyProcessed(false)
249 inline void addAdjacentVertex(
Vertex* v);
250 inline void removeAdjacentVertex(
Vertex* v);
251 inline void computeAdjacentVertices();
252 inline bool checkConnectivity();
253 inline bool isAdjacentVertex(
Vertex*)
const;
254 inline bool isIncidentTriangle(
Triangle*)
const;
255 inline void discardRemovedTriangles();
256 inline void removeIncidentTriangle(
const Triangle*);
257 inline bool checkTriangles()
const;
258 inline void computeEdgePenalty();
324 void simplify(
const std::vector<fvec3>& in_verts,
const std::vector<int>& in_tris);
330 std::vector< u32 >&
targets() {
return mTargets; }
331 const std::vector< u32 >&
targets()
const {
return mTargets; }
333 std::vector< ref<Geometry> >&
output() {
return mOutput; }
334 const std::vector< ref<Geometry> >&
output()
const {
return mOutput; }
344 void clearTrianglesAndVertices();
352 bool quick()
const {
return mQuick; }
356 void outputSimplifiedGeometry();
357 inline void collapse(
Vertex* v);
358 inline void computeCollapseInfo(
Vertex* v);
372 std::vector<Triangle> mTriangleLump;
373 std::vector<Vertex> mVertexLump;
382 for(
int i=0; i<adjacentVerticesCount(); ++i)
383 if (mAdjacentVerts[i] == v)
385 mAdjacentVerts.push_back(v);
392 VL_CHECK( std::find(mAdjacentVerts.begin(), mAdjacentVerts.end(), v) != mAdjacentVerts.end() )
393 for(
int i=0; i<adjacentVerticesCount(); ++i)
394 if (mAdjacentVerts[i] == v)
396 mAdjacentVerts.erase(mAdjacentVerts.begin() + i);
403 mAdjacentVerts.clear();
404 for(
int itri=0; itri<incidentTrianglesCount(); ++itri)
406 VL_CHECK(!mIncidentTriangles[itri]->mRemoved)
407 addAdjacentVertex( mIncidentTriangles[itri]->mVertices[0] );
408 addAdjacentVertex( mIncidentTriangles[itri]->mVertices[1] );
409 addAdjacentVertex( mIncidentTriangles[itri]->mVertices[2] );
411 mRemoved = mAdjacentVerts.empty();
416 for(
int itri=incidentTrianglesCount(); itri--; )
417 if ( !incidentTriangle(itri)->checkTriangle() )
424 for(
int ivert=0; ivert<adjacentVerticesCount(); ++ivert)
428 for(
int itri=0; itri<incidentTrianglesCount() && edge_count<=1; ++itri)
430 if ( incidentTriangle(itri)->hasVertex( adjacentVertex(ivert) ) )
436 if ( edge_count == 1 )
438 fvec3 edge = position() - adjacentVertex(ivert)->position();
441 double d = -
dot(n,(
dvec3)position());
442 mQErr +=
QErr( n, d,
dot(edge, edge) * 1.0 );
448 for(
int itri=incidentTrianglesCount(); itri--; )
450 if (mIncidentTriangles[itri] == tri)
452 mIncidentTriangles.erase( mIncidentTriangles.begin() + itri );
460 for(
int itri=incidentTrianglesCount(); itri--; )
462 if (mIncidentTriangles[itri]->mRemoved)
463 mIncidentTriangles.erase( mIncidentTriangles.begin() + itri );
469 for(
int i=0; i<adjacentVerticesCount(); ++i)
470 if ( adjacentVertex(i) == v )
477 for(
int i=0; i<incidentTrianglesCount(); ++i)
478 if ( incidentTriangle(i) == t )
486 VL_CHECK( !mCollapseVertex->removed() )
488 for(
int ivert=0; ivert<adjacentVerticesCount(); ++ivert)
490 Vertex* adj = mAdjacentVerts[ivert];
504 double d = -
dot((
dvec3)vertex(0)->position(), n);
505 QErr qerr(n, d, computeArea() * (1.0 / 3.0) );
523 ok &= !mVertices[0]->removed();
VL_CHECK(ok)
524 ok &= !mVertices[1]->removed();
VL_CHECK(ok)
525 ok &= !mVertices[2]->removed();
VL_CHECK(ok)
526 ok &= mVertices[0] != mVertices[1];
VL_CHECK(ok)
527 ok &= mVertices[0] != mVertices[2];
VL_CHECK(ok)
528 ok &= mVertices[1] != mVertices[2];
VL_CHECK(ok)
534 return mVertices[0] == v || mVertices[1] == v || mVertices[2] == v;
548 AB = AB * (1.0f / base);
552 return base * height * 0.5f;
557 const fvec3& A = mVertices[0]->mPosition;
558 const fvec3& B = mVertices[1]->mPosition;
559 const fvec3& C = mVertices[2]->mPosition;
568 AB = AB * (1.0f / base);
574 return base * height * 0.5f;
579 const fvec3& a = mVertices[0]->mPosition;
580 fvec3 b = mVertices[1]->mPosition - a;
581 fvec3 c = mVertices[2]->mPosition - a;
582 mNormal =
cross(b,c);
589 mRemoved = hasVertex(newv);
598 if (mVertices[0] == oldv) mVertices[0] = newv;
599 if (mVertices[1] == oldv) mVertices[1] = newv;
600 if (mVertices[2] == oldv) mVertices[2] = newv;
626 for(
int itri=0; itri<v->incidentTrianglesCount(); ++itri)
654 ...
this is not correct since does not mark as removed the vertices that remain without triangles ...
655 mAdjacentVerts[ivert]->removeAdjacentVertex(
this);
656 mAdjacentVerts[ivert]->addAdjacentVertex(mCollapseVertex);
657 mCollapseVertex->addAdjacentVertex(mAdjacentVerts[ivert]);
738 dvec3 c = (a+b) * 0.5;
742 if (ae < be && ae < ce)
748 if (be < ae && be < ce)
761 int degenerate_count = 0;
791 float d2 =
dot( (
fvec3)solution - edgev[0]->position(), n );
828 float d2 =
dot( (
fvec3)solution - edgev[0]->position(), n );
835 if (degenerate_count)
844 if ( cost < v->mCollapseCost )
int incidentTrianglesCount() const
adjacent triangles
int simplifiedIndex() const
Internally used to regenerated the index buffer.
const Vector3 & normalize(T_Scalar *len=NULL)
Vector3< float > fvec3
A 3 components vector with float precision.
bool mAlreadyProcessed
internally used
float mCollapseCost
cost of the collapse
fvec3 mCollapsePosition
the collapse position
std::vector< int > mProtectedVerts
QErr operator+(const QErr &other)
const T_Scalar & z() const
std::vector< Vertex * > mSimplifiedVertices
A Triangle as defined by PolygonSimplifier.
void setRemoveDoubles(bool remove_doubles)
Vertex * mCollapseVertex
vertex to which collapse
bool removed() const
has the vertex been removed
T_Scalar getInverse(Matrix3 &dest) const
const fvec3 & collapsePosition() const
collapse position
int mRemoveOrder
when the vertex has collapsed
std::vector< ref< Geometry > > & output()
void computeEdgePenalty()
void addAdjacentVertex(Vertex *v)
bool checkTriangle() const
void removeIncidentTriangle(const Triangle *)
const Vertex * vertex(int index) const
vertices of the triangle
fvec3 cross(const fvec3 &v1, const fvec3 &v2)
const Geometry * input() const
std::vector< Vertex *> mAdjacentVerts
ajacent vertices
const QErr & qerr() const
Accumulated vertex error.
Vertex * collapseVertex() const
vertex to which collapse
void setQuick(bool quick)
bool isProtected() const
is the vertex protected?
#define VL_INSTRUMENT_CLASS(ClassName, BaseClass)
Triangle * simplifiedTriangles(int index) const
bool analyticSolution(dvec3 &v) const
The Geometry class is a Renderable that implements a polygonal mesh made of polygons, lines and points.
void setVerbose(bool verbose)
Visualization Library main namespace.
int mOriginalIndex
original index of this vertex
float dot(float a, float b)
int simplifiedVerticesCount() const
The Matrix3 class is a template class that implements a generic 3x3 matrix, see also vl::dmat3...
bool mProtected
is the vertex protected?
const std::vector< ref< Geometry > > & output() const
bool removed() const
ara of the triangle
std::vector< u32 > & targets()
The Vector3 class is a template class that implements a generic 3 components vector, see also vl::fvec3, vl::dvec3, vl::uvec3, vl::ivec3, vl::svec3, vl::usvec3, vl::bvec3, vl::ubvec3.
void discardRemovedTriangles()
const std::vector< u32 > & targets() const
bool mRemoved
has the vertex been removed
The base class for all the reference counted objects.
const fvec3 & normal() const
normal of the triangle
Matrix3< double > dmat3
A 3x3 matrix using double precision.
Implements the OpenGL Shading Language convenience functions for scalar and vector operations...
Vertex * simplifiedVertices(int index) const
The PolygonSimplifier class reduces the amount of polygons present in a Geometry using a quadric erro...
int originalIndex() const
original index of this vertex
void replaceVertex(Vertex *oldv, Vertex *newv)
Vector3< double > dvec3
A 3 components vector with double precision.
bool removeDoubles() const
std::vector< Triangle *> mIncidentTriangles
adjacent triangles
Triangle * incidentTriangle(int index) const
const T_Scalar & y() const
A Vertex as defined by PolygonSimplifier.
float collapseCost() const
cost of the collapse
void setQErr(const QErr &qerr)
void computeCollapseInfo(Vertex *v)
int adjacentVerticesCount() const
ajacent vertices
int simplifiedTrianglesCount() const
void removeAdjacentVertex(Vertex *v)
fvec3 computePotentialNormal(const Vertex *oldv, const Vertex *newv) const
void computeAdjacentVertices()
std::vector< u32 > mTargets
const QErr & operator+=(const QErr &other)
Vertex * vertex(int index)
#define VLGRAPHICS_EXPORT
const fvec3 & position() const
the position
int removeOrder() const
when the vertex has collapsed
float computePotentialArea(const Vertex *oldv, const Vertex *newv) const
std::vector< Triangle * > mSimplifiedTriangles
The quadric error metric as defined by PolygonSimplifier.
std::vector< ref< Geometry > > mOutput
int mSimplifiedIndex
only used during index buffer regeneration
const T_Scalar & x() const
fvec3 mPosition
the position
bool isAdjacentVertex(Vertex *) const
void setCollapsePosition(const fvec3 &pos)
The ref<> class is used to reference-count an Object.
bool isIncidentTriangle(Triangle *) const
bool mRemoved
ara of the triangle
float computeArea() const
void setProtectedVertices(const std::vector< int > &protected_verts)
bool hasVertex(const Vertex *v) const
QErr(const dvec3 &n, double d, double w=1.0)
bool checkTriangles() const
void addQErr(const QErr &qerr)
double evaluate(const dvec3 &v) const
void setIntput(Geometry *geom)
bool alreadyProcessed() const
Internally used.
Vertex * adjacentVertex(int index) const
fvec3 mNormal
normal of the triangle