0024406: Visualization - crash on re-usage of the same primitive array
[occt.git] / src / Graphic3d / Graphic3d_ArrayOfPrimitives.lxx
CommitLineData
b311480e 1// Created on: 2000-06-16
973c2be1 2// Copyright (c) 2000-2014 OPEN CASCADE SAS
b311480e 3//
973c2be1 4// This file is part of Open CASCADE Technology software library.
b311480e 5//
d5f74e42 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
973c2be1 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.
b311480e 11//
973c2be1 12// Alternatively, this file may be used under the terms of Open CASCADE
13// commercial license or contractual agreement.
7fd59977 14
15#include <Graphic3d_ArrayOfPrimitives.hxx>
16#include <Standard_OutOfRange.hxx>
17
b8ddfc2f 18#include <gp_Dir.hxx>
19#include <gp_Pnt.hxx>
7fd59977 20
871fa103 21inline const Handle(Graphic3d_IndexBuffer)& Graphic3d_ArrayOfPrimitives::Indices() const
22{
23 return myIndices;
24}
25
26inline const Handle(Graphic3d_Buffer)& Graphic3d_ArrayOfPrimitives::Attributes() const
b8ddfc2f 27{
871fa103 28 return myAttribs;
29}
30
31inline const Handle(Graphic3d_BoundBuffer)& Graphic3d_ArrayOfPrimitives::Bounds() const
32{
33 return myBounds;
7fd59977 34}
35
b8ddfc2f 36inline Graphic3d_TypeOfPrimitiveArray Graphic3d_ArrayOfPrimitives::Type() const
37{
871fa103 38 return myType;
7fd59977 39}
40
b8ddfc2f 41inline Standard_Boolean Graphic3d_ArrayOfPrimitives::HasVertexNormals() const
42{
871fa103 43 return myVNor != 0;
7fd59977 44}
45
b8ddfc2f 46inline Standard_Boolean Graphic3d_ArrayOfPrimitives::HasVertexColors() const
47{
871fa103 48 return myVCol != 0;
7fd59977 49}
50
b8ddfc2f 51inline Standard_Boolean Graphic3d_ArrayOfPrimitives::HasVertexTexels() const
52{
871fa103 53 return myVTex != 0;
7fd59977 54}
55
b8ddfc2f 56inline Standard_Integer Graphic3d_ArrayOfPrimitives::VertexNumber() const
57{
871fa103 58 return !myAttribs.IsNull() ? myAttribs->NbElements : -1;
7fd59977 59}
60
871fa103 61inline Standard_Integer Graphic3d_ArrayOfPrimitives::AddVertex (const gp_Pnt& theVertex)
b8ddfc2f 62{
871fa103 63 return AddVertex (theVertex.X(), theVertex.Y(), theVertex.Z());
b8ddfc2f 64}
65
871fa103 66inline Standard_Integer Graphic3d_ArrayOfPrimitives::AddVertex (const Standard_Real theX,
67 const Standard_Real theY,
68 const Standard_Real theZ)
b8ddfc2f 69{
871fa103 70 return AddVertex (Standard_ShortReal (theX),
71 Standard_ShortReal (theY),
72 Standard_ShortReal (theZ));
b8ddfc2f 73}
74
871fa103 75inline Standard_Integer Graphic3d_ArrayOfPrimitives::AddVertex (const gp_Pnt& theVertex,
76 const gp_Dir& theNormal)
b8ddfc2f 77{
871fa103 78 return AddVertex (theVertex.X(), theVertex.Y(), theVertex.Z(),
79 theNormal.X(), theNormal.Y(), theNormal.Z());
b8ddfc2f 80}
81
871fa103 82inline Standard_Integer Graphic3d_ArrayOfPrimitives::AddVertex (const Standard_Real theX, const Standard_Real theY, const Standard_Real theZ,
83 const Standard_Real theNX, const Standard_Real theNY, const Standard_Real theNZ)
b8ddfc2f 84{
871fa103 85 return AddVertex (Standard_ShortReal (theX), Standard_ShortReal (theY), Standard_ShortReal (theZ),
86 Standard_ShortReal (theNX), Standard_ShortReal (theNY), Standard_ShortReal (theNZ));
b8ddfc2f 87}
88
871fa103 89inline Standard_Integer Graphic3d_ArrayOfPrimitives::AddVertex (const gp_Pnt& theVertex,
90 const gp_Pnt2d& theTexel)
b8ddfc2f 91{
871fa103 92 return AddVertex (theVertex.X(), theVertex.Y(), theVertex.Z(),
93 theTexel.X(), theTexel.Y());
b8ddfc2f 94}
95
871fa103 96inline Standard_Integer Graphic3d_ArrayOfPrimitives::AddVertex (const Standard_Real theX, const Standard_Real theY, const Standard_Real theZ,
97 const Standard_Real theTX, const Standard_Real theTY)
b8ddfc2f 98{
871fa103 99 return AddVertex (Standard_ShortReal (theX), Standard_ShortReal (theY), Standard_ShortReal (theZ),
100 Standard_ShortReal (theTX), Standard_ShortReal (theTY));
b8ddfc2f 101}
102
871fa103 103inline Standard_Integer Graphic3d_ArrayOfPrimitives::AddVertex (const gp_Pnt& theVertex,
104 const gp_Dir& theNormal,
105 const gp_Pnt2d& theTexel)
b8ddfc2f 106{
871fa103 107 return AddVertex (theVertex.X(), theVertex.Y(), theVertex.Z(),
108 theNormal.X(), theNormal.Y(), theNormal.Z(),
109 theTexel.X(), theTexel.Y());
b8ddfc2f 110}
111
871fa103 112inline Standard_Integer Graphic3d_ArrayOfPrimitives::AddVertex (const Standard_Real theX, const Standard_Real theY, const Standard_Real theZ,
113 const Standard_Real theNX, const Standard_Real theNY, const Standard_Real theNZ,
114 const Standard_Real theTX, const Standard_Real theTY)
b8ddfc2f 115{
871fa103 116 return AddVertex (Standard_ShortReal (theX), Standard_ShortReal (theY), Standard_ShortReal (theZ),
117 Standard_ShortReal (theNX), Standard_ShortReal (theNY), Standard_ShortReal (theNZ),
118 Standard_ShortReal (theTX), Standard_ShortReal (theTY));
b8ddfc2f 119}
120
871fa103 121inline void Graphic3d_ArrayOfPrimitives::SetVertice (const Standard_Integer theIndex,
122 const Standard_ShortReal theX,
123 const Standard_ShortReal theY,
124 const Standard_ShortReal theZ)
b8ddfc2f 125{
871fa103 126 if (myAttribs.IsNull())
127 {
128 return;
129 }
7fd59977 130
871fa103 131 if (theIndex < 1
132 || theIndex > myMaxVertexs)
133 {
134 Standard_OutOfRange::Raise ("BAD VERTEX index");
7fd59977 135 }
871fa103 136
137 Graphic3d_Vec3& aVec = myAttribs->ChangeValue<Graphic3d_Vec3> (theIndex - 1);
138 aVec.x() = theX;
139 aVec.y() = theY;
140 aVec.z() = theZ;
141
142 myAttribs->NbElements = Max (theIndex, myAttribs->NbElements);
7fd59977 143}
144
871fa103 145inline void Graphic3d_ArrayOfPrimitives::SetVertexColor (const Standard_Integer theIndex,
146 const Standard_Real theR,
147 const Standard_Real theG,
148 const Standard_Real theB)
b8ddfc2f 149{
871fa103 150 if (myAttribs.IsNull())
151 {
152 return;
153 }
154
155 if (theIndex < 1
156 || theIndex > myMaxVertexs)
157 {
158 Standard_OutOfRange::Raise ("BAD VERTEX index");
7fd59977 159 }
160
871fa103 161 if (myVCol != 0)
162 {
163 Graphic3d_Vec4ub aColor (Standard_Byte(theR * 255.0),
164 Standard_Byte(theG * 255.0),
165 Standard_Byte(theB * 255.0), 0);
166 SetVertexColor (theIndex, *reinterpret_cast<Standard_Integer*>(&aColor));
6598416b 167 }
871fa103 168 myAttribs->NbElements = Max (theIndex, myAttribs->NbElements);
7fd59977 169}
170
871fa103 171inline void Graphic3d_ArrayOfPrimitives::SetVertexNormal (const Standard_Integer theIndex,
172 const Standard_Real theNX,
173 const Standard_Real theNY,
174 const Standard_Real theNZ)
7fd59977 175{
871fa103 176 if (myAttribs.IsNull())
177 {
178 return;
7fd59977 179 }
180
871fa103 181 if (theIndex < 1
182 || theIndex > myMaxVertexs)
183 {
184 Standard_OutOfRange::Raise ("BAD VERTEX index");
185 }
7fd59977 186
871fa103 187 if (myVNor != 0)
7fd59977 188 {
871fa103 189 Graphic3d_Vec3& aVec = *reinterpret_cast<Graphic3d_Vec3* >(myAttribs->changeValue (theIndex - 1) + size_t(myVNor));
190 aVec.x() = Standard_ShortReal (theNX);
191 aVec.y() = Standard_ShortReal (theNY);
192 aVec.z() = Standard_ShortReal (theNZ);
7fd59977 193 }
871fa103 194 myAttribs->NbElements = Max (theIndex, myAttribs->NbElements);
7fd59977 195}
196
871fa103 197inline void Graphic3d_ArrayOfPrimitives::SetVertexTexel (const Standard_Integer theIndex,
198 const Standard_Real theTX,
199 const Standard_Real theTY)
b8ddfc2f 200{
871fa103 201 if (myAttribs.IsNull())
202 {
203 return;
7fd59977 204 }
205
871fa103 206 if (theIndex < 1
207 || theIndex > myMaxVertexs)
208 {
209 Standard_OutOfRange::Raise ("BAD VERTEX index");
7fd59977 210 }
871fa103 211
212 if (myVTex != 0)
213 {
214 Graphic3d_Vec2& aVec = *reinterpret_cast<Graphic3d_Vec2* >(myAttribs->changeValue (theIndex - 1) + size_t(myVTex));
215 aVec.x() = Standard_ShortReal (theTX);
216 aVec.y() = Standard_ShortReal (theTY);
217 }
218 myAttribs->NbElements = Max (theIndex, myAttribs->NbElements);
7fd59977 219}
220
871fa103 221inline void Graphic3d_ArrayOfPrimitives::SetBoundColor (const Standard_Integer theIndex,
222 const Standard_Real theR,
223 const Standard_Real theG,
224 const Standard_Real theB)
b8ddfc2f 225{
871fa103 226 if (myBounds.IsNull())
227 {
228 return;
7fd59977 229 }
230
871fa103 231 if (theIndex < 1
232 || theIndex > myMaxBounds)
233 {
234 Standard_OutOfRange::Raise ("BAD BOUND index");
235 }
7fd59977 236
871fa103 237 Graphic3d_Vec4& aVec = myBounds->Colors[theIndex - 1];
238 aVec.r() = Standard_ShortReal (theR);
239 aVec.g() = Standard_ShortReal (theG);
240 aVec.b() = Standard_ShortReal (theB);
241 aVec.a() = 1.0f;
242 myBounds->NbBounds = Max (theIndex, myBounds->NbBounds);
7fd59977 243}
244
871fa103 245inline void Graphic3d_ArrayOfPrimitives::Vertice (const Standard_Integer theIndex,
246 Standard_Real& theX,
247 Standard_Real& theY,
248 Standard_Real& theZ) const
b8ddfc2f 249{
871fa103 250 theX = theY = theZ = 0.0;
251 if (myAttribs.IsNull())
252 {
253 return;
7fd59977 254 }
255
871fa103 256 if (theIndex < 1
257 || theIndex > myAttribs->NbElements)
258 {
259 Standard_OutOfRange::Raise ("BAD VERTEX index");
7fd59977 260 }
871fa103 261
262 const Graphic3d_Vec3& aVec = myAttribs->Value<Graphic3d_Vec3> (theIndex - 1);
263 theX = Standard_Real(aVec.x());
264 theY = Standard_Real(aVec.y());
265 theZ = Standard_Real(aVec.z());
7fd59977 266}
267
871fa103 268inline void Graphic3d_ArrayOfPrimitives::VertexColor (const Standard_Integer theIndex,
269 Standard_Real& theR,
270 Standard_Real& theG,
271 Standard_Real& theB) const
b8ddfc2f 272{
871fa103 273 theR = theG = theB = 0.0;
274 if (myAttribs.IsNull())
275 {
276 return;
6598416b 277 }
871fa103 278
279 if (theIndex < 1
280 || theIndex > myAttribs->NbElements)
281 {
282 Standard_OutOfRange::Raise ("BAD VERTEX index");
7fd59977 283 }
871fa103 284 Standard_Integer aColorInt = 0;
285 VertexColor (theIndex, aColorInt);
286 const Graphic3d_Vec4ub& aColor = *reinterpret_cast<const Graphic3d_Vec4ub* >(aColorInt);
287 theR = Standard_Real(aColor.r()) / 255.0;
288 theG = Standard_Real(aColor.g()) / 255.0;
289 theB = Standard_Real(aColor.b()) / 255.0;
7fd59977 290}
291
871fa103 292inline void Graphic3d_ArrayOfPrimitives::VertexColor (const Standard_Integer theIndex,
293 Standard_Integer& theColor) const
7fd59977 294{
871fa103 295 if (myVCol != 0)
296 {
297 theColor = *reinterpret_cast<const Standard_Integer* >(myAttribs->value (theIndex - 1) + size_t(myVCol));
298 }
7fd59977 299}
300
871fa103 301inline void Graphic3d_ArrayOfPrimitives::VertexNormal (const Standard_Integer theIndex,
302 Standard_Real& theNX,
303 Standard_Real& theNY,
304 Standard_Real& theNZ) const
b8ddfc2f 305{
871fa103 306 theNX = theNY = theNZ = 0.0;
307 if (myAttribs.IsNull())
308 {
309 return;
310 }
311
312 if (theIndex < 1
313 || theIndex > myAttribs->NbElements)
314 {
315 Standard_OutOfRange::Raise ("BAD VERTEX index");
7fd59977 316 }
317
871fa103 318 if (myVNor != 0)
319 {
320 const Graphic3d_Vec3& aVec = *reinterpret_cast<const Graphic3d_Vec3* >(myAttribs->value (theIndex - 1) + size_t(myVNor));
321 theNX = Standard_Real(aVec.x());
322 theNY = Standard_Real(aVec.y());
323 theNZ = Standard_Real(aVec.z());
7fd59977 324 }
325}
326
871fa103 327inline void Graphic3d_ArrayOfPrimitives::VertexTexel (const Standard_Integer theIndex,
328 Standard_Real& theTX,
329 Standard_Real& theTY) const
b8ddfc2f 330{
871fa103 331 theTX = theTY = 0.0;
332 if (myAttribs.IsNull())
333 {
334 return;
7fd59977 335 }
336
871fa103 337 if (theIndex < 1
338 || theIndex > myAttribs->NbElements)
339 {
340 Standard_OutOfRange::Raise ("BAD VERTEX index");
7fd59977 341 }
7fd59977 342
871fa103 343 if (myVTex != 0)
344 {
345 const Graphic3d_Vec2& aVec = *reinterpret_cast<const Graphic3d_Vec2* >(myAttribs->value (theIndex - 1) + size_t(myVTex));
346 theTX = Standard_Real(aVec.x());
347 theTY = Standard_Real(aVec.y());
348 }
7fd59977 349}
350
871fa103 351inline Standard_Integer Graphic3d_ArrayOfPrimitives::EdgeNumber() const
b8ddfc2f 352{
871fa103 353 return !myIndices.IsNull() ? myIndices->NbElements : -1;
7fd59977 354}
355
871fa103 356inline Standard_Integer Graphic3d_ArrayOfPrimitives::Edge (const Standard_Integer theIndex) const
b8ddfc2f 357{
871fa103 358 if (myIndices.IsNull()
359 || theIndex <= 0
360 || theIndex > myIndices->NbElements)
361 {
362 Standard_OutOfRange::Raise ("BAD EDGE index");
7fd59977 363 }
871fa103 364 return Standard_Integer(myIndices->Index (theIndex - 1) + 1);
7fd59977 365}
366
b8ddfc2f 367inline Standard_Boolean Graphic3d_ArrayOfPrimitives::HasBoundColors() const
368{
871fa103 369 return !myBounds.IsNull() && myBounds->Colors != NULL;
7fd59977 370}
371
b8ddfc2f 372inline Standard_Integer Graphic3d_ArrayOfPrimitives::BoundNumber() const
373{
871fa103 374 return !myBounds.IsNull() ? myBounds->NbBounds : -1;
7fd59977 375}
376
871fa103 377inline Standard_Integer Graphic3d_ArrayOfPrimitives::Bound (const Standard_Integer theIndex) const
b8ddfc2f 378{
871fa103 379 if (myBounds.IsNull()
380 || theIndex <= 0
381 || theIndex > myBounds->NbBounds)
382 {
383 Standard_OutOfRange::Raise ("BAD BOUND index");
7fd59977 384 }
871fa103 385 return myBounds->Bounds[theIndex - 1];
7fd59977 386}
387
871fa103 388inline void Graphic3d_ArrayOfPrimitives::BoundColor (const Standard_Integer theIndex,
389 Standard_Real& theR,
390 Standard_Real& theG,
391 Standard_Real& theB) const
b8ddfc2f 392{
871fa103 393 if (myBounds.IsNull()
394 || myBounds->Colors == NULL
395 || theIndex <= 0
396 || theIndex > myBounds->NbBounds)
397 {
7fd59977 398 Standard_OutOfRange::Raise(" BAD BOUND index");
399 }
871fa103 400
401 const Graphic3d_Vec4& aVec = myBounds->Colors[theIndex - 1];
402 theR = Standard_Real(aVec.r());
403 theG = Standard_Real(aVec.g());
404 theB = Standard_Real(aVec.b());
7fd59977 405}