1 /*
2  MDAL - Mesh Data Abstraction Library (MIT License)
3  Copyright (C) 2018 Peter Petrik (zilolv at gmail dot com)
4 */
5 
6 #ifndef MDAL_MEMORY_DATA_MODEL_HPP
7 #define MDAL_MEMORY_DATA_MODEL_HPP
8 
9 #include <stddef.h>
10 #include <assert.h>
11 #include <vector>
12 #include <memory>
13 #include <map>
14 #include <string>
15 #include "mdal.h"
16 #include "mdal_data_model.hpp"
17 
18 namespace MDAL
19 {
20   class MemoryMesh;
21 
22   typedef struct VertexType
23   {
24     double x = std::numeric_limits<double>::quiet_NaN();
25     double y = std::numeric_limits<double>::quiet_NaN();
26     double z = 0.0; // Bed elevation
27 
28   } Vertex;
29 
30   typedef struct
31   {
32     size_t startVertex;
33     size_t endVertex;
34   } Edge;
35 
36   typedef std::vector<size_t> Face;
37   typedef std::vector<Vertex> Vertices;
38   typedef std::vector<Edge> Edges;
39   typedef std::vector<Face> Faces;
40 
41   /**
42    * The MemoryDataset stores all the data in the memory
43    */
44   class MemoryDataset2D: public Dataset2D
45   {
46     public:
47       MemoryDataset2D( DatasetGroup *grp, bool hasActiveFlag = false );
48       ~MemoryDataset2D() override;
49 
50       size_t scalarData( size_t indexStart, size_t count, double *buffer ) override;
51       size_t vectorData( size_t indexStart, size_t count, double *buffer ) override;
52 
53       //! Returns 0 for datasets that does not support active flags
54       size_t activeData( size_t indexStart, size_t count, int *buffer ) override;
55 
56       /**
57        * Loop through all faces and activate those which has all 4 values on vertices valid
58        * Dataset must support active flags and be defined on vertices
59        */
60       void activateFaces( MDAL::MemoryMesh *mesh );
61 
62       /**
63        * Sets active flag for index
64        *
65        * \param stat 1 for active, 0 for non-active
66        * \param index index of the flag
67        *
68        * Dataset must support active flags
69        */
setActive(size_t index,int stat)70       void setActive( size_t index, int stat )
71       {
72         assert( supportsActiveFlag() );
73         assert( mActive.size() > index );
74         mActive[index] = stat;
75       }
76 
77       void setActive( const int *activeBuffer );
78 
active(size_t index) const79       int active( size_t index ) const
80       {
81         assert( supportsActiveFlag() );
82         assert( mActive.size() > index );
83         return mActive[index];
84       }
85 
setScalarValue(size_t index,double value)86       void setScalarValue( size_t index, double value )
87       {
88         assert( mValues.size() > index );
89         assert( group()->isScalar() );
90         mValues[index] = value;
91       }
92 
setVectorValue(size_t index,double x,double y)93       void setVectorValue( size_t index, double x, double y )
94       {
95         assert( mValues.size() > 2 * index + 1 );
96         assert( !group()->isScalar() );
97         mValues[2 * index] = x;
98         mValues[2 * index + 1] = y;
99       }
100 
setValueX(size_t index,double x)101       void setValueX( size_t index, double x )
102       {
103         assert( mValues.size() > 2 * index );
104         assert( !group()->isScalar() );
105 
106         mValues[2 * index] = x;
107       }
108 
setValueY(size_t index,double x)109       void setValueY( size_t index, double x )
110       {
111         assert( mValues.size() > 2 * index + 1 );
112         assert( !group()->isScalar() );
113         mValues[2 * index + 1] = x;
114       }
115 
valueX(size_t index) const116       double valueX( size_t index ) const
117       {
118         assert( mValues.size() > 2 * index + 1 );
119         assert( !group()->isScalar() );
120         return mValues[2 * index];
121       }
122 
valueY(size_t index) const123       double valueY( size_t index ) const
124       {
125         assert( mValues.size() > 2 * index + 1 );
126         assert( !group()->isScalar() );
127         return mValues[2 * index + 1];
128       }
129 
scalarValue(size_t index) const130       double scalarValue( size_t index ) const
131       {
132         assert( mValues.size() > index );
133         assert( group()->isScalar() );
134         return mValues[index];
135       }
136 
137       //! Returns pointer to internal buffer with values
138       //! Never null, already allocated
139       //! for vector datasets in form x1, y1, ..., xN, yN
values()140       double *values()
141       {
142         return mValues.data();
143       }
144 
145     private:
146       /**
147        * Stores vector2d/scalar data for dataset in form
148        * scalars: x1, x2, x3, ..., xN
149        * vector2D: x1, y1, x2, y2, x3, y3, .... , xN, yN
150        *
151        * all values are initialized to std::numerical_limits<double>::quiet_NaN ( == NODATA )
152        *
153        * size:
154        *   - face count if isOnFaces & isScalar
155        *   - vertex count if isOnVertices & isScalar
156        *   - face count * 2 if isOnFaces & isVector
157        *   - vertex count * 2 if isOnVertices & isVector
158        */
159       std::vector<double> mValues;
160 
161       /**
162        * Active flag, whether the face is active or not (disabled)
163        * Only make sense for dataset defined on vertices
164        * For dataset defined on faces, this is empty vector
165        *
166        * Values are initialized by default to 1 (active)
167        */
168       std::vector<int> mActive;
169   };
170 
171   class MemoryMesh: public Mesh
172   {
173     public:
174       //! Constructs an empty mesh
175       MemoryMesh( const std::string &driverName,
176                   size_t faceVerticesMaximumCount,
177                   const std::string &uri );
178 
179       ~MemoryMesh() override;
180 
181       std::unique_ptr<MDAL::MeshVertexIterator> readVertices() override;
182       std::unique_ptr<MDAL::MeshEdgeIterator> readEdges() override;
183       std::unique_ptr<MDAL::MeshFaceIterator> readFaces() override;
184 
vertices() const185       const Vertices &vertices() const {return mVertices;}
faces() const186       const Faces &faces() const {return mFaces;}
edges() const187       const Edges &edges() const {return mEdges;}
188 
189       //! Sets all vertices using std::move if possible
190       void setVertices( Vertices vertices );
191 
192       //! Sets all faces using std::move if possible
193       void setFaces( Faces faces );
194 
195       //! Sets all edges using std::move if possible
196       void setEdges( Edges edges );
197 
verticesCount() const198       size_t verticesCount() const override {return mVertices.size();}
edgesCount() const199       size_t edgesCount() const override {return mEdges.size();}
facesCount() const200       size_t facesCount() const override {return mFaces.size();}
201       BBox extent() const override;
202       void addVertices( size_t vertexCount, double *coordinates ) override;
203       void addFaces( size_t faceCount, size_t driverMaxVerticesPerFace, int *faceSizes, int *vertexIndices ) override;
204 
isEditable() const205       bool isEditable() const override {return true;}
206 
207     private:
208       BBox mExtent;
209       Vertices mVertices;
210       Faces mFaces;
211       Edges mEdges;
212   };
213 
214   class MemoryMeshVertexIterator: public MeshVertexIterator
215   {
216     public:
217       MemoryMeshVertexIterator( const MemoryMesh *mesh );
218       ~MemoryMeshVertexIterator() override;
219 
220       size_t next( size_t vertexCount, double *coordinates ) override;
221 
222       const MemoryMesh *mMemoryMesh;
223       size_t mLastVertexIndex = 0;
224   };
225 
226   class MemoryMeshEdgeIterator: public MeshEdgeIterator
227   {
228     public:
229       MemoryMeshEdgeIterator( const MemoryMesh *mesh );
230       ~MemoryMeshEdgeIterator() override;
231 
232       size_t next( size_t edgeCount,
233                    int *startVertexIndices,
234                    int *endVertexIndices ) override;
235 
236       const MemoryMesh *mMemoryMesh;
237       size_t mLastEdgeIndex = 0;
238   };
239 
240   class MemoryMeshFaceIterator: public MeshFaceIterator
241   {
242     public:
243       MemoryMeshFaceIterator( const MemoryMesh *mesh );
244       ~MemoryMeshFaceIterator() override;
245 
246       size_t next( size_t faceOffsetsBufferLen,
247                    int *faceOffsetsBuffer,
248                    size_t vertexIndicesBufferLen,
249                    int *vertexIndicesBuffer ) override;
250 
251       const MemoryMesh *mMemoryMesh;
252       size_t mLastFaceIndex = 0;
253   };
254 } // namespace MDAL
255 #endif //MDAL_MEMORY_DATA_MODEL_HPP
256