| 171 | } |
| 172 | |
| 173 | Expected<void> RadiusCompensator::applyCompensation() |
| 174 | { |
| 175 | MR_TIMER; |
| 176 | MR_WRITER( mesh_ ); |
| 177 | |
| 178 | Mesh cpyMesh = mesh_; // for projecting |
| 179 | VertBitSet updatedVerts( planeVerts_.size() ); |
| 180 | |
| 181 | int maxIters = std::max( 1, params_.maxIterations ); |
| 182 | |
| 183 | auto sb = subprogress( params_.callback, 0.4f, 1.0f ); |
| 184 | auto maxAllowedShiftSq = sqr( std::min( mesh_.computeBoundingBox( params_.region ).diagonal() * 1e-2f, 2 * params_.toolRadius / float( maxIters ) ) ); |
| 185 | for ( int i = 1; i <= maxIters; ++i ) |
| 186 | { |
| 187 | if ( i == maxIters ) |
| 188 | maxAllowedShiftSq = radiusSq_; // allow full move on last iteration |
| 189 | |
| 190 | float maxShiftSq = -FLT_MAX; |
| 191 | updatedVerts.reset(); |
| 192 | for ( auto [cost, cId] : costs_ ) |
| 193 | { |
| 194 | if ( cId < 0 ) |
| 195 | continue; |
| 196 | auto planeToolCenter = toPlaneXf_( toolCenters_[cId] ); |
| 197 | |
| 198 | findPointsInBall( *planeTree_, { .center = to3dim( to2dim( planeToolCenter ) ),.radiusSq = radiusSq_ }, |
| 199 | [&] ( const PointsProjectionResult & found, const Vector3f &, Ball3f & ) |
| 200 | { |
| 201 | const auto v = found.vId; |
| 202 | if ( !vertRegion_.test( v ) ) |
| 203 | return Processing::Continue; // boundary vert |
| 204 | auto planePoint = toPlaneXf_( mesh_.points[v] ); |
| 205 | auto shift = calcCompensationMovement_( planePoint, planeToolCenter ); |
| 206 | if ( shift == Vector3f() ) |
| 207 | return Processing::Continue; |
| 208 | |
| 209 | auto shiftLenSq = shift.lengthSq(); |
| 210 | if ( shiftLenSq > maxShiftSq ) |
| 211 | maxShiftSq = shiftLenSq; |
| 212 | if ( shiftLenSq < maxAllowedShiftSq ) |
| 213 | { |
| 214 | mesh_.points[v] += shift; |
| 215 | if ( shiftLenSq < maxAllowedShiftSq * 1e-4f ) |
| 216 | return Processing::Continue; |
| 217 | } |
| 218 | else |
| 219 | { |
| 220 | mesh_.points[v] += shift.normalized() * maxAllowedShiftSq; |
| 221 | } |
| 222 | updatedVerts.set( v ); |
| 223 | return Processing::Continue; |
| 224 | } ); |
| 225 | } |
| 226 | |
| 227 | if ( updatedVerts.none() ) |
| 228 | break; // fast return on finish |
| 229 | |
| 230 | expand( mesh_.topology, updatedVerts, params_.relaxExpansion ); |
no test coverage detected