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 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
|
/*******************************************************************************
* photonsortingtask.cpp
*
* This module implements Photon Mapping.
*
* Author: Nathan Kopp
*
* ---------------------------------------------------------------------------
* Persistence of Vision Ray Tracer ('POV-Ray') version 3.7.
* Copyright 1991-2013 Persistence of Vision Raytracer Pty. Ltd.
*
* POV-Ray is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* POV-Ray 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ---------------------------------------------------------------------------
* POV-Ray is based on the popular DKB raytracer version 2.12.
* DKBTrace was originally written by David K. Buck.
* DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
* ---------------------------------------------------------------------------
* $File: //depot/public/povray/3.x/source/backend/lighting/photonsortingtask.cpp $
* $Revision: #1 $
* $Change: 6069 $
* $DateTime: 2013/11/06 11:59:40 $
* $Author: chrisc $
*******************************************************************************/
// frame.h must always be the first POV file included (pulls in platform config)
#include "backend/frame.h"
#include "base/povms.h"
#include "base/povmsgid.h"
#include "backend/math/vector.h"
#include "backend/math/matrices.h"
#include "backend/scene/objects.h"
#include "backend/shape/csg.h"
#include "backend/support/octree.h"
#include "backend/bounding/bbox.h"
#include "backend/scene/threaddata.h"
#include "backend/scene/scene.h"
#include "backend/scene/view.h"
#include "backend/support/msgutil.h"
#include "backend/lighting/point.h"
#include "backend/lighting/photonsortingtask.h"
#include "backend/lighting/photonshootingstrategy.h"
#include "lightgrp.h"
#include <algorithm>
// this must be the last file included
#include "base/povdebug.h"
namespace pov
{
/*
If you pass a NULL for the "strategy" parameter, then this will
load the photon map from a file.
Otherwise, it will:
1) merge
2) sort
3) compute gather options
4) clean up memory (delete the non-merged maps and delete the strategy)
*/
PhotonSortingTask::PhotonSortingTask(ViewData *vd, const vector<PhotonMap*>& surfaceMaps, const vector<PhotonMap*>& mediaMaps, PhotonShootingStrategy* strategy) :
RenderTask(vd),
surfaceMaps(surfaceMaps),
mediaMaps(mediaMaps),
strategy(strategy),
messageFactory(10, 370, "Photon", vd->GetSceneData()->backendAddress, vd->GetSceneData()->frontendAddress, vd->GetSceneData()->sceneId, 0), // TODO FIXME - Values need to come from the correct place!
cooperate(*this)
{
}
PhotonSortingTask::~PhotonSortingTask()
{
}
void PhotonSortingTask::SendProgress(void)
{
#if 0
// TODO FIXME PHOTONS
// for now, we won't send this, as it can be confusing on the front-end due to out-of-order delivery from multiple threads.
// we need to create a new progress message for sorting.
if (timer.ElapsedRealTime() > 1000)
{
timer.Reset();
POVMS_Object obj(kPOVObjectClass_PhotonProgress);
obj.SetInt(kPOVAttrib_CurrentPhotonCount, (GetSceneData()->surfacePhotonMap.numPhotons + GetSceneData()->mediaPhotonMap.numPhotons));
RenderBackend::SendViewOutput(GetViewData()->GetViewId(), GetSceneData()->frontendAddress, kPOVMsgIdent_Progress, obj);
}
#endif
}
void PhotonSortingTask::Run()
{
// quit right away if photons not enabled
if (!GetSceneData()->photonSettings.photonsEnabled) return;
Cooperate();
if(strategy!=NULL)
{
delete strategy;
sortPhotonMap();
}
else
{
if (!this->load())
messageFactory.Error(POV_EXCEPTION_STRING("Failed to load photon map from disk"), "Could not load photon map (%s)",GetSceneData()->photonSettings.fileName);
// set photon options automatically
if (GetSceneData()->surfacePhotonMap.numPhotons>0)
GetSceneData()->surfacePhotonMap.setGatherOptions(GetSceneData()->photonSettings,false);
if (GetSceneData()->mediaPhotonMap.numPhotons>0)
GetSceneData()->mediaPhotonMap.setGatherOptions(GetSceneData()->photonSettings,true);
}
// good idea to make sure all warnings and errors arrive frontend now [trf]
SendProgress();
Cooperate();
}
void PhotonSortingTask::Stopped()
{
// nothing to do for now [trf]
}
void PhotonSortingTask::Finish()
{
GetViewDataPtr()->timeType = SceneThreadData::kPhotonTime;
GetViewDataPtr()->realTime = ConsumedRealTime();
GetViewDataPtr()->cpuTime = ConsumedCPUTime();
}
void PhotonSortingTask::sortPhotonMap()
{
vector<PhotonMap*>::iterator mapIter;
for(mapIter = surfaceMaps.begin(); mapIter != surfaceMaps.end(); mapIter++)
{
GetSceneData()->surfacePhotonMap.mergeMap(*mapIter);
//delete (*mapIter);
}
for(mapIter = mediaMaps.begin(); mapIter != mediaMaps.end(); mapIter++)
{
GetSceneData()->mediaPhotonMap.mergeMap(*mapIter);
//delete (*mapIter);
}
/* now actually build the kd-tree by sorting the array of photons */
if (GetSceneData()->surfacePhotonMap.numPhotons>0)
{
//povwin::WIN32_DEBUG_FILE_OUTPUT("\n\nsurfacePhotonMap.buildTree about to be called\n");
GetSceneData()->surfacePhotonMap.buildTree();
GetSceneData()->surfacePhotonMap.setGatherOptions(GetSceneData()->photonSettings,false);
// povwin::WIN32_DEBUG_FILE_OUTPUT("gatherNumSteps: %d\n",GetSceneData()->surfacePhotonMap.gatherNumSteps);
// povwin::WIN32_DEBUG_FILE_OUTPUT("gatherRadStep: %lf\n",GetSceneData()->surfacePhotonMap.gatherRadStep);
// povwin::WIN32_DEBUG_FILE_OUTPUT("minGatherRad: %lf\n",GetSceneData()->surfacePhotonMap.minGatherRad);
// povwin::WIN32_DEBUG_FILE_OUTPUT("minGatherRadMult: %lf\n",GetSceneData()->surfacePhotonMap.minGatherRadMult);
// povwin::WIN32_DEBUG_FILE_OUTPUT("numBlocks: %d\n",GetSceneData()->surfacePhotonMap.numBlocks);
// povwin::WIN32_DEBUG_FILE_OUTPUT("numPhotons: %d\n",GetSceneData()->surfacePhotonMap.numPhotons);
}
#ifdef GLOBAL_PHOTONS
/* ----------- global photons ------------- */
if (globalPhotonMap.numPhotons>0)
{
globalPhotonMap.buildTree();
globalPhotonMap.setGatherOptions(false);
}
#endif
/* ----------- media photons ------------- */
if (GetSceneData()->mediaPhotonMap.numPhotons>0)
{
GetSceneData()->mediaPhotonMap.buildTree();
GetSceneData()->mediaPhotonMap.setGatherOptions(GetSceneData()->photonSettings,true);
}
if (GetSceneData()->surfacePhotonMap.numPhotons+
#ifdef GLOBAL_PHOTONS
globalPhotonMap.numPhotons+
#endif
GetSceneData()->mediaPhotonMap.numPhotons > 0)
{
/* should we load the photon map now that it is built? */
if (GetSceneData()->photonSettings.fileName && !GetSceneData()->photonSettings.loadFile)
{
/* status bar for user */
// Send_Progress("Saving Photon Maps", PROGRESS_SAVING_PHOTON_MAPS);
if (!this->save())
messageFactory.Warning(0,"Could not save photon map.");
}
}
else
{
if (GetSceneData()->photonSettings.fileName && !GetSceneData()->photonSettings.loadFile)
messageFactory.Warning(0,"Could not save photon map - no photons!");
}
}
/* savePhotonMap()
Saves the caustic photon map to a file.
Preconditions:
InitBacktraceEverything was called
the photon map has been built and balanced
photonSettings.fileName contains the filename to save
Postconditions:
Returns 1 if success, 0 if failure.
If success, the photon map has been written to the file.
*/
int PhotonSortingTask::save()
{
Photon *ph;
FILE *f;
int i;
size_t err;
int numph;
f = fopen(GetSceneData()->photonSettings.fileName, "wb");
if (!f) return 0;
/* caustic photons */
numph = GetSceneData()->surfacePhotonMap.numPhotons;
fwrite(&numph, sizeof(numph),1,f);
if (numph>0 && GetSceneData()->surfacePhotonMap.head)
{
for(i=0; i<numph; i++)
{
ph = &(PHOTON_AMF(GetSceneData()->surfacePhotonMap.head, i));
err = fwrite(ph, sizeof(Photon), 1, f);
if (err<=0)
{
/* fwrite returned an error! */
fclose(f);
return 0;
}
}
}
else
{
messageFactory.PossibleError("Photon map for surface is empty.");
}
#ifdef GLOBAL_PHOTONS
/* global photons */
numph = globalPhotonMap.numPhotons;
fwrite(&numph, sizeof(numph),1,f);
if (numph>0 && globalPhotonMap.head)
{
for(i=0; i<numph; i++)
{
ph = &(PHOTON_AMF(globalPhotonMap.head, i));
err = fwrite(ph, sizeof(Photon), 1, f);
if (err<=0)
{
/* fwrite returned an error! */
fclose(f);
return 0;
}
}
}
else
{
messageFactory.PossibleError("Global photon map is empty.");
}
#endif
/* media photons */
numph = GetSceneData()->mediaPhotonMap.numPhotons;
fwrite(&numph, sizeof(numph),1,f);
if (numph>0 && GetSceneData()->mediaPhotonMap.head)
{
for(i=0; i<numph; i++)
{
ph = &(PHOTON_AMF(GetSceneData()->mediaPhotonMap.head, i));
err = fwrite(ph, sizeof(Photon), 1, f);
if (err<=0)
{
/* fwrite returned an error! */
fclose(f);
return 0;
}
}
}
else
{
messageFactory.PossibleError("Photon map for media is empty.");
}
fclose(f);
return true;
}
/* loadPhotonMap()
Loads the caustic photon map from a file.
Preconditions:
InitBacktraceEverything was called
the photon map is empty
renderer->sceneData->photonSettings.fileName contains the filename to load
Postconditions:
Returns 1 if success, 0 if failure.
If success, the photon map has been loaded from the file.
If failure then the render should stop with an error
*/
int PhotonSortingTask::load()
{
int i;
size_t err;
Photon *ph;
FILE *f;
int numph;
if (!GetSceneData()->photonSettings.photonsEnabled) return 0;
messageFactory.Warning(0,"Starting the load of photon file %s\n",GetSceneData()->photonSettings.fileName);
f = fopen(GetSceneData()->photonSettings.fileName, "rb");
if (!f) return 0;
fread(&numph, sizeof(numph),1,f);
for(i=0; i<numph; i++)
{
ph = GetSceneData()->surfacePhotonMap.AllocatePhoton();
err = fread(ph, sizeof(Photon), 1, f);
if (err<=0)
{
/* fread returned an error! */
fclose(f);
return 0;
}
}
if (!feof(f)) /* for backwards file format compatibility */
{
#ifdef GLOBAL_PHOTONS
/* global photons */
fread(&numph, sizeof(numph),1,f);
for(i=0; i<numph; i++)
{
ph = GetSceneData()->globalPhotonMap.AllocatePhoton();
err = fread(ph, sizeof(Photon), 1, f);
if (err<=0)
{
/* fread returned an error! */
fclose(f);
return 0;
}
}
#endif
/* media photons */
fread(&numph, sizeof(numph),1,f);
for(i=0; i<numph; i++)
{
ph = GetSceneData()->mediaPhotonMap.AllocatePhoton();
err = fread(ph, sizeof(Photon), 1, f);
if (err<=0)
{
/* fread returned an error! */
fclose(f);
return 0;
}
}
}
fclose(f);
return true;
}
}
|