1 // Created on: 2000-06-16
2 // Copyright (c) 2000-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 #ifndef _Graphic3d_ArrayOfPrimitives_HeaderFile
16 #define _Graphic3d_ArrayOfPrimitives_HeaderFile
18 #include <Graphic3d_BoundBuffer.hxx>
19 #include <Graphic3d_ArrayFlags.hxx>
20 #include <Graphic3d_Buffer.hxx>
21 #include <Graphic3d_IndexBuffer.hxx>
22 #include <Graphic3d_TypeOfPrimitiveArray.hxx>
25 #include <Standard_OutOfRange.hxx>
26 #include <Standard_TypeMismatch.hxx>
27 #include <Quantity_Color.hxx>
29 class Graphic3d_ArrayOfPrimitives;
30 DEFINE_STANDARD_HANDLE(Graphic3d_ArrayOfPrimitives, Standard_Transient)
32 //! This class furnish services to defined and fill an array of primitives
33 //! which can be passed directly to graphics rendering API.
35 //! The basic interface consists of the following parts:
36 //! 1) Specifying primitive type.
37 //! WARNING! Particular primitive types might be unsupported by specific hardware/graphics API (like quads and polygons).
38 //! It is always preferred using one of basic types having maximum compatibility:
39 //! Point, Triangle (or Triangle strip), Segment aka Lines (or Polyline aka Line Strip).
40 //! Primitive strip types can be used to reduce memory usage as alternative to Indexed arrays.
42 //! - Specifying the (maximum) number of vertexes within array.
43 //! - Specifying the vertex attributes, complementary to mandatory vertex Position (normal, color, UV texture coordinates).
44 //! - Defining vertex values by using various versions of AddVertex() or SetVertex*() methods.
45 //! 3) Index array (optional).
46 //! - Specifying the (maximum) number of indexes (edges).
47 //! - Defining index values by using AddEdge() method; the index value should be within number of defined Vertexes.
49 //! Indexed array allows sharing vertex data across Primitives and thus reducing memory usage,
50 //! since index size is much smaller then size of vertex with all its attributes.
51 //! It is a preferred way for defining primitive array and main alternative to Primitive Strips for optimal memory usage,
52 //! although it is also possible (but unusual) defining Indexed Primitive Strip.
53 //! Note that it is NOT possible sharing Vertex Attributes partially (e.g. share Position, but have different Normals);
54 //! in such cases Vertex should be entirely duplicated with all Attributes.
55 //! 4) Bounds array (optional).
56 //! - Specifying the (maximum) number of bounds.
57 //! - Defining bounds using AddBound() methods.
59 //! Bounds allow splitting Primitive Array into sub-groups.
60 //! This is useful only in two cases - for specifying per-group color and for restarting Primitive Strips.
61 //! WARNING! Bounds within Primitive Array break rendering batches into parts (additional for loops),
62 //! affecting rendering performance negatively (increasing CPU load).
63 class Graphic3d_ArrayOfPrimitives : public Standard_Transient
65 DEFINE_STANDARD_RTTIEXT(Graphic3d_ArrayOfPrimitives, Standard_Transient)
68 //! Create an array of specified type.
69 static Handle(Graphic3d_ArrayOfPrimitives) CreateArray (Graphic3d_TypeOfPrimitiveArray theType,
70 Standard_Integer theMaxVertexs,
71 Standard_Integer theMaxEdges,
72 Graphic3d_ArrayFlags theArrayFlags)
74 return CreateArray (theType, theMaxVertexs, 0, theMaxEdges, theArrayFlags);
77 //! Create an array of specified type.
78 static Standard_EXPORT Handle(Graphic3d_ArrayOfPrimitives) CreateArray (Graphic3d_TypeOfPrimitiveArray theType,
79 Standard_Integer theMaxVertexs,
80 Standard_Integer theMaxBounds,
81 Standard_Integer theMaxEdges,
82 Graphic3d_ArrayFlags theArrayFlags);
86 Standard_EXPORT virtual ~Graphic3d_ArrayOfPrimitives();
88 //! Returns vertex attributes buffer (colors, normals, texture coordinates).
89 const Handle(Graphic3d_Buffer)& Attributes() const { return myAttribs; }
91 //! Returns the type of this primitive
92 Graphic3d_TypeOfPrimitiveArray Type() const { return myType; }
94 //! Returns the string type of this primitive
95 Standard_EXPORT Standard_CString StringType() const;
97 //! Returns TRUE when vertex normals array is defined.
98 Standard_Boolean HasVertexNormals() const { return myNormData != NULL; }
100 //! Returns TRUE when vertex colors array is defined.
101 Standard_Boolean HasVertexColors() const { return myColData != NULL; }
103 //! Returns TRUE when vertex texels array is defined.
104 Standard_Boolean HasVertexTexels() const { return myTexData != NULL; }
106 //! Returns the number of defined vertex
107 Standard_Integer VertexNumber() const { return myAttribs->NbElements; }
109 //! Returns the number of allocated vertex
110 Standard_Integer VertexNumberAllocated() const { return myAttribs->NbMaxElements(); }
112 //! Returns the number of total items according to the array type.
113 Standard_EXPORT Standard_Integer ItemNumber() const;
115 //! Returns TRUE only when the contains of this array is available.
116 Standard_EXPORT Standard_Boolean IsValid();
118 //! Adds a vertice in the array.
119 //! @return the actual vertex number
120 Standard_Integer AddVertex (const gp_Pnt& theVertex) { return AddVertex (theVertex.X(), theVertex.Y(), theVertex.Z()); }
122 //! Adds a vertice in the array.
123 //! @return the actual vertex number
124 Standard_Integer AddVertex (const Graphic3d_Vec3& theVertex) { return AddVertex (theVertex.x(), theVertex.y(), theVertex.z()); }
126 //! Adds a vertice in the array.
127 //! @return the actual vertex number
128 Standard_Integer AddVertex (const Standard_Real theX, const Standard_Real theY, const Standard_Real theZ)
130 return AddVertex (RealToShortReal (theX), RealToShortReal (theY), RealToShortReal (theZ));
133 //! Adds a vertice in the array.
134 //! @return the actual vertex number.
135 Standard_Integer AddVertex (const Standard_ShortReal theX, const Standard_ShortReal theY, const Standard_ShortReal theZ)
137 const Standard_Integer anIndex = myAttribs->NbElements + 1;
138 SetVertice (anIndex, theX, theY, theZ);
142 //! Adds a vertice and vertex color in the vertex array.
143 //! Warning: theColor is ignored when the hasVColors constructor parameter is FALSE
144 //! @return the actual vertex number
145 Standard_Integer AddVertex (const gp_Pnt& theVertex, const Quantity_Color& theColor)
147 const Standard_Integer anIndex = AddVertex (theVertex);
148 SetVertexColor (anIndex, theColor.Red(), theColor.Green(), theColor.Blue());
152 //! Adds a vertice and vertex color in the vertex array.
153 //! Warning: theColor is ignored when the hasVColors constructor parameter is FALSE
155 //! theColor32 = Alpha << 24 + Blue << 16 + Green << 8 + Red
157 //! @return the actual vertex number
158 Standard_Integer AddVertex (const gp_Pnt& theVertex, const Standard_Integer theColor32)
160 const Standard_Integer anIndex = AddVertex (theVertex);
161 SetVertexColor (anIndex, theColor32);
165 //! Adds a vertice and vertex color in the vertex array.
166 //! Warning: theColor is ignored when the hasVColors constructor parameter is FALSE
167 //! @return the actual vertex number
168 Standard_Integer AddVertex (const gp_Pnt& theVertex,
169 const Graphic3d_Vec4ub& theColor)
171 const Standard_Integer anIndex = AddVertex (theVertex);
172 SetVertexColor (anIndex, theColor);
176 //! Adds a vertice and vertex normal in the vertex array.
177 //! Warning: theNormal is ignored when the hasVNormals constructor parameter is FALSE.
178 //! @return the actual vertex number
179 Standard_Integer AddVertex (const gp_Pnt& theVertex, const gp_Dir& theNormal)
181 return AddVertex (theVertex.X(), theVertex.Y(), theVertex.Z(),
182 theNormal.X(), theNormal.Y(), theNormal.Z());
185 //! Adds a vertice and vertex normal in the vertex array.
186 //! Warning: Normal is ignored when the hasVNormals constructor parameter is FALSE.
187 //! @return the actual vertex number
188 Standard_Integer AddVertex (const Standard_Real theX, const Standard_Real theY, const Standard_Real theZ,
189 const Standard_Real theNX, const Standard_Real theNY, const Standard_Real theNZ)
191 return AddVertex (RealToShortReal (theX), RealToShortReal (theY), RealToShortReal (theZ),
192 Standard_ShortReal (theNX), Standard_ShortReal (theNY), Standard_ShortReal (theNZ));
195 //! Adds a vertice and vertex normal in the vertex array.
196 //! Warning: Normal is ignored when the hasVNormals constructor parameter is FALSE.
197 //! @return the actual vertex number
198 Standard_Integer AddVertex (const Standard_ShortReal theX, const Standard_ShortReal theY, const Standard_ShortReal theZ,
199 const Standard_ShortReal theNX, const Standard_ShortReal theNY, const Standard_ShortReal theNZ)
201 const Standard_Integer anIndex = myAttribs->NbElements + 1;
202 SetVertice (anIndex, theX, theY, theZ);
203 SetVertexNormal (anIndex, theNX, theNY, theNZ);
207 //! Adds a vertice,vertex normal and color in the vertex array.
208 //! Warning: theNormal is ignored when the hasVNormals constructor parameter is FALSE
209 //! and theColor is ignored when the hasVColors constructor parameter is FALSE.
210 //! @return the actual vertex number
211 Standard_Integer AddVertex (const gp_Pnt& theVertex, const gp_Dir& theNormal, const Quantity_Color& theColor)
213 const Standard_Integer anIndex = AddVertex (theVertex, theNormal);
214 SetVertexColor (anIndex, theColor.Red(), theColor.Green(), theColor.Blue());
218 //! Adds a vertice,vertex normal and color in the vertex array.
219 //! Warning: theNormal is ignored when the hasVNormals constructor parameter is FALSE
220 //! and theColor is ignored when the hasVColors constructor parameter is FALSE.
222 //! theColor32 = Alpha << 24 + Blue << 16 + Green << 8 + Red
224 //! @return the actual vertex number
225 Standard_Integer AddVertex (const gp_Pnt& theVertex, const gp_Dir& theNormal, const Standard_Integer theColor32)
227 const Standard_Integer anIndex = AddVertex (theVertex, theNormal);
228 SetVertexColor (anIndex, theColor32);
232 //! Adds a vertice and vertex texture in the vertex array.
233 //! theTexel is ignored when the hasVTexels constructor parameter is FALSE.
234 //! @return the actual vertex number
235 Standard_Integer AddVertex (const gp_Pnt& theVertex, const gp_Pnt2d& theTexel)
237 return AddVertex (theVertex.X(), theVertex.Y(), theVertex.Z(),
238 theTexel.X(), theTexel.Y());
241 //! Adds a vertice and vertex texture coordinates in the vertex array.
242 //! Texel is ignored when the hasVTexels constructor parameter is FALSE.
243 //! @return the actual vertex number
244 Standard_Integer AddVertex (const Standard_Real theX, const Standard_Real theY, const Standard_Real theZ,
245 const Standard_Real theTX, const Standard_Real theTY)
247 return AddVertex (RealToShortReal (theX), RealToShortReal (theY), RealToShortReal (theZ),
248 Standard_ShortReal (theTX), Standard_ShortReal (theTY));
251 //! Adds a vertice and vertex texture coordinates in the vertex array.
252 //! Texel is ignored when the hasVTexels constructor parameter is FALSE.
253 //! @return the actual vertex number
254 Standard_Integer AddVertex (const Standard_ShortReal theX, const Standard_ShortReal theY, const Standard_ShortReal theZ,
255 const Standard_ShortReal theTX, const Standard_ShortReal theTY)
257 const Standard_Integer anIndex = myAttribs->NbElements + 1;
258 SetVertice (anIndex, theX, theY, theZ);
259 SetVertexTexel (anIndex, theTX, theTY);
263 //! Adds a vertice,vertex normal and texture in the vertex array.
264 //! Warning: theNormal is ignored when the hasVNormals constructor parameter is FALSE
265 //! and theTexel is ignored when the hasVTexels constructor parameter is FALSE.
266 //! @return the actual vertex number
267 Standard_Integer AddVertex (const gp_Pnt& theVertex, const gp_Dir& theNormal, const gp_Pnt2d& theTexel)
269 return AddVertex (theVertex.X(), theVertex.Y(), theVertex.Z(),
270 theNormal.X(), theNormal.Y(), theNormal.Z(),
271 theTexel.X(), theTexel.Y());
274 //! Adds a vertice,vertex normal and texture in the vertex array.
275 //! Warning: Normal is ignored when the hasVNormals constructor parameter is FALSE
276 //! and Texel is ignored when the hasVTexels constructor parameter is FALSE.
277 //! @return the actual vertex number
278 Standard_Integer AddVertex (const Standard_Real theX, const Standard_Real theY, const Standard_Real theZ,
279 const Standard_Real theNX, const Standard_Real theNY, const Standard_Real theNZ,
280 const Standard_Real theTX, const Standard_Real theTY)
282 return AddVertex (RealToShortReal (theX), RealToShortReal (theY), RealToShortReal (theZ),
283 Standard_ShortReal (theNX), Standard_ShortReal (theNY), Standard_ShortReal (theNZ),
284 Standard_ShortReal (theTX), Standard_ShortReal (theTY));
287 //! Adds a vertice,vertex normal and texture in the vertex array.
288 //! Warning: Normal is ignored when the hasVNormals constructor parameter is FALSE
289 //! and Texel is ignored when the hasVTexels constructor parameter is FALSE.
290 //! @return the actual vertex number
291 Standard_Integer AddVertex (const Standard_ShortReal theX, const Standard_ShortReal theY, const Standard_ShortReal theZ,
292 const Standard_ShortReal theNX, const Standard_ShortReal theNY, const Standard_ShortReal theNZ,
293 const Standard_ShortReal theTX, const Standard_ShortReal theTY)
295 const Standard_Integer anIndex = myAttribs->NbElements + 1;
296 SetVertice (anIndex, theX, theY, theZ);
297 SetVertexNormal(anIndex, theNX, theNY, theNZ);
298 SetVertexTexel (anIndex, theTX, theTY);
302 //! Change the vertice of rank theIndex in the array.
303 void SetVertice (const Standard_Integer theIndex, const gp_Pnt& theVertex)
305 SetVertice (theIndex, Standard_ShortReal (theVertex.X()), Standard_ShortReal (theVertex.Y()), Standard_ShortReal (theVertex.Z()));
308 //! Change the vertice of rank theIndex in the array.
309 void SetVertice (const Standard_Integer theIndex, const Standard_ShortReal theX, const Standard_ShortReal theY, const Standard_ShortReal theZ)
311 Standard_OutOfRange_Raise_if (theIndex < 1 || theIndex > myAttribs->NbMaxElements(), "BAD VERTEX index");
312 Graphic3d_Vec3& aVec = *reinterpret_cast<Graphic3d_Vec3*> (myAttribs->ChangeData() + myPosStride * (theIndex - 1));
316 if (myAttribs->NbElements < theIndex)
318 myAttribs->NbElements = theIndex;
322 //! Change the vertex color of rank theIndex in the array.
323 void SetVertexColor (const Standard_Integer theIndex, const Quantity_Color& theColor)
325 SetVertexColor (theIndex, theColor.Red(), theColor.Green(), theColor.Blue());
328 //! Change the vertex color of rank theIndex in the array.
329 void SetVertexColor (const Standard_Integer theIndex, const Standard_Real theR, const Standard_Real theG, const Standard_Real theB)
331 Standard_OutOfRange_Raise_if (theIndex < 1 || theIndex > myAttribs->NbMaxElements(), "BAD VERTEX index");
332 if (myColData != NULL)
334 Graphic3d_Vec4ub* aColorPtr = reinterpret_cast<Graphic3d_Vec4ub* >(myColData + myColStride * (theIndex - 1));
335 aColorPtr->SetValues (Standard_Byte(theR * 255.0),
336 Standard_Byte(theG * 255.0),
337 Standard_Byte(theB * 255.0), 255);
339 myAttribs->NbElements = Max (theIndex, myAttribs->NbElements);
342 //! Change the vertex color of rank theIndex in the array.
343 void SetVertexColor (const Standard_Integer theIndex,
344 const Graphic3d_Vec4ub& theColor)
346 Standard_OutOfRange_Raise_if (theIndex < 1 || theIndex > myAttribs->NbMaxElements(), "BAD VERTEX index");
347 if (myColData != NULL)
349 Graphic3d_Vec4ub* aColorPtr = reinterpret_cast<Graphic3d_Vec4ub* >(myColData + myColStride * (theIndex - 1));
350 (*aColorPtr) = theColor;
352 myAttribs->NbElements = Max (theIndex, myAttribs->NbElements);
355 //! Change the vertex color of rank theIndex> in the array.
357 //! theColor32 = Alpha << 24 + Blue << 16 + Green << 8 + Red
359 void SetVertexColor (const Standard_Integer theIndex, const Standard_Integer theColor32)
361 Standard_OutOfRange_Raise_if (theIndex < 1 || theIndex > myAttribs->NbMaxElements(), "BAD VERTEX index");
362 if (myColData != NULL)
364 *reinterpret_cast<Standard_Integer* >(myColData + myColStride * (theIndex - 1)) = theColor32;
368 //! Change the vertex normal of rank theIndex in the array.
369 void SetVertexNormal (const Standard_Integer theIndex, const gp_Dir& theNormal)
371 SetVertexNormal (theIndex, theNormal.X(), theNormal.Y(), theNormal.Z());
374 //! Change the vertex normal of rank theIndex in the array.
375 void SetVertexNormal (const Standard_Integer theIndex, const Standard_Real theNX, const Standard_Real theNY, const Standard_Real theNZ)
377 Standard_OutOfRange_Raise_if (theIndex < 1 || theIndex > myAttribs->NbMaxElements(), "BAD VERTEX index");
378 if (myNormData != NULL)
380 Graphic3d_Vec3& aVec = *reinterpret_cast<Graphic3d_Vec3* >(myNormData + myNormStride * (theIndex - 1));
381 aVec.x() = Standard_ShortReal (theNX);
382 aVec.y() = Standard_ShortReal (theNY);
383 aVec.z() = Standard_ShortReal (theNZ);
385 myAttribs->NbElements = Max (theIndex, myAttribs->NbElements);
388 //! Change the vertex texel of rank theIndex in the array.
389 void SetVertexTexel (const Standard_Integer theIndex, const gp_Pnt2d& theTexel)
391 SetVertexTexel (theIndex, theTexel.X(), theTexel.Y());
394 //! Change the vertex texel of rank theIndex in the array.
395 void SetVertexTexel (const Standard_Integer theIndex, const Standard_Real theTX, const Standard_Real theTY)
397 Standard_OutOfRange_Raise_if (theIndex < 1 || theIndex > myAttribs->NbMaxElements(), "BAD VERTEX index");
398 if (myTexData != NULL)
400 Graphic3d_Vec2& aVec = *reinterpret_cast<Graphic3d_Vec2* >(myTexData + myTexStride * (theIndex - 1));
401 aVec.x() = Standard_ShortReal (theTX);
402 aVec.y() = Standard_ShortReal (theTY);
404 myAttribs->NbElements = Max (theIndex, myAttribs->NbElements);
407 //! Returns the vertice at rank theRank from the vertex table if defined.
408 gp_Pnt Vertice (const Standard_Integer theRank) const
410 Standard_Real anXYZ[3];
411 Vertice (theRank, anXYZ[0], anXYZ[1], anXYZ[2]);
412 return gp_Pnt (anXYZ[0], anXYZ[1], anXYZ[2]);
415 //! Returns the vertice coordinates at rank theRank from the vertex table if defined.
416 void Vertice (const Standard_Integer theRank, Standard_Real& theX, Standard_Real& theY, Standard_Real& theZ) const
418 theX = theY = theZ = 0.0;
419 Standard_OutOfRange_Raise_if (theRank < 1 || theRank > myAttribs->NbElements, "BAD VERTEX index");
420 const Graphic3d_Vec3& aVec = *reinterpret_cast<const Graphic3d_Vec3*> (myAttribs->Data() + myPosStride * (theRank - 1));
421 theX = Standard_Real(aVec.x());
422 theY = Standard_Real(aVec.y());
423 theZ = Standard_Real(aVec.z());
426 //! Returns the vertex color at rank theRank from the vertex table if defined.
427 Quantity_Color VertexColor (const Standard_Integer theRank) const
429 Standard_Real anRGB[3];
430 VertexColor (theRank, anRGB[0], anRGB[1], anRGB[2]);
431 return Quantity_Color (anRGB[0], anRGB[1], anRGB[2], Quantity_TOC_RGB);
434 //! Returns the vertex color at rank theIndex from the vertex table if defined.
435 void VertexColor (const Standard_Integer theIndex,
436 Graphic3d_Vec4ub& theColor) const
438 Standard_OutOfRange_Raise_if (myColData == NULL || theIndex < 1 || theIndex > myAttribs->NbElements, "BAD VERTEX index");
439 theColor = *reinterpret_cast<const Graphic3d_Vec4ub* >(myColData + myColStride * (theIndex - 1));
442 //! Returns the vertex color values at rank theRank from the vertex table if defined.
443 void VertexColor (const Standard_Integer theRank, Standard_Real& theR, Standard_Real& theG, Standard_Real& theB) const
445 theR = theG = theB = 0.0;
446 Standard_OutOfRange_Raise_if (theRank < 1 || theRank > myAttribs->NbElements, "BAD VERTEX index");
447 if (myColData == NULL)
451 const Graphic3d_Vec4ub& aColor = *reinterpret_cast<const Graphic3d_Vec4ub* >(myColData + myColStride * (theRank - 1));
452 theR = Standard_Real(aColor.r()) / 255.0;
453 theG = Standard_Real(aColor.g()) / 255.0;
454 theB = Standard_Real(aColor.b()) / 255.0;
457 //! Returns the vertex color values at rank theRank from the vertex table if defined.
458 void VertexColor (const Standard_Integer theRank, Standard_Integer& theColor) const
460 Standard_OutOfRange_Raise_if (theRank < 1 || theRank > myAttribs->NbElements, "BAD VERTEX index");
461 if (myColData != NULL)
463 theColor = *reinterpret_cast<const Standard_Integer* >(myColData + myColStride * (theRank - 1));
467 //! Returns the vertex normal at rank theRank from the vertex table if defined.
468 gp_Dir VertexNormal (const Standard_Integer theRank) const
470 Standard_Real anXYZ[3];
471 VertexNormal (theRank, anXYZ[0], anXYZ[1], anXYZ[2]);
472 return gp_Dir (anXYZ[0], anXYZ[1], anXYZ[2]);
475 //! Returns the vertex normal coordinates at rank theRank from the vertex table if defined.
476 void VertexNormal (const Standard_Integer theRank, Standard_Real& theNX, Standard_Real& theNY, Standard_Real& theNZ) const
478 theNX = theNY = theNZ = 0.0;
479 Standard_OutOfRange_Raise_if (theRank < 1 || theRank > myAttribs->NbElements, "BAD VERTEX index");
480 if (myNormData != NULL)
482 const Graphic3d_Vec3& aVec = *reinterpret_cast<const Graphic3d_Vec3* >(myNormData + myNormStride * (theRank - 1));
483 theNX = Standard_Real(aVec.x());
484 theNY = Standard_Real(aVec.y());
485 theNZ = Standard_Real(aVec.z());
489 //! Returns the vertex texture at rank theRank from the vertex table if defined.
490 gp_Pnt2d VertexTexel (const Standard_Integer theRank) const
492 Standard_Real anXY[2];
493 VertexTexel (theRank, anXY[0], anXY[1]);
494 return gp_Pnt2d (anXY[0], anXY[1]);
497 //! Returns the vertex texture coordinates at rank theRank from the vertex table if defined.
498 void VertexTexel (const Standard_Integer theRank, Standard_Real& theTX, Standard_Real& theTY) const
501 Standard_OutOfRange_Raise_if (theRank < 1 || theRank > myAttribs->NbElements, "BAD VERTEX index");
502 if (myTexData != NULL)
504 const Graphic3d_Vec2& aVec = *reinterpret_cast<const Graphic3d_Vec2* >(myTexData + myTexStride * (theRank - 1));
505 theTX = Standard_Real(aVec.x());
506 theTY = Standard_Real(aVec.y());
510 public: //! @name optional array of Indices/Edges for using shared Vertex data
512 //! Returns optional index buffer.
513 const Handle(Graphic3d_IndexBuffer)& Indices() const { return myIndices; }
515 //! Returns the number of defined edges
516 Standard_Integer EdgeNumber() const { return !myIndices.IsNull() ? myIndices->NbElements : -1; }
518 //! Returns the number of allocated edges
519 Standard_Integer EdgeNumberAllocated() const { return !myIndices.IsNull() ? myIndices->NbMaxElements() : 0; }
521 //! Returns the vertex index at rank theRank in the range [1,EdgeNumber()]
522 Standard_Integer Edge (const Standard_Integer theRank) const
524 Standard_OutOfRange_Raise_if (myIndices.IsNull() || theRank < 1 || theRank > myIndices->NbElements, "BAD EDGE index");
525 return Standard_Integer(myIndices->Index (theRank - 1) + 1);
528 //! Adds an edge in the range [1,VertexNumber()] in the array.
529 //! @return the actual edges number
530 Standard_EXPORT Standard_Integer AddEdge (const Standard_Integer theVertexIndex);
532 //! Convenience method, adds two vertex indices (a segment) in the range [1,VertexNumber()] in the array.
533 //! @return the actual edges number
534 Standard_Integer AddEdges (Standard_Integer theVertexIndex1,
535 Standard_Integer theVertexIndex2)
537 AddEdge (theVertexIndex1);
538 return AddEdge (theVertexIndex2);
541 //! Convenience method, adds two vertex indices (a segment) in the range [1,VertexNumber()] in the array of segments (Graphic3d_TOPA_SEGMENTS).
542 //! Raises exception if array is not of type Graphic3d_TOPA_SEGMENTS.
543 //! @return the actual edges number
544 Standard_Integer AddSegmentEdges (Standard_Integer theVertexIndex1,
545 Standard_Integer theVertexIndex2)
547 Standard_TypeMismatch_Raise_if (myType != Graphic3d_TOPA_SEGMENTS, "Not array of segments");
548 return AddEdges (theVertexIndex1, theVertexIndex2);
551 //! Convenience method, adds three vertex indices (a triangle) in the range [1,VertexNumber()] in the array.
552 //! @return the actual edges number
553 Standard_Integer AddEdges (Standard_Integer theVertexIndex1,
554 Standard_Integer theVertexIndex2,
555 Standard_Integer theVertexIndex3)
557 AddEdge (theVertexIndex1);
558 AddEdge (theVertexIndex2);
559 return AddEdge (theVertexIndex3);
562 //! Convenience method, adds three vertex indices of triangle in the range [1,VertexNumber()] in the array of triangles.
563 //! Raises exception if array is not of type Graphic3d_TOPA_TRIANGLES.
564 //! @return the actual edges number
565 Standard_Integer AddTriangleEdges (Standard_Integer theVertexIndex1,
566 Standard_Integer theVertexIndex2,
567 Standard_Integer theVertexIndex3)
569 Standard_TypeMismatch_Raise_if (myType != Graphic3d_TOPA_TRIANGLES, "Not array of triangles");
570 return AddEdges (theVertexIndex1, theVertexIndex2, theVertexIndex3);
573 //! Convenience method, adds three vertex indices of triangle in the range [1,VertexNumber()] in the array of triangles.
574 //! Raises exception if array is not of type Graphic3d_TOPA_TRIANGLES.
575 //! @return the actual edges number
576 Standard_Integer AddTriangleEdges (const Graphic3d_Vec3i& theIndexes)
578 Standard_TypeMismatch_Raise_if (myType != Graphic3d_TOPA_TRIANGLES, "Not array of triangles");
579 return AddEdges (theIndexes[0], theIndexes[1], theIndexes[2]);
582 //! Convenience method, adds three vertex indices (4th component is ignored) of triangle in the range [1,VertexNumber()] in the array of triangles.
583 //! Raises exception if array is not of type Graphic3d_TOPA_TRIANGLES.
584 //! @return the actual edges number
585 Standard_Integer AddTriangleEdges (const Graphic3d_Vec4i& theIndexes)
587 Standard_TypeMismatch_Raise_if (myType != Graphic3d_TOPA_TRIANGLES, "Not array of triangles");
588 return AddEdges (theIndexes[0], theIndexes[1], theIndexes[2]);
591 //! Convenience method, adds four vertex indices (a quad) in the range [1,VertexNumber()] in the array.
592 //! @return the actual edges number
593 Standard_Integer AddEdges (Standard_Integer theVertexIndex1,
594 Standard_Integer theVertexIndex2,
595 Standard_Integer theVertexIndex3,
596 Standard_Integer theVertexIndex4)
598 AddEdge (theVertexIndex1);
599 AddEdge (theVertexIndex2);
600 AddEdge (theVertexIndex3);
601 return AddEdge (theVertexIndex4);
604 //! Convenience method, adds four vertex indices (a quad) in the range [1,VertexNumber()] in the array of quads.
605 //! Raises exception if array is not of type Graphic3d_TOPA_QUADRANGLES.
606 //! @return the actual edges number
607 Standard_Integer AddQuadEdges (Standard_Integer theVertexIndex1,
608 Standard_Integer theVertexIndex2,
609 Standard_Integer theVertexIndex3,
610 Standard_Integer theVertexIndex4)
612 Standard_TypeMismatch_Raise_if (myType != Graphic3d_TOPA_QUADRANGLES, "Not array of quads");
613 return AddEdges (theVertexIndex1, theVertexIndex2, theVertexIndex3, theVertexIndex4);
616 //! Convenience method, adds quad indices in the range [1,VertexNumber()] into array or triangles as two triangles.
617 //! Raises exception if array is not of type Graphic3d_TOPA_TRIANGLES.
618 //! @return the actual edges number
619 Standard_Integer AddQuadTriangleEdges (Standard_Integer theVertexIndex1,
620 Standard_Integer theVertexIndex2,
621 Standard_Integer theVertexIndex3,
622 Standard_Integer theVertexIndex4)
624 AddTriangleEdges (theVertexIndex3, theVertexIndex1, theVertexIndex2);
625 return AddTriangleEdges (theVertexIndex1, theVertexIndex3, theVertexIndex4);
628 //! Convenience method, adds quad indices in the range [1,VertexNumber()] into array or triangles as two triangles.
629 //! Raises exception if array is not of type Graphic3d_TOPA_TRIANGLES.
630 //! @return the actual edges number
631 Standard_Integer AddQuadTriangleEdges (const Graphic3d_Vec4i& theIndexes)
633 return AddQuadTriangleEdges (theIndexes[0], theIndexes[1], theIndexes[2], theIndexes[3]);
636 //! Add triangle strip into indexed triangulation array.
637 //! N-2 triangles are added from N input nodes.
638 //! Raises exception if array is not of type Graphic3d_TOPA_TRIANGLES.
639 //! @param theVertexLower [in] index of first node defining triangle strip
640 //! @param theVertexUpper [in] index of last node defining triangle strip
641 Standard_EXPORT void AddTriangleStripEdges (Standard_Integer theVertexLower,
642 Standard_Integer theVertexUpper);
644 //! Add triangle fan into indexed triangulation array.
645 //! N-2 triangles are added from N input nodes (or N-1 with closed flag).
646 //! Raises exception if array is not of type Graphic3d_TOPA_TRIANGLES.
647 //! @param theVertexLower [in] index of first node defining triangle fun (center)
648 //! @param theVertexUpper [in] index of last node defining triangle fun
649 //! @param theToClose [in] close triangle fan (connect first and last points)
650 Standard_EXPORT void AddTriangleFanEdges (Standard_Integer theVertexLower,
651 Standard_Integer theVertexUpper,
652 Standard_Boolean theToClose);
654 //! Add line strip (polyline) into indexed segments array.
655 //! N-1 segments are added from N input nodes (or N with closed flag).
656 //! Raises exception if array is not of type Graphic3d_TOPA_SEGMENTS.
657 //! @param theVertexLower [in] index of first node defining line strip fun (center)
658 //! @param theVertexUpper [in] index of last node defining triangle fun
659 //! @param theToClose [in] close triangle fan (connect first and last points)
660 Standard_EXPORT void AddPolylineEdges (Standard_Integer theVertexLower,
661 Standard_Integer theVertexUpper,
662 Standard_Boolean theToClose);
664 public: //! @name optional array of Bounds/Subgroups within primitive array (e.g. restarting primitives / assigning colors)
666 //! Returns optional bounds buffer.
667 const Handle(Graphic3d_BoundBuffer)& Bounds() const { return myBounds; }
669 //! Returns TRUE when bound colors array is defined.
670 Standard_Boolean HasBoundColors() const { return !myBounds.IsNull() && myBounds->Colors != NULL; }
672 //! Returns the number of defined bounds
673 Standard_Integer BoundNumber() const { return !myBounds.IsNull() ? myBounds->NbBounds : -1; }
675 //! Returns the number of allocated bounds
676 Standard_Integer BoundNumberAllocated() const { return !myBounds.IsNull() ? myBounds->NbMaxBounds : 0; }
678 //! Returns the edge number at rank theRank.
679 Standard_Integer Bound (const Standard_Integer theRank) const
681 Standard_OutOfRange_Raise_if (myBounds.IsNull() || theRank < 1 || theRank > myBounds->NbBounds, "BAD BOUND index");
682 return myBounds->Bounds[theRank - 1];
685 //! Returns the bound color at rank theRank from the bound table if defined.
686 Quantity_Color BoundColor (const Standard_Integer theRank) const
688 Standard_Real anRGB[3] = {0.0, 0.0, 0.0};
689 BoundColor (theRank, anRGB[0], anRGB[1], anRGB[2]);
690 return Quantity_Color (anRGB[0], anRGB[1], anRGB[2], Quantity_TOC_RGB);
693 //! Returns the bound color values at rank theRank from the bound table if defined.
694 void BoundColor (const Standard_Integer theRank, Standard_Real& theR, Standard_Real& theG, Standard_Real& theB) const
696 Standard_OutOfRange_Raise_if (myBounds.IsNull() || myBounds->Colors == NULL || theRank < 1 || theRank > myBounds->NbBounds, "BAD BOUND index");
697 const Graphic3d_Vec4& aVec = myBounds->Colors[theRank - 1];
698 theR = Standard_Real(aVec.r());
699 theG = Standard_Real(aVec.g());
700 theB = Standard_Real(aVec.b());
703 //! Adds a bound of length theEdgeNumber in the bound array
704 //! @return the actual bounds number
705 Standard_EXPORT Standard_Integer AddBound (const Standard_Integer theEdgeNumber);
707 //! Adds a bound of length theEdgeNumber and bound color theBColor in the bound array.
708 //! Warning: theBColor is ignored when the hasBColors constructor parameter is FALSE
709 //! @return the actual bounds number
710 Standard_Integer AddBound (const Standard_Integer theEdgeNumber, const Quantity_Color& theBColor)
712 return AddBound (theEdgeNumber, theBColor.Red(), theBColor.Green(), theBColor.Blue());
715 //! Adds a bound of length theEdgeNumber and bound color coordinates in the bound array.
716 //! Warning: <theR,theG,theB> are ignored when the hasBColors constructor parameter is FALSE
717 //! @return the actual bounds number
718 Standard_EXPORT Standard_Integer AddBound (const Standard_Integer theEdgeNumber, const Standard_Real theR, const Standard_Real theG, const Standard_Real theB);
720 //! Change the bound color of rank theIndex in the array.
721 void SetBoundColor (const Standard_Integer theIndex, const Quantity_Color& theColor)
723 SetBoundColor (theIndex, theColor.Red(), theColor.Green(), theColor.Blue());
726 //! Change the bound color of rank theIndex in the array.
727 void SetBoundColor (const Standard_Integer theIndex, const Standard_Real theR, const Standard_Real theG, const Standard_Real theB)
729 if (myBounds.IsNull())
733 Standard_OutOfRange_Raise_if (myBounds.IsNull() || myBounds->Colors == NULL || theIndex < 1 || theIndex > myBounds->NbMaxBounds, "BAD BOUND index");
734 Graphic3d_Vec4& aVec = myBounds->Colors[theIndex - 1];
735 aVec.r() = Standard_ShortReal (theR);
736 aVec.g() = Standard_ShortReal (theG);
737 aVec.b() = Standard_ShortReal (theB);
739 myBounds->NbBounds = Max (theIndex, myBounds->NbBounds);
742 protected: //! @name protected constructors
744 //! Main constructor.
745 //! @param theType type of primitive
746 //! @param theMaxVertexs length of vertex attributes buffer to be allocated (maximum number of vertexes, @sa ::AddVertex())
747 //! @param theMaxBounds length of bounds buffer to be allocated (maximum number of bounds, @sa ::AddBound())
748 //! @param theMaxEdges length of edges (index) buffer to be allocated (maximum number of indexes @sa ::AddEdge())
749 //! @param theArrayFlags array flags
750 Graphic3d_ArrayOfPrimitives (Graphic3d_TypeOfPrimitiveArray theType,
751 Standard_Integer theMaxVertexs,
752 Standard_Integer theMaxBounds,
753 Standard_Integer theMaxEdges,
754 Graphic3d_ArrayFlags theArrayFlags)
755 : myNormData (NULL), myTexData (NULL), myColData (NULL), myPosStride (0), myNormStride (0), myTexStride (0), myColStride (0),
756 myType (Graphic3d_TOPA_UNDEFINED)
758 init (theType, theMaxVertexs, theMaxBounds, theMaxEdges, theArrayFlags);
761 //! Array constructor.
762 Standard_EXPORT void init (Graphic3d_TypeOfPrimitiveArray theType,
763 Standard_Integer theMaxVertexs,
764 Standard_Integer theMaxBounds,
765 Standard_Integer theMaxEdges,
766 Graphic3d_ArrayFlags theArrayFlags);
768 private: //! @name private fields
770 Handle(Graphic3d_IndexBuffer) myIndices;
771 Handle(Graphic3d_Buffer) myAttribs;
772 Handle(Graphic3d_BoundBuffer) myBounds;
773 Standard_Byte* myNormData;
774 Standard_Byte* myTexData;
775 Standard_Byte* myColData;
776 Standard_Size myPosStride;
777 Standard_Size myNormStride;
778 Standard_Size myTexStride;
779 Standard_Size myColStride;
780 Graphic3d_TypeOfPrimitiveArray myType;
784 #endif // _Graphic3d_ArrayOfPrimitives_HeaderFile