24 #ifndef COM_DAFER45_TBTK_RAY_TRACER
25 #define COM_DAFER45_TBTK_RAY_TRACER
27 #include "TBTK/FieldWrapper.h"
37 #include <initializer_list>
39 #include <opencv2/core/core.hpp>
40 #include <opencv2/highgui/highgui.hpp>
50 RayTracer(
const RayTracer &rayTracer);
53 RayTracer(RayTracer &&rayTracer);
59 RayTracer& operator=(
const RayTracer &rhs);
62 RayTracer& operator=(RayTracer &&rhs);
65 void setCameraPosition(
const Vector3d &cameraPosition);
68 void setCameraPosition(std::initializer_list<double> cameraPosition);
71 void setFocus(
const Vector3d &focus);
74 void setFocus(std::initializer_list<double> focus);
77 void setUp(
const Vector3d &up);
80 void setUp(std::initializer_list<double> up);
83 void setWidth(
unsigned int width);
86 void setHeight(
unsigned int height);
89 void setStateRadius(
double stateRadius);
92 void setNumDeflections(
unsigned int numDeflections);
95 unsigned int getNumDeflections()
const;
98 void setRayLength(
double rayLength);
101 double getRayLength()
const;
104 void setNumRaySegments(
unsigned int numRaySegments);
107 unsigned int getNumRaySegments()
const;
110 void plot(
const Model& model,
const Property::Density &density);
115 const Property::Magnetization &magnetization
121 const Property::WaveFunctions &waveFunctions,
127 Field<std::complex<double>,
double> &field
132 const std::vector<const FieldWrapper*> &fields
136 void interactivePlot(
138 const Property::LDOS &ldos,
140 unsigned int windowSize = 51
144 void save(std::string filename);
162 double ambient, diffusive, emissive, specular;
165 static constexpr
double DEFAULT_AMBIENT = 1;
166 static constexpr
double DEFAULT_DIFFUSIVE = 0.5;
167 static constexpr
double DEFAULT_EMISSIVE = 0;
168 static constexpr
double DEFAULT_SPECULAR = 0.1;
181 void setCameraPosition(
const Vector3d &cameraPosition);
184 void setCameraPosition(std::initializer_list<double> cameraPosition);
187 const Vector3d& getCameraPosition()
const;
190 void setFocus(
const Vector3d &focus);
193 void setFocus(std::initializer_list<double> focus);
196 const Vector3d& getFocus()
const;
199 void setUp(
const Vector3d &up);
202 void setUp(std::initializer_list<double> up);
205 const Vector3d& getUp()
const;
208 void setWidth(
unsigned int width);
211 unsigned int getWidth()
const;
214 void setHeight(
unsigned int height);
217 unsigned int getHeight()
const;
220 void setStateRadius(
double stateRadius);
223 double getStateRadius()
const;
226 void setNumDeflections(
unsigned int numDeflections);
229 unsigned int getNumDeflections()
const;
232 void setRayLength(
double rayLength);
235 double getRayLength()
const;
238 void setNumRaySegments(
unsigned int numRaySegments);
241 unsigned int getNumRaySegments()
const;
244 Vector3d cameraPosition;
263 unsigned int numDeflections;
269 unsigned int numRaySegments;
272 RenderContext renderContext;
278 HitDescriptor(
const RenderContext &renderContext);
281 HitDescriptor(
const HitDescriptor &hitDescriptor);
284 HitDescriptor(HitDescriptor &&hitDescriptor);
290 HitDescriptor& operator=(
const HitDescriptor &rhs);
293 HitDescriptor& operator=(HitDescriptor &&rhs);
296 void setRaySource(
const Vector3d &raySource);
299 const Vector3d& getRaySource()
const;
302 void setRayDirection(
const Vector3d &rayDirection);
305 const Vector3d& getRayDirection()
const;
308 void setIndex(
const Index &index);
311 const Index& getIndex()
const;
314 void setCoordinate(
const Vector3d coordainte);
317 const Vector3d& getCoordinate()
const;
320 const Vector3d& getDirectionFromObject();
323 const Vector3d& getImpactPosition();
326 const RenderContext *renderContext;
332 Vector3d rayDirection;
341 Vector3d *directionFromObject;
344 Vector3d *impactPosition;
350 RenderResult(
unsigned int width,
unsigned int height);
353 RenderResult(
const RenderResult &renderResult);
356 RenderResult(RenderResult &&renderResult);
362 RenderResult& operator=(
const RenderResult &rhs);
365 RenderResult& operator=(RenderResult &&rhs);
368 cv::Mat& getCanvas();
371 const cv::Mat& getCanvas()
const;
374 std::vector<HitDescriptor>** getHitDescriptors();
377 unsigned int width, height;
384 std::vector<HitDescriptor> **hitDescriptors;
387 RenderResult *renderResult;
391 const IndexDescriptor &indexDescriptor,
393 const std::vector<const FieldWrapper*> &fields,
394 std::function<Material(HitDescriptor &hitDescriptor)> &&lambdaColorPicker,
395 std::function<
void(cv::Mat &canvas,
const Index &index)> &&lambdaInteractive = {}
400 const std::vector<Vector3d> &coordinates,
401 const Vector3d &raySource,
402 const Vector3d &rayDirection,
403 const IndexTree &indexTree,
404 const std::vector<const FieldWrapper*> &fields,
405 std::vector<HitDescriptor> &hitDescriptors,
406 std::function<Material(HitDescriptor &hitDescriptor)> lambdaColorPicker,
407 unsigned int deflections = 0
412 const std::vector<const FieldWrapper*> &fields,
413 const Vector3d &raySource,
414 const Vector3d &rayDirection
418 cv::Mat getCharImage()
const;
432 )> &&lambdaOnMouseChange
436 static bool unlock(
const RayTracer *owner);
439 static void onMouseChange(
448 static bool isLocked;
451 static RayTracer *owner;
454 static std::function<void(
int event,
int x,
int y,
int flags,
void *userData)> &&lambdaOnMouseChange;
458 inline void RayTracer::setCameraPosition(
const Vector3d &cameraPosition){
459 renderContext.setCameraPosition(cameraPosition);
462 inline void RayTracer::setCameraPosition(
463 std::initializer_list<double> cameraPosition
465 renderContext.setCameraPosition(cameraPosition);
468 inline void RayTracer::setFocus(
const Vector3d &focus){
469 renderContext.setFocus(focus);
472 inline void RayTracer::setFocus(std::initializer_list<double> focus){
473 renderContext.setFocus(focus);
476 inline void RayTracer::setUp(
const Vector3d &up){
477 renderContext.setUp(up);
480 inline void RayTracer::setWidth(
unsigned int width){
481 renderContext.setWidth(width);
484 inline void RayTracer::setHeight(
unsigned int height){
485 renderContext.setHeight(height);
488 inline void RayTracer::setUp(std::initializer_list<double> up){
489 renderContext.setUp(up);
492 inline void RayTracer::setStateRadius(
double stateRadius){
493 renderContext.setStateRadius(stateRadius);
496 inline void RayTracer::setNumDeflections(
unsigned int numDeflections){
497 renderContext.setNumDeflections(numDeflections);
500 inline unsigned int RayTracer::getNumDeflections()
const{
501 return renderContext.getNumDeflections();
504 inline void RayTracer::setRayLength(
double rayLength){
505 renderContext.setRayLength(rayLength);
508 inline double RayTracer::getRayLength()
const{
509 return renderContext.getRayLength();
512 inline void RayTracer::setNumRaySegments(
unsigned int numRaySegments){
513 renderContext.setNumRaySegments(numRaySegments);
516 inline unsigned int RayTracer::getNumRaySegments()
const{
517 return renderContext.getNumRaySegments();
540 inline cv::Mat& RayTracer::RenderResult::getCanvas(){
544 inline const cv::Mat& RayTracer::RenderResult::getCanvas()
const{
548 inline std::vector<RayTracer::HitDescriptor>** RayTracer::RenderResult::getHitDescriptors(){
549 return hitDescriptors;
552 inline RayTracer::Material::Material(){
556 ambient = DEFAULT_AMBIENT;
557 diffusive = DEFAULT_AMBIENT;
558 emissive = DEFAULT_EMISSIVE;
559 specular = DEFAULT_SPECULAR;
562 inline void RayTracer::RenderContext::setCameraPosition(
563 const Vector3d &cameraPosition
565 this->cameraPosition = cameraPosition;
568 inline void RayTracer::RenderContext::setCameraPosition(
569 std::initializer_list<double> cameraPosition
572 cameraPosition.size() == 3,
573 "RayTracer::setCameraPosition()",
574 "Camera position can only have three coordinates.",
578 this->cameraPosition.x = *(cameraPosition.begin() + 0);
579 this->cameraPosition.y = *(cameraPosition.begin() + 1);
580 this->cameraPosition.z = *(cameraPosition.begin() + 2);
583 inline const Vector3d& RayTracer::RenderContext::getCameraPosition()
const{
584 return cameraPosition;
587 inline void RayTracer::RenderContext::setFocus(
const Vector3d &focus){
591 inline void RayTracer::RenderContext::setFocus(
592 std::initializer_list<double> focus
596 "RayTracer::setFocus()",
597 "Focus can only have three coordinates.",
601 this->focus.x = *(focus.begin() + 0);
602 this->focus.y = *(focus.begin() + 1);
603 this->focus.z = *(focus.begin() + 2);
606 inline const Vector3d& RayTracer::RenderContext::getFocus()
const{
610 inline void RayTracer::RenderContext::setUp(
const Vector3d &up){
614 inline void RayTracer::RenderContext::setUp(
615 std::initializer_list<double> up
619 "RayTracer::setCameraPosition()",
620 "Camera position can only have three coordinates.",
624 this->up.x = *(up.begin() + 0);
625 this->up.y = *(up.begin() + 1);
626 this->up.z = *(up.begin() + 2);
629 inline const Vector3d& RayTracer::RenderContext::getUp()
const{
633 inline void RayTracer::RenderContext::setWidth(
unsigned int width){
637 inline unsigned int RayTracer::RenderContext::getWidth()
const{
641 inline void RayTracer::RenderContext::setHeight(
unsigned int height){
642 this->height = height;
645 inline unsigned int RayTracer::RenderContext::getHeight()
const{
649 inline void RayTracer::RenderContext::setStateRadius(
double stateRadius){
650 this->stateRadius = stateRadius;
653 inline double RayTracer::RenderContext::getStateRadius()
const{
657 inline void RayTracer::RenderContext::setNumDeflections(
658 unsigned int numDeflections
660 this->numDeflections = numDeflections;
663 inline unsigned int RayTracer::RenderContext::getNumDeflections()
const{
664 return numDeflections;
667 inline void RayTracer::RenderContext::setRayLength(
double rayLength){
668 this->rayLength = rayLength;
671 inline double RayTracer::RenderContext::getRayLength()
const{
675 inline void RayTracer::RenderContext::setNumRaySegments(
676 unsigned int numRaySegments
678 this->numRaySegments = numRaySegments;
681 inline unsigned int RayTracer::RenderContext::getNumRaySegments()
const{
682 return numRaySegments;
685 inline void RayTracer::HitDescriptor::setRaySource(
686 const Vector3d &raySource
688 this->raySource = raySource;
691 inline const Vector3d& RayTracer::HitDescriptor::getRaySource()
const{
692 return this->raySource;
695 inline void RayTracer::HitDescriptor::setRayDirection(
696 const Vector3d &rayDirection
698 this->rayDirection = rayDirection;
701 inline const Vector3d& RayTracer::HitDescriptor::getRayDirection()
const{
702 return this->rayDirection;
705 inline void RayTracer::HitDescriptor::setIndex(
const Index &index){
709 inline const Index& RayTracer::HitDescriptor::getIndex()
const{
713 inline void RayTracer::HitDescriptor::setCoordinate(
const Vector3d coordinate){
714 this->coordinate = coordinate;
717 inline const Vector3d& RayTracer::HitDescriptor::getCoordinate()
const{
721 inline bool RayTracer::EventHandler::lock(
729 )> &&lambdaOnMouseChange
736 EventHandler::owner = owner;
737 EventHandler::lambdaOnMouseChange = lambdaOnMouseChange;
742 inline bool RayTracer::EventHandler::unlock(
const RayTracer *owner){
743 if(EventHandler::owner == owner){
744 EventHandler::owner =
nullptr;
745 EventHandler::isLocked =
false;
746 EventHandler::lambdaOnMouseChange =
nullptr;
754 inline void RayTracer::EventHandler::onMouseChange(
761 if(lambdaOnMouseChange){
762 lambdaOnMouseChange(event, x, y, flags, userData);
766 "RayTracer::EventHandler::onMouseChange()",
767 "lambdaOnMouseChange is nullptr.",
768 "This should never happen, contact the developer."