OpenWalnut  1.4.0
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 <string>
27 
28 #include "../common/exceptions/WOutOfBounds.h"
29 #include "../common/WAssert.h"
30 #include "../common/WColor.h"
31 #include "../common/math/linearAlgebra/WPosition.h"
32 
33 #include "WDataSetPoints.h"
34 
35 // prototype instance as singleton
36 boost::shared_ptr< WPrototyped > WDataSetPoints::m_prototype = boost::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  m_vertices( vertices ),
61  m_colors( colors )
62 {
63  WAssert( vertices->size() % 3 == 0, "Number of floats in the vertex array must be a multiple of 3" );
64  if( colors )
65  {
66  size_t numPoints = vertices->size() / 3;
67  WAssert( ( colors->size() / 4 == numPoints ) ||
68  ( colors->size() / 3 == numPoints ) ||
69  ( colors->size() / 1 == numPoints )
70  , "Number of floats in the color array must be 1,3, or 4 per vertex" );
71  }
72 
73  init( true );
74 }
75 
77 {
78  // dummy construction. Empty point and color list
79 }
80 
82 {
83  // cleanup
84 }
85 
86 void WDataSetPoints::init( bool calcBB )
87 {
88  // no colors specified? Use white as default
89  if( !m_colors )
90  {
91  // Store 1 value for each point (gray scale colors)
92  m_colors = ColorArray( new ColorArray::element_type( m_vertices->size() / 3, 1.0 ) );
93  }
94 
95  // calculate the bounding box if needed
96  if( calcBB && ( m_vertices->size() != 0 ) )
97  {
98  float minX = m_vertices->operator[]( 0 );
99  float minY = m_vertices->operator[]( 1 );
100  float minZ = m_vertices->operator[]( 2 );
101  float maxX = minX;
102  float maxY = minY;
103  float maxZ = minZ;
104 
105  // go through each point
106  for( size_t pointIdx = 3; pointIdx < m_vertices->size(); pointIdx+=3 )
107  {
108  minX = std::min( m_vertices->operator[]( pointIdx + 0 ), minX );
109  minY = std::min( m_vertices->operator[]( pointIdx + 1 ), minY );
110  minZ = std::min( m_vertices->operator[]( pointIdx + 2 ), minZ );
111  maxX = std::max( m_vertices->operator[]( pointIdx + 0 ), maxX );
112  maxY = std::max( m_vertices->operator[]( pointIdx + 1 ), maxY );
113  maxZ = std::max( m_vertices->operator[]( pointIdx + 2 ), maxZ );
114  }
115 
116  m_bb = WBoundingBox( minX, minY, minZ, maxX, maxY, maxZ );
117  }
118 
119  // which colortype do we have?
120  m_colorType = static_cast< ColorType >( m_colors->size() / ( m_vertices->size() / 3 ) );
121 }
122 
123 size_t WDataSetPoints::size() const
124 {
125  return m_vertices->size() / 3;
126 }
127 
129 {
130  return false;
131 }
132 
133 const std::string WDataSetPoints::getName() const
134 {
135  return "WDataSetPoints";
136 }
137 
138 const std::string WDataSetPoints::getDescription() const
139 {
140  return "Dataset which contains points without any topological relation.";
141 }
142 
143 boost::shared_ptr< WPrototyped > WDataSetPoints::getPrototype()
144 {
145  if( !m_prototype )
146  {
147  m_prototype = boost::shared_ptr< WPrototyped >( new WDataSetPoints() );
148  }
149 
150  return m_prototype;
151 }
152 
154 {
155  return m_vertices;
156 }
157 
159 {
160  return m_colors;
161 }
162 
164 {
165  return m_bb;
166 }
167 
168 WPosition WDataSetPoints::operator[]( const size_t pointIdx ) const
169 {
170  if( !isValidPointIdx( pointIdx ) )
171  {
172  throw WOutOfBounds( "The specified index is invalid." );
173  }
174 
175  return WPosition( m_vertices->operator[]( pointIdx * 3 + 0 ),
176  m_vertices->operator[]( pointIdx * 3 + 1 ),
177  m_vertices->operator[]( pointIdx * 3 + 2 ) );
178 }
179 
180 WPosition WDataSetPoints::getPosition( const size_t pointIdx ) const
181 {
182  return operator[]( pointIdx );
183 }
184 
185 WColor WDataSetPoints::getColor( const size_t pointIdx ) const
186 {
187  if( !isValidPointIdx( pointIdx ) )
188  {
189  throw WOutOfBounds( "The specified index is invalid." );
190  }
191 
192  switch( getColorType() )
193  {
194  case GRAY:
195  return WColor( m_colors->operator[]( pointIdx * 1 + 0 ),
196  m_colors->operator[]( pointIdx * 1 + 0 ),
197  m_colors->operator[]( pointIdx * 1 + 0 ),
198  1.0 );
199  case RGB:
200  return WColor( m_colors->operator[]( pointIdx * 3 + 0 ),
201  m_colors->operator[]( pointIdx * 3 + 1 ),
202  m_colors->operator[]( pointIdx * 3 + 2 ),
203  1.0 );
204  case RGBA:
205  return WColor( m_colors->operator[]( pointIdx * 4 + 0 ),
206  m_colors->operator[]( pointIdx * 4 + 1 ),
207  m_colors->operator[]( pointIdx * 4 + 2 ),
208  m_colors->operator[]( pointIdx * 4 + 3 ) );
209  default:
210  return WColor( 1.0, 1.0, 1.0, 1.0 );
211  }
212 }
213 
214 bool WDataSetPoints::isValidPointIdx( const size_t pointIdx ) const
215 {
216  return ( pointIdx < m_vertices->size() / 3 );
217 }
218 
220 {
221  return m_colorType;
222 }
ColorArray getColors() const
Getter for the point colors.
WPosition getPosition(const size_t pointIdx) const
Query coordinates of a given point.
WPosition operator[](const size_t pointIdx) const
Query coordinates of a given point.
WBoundingBox getBoundingBox() const
Get the bounding box.
virtual const std::string getDescription() const
Gets the description for this prototype.
Indicates invalid element access of a container.
Definition: WOutOfBounds.h:36
WColor getColor(const size_t pointIdx) const
The color of a given point.
This only is a 3d double vector.
VertexArray m_vertices
Point vector for all points.
static boost::shared_ptr< WPrototyped > m_prototype
The prototype as singleton.
ColorArray m_colors
An array of the colors per vertex.
bool isValidPointIdx(const size_t pointIdx) const
Is this a valid point index?
WDataSetPoints()
Constructs a new set of points.
VertexArray getVertices() const
Getter for the point vertices.
void init(bool calcBB=false)
Initialize arrays and bbox if needed.
virtual bool isTexture() const
Determines whether this dataset can be used as a texture.
virtual ~WDataSetPoints()
Destructor.
ColorType getColorType() const
Check the type of color.
virtual const std::string getName() const
Gets the name of this prototype.
WBoundingBox m_bb
Axis aligned bounding box for all point-vertices of this dataset.
ColorType m_colorType
Which colortype do we use in m_colors.
boost::shared_ptr< std::vector< float > > ColorArray
Colors for each vertex in VertexArray.
size_t size() const
Get number of points in this data set.
static boost::shared_ptr< WPrototyped > getPrototype()
Returns a prototype instantiated with the true type of the deriving class.
ColorType
The type of colors we have for each point.
boost::shared_ptr< std::vector< float > > VertexArray
List of vertex coordinates in term of components of vertices.