OpenWalnut
1.4.0
|
00001 //--------------------------------------------------------------------------- 00002 // 00003 // Project: OpenWalnut ( http://www.openwalnut.org ) 00004 // 00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS 00006 // For more information see http://www.openwalnut.org/copying 00007 // 00008 // This file is part of OpenWalnut. 00009 // 00010 // OpenWalnut is free software: you can redistribute it and/or modify 00011 // it under the terms of the GNU Lesser General Public License as published by 00012 // the Free Software Foundation, either version 3 of the License, or 00013 // (at your option) any later version. 00014 // 00015 // OpenWalnut is distributed in the hope that it will be useful, 00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 // GNU Lesser General Public License for more details. 00019 // 00020 // You should have received a copy of the GNU Lesser General Public License 00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>. 00022 // 00023 //--------------------------------------------------------------------------- 00024 00025 #include <algorithm> 00026 #include <string> 00027 00028 #include "../common/exceptions/WOutOfBounds.h" 00029 #include "../common/WAssert.h" 00030 #include "../common/WColor.h" 00031 #include "../common/math/linearAlgebra/WPosition.h" 00032 00033 #include "WDataSetPoints.h" 00034 00035 // prototype instance as singleton 00036 boost::shared_ptr< WPrototyped > WDataSetPoints::m_prototype = boost::shared_ptr< WPrototyped >(); 00037 00038 WDataSetPoints::WDataSetPoints( WDataSetPoints::VertexArray vertices, 00039 WDataSetPoints::ColorArray colors, 00040 WBoundingBox boundingBox ): 00041 m_vertices( vertices ), 00042 m_colors( colors ), 00043 m_bb( boundingBox ) 00044 { 00045 WAssert( vertices->size() % 3 == 0, "Number of floats in the vertex array must be a multiple of 3" ); 00046 if( colors ) 00047 { 00048 size_t numPoints = vertices->size() / 3; 00049 WAssert( ( colors->size() / 4 == numPoints ) || 00050 ( colors->size() / 3 == numPoints ) || 00051 ( colors->size() / 1 == numPoints ) 00052 , "Number of floats in the color array must be 1,3, or 4 per vertex" ); 00053 } 00054 00055 init(); 00056 } 00057 00058 WDataSetPoints::WDataSetPoints( WDataSetPoints::VertexArray vertices, 00059 WDataSetPoints::ColorArray colors ): 00060 m_vertices( vertices ), 00061 m_colors( colors ) 00062 { 00063 WAssert( vertices->size() % 3 == 0, "Number of floats in the vertex array must be a multiple of 3" ); 00064 if( colors ) 00065 { 00066 size_t numPoints = vertices->size() / 3; 00067 WAssert( ( colors->size() / 4 == numPoints ) || 00068 ( colors->size() / 3 == numPoints ) || 00069 ( colors->size() / 1 == numPoints ) 00070 , "Number of floats in the color array must be 1,3, or 4 per vertex" ); 00071 } 00072 00073 init( true ); 00074 } 00075 00076 WDataSetPoints::WDataSetPoints() 00077 { 00078 // dummy construction. Empty point and color list 00079 } 00080 00081 WDataSetPoints::~WDataSetPoints() 00082 { 00083 // cleanup 00084 } 00085 00086 void WDataSetPoints::init( bool calcBB ) 00087 { 00088 // no colors specified? Use white as default 00089 if( !m_colors ) 00090 { 00091 // Store 1 value for each point (gray scale colors) 00092 m_colors = ColorArray( new ColorArray::element_type( m_vertices->size() / 3, 1.0 ) ); 00093 } 00094 00095 // calculate the bounding box if needed 00096 if( calcBB && ( m_vertices->size() != 0 ) ) 00097 { 00098 float minX = m_vertices->operator[]( 0 ); 00099 float minY = m_vertices->operator[]( 1 ); 00100 float minZ = m_vertices->operator[]( 2 ); 00101 float maxX = minX; 00102 float maxY = minY; 00103 float maxZ = minZ; 00104 00105 // go through each point 00106 for( size_t pointIdx = 3; pointIdx < m_vertices->size(); pointIdx+=3 ) 00107 { 00108 minX = std::min( m_vertices->operator[]( pointIdx + 0 ), minX ); 00109 minY = std::min( m_vertices->operator[]( pointIdx + 1 ), minY ); 00110 minZ = std::min( m_vertices->operator[]( pointIdx + 2 ), minZ ); 00111 maxX = std::max( m_vertices->operator[]( pointIdx + 0 ), maxX ); 00112 maxY = std::max( m_vertices->operator[]( pointIdx + 1 ), maxY ); 00113 maxZ = std::max( m_vertices->operator[]( pointIdx + 2 ), maxZ ); 00114 } 00115 00116 m_bb = WBoundingBox( minX, minY, minZ, maxX, maxY, maxZ ); 00117 } 00118 00119 // which colortype do we have? 00120 m_colorType = static_cast< ColorType >( m_colors->size() / ( m_vertices->size() / 3 ) ); 00121 } 00122 00123 size_t WDataSetPoints::size() const 00124 { 00125 return m_vertices->size() / 3; 00126 } 00127 00128 bool WDataSetPoints::isTexture() const 00129 { 00130 return false; 00131 } 00132 00133 const std::string WDataSetPoints::getName() const 00134 { 00135 return "WDataSetPoints"; 00136 } 00137 00138 const std::string WDataSetPoints::getDescription() const 00139 { 00140 return "Dataset which contains points without any topological relation."; 00141 } 00142 00143 boost::shared_ptr< WPrototyped > WDataSetPoints::getPrototype() 00144 { 00145 if( !m_prototype ) 00146 { 00147 m_prototype = boost::shared_ptr< WPrototyped >( new WDataSetPoints() ); 00148 } 00149 00150 return m_prototype; 00151 } 00152 00153 WDataSetPoints::VertexArray WDataSetPoints::getVertices() const 00154 { 00155 return m_vertices; 00156 } 00157 00158 WDataSetPoints::ColorArray WDataSetPoints::getColors() const 00159 { 00160 return m_colors; 00161 } 00162 00163 WBoundingBox WDataSetPoints::getBoundingBox() const 00164 { 00165 return m_bb; 00166 } 00167 00168 WPosition WDataSetPoints::operator[]( const size_t pointIdx ) const 00169 { 00170 if( !isValidPointIdx( pointIdx ) ) 00171 { 00172 throw WOutOfBounds( "The specified index is invalid." ); 00173 } 00174 00175 return WPosition( m_vertices->operator[]( pointIdx * 3 + 0 ), 00176 m_vertices->operator[]( pointIdx * 3 + 1 ), 00177 m_vertices->operator[]( pointIdx * 3 + 2 ) ); 00178 } 00179 00180 WPosition WDataSetPoints::getPosition( const size_t pointIdx ) const 00181 { 00182 return operator[]( pointIdx ); 00183 } 00184 00185 WColor WDataSetPoints::getColor( const size_t pointIdx ) const 00186 { 00187 if( !isValidPointIdx( pointIdx ) ) 00188 { 00189 throw WOutOfBounds( "The specified index is invalid." ); 00190 } 00191 00192 switch( getColorType() ) 00193 { 00194 case GRAY: 00195 return WColor( m_colors->operator[]( pointIdx * 1 + 0 ), 00196 m_colors->operator[]( pointIdx * 1 + 0 ), 00197 m_colors->operator[]( pointIdx * 1 + 0 ), 00198 1.0 ); 00199 case RGB: 00200 return WColor( m_colors->operator[]( pointIdx * 3 + 0 ), 00201 m_colors->operator[]( pointIdx * 3 + 1 ), 00202 m_colors->operator[]( pointIdx * 3 + 2 ), 00203 1.0 ); 00204 case RGBA: 00205 return WColor( m_colors->operator[]( pointIdx * 4 + 0 ), 00206 m_colors->operator[]( pointIdx * 4 + 1 ), 00207 m_colors->operator[]( pointIdx * 4 + 2 ), 00208 m_colors->operator[]( pointIdx * 4 + 3 ) ); 00209 default: 00210 return WColor( 1.0, 1.0, 1.0, 1.0 ); 00211 } 00212 } 00213 00214 bool WDataSetPoints::isValidPointIdx( const size_t pointIdx ) const 00215 { 00216 return ( pointIdx < m_vertices->size() / 3 ); 00217 } 00218 00219 WDataSetPoints::ColorType WDataSetPoints::getColorType() const 00220 { 00221 return m_colorType; 00222 }