diff --git a/carta-protobuf b/carta-protobuf index 7be0e2c37..f4d989b36 160000 --- a/carta-protobuf +++ b/carta-protobuf @@ -1 +1 @@ -Subproject commit 7be0e2c378c1472703c4e39cc3990f90a14d1fa6 +Subproject commit f4d989b36932bbecf3f8f845500491aa476f57cc diff --git a/src/Region/RegionHandler.cc b/src/Region/RegionHandler.cc index 38af4847f..4e59aff64 100644 --- a/src/Region/RegionHandler.cc +++ b/src/Region/RegionHandler.cc @@ -955,67 +955,6 @@ bool RegionHandler::CalculateMoments(int file_id, int region_id, const std::shar return !collapse_results.empty(); } -// bool RegionHandler::CalculateRender3DData(const CARTA::Render3DRequest& render3d_request, -// std::shared_ptr& frame, GeneratorProgressCallback progress_callback, -// CARTA::Render3DResponse render3d_response, CARTA::Render3DData render3d_data) { -// // Unpack request message and send it along -// int file_id(render3d_request.file_id()); -// int region_id(render3d_request.region_id()); -// int viewer_id(render3d_request.viewer_id()); -// bool keep(render3d_request.keep()); -// AxisRange spectral_range; -// if (render3d_request.has_spectral_range()) { -// spectral_range = AxisRange(render3d_request.spectral_range().min(), render3d_request.spectral_range().max()); -// } else { -// spectral_range = AxisRange(0, frame->Depth() - 1); -// } -// int rebin_xy = std::max(render3d_request.rebin_xy(), 1); -// int rebin_z = std::max(render3d_request.rebin_z(), 1); -// auto compression = render3d_request.compression_type(); -// float image_quality = render3d_request.image_compression_quality(); -// // CARTA::ImageBounds image_bounds; -// // if (render3d_request.has_image_bounds()) { -// // image_bounds = CARTA::ImageBounds(render3d_request.image_bounds().x_min(), render3d_request.image_bounds().x_max(), render3d_request.image_bounds().y_min(), render3d_request.image_bounds().y_max()); -// // } else { -// // image_bounds = CARTA::ImageBounds(0, frame->Width() - 1, 0, frame->Height() - 1); -// // } -// render3d_response.set_success(false); -// render3d_response.set_cancel(false); - -// // Checks for valid request: -// // 1. Region is set. For Render3D Region is optional -// // if (!RegionSet(region_id, true)) { -// // render3d_response.set_message("3D Rendering requested for invalid region."); -// // return false; -// // } - -// // 2. Region is closed -// if (!IsClosedRegion(region_id)) { -// render3d_response.set_message("Region type not supported for 3D Rendering."); -// return false; -// } - -// // 3. Image has spectral axis -// if (!frame->CoordinateSystem()->hasSpectralAxis()) { -// render3d_response.set_message("No spectral coordinate for generating 3D rendering."); -// return false; -// } - -// // 4. Image is smaller than limit -// if (frame->Width() * frame->Height() * frame->Depth() > MAX_RENDER3D_PIXELS) { -// render3d_response.set_message("Cube size exceeds maximum for 3D Rendering. Please use smaller region or spectral range."); -// return false; -// } - -// // Set frame -// if (!FrameSet(file_id)) { -// _frames[file_id] = frame; -// } -// // compression, image_quality, -// return CalculateRender3DData(file_id, region_id, viewer_id, spectral_range, rebin_xy, rebin_z, keep, frame, progress_callback, render3d_response, render3d_data); - -// } - bool RegionHandler::CalculatePvImage(const CARTA::PvRequest& pv_request, std::shared_ptr& frame, GeneratorProgressCallback progress_callback, CARTA::PvResponse& pv_response, GeneratedImage& pv_image) { // Unpack request message and send it along @@ -1408,83 +1347,6 @@ bool RegionHandler::CalculatePvPreviewImage(int frame_id, int preview_id, bool q return true; } -// // CARTA::ImageBounds& image_bounds, -// bool RegionHandler::CalculateRender3DData(int file_id, int region_id, int viewer_id, -// AxisRange& spectral_range, int rebin_xy, int rebin_z, bool keep, std::shared_ptr& frame, GeneratorProgressCallback progress_callback, CARTA::Render3DResponse& render3d_response, -// CARTA::Render3DData& render3d_data) { - -// // use if region is mandatory -// // auto region = GetRegion(region_id); -// // if (!region) { -// // render3d_response.set_message("Render3D region not set"); -// // return false; -// // } -// // casacore::Cube render3d_cube; - -// bool is_image_region(region_id == IMAGE_REGION_ID); -// RegionState region_state; -// if (!is_image_region) { -// if (!RegionSet(region_id)) { -// render3d_response.set_message("3D rendering cube requested for invalid region id."); -// return false; -// } -// if (!IsClosedRegion(region_id)) { -// render3d_response.set_message("3D rendering cube requested for invalid region type."); -// return false; -// } - -// region_state = _regions.at(region_id)->GetRegionState(); -// } - -// auto stokes = frame->CurrentStokes(); -// PreviewCubeParameters cube_parameters(file_id, region_id, spectral_range, rebin_xy, rebin_z, stokes, region_state); - -// auto frame_id = GetRender3DViewerFrameId(viewer_id); -// bool viewer_frame_set = _frames.find(frame_id) != _frames.end(); - -// // Update cube settings for existing ID. -// // Set unique locks so in-progress cubes are completed before update. -// std::unique_lock render3d_cube_lock(_render3d_cube_mutex); -// if (_render3d_cubes.find(viewer_id) == _render3d_cubes.end() || -// !_render3d_cubes.at(viewer_id)->HasSameParameters(cube_parameters)) { -// // Cube changed, see if set for another viewer ID -// bool cube_found(false); -// for (auto& render3d_cube : _render3d_cubes) { -// if (render3d_cube.second->HasSameParameters(cube_parameters)) { -// _render3d_cubes[viewer_id] = render3d_cube.second; -// cube_found = true; -// break; -// } -// } -// if (!cube_found) { -// _render3d_cubes[viewer_id] = std::shared_ptr(new PvPreviewCube(cube_parameters)); -// } - -// // If preview cube changed, then frame for its preview image cube is invalid -// viewer_frame_set = false; -// } -// auto render3d_cube = _render3d_cubes.at(viewer_id); -// bool render3d_cube_loaded = render3d_cube->CubeLoaded(); -// render3d_cube_lock.unlock(); - -// // FINISH FUNCTION!! - -// } - -// bool RegionHandler::CalculateRender3DData(int file_id, int region_id, int viewer_id, std::shared_ptr render3d_cube,std::shared_ptr& frame, GeneratorProgressCallback progress_callback, CARTA::Render3DResponse& render3d_response) { -// // Prepare response; if error, add message. -// render3d_response.set_success(false); -// render3d_response.set_cancel(false); -// auto* render3d_data_message = render3d_response.mutable_render3d_data(); -// render3d_data_message->set_viewer_id(viewer_id); -// render3d_data_message->set_width(0); -// render3d_data_message->set_height(0); -// render3d_data_message->set_depth(0); - -// return true; - -// } - bool RegionHandler::CalculatePvImage(int file_id, int region_id, int width, AxisRange& spectral_range, bool reverse, bool keep, std::shared_ptr& frame, GeneratorProgressCallback progress_callback, CARTA::PvResponse& pv_response, GeneratedImage& pv_image) { // Generate PV image by approximating line/polyline as box regions and getting spectral profile for each. @@ -1933,12 +1795,82 @@ bool RegionHandler::GetRegionHistogramData( } // ***** Fill 3d rendering ***** -// GeneratorProgressCallback progress_callback, CARTA::Render3DData& render3d_data +bool RegionHandler::FillRender3DData(const CARTA::Render3DRequest& render3d_request, CARTA::Render3DResponse& render3d_response, std::function ack_callback, std::function cb) { + + // Unpack request message + int file_id(render3d_request.file_id()); + int region_id(render3d_request.region_id()); + int viewer_id(render3d_request.viewer_id()); + bool keep(render3d_request.keep()); + int rebin_xy = std::max(render3d_request.rebin_xy(), 1); + int rebin_z = std::max(render3d_request.rebin_z(), 1); + auto compression_type = render3d_request.compression_type(); + float compression_quality = render3d_request.compression_quality(); -bool RegionHandler::SendRender3DData(int file_id, int region_id, int viewer_id, AxisRange& spectral_range, int rebin_xy, int rebin_z, std::shared_ptr& frame, std::function cb) { + bool data_sent = false; + + if (_frames.count(file_id)) { + auto& frame = _frames.at(file_id); + + AxisRange spectral_range; + if (render3d_request.has_spectral_range()) { + spectral_range = AxisRange(render3d_request.spectral_range().min(), render3d_request.spectral_range().max()); + } else { + spectral_range = AxisRange(0, frame->Depth() - 1); + } + + // Checks for 3D rendering + + bool is_image_region(region_id == IMAGE_REGION_ID); + if (!is_image_region) { + if (!RegionSet(region_id)) { + render3d_response.set_message("3D rendering cube requested for invalid region id."); + render3d_response.set_success(false); + return false; + } + } + + // 2. Region is closed + if (!IsClosedRegion(region_id)) { + render3d_response.set_message("Region type not supported for 3D Rendering."); + render3d_response.set_success(false); + return false; + } + + // 3. Image has spectral axis + if (!frame->CoordinateSystem()->hasSpectralAxis()) { + render3d_response.set_message("No spectral coordinate for generating 3D rendering."); + render3d_response.set_success(false); + return false; + } + + // 4. Image is smaller than limit + if (frame->Width() * frame->Height() * frame->Depth() > MAX_RENDER3D_PIXELS) { + render3d_response.set_message("Cube size exceeds maximum for 3D Rendering. Use smaller region or spectral range."); + render3d_response.set_success(false); + return false; + } + + render3d_response.set_success(true); + render3d_response.set_message("3D rendering started"); + ack_callback(render3d_response); + + data_sent = SendRender3DData(file_id, region_id, viewer_id, spectral_range, rebin_xy, rebin_z, compression_type, compression_quality, frame, cb); + + return data_sent; + + } else { + render3d_response.set_message("File ID not found."); + render3d_response.set_success(false); + ack_callback(render3d_response); + return false; + } +} + +bool RegionHandler::SendRender3DData(int file_id, int region_id, int viewer_id, AxisRange& spectral_range, int rebin_xy, int rebin_z, CARTA::CompressionType compression_type, float compression_quality, std::shared_ptr& frame, std::function cb) { - RegionState region_state = _regions.at(region_id)->GetRegionState(); + RegionState region_state = GetRegion(region_id)->GetRegionState(); auto stokes = frame->CurrentStokes(); // PreviewCubeParameters cube_parameters(file_id, region_id, spectral_range, rebin_xy, rebin_z, stokes, region_state); @@ -2053,8 +1985,8 @@ bool RegionHandler::SendRender3DData(int file_id, int region_id, int viewer_id, int height = frame->Height(); // make compression - CARTA::CompressionType compression_type = CARTA::CompressionType::ZFP; - int compression_quality = 20; // high is 32, use 20 for now + compression_type = CARTA::CompressionType::ZFP; + compression_quality = 20; // high is 32, use 20 for now std::vector compression_buffer; size_t compressed_size; // get data and transform to vector @@ -2067,21 +1999,11 @@ bool RegionHandler::SendRender3DData(int file_id, int region_id, int viewer_id, Compress(image_data, 0, compression_buffer, compressed_size, width, height, compression_quality); auto data_message = Message::Render3DData( - viewer_id, compression_buffer, nan_encodings, compression_type, + viewer_id, compression_buffer.data(), nan_encodings, compression_type, compression_quality, progress); cb(data_message); - // render3d_data.set_viewer_id(viewer_id); - // - // render3d_data.set_width(width); - // - // render3d_data.set_height(height); - // int depth = slices_range.to - slices_range.from + 1; - // render3d_data.set_depth(depth); - // render3d_data.set_progress(progress); - // render3d_data.set_image_data(image_data); - if (progress >= 1.0) { return true; } @@ -2090,11 +2012,6 @@ bool RegionHandler::SendRender3DData(int file_id, int region_id, int viewer_id, } } -// bool RegionHandler::FillRender3DData(std::function cb) { - - -// } - // ***** Fill spectral profile ***** bool RegionHandler::FillSpectralProfileData( diff --git a/src/Region/RegionHandler.h b/src/Region/RegionHandler.h index e95412f9e..5a0f48404 100644 --- a/src/Region/RegionHandler.h +++ b/src/Region/RegionHandler.h @@ -65,9 +65,9 @@ class RegionHandler { bool FillRegionStatsData(std::function cb, int region_id, int file_id); bool FillPointSpatialProfileData(int file_id, int region_id, std::vector& spatial_data_vec); bool FillLineSpatialProfileData(int file_id, int region_id, std::function cb); - // bool FillRender3DData(); + bool FillRender3DData(const CARTA::Render3DRequest& render3d_request, CARTA::Render3DResponse& render3d_response, std::function ack_callback, std::function cb); bool SendRender3DData(int file_id, int region_id, int viewer_id, - AxisRange& spectral_range, int rebin_xy, int rebin_z, std::shared_ptr& frame, std::function cb); + AxisRange& spectral_range, int rebin_xy, int rebin_z, CARTA::CompressionType compression_type, float compression_quality, std::shared_ptr& frame, std::function cb); // Calculate moments bool CalculateMoments(int file_id, int region_id, const std::shared_ptr& frame, GeneratorProgressCallback progress_callback, diff --git a/src/Session/Session.cc b/src/Session/Session.cc index a159d4fd2..0eeb29281 100644 --- a/src/Session/Session.cc +++ b/src/Session/Session.cc @@ -1336,93 +1336,22 @@ bool Session::OnConcatStokesFiles(const CARTA::ConcatStokesFiles& message, uint3 SendEvent(CARTA::EventType::CONCAT_STOKES_FILES_ACK, request_id, response); return success; } - +// VOID OR BOOL? void Session::OnRender3DRequest(const CARTA::Render3DRequest& render3d_request, uint32_t request_id) { - // return true if data sent - bool data_sent(false); - - // Unpack request message - int file_id(render3d_request.file_id()); - int region_id(render3d_request.region_id()); - int viewer_id(render3d_request.viewer_id()); - bool keep(render3d_request.keep()); - int rebin_xy = std::max(render3d_request.rebin_xy(), 1); - int rebin_z = std::max(render3d_request.rebin_z(), 1); - auto compression = render3d_request.compression_type(); - float image_quality = render3d_request.image_compression_quality(); - - if (_frames.count(file_id)) { - - auto& frame = _frames.at(file_id); - - AxisRange spectral_range; - if (render3d_request.has_spectral_range()) { - spectral_range = AxisRange(render3d_request.spectral_range().min(), render3d_request.spectral_range().max()); - } else { - spectral_range = AxisRange(0, frame->Depth() - 1); - } - - CARTA::Render3DResponse render3d_response; - std::cout << "Render3D function called" << std::endl; - - // Checks for valid request: - // 1. Region is set. For Render3D Region is optional - // if (!RegionSet(region_id, true)) { - // render3d_response.set_message("3D Rendering requested for invalid region."); - // return false; - // } - bool is_image_region(region_id == IMAGE_REGION_ID); - if (!is_image_region) { - if (!RegionSet(region_id)) { - render3d_response.set_message("3D rendering cube requested for invalid region id."); - render3d_response.set_success(false); - } - } - - // 2. Region is closed - if (!IsClosedRegion(region_id)) { - render3d_response.set_message("Region type not supported for 3D Rendering."); - render3d_response.set_success(false); - } - - // 3. Image has spectral axis - if (!frame->CoordinateSystem()->hasSpectralAxis()) { - render3d_response.set_message("No spectral coordinate for generating 3D rendering."); - render3d_response.set_success(false); - } - // 4. Image is smaller than limit - if (frame->Width() * frame->Height() * frame->Depth() > MAX_RENDER3D_PIXELS) { - render3d_response.set_message("Cube size exceeds maximum for 3D Rendering. Use smaller region or spectral range."); - render3d_response.set_success(false); - } + CARTA::Render3DResponse render3d_response; + std::cout << "Render3D function called" << std::endl; - // Acknowledge if any of the checks failed - if (!render3d_response.success) { - SendEvent(CARTA::EventType::RENDER3D_RESPONSE, request_id, render3d_response); - return false; - } - - // if all checks passed, send success message - render3d_response.set_message("3D Rendering started."); - render3d_response.set_success(true); + auto ack_callback = [&](CARTA::Render3DResponse render3d_response) { SendEvent(CARTA::EventType::RENDER3D_RESPONSE, request_id, render3d_response); - Timer t; - CARTA::Render3DData render3d_data; + }; - data_sent = _region_handler->SendRender3DData(file_id, region_id, viewer_id, spectral_range, rebin_xy, rebin_z, - frame, - [&](CARTA::Render3DData render3d_data) { - // send (partial) render3d datacube - SendEvent(CARTA::EventType::RENDER3D_DATA, request_id, render3d_data); - }, - ); - } else { - render3d_response.set_message("File id not found."); - render3d_response.set_success(false); - SendEvent(CARTA::EventType::RENDER3D_RESPONSE, request_id, render3d_response); - } - return data_sent; + bool data_sent = _region_handler->FillRender3DData(render3d_request, render3d_response, ack_callback, + [&](CARTA::Render3DData render3d_data) { + // send (partial) render3d datacube + SendEvent(CARTA::EventType::RENDER3D_DATA, request_id, render3d_data); + } + ); } void Session::OnPvRequest(const CARTA::PvRequest& pv_request, uint32_t request_id) { @@ -1922,24 +1851,6 @@ void Session::StopPvPreviewUpdates(int preview_id) { } } -bool Session::SendRender3D(int file_id, int region_id) { - // return true if data sent - Timer t; - bool data_sent(false); - data_sent = _region_handler->FillRender3DData( - [&](CARTA::Render3DData render3d_data) { - - } - ); - return true; -} - -void Session::StopRender3DUpdates(int viewer_id) { - if (_region_handler) { - _region_handler->StopRender3DUpdates(viewer_id); - } -} - bool Session::SendContourData(int file_id, bool ignore_empty) { if (_frames.count(file_id)) { auto frame = _frames.at(file_id); diff --git a/src/Util/Message.cc b/src/Util/Message.cc index a30ae8402..932c30c2b 100644 --- a/src/Util/Message.cc +++ b/src/Util/Message.cc @@ -551,13 +551,17 @@ CARTA::MomentProgress Message::MomentProgress(int32_t file_id, float progress) { return message; } -CARTA::Render3DRequest Message::Render3DRequest(int32_t file_id, int32_t region_id, CARTA::ImageBounds image_bounds, CARTA::IntBounds spectral_range, bool keep) { +CARTA::Render3DRequest Message::Render3DRequest(int32_t file_id, int32_t region_id, int32_t viewer_id, CARTA::IntBounds spectral_range, int32_t rebin_xy, int32_t rebin_z, CARTA::CompressionType compression_type, float compression_quality, bool keep) { CARTA::Render3DRequest message; // Probably wrong, image bounds and spectral range message.set_file_id(file_id); message.set_region_id(region_id); - *message.mutable_image_bounds() = image_bounds; + message.set_viewer_id(viewer_id); *message.mutable_spectral_range() = spectral_range; + message.set_rebin_xy(rebin_xy); + message.set_rebin_z(rebin_z); + message.set_compression_type(compression_type); + message.set_compression_quality(compression_quality); message.set_keep(keep); return message; } @@ -570,7 +574,7 @@ CARTA::Render3DRequest Message::Render3DRequest(int32_t file_id, int32_t region_ // return message; // } -CARTA::Render3DData Message::Render3DData(int32_t viewer_id, const std::vector& image_data, const std::vector& nan_encodings, CompressionType compression_type, float compression_quality, float progress) { +CARTA::Render3DData Message::Render3DData(int32_t viewer_id, const std::vector& image_data, const std::vector& nan_encodings, CARTA::CompressionType compression_type, float compression_quality, float progress) { CARTA::Render3DData message; message.set_viewer_id(viewer_id); message.set_image_data(image_data); diff --git a/src/Util/Message.h b/src/Util/Message.h index 3c66a0732..893478afc 100644 --- a/src/Util/Message.h +++ b/src/Util/Message.h @@ -35,6 +35,7 @@ #include #include #include +#include #include #include #include @@ -134,7 +135,7 @@ class Message { uint32_t session_id, bool success, const std::string& status, const CARTA::SessionType& type); static CARTA::MomentProgress MomentProgress(int32_t file_id, float progress); static CARTA::Render3DRequest Render3DRequest( - int32_t file_id, int32_t region_id, CARTA::ImageBounds image_bounds, CARTA::IntBounds spectral_range, bool keep); + int32_t file_id, int32_t region_id, int32_t viewer_id, CARTA::IntBounds spectral_range, int32_t rebin_xy, int32_t rebin_z, CARTA::CompressionType compression_type, float compression_quality, bool keep); // static CARTA::Render3DResponse Render3DResponse( // bool success, const std::string& message, bool cancel); static CARTA::Render3DData Render3DData(