0022815: Missing delete operator for placement new
[occt.git] / src / NIS / NIS_Triangulated.hxx
1 // File:      NIS_Triangulated.hxx
2 // Created:   17.07.07 08:42
3 // Author:    Alexander GRIGORIEV
4 // Copyright: Open Cascade 2007
5
6
7 #ifndef NIS_Triangulated_HeaderFile
8 #define NIS_Triangulated_HeaderFile
9
10 #include <NIS_InteractiveObject.hxx>
11 #include <Quantity_Color.hxx>
12
13 class Handle_NIS_TriangulatedDrawer;
14 class NCollection_BaseAllocator;
15 class Handle_NCollection_BaseAllocator;
16 class NIS_TriangulatedDrawer;
17
18 /**
19  * Interactive object that consists of triangles, lines and polygons without
20  * normals. Particularly can be used to render planar 2D shapes.
21  *
22  * @par 2D and 3D model
23  * Vertices are stored in an array of float numbers, 2 or 3 numbers per vertex.
24  * The number of dimensions is defined in the constructor, see the parameter
25  * 'is2D'. When 2D is defined then for all vertices the Z coordinate is 0.
26  * To display planar objects in a plane different from XOY you should subclass
27  * this type together with the correponding Drawer and store the transformation
28  * parameters. In Drawer subclass either in method BeforeDraw() or in method
29  * Draw() you would call glTranslate() or glMultMatrix() so that all vertices
30  * should be located in their proper positions.
31  *
32  * @par Compressed storage
33  * For efficient memory utilization, indice (triangles, segments, polygons) are
34  * 8-bit, 16-bit or 32-bit numbers. The width of this numeric representation is
35  * chosen automatically when the total number of nodes is passed in the
36  * constructor or in any Set* method. For example, if this number of nodes is
37  * smaller than 256 then 8-bit representation is selected. The choice is stored
38  * in 'myIndexType' data member.
39  */
40
41 class NIS_Triangulated : public NIS_InteractiveObject
42 {
43  protected:
44   /**
45    * Constants defining the mode (type) of presentation. They allow mixed type,
46    * e.g., Triangulation+Line. Line and Segments are not mixable, their mix is
47    * treated as Line only.
48    */
49   enum {
50     Type_None          =  0,
51     Type_Loop          =  1,  //!< modifier to Line
52     Type_Line          =  2,
53     Type_Segments      =  4,
54     Type_Triangulation =  8,
55     Type_Polygons      = 16
56   };
57
58  public:
59   /**
60    * Enumerated type of polygon rendering.
61    */
62   enum PolygonType {
63     Polygon_Default  = 0,  //!< Polygon as LINE, Triangulation as FILL
64     Polygon_Line     = 1,  //!< Both Polygon and Triangulation as LINE
65     Polygon_Fill     = 2   //!< Both Polygon and Triangulation as FILL
66   };
67
68  public:
69   // ---------- PUBLIC METHODS ----------
70
71
72   /**
73    * Constructor. Optionally defines the number of nodes that will be allocated
74    * (this number may be defined later in methods Set*Prs) as well as the
75    * memory allocator where the nodes, lines and triangles will be stored by
76    * this instance.
77    * @param nNodes
78    *   Total number of nodes that will be initialized for this object
79    * @param is2D
80    *   If true then the nodes will be 2D in plane Z=0, otherwise normal 3D.
81    * @param theAlloc
82    *   Allocator for internal data
83    */
84   Standard_EXPORT NIS_Triangulated(const Standard_Integer nNodes = 0,
85                                    const Standard_Boolean is2D = Standard_False,
86                                    const Handle_NCollection_BaseAllocator&
87                                    theAlloc = 0L);
88
89   /**
90    * Define the polygonal presentration.
91    * @param nPolygons
92    *   Number of separate polygons. If set to 0, polygons are cancelled
93    * @param nNodes
94    *   Optional: Number of nodes to allocate. If left as 0, the previous nodes
95    *   allocation is used, otherwise a new allocation is created.
96    */ 
97   Standard_EXPORT void              SetPolygonsPrs
98                                         (const Standard_Integer nPolygons,
99                                          const Standard_Integer nNodes = 0);
100
101   /**
102    * Define the triangulated presentration.
103    * @param nTriangles
104    *   Number of triangles. If set to 0, triangulation is cancelled
105    * @param nNodes
106    *   Optional: Number of nodes to allocate. If left as 0, the previous nodes
107    *   allocation is used, otherwise a new allocation is created.
108    */ 
109   Standard_EXPORT void              SetTriangulationPrs
110                                         (const Standard_Integer nTriangles,
111                                          const Standard_Integer nNodes = 0);
112
113   /**
114    * Define the line presentration (polygon through points)
115    * @param nPoints
116    *   Number of nodes defining the line. If set to 0, line is cancelled
117    * @param isClosed
118    *   True if the polygon is closed, so the segment between the first and
119    *   the last points is created automatically.
120    * @param nNodes
121    *   Optional: Number of nodes to allocate. If left as 0, the previous nodes
122    *   allocation is used, otherwise a new allocation is created.
123    */ 
124   Standard_EXPORT void              SetLinePrs
125                                         (const Standard_Integer nPoints,
126                                          const Standard_Boolean isClosed,
127                                          const Standard_Integer nNodes = 0);
128
129   /**
130    * Define the segments presentration. Each segment is defined by 2 nodes
131    * @param nSegments
132    *   Number of segments. If set to 0, segments presentation is cancelled
133    * @param nNodes
134    *   Optional: Number of nodes to allocate. If left as 0, the previous nodes
135    *   allocation is used, otherwise a new allocation is created.
136    */ 
137   Standard_EXPORT void              SetSegmentPrs
138                                         (const Standard_Integer nSegments,
139                                          const Standard_Integer nNodes = 0);
140
141   /**
142    * Query if there is Triangulation component in the presentation.
143    */
144   inline Standard_Boolean           IsTriangulation () const
145   { return (myType & Type_Triangulation) != 0; }
146
147   /**
148    * Query if there is Polygons component in the presentation.
149    */
150   inline Standard_Boolean           IsPolygons  () const
151   { return (myType & Type_Polygons) != 0; }
152
153   /**
154    * Query if there is Line component in the presentation.
155    */
156   inline Standard_Boolean           IsLine      (Standard_Boolean& isLoop) const
157   { isLoop = (myType & Type_Loop) != 0; return (myType & Type_Line) != 0; }
158
159   /**
160    * Query if there is Segments component in the presentation.
161    */
162   inline Standard_Boolean           IsSegments  () const
163   { return (myType & Type_Segments) != 0; }
164
165   /**
166    * Reset all data memebers and free all allocated memory.
167    * Called from the destructor, also can be usedto re-initialize a given
168    * Interactive Objects. 
169    */
170   Standard_EXPORT void              Clear       ();
171
172   /**
173    * Destructor.
174    */
175   Standard_EXPORT virtual ~NIS_Triangulated     ();
176
177   /**
178    * Create a default drawer instance.
179    */
180   Standard_EXPORT virtual NIS_Drawer *
181                                     DefaultDrawer (NIS_Drawer *) const;
182
183   /**
184    * Define the coordinates of node [ind].
185    */
186   Standard_EXPORT void              SetNode   (const Standard_Integer  ind,
187                                                const gp_XYZ&           thePnt);
188
189   /**
190    * Define the coordinates of node [ind]. Z coordinate is assigned to 0.
191    */
192   Standard_EXPORT void              SetNode   (const Standard_Integer  ind,
193                                                const gp_XY&            thePnt);
194
195   /**
196    * Define the triangle [ind] by indices of its three nodes.
197    */
198   Standard_EXPORT void              SetTriangle(const Standard_Integer  ind,
199                                                 const Standard_Integer  iNode0,
200                                                 const Standard_Integer  iNode1,
201                                                 const Standard_Integer  iNode2);
202
203   /**
204    * Allocate a single polygon, should be called for each polygon following
205    * the call SetPolygonsPrs(). The polygon can be filled by node indices using
206    * the method SetPolygonNode().
207    * @param ind
208    *   Index of the polygon, should be [0..Npolygons-1]
209    * @param theSz
210    *   Number of points (segments) in the polygon.
211    */
212   Standard_EXPORT void              SetPolygon (const Standard_Integer  ind,
213                                                 const Standard_Integer  theSz);
214
215   /**
216    * Define the line node by index.
217    */
218   Standard_EXPORT void              SetLineNode(const Standard_Integer ind,
219                                                 const Standard_Integer iNode);
220
221   /**
222    * Query the number of nodes.
223    */
224   inline Standard_Integer           NNodes    () const
225   { return myNNodes; }
226
227   /**
228    * Query the number of triangles.
229    */
230   inline Standard_Integer           NTriangles() const
231   { return myNTriangles; }
232
233   /**
234    * Query the number of line points.
235    */
236   inline Standard_Integer           NLineNodes() const
237   { return myNLineNodes; }
238
239   /**
240    * Query the number of polygons.
241    */
242   inline Standard_Integer           NPolygons () const
243   { return static_cast<Standard_Integer>(myNPolygons); }
244
245   /**
246    * Query the node by its index.
247    * @return
248    *   pointer to array of 2 or 3 Standard_ShortReal values {X,Y(,Z) coord}
249    */
250   inline const Standard_ShortReal * Node      (const Standard_Integer ind) const
251   { return &mypNodes[ind * myNodeCoord]; }
252
253   /**
254    * Define the node of a polygon by index.
255    * @param indPoly
256    *   Index of the Polygon, should be less than the number of polygons that is
257    *   defined in SetPolygonsPrs() and can be returned by NPOlygons().
258    * @param ind
259    *   Index of the node in the Polygon. Should be less than the parameter theSz
260    *   in the corresponding previous SetPolygon() call.
261    * @param iNode
262    *   Index of the node in the given position of the Polygon. 
263    */
264   Standard_EXPORT void              SetPolygonNode
265                                               (const Standard_Integer indPoly,
266                                                const Standard_Integer ind,
267                                                const Standard_Integer iNode);
268
269   /**
270    * Get the node with index 'ind' from the polygon number 'indPoly'.
271    */
272   Standard_EXPORT Standard_Integer PolygonNode(const Standard_Integer indPoly,
273                                                const Standard_Integer ind)const;
274
275   /**
276    * Get the number of nodes for the polygon number 'indPoly'.
277    */
278   Standard_EXPORT Standard_Integer NPolygonNodes
279                                          (const Standard_Integer indPoly)const;
280
281   /**
282    * Set the boolean flag defining if the polygons or the triangulation
283    * should be drawn. This method does not affect the presentation of
284    * Line/Segments.
285    * @param isDrawPolygons
286    *   True defines that no triangulation is drawn, only polygons are. False
287    *   defines that only triangulation is drawn, no polygons.
288    */
289   Standard_EXPORT void              SetDrawPolygons
290                                         (const Standard_Boolean isDrawPolygons);
291   /**
292    * Set the type of polygon rendering.
293    */
294   Standard_EXPORT void              SetPolygonType
295                                         (const PolygonType      theType);
296
297   /**
298    * Set the normal color for presentation.
299    * @param theColor
300    *   New color to use for the presentation.
301    */
302   Standard_EXPORT void              SetColor  (const Quantity_Color&  theColor);
303
304   /**
305    * Get Normal, Transparent or Hilighted color of the presentation.
306    * @param theDrawType
307    *   The draw type, for which the color is retrieved.
308    */
309   Standard_EXPORT Quantity_Color GetColor  
310                           (const NIS_Drawer::DrawType theDrawType) const;
311
312   /**
313    * Set the color for hilighted presentation.
314    * @param theColor
315    *   New color to use for the presentation.
316    */
317   Standard_EXPORT void      SetHilightColor   (const Quantity_Color&  theColor);
318
319   /**
320    * Set the color for dynamic hilight presentation.
321    * @param theColor
322    *   New color to use for the presentation.
323    */
324   Standard_EXPORT void      SetDynHilightColor(const Quantity_Color&  theColor);
325
326   /**
327    * Set the width of line presentations in pixels.
328    * @param theWidth
329    *   New line width to use for the presentation.
330    */
331   Standard_EXPORT void      SetLineWidth      (const Standard_Real    theWidth);
332
333   /**
334    * Create a copy of theObject except its ID.
335    * @param theAll
336    *   Allocator where the Dest should store its private data.
337    * @param theDest
338    *   <tt>[in-out]</tt> The target object where the data are copied. If
339    *   passed NULL then the target should be created.
340    */
341   Standard_EXPORT virtual void
342                           Clone (const Handle_NCollection_BaseAllocator& theAll,
343                                  Handle_NIS_InteractiveObject& theDest) const;
344
345   /**
346    * Intersect the InteractiveObject geometry with a line/ray.
347    * @param theAxis
348    *   The line or ray in 3D space.
349    * @param theOver
350    *   Half-thickness of the selecting line.
351    * @return
352    *   If the return value is more than 0.1*RealLast() then no intersection is
353    *   detected. Otherwise returns the coordinate of thePnt on the ray. May be
354    *   negative.
355    */
356   Standard_EXPORT virtual Standard_Real
357                             Intersect       (const gp_Ax1&       theAxis,
358                                              const Standard_Real theOver) const;
359
360   /**
361    * Intersect the InteractiveObject geometry with an oriented box.
362    * @param theBox
363    *   3D box of selection
364    * @param theTrf
365    *   Position/Orientation of the box.
366    * @param isFull
367    *   True if full inclusion is required, False - if partial.
368    * @return
369    *   True if the InteractiveObject geometry intersects the box or is inside it
370    */
371   Standard_EXPORT virtual Standard_Boolean
372                              Intersect     (const Bnd_B3f&         theBox,
373                                             const gp_Trsf&         theTrf,
374                                             const Standard_Boolean isFull)const;
375
376   /**
377    * Intersect the InteractiveObject geometry with a selection polygon.
378    * @param thePolygon
379    *   the list of vertices of a free-form closed polygon without
380    *   self-intersections. The last point should not coincide with the first
381    *   point of the list. Any two neighbor points should not be confused.
382    * @param theTrf
383    *   Position/Orientation of the polygon.
384    * @param isFullIn
385    *   True if full inclusion is required, False - if partial.
386    * @return
387    *   True if the InteractiveObject geometry intersects the polygon or is
388    *   inside it
389    */
390   Standard_EXPORT virtual Standard_Boolean Intersect
391                     (const NCollection_List<gp_XY> &thePolygon,
392                      const gp_Trsf                 &theTrf,
393                      const Standard_Boolean         isFullIn) const;
394
395   Standard_EXPORT static int tri_line_intersect  (const double      start[3],
396                                                   const double      dir[3],
397                                                   const float       V0[3],
398                                                   const float       V1[3],
399                                                   const float       V2[3],
400                                                   double            * tInter);
401
402   Standard_EXPORT static int tri2d_line_intersect(const double      start[3],
403                                                   const double      dir[3],
404                                                   const float       V0[2],
405                                                   const float       V1[2],
406                                                   const float       V2[2],
407                                                   double            * tInter);
408
409   Standard_EXPORT static int seg_line_intersect  (const gp_XYZ&     aStart,
410                                                   const gp_XYZ&     aDir,
411                                                   const double      over2,
412                                                   const float       V0[3],
413                                                   const float       V1[3],
414                                                   double            * tInter);
415
416   Standard_EXPORT static int seg2d_line_intersect(const gp_XYZ&     aStart,
417                                                   const gp_XYZ&     aDir,
418                                                   const double      over2,
419                                                   const float       V0[2],
420                                                   const float       V1[2],
421                                                   double            * tInter);
422
423   Standard_EXPORT static int seg_box_intersect  (const Bnd_B3f&    theBox,
424                                                  const gp_Pnt      thePnt[2]);
425
426   Standard_EXPORT static int seg_box_included   (const Bnd_B3f&    theBox,
427                                                  const gp_Pnt      thePnt[2]);
428
429   Standard_EXPORT static int seg_polygon_intersect
430                               (const NCollection_List<gp_XY> &thePolygon,
431                                const gp_XY                    thePnt[2]);
432
433   Standard_EXPORT static int seg_polygon_included
434                               (const NCollection_List<gp_XY> &thePolygon,
435                                const gp_XY                    thePnt[2]);
436
437   Standard_EXPORT static void ComputeBox    (Bnd_B3f&                  theBox,
438                                              const Standard_Integer    nNodes,
439                                              const Standard_ShortReal* pNodes,
440                                              const Standard_Integer    nCoord);
441
442   /**
443    * Classification of thePoint with respect to thePolygon.
444    * @param thePolygon
445    *   the list of vertices of a free-form closed polygon without
446    *   self-intersections. The last point should not coincide with the first
447    *   point of the list. Any two neighbor points should not be confused.
448    * @param thePoint
449    *   the point to be classified.
450    * @return
451    *   Standard_True if thePoint in inside thePolygon or lies on its boundary.
452    */
453   Standard_EXPORT static Standard_Boolean
454                  IsIn (const NCollection_List<gp_XY> &thePolygon,
455                        const gp_XY                   &thePoint);
456
457   /**
458    * Implements deallocation of the object instance
459    */
460   Standard_EXPORT virtual void Delete () const; 
461
462  protected:
463
464   /**
465    * Allocator-based operator new for dynamic allocations in method Clone()
466    */
467   DEFINE_STANDARD_ALLOC
468   DEFINE_NCOLLECTION_ALLOC
469
470   /**
471    * Create a 3D bounding box of the object.
472    */
473   Standard_EXPORT virtual void computeBox     ();
474
475   /**
476    * Create the memory buffer for the declared number of nodes, old nodes
477    * are deallocated.
478    */
479   Standard_EXPORT void         allocateNodes  (const Standard_Integer nNodes);
480
481   /**
482    * Get the node pointed by the i-th index in the array.
483    */
484   Standard_EXPORT gp_Pnt       nodeAtInd      (const Standard_Integer * theArr,
485                                                const Standard_Integer i) const;
486
487   /**
488    * Get the node pointed by the i-th index in the array.
489    */
490   Standard_EXPORT float*       nodeArrAtInd   (const Standard_Integer * theArr,
491                                                const Standard_Integer i) const;
492
493  protected:
494   // ---------- PROTECTED FIELDS ----------
495
496   NCollection_BaseAllocator  * myAlloc; //!< Usually from InteractiveContext
497   Standard_Integer           myType;    //!< Combination of Type_* constants
498   Standard_ShortReal         *  mypNodes;
499   Standard_Integer           *  mypTriangles;
500   Standard_Integer           *  mypLines;
501   Standard_Integer           ** mypPolygons;
502   Standard_Integer           myNNodes;
503   Standard_Integer           myNTriangles;
504   Standard_Integer           myNLineNodes;
505   unsigned int               myNPolygons      : 24;
506   Standard_Boolean           myIsDrawPolygons : 1;
507   Standard_Boolean           myIsCloned       : 1; //!< How it is allocated
508   unsigned int               myIndexType      : 2; //!< 0:8bit, 1:16bit, 2:32bit
509   unsigned int               myNodeCoord      : 2; //!< 2 or 3 coordinates
510   unsigned int               myPolygonType    : 2;
511
512  public:
513 // Declaration of CASCADE RTTI
514 DEFINE_STANDARD_RTTI (NIS_Triangulated)
515
516   friend class NIS_TriangulatedDrawer;
517 };
518
519 // Definition of HANDLE object using Standard_DefineHandle.hxx
520 DEFINE_STANDARD_HANDLE (NIS_Triangulated, NIS_InteractiveObject)
521
522 #endif