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