|
1 | 1 | #include "SurfaceDataItem.h" |
| 2 | +#include <vtkIdList.h> |
| 3 | +#include <vtkIntArray.h> |
| 4 | +#include <vtkCellArray.h> |
2 | 5 | #include <QDebug> |
3 | | -#include <QMetaObject> |
4 | 6 |
|
5 | 7 | using namespace mb_system; |
6 | 8 |
|
| 9 | +// ───────────────────────────────────────────────────────────────────────────── |
| 10 | +// Constructor |
7 | 11 | // ───────────────────────────────────────────────────────────────────────────── |
8 | 12 |
|
9 | 13 | SurfaceDataItem::SurfaceDataItem() |
10 | 14 | : TopoDataItem() |
11 | 15 | { |
12 | | - // Surface view defaults: polygon surface, elevation coloring |
13 | 16 | surfaceRenderType_ = SurfaceRenderType::Polys; |
14 | 17 | coloredScalar_ = ColoredScalar::Elevation; |
15 | 18 | } |
16 | 19 |
|
17 | 20 | // ───────────────────────────────────────────────────────────────────────────── |
18 | | -// assemblePipeline override — apply surface-specific defaults |
| 21 | +// assemblePipeline — surface defaults, then base-class assembly |
19 | 22 | // ───────────────────────────────────────────────────────────────────────────── |
| 23 | + |
20 | 24 | void SurfaceDataItem::assemblePipeline(Pipeline *pipeline) { |
21 | | - // Ensure surface defaults are set before the base class builds the pipeline |
22 | 25 | surfaceRenderType_ = SurfaceRenderType::Polys; |
23 | 26 | coloredScalar_ = ColoredScalar::Elevation; |
| 27 | + // Base class calls connectDataset() → applyColoredScalar() → applyRenderType() |
| 28 | + // etc., in that order. connectDataset() is overridden below and will |
| 29 | + // initialise qualitySource_ before applyColoredScalar() calls |
| 30 | + // dataOutputPort() (also overridden below). |
24 | 31 | TopoDataItem::assemblePipeline(pipeline); |
25 | 32 | } |
26 | 33 |
|
27 | 34 | // ───────────────────────────────────────────────────────────────────────────── |
28 | | -// onRegionSelected — extend selection to full Z range, emit signal |
| 35 | +// connectDataset — wire quality source AFTER base wires the shared polyData |
| 36 | +// |
| 37 | +// IMPORTANT: does NOT change pipeline->source_ or pipeline->polyData_. |
| 38 | +// The rubber-band area picker uses pipeline_->polyData_ directly; keeping it |
| 39 | +// pointed at dataset_->polyData() ensures extraction of all points (good and |
| 40 | +// bad) for the EditDataItem to edit. |
29 | 41 | // ───────────────────────────────────────────────────────────────────────────── |
30 | | -void SurfaceDataItem::onRegionSelected(double worldBounds[6]) { |
31 | 42 |
|
32 | | - qDebug() << "SurfaceDataItem::onRegionSelected(): dataset_=" << dataset_; |
| 43 | +bool SurfaceDataItem::connectDataset(Pipeline *pipeline) { |
| 44 | + // Base: sets pipeline->source_->SetOutput(dataset_->polyData()) |
| 45 | + // and pipeline->polyData_ = dataset_->polyData(). |
| 46 | + // Returns false if dataset is not yet loaded. |
| 47 | + if (!TopoDataItem::connectDataset(pipeline)) return false; |
| 48 | + |
| 49 | + // Build the initial good-only geometry (at load time every point is GOOD, |
| 50 | + // so this is a full copy) and wire qualitySource_ to it. |
| 51 | + rebuildSurfacePolyData(); |
| 52 | + qualitySource_->SetOutput(surfacePolyData_); |
| 53 | + |
| 54 | + qDebug() << "SurfaceDataItem::connectDataset(): qualitySource_ wired," |
| 55 | + << surfacePolyData_->GetNumberOfCells() << "cells in render copy"; |
| 56 | + |
| 57 | + return true; |
| 58 | +} |
| 59 | + |
| 60 | +// ───────────────────────────────────────────────────────────────────────────── |
| 61 | +// dataOutputPort — serve the good-only copy to the rendering stages |
| 62 | +// ───────────────────────────────────────────────────────────────────────────── |
| 63 | + |
| 64 | +vtkAlgorithmOutput *SurfaceDataItem::dataOutputPort(Pipeline *pipeline) { |
| 65 | + (void)pipeline; |
| 66 | + return qualitySource_->GetOutputPort(); |
| 67 | +} |
| 68 | + |
| 69 | +// ───────────────────────────────────────────────────────────────────────────── |
| 70 | +// onQualityChanged — rebuild the good-only surface and re-render |
| 71 | +// ───────────────────────────────────────────────────────────────────────────── |
| 72 | + |
| 73 | +void SurfaceDataItem::onQualityChanged() { |
| 74 | + // qualityChanged() is emitted on the render thread; this slot is connected |
| 75 | + // with Qt::QueuedConnection so it runs on the main thread first, then we |
| 76 | + // dispatch VTK work back to the render thread. |
| 77 | + dispatch_async([this](vtkRenderWindow *rw, vtkUserData) { |
| 78 | + if (!dataset_ || !dataset_->isLoaded()) { |
| 79 | + rw->Render(); |
| 80 | + return; |
| 81 | + } |
| 82 | + rebuildSurfacePolyData(); |
| 83 | + rw->Render(); |
| 84 | + }); |
| 85 | +} |
| 86 | + |
| 87 | +// ───────────────────────────────────────────────────────────────────────────── |
| 88 | +// rebuildSurfacePolyData — core quality-hiding logic (render thread only) |
| 89 | +// |
| 90 | +// Shallow-copies the shared polyData so that points, point data, and cell |
| 91 | +// data arrays are shared (no data duplication). Then replaces the polys |
| 92 | +// array with a new vtkCellArray that omits every cell whose vertices include |
| 93 | +// at least one BAD_DATA point. The result is a geometrically consistent |
| 94 | +// surface with holes where bad soundings were removed. |
| 95 | +// ───────────────────────────────────────────────────────────────────────────── |
| 96 | + |
| 97 | +void SurfaceDataItem::rebuildSurfacePolyData() { |
| 98 | + vtkPolyData *src = dataset_->polyData(); |
| 99 | + |
| 100 | + // ShallowCopy: new container object, shared underlying array references. |
| 101 | + // We immediately replace the polys array, so the source polys are not |
| 102 | + // aliased after this function returns. |
| 103 | + surfacePolyData_->ShallowCopy(src); |
| 104 | + |
| 105 | + vtkIntArray *qa = vtkIntArray::SafeDownCast( |
| 106 | + src->GetPointData()->GetArray(DATA_QUALITY_NAME)); |
| 107 | + |
| 108 | + if (!qa) { |
| 109 | + // No quality array yet — nothing to hide, leave the full surface. |
| 110 | + surfacePolyData_->Modified(); |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + // Walk source cells; include only those with ALL good vertices. |
| 115 | + vtkNew<vtkCellArray> goodPolys; |
| 116 | + vtkNew<vtkIdList> ptIds; |
| 117 | + |
| 118 | + const vtkIdType nCells = src->GetNumberOfCells(); |
| 119 | + vtkIdType nRemoved = 0; |
33 | 120 |
|
34 | | - if (!dataset_ || !dataset_->isLoaded()) return; |
35 | | - const double xMin = worldBounds[0]; |
36 | | - const double xMax = worldBounds[1]; |
37 | | - const double yMin = worldBounds[2]; |
38 | | - const double yMax = worldBounds[3]; |
| 121 | + for (vtkIdType ci = 0; ci < nCells; ++ci) { |
| 122 | + src->GetCellPoints(ci, ptIds); |
39 | 123 |
|
40 | | - // Extend Z to cover the full dataset elevation range, scaled by the |
41 | | - // current vertical exaggeration so the clip volume matches what the user |
42 | | - // sees. A small outward margin avoids clipping surface-level points. |
43 | | - const double zCenter = 0.5 * (dataset_->elevMin() + dataset_->elevMax()); |
44 | | - const double zHalf = 0.5 * (dataset_->elevMax() - dataset_->elevMin()) |
45 | | - * verticalExagg_; |
46 | | - const double margin = zHalf * 0.05; // 5% margin |
| 124 | + bool anyBad = false; |
| 125 | + for (vtkIdType pi = 0; pi < ptIds->GetNumberOfIds(); ++pi) { |
| 126 | + if (qa->GetValue(ptIds->GetId(pi)) == BAD_DATA) { |
| 127 | + anyBad = true; |
| 128 | + break; |
| 129 | + } |
| 130 | + } |
47 | 131 |
|
48 | | - const double zMin = zCenter - zHalf - margin; |
49 | | - const double zMax = zCenter + zHalf + margin; |
| 132 | + if (!anyBad) { |
| 133 | + goodPolys->InsertNextCell(ptIds); |
| 134 | + } else { |
| 135 | + ++nRemoved; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + surfacePolyData_->SetPolys(goodPolys); |
| 140 | + surfacePolyData_->Modified(); |
| 141 | + |
| 142 | + qDebug() << "SurfaceDataItem::rebuildSurfacePolyData():" |
| 143 | + << nCells - nRemoved << "/" << nCells << "cells kept," |
| 144 | + << nRemoved << "removed (bad vertices)"; |
| 145 | +} |
| 146 | + |
| 147 | +// ───────────────────────────────────────────────────────────────────────────── |
| 148 | +// onRegionSelected — emit editBoundsChanged with the rubber-band XY extent |
| 149 | +// ───────────────────────────────────────────────────────────────────────────── |
| 150 | + |
| 151 | +void SurfaceDataItem::onRegionSelected(double worldBounds[6]) { |
| 152 | + qDebug() << "SurfaceDataItem::onRegionSelected() CALLED" |
| 153 | + << "x[" << worldBounds[0] << "," << worldBounds[1] << "]" |
| 154 | + << "y[" << worldBounds[2] << "," << worldBounds[3] << "]" |
| 155 | + << "z[" << worldBounds[4] << "," << worldBounds[5] << "]"; |
50 | 156 |
|
51 | | - qDebug() << "SurfaceDataItem::onRegionSelected():" |
52 | | - << "x[" << xMin << "," << xMax << "]" |
53 | | - << "y[" << yMin << "," << yMax << "]" |
54 | | - << "z[" << zMin << "," << zMax << "]"; |
| 157 | + if (!dataset_) { |
| 158 | + qWarning() << "SurfaceDataItem::onRegionSelected(): dataset_ is null"; |
| 159 | + return; |
| 160 | + } |
| 161 | + if (!dataset_->isLoaded()) { |
| 162 | + qWarning() << "SurfaceDataItem::onRegionSelected(): dataset not loaded"; |
| 163 | + return; |
| 164 | + } |
55 | 165 |
|
56 | | - qDebug() << "emit editBoundsChanged()"; |
57 | | - emit editBoundsChanged(xMin, xMax, yMin, yMax, zMin, zMax); |
| 166 | + // Emit directly; Qt AutoConnection queues the call cross-thread if needed. |
| 167 | + emit editBoundsChanged(worldBounds[0], worldBounds[1], |
| 168 | + worldBounds[2], worldBounds[3], |
| 169 | + worldBounds[4], worldBounds[5]); |
58 | 170 | } |
0 commit comments