1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
|
//##########################################################################
//# #
//# CLOUDCOMPARE #
//# #
//# This program is free software; you can redistribute it and/or modify #
//# it under the terms of the GNU General Public License as published by #
//# the Free Software Foundation; version 2 or later of the License. #
//# #
//# This program is distributed in the hope that it will be useful, #
//# but WITHOUT ANY WARRANTY; without even the implied warranty of #
//# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
//# GNU General Public License for more details. #
//# #
//# COPYRIGHT: EDF R&D / TELECOM ParisTech (ENST-TSI) #
//# #
//##########################################################################
//Local
#include "ccGlobalShiftManager.h"
#include "ccShiftAndScaleCloudDlg.h"
//qCC_db
#include <ccHObject.h>
//System
#include <string.h>
#include <assert.h>
double ccGlobalShiftManager::MAX_COORDINATE_ABS_VALUE = 1.0e4;
double ccGlobalShiftManager::MAX_DIAGONAL_LENGTH = 1.0e6;
//semi-persistent settings
static std::vector<ccGlobalShiftManager::ShiftInfo> s_lastInfoBuffer;
void ccGlobalShiftManager::StoreShift(const CCVector3d& shift, double scale, bool preserve/*=true*/)
{
if (scale == 1.0 && shift.norm2d() == 0)
{
//default shift and scale are ignored
return;
}
for (const ccGlobalShiftManager::ShiftInfo& shiftInfo : s_lastInfoBuffer)
{
if (shiftInfo.scale == scale && (shiftInfo.shift - shift).norm2d() == 0)
{
//we already know this one
return;
}
}
ccGlobalShiftManager::ShiftInfo info("Last input");
info.scale = scale;
info.shift = shift;
info.preserve = preserve;
if (!s_lastInfoBuffer.empty())
{
info.name += QString(" (%1)").arg(s_lastInfoBuffer.size());
}
s_lastInfoBuffer.emplace_back(info);
}
bool ccGlobalShiftManager::GetLast(ShiftInfo& info)
{
if (s_lastInfoBuffer.empty())
{
return false;
}
info = s_lastInfoBuffer.back();
return true;
}
bool ccGlobalShiftManager::GetLast(std::vector<ShiftInfo>& infos)
{
try
{
infos = s_lastInfoBuffer;
}
catch (const std::bad_alloc&)
{
ccLog::Warning("[ccGlobalShiftManager::GetLast] Not enough memory");
return false;
}
return true;
}
bool ccGlobalShiftManager::NeedShift(const CCVector3d& P)
{
return NeedShift(P.x) || NeedShift(P.y) || NeedShift(P.z);
}
bool ccGlobalShiftManager::NeedShift(double d)
{
return fabs(d) >= MAX_COORDINATE_ABS_VALUE;
}
bool ccGlobalShiftManager::NeedRescale(double d)
{
return fabs(d) >= MAX_DIAGONAL_LENGTH;
}
bool ccGlobalShiftManager::Handle( const CCVector3d& P,
double diagonal,
Mode mode,
bool useInputCoordinatesShiftIfPossible,
CCVector3d& coordinatesShift,
bool* preserveCoordinateShift,
double* coordinatesScale,
bool* applyAll/*=0*/)
{
assert(diagonal >= 0);
if (preserveCoordinateShift)
{
*preserveCoordinateShift = true;
}
if (applyAll)
{
*applyAll = false;
}
//default scale
double scale = (coordinatesScale ? std::max(*coordinatesScale, ZERO_TOLERANCE) : 1.0);
bool needShift = NeedShift(P);
bool needRescale = NeedRescale(diagonal);
//if we can't display a dialog and no usable shift is specified, there's nothing we can do...
if (mode == NO_DIALOG && !useInputCoordinatesShiftIfPossible)
{
coordinatesShift = CCVector3d(0, 0, 0);
if (coordinatesScale)
{
*coordinatesScale = 1.0;
}
if (preserveCoordinateShift)
{
*preserveCoordinateShift = false;
}
if (needShift || needRescale)
{
ccLog::Warning("[ccGlobalShiftManager] Entity has very big coordinates: original accuracy may be lost! (you should apply a Global Shift or Scale)");
}
return false;
}
//is shift necessary?
if ( needShift || useInputCoordinatesShiftIfPossible || needRescale || mode == ALWAYS_DISPLAY_DIALOG )
{
//shift information already provided? (typically from a previous entity)
if (useInputCoordinatesShiftIfPossible && mode != ALWAYS_DISPLAY_DIALOG)
{
if ( mode == NO_DIALOG //either we are in non interactive mode (which means that shift is 'forced' by caller)
|| (!NeedShift(P*scale + coordinatesShift) && !NeedRescale(diagonal*scale)) //or we are in interactive mode and existing shift is pertinent
)
{
//have we already stored shift info?
if (mode == NO_DIALOG_AUTO_SHIFT && !s_lastInfoBuffer.empty())
{
//in "auto shift" mode, we may want to use it (to synchronize multiple clouds!)
for (const ccGlobalShiftManager::ShiftInfo& shiftInfo : s_lastInfoBuffer)
{
if (!NeedShift(P*shiftInfo.scale + shiftInfo.shift) && !NeedRescale(diagonal*shiftInfo.scale))
{
coordinatesShift = shiftInfo.shift;
if (coordinatesScale)
{
*coordinatesScale = shiftInfo.scale;
}
if (preserveCoordinateShift)
{
*preserveCoordinateShift = shiftInfo.preserve;
}
return true;
}
}
}
//save info for next time
if (coordinatesShift.norm2() != 0 || scale != 1.0)
{
StoreShift(coordinatesShift, scale);
}
//user should use the provided shift information
return true;
}
//--> otherwise we (should) ask for a better one
}
//let's deduce the right values (AUTO mode)
if (mode == NO_DIALOG_AUTO_SHIFT)
{
//guess best shift & scale info from input point/diagonal
coordinatesShift = CCVector3d(0, 0, 0);
scale = 1.0;
if (needShift)
{
coordinatesShift = BestShift(P);
if (preserveCoordinateShift)
{
*preserveCoordinateShift = true;
}
}
if (coordinatesScale && needRescale)
{
*coordinatesScale = BestScale(diagonal);
if (preserveCoordinateShift)
{
*preserveCoordinateShift = true;
}
//save info for next time
scale = *coordinatesScale;
}
//save info for next time
StoreShift(coordinatesShift, 1.0, true);
return true;
}
//otherwise let's ask the user for those values
ccShiftAndScaleCloudDlg sasDlg(P, diagonal);
if (!applyAll)
{
sasDlg.showApplyAllButton(false);
}
if (!coordinatesScale)
{
sasDlg.showScaleItems(false);
}
scale = 1.0;
CCVector3d shift(0, 0, 0);
if (useInputCoordinatesShiftIfPossible)
{
//shift on load already provided? (typically from a previous file)
shift = coordinatesShift;
if (coordinatesScale)
{
scale = *coordinatesScale;
}
if (mode != ALWAYS_DISPLAY_DIALOG)
{
sasDlg.showWarning(true); //if we are here, it means that the provided shift isn't concordant
}
}
else
{
//guess best shift & scale info from input point/diagonal
if (needShift)
{
shift = BestShift(P);
}
if (needRescale)
{
scale = BestScale(diagonal);
}
}
//add "suggested" entry
int index = sasDlg.addShiftInfo(ShiftInfo("Suggested", shift, scale));
sasDlg.setCurrentProfile(index);
//add "last" entry (if available)
if (!s_lastInfoBuffer.empty())
{
sasDlg.addShiftInfo(s_lastInfoBuffer);
//use the very last one for preserve or not preserve
sasDlg.setPreserveShiftOnSave(s_lastInfoBuffer.back().preserve);
}
sasDlg.showPreserveShiftOnSave(preserveCoordinateShift != nullptr);
//add entries from file (if any)
sasDlg.addFileInfo();
//automatically make the first available shift that works
//(different than the suggested one) active
{
for (size_t i = static_cast<size_t>(std::max(0, index + 1)); i < sasDlg.infoCount(); ++i)
{
ShiftInfo info;
if (sasDlg.getInfo(i, info))
{
//check if they work
if ( !NeedShift((CCVector3d(P) + info.shift) * info.scale )
&& !NeedRescale(diagonal*info.scale) )
{
sasDlg.setCurrentProfile(static_cast<int>(i));
break;
}
}
}
}
sasDlg.showTitle(needShift || needRescale);
if (sasDlg.exec())
{
//save info for next time
StoreShift(sasDlg.getShift(), sasDlg.getScale(), sasDlg.preserveShiftOnSave());
coordinatesShift = sasDlg.getShift();
if (coordinatesScale)
{
*coordinatesScale = sasDlg.getScale();
}
if (preserveCoordinateShift)
{
*preserveCoordinateShift = sasDlg.preserveShiftOnSave();
}
if (applyAll)
{
*applyAll = sasDlg.applyAll();
}
return true;
}
}
coordinatesShift = CCVector3d(0, 0, 0);
if (coordinatesScale)
{
*coordinatesScale = 1.0;
}
if (preserveCoordinateShift)
{
*preserveCoordinateShift = false;
}
return false;
}
CCVector3d ccGlobalShiftManager::BestShift(const CCVector3d& P)
{
if (!NeedShift(P))
{
return CCVector3d(0, 0, 0);
}
CCVector3d shift( fabs(P[0]) >= MAX_COORDINATE_ABS_VALUE ? -P[0] : 0,
fabs(P[1]) >= MAX_COORDINATE_ABS_VALUE ? -P[1] : 0,
fabs(P[2]) >= MAX_COORDINATE_ABS_VALUE ? -P[2] : 0 );
//round-off to the nearest hundred
shift.x = static_cast<int>(shift.x / 100) * 100.0;
shift.y = static_cast<int>(shift.y / 100) * 100.0;
shift.z = static_cast<int>(shift.z / 100) * 100.0;
return shift;
}
double ccGlobalShiftManager::BestScale(double d)
{
return d < MAX_DIAGONAL_LENGTH ? 1.0 : pow(10.0, -static_cast<double>(ceil(log(d / MAX_DIAGONAL_LENGTH))));
}
|