static int FindMaterialIndex(const RNArray<R3Material *>& materials, const char *name) { // Return material with matching name for (int i = 0; i < materials.NEntries(); i++) { R3Material *material = materials.Kth(i); if (!strcmp(name, material->Name())) return i; } // No matching material found return -1; }
R3Plane:: R3Plane(const RNArray<R3Point *>& points, RNBoolean polygon_vertices) { // Initialize plane v = R3null_vector; d = 0; // Check number of points int npoints = points.NEntries(); if (npoints < 3) { *this = R3null_plane; return; } // Compute centroid R3Point c = R3Centroid(points); // Check if points form (counter-clockwise) boundary of polygon if (polygon_vertices) { // Compute best normal for counter-clockwise array of points using newell's method const R3Point *p1 = points[npoints-1]; for (int i = 0; i < npoints; i++) { const R3Point *p2 = points[i]; v[0] += (p1->Y() - p2->Y()) * (p1->Z() + p2->Z()); v[1] += (p1->Z() - p2->Z()) * (p1->X() + p2->X()); v[2] += (p1->X() - p2->X()) * (p1->Y() + p2->Y()); p1 = p2; } // Normalize v.Normalize(); } else { // Compute principle axes R3Triad triad = R3PrincipleAxes(c, points); // Select direction of least variation v = triad[2]; } // Compute d from centroid and normal d = -(v[0]*c[0] + v[1]*c[1] + v[2]*c[2]); }
void GLUTRedraw(void) { // Set viewing transformation viewer->Camera().Load(); // Clear window glClearColor(200.0/255.0, 200.0/255.0, 200.0/255.0, 1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Set lights static GLfloat light0_position[] = { 3.0, 4.0, 5.0, 0.0 }; glLightfv(GL_LIGHT0, GL_POSITION, light0_position); static GLfloat light1_position[] = { -3.0, -2.0, -3.0, 0.0 }; glLightfv(GL_LIGHT1, GL_POSITION, light1_position); // Show all points if (show_points) { glEnable(GL_LIGHTING); static GLfloat material[4] = { 0, 0, 1, 1 }; glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, material); for (int i = 0; i < all_points.NEntries(); i++) { TestPoint *point = all_points[i]; R3Sphere(point->position, 0.01).Draw(); } } // Show nearby points if (selected_point && !nearby_points.IsEmpty()) { glEnable(GL_LIGHTING); static GLfloat material[4] = { 1, 0, 0, 1 }; glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, material); for (int i = 0; i < nearby_points.NEntries(); i++) { TestPoint *point = nearby_points.Kth(i); R3Sphere(point->position, 0.02).Draw(); } } // Show closest point if (selected_point && closest_point) { glEnable(GL_LIGHTING); static GLfloat material[4] = { 0, 1, 0, 1 }; glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, material); R3Sphere(closest_point->position, 0.03).Draw(); } // Show selected point if (selected_point) { glEnable(GL_LIGHTING); static GLfloat material[4] = { 1, 1, 1, 1 }; glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, material); R3Sphere(selected_point->position, 0.05).Draw(); } // Show constraints if (0 && show_constraints) { glDisable(GL_LIGHTING); glColor3f(0.5, 0, 0); R3Sphere(selected_point->position, min_nearby_distance).Outline(); R3Sphere(selected_point->position, max_nearby_distance).Outline(); } // Show KD tree if (show_kdtree) { glDisable(GL_LIGHTING); glColor3f(0, 0, 0); kdtree->Outline(); } // Swap buffers glutSwapBuffers(); }
void GLUTMouse(int button, int state, int x, int y) { // Invert y coordinate y = GLUTwindow_height - y; // Process mouse button event if ((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN)) { // Check for double click static RNTime click_time; const RNScalar max_double_click_elapsed = 0.5; RNBoolean double_click = (click_time.Elapsed() < max_double_click_elapsed); click_time.Read(); // Select closest point to cursor if (double_click) { selected_point = NULL; RNLength closest_distance = 10; R2Point cursor_position(x, y); for (int i = 0; i < all_points.NEntries(); i++) { TestPoint *point = all_points[i]; const R3Point& world_position = point->position; R2Point screen_position = viewer->ViewportPoint(world_position); RNLength distance = R2Distance(screen_position, cursor_position); if (distance < closest_distance) { selected_point = point; closest_distance = distance; } } // Find closest points closest_point = NULL; nearby_points.Empty(); if (selected_point) { if (max_nearby_points > 0) { kdtree->FindClosest(selected_point, min_nearby_distance, max_nearby_distance, max_nearby_points, nearby_points); closest_point = (nearby_points.NEntries() > 0) ? nearby_points.Head() : NULL; if (print_debug) printf("Found %d points\n", nearby_points.NEntries()); } else { kdtree->FindAll(selected_point, min_nearby_distance, max_nearby_distance, nearby_points); closest_point = kdtree->FindClosest(selected_point); if (print_debug) printf("Found %d points\n", nearby_points.NEntries()); } } } } // Remember button state int b = (button == GLUT_LEFT_BUTTON) ? 0 : ((button == GLUT_MIDDLE_BUTTON) ? 1 : 2); GLUTbutton[b] = (state == GLUT_DOWN) ? 1 : 0; // Remember modifiers GLUTmodifiers = glutGetModifiers(); // Remember mouse position GLUTmouse[0] = x; GLUTmouse[1] = y; // Redraw glutPostRedisplay(); }