void Slice::setup_draw (int axis, Projection& with_projection) { // info for projection: float fov = FOV() / (float) (with_projection.width()+with_projection.height()); float depth = 2.0 * std::max (std::max ( image()->header().vox(0) * image()->header().dim(0), image()->header().vox(1) * image()->header().dim(1)), image()->header().vox(2) * image()->header().dim(2)); // set up projection & modelview matrices: GL::mat4 P = GL::ortho ( -with_projection.width()*fov, with_projection.width()*fov, -with_projection.height()*fov, with_projection.height()*fov, -depth, depth); GL::mat4 M = snap_to_image() ? GL::mat4 (image()->interp.image2scanner_matrix()) : GL::mat4 (orientation()); GL::mat4 MV = adjust_projection_matrix (M, axis) * GL::translate (-target()); with_projection.set (MV, P); }
void AbstractFixel::update_interp_image_buffer (const Projection& projection, const MR::Image::ConstHeader &fixel_header, const MR::Image::Transform &header_transform) { // Code below "inspired" by ODF::draw Point<> p (Window::main->target()); p += projection.screen_normal() * (projection.screen_normal().dot (Window::main->focus() - p)); p = header_transform.scanner2voxel (p); if (fixel_tool.do_lock_to_grid) { p[0] = (int)std::round (p[0]); p[1] = (int)std::round (p[1]); p[2] = (int)std::round (p[2]); } p = header_transform.voxel2scanner (p); const MR::Image::Info& header_info = fixel_header.info(); Point<> x_dir = projection.screen_to_model_direction (1.0f, 0.0f, projection.depth_of (p)); x_dir.normalise(); x_dir = header_transform.scanner2image_dir (x_dir); x_dir[0] *= header_info.vox(0); x_dir[1] *= header_info.vox(1); x_dir[2] *= header_info.vox(2); x_dir = header_transform.image2scanner_dir (x_dir); Point<> y_dir = projection.screen_to_model_direction (0.0f, 1.0f, projection.depth_of (p)); y_dir.normalise(); y_dir = header_transform.scanner2image_dir (y_dir); y_dir[0] *= header_info.vox(0); y_dir[1] *= header_info.vox(1); y_dir[2] *= header_info.vox(2); y_dir = header_transform.image2scanner_dir (y_dir); Point<> x_width = projection.screen_to_model_direction (projection.width()/2.0f, 0.0f, projection.depth_of (p)); int nx = std::ceil (x_width.norm() / x_dir.norm()); Point<> y_width = projection.screen_to_model_direction (0.0f, projection.height()/2.0f, projection.depth_of (p)); int ny = std::ceil (y_width.norm() / y_dir.norm()); regular_grid_buffer_pos.clear(); regular_grid_buffer_dir.clear(); regular_grid_buffer_val.clear(); for (int y = -ny; y <= ny; ++y) { for (int x = -nx; x <= nx; ++x) { Point<> scanner_pos = p + float(x)*x_dir + float(y)*y_dir; Point<> voxel_pos = header_transform.scanner2voxel(scanner_pos); // Round to nearest neighbour voxel_pos[0] = (int)std::round (voxel_pos[0]); voxel_pos[1] = (int)std::round (voxel_pos[1]); voxel_pos[2] = (int)std::round (voxel_pos[2]); // Find and add point indices that correspond to projected voxel const auto &voxel_indices = voxel_to_indices_map[voxel_pos]; // Load all corresponding fixel data into separate buffer // We can't reuse original buffer because off-axis rendering means that // two or more points in our regular grid may correspond to the same nearest voxel for(const GLsizei index : voxel_indices) { regular_grid_buffer_pos.push_back(scanner_pos); regular_grid_buffer_dir.push_back(buffer_dir[index]); regular_grid_buffer_val.push_back(buffer_val[2 * index]); regular_grid_buffer_val.push_back(buffer_val[(2 * index) + 1]); } } } if(!regular_grid_buffer_pos.size()) return; regular_grid_vao.bind(); regular_grid_vertex_buffer.bind (gl::ARRAY_BUFFER); gl::BufferData (gl::ARRAY_BUFFER, regular_grid_buffer_pos.size() * sizeof(Point<float>), ®ular_grid_buffer_pos[0], gl::DYNAMIC_DRAW); gl::EnableVertexAttribArray (0); gl::VertexAttribPointer (0, 3, gl::FLOAT, gl::FALSE_, 0, (void*)0); // fixel directions regular_grid_dir_buffer.bind (gl::ARRAY_BUFFER); gl::BufferData (gl::ARRAY_BUFFER, regular_grid_buffer_dir.size() * sizeof(Point<float>), ®ular_grid_buffer_dir[0], gl::DYNAMIC_DRAW); gl::EnableVertexAttribArray (1); gl::VertexAttribPointer (1, 3, gl::FLOAT, gl::FALSE_, 0, (void*)0); // fixel sizes and values regular_grid_val_buffer.bind (gl::ARRAY_BUFFER); gl::BufferData (gl::ARRAY_BUFFER, regular_grid_buffer_val.size() * sizeof(float), ®ular_grid_buffer_val[0], gl::DYNAMIC_DRAW); gl::EnableVertexAttribArray (2); gl::VertexAttribPointer (2, 2, gl::FLOAT, gl::FALSE_, 0, (void*)0); }