Warning, file /include/opencascade/Graphic3d_Buffer.hxx was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #ifndef _Graphic3d_Buffer_HeaderFile
0015 #define _Graphic3d_Buffer_HeaderFile
0016
0017 #include <Graphic3d_BufferRange.hxx>
0018 #include <Graphic3d_Vec.hxx>
0019 #include <NCollection_Array1.hxx>
0020 #include <NCollection_Buffer.hxx>
0021 #include <Standard_NotImplemented.hxx>
0022
0023
0024 enum Graphic3d_TypeOfAttribute
0025 {
0026 Graphic3d_TOA_POS = 0,
0027 Graphic3d_TOA_NORM,
0028 Graphic3d_TOA_UV,
0029 Graphic3d_TOA_COLOR,
0030 Graphic3d_TOA_CUSTOM,
0031 };
0032
0033
0034 enum Graphic3d_TypeOfData
0035 {
0036 Graphic3d_TOD_USHORT,
0037 Graphic3d_TOD_UINT,
0038 Graphic3d_TOD_VEC2,
0039 Graphic3d_TOD_VEC3,
0040 Graphic3d_TOD_VEC4,
0041 Graphic3d_TOD_VEC4UB,
0042 Graphic3d_TOD_FLOAT,
0043 };
0044
0045
0046 struct Graphic3d_Attribute
0047 {
0048
0049 Graphic3d_TypeOfAttribute Id;
0050
0051 Graphic3d_TypeOfData DataType;
0052
0053 Standard_Integer Stride() const { return Stride(DataType); }
0054
0055
0056 static Standard_Integer Stride(const Graphic3d_TypeOfData theType)
0057 {
0058 switch (theType)
0059 {
0060 case Graphic3d_TOD_USHORT:
0061 return sizeof(unsigned short);
0062 case Graphic3d_TOD_UINT:
0063 return sizeof(unsigned int);
0064 case Graphic3d_TOD_VEC2:
0065 return sizeof(Graphic3d_Vec2);
0066 case Graphic3d_TOD_VEC3:
0067 return sizeof(Graphic3d_Vec3);
0068 case Graphic3d_TOD_VEC4:
0069 return sizeof(Graphic3d_Vec4);
0070 case Graphic3d_TOD_VEC4UB:
0071 return sizeof(Graphic3d_Vec4ub);
0072 case Graphic3d_TOD_FLOAT:
0073 return sizeof(float);
0074 }
0075 return 0;
0076 }
0077 };
0078
0079 typedef NCollection_Array1<Graphic3d_Attribute> Graphic3d_Array1OfAttribute;
0080
0081
0082 class Graphic3d_Buffer : public NCollection_Buffer
0083 {
0084 DEFINE_STANDARD_RTTIEXT(Graphic3d_Buffer, NCollection_Buffer)
0085 public:
0086
0087 Standard_EXPORT static const Handle(NCollection_BaseAllocator)& DefaultAllocator();
0088
0089 public:
0090
0091 Graphic3d_Buffer(const Handle(NCollection_BaseAllocator)& theAlloc)
0092 : NCollection_Buffer(theAlloc),
0093 Stride(0),
0094 NbElements(0),
0095 NbAttributes(0)
0096 {
0097
0098 }
0099
0100
0101
0102 Standard_Integer NbMaxElements() const
0103 {
0104 return Stride != 0 ? Standard_Integer(mySize / size_t(Stride)) : 0;
0105 }
0106
0107
0108 const Graphic3d_Attribute* AttributesArray() const
0109 {
0110 return (Graphic3d_Attribute*)(myData + mySize);
0111 }
0112
0113
0114 const Graphic3d_Attribute& Attribute(const Standard_Integer theAttribIndex) const
0115 {
0116 return AttributesArray()[theAttribIndex];
0117 }
0118
0119
0120 Graphic3d_Attribute& ChangeAttribute(const Standard_Integer theAttribIndex)
0121 {
0122 return *((Graphic3d_Attribute*)(myData + mySize) + theAttribIndex);
0123 }
0124
0125
0126
0127
0128 Standard_Integer FindAttribute(Graphic3d_TypeOfAttribute theAttrib) const
0129 {
0130 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter)
0131 {
0132 const Graphic3d_Attribute& anAttrib = Attribute(anAttribIter);
0133 if (anAttrib.Id == theAttrib)
0134 {
0135 return anAttribIter;
0136 }
0137 }
0138 return -1;
0139 }
0140
0141
0142 public:
0143
0144 Standard_Integer AttributeOffset(const Standard_Integer theAttribIndex) const
0145 {
0146 Standard_Integer anOffset = 0;
0147 for (Standard_Integer anAttribIter = 0; anAttribIter < theAttribIndex; ++anAttribIter)
0148 {
0149 anOffset += Graphic3d_Attribute::Stride(Attribute(anAttribIter).DataType);
0150 }
0151 return anOffset;
0152 }
0153
0154
0155 const Standard_Byte* Data(const Standard_Integer theAttribIndex) const
0156 {
0157 return myData + AttributeOffset(theAttribIndex);
0158 }
0159
0160
0161 Standard_Byte* ChangeData(const Standard_Integer theAttribIndex)
0162 {
0163 return myData + AttributeOffset(theAttribIndex);
0164 }
0165
0166
0167 inline const Standard_Byte* value(const Standard_Integer theElem) const
0168 {
0169 return myData + Stride * size_t(theElem);
0170 }
0171
0172
0173 inline Standard_Byte* changeValue(const Standard_Integer theElem)
0174 {
0175 return myData + Stride * size_t(theElem);
0176 }
0177
0178
0179 template <typename Type_t>
0180 inline const Type_t& Value(const Standard_Integer theElem) const
0181 {
0182 return *reinterpret_cast<const Type_t*>(value(theElem));
0183 }
0184
0185
0186 template <typename Type_t>
0187 inline Type_t& ChangeValue(const Standard_Integer theElem)
0188 {
0189 return *reinterpret_cast<Type_t*>(changeValue(theElem));
0190 }
0191
0192
0193 public:
0194 using NCollection_Buffer::ChangeData;
0195 using NCollection_Buffer::Data;
0196
0197
0198
0199
0200
0201
0202 Standard_Byte* ChangeAttributeData(Graphic3d_TypeOfAttribute theAttrib,
0203 Standard_Integer& theAttribIndex,
0204 Standard_Size& theAttribStride)
0205 {
0206 return (Standard_Byte*)AttributeData(theAttrib, theAttribIndex, theAttribStride);
0207 }
0208
0209
0210
0211
0212
0213
0214 const Standard_Byte* AttributeData(Graphic3d_TypeOfAttribute theAttrib,
0215 Standard_Integer& theAttribIndex,
0216 Standard_Size& theAttribStride) const
0217 {
0218 const Standard_Byte* aDataPtr = Data();
0219 if (IsInterleaved())
0220 {
0221 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter)
0222 {
0223 const Graphic3d_Attribute& anAttrib = Attribute(anAttribIter);
0224 const Standard_Size anAttribStride = Graphic3d_Attribute::Stride(anAttrib.DataType);
0225 if (anAttrib.Id == theAttrib)
0226 {
0227 theAttribIndex = anAttribIter;
0228 theAttribStride = Stride;
0229 return aDataPtr;
0230 }
0231
0232 aDataPtr += anAttribStride;
0233 }
0234 }
0235 else
0236 {
0237 const Standard_Integer aNbMaxVerts = NbMaxElements();
0238 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter)
0239 {
0240 const Graphic3d_Attribute& anAttrib = Attribute(anAttribIter);
0241 const Standard_Size anAttribStride = Graphic3d_Attribute::Stride(anAttrib.DataType);
0242 if (anAttrib.Id == theAttrib)
0243 {
0244 theAttribIndex = anAttribIter;
0245 theAttribStride = anAttribStride;
0246 return aDataPtr;
0247 }
0248
0249 aDataPtr += anAttribStride * aNbMaxVerts;
0250 }
0251 }
0252 return NULL;
0253 }
0254
0255 public:
0256
0257 void release()
0258 {
0259 Free();
0260 Stride = 0;
0261 NbElements = 0;
0262 NbAttributes = 0;
0263 }
0264
0265
0266 bool Init(const Standard_Integer theNbElems,
0267 const Graphic3d_Attribute* theAttribs,
0268 const Standard_Integer theNbAttribs)
0269 {
0270 release();
0271 Standard_Integer aStride = 0;
0272 for (Standard_Integer anAttribIter = 0; anAttribIter < theNbAttribs; ++anAttribIter)
0273 {
0274 const Graphic3d_Attribute& anAttrib = theAttribs[anAttribIter];
0275 aStride += anAttrib.Stride();
0276 }
0277 if (aStride == 0)
0278 {
0279 return false;
0280 }
0281
0282 Stride = aStride;
0283 NbElements = theNbElems;
0284 NbAttributes = theNbAttribs;
0285 if (NbElements != 0)
0286 {
0287 const size_t aDataSize = size_t(Stride) * size_t(NbElements);
0288 if (!Allocate(aDataSize + sizeof(Graphic3d_Attribute) * NbAttributes))
0289 {
0290 release();
0291 return false;
0292 }
0293
0294 mySize = aDataSize;
0295 for (Standard_Integer anAttribIter = 0; anAttribIter < theNbAttribs; ++anAttribIter)
0296 {
0297 ChangeAttribute(anAttribIter) = theAttribs[anAttribIter];
0298 }
0299 }
0300 return true;
0301 }
0302
0303
0304 bool Init(const Standard_Integer theNbElems, const Graphic3d_Array1OfAttribute& theAttribs)
0305 {
0306 return Init(theNbElems, &theAttribs.First(), theAttribs.Size());
0307 }
0308
0309 public:
0310
0311
0312 virtual Standard_Boolean IsInterleaved() const { return Standard_True; }
0313
0314
0315
0316 virtual Standard_Boolean IsMutable() const { return Standard_False; }
0317
0318
0319
0320 virtual Graphic3d_BufferRange InvalidatedRange() const { return Graphic3d_BufferRange(); }
0321
0322
0323
0324 virtual void Validate() {}
0325
0326
0327 virtual void Invalidate() {}
0328
0329
0330 Standard_EXPORT virtual void DumpJson(Standard_OStream& theOStream,
0331 Standard_Integer theDepth = -1) const Standard_OVERRIDE;
0332
0333 public:
0334
0335 Standard_Integer Stride;
0336 Standard_Integer NbElements;
0337
0338 Standard_Integer NbAttributes;
0339 };
0340
0341 DEFINE_STANDARD_HANDLE(Graphic3d_Buffer, NCollection_Buffer)
0342
0343 #endif