MRPT  2.0.4
TTriangle.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <mrpt/core/bits_math.h>
12 #include <mrpt/core/exceptions.h>
13 #include <mrpt/img/TColor.h>
14 #include <mrpt/math/TPoint2D.h>
15 #include <mrpt/math/TPoint3D.h>
16 #include <mrpt/math/TPolygon3D.h>
18 #include <array>
19 
20 namespace mrpt::opengl
21 {
22 // Ensure 1-byte memory alignment, no additional stride bytes.
23 #pragma pack(push, 1)
24 
25 /** A triangle (float coordinates) with RGBA colors (u8) and UV (texture
26  * coordinates) for each vertex. Note that not all the fields must be filled in,
27  * it depends on the consumer of the structure.
28  *
29  * The structure is memory packed to 1-byte, to ensure it can be used in GPU
30  * memory vertex arrays without unexpected paddings.
31  *
32  * \sa opengl::COpenGLScene, CSetOfTexturedTriangles
33  * \ingroup mrpt_opengl_grp
34  */
35 struct TTriangle
36 {
37  struct Vertex
38  {
40  mrpt::math::TVector3Df normal; //!< Must not be normalized
41  mrpt::math::TVector2Df uv; //!< texture coordinates (0,0)-(1,1)
42 
43  void setColor(const mrpt::img::TColor& c)
44  {
45  xyzrgba.r = c.R;
46  xyzrgba.g = c.G;
47  xyzrgba.b = c.B;
48  xyzrgba.a = c.A;
49  }
50  };
51 
52  TTriangle() = default;
53  explicit TTriangle(const mrpt::math::TPolygon3D& p)
54  {
55  ASSERT_EQUAL_(p.size(), 3U);
56  for (size_t i = 0; i < 3; i++)
57  {
58  auto& pp = vertices[i].xyzrgba;
59  pp.pt = p[i];
60  pp.r = pp.g = pp.b = 0xff;
61  }
63  }
64  /** Constructor from 3 points (default normals are computed) */
65  explicit TTriangle(
66  const mrpt::math::TPoint3Df& p1, const mrpt::math::TPoint3Df& p2,
67  const mrpt::math::TPoint3Df& p3)
68  {
69  vertices[0].xyzrgba.pt = p1;
70  vertices[1].xyzrgba.pt = p2;
71  vertices[2].xyzrgba.pt = p3;
73  }
74  /** Constructor from 3 points and its 3 normals */
75  explicit TTriangle(
76  const mrpt::math::TPoint3Df& p1, const mrpt::math::TPoint3Df& p2,
77  const mrpt::math::TPoint3Df& p3, const mrpt::math::TVector3Df& n1,
79  {
80  vertices[0].xyzrgba.pt = p1;
81  vertices[0].normal = n1;
82  vertices[1].xyzrgba.pt = p2;
83  vertices[1].normal = n2;
84  vertices[2].xyzrgba.pt = p3;
85  vertices[2].normal = n3;
86  }
87 
88  std::array<Vertex, 3> vertices;
89 
90  const float& x(size_t i) const { return vertices[i].xyzrgba.pt.x; }
91  const float& y(size_t i) const { return vertices[i].xyzrgba.pt.y; }
92  const float& z(size_t i) const { return vertices[i].xyzrgba.pt.z; }
93  const uint8_t& r(size_t i) const { return vertices[i].xyzrgba.r; }
94  const uint8_t& g(size_t i) const { return vertices[i].xyzrgba.g; }
95  const uint8_t& b(size_t i) const { return vertices[i].xyzrgba.b; }
96  const uint8_t& a(size_t i) const { return vertices[i].xyzrgba.a; }
97  const float& u(size_t i) const { return vertices[i].uv.x; }
98  const float& v(size_t i) const { return vertices[i].uv.y; }
99  float& x(size_t i) { return vertices[i].xyzrgba.pt.x; }
100  float& y(size_t i) { return vertices[i].xyzrgba.pt.y; }
101  float& z(size_t i) { return vertices[i].xyzrgba.pt.z; }
102  uint8_t& r(size_t i) { return vertices[i].xyzrgba.r; }
103  uint8_t& g(size_t i) { return vertices[i].xyzrgba.g; }
104  uint8_t& b(size_t i) { return vertices[i].xyzrgba.b; }
105  uint8_t& a(size_t i) { return vertices[i].xyzrgba.a; }
106  float& u(size_t i) { return vertices[i].uv.x; }
107  float& v(size_t i) { return vertices[i].uv.y; }
108 
109  mrpt::math::TPoint3Df& vertex(size_t i) { return vertices[i].xyzrgba.pt; }
110  const mrpt::math::TPoint3Df& vertex(size_t i) const
111  {
112  return vertices[i].xyzrgba.pt;
113  }
114 
115  /** Sets the color of all vertices */
116  inline void setColor(const mrpt::img::TColor& c)
117  {
118  for (size_t i = 0; i < 3; i++)
119  {
120  vertices[i].xyzrgba.r = c.R;
121  vertices[i].xyzrgba.g = c.G;
122  vertices[i].xyzrgba.b = c.B;
123  vertices[i].xyzrgba.a = c.A;
124  }
125  }
126  inline void setColor(const mrpt::img::TColorf& c)
127  {
128  setColor(mrpt::img::TColor(f2u8(c.R), f2u8(c.G), f2u8(c.B), f2u8(c.A)));
129  }
130 
131  /** Compute the three normals from the cross-product of "v01 x v02".
132  * Note that using this default normals will not lead to interpolated
133  * lighting in the fragment shaders, since all vertex are equal; a derived
134  * class should use custom, more accurate normals to enable soft lighting.
135  */
136  void computeNormals();
137 
138  // These methods are explicitly instantiated for T=float and double.
140  void writeTo(mrpt::serialization::CArchive& o) const;
141 };
142 #pragma pack(pop)
143 
144 } // namespace mrpt::opengl
mrpt::math::TPoint3Df & vertex(size_t i)
Definition: TTriangle.h:109
const uint8_t & b(size_t i) const
Definition: TTriangle.h:95
uint8_t & b(size_t i)
Definition: TTriangle.h:104
const mrpt::math::TPoint3Df & vertex(size_t i) const
Definition: TTriangle.h:110
const uint8_t & r(size_t i) const
Definition: TTriangle.h:93
const float & x(size_t i) const
Definition: TTriangle.h:90
void readFrom(mrpt::serialization::CArchive &i)
Definition: TTriangle.cpp:43
A triangle (float coordinates) with RGBA colors (u8) and UV (texture coordinates) for each vertex...
Definition: TTriangle.h:35
const uint8_t & g(size_t i) const
Definition: TTriangle.h:94
mrpt::math::TVector2Df uv
texture coordinates (0,0)-(1,1)
Definition: TTriangle.h:41
uint8_t B
Definition: TColor.h:51
uint8_t & r(size_t i)
Definition: TTriangle.h:102
uint8_t G
Definition: TColor.h:51
TTriangle(const mrpt::math::TPoint3Df &p1, const mrpt::math::TPoint3Df &p2, const mrpt::math::TPoint3Df &p3)
Constructor from 3 points (default normals are computed)
Definition: TTriangle.h:65
float & y(size_t i)
Definition: TTriangle.h:100
const uint8_t & a(size_t i) const
Definition: TTriangle.h:96
std::array< Vertex, 3 > vertices
Definition: TTriangle.h:88
const float & u(size_t i) const
Definition: TTriangle.h:97
mrpt::math::TVector3Df normal
Must not be normalized.
Definition: TTriangle.h:40
TTriangle(const mrpt::math::TPoint3Df &p1, const mrpt::math::TPoint3Df &p2, const mrpt::math::TPoint3Df &p3, const mrpt::math::TVector3Df &n1, const mrpt::math::TVector3Df &n2, const mrpt::math::TVector3Df &n3)
Constructor from 3 points and its 3 normals.
Definition: TTriangle.h:75
#define ASSERT_EQUAL_(__A, __B)
Assert comparing two values, reporting their actual values upon failure.
Definition: exceptions.h:137
float & z(size_t i)
Definition: TTriangle.h:101
TTriangle(const mrpt::math::TPolygon3D &p)
Definition: TTriangle.h:53
XYZ point (float) + RGBA(u8)
Definition: TPoint3D.h:330
float & u(size_t i)
Definition: TTriangle.h:106
uint8_t R
Definition: TColor.h:51
Base template for TPoint2D and TPoint2Df.
Definition: TPoint2D.h:32
float & v(size_t i)
Definition: TTriangle.h:107
void setColor(const mrpt::img::TColor &c)
Sets the color of all vertices.
Definition: TTriangle.h:116
float & x(size_t i)
Definition: TTriangle.h:99
uint8_t f2u8(const float f)
converts a float [0,1] into an uint8_t [0,255] (without checking for out of bounds) ...
const float & y(size_t i) const
Definition: TTriangle.h:91
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:54
mrpt::math::TPointXYZfRGBAu8 xyzrgba
Definition: TTriangle.h:39
const float & z(size_t i) const
Definition: TTriangle.h:92
An RGBA color - floats in the range [0,1].
Definition: TColor.h:88
uint8_t & a(size_t i)
Definition: TTriangle.h:105
void setColor(const mrpt::img::TColorf &c)
Definition: TTriangle.h:126
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:13
void computeNormals()
Compute the three normals from the cross-product of "v01 x v02".
Definition: TTriangle.cpp:21
const float & v(size_t i) const
Definition: TTriangle.h:98
A RGB color - 8bit.
Definition: TColor.h:25
uint8_t & g(size_t i)
Definition: TTriangle.h:103
3D polygon, inheriting from std::vector<TPoint3D>
Definition: TPolygon3D.h:20
uint8_t A
Definition: TColor.h:51
void writeTo(mrpt::serialization::CArchive &o) const
Definition: TTriangle.cpp:35
void setColor(const mrpt::img::TColor &c)
Definition: TTriangle.h:43



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020