NAP
polyline.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 #include "mesh.h"
8 #include <mathutils.h>
9 #include <lineutils.h>
10 #include <unordered_map>
11 
12 namespace nap
13 {
14  // Forward Declares
15  class Core;
16  class RenderService;
17 
22  {
24  glm::vec4 mColor = { 1.0f, 1.0f, 1.0f,1.0f };
25  };
26 
27 
29  // PolyLine
31 
37  class NAPAPI PolyLine : public IMesh
38  {
39  RTTI_ENABLE(IMesh)
40  public:
41  PolyLine(nap::Core& core);
42 
49  virtual bool init(utility::ErrorState& errorState) override;
50 
54  virtual MeshInstance& getMeshInstance() override { return *mMeshInstance; }
55 
59  virtual const MeshInstance& getMeshInstance() const override { return *mMeshInstance; }
60 
64  Vec3VertexAttribute& getPositionAttr();
65 
69  const Vec3VertexAttribute& getPositionAttr() const;
70 
74  Vec4VertexAttribute& getColorAttr();
75 
79  const Vec4VertexAttribute& getColorAttr() const;
80 
84  Vec3VertexAttribute& getNormalAttr();
85 
89  const Vec3VertexAttribute& getNormalAttr() const;
90 
94  Vec3VertexAttribute& getUvAttr();
95 
99  const Vec3VertexAttribute& getUvAttr() const;
100 
109  template<typename T>
110  void getValue(const VertexAttribute<T>& attr, float location, T& outValue) const;
111 
121  template<typename T>
122  void getValue(const std::map<float, int>& distanceMap, const VertexAttribute<T>& attr, float location, T& outValue) const;
123 
133  void getNormal(const std::map<float, int>& distanceMap, const Vec3VertexAttribute& attr, float location, glm::vec3& outValue) const;
134 
141  float getDistances(std::map<float, int>& outDistances) const;
142 
146  virtual bool isClosed() const = 0;
147 
148  // Properties associated with a line
150 
151  protected:
152  std::unique_ptr<MeshInstance> mMeshInstance;
153  RenderService* mRenderService = nullptr;
154 
159  static void createVertexAttributes(MeshInstance& instance);
160  };
161 
162 
164  // Line
166 
172  class NAPAPI Line : public PolyLine
173  {
174  RTTI_ENABLE(PolyLine)
175  public:
176  Line(nap::Core& core);
177 
182  virtual bool init(utility::ErrorState& errorState) override;
183 
187  virtual bool isClosed() const override { return mClosed; }
188 
189  glm::vec3 mStart = { -0.5f, 0.0f, 0.0f };
190  glm::vec3 mEnd = { 0.5f, 0.0f, 0.0f };
191  bool mClosed = false;
192  int mVertexCount = 2;
193 
194  };
195 
196 
198  // Rectangle
200 
207  class NAPAPI Rectangle : public PolyLine
208  {
209  RTTI_ENABLE(PolyLine)
210  public:
211  Rectangle(nap::Core& core);
212 
217  virtual bool init(utility::ErrorState& errorState) override;
218 
222  virtual bool isClosed() const override { return true; }
223 
224  glm::vec2 mDimensions = { 1.0f, 1.0f };
225  };
226 
227 
229  // Circle
231 
236  class NAPAPI Circle : public PolyLine
237  {
238  RTTI_ENABLE(PolyLine)
239  public:
240  Circle(nap::Core& core);
241 
246  virtual bool init(utility::ErrorState& errorState) override;
247 
251  virtual bool isClosed() const override { return true; }
252 
253  float mRadius = 1.0f;
254  int mSegments = 100;
255  };
256 
257 
259  // Hexagon
261 
266  class NAPAPI Hexagon : public PolyLine
267  {
268  RTTI_ENABLE(PolyLine)
269  public:
270  Hexagon(nap::Core& core);
271  float mRadius = 1.0f;
272 
277  virtual bool init(utility::ErrorState& errorState) override;
278 
282  virtual bool isClosed() const override { return true; }
283  };
284 
285 
287  // TriangleLine
289 
294  class NAPAPI TriangleLine : public PolyLine
295  {
296  RTTI_ENABLE(PolyLine)
297  public:
298  TriangleLine(nap::Core& core);
299  float mRadius = 1.0f;
300 
305  virtual bool init(utility::ErrorState& errorState) override;
306 
310  virtual bool isClosed() const override { return true; }
311  };
312 
313 
315  // Template definitions
317 
318  template<typename T>
319  void nap::PolyLine::getValue(const VertexAttribute<T>& attr, float location, T& outValue) const
320  {
321  return math::getValueAlongLine(attr.getData(), location, isClosed(), outValue);
322  }
323 
324  template<typename T>
325  void nap::PolyLine::getValue(const std::map<float, int>& distanceMap, const VertexAttribute<T>& attr, float location, T& outValue) const
326  {
327  return math::getValueAlongLine(distanceMap, attr.getData(), location, outValue);
328  }
329 }
nap::PolyLine::mMeshInstance
std::unique_ptr< MeshInstance > mMeshInstance
Definition: polyline.h:152
nap::Line
Definition: polyline.h:172
nap::VertexAttribute< glm::vec3 >
nap::EMemoryUsage::Static
@ Static
Buffer data is uploaded only once from the CPU to the GPU.
nap::Rectangle::isClosed
virtual bool isClosed() const override
Definition: polyline.h:222
nap::Rectangle
Definition: polyline.h:207
nap::TriangleLine
Definition: polyline.h:294
nap::PolyLine::getMeshInstance
virtual const MeshInstance & getMeshInstance() const override
Definition: polyline.h:59
nap::EMemoryUsage
EMemoryUsage
Definition: gpubuffer.h:41
nap::PolyLineProperties::mColor
glm::vec4 mColor
Property: 'Color' RGBA color of the line.
Definition: polyline.h:24
nap::Hexagon::isClosed
virtual bool isClosed() const override
Definition: polyline.h:282
nap::utility::ErrorState
Definition: errorstate.h:19
nap::PolyLine::mLineProperties
PolyLineProperties mLineProperties
Definition: polyline.h:149
nap::IMesh
Definition: mesh.h:394
nap::PolyLine::getMeshInstance
virtual MeshInstance & getMeshInstance() override
Definition: polyline.h:54
nap::PolyLineProperties
Definition: polyline.h:21
nap::Circle
Definition: polyline.h:236
nap::PolyLine
Definition: polyline.h:37
nap::Circle::isClosed
virtual bool isClosed() const override
Definition: polyline.h:251
nap::RenderService
Definition: renderservice.h:275
nap::Core
Definition: core.h:82
nap::TriangleLine::isClosed
virtual bool isClosed() const override
Definition: polyline.h:310
nap::Hexagon
Definition: polyline.h:266
nap::VertexAttribute::getData
const std::vector< ELEMENTTYPE > & getData() const
Definition: vertexattribute.h:95
nap::PolyLineProperties::mUsage
EMemoryUsage mUsage
Property: 'Usage' If the line is created once or frequently updated.
Definition: polyline.h:23
nap
Definition: templateapp.h:17
nap::MeshInstance
Definition: mesh.h:196
nap::Line::isClosed
virtual bool isClosed() const override
Definition: polyline.h:187
nap::math::getValueAlongLine
void getValueAlongLine(const std::vector< T > &vertexData, float location, bool closed, T &outValue)
Definition: lineutils.h:93
nap::PolyLine::getValue
void getValue(const VertexAttribute< T > &attr, float location, T &outValue) const
Definition: polyline.h:319