source: GTP/trunk/Lib/Vis/Preprocessing/src/Polygon3.h @ 1420

Revision 1420, 4.4 KB checked in by mattausch, 18 years ago (diff)

corrected raycasting bug for triangles because of ill defined triangles

Line 
1#ifndef _Polygon3_h__
2#define _Polygon3_h__
3
4
5//#include <iostream>
6//#include <math.h>
7//#include "common.h"
8#include "Containers.h"
9#include "Mesh.h"
10#include <iomanip>
11
12namespace GtpVisibilityPreprocessor {
13
14class Polygon3;
15class Plane3;
16struct Face;
17class Intersectable;
18class AxisAlignedBox3;
19class Ray;
20//typedef Vertex3 Vector3;
21
22/** Class representing a planar convex polygon in 3d.
23*/
24class Polygon3
25{
26public:
27        enum {BACK_SIDE, FRONT_SIDE, SPLIT, COINCIDENT};
28
29        /** Default constructor creating an empty polygon.
30        */
31        Polygon3();
32        /** Constructor creating a polygon from the vertices.
33        */
34        Polygon3(const VertexContainer &vertices);
35        /** Creates a polygon and stores pointer to parent mesh
36                instance.
37        */
38        Polygon3(MeshInstance *parent);
39        /** A Triangle is converted to a polygon.
40        */
41        Polygon3(const Triangle3 &tri);
42
43        ~Polygon3() {DEL_PTR(mPlane);}
44       
45        /** Copies all the vertices of the face.
46        */
47        Polygon3(Face *face, Mesh *parentMesh);
48       
49        /** Returns supporting plane of this polygon.
50        */
51        Plane3 GetSupportingPlane(); //const;
52
53        /** Splits polygon.
54                @param partition the split plane
55                @param front returns the front the front polygon
56                @param back returns the back polygon
57                @param epsilon epsilon where two points are considered equal
58        */
59        void Split(const Plane3 &partition,
60                           Polygon3 &front,
61                           Polygon3 &back,
62                           const float epsilon);// = Limits::Small);
63
64        /** Returns the area of this polygon.
65        */
66        float GetArea() const;
67       
68        /** Classify polygon with respect to the plane.
69            @param epsilon tolerance value
70                @returns one of BACK_SIDE, FRONT_SIDE, SPLIT, COINCIDENT
71
72        */
73        int ClassifyPlane(const Plane3 &plane, const float epsilon) const;
74
75        /** Side of the polygon with respect to the plane.
76                @returns 1 if on front side, -1 if on back side, 0 else.
77        */
78        int Side(const Plane3 &plane, const float epsilon) const;
79
80        /** Scales the polygon about its center
81        */
82        void Scale(const float scale);
83       
84        /** Computes the center of mass of the polygon
85         */
86        Vector3 Center() const;
87
88        /** Checks if the polygon is valid, i.e., not degenerated.
89                @returns true if polygon is valid.
90        */
91        bool Valid(const float epsilon) const;
92
93        /** Returns the surface normal.
94        */
95        Vector3 GetNormal() const;
96
97        /** Casts ray to polygon.
98        */
99        int CastRay(const Ray &ray, float &t, const float nearestT);
100
101        /** The polygon is converted to triangles.
102        */
103        void Triangulate(vector<Triangle3> &triangles) const;
104
105        /**
106                Triangle strip indices are created from this polgon.
107        */
108        void Triangulate(VertexIndexContainer &indices) const;
109       
110        /** The piercing rays of the polygon are inherited by the child fragments
111                @parm front_piece the front fragment inheriting the front rays
112                @param back_piece the back fragment inheriting the back rays
113        */
114        void InheritRays(Polygon3 &front_piece,
115                                         Polygon3 &back_piece) const;
116
117        /** Returns new polygon with reverse orientation.
118        */
119        Polygon3 *CreateReversePolygon() const;
120
121        AxisAlignedBox3 GetBoundingBox() const;
122
123        ////////////////////////////////////////////
124
125        /** Classify polygons with respect to the plane.
126            @returns one of BACK_SIDE, FRONT_SIDE, SPLIT, COINCIDENT
127        */
128        static int ClassifyPlane(const PolygonContainer &polys,
129                                                         const Plane3 &plane,
130                                                         const float epsilon);
131
132        /** Counts the number of different intersectables associated with the polygons.
133        */
134        static int ParentObjectsSize(const PolygonContainer &polys);
135
136        /** Area of the accumulated polygons.
137        */
138        static float GetArea(const PolygonContainer &cell);
139
140        //////////////////////////////////////////////////////
141
142        /**
143                Adds polygon to mesh description as a face
144        */
145        friend void IncludePolyInMesh(const Polygon3 &poly, Mesh &mesh);
146
147
148        //////////////////////////////////////////
149       
150        /// vertices are connected in counterclockwise order.
151        VertexContainer mVertices;
152
153        /// we can also store materials with polygons
154        Material *mMaterial;
155       
156        /// pointer to the mesh instance this polygon is derived from
157        MeshInstance *mParent;
158
159        /// Rays piercing this polygon
160        RayContainer mPiercingRays;
161
162protected:
163
164        Plane3 *mPlane;
165};
166
167
168// Overload << operator for C++-style output
169inline ostream&
170operator<< (ostream &s, const Polygon3 &A)
171{
172        VertexContainer::const_iterator it;
173
174        //s << setprecision(6) << "Polygon:\n";
175        for (it = A.mVertices.begin(); it != A.mVertices.end(); ++it)
176                s << *it << " ";
177       
178        return s;
179}
180
181}
182
183#endif
Note: See TracBrowser for help on using the repository browser.