fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / NodeCores / Drawables / Nurbs / Internal / OSGNurbsPatchSurface.h
blobbd2c97d00d98c41dbbe0e1df51e461933478d5cb
1 /*---------------------------------------------------------------------------*\
2 * OpenSG NURBS Library *
3 * *
4 * *
5 * Copyright (C) 2001-2006 by the University of Bonn, Computer Graphics Group*
6 * *
7 * http://cg.cs.uni-bonn.de/ *
8 * *
9 * contact: edhellon@cs.uni-bonn.de, guthe@cs.uni-bonn.de, rk@cs.uni-bonn.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
18 * *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
38 #ifndef _OSG_NurbsPatchSurface_h
39 #define _OSG_NurbsPatchSurface_h
40 #ifdef __sgi
41 #pragma once
42 #endif
44 #include "OSGDrawableDef.h"
45 #include "OSGConfig.h"
48 #include "OSGBaseTypes.h"
49 #include "OSGVector.h"
51 // DCTP-lib includes
52 #include "OSGdctptypes.h"
53 #include "OSGBezierCurve2D.h"
54 #include "OSGBSplineTrimmedSurface.h"
55 #include "OSGDCTPMesh.h"
57 // own includes
58 #include "OSGGraphTraverser.h"
59 #include "OSGErrorQuadTree.h" // needed due to arbitrary split -> new tree generation stuff
60 #include "OSGQuadTreeCreator.h" // needed due to quadtree moved back to class variable
61 #include "OSGParSpaceTrimmer.h" // needed due to triangle reduction at trimming curves
63 OSG_BEGIN_NAMESPACE
65 #define OSG_TIMING_FUNCS
67 static const unsigned char BEZIER_SURFACES_VALID = 0x01;
68 static const unsigned char QUAD_TREE_VALID = 0x02;
69 static const unsigned char TRIMMING_VALID = 0x04;
70 static const unsigned char TRIANGULATION_VALID = 0x08;
71 static const unsigned char INTERVALS_VALID = 0x10;
72 static const unsigned char TRIM_SEGS_VALID = 0x20;
74 static const unsigned char QUAD_TREE_ERROR = 0x80;
76 static const unsigned char QUAD_TREE_INVALID_MASK = BEZIER_SURFACES_VALID | TRIM_SEGS_VALID;
78 // Activate the OpenSG namespace
79 //OSG_USING_NAMESPACE
81 struct SCone
83 Vec3d clDirection;
84 // double dHalfAngle;
85 double dThreshold;
87 SCone(void) : clDirection(), dThreshold() {}
90 struct SSurface
92 double dError;
93 float fMinError;
94 float fMaxError;
95 unsigned char ucStatus;
96 bezier2ddequevector vvclBezierCurves;
97 bezier3ddequevector vvcl3DCurves;
98 std::vector<std::vector<Vec2d> > vvclEdgeLoops;
99 std::vector<std::vector<Vec3d> > vvclEdgeLoops3D;
100 // added to comply with ParSpaceTrimmer when using FORCE_NO_T_VERTICES
101 std::vector<std::vector<Vec3d> > vvclEdgeLoopsNorm;
102 std::vector<std::vector<Vec2d> > vvclEdgeLoopsTex;
103 Vec3d clMin;
104 Vec3d clMax;
105 CErrorQuadTree *pclErrorTree;
106 unsigned int uiTriangleCnt;
107 std::vector<bool> vbReversed;
108 std::vector<bool> vbUsed;
109 SCone tNormalCone;
110 #ifdef OSG_ARBITRARY_SPLIT
111 Vec2d clMinParam;
112 Vec2d clMaxParam;
113 #endif
115 SSurface(void):
116 dError ( 0.0),
117 fMinError ( 0.f),
118 fMaxError ( 0.f),
119 ucStatus ( 0),
120 vvclBezierCurves ( ),
121 vvcl3DCurves ( ),
122 vvclEdgeLoops ( ),
123 vvclEdgeLoops3D ( ),
124 // added to comply with ParSpaceTrimmer when using FORCE_NO_T_VERTICES
125 vvclEdgeLoopsNorm( ),
126 vvclEdgeLoopsTex ( ),
127 clMin ( ),
128 clMax ( ),
129 pclErrorTree (NULL),
130 uiTriangleCnt ( 0),
131 vbReversed ( ),
132 vbUsed ( ),
133 tNormalCone ( ),
134 #ifdef OSG_ARBITRARY_SPLIT
135 clMinParam ( ),
136 clMaxParam ( )
139 #endif
141 SSurface(const SSurface &source):
142 dError (source.dError ),
143 fMinError (source.fMinError ),
144 fMaxError (source.fMaxError ),
145 ucStatus (source.ucStatus ),
146 vvclBezierCurves (source.vvclBezierCurves ),
147 vvcl3DCurves (source.vvcl3DCurves ),
148 vvclEdgeLoops (source.vvclEdgeLoops ),
149 vvclEdgeLoops3D (source.vvclEdgeLoops3D ),
150 // added to comply with ParSpaceTrimmer when using FORCE_NO_T_VERTICES
151 vvclEdgeLoopsNorm(source.vvclEdgeLoopsNorm),
152 vvclEdgeLoopsTex (source.vvclEdgeLoopsTex ),
153 clMin (source.clMin ),
154 clMax (source.clMax ),
155 pclErrorTree (source.pclErrorTree ),
156 uiTriangleCnt (source.uiTriangleCnt ),
157 vbReversed (source.vbReversed ),
158 vbUsed (source.vbUsed ),
159 tNormalCone (source.tNormalCone ),
160 #ifdef OSG_ARBITRARY_SPLIT
161 clMinParam (source.clMinParam ),
162 clMaxParam (source.clMaxParam )
165 #endif
167 SSurface &operator=(const SSurface &rhs)
169 dError = rhs.dError;
170 fMinError = rhs.fMinError;
171 fMaxError = rhs.fMaxError;
172 ucStatus = rhs.ucStatus;
173 vvclBezierCurves = rhs.vvclBezierCurves;
174 vvcl3DCurves = rhs.vvcl3DCurves;
175 vvclEdgeLoops = rhs.vvclEdgeLoops;
176 vvclEdgeLoops3D = rhs.vvclEdgeLoops3D;
177 // added to comply with ParSpaceTrimmer when using FORCE_NO_T_VERTICES
178 vvclEdgeLoopsNorm = rhs.vvclEdgeLoopsNorm;
179 vvclEdgeLoopsTex = rhs.vvclEdgeLoopsTex;
180 clMin = rhs.clMin;
181 clMax = rhs.clMax;
182 pclErrorTree = rhs.pclErrorTree;
183 uiTriangleCnt = rhs.uiTriangleCnt;
184 vbReversed = rhs.vbReversed;
185 vbUsed = rhs.vbUsed;
186 tNormalCone = rhs.tNormalCone;
187 #ifdef OSG_ARBITRARY_SPLIT
188 clMinParam = rhs.clMinParam;
189 clMaxParam = rhs.clMaxParam;
190 #endif
192 return *this;
196 class OSG_DRAWABLE_DLLMAPPING CNurbsPatchSurface
199 public:
201 CNurbsPatchSurface();
203 ~CNurbsPatchSurface();
205 // add surface patch
206 void AddSurface(BSplineTrimmedSurface *clSurface);
208 // set error for given surface patch
209 void SetError(const unsigned int cuiSurface, double dError);
212 // return pointer to meshes and recalculate what is needed
213 // std::vector< NodePtr >* GetNodes( std::ofstream &meshout, std::ofstream &rclLinesOut, std::ofstream &rclLinesOut2 );
215 // std::vector< NodePtr >* GetNodes( void );
217 // returns the number of surfaces
218 unsigned int GetSurfaceNumber() const
220 return UInt32( m_vtSurfaces.size());
223 // returns if node is valid
224 bool IsNodeValid(unsigned int uiNode) const
226 return (m_vtSurfaces[uiNode].ucStatus & TRIANGULATION_VALID) ? true : false;
229 // new interface:
230 // set surface
231 void setSurface(BSplineTrimmedSurface *clSurface, std::vector<Pnt2f> &texturecps, bool bUseTextures = false);
232 // set error
233 void setError(double dError);
234 // get tessellated geometry
235 void getTessellation(std::vector<Pnt3f> & gverts,
236 std::vector<Vec3f> & norms,
237 std::vector<SimplePolygon> &tris,
238 bool usedelaunay);
239 void getTessellation(std::vector<Pnt3f> & gverts,
240 std::vector<Vec3f> & norms,
241 std::vector<Pnt2f> & texturecoords,
242 std::vector<SimplePolygon> &tris,
243 bool usedelaunay);
244 void getTessellation(std::vector<Pnt3f> & gverts,
245 std::vector<Pnt2f> & texturecoords,
246 std::vector<SimplePolygon> &tris,
247 bool usedelaunay);
248 void calculateNormals(std::vector<Vec3f> &norms);
249 void calculatePointsAndNormals(
250 std::vector<Pnt3f> & gverts,
251 std::vector<SimplePolygon> &tris,
252 std::vector<Vec3f> & norms);
253 void calculatePointsNormalsAndTextureCoords(
254 std::vector<Pnt3f> & gverts,
255 std::vector<SimplePolygon> &tris,
256 std::vector<Vec3f> & norms,
257 std::vector<Pnt2f> & texturecoords);
258 void calculatePointsAndTextureCoords(
259 std::vector<Pnt3f> & gverts,
260 std::vector<SimplePolygon> &tris,
261 std::vector<Pnt2f> & texturecoords);
264 // get internal representation (Surface + additional data)
265 inline SSurface * getSurfaceData()
267 return &m_vtSurfaces[0];
270 #ifdef OSG_TIMING_FUNCS
271 unsigned int GetTotalTriangleCnt() const
273 return m_uiTotalTriangleCnt;
275 unsigned int GetNewTriangleCnt() const
277 return m_uiNewTriangleCnt;
279 #endif
281 void setupErrorTree(double dError);
283 void clearEdgeloopNormals()
285 m_vtSurfaces[0].vvclEdgeLoopsNorm.clear();
288 private:
290 // convert surface to piecewise bezier form
291 void ConvertToBezier(unsigned int uiSurface);
293 // set up quad tree with saved error
294 void CalculateQuadTree(unsigned int uiSurface, bool bForEdges);
296 // calculate finest trimming curve
297 void CalculateTrimmingLoops(unsigned int uiSurface);
299 // calculate actual trimming curve
300 void CalculateActualTrimming(unsigned int uiSurface);
302 // perform graph traversal
303 void CalculateGraph(unsigned int uiSurface, bool usedelaunay);
305 // convert points from parameter to object space
306 void ConvertPoints(unsigned int uiSurface);
307 void convertPoints(std::vector<Pnt3f> & gverts,
308 std::vector<SimplePolygon> &tris);
310 // compute the normalcone (needed for backface culling)
311 void ComputeNormalCone(const unsigned int cuiSurface,
312 std::vector<Vec3f>& normals);
314 void Gen3DLoops(const unsigned int cuiSurface);
315 void CutCurve(const unsigned int cuiSurface, BezierCurve2D &rclCurve, bezier2ddeque &rclCut, std::vector<unsigned int> &rvuiUSeg, std::vector<unsigned int> &rvuiVSeg);
317 std::vector<BSplineTrimmedSurface> m_vclBSplineSurfaces;
318 std::vector<beziersurfacematrix> m_vvvclBezierSurfaces;
319 std::vector<std::vector<double> > m_vvdUParams;
320 std::vector<std::vector<double> > m_vvdVParams;
321 DCTPMesh m_clMesh;
322 std::vector<SSurface> m_vtSurfaces;
323 std::vector<Vec2d> m_vclParameterVertices;
324 std::vector<Vec3d> m_vclGlobalVertices;
325 // std::vector< Vec3d > m_vclNormal;
326 DirectedGraph<Vec2d, unsigned char> *m_pclGraph;
327 simplepolygonvector m_vclTriangles;
328 QuadTreeCreator *m_pclQuadTree;
329 bool m_bErrorTreeValid;
330 bool m_bUseTextures;
331 std::vector<std::vector<Vec2d> > m_vvTextureControlPoints;
333 #ifdef OSG_TIMING_FUNCS
334 unsigned int m_uiTotalTriangleCnt;
335 unsigned int m_uiNewTriangleCnt;
336 #endif
338 #ifdef OSG_KEEP_2D_POINTS
339 std::vector<unsigned int> m_vuiIndex;
340 #endif
342 private:
344 CNurbsPatchSurface(const CNurbsPatchSurface &other);
345 void operator=(const CNurbsPatchSurface &rhs);
348 OSG_END_NAMESPACE
350 #endif