1 // Created on: 2007-07-17
2 // Created by: Alexander GRIGORIEV
3 // Copyright (c) 2007-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #ifndef NIS_Triangulated_HeaderFile
17 #define NIS_Triangulated_HeaderFile
19 #include <NIS_InteractiveObject.hxx>
20 #include <Quantity_Color.hxx>
22 class Handle(NIS_TriangulatedDrawer);
23 class NCollection_BaseAllocator;
24 class Handle(NCollection_BaseAllocator);
25 class NIS_TriangulatedDrawer;
28 * Interactive object that consists of triangles, lines and polygons without
29 * normals. Particularly can be used to render planar 2D shapes.
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.
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.
50 class NIS_Triangulated : public NIS_InteractiveObject
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.
60 Type_Loop = 1, //!< modifier to Line
63 Type_Triangulation = 8,
69 * Enumerated type of polygon rendering.
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
78 // ---------- PUBLIC METHODS ----------
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
87 * Total number of nodes that will be initialized for this object
89 * If true then the nodes will be 2D in plane Z=0, otherwise normal 3D.
91 * Allocator for internal data
93 Standard_EXPORT NIS_Triangulated(const Standard_Integer nNodes = 0,
94 const Standard_Boolean is2D = Standard_False,
95 const Handle(NCollection_BaseAllocator)&
99 * Define the polygonal presentration.
101 * Number of separate polygons. If set to 0, polygons are cancelled
103 * Optional: Number of nodes to allocate. If left as 0, the previous nodes
104 * allocation is used, otherwise a new allocation is created.
106 Standard_EXPORT void SetPolygonsPrs
107 (const Standard_Integer nPolygons,
108 const Standard_Integer nNodes = 0);
111 * Define the triangulated presentration.
113 * Number of triangles. If set to 0, triangulation is cancelled
115 * Optional: Number of nodes to allocate. If left as 0, the previous nodes
116 * allocation is used, otherwise a new allocation is created.
118 Standard_EXPORT void SetTriangulationPrs
119 (const Standard_Integer nTriangles,
120 const Standard_Integer nNodes = 0);
123 * Define the line presentration (polygon through points)
125 * Number of nodes defining the line. If set to 0, line is cancelled
127 * True if the polygon is closed, so the segment between the first and
128 * the last points is created automatically.
130 * Optional: Number of nodes to allocate. If left as 0, the previous nodes
131 * allocation is used, otherwise a new allocation is created.
133 Standard_EXPORT void SetLinePrs
134 (const Standard_Integer nPoints,
135 const Standard_Boolean isClosed,
136 const Standard_Integer nNodes = 0);
139 * Define the segments presentration. Each segment is defined by 2 nodes
141 * Number of segments. If set to 0, segments presentation is cancelled
143 * Optional: Number of nodes to allocate. If left as 0, the previous nodes
144 * allocation is used, otherwise a new allocation is created.
146 Standard_EXPORT void SetSegmentPrs
147 (const Standard_Integer nSegments,
148 const Standard_Integer nNodes = 0);
151 * Query if there is Triangulation component in the presentation.
153 inline Standard_Boolean IsTriangulation () const
154 { return (myType & Type_Triangulation) != 0; }
157 * Query if there is Polygons component in the presentation.
159 inline Standard_Boolean IsPolygons () const
160 { return (myType & Type_Polygons) != 0; }
163 * Query if there is Line component in the presentation.
165 inline Standard_Boolean IsLine (Standard_Boolean& isLoop) const
166 { isLoop = (myType & Type_Loop) != 0; return (myType & Type_Line) != 0; }
169 * Query if there is Segments component in the presentation.
171 inline Standard_Boolean IsSegments () const
172 { return (myType & Type_Segments) != 0; }
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.
179 Standard_EXPORT void Clear ();
184 Standard_EXPORT virtual ~NIS_Triangulated ();
187 * Create a default drawer instance.
189 Standard_EXPORT virtual NIS_Drawer *
190 DefaultDrawer (NIS_Drawer *) const;
193 * Define the coordinates of node [ind].
195 Standard_EXPORT void SetNode (const Standard_Integer ind,
196 const gp_XYZ& thePnt);
199 * Define the coordinates of node [ind]. Z coordinate is assigned to 0.
201 Standard_EXPORT void SetNode (const Standard_Integer ind,
202 const gp_XY& thePnt);
205 * Define the triangle [ind] by indices of its three nodes.
207 Standard_EXPORT void SetTriangle(const Standard_Integer ind,
208 const Standard_Integer iNode0,
209 const Standard_Integer iNode1,
210 const Standard_Integer iNode2);
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().
217 * Index of the polygon, should be [0..Npolygons-1]
219 * Number of points (segments) in the polygon.
221 Standard_EXPORT void SetPolygon (const Standard_Integer ind,
222 const Standard_Integer theSz);
225 * Define the line node by index.
227 Standard_EXPORT void SetLineNode(const Standard_Integer ind,
228 const Standard_Integer iNode);
231 * Query the number of nodes.
233 inline Standard_Integer NNodes () const
237 * Query the number of triangles.
239 inline Standard_Integer NTriangles() const
240 { return myNTriangles; }
243 * Query the number of line points.
245 inline Standard_Integer NLineNodes() const
246 { return myNLineNodes; }
249 * Query the number of polygons.
251 inline Standard_Integer NPolygons () const
252 { return static_cast<Standard_Integer>(myNPolygons); }
255 * Query the node by its index.
257 * pointer to array of 2 or 3 Standard_ShortReal values {X,Y(,Z) coord}
259 inline const Standard_ShortReal * Node (const Standard_Integer ind) const
260 { return &mypNodes[ind * myNodeCoord]; }
263 * Define the node of a polygon by index.
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().
268 * Index of the node in the Polygon. Should be less than the parameter theSz
269 * in the corresponding previous SetPolygon() call.
271 * Index of the node in the given position of the Polygon.
273 Standard_EXPORT void SetPolygonNode
274 (const Standard_Integer indPoly,
275 const Standard_Integer ind,
276 const Standard_Integer iNode);
279 * Get the node with index 'ind' from the polygon number 'indPoly'.
281 Standard_EXPORT Standard_Integer PolygonNode(const Standard_Integer indPoly,
282 const Standard_Integer ind)const;
285 * Get the number of nodes for the polygon number 'indPoly'.
287 Standard_EXPORT Standard_Integer NPolygonNodes
288 (const Standard_Integer indPoly)const;
291 * Set the boolean flag defining if the polygons or the triangulation
292 * should be drawn. This method does not affect the presentation of
294 * @param isDrawPolygons
295 * True defines that no triangulation is drawn, only polygons are. False
296 * defines that only triangulation is drawn, no polygons.
298 Standard_EXPORT void SetDrawPolygons
299 (const Standard_Boolean isDrawPolygons);
301 * Set the type of polygon rendering.
303 Standard_EXPORT void SetPolygonType
304 (const PolygonType theType);
307 * Set the normal color for presentation.
309 * New color to use for the presentation.
311 Standard_EXPORT void SetColor (const Quantity_Color& theColor);
314 * Get Normal, Transparent or Hilighted color of the presentation.
316 * The draw type, for which the color is retrieved.
318 Standard_EXPORT Quantity_Color GetColor
319 (const NIS_Drawer::DrawType theDrawType) const;
322 * Set the color for hilighted presentation.
324 * New color to use for the presentation.
326 Standard_EXPORT void SetHilightColor (const Quantity_Color& theColor);
329 * Set the color for dynamic hilight presentation.
331 * New color to use for the presentation.
333 Standard_EXPORT void SetDynHilightColor(const Quantity_Color& theColor);
336 * Set the width of line presentations in pixels.
338 * New line width to use for the presentation.
340 Standard_EXPORT void SetLineWidth (const Standard_Real theWidth);
343 * Create a copy of theObject except its ID.
345 * Allocator where the Dest should store its private data.
347 * <tt>[in-out]</tt> The target object where the data are copied. If
348 * passed NULL then the target should be created.
350 Standard_EXPORT virtual void
351 Clone (const Handle(NCollection_BaseAllocator)& theAll,
352 Handle(NIS_InteractiveObject)& theDest) const;
355 * Intersect the InteractiveObject geometry with a line/ray.
357 * The line or ray in 3D space.
359 * Half-thickness of the selecting line.
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
365 Standard_EXPORT virtual Standard_Real
366 Intersect (const gp_Ax1& theAxis,
367 const Standard_Real theOver) const;
370 * Intersect the InteractiveObject geometry with an oriented box.
372 * 3D box of selection
374 * Position/Orientation of the box.
376 * True if full inclusion is required, False - if partial.
378 * True if the InteractiveObject geometry intersects the box or is inside it
380 Standard_EXPORT virtual Standard_Boolean
381 Intersect (const Bnd_B3f& theBox,
382 const gp_Trsf& theTrf,
383 const Standard_Boolean isFull)const;
386 * Intersect the InteractiveObject geometry with a selection polygon.
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.
392 * Position/Orientation of the polygon.
394 * True if full inclusion is required, False - if partial.
396 * True if the InteractiveObject geometry intersects the polygon or is
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;
404 Standard_EXPORT static int tri_line_intersect (const double start[3],
411 Standard_EXPORT static int tri2d_line_intersect(const double start[3],
418 Standard_EXPORT static int seg_line_intersect (const gp_XYZ& aStart,
425 Standard_EXPORT static int seg2d_line_intersect(const gp_XYZ& aStart,
432 Standard_EXPORT static int seg_box_intersect (const Bnd_B3f& theBox,
433 const gp_Pnt thePnt[2]);
435 Standard_EXPORT static int seg_box_included (const Bnd_B3f& theBox,
436 const gp_Pnt thePnt[2]);
438 Standard_EXPORT static int seg_polygon_intersect
439 (const NCollection_List<gp_XY> &thePolygon,
440 const gp_XY thePnt[2]);
442 Standard_EXPORT static int seg_polygon_included
443 (const NCollection_List<gp_XY> &thePolygon,
444 const gp_XY thePnt[2]);
446 Standard_EXPORT static void ComputeBox (Bnd_B3f& theBox,
447 const Standard_Integer nNodes,
448 const Standard_ShortReal* pNodes,
449 const Standard_Integer nCoord);
452 * Classification of thePoint with respect to 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.
458 * the point to be classified.
460 * Standard_True if thePoint in inside thePolygon or lies on its boundary.
462 Standard_EXPORT static Standard_Boolean
463 IsIn (const NCollection_List<gp_XY> &thePolygon,
464 const gp_XY &thePoint);
467 * Implements deallocation of the object instance
469 Standard_EXPORT virtual void Delete () const;
474 * Allocator-based operator new for dynamic allocations in method Clone()
476 DEFINE_STANDARD_ALLOC
477 DEFINE_NCOLLECTION_ALLOC
480 * Create a 3D bounding box of the object.
482 Standard_EXPORT virtual void computeBox ();
485 * Create the memory buffer for the declared number of nodes, old nodes
488 Standard_EXPORT void allocateNodes (const Standard_Integer nNodes);
491 * Get the node pointed by the i-th index in the array.
493 Standard_EXPORT gp_Pnt nodeAtInd (const Standard_Integer * theArr,
494 const Standard_Integer i) const;
497 * Get the node pointed by the i-th index in the array.
499 Standard_EXPORT float* nodeArrAtInd (const Standard_Integer * theArr,
500 const Standard_Integer i) const;
503 // ---------- PROTECTED FIELDS ----------
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;
522 // Declaration of CASCADE RTTI
523 DEFINE_STANDARD_RTTI (NIS_Triangulated)
525 friend class NIS_TriangulatedDrawer;
528 // Definition of HANDLE object using Standard_DefineHandle.hxx
529 DEFINE_STANDARD_HANDLE (NIS_Triangulated, NIS_InteractiveObject)