OpenWalnut  1.5.0dev
WDataSetPoints.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <algorithm>
26 #include <memory>
27 #include <string>
28 
29 #include "../common/WAssert.h"
30 #include "../common/WColor.h"
31 #include "../common/exceptions/WOutOfBounds.h"
32 #include "../common/math/linearAlgebra/WPosition.h"
33 #include "WDataSetPoints.h"
34 
35 // prototype instance as singleton
36 std::shared_ptr< WPrototyped > WDataSetPoints::m_prototype = std::shared_ptr< WPrototyped >();
37 
40  WBoundingBox boundingBox ):
41  m_vertices( vertices ),
42  m_colors( colors ),
43  m_bb( boundingBox )
44 {
45  WAssert( vertices->size() % 3 == 0, "Number of floats in the vertex array must be a multiple of 3" );
46  if( colors )
47  {
48  size_t numPoints = vertices->size() / 3;
49  WAssert( ( colors->size() / 4 == numPoints ) ||
50  ( colors->size() / 3 == numPoints ) ||
51  ( colors->size() / 1 == numPoints )
52  , "Number of floats in the color array must be 1,3, or 4 per vertex" );
53  }
54 
55  init();
56 }
57 
60  std::any data ):
61  m_vertices( vertices ),
62  m_colors( colors ),
63  m_data( data )
64 {
65  WAssert( vertices->size() % 3 == 0, "Number of floats in the vertex array must be a multiple of 3" );
66  if( colors )
67  {
68  size_t numPoints = vertices->size() / 3;
69  WAssert( ( colors->size() / 4 == numPoints ) ||
70  ( colors->size() / 3 == numPoints ) ||
71  ( colors->size() / 1 == numPoints )
72  , "Number of floats in the color array must be 1,3, or 4 per vertex" );
73  }
74 
75  init( true );
76 }
77 
79 {
80  // dummy construction. Empty point and color list
81 }
82 
84 {
85  // cleanup
86 }
87 
88 void WDataSetPoints::init( bool calcBB )
89 {
90  // no colors specified? Use white as default
91  if( !m_colors )
92  {
93  // Store 1 value for each point (gray scale colors)
94  m_colors = ColorArray( new ColorArray::element_type( m_vertices->size() / 3, 1.0 ) );
95  m_colorType = GRAY;
96  }
97 
98  // which colortype do we have?
99  if( m_vertices->size() > 0 )
100  {
101  m_colorType = static_cast< ColorType >( m_colors->size() / ( m_vertices->size() / 3 ) );
102  }
103  else
104  {
105  // Ensure a defined value.
106  m_colorType = GRAY;
107  }
108 
109  // calculate the bounding box if needed
110  if( calcBB && ( m_vertices->size() != 0 ) )
111  {
112  float minX = m_vertices->operator[]( 0 );
113  float minY = m_vertices->operator[]( 1 );
114  float minZ = m_vertices->operator[]( 2 );
115  float maxX = minX;
116  float maxY = minY;
117  float maxZ = minZ;
118 
119  // go through each point
120  for( size_t pointIdx = 3; pointIdx < m_vertices->size(); pointIdx+=3 )
121  {
122  minX = std::min( m_vertices->operator[]( pointIdx + 0 ), minX );
123  minY = std::min( m_vertices->operator[]( pointIdx + 1 ), minY );
124  minZ = std::min( m_vertices->operator[]( pointIdx + 2 ), minZ );
125  maxX = std::max( m_vertices->operator[]( pointIdx + 0 ), maxX );
126  maxY = std::max( m_vertices->operator[]( pointIdx + 1 ), maxY );
127  maxZ = std::max( m_vertices->operator[]( pointIdx + 2 ), maxZ );
128  }
129 
130  m_bb = WBoundingBox( minX, minY, minZ, maxX, maxY, maxZ );
131  }
132 }
133 
134 size_t WDataSetPoints::size() const
135 {
136  return m_vertices->size() / 3;
137 }
138 
140 {
141  return false;
142 }
143 
144 const std::string WDataSetPoints::getName() const
145 {
146  return "WDataSetPoints";
147 }
148 
149 const std::string WDataSetPoints::getDescription() const
150 {
151  return "Dataset which contains points without any topological relation.";
152 }
153 
154 std::shared_ptr< WPrototyped > WDataSetPoints::getPrototype()
155 {
156  if( !m_prototype )
157  {
158  m_prototype = std::shared_ptr< WPrototyped >( new WDataSetPoints() );
159  }
160 
161  return m_prototype;
162 }
163 
165 {
166  return m_vertices;
167 }
168 
170 {
171  return m_colors;
172 }
173 
174 std::any WDataSetPoints::getData() const
175 {
176  return m_data;
177 }
178 
180 {
181  return m_bb;
182 }
183 
184 WPosition WDataSetPoints::operator[]( const size_t pointIdx ) const
185 {
186  if( !isValidPointIdx( pointIdx ) )
187  {
188  throw WOutOfBounds( "The specified index is invalid." );
189  }
190 
191  return WPosition( m_vertices->operator[]( pointIdx * 3 + 0 ),
192  m_vertices->operator[]( pointIdx * 3 + 1 ),
193  m_vertices->operator[]( pointIdx * 3 + 2 ) );
194 }
195 
196 WPosition WDataSetPoints::getPosition( const size_t pointIdx ) const
197 {
198  return operator[]( pointIdx );
199 }
200 
201 WColor WDataSetPoints::getColor( const size_t pointIdx ) const
202 {
203  if( !isValidPointIdx( pointIdx ) )
204  {
205  throw WOutOfBounds( "The specified index is invalid." );
206  }
207 
208  switch( getColorType() )
209  {
210  case GRAY:
211  return WColor( m_colors->operator[]( pointIdx * 1 + 0 ),
212  m_colors->operator[]( pointIdx * 1 + 0 ),
213  m_colors->operator[]( pointIdx * 1 + 0 ),
214  1.0 );
215  case RGB:
216  return WColor( m_colors->operator[]( pointIdx * 3 + 0 ),
217  m_colors->operator[]( pointIdx * 3 + 1 ),
218  m_colors->operator[]( pointIdx * 3 + 2 ),
219  1.0 );
220  case RGBA:
221  return WColor( m_colors->operator[]( pointIdx * 4 + 0 ),
222  m_colors->operator[]( pointIdx * 4 + 1 ),
223  m_colors->operator[]( pointIdx * 4 + 2 ),
224  m_colors->operator[]( pointIdx * 4 + 3 ) );
225  default:
226  return WColor( 1.0, 1.0, 1.0, 1.0 );
227  }
228 }
229 
230 bool WDataSetPoints::isValidPointIdx( const size_t pointIdx ) const
231 {
232  return ( pointIdx < m_vertices->size() / 3 );
233 }
234 
236 {
237  return m_colorType;
238 }
virtual const std::string getName() const
Gets the name of this prototype.
VertexArray getVertices() const
Getter for the point vertices.
bool isValidPointIdx(const size_t pointIdx) const
Is this a valid point index?
WColor getColor(const size_t pointIdx) const
The color of a given point.
ColorArray getColors() const
Getter for the point colors.
WBoundingBox m_bb
Axis aligned bounding box for all point-vertices of this dataset.
ColorArray m_colors
An array of the colors per vertex.
std::any m_data
An optional vector for data per vertex.
size_t size() const
Get number of points in this data set.
static std::shared_ptr< WPrototyped > m_prototype
The prototype as singleton.
VertexArray m_vertices
Point vector for all points.
ColorType m_colorType
Which colortype do we use in m_colors.
virtual const std::string getDescription() const
Gets the description for this prototype.
ColorType
The type of colors we have for each point.
virtual bool isTexture() const
Determines whether this dataset can be used as a texture.
void init(bool calcBB=false)
Initialize arrays and bbox if needed.
std::shared_ptr< std::vector< float > > ColorArray
Colors for each vertex in VertexArray.
WPosition getPosition(const size_t pointIdx) const
Query coordinates of a given point.
ColorType getColorType() const
Check the type of color.
std::shared_ptr< std::vector< float > > VertexArray
List of vertex coordinates in term of components of vertices.
WPosition operator[](const size_t pointIdx) const
Query coordinates of a given point.
static std::shared_ptr< WPrototyped > getPrototype()
Returns a prototype instantiated with the true type of the deriving class.
std::any getData() const
Getter for the data set.
virtual ~WDataSetPoints()
Destructor.
WBoundingBox getBoundingBox() const
Get the bounding box.
WDataSetPoints()
Constructs a new set of points.
Indicates invalid element access of a container.
Definition: WOutOfBounds.h:37
This only is a 3d double vector.