CP_Vector3D operator * (const CP_Vector3D& p, const CMatrix4& mat) { double x, y, z; const double *mt = &mat.mat[0][0]; x = p.m_x*mt[0] + p.m_y*mt[4] + p.m_z*mt[8]; y = p.m_x*mt[1] + p.m_y*mt[5] + p.m_z*mt[9]; z = p.m_x*mt[2] + p.m_y*mt[6] + p.m_z*mt[10]; return CP_Vector3D (x, y, z); }
void InitRefPlanesByCooSys(CP_Vector3D coorSys[3], vector<vector<CP_LineSegment3D>> &vResultRefPlanes) { const double X_OFFSET_TOLER = 0.2; for (int i = 0; i < 3; ++i) { vector<CP_LineSegment3D> vRefPlane; InitPlaneByVecs(vRefPlane, &coorSys[i], &coorSys[(i+1)%3]); vResultRefPlanes.push_back(vRefPlane); } // The last reference plane is construct by Y-axis and v(1,0,0) vector<CP_LineSegment3D> vRefPlane; InitPlaneByVecs(vRefPlane, &CP_Vector3D(1, 0, 0), &coorSys[1]); vResultRefPlanes.push_back(vRefPlane); }
void genModels(int modelnum, string config) { /*srand(time(NULL)); int lastsize = 0; for(int i = 0; i < polyhedra.size(); i++) { Polygon3D* poly = polyhedra[i]; int cur_size = poly->data.size(); for(int j = 0;j < cur_size; j++) MeshPolyhedronPoints.push_back(poly->data[j]); for (int j = 1; j < cur_size-1; j++) { MeshPolyhedronIndex.push_back(lastsize + 0); MeshPolyhedronIndex.push_back(lastsize + j); MeshPolyhedronIndex.push_back(lastsize + j+1); } lastsize += cur_size; }*/ bool isConfigreadin = config.length() > 0; streambuf *defaultstream = cout.rdbuf(); vector<int> rotate_angles; vector<CP_Vector3D> translations; vector<CP_Vector3D> rotations; if(! isConfigreadin) { time_t t = time(0); char tmp[64]; strftime( tmp, sizeof(tmp), "%m-%d-%H-%M", localtime(&t) ); ofstream f(string(tmp) + "-" + to_string(modelnum) + "-rand.config"); cout << "rand config write to :" << string(tmp) + "-" + to_string(modelnum) + "-rand.config" << endl; cout.rdbuf(f.rdbuf()); double xx = 20.0 / draw_scale; double range = modelnum*xx; for(int i = 0; i < modelnum; i++) { float trans_x = (rand() % (int) 5*range) / (range); float trans_y = (rand() % (int) 5*range) / (range); float trans_z = (rand() % (int) 5*range) / (range); if(rand() & 0x1) trans_x = -trans_x; if(rand() & 0x1) trans_y = -trans_y; if(rand() & 0x1) trans_z = -trans_z; int rotate_angle = rand() % 180; float rot_x = (rand() % 100) / 100.0; float rot_y = (rand() % 100) / 100.0; float rot_z = (rand() % 100) / 100.0; if(rand() & 0x1) rot_x = -rot_x; if(rand() & 0x1) rot_y = -rot_y; if(rand() & 0x1) rot_z = -rot_z; rotate_angles.push_back(rotate_angle); translations.push_back(CP_Vector3D(trans_x, trans_y, trans_z)); rotations.push_back(CP_Vector3D(rot_x, rot_y, rot_z)); cout << rotate_angle << " " << rot_x << " " << rot_y << " " << rot_z << endl; cout << trans_x << " " << trans_y << " " << trans_z << endl; } cout.rdbuf(defaultstream); }else { ifstream fin(config.data()); string line; int i = 0; int angle; float x,y,z; while(std::getline(fin, line)) { stringstream ss; ss << line; if(i & 0x1) // tranls { ss >> x >> y >> z; translations.push_back(CP_Vector3D(x,y,z)); }else //angle rot { ss >> angle >> x >> y >> z; rotations.push_back(CP_Vector3D(x,y,z)); rotate_angles.push_back(angle); } i++; }
CP_Vector3D operator()(CP_Vector3D &point) { return CP_Vector3D(mat * point); }
static void clusterWighted(vector<CP_Vector3D> &init_centers, vector<CP_Vector3D> &normals, vector<AreaIndex> &input_areas, vector<CP_Vector3D> ¢roids, bool delete_initcenter_if_none_blongto = false) { int k = init_centers.size(); centroids = init_centers; int iterTime = 0; const int MaxIterCount = 100; const RealValueType tol = 1 * PI / 180.0; //1 degree const CP_Vector3D deleted_center = CP_Vector3D(0,0,0); while (iterTime ++ < MaxIterCount) { vector<int> cluster_types(input_areas.size()); vector<RealValueType> cluster_num(k); for (unsigned int i = 0; i < input_areas.size(); i++) { RealValueType tmp = -RealValueTypeMax; int cluster_type = -1; for (int j = 0; j < k; j++) { if(centroids[j] == deleted_center) {continue;} //cos RealValueType cos_ = normals[input_areas[i].index] * centroids[j]; if(cos_ > tmp) { tmp = cos_; cluster_type = j; } } cluster_types[i] = cluster_type;// the i th point belongs to the j th(cluster_type) centroid cluster_num[cluster_type] += input_areas[i].area; } vector<CP_Vector3D> sum(k); for (unsigned int i = 0; i < input_areas.size(); i++) sum[cluster_types[i]] += normals[input_areas[i].index] * input_areas[i].area; bool move_on = false; for (int i = 0; i < k; i++) { if (cluster_num[i] == 0) { //new center remove centroids[i] = deleted_center; continue; } CP_Vector3D new_center = sum[i] / cluster_num[i];//average new_center.mf_normalize(); //normalized RealValueType acs = centroids[i] * new_center; if(acs < cos(tol)) // center move on // cos(x1) < cos(x2):x1>x2 { move_on = true; centroids[i] = new_center; } } if(!move_on) { #ifdef PRINT_DETAILS cout << "Clustering Converged with iterate times: " << iterTime << endl; #endif break; } } if(iterTime == MaxIterCount) cout << "WARNING! Clustering hit MaxIterate times : " << MaxIterCount << endl; vector<CP_Vector3D> result; for (int i = 0; i < k; i++) { if(! (centroids[i] == deleted_center)) result.push_back(centroids[i]); else if(!delete_initcenter_if_none_blongto) //make sure the result should be k clusters,if delete_initcenter_if_none_blongto result.push_back(init_centers[i]); } centroids = result; }