static int initialize_convex_hull(int num_points, const double (*points)[3], int8_t facets[][3], double plane_normal[][3], bool* processed, int* initial_vertices, double* barycentre) { memset(processed, 0, 15 * sizeof(bool)); memset(barycentre, 0, 3 * sizeof(double)); int ret = initial_simplex(num_points, points, initial_vertices); if (ret != 0) return ret; for (int i = 0;i<4;i++) { int a = initial_vertices[i]; processed[a] = true; barycentre[0] += points[a][0]; barycentre[1] += points[a][1]; barycentre[2] += points[a][2]; } barycentre[0] /= 4; barycentre[1] /= 4; barycentre[2] /= 4; add_facet(points, initial_vertices[0], initial_vertices[1], initial_vertices[2], facets[0], plane_normal[0], barycentre); add_facet(points, initial_vertices[0], initial_vertices[1], initial_vertices[3], facets[1], plane_normal[1], barycentre); add_facet(points, initial_vertices[0], initial_vertices[2], initial_vertices[3], facets[2], plane_normal[2], barycentre); add_facet(points, initial_vertices[1], initial_vertices[2], initial_vertices[3], facets[3], plane_normal[3], barycentre); return 0; }
Benchmark::Benchmark (const char* name, TestFacet& facet) : name_ (name), facets_ (name) { add_facet (&facet, true); }
int DXF::face3dEndFunc(void) { if(add_facet()) return -1; curLayer = (Layer *)0; curFacet[0][0] = curFacet[0][1] = curFacet[0][2] = -1E9f; curFacet[1][0] = curFacet[1][1] = curFacet[1][2] = -1E9f; curFacet[2][0] = curFacet[2][1] = curFacet[2][2] = -1E9f; curFacet[3][0] = curFacet[3][1] = curFacet[3][2] = -1E9f; return 0; } // end of face3dEndFunc
bool TMesh::ReadOffFile(char *filename) { string str(filename); OffFileReader reader(str); if(reader.bad) return false; OffObj obj; if( reader.get_next_off_object(obj) ) { for(unsigned int i = 0; i < obj.vertices.size(); i ++) { add_vertex( VTMesh( obj.vertices[i].x, obj.vertices[i].y, obj.vertices[i].z) ); } for(unsigned int i = 0; i < obj.facets.size(); i ++) { if(obj.facets[i].size() != 3) { cerr<<"Error: invalid triangle mesh."<<endl; return false; } unsigned int fid = add_facet( FTMesh( (obj.facets[i])[0], (obj.facets[i])[1], (obj.facets[i])[2]) ); assert(fid == i); } //get the bounding box of mesh GetBBox(); //Generate the topology for tmesh GenerateMeshTopo(); //Mark the non_manifoldness MarkNonManifoldness(); //debug //PrintMeshTopo(); //getchar(); return true; } else { return false; } }
bool TMesh::ReadOffFile(char *filename, bool wcolor) { ifstream fin_temp; fin_temp.open(filename); if( !fin_temp.good() ) { return false; } ofstream fout_temp; fout_temp.open("temp.m"); if(!fout_temp.good()) { cerr<<"Failed to open file temp.m"<<endl; return false; } // Read the off file and skip the comments. // Write the comment-less off file to a file, called "temp.m". while(! fin_temp.eof()) { char line[90]; fin_temp.getline(line, 90); if(line[0] == 'O' || line[0] == '#') ; else fout_temp << line << endl; } fin_temp.close(); fout_temp.close(); FILE *fp; if( (fp = fopen("temp.m", "r")) == NULL ) { cerr<<"Failed to open file temp.m"<<endl; return false; } unsigned int n_ver, n_facet, n_edge; fscanf(fp, "%d %d %d", &n_ver, &n_facet, &n_edge); // cerr<<"n_ver: "<<n_ver<<" n_facet: "<<n_facet<<endl; float x, y, z; //vertex information for(unsigned int i = 0; i < n_ver; i ++) { fscanf(fp, "%f %f %f", &x, &y, &z); //cerr<<"x y z"<<x<<" "<<y<<" "<<z<<endl; //VTMesh v(x, y, z); add_vertex( VTMesh(x, y, z) ); } //cout<<"vertex done"<<endl; //facet information int nv, vid0, vid1, vid2; float r, g, b, a; for(unsigned int i = 0; i < n_facet; i ++) { fscanf(fp, "%d %d %d %d", &nv, &vid0, &vid1, &vid2); if(wcolor) fscanf(fp, "%f %f %f %f", &r, &g, &b, &a); unsigned int fid = add_facet( FTMesh(vid0, vid1, vid2) ); assert(fid == i); } //cerr<<"facet done"<<endl; assert(v_count() == n_ver); assert(f_count() == n_facet); //get the bounding box of mesh GetBBox(); //Delete the temp.m file system("rm temp.m"); //Generate the topology for tmesh GenerateMeshTopo(); //Mark the non_manifoldness MarkNonManifoldness(); //debug // PrintMeshTopo(); // getchar(); return true; }
int get_convex_hull(int num_points, const double (*points)[3], int num_expected_facets, convexhull_t* ch, int8_t simplex[][3]) { assert(num_points == 7 || num_points == 13 || num_points == 15); int ret = 0; int num_prev = ch->num_prev; ch->num_prev = num_points; if (!ch->ok || 0) { ret = initialize_convex_hull(num_points, points, ch->facets, ch->plane_normal, ch->processed, ch->initial_vertices, ch->barycentre); if (ret != 0) return ret; ch->num_facets = 4; num_prev = 0; } for (int i = num_prev;i<num_points;i++) { if (ch->processed[i]) continue; ch->processed[i] = true; int num_to_add = 0; int8_t to_add[MAXF][3]; int8_t edge_visible[15][15]; memset(edge_visible, 0, sizeof(int8_t) * 15 * 15); for (int j = 0;j<ch->num_facets;j++) { int a = ch->facets[j][0]; int b = ch->facets[j][1]; int c = ch->facets[j][2]; int u = 0, v = 0, w = 0; double distance = point_plane_distance(points[i], points[a], ch->plane_normal[j]); bool vis = distance > TOLERANCE; if (vis) { u = edge_visible[a][b] |= VISIBLE; edge_visible[b][a] |= VISIBLE; v = edge_visible[b][c] |= VISIBLE; edge_visible[c][b] |= VISIBLE; w = edge_visible[c][a] |= VISIBLE; edge_visible[a][c] |= VISIBLE; memcpy(ch->facets[j], ch->facets[ch->num_facets-1], 3 * sizeof(int8_t)); memcpy(ch->plane_normal[j], ch->plane_normal[ch->num_facets-1], 3 * sizeof(double)); ch->num_facets--; j--; } else { u = edge_visible[a][b] |= INVISIBLE; edge_visible[b][a] |= INVISIBLE; v = edge_visible[b][c] |= INVISIBLE; edge_visible[c][b] |= INVISIBLE; w = edge_visible[c][a] |= INVISIBLE; edge_visible[a][c] |= INVISIBLE; } if (u == BOTH) { to_add[num_to_add][0] = i; to_add[num_to_add][1] = a; to_add[num_to_add][2] = b; num_to_add++; } if (v == BOTH) { to_add[num_to_add][0] = i; to_add[num_to_add][1] = b; to_add[num_to_add][2] = c; num_to_add++; } if (w == BOTH) { to_add[num_to_add][0] = i; to_add[num_to_add][1] = c; to_add[num_to_add][2] = a; num_to_add++; } } for (int j = 0;j<num_to_add;j++) { if (ch->num_facets >= MAXF) return -4; add_facet(points, to_add[j][0], to_add[j][1], to_add[j][2], ch->facets[ch->num_facets], ch->plane_normal[ch->num_facets], ch->barycentre); ch->num_facets++; } } if (ch->num_facets != num_expected_facets) return -5; //incorrect number of facets in convex hull for (int i=0;i<ch->num_facets;i++) { int a = ch->facets[i][0]; int b = ch->facets[i][1]; int c = ch->facets[i][2]; if (a == 0 || b == 0 || c == 0) return -6; //central atom contained in convex hull simplex[i][0] = a - 1; simplex[i][1] = b - 1; simplex[i][2] = c - 1; } return ret; }
void Build_SphericalSurface::operator()(Polyhedron::HalfedgeDS& hds) { Builder B(hds, true); B.begin_surface(0, 0, 0); // north pole add_vertex(B, 0, 0, _f(0, 0)); // add vertices between poles double delta = M_PI / (2 * _steps); for (int theta = 1; theta < 2 * _steps; theta++) { double _theta = theta * delta; double costheta = cos(_theta); double sintheta = sin(_theta); for (int phi = 0; phi < 4 * _steps; phi++) { double _phi = phi * delta; double _radius = _f(_theta, _phi); double st = _radius * sintheta; double x = st * cos(_phi); double y = st * sin(_phi); double z = _radius * costheta; add_vertex(B, x, y, z); } } // south pole add_vertex(B, 0, 0, -_f(M_PI, 0)); // triangles around the north pole debug(LOG_DEBUG, DEBUG_LOG, 0, "triangles around north pole"); for (int i = 1; i <= 4 * _steps - 1; i++) { add_facet(B, 0, i, i + 1); } add_facet(B, 0, 4 * _steps, 1); // triangles around the south pole debug(LOG_DEBUG, DEBUG_LOG, 0, "triangles around south pole"); for (int i = 1; i <= 4 * _steps - 1; i++) { add_facet(B, vertexnumber() - 1, vertexnumber() - i - 1, vertexnumber() - i - 2); } add_facet(B, vertexnumber() - 1, vertexnumber() - 4 * _steps - 1, vertexnumber() - 2); // triangles in strips for (int theta = 1; theta < 2 * _steps - 1; theta++) { debug(LOG_DEBUG, DEBUG_LOG, 0, "zone theta = %d", theta); int t = 4 * (theta - 1) * _steps + 1; for (int phi = 0; phi < 4 * _steps - 1; phi++) { add_facet(B, t + phi, t + phi + 4 * _steps, t + phi + 1); add_facet(B, t + phi + 1, t + phi + 4 * _steps, t + phi + 4 * _steps + 1); } add_facet(B, t, t + 4 * _steps - 1, t + 8 * _steps - 1); add_facet(B, t, t + 8 * _steps - 1, t + 4 * _steps); } B.end_surface(); }