scene_instance.h 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // ======================================================================== //
  2. // Copyright 2009-2017 Intel Corporation //
  3. // //
  4. // Licensed under the Apache License, Version 2.0 (the "License"); //
  5. // you may not use this file except in compliance with the License. //
  6. // You may obtain a copy of the License at //
  7. // //
  8. // http://www.apache.org/licenses/LICENSE-2.0 //
  9. // //
  10. // Unless required by applicable law or agreed to in writing, software //
  11. // distributed under the License is distributed on an "AS IS" BASIS, //
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
  13. // See the License for the specific language governing permissions and //
  14. // limitations under the License. //
  15. // ======================================================================== //
  16. #pragma once
  17. #include "accelset.h"
  18. namespace embree
  19. {
  20. class InstanceFactory
  21. {
  22. public:
  23. InstanceFactory(int features);
  24. DEFINE_SYMBOL2(RTCBoundsFunc3,InstanceBoundsFunc);
  25. DEFINE_SYMBOL2(AccelSet::Intersector1,InstanceIntersector1);
  26. DEFINE_SYMBOL2(AccelSet::Intersector4,InstanceIntersector4);
  27. DEFINE_SYMBOL2(AccelSet::Intersector8,InstanceIntersector8);
  28. DEFINE_SYMBOL2(AccelSet::Intersector16,InstanceIntersector16);
  29. DEFINE_SYMBOL2(AccelSet::Intersector1M,InstanceIntersector1M);
  30. };
  31. /*! Instanced acceleration structure */
  32. struct Instance : public AccelSet
  33. {
  34. ALIGNED_STRUCT;
  35. public:
  36. static Instance* create (Scene* parent, Scene* object, size_t numTimeSteps) {
  37. return ::new (alignedMalloc(sizeof(Instance)+(numTimeSteps-1)*sizeof(AffineSpace3fa))) Instance(parent,object,numTimeSteps);
  38. }
  39. private:
  40. Instance (Scene* parent, Scene* object, size_t numTimeSteps);
  41. public:
  42. virtual void setTransform(const AffineSpace3fa& local2world, size_t timeStep);
  43. virtual void setMask (unsigned mask);
  44. virtual void build() {}
  45. public:
  46. __forceinline AffineSpace3fa getWorld2Local() const {
  47. return world2local0;
  48. }
  49. __forceinline AffineSpace3fa getWorld2Local(float t) const
  50. {
  51. float ftime;
  52. const size_t itime = getTimeSegment(t, fnumTimeSegments, ftime);
  53. return rcp(lerp(local2world[itime+0],local2world[itime+1],ftime));
  54. }
  55. template<int K>
  56. __forceinline AffineSpaceT<LinearSpace3<Vec3<vfloat<K>>>> getWorld2Local(const vbool<K>& valid, const vfloat<K>& t) const
  57. {
  58. typedef AffineSpaceT<LinearSpace3<Vec3<vfloat<K>>>> AffineSpace3vfK;
  59. vfloat<K> ftime;
  60. const vint<K> itime_k = getTimeSegment(t, vfloat<K>(fnumTimeSegments), ftime);
  61. assert(any(valid));
  62. const size_t index = __bsf(movemask(valid));
  63. const int itime = itime_k[index];
  64. const vfloat<K> t0 = vfloat<K>(1.0f)-ftime, t1 = ftime;
  65. if (likely(all(valid,itime_k == vint<K>(itime)))) {
  66. return rcp(t0*AffineSpace3vfK(local2world[itime+0])+t1*AffineSpace3vfK(local2world[itime+1]));
  67. }
  68. else {
  69. AffineSpace3vfK space0,space1;
  70. foreach_unique(valid,itime_k,[&] (const vbool<K>& valid, int itime) {
  71. space0 = select(valid,AffineSpace3vfK(local2world[itime+0]),space0);
  72. space1 = select(valid,AffineSpace3vfK(local2world[itime+1]),space1);
  73. });
  74. return rcp(t0*space0+t1*space1);
  75. }
  76. }
  77. public:
  78. Scene* object; //!< pointer to instanced acceleration structure
  79. AffineSpace3fa world2local0; //!< transformation from world space to local space for timestep 0
  80. AffineSpace3fa local2world[1]; //!< transformation from local space to world space for each timestep
  81. };
  82. }