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