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>
14 // Disable the warning "operator new unmatched by delete"
15 #pragma warning (push)
16 #pragma warning (disable:4291)
19 class Handle_NIS_TriangulatedDrawer;
20 class NCollection_BaseAllocator;
21 class Handle_NCollection_BaseAllocator;
22 class NIS_TriangulatedDrawer;
25 * Interactive object that consists of triangles, lines and polygons without
26 * normals. Particularly can be used to render planar 2D shapes.
28 * @par 2D and 3D model
29 * Vertices are stored in an array of float numbers, 2 or 3 numbers per vertex.
30 * The number of dimensions is defined in the constructor, see the parameter
31 * 'is2D'. When 2D is defined then for all vertices the Z coordinate is 0.
32 * To display planar objects in a plane different from XOY you should subclass
33 * this type together with the correponding Drawer and store the transformation
34 * parameters. In Drawer subclass either in method BeforeDraw() or in method
35 * Draw() you would call glTranslate() or glMultMatrix() so that all vertices
36 * should be located in their proper positions.
38 * @par Compressed storage
39 * For efficient memory utilization, indice (triangles, segments, polygons) are
40 * 8-bit, 16-bit or 32-bit numbers. The width of this numeric representation is
41 * chosen automatically when the total number of nodes is passed in the
42 * constructor or in any Set* method. For example, if this number of nodes is
43 * smaller than 256 then 8-bit representation is selected. The choice is stored
44 * in 'myIndexType' data member.
47 class NIS_Triangulated : public NIS_InteractiveObject
51 * Constants defining the mode (type) of presentation. They allow mixed type,
52 * e.g., Triangulation+Line. Line and Segments are not mixable, their mix is
53 * treated as Line only.
57 Type_Loop = 1, //!< modifier to Line
60 Type_Triangulation = 8,
66 * Enumerated type of polygon rendering.
69 Polygon_Default = 0, //!< Polygon as LINE, Triangulation as FILL
70 Polygon_Line = 1, //!< Both Polygon and Triangulation as LINE
71 Polygon_Fill = 2 //!< Both Polygon and Triangulation as FILL
75 // ---------- PUBLIC METHODS ----------
79 * Constructor. Optionally defines the number of nodes that will be allocated
80 * (this number may be defined later in methods Set*Prs) as well as the
81 * memory allocator where the nodes, lines and triangles will be stored by
84 * Total number of nodes that will be initialized for this object
86 * If true then the nodes will be 2D in plane Z=0, otherwise normal 3D.
88 * Allocator for internal data
90 Standard_EXPORT NIS_Triangulated(const Standard_Integer nNodes = 0,
91 const Standard_Boolean is2D = Standard_False,
92 const Handle_NCollection_BaseAllocator&
96 * Define the polygonal presentration.
98 * Number of separate polygons. If set to 0, polygons are cancelled
100 * Optional: Number of nodes to allocate. If left as 0, the previous nodes
101 * allocation is used, otherwise a new allocation is created.
103 Standard_EXPORT void SetPolygonsPrs
104 (const Standard_Integer nPolygons,
105 const Standard_Integer nNodes = 0);
108 * Define the triangulated presentration.
110 * Number of triangles. If set to 0, triangulation is cancelled
112 * Optional: Number of nodes to allocate. If left as 0, the previous nodes
113 * allocation is used, otherwise a new allocation is created.
115 Standard_EXPORT void SetTriangulationPrs
116 (const Standard_Integer nTriangles,
117 const Standard_Integer nNodes = 0);
120 * Define the line presentration (polygon through points)
122 * Number of nodes defining the line. If set to 0, line is cancelled
124 * True if the polygon is closed, so the segment between the first and
125 * the last points is created automatically.
127 * Optional: Number of nodes to allocate. If left as 0, the previous nodes
128 * allocation is used, otherwise a new allocation is created.
130 Standard_EXPORT void SetLinePrs
131 (const Standard_Integer nPoints,
132 const Standard_Boolean isClosed,
133 const Standard_Integer nNodes = 0);
136 * Define the segments presentration. Each segment is defined by 2 nodes
138 * Number of segments. If set to 0, segments presentation is cancelled
140 * Optional: Number of nodes to allocate. If left as 0, the previous nodes
141 * allocation is used, otherwise a new allocation is created.
143 Standard_EXPORT void SetSegmentPrs
144 (const Standard_Integer nSegments,
145 const Standard_Integer nNodes = 0);
148 * Query if there is Triangulation component in the presentation.
150 inline Standard_Boolean IsTriangulation () const
151 { return (myType & Type_Triangulation) != 0; }
154 * Query if there is Polygons component in the presentation.
156 inline Standard_Boolean IsPolygons () const
157 { return (myType & Type_Polygons) != 0; }
160 * Query if there is Line component in the presentation.
162 inline Standard_Boolean IsLine (Standard_Boolean& isLoop) const
163 { isLoop = (myType & Type_Loop) != 0; return (myType & Type_Line) != 0; }
166 * Query if there is Segments component in the presentation.
168 inline Standard_Boolean IsSegments () const
169 { return (myType & Type_Segments) != 0; }
172 * Reset all data memebers and free all allocated memory.
173 * Called from the destructor, also can be usedto re-initialize a given
174 * Interactive Objects.
176 Standard_EXPORT void Clear ();
181 Standard_EXPORT virtual ~NIS_Triangulated ();
184 * Create a default drawer instance.
186 Standard_EXPORT virtual NIS_Drawer *
187 DefaultDrawer (NIS_Drawer *) const;
190 * Define the coordinates of node [ind].
192 Standard_EXPORT void SetNode (const Standard_Integer ind,
193 const gp_XYZ& thePnt);
196 * Define the coordinates of node [ind]. Z coordinate is assigned to 0.
198 Standard_EXPORT void SetNode (const Standard_Integer ind,
199 const gp_XY& thePnt);
202 * Define the triangle [ind] by indices of its three nodes.
204 Standard_EXPORT void SetTriangle(const Standard_Integer ind,
205 const Standard_Integer iNode0,
206 const Standard_Integer iNode1,
207 const Standard_Integer iNode2);
210 * Allocate a single polygon, should be called for each polygon following
211 * the call SetPolygonsPrs(). The polygon can be filled by node indices using
212 * the method SetPolygonNode().
214 * Index of the polygon, should be [0..Npolygons-1]
216 * Number of points (segments) in the polygon.
218 Standard_EXPORT void SetPolygon (const Standard_Integer ind,
219 const Standard_Integer theSz);
222 * Define the line node by index.
224 Standard_EXPORT void SetLineNode(const Standard_Integer ind,
225 const Standard_Integer iNode);
228 * Query the number of nodes.
230 inline Standard_Integer NNodes () const
234 * Query the number of triangles.
236 inline Standard_Integer NTriangles() const
237 { return myNTriangles; }
240 * Query the number of line points.
242 inline Standard_Integer NLineNodes() const
243 { return myNLineNodes; }
246 * Query the number of polygons.
248 inline Standard_Integer NPolygons () const
249 { return static_cast<Standard_Integer>(myNPolygons); }
252 * Query the node by its index.
254 * pointer to array of 2 or 3 Standard_ShortReal values {X,Y(,Z) coord}
256 inline const Standard_ShortReal * Node (const Standard_Integer ind) const
257 { return &mypNodes[ind * myNodeCoord]; }
260 * Define the node of a polygon by index.
262 * Index of the Polygon, should be less than the number of polygons that is
263 * defined in SetPolygonsPrs() and can be returned by NPOlygons().
265 * Index of the node in the Polygon. Should be less than the parameter theSz
266 * in the corresponding previous SetPolygon() call.
268 * Index of the node in the given position of the Polygon.
270 Standard_EXPORT void SetPolygonNode
271 (const Standard_Integer indPoly,
272 const Standard_Integer ind,
273 const Standard_Integer iNode);
276 * Get the node with index 'ind' from the polygon number 'indPoly'.
278 Standard_EXPORT Standard_Integer PolygonNode(const Standard_Integer indPoly,
279 const Standard_Integer ind)const;
282 * Get the number of nodes for the polygon number 'indPoly'.
284 Standard_EXPORT Standard_Integer NPolygonNodes
285 (const Standard_Integer indPoly)const;
288 * Set the boolean flag defining if the polygons or the triangulation
289 * should be drawn. This method does not affect the presentation of
291 * @param isDrawPolygons
292 * True defines that no triangulation is drawn, only polygons are. False
293 * defines that only triangulation is drawn, no polygons.
295 Standard_EXPORT void SetDrawPolygons
296 (const Standard_Boolean isDrawPolygons);
298 * Set the type of polygon rendering.
300 Standard_EXPORT void SetPolygonType
301 (const PolygonType theType);
304 * Set the normal color for presentation.
306 * New color to use for the presentation.
308 Standard_EXPORT void SetColor (const Quantity_Color& theColor);
311 * Get Normal, Transparent or Hilighted color of the presentation.
313 * The draw type, for which the color is retrieved.
315 Standard_EXPORT Quantity_Color GetColor
316 (const NIS_Drawer::DrawType theDrawType) const;
319 * Set the color for hilighted presentation.
321 * New color to use for the presentation.
323 Standard_EXPORT void SetHilightColor (const Quantity_Color& theColor);
326 * Set the color for dynamic hilight presentation.
328 * New color to use for the presentation.
330 Standard_EXPORT void SetDynHilightColor(const Quantity_Color& theColor);
333 * Set the width of line presentations in pixels.
335 * New line width to use for the presentation.
337 Standard_EXPORT void SetLineWidth (const Standard_Real theWidth);
340 * Create a copy of theObject except its ID.
342 * Allocator where the Dest should store its private data.
344 * <tt>[in-out]</tt> The target object where the data are copied. If
345 * passed NULL then the target should be created.
347 Standard_EXPORT virtual void
348 Clone (const Handle_NCollection_BaseAllocator& theAll,
349 Handle_NIS_InteractiveObject& theDest) const;
352 * Intersect the InteractiveObject geometry with a line/ray.
354 * The line or ray in 3D space.
356 * Half-thickness of the selecting line.
358 * If the return value is more than 0.1*RealLast() then no intersection is
359 * detected. Otherwise returns the coordinate of thePnt on the ray. May be
362 Standard_EXPORT virtual Standard_Real
363 Intersect (const gp_Ax1& theAxis,
364 const Standard_Real theOver) const;
367 * Intersect the InteractiveObject geometry with an oriented box.
369 * 3D box of selection
371 * Position/Orientation of the box.
373 * True if full inclusion is required, False - if partial.
375 * True if the InteractiveObject geometry intersects the box or is inside it
377 Standard_EXPORT virtual Standard_Boolean
378 Intersect (const Bnd_B3f& theBox,
379 const gp_Trsf& theTrf,
380 const Standard_Boolean isFull)const;
383 * Intersect the InteractiveObject geometry with a selection polygon.
385 * the list of vertices of a free-form closed polygon without
386 * self-intersections. The last point should not coincide with the first
387 * point of the list. Any two neighbor points should not be confused.
389 * Position/Orientation of the polygon.
391 * True if full inclusion is required, False - if partial.
393 * True if the InteractiveObject geometry intersects the polygon or is
396 Standard_EXPORT virtual Standard_Boolean Intersect
397 (const NCollection_List<gp_XY> &thePolygon,
398 const gp_Trsf &theTrf,
399 const Standard_Boolean isFullIn) const;
401 Standard_EXPORT static int tri_line_intersect (const double start[3],
408 Standard_EXPORT static int tri2d_line_intersect(const double start[3],
415 Standard_EXPORT static int seg_line_intersect (const gp_XYZ& aStart,
422 Standard_EXPORT static int seg2d_line_intersect(const gp_XYZ& aStart,
429 Standard_EXPORT static int seg_box_intersect (const Bnd_B3f& theBox,
430 const gp_Pnt thePnt[2]);
432 Standard_EXPORT static int seg_box_included (const Bnd_B3f& theBox,
433 const gp_Pnt thePnt[2]);
435 Standard_EXPORT static int seg_polygon_intersect
436 (const NCollection_List<gp_XY> &thePolygon,
437 const gp_XY thePnt[2]);
439 Standard_EXPORT static int seg_polygon_included
440 (const NCollection_List<gp_XY> &thePolygon,
441 const gp_XY thePnt[2]);
443 Standard_EXPORT static void ComputeBox (Bnd_B3f& theBox,
444 const Standard_Integer nNodes,
445 const Standard_ShortReal* pNodes,
446 const Standard_Integer nCoord);
449 * Classification of thePoint with respect to thePolygon.
451 * the list of vertices of a free-form closed polygon without
452 * self-intersections. The last point should not coincide with the first
453 * point of the list. Any two neighbor points should not be confused.
455 * the point to be classified.
457 * Standard_True if thePoint in inside thePolygon or lies on its boundary.
459 Standard_EXPORT static Standard_Boolean
460 IsIn (const NCollection_List<gp_XY> &thePolygon,
461 const gp_XY &thePoint);
464 * Implements deallocation of the object instance
466 Standard_EXPORT virtual void Delete () const;
469 * Operator new for memory allocation uses Open CASCADE memory manager
471 void* operator new (size_t size)
473 return Standard::Allocate(size);
479 * Allocator-based operator new for dynamic allocations in method Clone()
481 void* operator new (Standard_Size theSz,
482 const Handle(NCollection_BaseAllocator)& theAllocator)
484 return theAllocator->Allocate(theSz);
488 * Create a 3D bounding box of the object.
490 Standard_EXPORT virtual void computeBox ();
493 * Create the memory buffer for the declared number of nodes, old nodes
496 Standard_EXPORT void allocateNodes (const Standard_Integer nNodes);
499 * Get the node pointed by the i-th index in the array.
501 Standard_EXPORT gp_Pnt nodeAtInd (const Standard_Integer * theArr,
502 const Standard_Integer i) const;
505 * Get the node pointed by the i-th index in the array.
507 Standard_EXPORT float* nodeArrAtInd (const Standard_Integer * theArr,
508 const Standard_Integer i) const;
511 // ---------- PROTECTED FIELDS ----------
513 NCollection_BaseAllocator * myAlloc; //!< Usually from InteractiveContext
514 Standard_Integer myType; //!< Combination of Type_* constants
515 Standard_ShortReal * mypNodes;
516 Standard_Integer * mypTriangles;
517 Standard_Integer * mypLines;
518 Standard_Integer ** mypPolygons;
519 Standard_Integer myNNodes;
520 Standard_Integer myNTriangles;
521 Standard_Integer myNLineNodes;
522 unsigned int myNPolygons : 24;
523 Standard_Boolean myIsDrawPolygons : 1;
524 Standard_Boolean myIsCloned : 1; //!< How it is allocated
525 unsigned int myIndexType : 2; //!< 0:8bit, 1:16bit, 2:32bit
526 unsigned int myNodeCoord : 2; //!< 2 or 3 coordinates
527 unsigned int myPolygonType : 2;
530 // Declaration of CASCADE RTTI
531 DEFINE_STANDARD_RTTI (NIS_Triangulated)
533 friend class NIS_TriangulatedDrawer;
536 // Definition of HANDLE object using Standard_DefineHandle.hxx
537 DEFINE_STANDARD_HANDLE (NIS_Triangulated, NIS_InteractiveObject)
540 #pragma warning (pop)