1 // Created on: 2007-07-17
2 // Created by: Alexander GRIGORIEV
3 // Copyright (c) 2007-2012 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
22 #ifndef NIS_Triangulated_HeaderFile
23 #define NIS_Triangulated_HeaderFile
25 #include <NIS_InteractiveObject.hxx>
26 #include <Quantity_Color.hxx>
28 class Handle_NIS_TriangulatedDrawer;
29 class NCollection_BaseAllocator;
30 class Handle_NCollection_BaseAllocator;
31 class NIS_TriangulatedDrawer;
34 * Interactive object that consists of triangles, lines and polygons without
35 * normals. Particularly can be used to render planar 2D shapes.
37 * @par 2D and 3D model
38 * Vertices are stored in an array of float numbers, 2 or 3 numbers per vertex.
39 * The number of dimensions is defined in the constructor, see the parameter
40 * 'is2D'. When 2D is defined then for all vertices the Z coordinate is 0.
41 * To display planar objects in a plane different from XOY you should subclass
42 * this type together with the correponding Drawer and store the transformation
43 * parameters. In Drawer subclass either in method BeforeDraw() or in method
44 * Draw() you would call glTranslate() or glMultMatrix() so that all vertices
45 * should be located in their proper positions.
47 * @par Compressed storage
48 * For efficient memory utilization, indice (triangles, segments, polygons) are
49 * 8-bit, 16-bit or 32-bit numbers. The width of this numeric representation is
50 * chosen automatically when the total number of nodes is passed in the
51 * constructor or in any Set* method. For example, if this number of nodes is
52 * smaller than 256 then 8-bit representation is selected. The choice is stored
53 * in 'myIndexType' data member.
56 class NIS_Triangulated : public NIS_InteractiveObject
60 * Constants defining the mode (type) of presentation. They allow mixed type,
61 * e.g., Triangulation+Line. Line and Segments are not mixable, their mix is
62 * treated as Line only.
66 Type_Loop = 1, //!< modifier to Line
69 Type_Triangulation = 8,
75 * Enumerated type of polygon rendering.
78 Polygon_Default = 0, //!< Polygon as LINE, Triangulation as FILL
79 Polygon_Line = 1, //!< Both Polygon and Triangulation as LINE
80 Polygon_Fill = 2 //!< Both Polygon and Triangulation as FILL
84 // ---------- PUBLIC METHODS ----------
88 * Constructor. Optionally defines the number of nodes that will be allocated
89 * (this number may be defined later in methods Set*Prs) as well as the
90 * memory allocator where the nodes, lines and triangles will be stored by
93 * Total number of nodes that will be initialized for this object
95 * If true then the nodes will be 2D in plane Z=0, otherwise normal 3D.
97 * Allocator for internal data
99 Standard_EXPORT NIS_Triangulated(const Standard_Integer nNodes = 0,
100 const Standard_Boolean is2D = Standard_False,
101 const Handle_NCollection_BaseAllocator&
105 * Define the polygonal presentration.
107 * Number of separate polygons. If set to 0, polygons are cancelled
109 * Optional: Number of nodes to allocate. If left as 0, the previous nodes
110 * allocation is used, otherwise a new allocation is created.
112 Standard_EXPORT void SetPolygonsPrs
113 (const Standard_Integer nPolygons,
114 const Standard_Integer nNodes = 0);
117 * Define the triangulated presentration.
119 * Number of triangles. If set to 0, triangulation is cancelled
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 SetTriangulationPrs
125 (const Standard_Integer nTriangles,
126 const Standard_Integer nNodes = 0);
129 * Define the line presentration (polygon through points)
131 * Number of nodes defining the line. If set to 0, line is cancelled
133 * True if the polygon is closed, so the segment between the first and
134 * the last points is created automatically.
136 * Optional: Number of nodes to allocate. If left as 0, the previous nodes
137 * allocation is used, otherwise a new allocation is created.
139 Standard_EXPORT void SetLinePrs
140 (const Standard_Integer nPoints,
141 const Standard_Boolean isClosed,
142 const Standard_Integer nNodes = 0);
145 * Define the segments presentration. Each segment is defined by 2 nodes
147 * Number of segments. If set to 0, segments presentation is cancelled
149 * Optional: Number of nodes to allocate. If left as 0, the previous nodes
150 * allocation is used, otherwise a new allocation is created.
152 Standard_EXPORT void SetSegmentPrs
153 (const Standard_Integer nSegments,
154 const Standard_Integer nNodes = 0);
157 * Query if there is Triangulation component in the presentation.
159 inline Standard_Boolean IsTriangulation () const
160 { return (myType & Type_Triangulation) != 0; }
163 * Query if there is Polygons component in the presentation.
165 inline Standard_Boolean IsPolygons () const
166 { return (myType & Type_Polygons) != 0; }
169 * Query if there is Line component in the presentation.
171 inline Standard_Boolean IsLine (Standard_Boolean& isLoop) const
172 { isLoop = (myType & Type_Loop) != 0; return (myType & Type_Line) != 0; }
175 * Query if there is Segments component in the presentation.
177 inline Standard_Boolean IsSegments () const
178 { return (myType & Type_Segments) != 0; }
181 * Reset all data memebers and free all allocated memory.
182 * Called from the destructor, also can be usedto re-initialize a given
183 * Interactive Objects.
185 Standard_EXPORT void Clear ();
190 Standard_EXPORT virtual ~NIS_Triangulated ();
193 * Create a default drawer instance.
195 Standard_EXPORT virtual NIS_Drawer *
196 DefaultDrawer (NIS_Drawer *) const;
199 * Define the coordinates of node [ind].
201 Standard_EXPORT void SetNode (const Standard_Integer ind,
202 const gp_XYZ& thePnt);
205 * Define the coordinates of node [ind]. Z coordinate is assigned to 0.
207 Standard_EXPORT void SetNode (const Standard_Integer ind,
208 const gp_XY& thePnt);
211 * Define the triangle [ind] by indices of its three nodes.
213 Standard_EXPORT void SetTriangle(const Standard_Integer ind,
214 const Standard_Integer iNode0,
215 const Standard_Integer iNode1,
216 const Standard_Integer iNode2);
219 * Allocate a single polygon, should be called for each polygon following
220 * the call SetPolygonsPrs(). The polygon can be filled by node indices using
221 * the method SetPolygonNode().
223 * Index of the polygon, should be [0..Npolygons-1]
225 * Number of points (segments) in the polygon.
227 Standard_EXPORT void SetPolygon (const Standard_Integer ind,
228 const Standard_Integer theSz);
231 * Define the line node by index.
233 Standard_EXPORT void SetLineNode(const Standard_Integer ind,
234 const Standard_Integer iNode);
237 * Query the number of nodes.
239 inline Standard_Integer NNodes () const
243 * Query the number of triangles.
245 inline Standard_Integer NTriangles() const
246 { return myNTriangles; }
249 * Query the number of line points.
251 inline Standard_Integer NLineNodes() const
252 { return myNLineNodes; }
255 * Query the number of polygons.
257 inline Standard_Integer NPolygons () const
258 { return static_cast<Standard_Integer>(myNPolygons); }
261 * Query the node by its index.
263 * pointer to array of 2 or 3 Standard_ShortReal values {X,Y(,Z) coord}
265 inline const Standard_ShortReal * Node (const Standard_Integer ind) const
266 { return &mypNodes[ind * myNodeCoord]; }
269 * Define the node of a polygon by index.
271 * Index of the Polygon, should be less than the number of polygons that is
272 * defined in SetPolygonsPrs() and can be returned by NPOlygons().
274 * Index of the node in the Polygon. Should be less than the parameter theSz
275 * in the corresponding previous SetPolygon() call.
277 * Index of the node in the given position of the Polygon.
279 Standard_EXPORT void SetPolygonNode
280 (const Standard_Integer indPoly,
281 const Standard_Integer ind,
282 const Standard_Integer iNode);
285 * Get the node with index 'ind' from the polygon number 'indPoly'.
287 Standard_EXPORT Standard_Integer PolygonNode(const Standard_Integer indPoly,
288 const Standard_Integer ind)const;
291 * Get the number of nodes for the polygon number 'indPoly'.
293 Standard_EXPORT Standard_Integer NPolygonNodes
294 (const Standard_Integer indPoly)const;
297 * Set the boolean flag defining if the polygons or the triangulation
298 * should be drawn. This method does not affect the presentation of
300 * @param isDrawPolygons
301 * True defines that no triangulation is drawn, only polygons are. False
302 * defines that only triangulation is drawn, no polygons.
304 Standard_EXPORT void SetDrawPolygons
305 (const Standard_Boolean isDrawPolygons);
307 * Set the type of polygon rendering.
309 Standard_EXPORT void SetPolygonType
310 (const PolygonType theType);
313 * Set the normal color for presentation.
315 * New color to use for the presentation.
317 Standard_EXPORT void SetColor (const Quantity_Color& theColor);
320 * Get Normal, Transparent or Hilighted color of the presentation.
322 * The draw type, for which the color is retrieved.
324 Standard_EXPORT Quantity_Color GetColor
325 (const NIS_Drawer::DrawType theDrawType) const;
328 * Set the color for hilighted presentation.
330 * New color to use for the presentation.
332 Standard_EXPORT void SetHilightColor (const Quantity_Color& theColor);
335 * Set the color for dynamic hilight presentation.
337 * New color to use for the presentation.
339 Standard_EXPORT void SetDynHilightColor(const Quantity_Color& theColor);
342 * Set the width of line presentations in pixels.
344 * New line width to use for the presentation.
346 Standard_EXPORT void SetLineWidth (const Standard_Real theWidth);
349 * Create a copy of theObject except its ID.
351 * Allocator where the Dest should store its private data.
353 * <tt>[in-out]</tt> The target object where the data are copied. If
354 * passed NULL then the target should be created.
356 Standard_EXPORT virtual void
357 Clone (const Handle_NCollection_BaseAllocator& theAll,
358 Handle_NIS_InteractiveObject& theDest) const;
361 * Intersect the InteractiveObject geometry with a line/ray.
363 * The line or ray in 3D space.
365 * Half-thickness of the selecting line.
367 * If the return value is more than 0.1*RealLast() then no intersection is
368 * detected. Otherwise returns the coordinate of thePnt on the ray. May be
371 Standard_EXPORT virtual Standard_Real
372 Intersect (const gp_Ax1& theAxis,
373 const Standard_Real theOver) const;
376 * Intersect the InteractiveObject geometry with an oriented box.
378 * 3D box of selection
380 * Position/Orientation of the box.
382 * True if full inclusion is required, False - if partial.
384 * True if the InteractiveObject geometry intersects the box or is inside it
386 Standard_EXPORT virtual Standard_Boolean
387 Intersect (const Bnd_B3f& theBox,
388 const gp_Trsf& theTrf,
389 const Standard_Boolean isFull)const;
392 * Intersect the InteractiveObject geometry with a selection polygon.
394 * the list of vertices of a free-form closed polygon without
395 * self-intersections. The last point should not coincide with the first
396 * point of the list. Any two neighbor points should not be confused.
398 * Position/Orientation of the polygon.
400 * True if full inclusion is required, False - if partial.
402 * True if the InteractiveObject geometry intersects the polygon or is
405 Standard_EXPORT virtual Standard_Boolean Intersect
406 (const NCollection_List<gp_XY> &thePolygon,
407 const gp_Trsf &theTrf,
408 const Standard_Boolean isFullIn) const;
410 Standard_EXPORT static int tri_line_intersect (const double start[3],
417 Standard_EXPORT static int tri2d_line_intersect(const double start[3],
424 Standard_EXPORT static int seg_line_intersect (const gp_XYZ& aStart,
431 Standard_EXPORT static int seg2d_line_intersect(const gp_XYZ& aStart,
438 Standard_EXPORT static int seg_box_intersect (const Bnd_B3f& theBox,
439 const gp_Pnt thePnt[2]);
441 Standard_EXPORT static int seg_box_included (const Bnd_B3f& theBox,
442 const gp_Pnt thePnt[2]);
444 Standard_EXPORT static int seg_polygon_intersect
445 (const NCollection_List<gp_XY> &thePolygon,
446 const gp_XY thePnt[2]);
448 Standard_EXPORT static int seg_polygon_included
449 (const NCollection_List<gp_XY> &thePolygon,
450 const gp_XY thePnt[2]);
452 Standard_EXPORT static void ComputeBox (Bnd_B3f& theBox,
453 const Standard_Integer nNodes,
454 const Standard_ShortReal* pNodes,
455 const Standard_Integer nCoord);
458 * Classification of thePoint with respect to thePolygon.
460 * the list of vertices of a free-form closed polygon without
461 * self-intersections. The last point should not coincide with the first
462 * point of the list. Any two neighbor points should not be confused.
464 * the point to be classified.
466 * Standard_True if thePoint in inside thePolygon or lies on its boundary.
468 Standard_EXPORT static Standard_Boolean
469 IsIn (const NCollection_List<gp_XY> &thePolygon,
470 const gp_XY &thePoint);
473 * Implements deallocation of the object instance
475 Standard_EXPORT virtual void Delete () const;
480 * Allocator-based operator new for dynamic allocations in method Clone()
482 DEFINE_STANDARD_ALLOC
483 DEFINE_NCOLLECTION_ALLOC
486 * Create a 3D bounding box of the object.
488 Standard_EXPORT virtual void computeBox ();
491 * Create the memory buffer for the declared number of nodes, old nodes
494 Standard_EXPORT void allocateNodes (const Standard_Integer nNodes);
497 * Get the node pointed by the i-th index in the array.
499 Standard_EXPORT gp_Pnt nodeAtInd (const Standard_Integer * theArr,
500 const Standard_Integer i) const;
503 * Get the node pointed by the i-th index in the array.
505 Standard_EXPORT float* nodeArrAtInd (const Standard_Integer * theArr,
506 const Standard_Integer i) const;
509 // ---------- PROTECTED FIELDS ----------
511 NCollection_BaseAllocator * myAlloc; //!< Usually from InteractiveContext
512 Standard_Integer myType; //!< Combination of Type_* constants
513 Standard_ShortReal * mypNodes;
514 Standard_Integer * mypTriangles;
515 Standard_Integer * mypLines;
516 Standard_Integer ** mypPolygons;
517 Standard_Integer myNNodes;
518 Standard_Integer myNTriangles;
519 Standard_Integer myNLineNodes;
520 unsigned int myNPolygons : 24;
521 Standard_Boolean myIsDrawPolygons : 1;
522 Standard_Boolean myIsCloned : 1; //!< How it is allocated
523 unsigned int myIndexType : 2; //!< 0:8bit, 1:16bit, 2:32bit
524 unsigned int myNodeCoord : 2; //!< 2 or 3 coordinates
525 unsigned int myPolygonType : 2;
528 // Declaration of CASCADE RTTI
529 DEFINE_STANDARD_RTTI (NIS_Triangulated)
531 friend class NIS_TriangulatedDrawer;
534 // Definition of HANDLE object using Standard_DefineHandle.hxx
535 DEFINE_STANDARD_HANDLE (NIS_Triangulated, NIS_InteractiveObject)