| 115 | } |
| 116 | |
| 117 | Expected<Mesh> mcOffsetMesh( const MeshPart& mp, float offset, |
| 118 | const OffsetParameters& params, Vector<VoxelId, FaceId> * outMap ) |
| 119 | { |
| 120 | MR_TIMER; |
| 121 | |
| 122 | if ( params.voxelSize <= 0 ) |
| 123 | { |
| 124 | assert( false ); |
| 125 | return unexpected( "invalid voxelSize value" ); |
| 126 | } |
| 127 | |
| 128 | if ( params.signDetectionMode == SignDetectionMode::OpenVDB ) |
| 129 | { |
| 130 | auto offsetInVoxels = offset / params.voxelSize; |
| 131 | auto voxelRes = meshToLevelSet( |
| 132 | mp, |
| 133 | AffineXf3f(), |
| 134 | Vector3f::diagonal( params.voxelSize ), |
| 135 | std::abs( offsetInVoxels ) + 2, |
| 136 | subprogress( params.callBack, 0.0f, 0.4f ) |
| 137 | ); |
| 138 | if ( !voxelRes ) |
| 139 | return unexpectedOperationCanceled(); |
| 140 | |
| 141 | VdbVolume volume = floatGridToVdbVolume( std::move( voxelRes ) ); |
| 142 | volume.voxelSize = Vector3f::diagonal( params.voxelSize ); |
| 143 | |
| 144 | MarchingCubesParams vmParams; |
| 145 | vmParams.iso = offsetInVoxels; |
| 146 | vmParams.lessInside = true; |
| 147 | vmParams.cb = subprogress( params.callBack, 0.4f, 1.0f ); |
| 148 | vmParams.outVoxelPerFaceMap = outMap; |
| 149 | vmParams.freeVolume = [&volume] |
| 150 | { |
| 151 | Timer t( "~FloatGrid" ); |
| 152 | volume.data.reset(); |
| 153 | }; |
| 154 | return marchingCubes( volume, vmParams ); |
| 155 | } |
| 156 | |
| 157 | const auto isHoleWindingRule = params.signDetectionMode == SignDetectionMode::HoleWindingRule; |
| 158 | const auto isFuncVolume = params.memoryEfficient && !( isHoleWindingRule && params.fwn ); |
| 159 | |
| 160 | const auto absOffset = std::abs( offset ); |
| 161 | const auto box = mp.mesh.computeBoundingBox( mp.region ).expanded( Vector3f::diagonal( absOffset ) ); |
| 162 | const auto [origin, dimensions] = calcOriginAndDimensions( box, params.voxelSize ); |
| 163 | |
| 164 | DistanceVolumeParams vol { |
| 165 | .origin = origin, |
| 166 | .cb = subprogress( params.callBack, 0.0f, 0.4f ), |
| 167 | .voxelSize = Vector3f::diagonal( params.voxelSize ), |
| 168 | .dimensions = dimensions, |
| 169 | }; |
| 170 | |
| 171 | DistanceToMeshOptions dist { |
| 172 | // we multiply by 1.001f to be sure not to have rounding errors (which may lead to unexpected NaN values ) |
| 173 | .minDistSq = sqr( std::max( absOffset - 1.001f * params.voxelSize, 0.0f ) ), |
| 174 | .maxDistSq = sqr( absOffset + 1.001f * params.voxelSize ), |