1 // File: NIS_Triangulated.hxx
2 // Created: 17.07.07 08:42
3 // Author: Alexander GRIGORIEV
4 // Copyright: Open Cascade 2007
7 #ifndef NIS_Triangulated_HeaderFile
8 #define NIS_Triangulated_HeaderFile
10 #include <NIS_InteractiveObject.hxx>
11 #include <Quantity_Color.hxx>
13 class Handle_NIS_TriangulatedDrawer;
14 class NCollection_BaseAllocator;
15 class Handle_NCollection_BaseAllocator;
16 class NIS_TriangulatedDrawer;
19 * Interactive object that consists of triangles, lines and polygons without
20 * normals. Particularly can be used to render planar 2D shapes.
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.
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.
41 class NIS_Triangulated : public NIS_InteractiveObject
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.
51 Type_Loop = 1, //!< modifier to Line
54 Type_Triangulation = 8,
60 * Enumerated type of polygon rendering.
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
69 // ---------- PUBLIC METHODS ----------
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
78 * Total number of nodes that will be initialized for this object
80 * If true then the nodes will be 2D in plane Z=0, otherwise normal 3D.
82 * Allocator for internal data
84 Standard_EXPORT NIS_Triangulated(const Standard_Integer nNodes = 0,
85 const Standard_Boolean is2D = Standard_False,
86 const Handle_NCollection_BaseAllocator&
90 * Define the polygonal presentration.
92 * Number of separate polygons. If set to 0, polygons are cancelled
94 * Optional: Number of nodes to allocate. If left as 0, the previous nodes
95 * allocation is used, otherwise a new allocation is created.
97 Standard_EXPORT void SetPolygonsPrs
98 (const Standard_Integer nPolygons,
99 const Standard_Integer nNodes = 0);
102 * Define the triangulated presentration.
104 * Number of triangles. If set to 0, triangulation is cancelled
106 * Optional: Number of nodes to allocate. If left as 0, the previous nodes
107 * allocation is used, otherwise a new allocation is created.
109 Standard_EXPORT void SetTriangulationPrs
110 (const Standard_Integer nTriangles,
111 const Standard_Integer nNodes = 0);
114 * Define the line presentration (polygon through points)
116 * Number of nodes defining the line. If set to 0, line is cancelled
118 * True if the polygon is closed, so the segment between the first and
119 * the last points is created automatically.
121 * Optional: Number of nodes to allocate. If left as 0, the previous nodes
122 * allocation is used, otherwise a new allocation is created.
124 Standard_EXPORT void SetLinePrs
125 (const Standard_Integer nPoints,
126 const Standard_Boolean isClosed,
127 const Standard_Integer nNodes = 0);
130 * Define the segments presentration. Each segment is defined by 2 nodes
132 * Number of segments. If set to 0, segments presentation is cancelled
134 * Optional: Number of nodes to allocate. If left as 0, the previous nodes
135 * allocation is used, otherwise a new allocation is created.
137 Standard_EXPORT void SetSegmentPrs
138 (const Standard_Integer nSegments,
139 const Standard_Integer nNodes = 0);
142 * Query if there is Triangulation component in the presentation.
144 inline Standard_Boolean IsTriangulation () const
145 { return (myType & Type_Triangulation) != 0; }
148 * Query if there is Polygons component in the presentation.
150 inline Standard_Boolean IsPolygons () const
151 { return (myType & Type_Polygons) != 0; }
154 * Query if there is Line component in the presentation.
156 inline Standard_Boolean IsLine (Standard_Boolean& isLoop) const
157 { isLoop = (myType & Type_Loop) != 0; return (myType & Type_Line) != 0; }
160 * Query if there is Segments component in the presentation.
162 inline Standard_Boolean IsSegments () const
163 { return (myType & Type_Segments) != 0; }
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.
170 Standard_EXPORT void Clear ();
175 Standard_EXPORT virtual ~NIS_Triangulated ();
178 * Create a default drawer instance.
180 Standard_EXPORT virtual NIS_Drawer *
181 DefaultDrawer (NIS_Drawer *) const;
184 * Define the coordinates of node [ind].
186 Standard_EXPORT void SetNode (const Standard_Integer ind,
187 const gp_XYZ& thePnt);
190 * Define the coordinates of node [ind]. Z coordinate is assigned to 0.
192 Standard_EXPORT void SetNode (const Standard_Integer ind,
193 const gp_XY& thePnt);
196 * Define the triangle [ind] by indices of its three nodes.
198 Standard_EXPORT void SetTriangle(const Standard_Integer ind,
199 const Standard_Integer iNode0,
200 const Standard_Integer iNode1,
201 const Standard_Integer iNode2);
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().
208 * Index of the polygon, should be [0..Npolygons-1]
210 * Number of points (segments) in the polygon.
212 Standard_EXPORT void SetPolygon (const Standard_Integer ind,
213 const Standard_Integer theSz);
216 * Define the line node by index.
218 Standard_EXPORT void SetLineNode(const Standard_Integer ind,
219 const Standard_Integer iNode);
222 * Query the number of nodes.
224 inline Standard_Integer NNodes () const
228 * Query the number of triangles.
230 inline Standard_Integer NTriangles() const
231 { return myNTriangles; }
234 * Query the number of line points.
236 inline Standard_Integer NLineNodes() const
237 { return myNLineNodes; }
240 * Query the number of polygons.
242 inline Standard_Integer NPolygons () const
243 { return static_cast<Standard_Integer>(myNPolygons); }
246 * Query the node by its index.
248 * pointer to array of 2 or 3 Standard_ShortReal values {X,Y(,Z) coord}
250 inline const Standard_ShortReal * Node (const Standard_Integer ind) const
251 { return &mypNodes[ind * myNodeCoord]; }
254 * Define the node of a polygon by index.
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().
259 * Index of the node in the Polygon. Should be less than the parameter theSz
260 * in the corresponding previous SetPolygon() call.
262 * Index of the node in the given position of the Polygon.
264 Standard_EXPORT void SetPolygonNode
265 (const Standard_Integer indPoly,
266 const Standard_Integer ind,
267 const Standard_Integer iNode);
270 * Get the node with index 'ind' from the polygon number 'indPoly'.
272 Standard_EXPORT Standard_Integer PolygonNode(const Standard_Integer indPoly,
273 const Standard_Integer ind)const;
276 * Get the number of nodes for the polygon number 'indPoly'.
278 Standard_EXPORT Standard_Integer NPolygonNodes
279 (const Standard_Integer indPoly)const;
282 * Set the boolean flag defining if the polygons or the triangulation
283 * should be drawn. This method does not affect the presentation of
285 * @param isDrawPolygons
286 * True defines that no triangulation is drawn, only polygons are. False
287 * defines that only triangulation is drawn, no polygons.
289 Standard_EXPORT void SetDrawPolygons
290 (const Standard_Boolean isDrawPolygons);
292 * Set the type of polygon rendering.
294 Standard_EXPORT void SetPolygonType
295 (const PolygonType theType);
298 * Set the normal color for presentation.
300 * New color to use for the presentation.
302 Standard_EXPORT void SetColor (const Quantity_Color& theColor);
305 * Get Normal, Transparent or Hilighted color of the presentation.
307 * The draw type, for which the color is retrieved.
309 Standard_EXPORT Quantity_Color GetColor
310 (const NIS_Drawer::DrawType theDrawType) const;
313 * Set the color for hilighted presentation.
315 * New color to use for the presentation.
317 Standard_EXPORT void SetHilightColor (const Quantity_Color& theColor);
320 * Set the color for dynamic hilight presentation.
322 * New color to use for the presentation.
324 Standard_EXPORT void SetDynHilightColor(const Quantity_Color& theColor);
327 * Set the width of line presentations in pixels.
329 * New line width to use for the presentation.
331 Standard_EXPORT void SetLineWidth (const Standard_Real theWidth);
334 * Create a copy of theObject except its ID.
336 * Allocator where the Dest should store its private data.
338 * <tt>[in-out]</tt> The target object where the data are copied. If
339 * passed NULL then the target should be created.
341 Standard_EXPORT virtual void
342 Clone (const Handle_NCollection_BaseAllocator& theAll,
343 Handle_NIS_InteractiveObject& theDest) const;
346 * Intersect the InteractiveObject geometry with a line/ray.
348 * The line or ray in 3D space.
350 * Half-thickness of the selecting line.
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
356 Standard_EXPORT virtual Standard_Real
357 Intersect (const gp_Ax1& theAxis,
358 const Standard_Real theOver) const;
361 * Intersect the InteractiveObject geometry with an oriented box.
363 * 3D box of selection
365 * Position/Orientation of the box.
367 * True if full inclusion is required, False - if partial.
369 * True if the InteractiveObject geometry intersects the box or is inside it
371 Standard_EXPORT virtual Standard_Boolean
372 Intersect (const Bnd_B3f& theBox,
373 const gp_Trsf& theTrf,
374 const Standard_Boolean isFull)const;
377 * Intersect the InteractiveObject geometry with a selection polygon.
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.
383 * Position/Orientation of the polygon.
385 * True if full inclusion is required, False - if partial.
387 * True if the InteractiveObject geometry intersects the polygon or is
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;
395 Standard_EXPORT static int tri_line_intersect (const double start[3],
402 Standard_EXPORT static int tri2d_line_intersect(const double start[3],
409 Standard_EXPORT static int seg_line_intersect (const gp_XYZ& aStart,
416 Standard_EXPORT static int seg2d_line_intersect(const gp_XYZ& aStart,
423 Standard_EXPORT static int seg_box_intersect (const Bnd_B3f& theBox,
424 const gp_Pnt thePnt[2]);
426 Standard_EXPORT static int seg_box_included (const Bnd_B3f& theBox,
427 const gp_Pnt thePnt[2]);
429 Standard_EXPORT static int seg_polygon_intersect
430 (const NCollection_List<gp_XY> &thePolygon,
431 const gp_XY thePnt[2]);
433 Standard_EXPORT static int seg_polygon_included
434 (const NCollection_List<gp_XY> &thePolygon,
435 const gp_XY thePnt[2]);
437 Standard_EXPORT static void ComputeBox (Bnd_B3f& theBox,
438 const Standard_Integer nNodes,
439 const Standard_ShortReal* pNodes,
440 const Standard_Integer nCoord);
443 * Classification of thePoint with respect to 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.
449 * the point to be classified.
451 * Standard_True if thePoint in inside thePolygon or lies on its boundary.
453 Standard_EXPORT static Standard_Boolean
454 IsIn (const NCollection_List<gp_XY> &thePolygon,
455 const gp_XY &thePoint);
458 * Implements deallocation of the object instance
460 Standard_EXPORT virtual void Delete () const;
465 * Allocator-based operator new for dynamic allocations in method Clone()
467 DEFINE_STANDARD_ALLOC
468 DEFINE_NCOLLECTION_ALLOC
471 * Create a 3D bounding box of the object.
473 Standard_EXPORT virtual void computeBox ();
476 * Create the memory buffer for the declared number of nodes, old nodes
479 Standard_EXPORT void allocateNodes (const Standard_Integer nNodes);
482 * Get the node pointed by the i-th index in the array.
484 Standard_EXPORT gp_Pnt nodeAtInd (const Standard_Integer * theArr,
485 const Standard_Integer i) const;
488 * Get the node pointed by the i-th index in the array.
490 Standard_EXPORT float* nodeArrAtInd (const Standard_Integer * theArr,
491 const Standard_Integer i) const;
494 // ---------- PROTECTED FIELDS ----------
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;
513 // Declaration of CASCADE RTTI
514 DEFINE_STANDARD_RTTI (NIS_Triangulated)
516 friend class NIS_TriangulatedDrawer;
519 // Definition of HANDLE object using Standard_DefineHandle.hxx
520 DEFINE_STANDARD_HANDLE (NIS_Triangulated, NIS_InteractiveObject)