0027670: Visualization - avoid duplication of structures defining primitive array...
[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 gp_Pnt&           theVertex,
72                                                                 const Graphic3d_Vec4ub& theColor)
73 {
74   const Standard_Integer anIndex = AddVertex (theVertex);
75   SetVertexColor (anIndex, theColor);
76   return anIndex;
77 }
78
79 inline Standard_Integer Graphic3d_ArrayOfPrimitives::AddVertex (const Standard_Real theX,
80                                                                 const Standard_Real theY,
81                                                                 const Standard_Real theZ)
82 {
83   return AddVertex (RealToShortReal (theX),
84                     RealToShortReal (theY),
85                     RealToShortReal (theZ));
86 }
87
88 inline Standard_Integer Graphic3d_ArrayOfPrimitives::AddVertex (const gp_Pnt& theVertex,
89                                                                 const gp_Dir& theNormal)
90 {
91   return AddVertex (theVertex.X(), theVertex.Y(), theVertex.Z(),
92                     theNormal.X(), theNormal.Y(), theNormal.Z());
93 }
94
95 inline Standard_Integer Graphic3d_ArrayOfPrimitives::AddVertex (const Standard_Real theX,  const Standard_Real theY,  const Standard_Real theZ,
96                                                                 const Standard_Real theNX, const Standard_Real theNY, const Standard_Real theNZ)
97 {
98   return AddVertex (RealToShortReal (theX),  RealToShortReal (theY),  RealToShortReal (theZ),
99                     Standard_ShortReal (theNX), Standard_ShortReal (theNY), Standard_ShortReal (theNZ));
100 }
101
102 inline Standard_Integer Graphic3d_ArrayOfPrimitives::AddVertex (const gp_Pnt&   theVertex,
103                                                                 const gp_Pnt2d& theTexel)
104 {
105   return AddVertex (theVertex.X(), theVertex.Y(), theVertex.Z(),
106                     theTexel.X(), theTexel.Y());
107 }
108
109 inline Standard_Integer Graphic3d_ArrayOfPrimitives::AddVertex (const Standard_Real theX,  const Standard_Real theY,  const Standard_Real theZ,
110                                                                 const Standard_Real theTX, const Standard_Real theTY)
111 {
112   return AddVertex (RealToShortReal (theX),  RealToShortReal (theY),  RealToShortReal (theZ),
113                     Standard_ShortReal (theTX), Standard_ShortReal (theTY));
114 }
115
116 inline Standard_Integer Graphic3d_ArrayOfPrimitives::AddVertex (const gp_Pnt&   theVertex,
117                                                                 const gp_Dir&   theNormal,
118                                                                 const gp_Pnt2d& theTexel)
119 {
120   return AddVertex (theVertex.X(), theVertex.Y(), theVertex.Z(),
121                     theNormal.X(), theNormal.Y(), theNormal.Z(),
122                     theTexel.X(),  theTexel.Y());
123 }
124
125 inline Standard_Integer Graphic3d_ArrayOfPrimitives::AddVertex (const Standard_Real theX,  const Standard_Real theY,  const Standard_Real theZ,
126                                                                 const Standard_Real theNX, const Standard_Real theNY, const Standard_Real theNZ,
127                                                                 const Standard_Real theTX, const Standard_Real theTY)
128 {
129   return AddVertex (RealToShortReal (theX),  RealToShortReal (theY),  RealToShortReal (theZ),
130                     Standard_ShortReal (theNX), Standard_ShortReal (theNY), Standard_ShortReal (theNZ),
131                     Standard_ShortReal (theTX), Standard_ShortReal (theTY));
132 }
133
134 inline void Graphic3d_ArrayOfPrimitives::SetVertice (const Standard_Integer   theIndex,
135                                                      const Standard_ShortReal theX,
136                                                      const Standard_ShortReal theY,
137                                                      const Standard_ShortReal theZ)
138 {
139   if (myAttribs.IsNull())
140   {
141     return;
142   }
143
144   if (theIndex < 1
145    || theIndex > myMaxVertexs)
146   {
147     Standard_OutOfRange::Raise ("BAD VERTEX index");
148   }
149
150   Graphic3d_Vec3& aVec = myAttribs->ChangeValue<Graphic3d_Vec3> (theIndex - 1);
151   aVec.x() = theX;
152   aVec.y() = theY;
153   aVec.z() = theZ;
154
155   myAttribs->NbElements = Max (theIndex, myAttribs->NbElements);
156 }
157
158 inline void Graphic3d_ArrayOfPrimitives::SetVertexColor (const Standard_Integer theIndex,
159                                                          const Standard_Real    theR,
160                                                          const Standard_Real    theG,
161                                                          const Standard_Real    theB)
162 {
163   if (myAttribs.IsNull())
164   {
165     return;
166   }
167
168   if (theIndex < 1
169    || theIndex > myMaxVertexs)
170   {
171     Standard_OutOfRange::Raise ("BAD VERTEX index");
172   }
173
174   if (myVCol != 0)
175   {
176     Graphic3d_Vec4ub aColor (Standard_Byte(theR * 255.0),
177                              Standard_Byte(theG * 255.0),
178                              Standard_Byte(theB * 255.0), 0);
179     SetVertexColor (theIndex, *reinterpret_cast<Standard_Integer*>(&aColor));
180   }
181   myAttribs->NbElements = Max (theIndex, myAttribs->NbElements);
182 }
183
184 inline void Graphic3d_ArrayOfPrimitives::SetVertexColor (const Standard_Integer  theIndex,
185                                                          const Graphic3d_Vec4ub& theColor)
186 {
187   SetVertexColor (theIndex, *reinterpret_cast<const Standard_Integer*> (&theColor));
188 }
189
190 inline void Graphic3d_ArrayOfPrimitives::SetVertexNormal (const Standard_Integer theIndex,
191                                                           const Standard_Real    theNX,
192                                                           const Standard_Real    theNY,
193                                                           const Standard_Real    theNZ)
194 {
195   if (myAttribs.IsNull())
196   {
197     return;
198   }
199
200   if (theIndex < 1
201    || theIndex > myMaxVertexs)
202   {
203     Standard_OutOfRange::Raise ("BAD VERTEX index");
204   }
205
206   if (myVNor != 0)
207   {
208     Graphic3d_Vec3& aVec = *reinterpret_cast<Graphic3d_Vec3* >(myAttribs->changeValue (theIndex - 1) + size_t(myVNor));
209     aVec.x() = Standard_ShortReal (theNX);
210     aVec.y() = Standard_ShortReal (theNY);
211     aVec.z() = Standard_ShortReal (theNZ);
212   }
213   myAttribs->NbElements = Max (theIndex, myAttribs->NbElements);
214 }
215
216 inline void Graphic3d_ArrayOfPrimitives::SetVertexTexel (const Standard_Integer theIndex,
217                                                          const Standard_Real    theTX,
218                                                          const Standard_Real    theTY)
219 {
220   if (myAttribs.IsNull())
221   {
222     return;
223   }
224
225   if (theIndex < 1
226    || theIndex > myMaxVertexs)
227   {
228     Standard_OutOfRange::Raise ("BAD VERTEX index");
229   }
230
231   if (myVTex != 0)
232   {
233     Graphic3d_Vec2& aVec = *reinterpret_cast<Graphic3d_Vec2* >(myAttribs->changeValue (theIndex - 1) + size_t(myVTex));
234     aVec.x() = Standard_ShortReal (theTX);
235     aVec.y() = Standard_ShortReal (theTY);
236   }
237   myAttribs->NbElements = Max (theIndex, myAttribs->NbElements);
238 }
239
240 inline void Graphic3d_ArrayOfPrimitives::SetBoundColor (const Standard_Integer theIndex,
241                                                         const Standard_Real    theR,
242                                                         const Standard_Real    theG,
243                                                         const Standard_Real    theB)
244 {
245   if (myBounds.IsNull())
246   {
247     return;
248   }
249
250   if (theIndex < 1
251    || theIndex > myMaxBounds)
252   {
253     Standard_OutOfRange::Raise ("BAD BOUND index");
254   }
255
256   Graphic3d_Vec4& aVec = myBounds->Colors[theIndex - 1];
257   aVec.r() = Standard_ShortReal (theR);
258   aVec.g() = Standard_ShortReal (theG);
259   aVec.b() = Standard_ShortReal (theB);
260   aVec.a() = 1.0f;
261   myBounds->NbBounds = Max (theIndex, myBounds->NbBounds);
262 }
263
264 inline void Graphic3d_ArrayOfPrimitives::Vertice (const Standard_Integer theIndex,
265                                                   Standard_Real&         theX,
266                                                   Standard_Real&         theY,
267                                                   Standard_Real&         theZ) const
268 {
269   theX = theY = theZ = 0.0;
270   if (myAttribs.IsNull())
271   {
272     return;
273   }
274
275   if (theIndex < 1
276    || theIndex > myAttribs->NbElements)
277   {
278     Standard_OutOfRange::Raise ("BAD VERTEX index");
279   }
280
281   const Graphic3d_Vec3& aVec = myAttribs->Value<Graphic3d_Vec3> (theIndex - 1);
282   theX = Standard_Real(aVec.x());
283   theY = Standard_Real(aVec.y());
284   theZ = Standard_Real(aVec.z());
285 }
286
287 inline void Graphic3d_ArrayOfPrimitives::VertexColor (const Standard_Integer theIndex,
288                                                       Standard_Real&         theR,
289                                                       Standard_Real&         theG,
290                                                       Standard_Real&         theB) const
291 {
292   theR = theG = theB = 0.0;
293   if (myAttribs.IsNull())
294   {
295     return;
296   }
297
298   if (theIndex < 1
299    || theIndex > myAttribs->NbElements)
300   {
301     Standard_OutOfRange::Raise ("BAD VERTEX index");
302   }
303   Standard_Integer aColorInt = 0;
304   VertexColor (theIndex, aColorInt);
305   const Graphic3d_Vec4ub& aColor = *reinterpret_cast<const Graphic3d_Vec4ub* >(&aColorInt);
306   theR = Standard_Real(aColor.r()) / 255.0;
307   theG = Standard_Real(aColor.g()) / 255.0;
308   theB = Standard_Real(aColor.b()) / 255.0;
309 }
310
311 inline void Graphic3d_ArrayOfPrimitives::VertexColor (const Standard_Integer theIndex,
312                                                       Standard_Integer&      theColor) const
313 {
314   if (myVCol != 0)
315   {
316     theColor = *reinterpret_cast<const Standard_Integer* >(myAttribs->value (theIndex - 1) + size_t(myVCol));
317   }
318 }
319
320 inline void Graphic3d_ArrayOfPrimitives::VertexNormal (const Standard_Integer theIndex,
321                                                        Standard_Real&         theNX,
322                                                        Standard_Real&         theNY,
323                                                        Standard_Real&         theNZ) const
324 {
325   theNX = theNY = theNZ = 0.0;
326   if (myAttribs.IsNull())
327   {
328     return;
329   }
330
331   if (theIndex < 1
332    || theIndex > myAttribs->NbElements)
333   {
334     Standard_OutOfRange::Raise ("BAD VERTEX index");
335   }
336
337   if (myVNor != 0)
338   {
339     const Graphic3d_Vec3& aVec = *reinterpret_cast<const Graphic3d_Vec3* >(myAttribs->value (theIndex - 1) + size_t(myVNor));
340     theNX = Standard_Real(aVec.x());
341     theNY = Standard_Real(aVec.y());
342     theNZ = Standard_Real(aVec.z());
343   }
344 }
345
346 inline void Graphic3d_ArrayOfPrimitives::VertexTexel (const Standard_Integer theIndex,
347                                                       Standard_Real&         theTX,
348                                                       Standard_Real&         theTY) const
349 {
350   theTX = theTY = 0.0;
351   if (myAttribs.IsNull())
352   {
353     return;
354   }
355
356   if (theIndex < 1
357    || theIndex > myAttribs->NbElements)
358   {
359     Standard_OutOfRange::Raise ("BAD VERTEX index");
360   }
361
362   if (myVTex != 0)
363   {
364     const Graphic3d_Vec2& aVec = *reinterpret_cast<const Graphic3d_Vec2* >(myAttribs->value (theIndex - 1) + size_t(myVTex));
365     theTX = Standard_Real(aVec.x());
366     theTY = Standard_Real(aVec.y());
367   }
368 }
369
370 inline Standard_Integer Graphic3d_ArrayOfPrimitives::EdgeNumber() const
371 {
372   return !myIndices.IsNull() ? myIndices->NbElements : -1;
373 }
374
375 inline Standard_Integer Graphic3d_ArrayOfPrimitives::Edge (const Standard_Integer theIndex) const
376 {
377   if (myIndices.IsNull()
378    || theIndex <= 0
379    || theIndex > myIndices->NbElements)
380   {
381     Standard_OutOfRange::Raise ("BAD EDGE index");
382   }
383   return Standard_Integer(myIndices->Index (theIndex - 1) + 1);
384 }
385
386 inline Standard_Boolean Graphic3d_ArrayOfPrimitives::HasBoundColors() const
387 {
388   return !myBounds.IsNull() && myBounds->Colors != NULL;
389 }
390
391 inline Standard_Integer Graphic3d_ArrayOfPrimitives::BoundNumber() const
392 {
393   return !myBounds.IsNull() ? myBounds->NbBounds : -1;
394 }
395
396 inline Standard_Integer Graphic3d_ArrayOfPrimitives::Bound (const Standard_Integer theIndex) const
397 {
398   if (myBounds.IsNull()
399    || theIndex <= 0
400    || theIndex > myBounds->NbBounds)
401   {
402     Standard_OutOfRange::Raise ("BAD BOUND index");
403   }
404   return myBounds->Bounds[theIndex - 1];
405 }
406
407 inline void Graphic3d_ArrayOfPrimitives::BoundColor (const Standard_Integer theIndex,
408                                                      Standard_Real&         theR,
409                                                      Standard_Real&         theG,
410                                                      Standard_Real&         theB) const
411 {
412   if (myBounds.IsNull()
413    || myBounds->Colors == NULL
414    || theIndex <= 0
415    || theIndex > myBounds->NbBounds)
416   {
417     Standard_OutOfRange::Raise(" BAD BOUND index");
418   }
419
420   const Graphic3d_Vec4& aVec = myBounds->Colors[theIndex - 1];
421   theR = Standard_Real(aVec.r());
422   theG = Standard_Real(aVec.g());
423   theB = Standard_Real(aVec.b());
424 }