|
| 1 | +#include "StudioImageActorPointPlacer.h" |
| 2 | + |
| 3 | +#include "vtkCamera.h" |
| 4 | +#include "vtkCellPicker.h" |
| 5 | +#include "vtkImageActor.h" |
| 6 | +#include "vtkImageData.h" |
| 7 | +#include "vtkImageMapper3D.h" |
| 8 | +#include "vtkMath.h" |
| 9 | +#include "vtkNew.h" |
| 10 | +#include "vtkObjectFactory.h" |
| 11 | +#include "vtkRenderer.h" |
| 12 | +#include "vtkTransform.h" |
| 13 | + |
| 14 | +vtkStandardNewMacro(StudioImageActorPointPlacer); |
| 15 | + |
| 16 | +//---------------------------------------------------------------------------- |
| 17 | +StudioImageActorPointPlacer::StudioImageActorPointPlacer() {} |
| 18 | + |
| 19 | +//---------------------------------------------------------------------------- |
| 20 | +StudioImageActorPointPlacer::~StudioImageActorPointPlacer() {} |
| 21 | + |
| 22 | +//---------------------------------------------------------------------------- |
| 23 | +void StudioImageActorPointPlacer::PrintSelf(ostream& os, vtkIndent indent) { this->Superclass::PrintSelf(os, indent); } |
| 24 | + |
| 25 | +//---------------------------------------------------------------------------- |
| 26 | +int StudioImageActorPointPlacer::ComputeWorldPosition(vtkRenderer* renderer, double displayPos[2], double worldPos[3], |
| 27 | + double worldOrient[9]) { |
| 28 | + vtkImageActor* actor = this->GetImageActor(); |
| 29 | + if (!actor) { |
| 30 | + return 0; |
| 31 | + } |
| 32 | + |
| 33 | + // Use a cell picker for accurate picking on the transformed image |
| 34 | + vtkNew<vtkCellPicker> picker; |
| 35 | + picker->SetTolerance(0.005); |
| 36 | + |
| 37 | + // Pick only on our specific actor |
| 38 | + picker->InitializePickList(); |
| 39 | + picker->AddPickList(actor); |
| 40 | + picker->PickFromListOn(); |
| 41 | + |
| 42 | + // Perform the pick |
| 43 | + if (!picker->Pick(displayPos[0], displayPos[1], 0.0, renderer)) { |
| 44 | + return 0; |
| 45 | + } |
| 46 | + |
| 47 | + // Check if we picked the correct actor |
| 48 | + vtkImageActor* pickedActor = vtkImageActor::SafeDownCast(picker->GetProp3D()); |
| 49 | + if (pickedActor != actor) { |
| 50 | + return 0; |
| 51 | + } |
| 52 | + |
| 53 | + // Get the picked position - this accounts for the transformation |
| 54 | + picker->GetPickPosition(worldPos); |
| 55 | + |
| 56 | + // If orientation is requested, compute it using the transformed plane |
| 57 | + if (worldOrient) { |
| 58 | + // Get the normal from the picker - this will be in transformed space |
| 59 | + double* normal = picker->GetPickNormal(); |
| 60 | + |
| 61 | + // Z axis is the normal to the image plane |
| 62 | + worldOrient[6] = normal[0]; |
| 63 | + worldOrient[7] = normal[1]; |
| 64 | + worldOrient[8] = normal[2]; |
| 65 | + |
| 66 | + // Compute a suitable X axis that's perpendicular to Z |
| 67 | + double xAxis[3] = {1.0, 0.0, 0.0}; |
| 68 | + if (fabs(normal[0]) > 0.99) { |
| 69 | + xAxis[0] = 0.0; |
| 70 | + xAxis[1] = 1.0; |
| 71 | + } |
| 72 | + |
| 73 | + // Make X perpendicular to Z |
| 74 | + double dot = vtkMath::Dot(xAxis, normal); |
| 75 | + xAxis[0] -= dot * normal[0]; |
| 76 | + xAxis[1] -= dot * normal[1]; |
| 77 | + xAxis[2] -= dot * normal[2]; |
| 78 | + vtkMath::Normalize(xAxis); |
| 79 | + |
| 80 | + worldOrient[0] = xAxis[0]; |
| 81 | + worldOrient[1] = xAxis[1]; |
| 82 | + worldOrient[2] = xAxis[2]; |
| 83 | + |
| 84 | + // Y axis is Z cross X |
| 85 | + double yAxis[3]; |
| 86 | + vtkMath::Cross(normal, xAxis, yAxis); |
| 87 | + worldOrient[3] = yAxis[0]; |
| 88 | + worldOrient[4] = yAxis[1]; |
| 89 | + worldOrient[5] = yAxis[2]; |
| 90 | + } |
| 91 | + |
| 92 | + return 1; |
| 93 | +} |
| 94 | + |
| 95 | +//---------------------------------------------------------------------------- |
| 96 | +int StudioImageActorPointPlacer::ValidateWorldPosition(double worldPos[3]) { |
| 97 | + vtkImageActor* actor = this->GetImageActor(); |
| 98 | + if (!actor) { |
| 99 | + return 0; |
| 100 | + } |
| 101 | + |
| 102 | + // Create a cell locator for the actor's mapper input |
| 103 | + vtkImageMapper3D* mapper = actor->GetMapper(); |
| 104 | + if (!mapper) { |
| 105 | + return 0; |
| 106 | + } |
| 107 | + |
| 108 | + vtkImageData* image = vtkImageData::SafeDownCast(mapper->GetInput()); |
| 109 | + if (!image) { |
| 110 | + return 0; |
| 111 | + } |
| 112 | + |
| 113 | + // Transform the world position back to image space |
| 114 | + double imagePos[3]; |
| 115 | + if (actor->GetUserTransform()) { |
| 116 | + vtkNew<vtkTransform> inverseTransform; |
| 117 | + inverseTransform->DeepCopy(actor->GetUserTransform()); |
| 118 | + inverseTransform->Inverse(); |
| 119 | + inverseTransform->TransformPoint(worldPos, imagePos); |
| 120 | + } else { |
| 121 | + imagePos[0] = worldPos[0]; |
| 122 | + imagePos[1] = worldPos[1]; |
| 123 | + imagePos[2] = worldPos[2]; |
| 124 | + } |
| 125 | + |
| 126 | + // Check if the point is within the image bounds |
| 127 | + double bounds[6]; |
| 128 | + image->GetBounds(bounds); |
| 129 | + |
| 130 | + if (imagePos[0] < bounds[0] || imagePos[0] > bounds[1] || imagePos[1] < bounds[2] || imagePos[1] > bounds[3] || |
| 131 | + imagePos[2] < bounds[4] || imagePos[2] > bounds[5]) { |
| 132 | + return 0; |
| 133 | + } |
| 134 | + |
| 135 | + return 1; |
| 136 | +} |
| 137 | + |
| 138 | +//---------------------------------------------------------------------------- |
| 139 | +int StudioImageActorPointPlacer::UpdateWorldPosition(vtkRenderer* renderer, double worldPos[3], double worldOrient[9]) { |
| 140 | + // For a point already on the plane, we need to ensure it stays within bounds |
| 141 | + if (!this->ValidateWorldPosition(worldPos)) { |
| 142 | + return 0; |
| 143 | + } |
| 144 | + |
| 145 | + // Orientation handling |
| 146 | + if (worldOrient) { |
| 147 | + vtkImageActor* actor = this->GetImageActor(); |
| 148 | + if (actor && actor->GetUserTransform()) { |
| 149 | + // Use the normal from the transformed image plane for the Z axis |
| 150 | + vtkNew<vtkCellPicker> picker; |
| 151 | + picker->SetTolerance(0.005); |
| 152 | + |
| 153 | + // Pick at the world position |
| 154 | + double displayPos[3]; |
| 155 | + renderer->SetWorldPoint(worldPos[0], worldPos[1], worldPos[2], 1.0); |
| 156 | + renderer->WorldToDisplay(); |
| 157 | + renderer->GetDisplayPoint(displayPos); |
| 158 | + |
| 159 | + // Try to pick on the actor |
| 160 | + picker->InitializePickList(); |
| 161 | + picker->AddPickList(actor); |
| 162 | + picker->PickFromListOn(); |
| 163 | + |
| 164 | + if (picker->Pick(displayPos[0], displayPos[1], 0.0, renderer)) { |
| 165 | + vtkImageActor* pickedActor = vtkImageActor::SafeDownCast(picker->GetProp3D()); |
| 166 | + if (pickedActor == actor) { |
| 167 | + // Use the picked normal |
| 168 | + double* normal = picker->GetPickNormal(); |
| 169 | + |
| 170 | + worldOrient[6] = normal[0]; |
| 171 | + worldOrient[7] = normal[1]; |
| 172 | + worldOrient[8] = normal[2]; |
| 173 | + |
| 174 | + // Compute suitable X and Y axes as in ComputeWorldPosition |
| 175 | + double xAxis[3] = {1.0, 0.0, 0.0}; |
| 176 | + if (fabs(normal[0]) > 0.99) { |
| 177 | + xAxis[0] = 0.0; |
| 178 | + xAxis[1] = 1.0; |
| 179 | + } |
| 180 | + |
| 181 | + double dot = vtkMath::Dot(xAxis, normal); |
| 182 | + xAxis[0] -= dot * normal[0]; |
| 183 | + xAxis[1] -= dot * normal[1]; |
| 184 | + xAxis[2] -= dot * normal[2]; |
| 185 | + vtkMath::Normalize(xAxis); |
| 186 | + |
| 187 | + worldOrient[0] = xAxis[0]; |
| 188 | + worldOrient[1] = xAxis[1]; |
| 189 | + worldOrient[2] = xAxis[2]; |
| 190 | + |
| 191 | + double yAxis[3]; |
| 192 | + vtkMath::Cross(normal, xAxis, yAxis); |
| 193 | + worldOrient[3] = yAxis[0]; |
| 194 | + worldOrient[4] = yAxis[1]; |
| 195 | + worldOrient[5] = yAxis[2]; |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + return 1; |
| 202 | +} |
0 commit comments