void initialize( float width, float height, int segmentsWidth, int segmentsHeight ) { const auto width_half = width / 2, height_half = height / 2; const auto gridX = segmentsWidth, gridZ = segmentsHeight; const auto gridX1 = gridX + 1, gridZ1 = gridZ + 1; const auto segment_width = width / gridX, segment_height = height / gridZ; Vector3 normal( 0, 0, 1 ); for ( int iz = 0; iz < gridZ1; iz ++ ) { for ( int ix = 0; ix < gridX1; ix ++ ) { const auto x = (float)ix * segment_width - width_half; const auto y = (float)iz * segment_height - height_half; vertices.push_back( Vector3( x, -y, 0 ) ); } } for ( int iz = 0; iz < gridZ; iz ++ ) { for ( int ix = 0; ix < gridX; ix ++ ) { const auto a = ix + gridX1 * iz; const auto b = ix + gridX1 * ( iz + 1 ); const auto c = ( ix + 1 ) + gridX1 * ( iz + 1 ); const auto d = ( ix + 1 ) + gridX1 * iz; Face4 face( a, b, c, d ); face.normal.copy( normal ); face.vertexNormals.fill( normal ); faces.push_back( face ); std::array<UV, 4> uvs = { UV( (float)ix / gridX, 1.f - iz / gridZ ), UV( (float)ix / gridX, 1.f - ( iz + 1 ) / gridZ ), UV( ( (float)ix + 1 ) / gridX, 1.f - ( iz + 1 ) / gridZ ), UV( ( (float)ix + 1 ) / gridX, 1.f - iz / gridZ ) }; faceVertexUvs[ 0 ].push_back( std::move( uvs ) ); } } computeCentroids(); }
bool DataCluster::doKMeansClustering(int k) { init(k); float change = 100000; while (change > 0){ //we can adjust the terminal condition for (int i = 0; i <clusterNum; ++i) groups[i].clear(); for (int i = 0; i < raw.size(); ++i) { int n = minDistanceCentroid(raw[i]); groups[n].push_back(raw[i]); } change = computeCentroids(); } return true; }
vector<vector<int> > kmeansModule::clusterData(float** data, int M, vector<int>& pts) { clusterMemberships = vector<int>(pts.size(),-1); for (int i = 1; i < maxIt; i++) { bool change = computeClustMemberships(data, M, pts); computeCentroids(data, M, pts); if (!change) break; } //check and fix degenerate clusters handleDegenerateClusters(); return getClusters(data, M, pts); }
void Fuzzy::initRandom () { // // メンバーシップをランダム初期化 // std::cout << "C-means Random Initialization" << std::endl; srand ((unsigned int) time (NULL)); float normalization_factor; for (int j = 0 ; j < number_points_; j++){ normalization_factor = 0.0; for (int i = 0; i < number_clusters_; i++) normalization_factor += membership_.at<float> (j, i) = (rand () / (RAND_MAX + 0.0)); // normalize for (int i = 0; i < number_clusters_; i++) membership_.at<float> (j, i) /= normalization_factor; } // centroids算出 computeCentroids(); }
void SphereGeometry::initialize( float radius, float segmentsWidth, float segmentsHeight, float phiStart, float phiLength, float thetaStart, float thetaLength ) { const auto segmentsX = Math::max( 3, ( int )Math::floor( segmentsWidth ) ); const auto segmentsY = Math::max( 2, ( int )Math::floor( segmentsHeight ) ); std::vector<std::vector<int>> indices; std::vector<std::vector<Vector2>> uvs; for ( int y = 0; y <= segmentsY; y ++ ) { std::vector<int> indicesRow; std::vector<Vector2> uvsRow; for ( int x = 0; x <= segmentsX; x ++ ) { const auto u = ( float )x / segmentsX; const auto v = ( float )y / segmentsY; Vertex vertex; vertex.x = - radius * Math::cos( phiStart + u * phiLength ) * Math::sin( thetaStart + v * thetaLength ); vertex.y = radius * Math::cos( thetaStart + v * thetaLength ); vertex.z = radius * Math::sin( phiStart + u * phiLength ) * Math::sin( thetaStart + v * thetaLength ); vertices.push_back( vertex ); indicesRow.push_back( ( int )vertices.size() - 1 ); uvsRow.push_back( Vector2( u, 1 - v ) ); } indices.push_back( indicesRow ); uvs.push_back( uvsRow ); } for ( int y = 0; y < segmentsY; y ++ ) { for ( int x = 0; x < segmentsX; x ++ ) { const auto v1 = indices[ y ][ x + 1 ]; const auto v2 = indices[ y ][ x ]; const auto v3 = indices[ y + 1 ][ x ]; const auto v4 = indices[ y + 1 ][ x + 1 ]; const auto n1 = vertices[ v1 ].clone().normalize(); const auto n2 = vertices[ v2 ].clone().normalize(); const auto n3 = vertices[ v3 ].clone().normalize(); const auto n4 = vertices[ v4 ].clone().normalize(); const auto& uv1 = uvs[ y ][ x + 1 ]; const auto& uv2 = uvs[ y ][ x ]; const auto& uv3 = uvs[ y + 1 ][ x ]; const auto& uv4 = uvs[ y + 1 ][ x + 1 ]; if ( Math::abs( vertices[ v1 ].y ) == radius ) { faces.push_back( Face( v1, v3, v4, n1, n3, n4 ) ); faceVertexUvs[ 0 ].push_back( toArray( uv1, uv3, uv4 ) ); } else if ( Math::abs( vertices[ v3 ].y ) == radius ) { faces.push_back( Face( v1, v2, v3, n1, n2, n3 ) ); faceVertexUvs[ 0 ].push_back( toArray( uv1, uv2, uv3 ) ); } else { faces.push_back( Face( v1, v2, v4, n1, n2, n4 ) ); faceVertexUvs[ 0 ].push_back( toArray( uv1, uv2, uv4 ) ); faces.push_back( Face( v2, v3, v4, n2.clone(), n3, n4.clone() ) ); faceVertexUvs[ 0 ].push_back( toArray( uv2.clone(), uv3, uv4.clone() ) ); } } } computeCentroids(); computeFaceNormals(); boundingSphere = Sphere( Vector3(), radius); }